00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <stdio.h>
00038 #ifndef _WIN32
00039 #include <unistd.h>
00040 #include "config.h"
00041 #else
00042 #include <windows.h>
00043 #include "winconfig.h"
00044 #endif
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include "include/cmd_prompt.h"
00048 #include "include/commands.h"
00049 #include "config.h"
00050
00051 #ifdef HAVE_LIBREADLINE
00052 #include <readline/readline.h>
00053 #include <readline/history.h>
00054 #endif
00055
00056
00057
00058
00059 #ifdef HAVE_LIBREADLINE
00060 int initialize_readline (void);
00061 char ** command_completion (char* text, int start, int end);
00062 char * command_generator (char* text, int state);
00063
00064 int initialize_readline (void)
00065 {
00066 rl_readline_name = "$";
00067 rl_attempted_completion_function = (CPPFunction *)command_completion;
00068 return 0;
00069 }
00070
00071 char ** command_completion (char* text, int start, int end)
00072 {
00073 char **matches;
00074 matches = (char **)NULL;
00075
00076 if (start == 0)
00077 matches = (char**)completion_matches (text, command_generator);
00078
00079 return (matches);
00080 }
00081
00082 char * command_generator (char* text, int state)
00083 {
00084 static int list_index, len;
00085 char *name;
00086
00087 if (!state)
00088 {
00089 list_index = 0;
00090 len = strlen (text);
00091 }
00092
00093 while ((name = command_cmds[list_index]))
00094 {
00095 list_index++;
00096
00097 if (strncmp (name, text, len) == 0)
00098 return (strdup(name));
00099 }
00100
00101 return ((char *)NULL);
00102 }
00103
00104
00105
00106
00107 char* stripwhite (char* string)
00108 {
00109 register char *s, *t;
00110 for (s = string; whitespace (*s); s++);
00111 if (*s == 0) return (s);
00112 t = s + strlen (s) - 1;
00113 while (t > s && whitespace (*t)) t--;
00114 *++t = '\0';
00115 return s;
00116 }
00117
00118 #endif
00119
00120 cmd_prompt_p
00121 cmd_prompt_Initialize(mc_platform_p mc_platform)
00122 {
00123 cmd_prompt_p cmd_prompt;
00124 cmd_prompt = (cmd_prompt_p)malloc(sizeof(cmd_prompt_t));
00125 return cmd_prompt;
00126 }
00127
00128 int
00129 cmd_prompt_Destroy(cmd_prompt_p cmd_prompt)
00130 {
00131 free(cmd_prompt);
00132 return MC_SUCCESS;
00133 }
00134
00135 void
00136 cmd_prompt_Start( mc_platform_p mc_platform )
00137 {
00138 cmd_prompt_p cmd_prompt = mc_platform->cmd_prompt;
00139 #ifndef _WIN32
00140 pthread_attr_t attr;
00141 pthread_attr_init(&attr);
00142 if(mc_platform->stack_size[MC_THREAD_CP] != -1) {
00143 pthread_attr_setstacksize
00144 (
00145 &attr,
00146 mc_platform->stack_size[MC_THREAD_CP]
00147 );
00148 }
00149 #else
00150 int stack_size;
00151 if (mc_platform->stack_size[MC_THREAD_CP] < 1) {
00152
00153 stack_size = mc_platform->stack_size[MC_THREAD_CP]+1;
00154 } else {
00155 stack_size = mc_platform->stack_size[MC_THREAD_CP];
00156 }
00157 #endif
00158 THREAD_CREATE
00159 (
00160 &cmd_prompt->thread,
00161 cmd_prompt_Thread,
00162 (void*)mc_platform
00163 );
00164 }
00165
00166 #ifndef _WIN32
00167 void*
00168 cmd_prompt_Thread(void* arg)
00169 #else
00170 DWORD WINAPI
00171 cmd_prompt_Thread( LPVOID arg )
00172 #endif
00173 {
00174 char *buf;
00175 command_t cmd;
00176 mc_platform_p mc_platform = (mc_platform_p)arg;
00177 cmd.index = COMMAND_COUNT;
00178 printf("\n");
00179 #if !defined(HAVE_LIBREADLINE) || defined(_WIN32)
00180 buf = (char*)malloc(sizeof(char) * 100);
00181 if (!buf) {fprintf(stderr, "Malloc failed at %s:%d.\n", __FILE__, __LINE__); }
00182 #endif
00183
00184 #ifdef HAVE_LIBREADLINE
00185 initialize_readline();
00186 #endif
00187 while(1)
00188 {
00189
00190 #ifdef HAVE_LIBREADLINE
00191 buf = readline("MobileC > ");
00192 if (buf == NULL) {
00193 sleep(5);
00194 continue;
00195 }
00196 if (*buf) {
00197 add_history(buf);
00198 }
00199 #else
00200 printf("MobileC > ");
00201 #ifndef _WIN32
00202 if (!fgets(buf, 100, stdin))
00203 #else
00204 if (!gets(buf))
00205 #endif
00206 {
00207 fprintf(stderr, "fgets failed at %s:%d\n", __FILE__, __LINE__);
00208 #ifdef _WIN32
00209 Sleep(5000);
00210 #else
00211 sleep(5);
00212 #endif
00213 return NULL;
00214 }
00215 #endif
00216
00217 if(strlen(buf) > 0) {
00218 if (buf[strlen(buf)-1] == '\n')
00219 {
00220 buf[strlen(buf)-1] = '\0';
00221 }
00222 }
00223 cmd.num_args = split_string(&cmd.args, buf);
00224 process_command(&cmd);
00225 exec_command(cmd, mc_platform);
00226 dealloc_command(&cmd);
00227 #ifdef HAVE_LIBREADLINE
00228 free(buf);
00229 #endif
00230 }
00231 return 0;
00232 }
00233
00234
00235
00236
00237
00238
00239 int split_string(char ***args, const char *buf)
00240 {
00241
00242 int toggle = 0;
00243 int num_args = 0;
00244 int i;
00245 int j;
00246 char *word;
00247 char *_buf;
00248 _buf = (char*)malloc(strlen(buf) * sizeof(char) + 1);
00249 strcpy(_buf, buf);
00250 for(i=0; i<(int)strlen(_buf); i++)
00251 {
00252 if(_buf[i] != ' ')
00253 {
00254 if(toggle == 0)
00255 {
00256 toggle = 1;
00257 num_args++;
00258 }
00259 }
00260 else
00261 {
00262 toggle = 0;
00263 }
00264 }
00265
00266
00267 *args = (char **)malloc(sizeof(char *)*num_args);
00268
00269
00270 j = 0;
00271 word = strtok(_buf, " ");
00272 while(word != NULL)
00273 {
00274 (*args)[j] = (char*)malloc(sizeof(char)*strlen(word)+1);
00275 strcpy((*args)[j], word);
00276 j++;
00277 word = strtok(NULL, " ");
00278 }
00279 free(_buf);
00280
00281 return num_args;
00282 }
00283
00284
00285
00286
00287 int process_command(command_t *cmd)
00288 {
00289 int i;
00290 if(cmd->num_args == 0)
00291 {
00292 return 0;
00293 }
00294 for(i=0; i<COMMAND_COUNT; i++)
00295 {
00296 if(!strcmp(cmd->args[0], command_cmds[i]))
00297 {
00298 break;
00299 }
00300 }
00301 cmd->index = i;
00302 return 0;
00303 }
00304
00305 int exec_command(command_t cmd, mc_platform_p global)
00306 {
00307 if(cmd.num_args == 0)
00308 {
00309 return 0;
00310 }
00311 if(cmd.index == COMMAND_COUNT)
00312 {
00313 printf("Unknown command: %s\n", cmd.args[0]);
00314 printf("Type \"help\" for a listing of commands.\n");
00315 return 1;
00316 }
00317
00318 return cmd_handlers[cmd.index](&cmd, global);
00319 }
00320
00321 int dealloc_command(command_t *cmd)
00322 {
00323 int i;
00324 for(i=0; i<cmd->num_args; i++)
00325 {
00326 free(cmd->args[i]);
00327 }
00328 free(cmd->args);
00329
00330 return 0;
00331 }
00332
00333
00334
00335
00336 int handler_QUIT(void *arg, mc_platform_p global)
00337 {
00338 MUTEX_LOCK(global->quit_lock);
00339 global->quit = 1;
00340 COND_BROADCAST (global->quit_cond);
00341 MUTEX_UNLOCK(global->quit_lock);
00342 return 0;
00343 }
00344
00345 int handler_HELP(void *arg, mc_platform_p global)
00346 {
00347 command_t *cmd = (command_t*)arg;
00348 int i;
00349
00350 if(cmd->num_args > 1)
00351 {
00352 for(i=0; i<COMMAND_COUNT; i++)
00353 {
00354 if(!strcmp(command_cmds[i], cmd->args[1]))
00355 {
00356 break;
00357 }
00358 }
00359
00360 if(i == COMMAND_COUNT)
00361 {
00362
00363 printf("Sorry, the command '%s' does not exist.\n", cmd->args[1]);
00364 }
00365 else
00366 {
00367 printf("%s\n", command_descriptions[i]);
00368 }
00369 }
00370 else
00371 {
00372 printf("For info about help, type \"help help\"\n");
00373 printf("Current commands are:\n");
00374 for(i=0; i<COMMAND_COUNT; i++)
00375 {
00376 printf("%s\n", command_cmds[i]);
00377 }
00378 }
00379
00380 return 0;
00381 }
00382
00383 int handler_SEND(void *arg, mc_platform_p global)
00384 {
00385 command_t* cmd = (command_t*)arg;
00386 if(cmd->num_args != 2)
00387 {
00388 printf("%s\n", command_descriptions[COMMAND_SEND]);
00389 return 0;
00390 }
00391 return MC_SendAgentFile(NULL, cmd->args[1]);
00392 }
00393
00394 int handler_PRINT_CONNECTLIST(void *arg, mc_platform_p global)
00395 {
00396 connection_queue_Print(global->connection_queue);
00397 return 0;
00398 }
00399
00400 int handler_PRINTLIST_MESSAGE(void *arg, mc_platform_p global)
00401 {
00402 message_queue_Print(global->message_queue);
00403 return 0;
00404 }
00405
00406 int handler_PRINTLIST_AGENTS(void *arg, mc_platform_p global)
00407 {
00408 agent_queue_Print(global->agent_queue);
00409 return 0;
00410 }
00411
00412 int handler_FLUSH_AGENTS(void *arg, mc_platform_p global)
00413 {
00414 agent_queue_Flush(global->agent_queue);
00415 return 0;
00416 }
00417
00418 int handler_COMPOSE_SEND(void *arg, mc_platform_p global)
00419 {
00420
00421 command_t* cmd = (command_t*)arg;
00422 MCAgent_t agent;
00423 char name[80];
00424 char server[200];
00425 if(cmd->num_args != 4)
00426 {
00427 printf("%s\n", command_descriptions[COMMAND_COMPOSE_SEND]);
00428 return 0;
00429 }
00430 sprintf(name, "agent%d", rand()%1000);
00431 sprintf(server, "%s:%s", cmd->args[2], cmd->args[3]);
00432 agent = MC_ComposeAgentFromFile(
00433 name,
00434 global->hostname,
00435 name,
00436 cmd->args[1],
00437 NULL,
00438 server,
00439 0
00440 );
00441 if (agent != NULL) {
00442 MC_AddAgent(global->agency, agent);
00443 return 0;
00444 } else {
00445 return -1;
00446 }
00447 }