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