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 buf = (char*)malloc(sizeof(char) * 100);
00180 if (!buf) {fprintf(stderr, "Malloc failed at %s:%d.\n", __FILE__, __LINE__); }
00181 #ifdef HAVE_LIBREADLINE
00182 initialize_readline();
00183 #endif
00184 while(1)
00185 {
00186
00187 #ifdef HAVE_LIBREADLINE
00188 buf = readline("MobileC > ");
00189 if (*buf) {
00190 add_history(buf);
00191 }
00192 #else
00193 printf("MobileC > ");
00194 #ifndef _WIN32
00195 if (!fgets(buf, 100, stdin))
00196 #else
00197 if (!gets(buf))
00198 #endif
00199 {
00200 fprintf(stderr, "fgets failed at %s:%d\n", __FILE__, __LINE__);
00201 }
00202 #endif
00203
00204 if (buf[strlen(buf)-1] == '\n')
00205 {
00206 buf[strlen(buf)-1] = '\0';
00207 }
00208 cmd.num_args = split_string(&cmd.args, buf);
00209 process_command(&cmd);
00210 exec_command(cmd, mc_platform);
00211 dealloc_command(&cmd);
00212 #ifdef HAVE_LIBREADLINE
00213 free(buf);
00214 #endif
00215 }
00216 return 0;
00217 }
00218
00219
00220
00221
00222
00223
00224 int split_string(char ***args, const char *buf)
00225 {
00226
00227 int toggle = 0;
00228 int num_args = 0;
00229 int i;
00230 int j;
00231 char *word;
00232 char *_buf;
00233 _buf = malloc(strlen(buf) * sizeof(char) + 1);
00234 strcpy(_buf, buf);
00235 for(i=0; i<(int)strlen(_buf); i++)
00236 {
00237 if(_buf[i] != ' ')
00238 {
00239 if(toggle == 0)
00240 {
00241 toggle = 1;
00242 num_args++;
00243 }
00244 }
00245 else
00246 {
00247 toggle = 0;
00248 }
00249 }
00250
00251
00252 *args = (char **)malloc(sizeof(char *)*num_args);
00253
00254
00255 j = 0;
00256 word = strtok(_buf, " ");
00257 while(word != NULL)
00258 {
00259 (*args)[j] = (char*)malloc(sizeof(char)*strlen(word)+1);
00260 strcpy((*args)[j], word);
00261 j++;
00262 word = strtok(NULL, " ");
00263 }
00264 free(_buf);
00265
00266 return num_args;
00267 }
00268
00269
00270
00271
00272 int process_command(command_t *cmd)
00273 {
00274 int i;
00275 if(cmd->num_args == 0)
00276 {
00277 return 0;
00278 }
00279 for(i=0; i<COMMAND_COUNT; i++)
00280 {
00281 if(!strcmp(cmd->args[0], command_cmds[i]))
00282 {
00283 break;
00284 }
00285 }
00286 cmd->index = i;
00287 return 0;
00288 }
00289
00290 int exec_command(command_t cmd, mc_platform_p global)
00291 {
00292 if(cmd.num_args == 0)
00293 {
00294 return 0;
00295 }
00296 if(cmd.index == COMMAND_COUNT)
00297 {
00298 printf("Unknown command: %s\n", cmd.args[0]);
00299 printf("Type \"help\" for a listing of commands.\n");
00300 return 1;
00301 }
00302
00303 return cmd_handlers[cmd.index](&cmd, global);
00304 }
00305
00306 int dealloc_command(command_t *cmd)
00307 {
00308 int i;
00309 for(i=0; i<cmd->num_args; i++)
00310 {
00311 free(cmd->args[i]);
00312 }
00313 free(cmd->args);
00314
00315 return 0;
00316 }
00317
00318
00319
00320
00321 int handler_QUIT(void *arg, mc_platform_p global)
00322 {
00323 MUTEX_LOCK(global->quit_lock);
00324 global->quit = 1;
00325 COND_BROADCAST (global->quit_cond);
00326 MUTEX_UNLOCK(global->quit_lock);
00327 return 0;
00328 }
00329
00330 int handler_HELP(void *arg, mc_platform_p global)
00331 {
00332 command_t *cmd = (command_t*)arg;
00333 int i;
00334
00335 if(cmd->num_args > 1)
00336 {
00337 for(i=0; i<COMMAND_COUNT; i++)
00338 {
00339 if(!strcmp(command_cmds[i], cmd->args[1]))
00340 {
00341 break;
00342 }
00343 }
00344
00345 if(i == COMMAND_COUNT)
00346 {
00347
00348 printf("Sorry, the command '%s' does not exist.\n", cmd->args[2]);
00349 }
00350 else
00351 {
00352 printf("%s\n", command_descriptions[i]);
00353 }
00354 }
00355 else
00356 {
00357 printf("For info about help, type \"help help\"\n");
00358 printf("Current commands are:\n");
00359 for(i=0; i<COMMAND_COUNT; i++)
00360 {
00361 printf("%s\n", command_cmds[i]);
00362 }
00363 }
00364
00365 return 0;
00366 }
00367
00368 int handler_SEND(void *arg, mc_platform_p global)
00369 {
00370 command_t* cmd = (command_t*)arg;
00371 if(cmd->num_args != 4)
00372 {
00373 printf("%s\n", command_descriptions[COMMAND_SEND]);
00374 return 0;
00375 }
00376 return MC_SendAgentMigrationMessageFile(
00377 NULL,
00378 cmd->args[1],
00379 cmd->args[2],
00380 atoi(cmd->args[3]));
00381 }
00382
00383 int handler_PRINT_CONNECTLIST(void *arg, mc_platform_p global)
00384 {
00385 connection_queue_Print(global->connection_queue);
00386 return 0;
00387 }
00388
00389 int handler_PRINTLIST_MESSAGE(void *arg, mc_platform_p global)
00390 {
00391 message_queue_Print(global->message_queue);
00392 return 0;
00393 }
00394
00395 int handler_PRINTLIST_AGENTS(void *arg, mc_platform_p global)
00396 {
00397 agent_queue_Print(global->agent_queue);
00398 return 0;
00399 }
00400
00401 int handler_FLUSH_AGENTS(void *arg, mc_platform_p global)
00402 {
00403 agent_queue_Flush(global->agent_queue);
00404 return 0;
00405 }
00406