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 cmd_prompt_p
00057 cmd_prompt_Initialize(mc_platform_p mc_platform)
00058 {
00059 cmd_prompt_p cmd_prompt;
00060 cmd_prompt = (cmd_prompt_p)malloc(sizeof(cmd_prompt_t));
00061 return cmd_prompt;
00062 }
00063
00064 int
00065 cmd_prompt_Destroy(cmd_prompt_p cmd_prompt)
00066 {
00067 free(cmd_prompt);
00068 return MC_SUCCESS;
00069 }
00070
00071 void
00072 cmd_prompt_Start( mc_platform_p mc_platform )
00073 {
00074 cmd_prompt_p cmd_prompt = mc_platform->cmd_prompt;
00075 #ifndef _WIN32
00076 pthread_attr_t attr;
00077 pthread_attr_init(&attr);
00078 if(mc_platform->stack_size[MC_THREAD_CP] != -1) {
00079 pthread_attr_setstacksize
00080 (
00081 &attr,
00082 mc_platform->stack_size[MC_THREAD_CP]
00083 );
00084 }
00085 #else
00086 int stack_size;
00087 if (mc_platform->stack_size[MC_THREAD_CP] < 1) {
00088
00089 stack_size = mc_platform->stack_size[MC_THREAD_CP]+1;
00090 } else {
00091 stack_size = mc_platform->stack_size[MC_THREAD_CP];
00092 }
00093 #endif
00094 THREAD_CREATE
00095 (
00096 &cmd_prompt->thread,
00097 cmd_prompt_Thread,
00098 (void*)mc_platform
00099 );
00100 }
00101
00102 #ifndef _WIN32
00103 void*
00104 cmd_prompt_Thread(void* arg)
00105 #else
00106 DWORD WINAPI
00107 cmd_prompt_Thread( LPVOID arg )
00108 #endif
00109 {
00110 char *buf;
00111 command_t cmd;
00112 mc_platform_p mc_platform = (mc_platform_p)arg;
00113 cmd.index = COMMAND_COUNT;
00114 printf("\n");
00115 buf = (char*)malloc(sizeof(char) * 100);
00116 if (!buf) {fprintf(stderr, "Malloc failed at %s:%d.\n", __FILE__, __LINE__); }
00117 while(1)
00118 {
00119
00120 #ifdef HAVE_LIBREADLINE
00121 buf = readline("MobileC > ");
00122 #else
00123 printf("MobileC > ");
00124 #ifndef _WIN32
00125 if (!fgets(buf, 100, stdin))
00126 #else
00127 if (!gets(buf))
00128 #endif
00129 {
00130 fprintf(stderr, "fgets failed at %s:%d\n", __FILE__, __LINE__);
00131 }
00132 #endif
00133
00134 if (buf[strlen(buf)-1] == '\n')
00135 {
00136 buf[strlen(buf)-1] = '\0';
00137 }
00138 cmd.num_args = split_string(&cmd.args, buf);
00139 process_command(&cmd);
00140 exec_command(cmd, mc_platform);
00141 dealloc_command(&cmd);
00142 }
00143 return 0;
00144 }
00145
00146
00147
00148
00149
00150
00151 int split_string(char ***args, const char *buf)
00152 {
00153
00154 int toggle = 0;
00155 int num_args = 0;
00156 int i;
00157 int j;
00158 char *word;
00159 char *_buf;
00160 _buf = malloc(strlen(buf) * sizeof(char) + 1);
00161 strcpy(_buf, buf);
00162 for(i=0; i<(int)strlen(_buf); i++)
00163 {
00164 if(_buf[i] != ' ')
00165 {
00166 if(toggle == 0)
00167 {
00168 toggle = 1;
00169 num_args++;
00170 }
00171 }
00172 else
00173 {
00174 toggle = 0;
00175 }
00176 }
00177
00178
00179 *args = (char **)malloc(sizeof(char *)*num_args);
00180
00181
00182 j = 0;
00183 word = strtok(_buf, " ");
00184 while(word != NULL)
00185 {
00186 (*args)[j] = (char*)malloc(sizeof(char)*strlen(word)+1);
00187 strcpy((*args)[j], word);
00188 j++;
00189 word = strtok(NULL, " ");
00190 }
00191 free(_buf);
00192
00193 return num_args;
00194 }
00195
00196
00197
00198
00199 int process_command(command_t *cmd)
00200 {
00201 int i;
00202 if(cmd->num_args == 0)
00203 {
00204 return 0;
00205 }
00206 for(i=0; i<COMMAND_COUNT; i++)
00207 {
00208 if(!strcmp(cmd->args[0], command_cmds[i]))
00209 {
00210 break;
00211 }
00212 }
00213 cmd->index = i;
00214 return 0;
00215 }
00216
00217 int exec_command(command_t cmd, mc_platform_p global)
00218 {
00219 if(cmd.num_args == 0)
00220 {
00221 return 0;
00222 }
00223 if(cmd.index == COMMAND_COUNT)
00224 {
00225 printf("Unknown command: %s\n", cmd.args[0]);
00226 printf("Type \"help\" for a listing of commands.\n");
00227 return 1;
00228 }
00229
00230 return cmd_handlers[cmd.index](&cmd, global);
00231 }
00232
00233 int dealloc_command(command_t *cmd)
00234 {
00235 int i;
00236 for(i=0; i<cmd->num_args; i++)
00237 {
00238 free(cmd->args[i]);
00239 }
00240 free(cmd->args);
00241
00242 return 0;
00243 }
00244
00245
00246
00247
00248 int handler_QUIT(void *arg, mc_platform_p global)
00249 {
00250 MUTEX_LOCK(global->quit_lock);
00251 global->quit = 1;
00252 COND_BROADCAST (global->quit_cond);
00253 MUTEX_UNLOCK(global->quit_lock);
00254 return 0;
00255 }
00256
00257 int handler_HELP(void *arg, mc_platform_p global)
00258 {
00259 command_t *cmd = (command_t*)arg;
00260 int i;
00261
00262 if(cmd->num_args > 1)
00263 {
00264 for(i=0; i<COMMAND_COUNT; i++)
00265 {
00266 if(!strcmp(command_cmds[i], cmd->args[1]))
00267 {
00268 break;
00269 }
00270 }
00271
00272 if(i == COMMAND_COUNT)
00273 {
00274
00275 printf("Sorry, the command '%s' does not exist.\n", cmd->args[2]);
00276 }
00277 else
00278 {
00279 printf("%s\n", command_descriptions[i]);
00280 }
00281 }
00282 else
00283 {
00284 printf("For info about help, type \"help help\"\n");
00285 printf("Current commands are:\n");
00286 for(i=0; i<COMMAND_COUNT; i++)
00287 {
00288 printf("%s\n", command_cmds[i]);
00289 }
00290 }
00291
00292 return 0;
00293 }
00294
00295 int handler_SEND(void *arg, mc_platform_p global)
00296 {
00297 command_t* cmd = (command_t*)arg;
00298 if(cmd->num_args != 4)
00299 {
00300 printf("%s\n", command_descriptions[COMMAND_SEND]);
00301 return 0;
00302 }
00303 return MC_SendAgentMigrationMessageFile(
00304 NULL,
00305 cmd->args[1],
00306 cmd->args[2],
00307 atoi(cmd->args[3]));
00308 }
00309
00310 int handler_PRINT_CONNECTLIST(void *arg, mc_platform_p global)
00311 {
00312 connection_queue_Print(global->connection_queue);
00313 return 0;
00314 }
00315
00316 int handler_PRINTLIST_MESSAGE(void *arg, mc_platform_p global)
00317 {
00318 message_queue_Print(global->message_queue);
00319 return 0;
00320 }
00321
00322 int handler_PRINTLIST_AGENTS(void *arg, mc_platform_p global)
00323 {
00324 agent_queue_Print(global->agent_queue);
00325 return 0;
00326 }
00327