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