Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void parse(const char * command){
- // first parse built-ins (ie, not a call to the OS)
- if (strcmp(command, "history") == 0){
- show_history();
- return;
- }
- if (strcmp(command, "exit") == 0){
- exit(0);
- }
- hist_add(command);
- // copy 'command' into arg_string, while ignoring any possible comments
- char * arg_str;
- int num_args = 1;
- arg_str = malloc(strlen(command));
- for (int i = 0; i < strlen(command); i++){
- if (command[i] == '#' || command[i] == '\n') break;
- if (command[i] == ' ') num_args++;
- arg_str[i] = command[i];
- }
- // split arg_str into a string array where each string is an argument
- // to the command
- char ** args = malloc(num_args+1);
- for (int i = 0; i < num_args+1; i++){
- args[i] = malloc(50);
- }
- int tokens = 0;
- const char token = ' ';
- char * next = strtok(arg_str, &token);
- while (next != NULL){
- strcpy(args[tokens++], next);
- next = strtok(NULL, &token);
- if (next == NULL)
- args[tokens] = (char *)NULL;
- }
- exec_command(args);
- }
Advertisement
Add Comment
Please, Sign In to add comment