t_shell *init_struct(char **env) { t_shell *sh; if (!(sh = (t_shell *)malloc(sizeof(t_shell)))) return (NULL); sh->env = init_env(env); deal_with_file(sh); sh->hist = sh->hist->next; sh->hist = create_hist(); sh->return_val = 0; push_hist(&sh->head, sh->hist); return (sh); }
/*parseinput: Shell Input Parser, gets instructions Returns: 0 if exiting the shell, 1 if returning to shell */ int parseinput(char *buf) { if (strcmp(buf, "\n") == 0) { return 1; } else if (strcmp(buf, "exit\n") == 0) { return 0; } else { push_hist(buf); char *cmd; char *arguments[22]; //Creates the argv array arguments[21] = NULL; //Initializes the 21st argument to be NULL for error checking char *pch; int i = 1; cmd = strtok (buf," \n"); arguments[0] = cmd; /*Build the arguments array*/ do { pch = strtok (NULL, " \n "); arguments[i] = pch; i++; } while ((pch != NULL) && (i < 22)); /*If there are over 20 arguments, then 21st argument is not NULL, throw error and return to shell*/ if (arguments[21] != NULL) { printf("error: Too many arguments.\n"); return 1; } /*Determine which instruction was called and call appropriate function*/ if (strcmp(cmd, "path") == 0) { // When there isn't any arguments then print the PATH if (arguments[1] == NULL) { printPath(); } else if (arguments[2] == NULL) // To handle case of null argument after path + { printf("error: No path input.\n"); } else if (strcmp(arguments[1], "+") == 0) { addPath(arguments[2]); } else if (strcmp(arguments[1], "-") == 0) { removePath(arguments[2]); } // When there's any arguments other than + and -, print the PATH else { printPath(); } } else if (strcmp(cmd, "cd") == 0) { cd(arguments); } else if (strcmp(cmd, "history") == 0) { print_hist(); } else if (strncmp(cmd, "!", 1) == 0) { if ((strlen(cmd) > 4)|(strlen(cmd) < 1)) { printf("Command not found.\n"); } else { int n = n_convert(cmd); if (n != 0) { call_hist(n); } else { printf("Command not found.\n"); } } } else { open (cmd, arguments); } } return 1; }