Guest User

Untitled

a guest
Jun 6th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. void parse(const char * command){
  2.     // first parse built-ins (ie, not a call to the OS)
  3.     if (strcmp(command, "history") == 0){
  4.         show_history();
  5.         return;
  6.     }
  7.     if (strcmp(command, "exit") == 0){
  8.         exit(0);
  9.     }
  10.    
  11.     hist_add(command);
  12.  
  13.     // copy 'command' into arg_string, while ignoring any possible comments
  14.     char * arg_str;
  15.     int num_args = 1;
  16.     arg_str = malloc(strlen(command));
  17.     for (int i = 0; i < strlen(command); i++){
  18.         if (command[i] == '#' || command[i] == '\n') break;
  19.         if (command[i] == ' ') num_args++;
  20.         arg_str[i] = command[i];
  21.     }
  22.  
  23.     // split arg_str into a string array where each string is an argument
  24.     // to the command
  25.     char ** args = malloc(num_args+1);
  26.     for (int i = 0; i < num_args+1; i++){
  27.         args[i] = malloc(50);
  28.     }
  29.     int tokens = 0;
  30.     const char token = ' ';
  31.     char * next = strtok(arg_str, &token);
  32.     while (next != NULL){
  33.         strcpy(args[tokens++], next);
  34.         next = strtok(NULL, &token);
  35.         if (next == NULL)
  36.             args[tokens] = (char *)NULL;
  37.     }
  38.  
  39.     exec_command(args);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment