int check_cmd(t_shell *sh, t_node *tree) { int i; i = -1; number_reset(sh); sh->cmd = my_str_to_wordtab(tree->str); dollar(sh); check_point_slash(sh, sh->env); if (built_in(sh) == -1) return (-1); while (sh->path != NULL && sh->path[++i] != NULL && sh->ch == 0) if (access(strcat(sh->path[i], sh->cmd[0]), X_OK) == 0) exec_cmd(sh->path[i], sh->cmd, sh->env, sh); if (sh->ch == 0) exec_slah_bin(sh->cmd, sh); if (sh->ch == 0) { fprintf(stderr, "Error: '%s' command not found\n", sh->cmd[0]); sh->ok_cmd = -1; return (0); } my_free(sh->cmd); return (0); }
//Parse in parallel int parParse(char*** commands){ const char* whitespace = " \t\n"; char** arguments; bool is_built_in = true; pid_t pid; int i; int state = 1; pid_t waitArr[arrLen(*commands)]; memset(waitArr, 0, (arrLen(*commands)+1)*sizeof(pid_t)); for (i = 0; (*commands)[i] != NULL; i++){ arguments = tokenify((*commands)[i], whitespace); is_built_in = built_in(arguments, &state); //check if built in if (!is_built_in){ pid = fork(); //create child process if (pid == 0){ execv(arguments[0], arguments); freeTokens(arguments); printf("Command not valid\n"); exit(0); } else{ waitArr[i] = pid; } } freeTokens(arguments); } waitParallel(waitArr); return state; }
void bip_parallel_init(int flags) { if (flags & INIT_SHARED) { #ifdef NEW_ORACLE (void) exported_built_in(in_dict("get_oracle", 3), p_get_oracle3, B_UNSAFE); (void) exported_built_in(in_dict("install_oracle", 1), p_install_oracle, B_SAFE); (void) exported_built_in(in_dict("install_pending_oracle", 0), p_install_pending_oracle, B_SAFE); (void) built_in(in_dict("recomputing", 0), p_recomputing, B_SAFE); (void) exported_built_in(in_dict("oracle_check", 1), p_oracle_check, B_UNSAFE); (void) local_built_in(in_dict("set_par_goal", 1), p_set_par_goal, B_UNSAFE); (void) local_built_in(in_dict("get_par_goal", 1), p_get_par_goal, B_UNSAFE|U_UNIFY); #endif (void) local_built_in(in_dict("dbag_create", 1), p_dbag_create, B_SAFE|U_GROUND); (void) local_built_in(in_dict("dbag_enter", 2), p_dbag_enter, B_SAFE|U_NONE); (void) local_built_in(in_dict("dbag_dissolve", 2), p_dbag_dissolve, B_UNSAFE|U_UNIFY); } }
//Parses commands sequentially int seqParse(char*** commands){ const char* whitespace = " \t\n"; char** arguments; bool is_built_in = true; int* status = 0; pid_t pid; int state = 0; for (int i = 0; (*commands)[i] != NULL; i++){ arguments = tokenify((*commands)[i], whitespace); is_built_in = built_in(arguments, &state); if (!is_built_in){ pid = fork(); if (pid == 0){ //child process execv(arguments[0], arguments);//execv the command freeTokens(arguments); printf("Command not valid\n"); //if execv returns, command was invalid exit(0); } else{ //parent process waitpid(pid, status, 0); // wait for child process to end } } freeTokens(arguments); } return state; }
void bip_module_init(int flags) { if (!(flags & INIT_SHARED)) return; (void) local_built_in(in_dict("erase_module_", 2), p_erase_module, B_SAFE); (void) local_built_in(in_dict("is_a_module", 1), p_is_module, B_SAFE); (void) local_built_in(in_dict("authorized_module", 1), p_authorized_module, B_SAFE); (void) built_in(in_dict("is_locked", 1), p_is_locked, B_SAFE); (void) built_in(in_dict("begin_module", 1), p_begin_module, B_SAFE); (void) local_built_in(in_dict("begin_module", 2), p_begin_module, B_SAFE); (void) local_built_in(in_dict("create_module_", 1), p_create_module, B_SAFE); (void) built_in(d_.lock, p_lock1, B_SAFE); (void) built_in(in_dict("lock", 2), p_lock2, B_SAFE); (void) built_in(in_dict("lock_pass_", 2), p_lock_pass_, B_SAFE); (void) built_in(in_dict("unlock", 2), p_unlock2, B_SAFE); (void) exported_built_in(in_dict("tool_", 2), p_tool1, B_UNSAFE); (void) exported_built_in(in_dict("tool_", 3), p_tool2, B_UNSAFE); exported_built_in(in_dict("tool_body_", 4), p_tool_body, B_UNSAFE|U_GROUND) -> mode = BoundArg(2, GROUND) | BoundArg(3, CONSTANT); (void) local_built_in(d_.localb, p_local, B_UNSAFE); (void) exported_built_in(in_dict("implicit_local",2), p_implicit_local, B_UNSAFE); (void) local_built_in(d_.exportb, p_export, B_UNSAFE); (void) local_built_in(in_dict("reexport_from_",3), p_reexport_from, B_UNSAFE); (void) local_built_in(d_.import_fromb, p_import_from, B_UNSAFE); (void) local_built_in(in_dict("import_", 2), p_import, B_UNSAFE); (void) local_built_in(in_dict("module_tag", 2), p_module_tag, B_UNSAFE); (void) exported_built_in(in_dict("default_module", 1), p_default_module, B_UNSAFE|U_SIMPLE); (void) exported_built_in(in_dict("pr", 1), p_pr, B_SAFE); }
void error_init(int flags) { if (flags & INIT_SHARED) { int i; /* * Due to the user entries, part of the message array is * mutable and must be in shared memory. */ ErrorMessage = (char **) hg_alloc_size(sizeof(char *) * MAX_ERRORS); for(i=0; i<MAX_ERRORS; i++) { ErrorMessage[i] = ec_error_message[i]; } (void) built_in(in_dict("error_id", 2), p_error_id, B_UNSAFE|U_SIMPLE); (void) local_built_in(in_dict("max_error", 1), p_max_error, B_UNSAFE|U_SIMPLE); (void) local_built_in(in_dict("set_last_errno", 1), p_set_last_errno, B_SAFE); (void) local_built_in(in_dict("get_last_errno", 1), p_get_last_errno, B_UNSAFE|U_SIMPLE); (void) built_in(in_dict("errno_id", 1), p_errno_id1, B_UNSAFE|U_SIMPLE); (void) built_in(in_dict("errno_id", 2), p_errno_id, B_UNSAFE|U_SIMPLE); } }
int Shelly::execute(Shelly::command command) { int status = 0; // Check if the command is a built in function of the shell commands::internal_function built_in = commands::internal[command.name]; if (built_in != 0) { return built_in(*this, command); } int pid = fork(); if (pid == 0) { // execvp always expects the last argument to be a null terminator command.argv_c.push_back(0); // Because the argv_c vector stores the C-strings in order we can // simply de-refrence the first element of the argv_c vector execvp(command.argv_c[0], (char * const *) &command.argv_c[0]); // exec failed, the command was _probably_ not found std::cerr << command.name << ": command not found\n"; finish(); } else { if (command.background) { if (background_jobs.size() < Shelly::MAX_BG_JOBS) { background_jobs.push_back({command, pid}); std::cout << "[" << pid << "] " << command.command << '\n'; return status; } // Let the user know only so many commands may be run in the bg std::cerr << "No more than " << Shelly::MAX_BG_JOBS << " background jobs may be run at once.\n" << "Executing " << command.name << " in the foreground\n"; } wait(&status); } return status; }
void bip_shelf_init(int flags) { if (flags & INIT_SHARED) { (void) built_in(in_dict("shelf_create", 3), p_shelf_create3, B_SAFE|U_SIMPLE); (void) built_in(in_dict("shelf_create", 2), p_shelf_create2, B_SAFE|U_SIMPLE); (void) built_in(in_dict("shelf_get_",4), p_shelf_get, B_UNSAFE|U_FRESH); (void) built_in(in_dict("shelf_set_",4), p_shelf_set, B_SAFE); (void) built_in(in_dict("shelf_inc_",3), p_shelf_inc, B_SAFE); (void) built_in(in_dict("shelf_dec_",3), p_shelf_dec, B_SAFE); (void) built_in(in_dict("shelf_abolish", 1), p_handle_free, B_SAFE|U_NONE); (void) local_built_in(in_dict("shelf_name",3), p_shelf_name, B_SAFE); } }
/* ------------------------------------------------------ * process_rq( char *rq, int fd ) do what the request asks for and write reply to fd handles request in a new process rq is HTTP command: GET /foo/bar.html HTTP/1.0 ------------------------------------------------------ */ process_rq( char *rq, int fd) { char cmd[BUFSIZ], arg[BUFSIZ]; if ( sscanf(rq, "%s%s", cmd, arg) != 2 ) return; sanitize(arg); printf("sanitized version is %s\n", arg); if ( strcmp(cmd,"GET") != 0 ) not_implemented(); else if ( built_in(arg, fd) ) ; else if ( not_exist( arg ) ) do_404(arg, fd); else if ( isadir( arg ) ) do_ls( arg, fd ); else do_cat( arg, fd ); }
PUBLIC void init_lib(void) { built_in("chr", char_type, L_CHR); built_in("ord", int_type, L_ORD); built_in("halt", void_type, L_HALT); built_in("eof", bool_type, L_EOF); built_in("eoln", bool_type, L_EOLN); built_in("write", void_type, L_WRITE); built_in("writeln", void_type, L_WRITELN); built_in("read", void_type, L_READ); built_in("readln", void_type, L_READLN); built_in("flush", void_type, L_FLUSH); built_in("argc", int_type, L_ARGC); built_in("argv", void_type, L_ARGV); built_in("openin", bool_type, L_OPENIN); built_in("closein", void_type, L_CLOSEIN); }
int main() { char prompt[MAXLINE]; char *cmd; char **param; struct parse_info parse; param=malloc(sizeof(char *)*(MAXARG+2)); open(".history",O_WRONLY|O_TRUNC,0666); open(".alias",O_WRONLY|O_TRUNC,0666); while(1){ int fd[2],in_fd,out_fd,status; pid_t pid,pid2; type_prompt(prompt); int count=read_cmd(&cmd,param,prompt); if(count==-1) continue; parsing(param,count,&parse); //printf("%d\n%s\n%s\n%s\n",parse.flag,parse.in_file,parse.out_file,parse.cmd2); if(built_in(cmd,param)) continue; if(parse.flag & IS_PIPED){ if(pipe(fd)<0){ printf("myshell error: pipe failed.\n"); exit(0); } } if((pid=fork())==0){ if(parse.flag & IS_PIPED){ if(!(parse.flag & OUT_REDIRECT)&&!(parse.flag & OUT_REDIRECT_APPEND)){ close(fd[0]); close(fileno(stdout)); dup2(fd[1],fileno(stdout)); close(fd[1]); } else{ close(fd[0]); close(fd[1]); if(parse.flag & OUT_REDIRECT) out_fd=open(parse.out_file,O_WRONLY|O_CREAT|O_TRUNC,0666); else out_fd=open(parse.out_file,O_WRONLY|O_CREAT|O_APPEND,0666); close(fileno(stdout)); dup2(out_fd,fileno(stdout)); close(out_fd); } } else{ if(parse.flag & OUT_REDIRECT){ out_fd=open(parse.out_file,O_WRONLY|O_CREAT|O_TRUNC,0666); close(fileno(stdout)); dup2(out_fd,fileno(stdout)); close(out_fd); } if(parse.flag & OUT_REDIRECT_APPEND){ out_fd=open(parse.out_file,O_WRONLY|O_CREAT|O_APPEND,0666); close(fileno(stdout)); dup2(out_fd,fileno(stdout)); close(out_fd); } } if(parse.flag & IN_REDIRECT){ in_fd=open(parse.in_file,O_RDONLY|O_CREAT,0666); close(fileno(stdin)); dup2(in_fd,fileno(stdin)); close(in_fd); } execvp(cmd,param); } else{ if(parse.flag & IS_PIPED){ if((pid2=fork())==0){ close(fd[1]); close(fileno(stdin)); dup2(fd[0],fileno(stdin)); close(fd[0]); execvp(parse.cmd2,parse.param2); } else{ close(fd[0]); close(fd[1]); waitpid(pid2,&status,0); } } if(parse.flag & BACKGROUND){ printf("Child pid: %u\n",pid); } else waitpid(pid,&status,0); } if(env_modify){ char str[1024]; env_modify=0; FILE *fp; fp=fopen(".environment","r"); while(!feof(fp)){ fgets(str,1024,fp); if(!strncmp(str,str_env,strlen(str_env))){ printf("%s",str+strlen(str_env)+1); break; } } fclose(fp); } } return 0; }
/* * FUNCTION NAME: bip_strings_init() * * PARAMETERS: NONE. * * DESCRIPTION: links the 'C' functions in this file with SEPIA * built-in predicates. */ void bip_strings_init(int flags) { if (flags & INIT_PRIVATE) { empty_string = enter_string_n("", 0, DICT_PERMANENT); d_sha_ = in_dict("sha", 0); } if (flags & INIT_SHARED) { built_in(in_dict("string_list", 2), p_string_list, B_UNSAFE|U_GROUND|PROC_DEMON) -> mode = BoundArg(1, NONVAR) | BoundArg(2, NONVAR); built_in(in_dict("utf8_list", 2), p_utf8_list, B_UNSAFE|U_GROUND|PROC_DEMON) -> mode = BoundArg(1, NONVAR) | BoundArg(2, NONVAR); (void) built_in(in_dict("hash_secure", 3), p_hash_secure, B_UNSAFE|U_SIMPLE); (void) built_in(in_dict("string_length", 2), p_string_length,B_UNSAFE|U_SIMPLE); (void) built_in(in_dict("get_string_code", 3), p_get_string_code, B_UNSAFE|U_SIMPLE); (void) b_built_in(in_dict("string_code", 4), p_string_code, d_.kernel_sepia); (void) built_in(in_dict("substring", 3), p_substring, B_UNSAFE|U_SIMPLE); (void) built_in(in_dict("atom_length", 2), p_atom_length, B_UNSAFE|U_SIMPLE); (void) built_in(in_dict("string_upper", 2), p_string_upper, B_UNSAFE|U_SIMPLE); (void) built_in(in_dict("string_lower", 2), p_string_lower, B_UNSAFE|U_SIMPLE); (void) built_in(in_dict("concat_atoms", 3), p_concat_atoms, B_UNSAFE|U_SIMPLE|PROC_DEMON); (void) built_in(in_dict("concat_atom", 2), p_concat_atom, B_UNSAFE|U_SIMPLE|PROC_DEMON); (void) built_in(in_dict("concat_strings", 3), p_concat_strings,B_UNSAFE|U_SIMPLE|PROC_DEMON); (void) built_in(in_dict("concat_string", 2), p_concat_string,B_UNSAFE|U_SIMPLE|PROC_DEMON); (void) built_in(in_dict("atomics_to_string", 2),p_concat_string,B_UNSAFE|U_SIMPLE|PROC_DEMON); (void) built_in(in_dict("join_string", 3), p_join_string, B_UNSAFE|U_SIMPLE|PROC_DEMON); (void) built_in(in_dict("atomics_to_string", 3),p_join_string, B_UNSAFE|U_SIMPLE|PROC_DEMON); (void) built_in(in_dict("text_to_string", 2), p_text_to_string, B_UNSAFE|U_SIMPLE|PROC_DEMON); built_in(in_dict("split_string", 4), p_split_string, B_UNSAFE|U_GROUND) -> mode = BoundArg(4, GROUND); built_in(in_dict("char_int", 2), p_char_int, B_UNSAFE|U_SIMPLE) -> mode = BoundArg(1, NONVAR) | BoundArg(2, NONVAR); (void) exported_built_in(in_dict("first_substring", 4), p_first_substring, B_UNSAFE|U_SIMPLE); exported_built_in(in_dict("string_print_length", 4), p_string_print_length, B_UNSAFE|U_SIMPLE) -> mode = BoundArg(3, CONSTANT); } }