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