int sh_env(t_info *info) { int i; t_info context; sh_context(info, &context); UNSET(context.opt, OPT_A); i = 1; while (env_opt(context.args[i], &context)) i++; if (GET(context.opt, OPT_I)) env_i(&context, i); else if (!context.args[1]) { sh_printenv(&context); return (EXIT_SUCCESS); } else env_set(&context); if (ft_strequ(context.args[0], "cd")) return (EXIT_SUCCESS); else if (context.args[0]) sh_exec(&context); else sh_printenv(&context); sh_clear_context(&context); return (EXIT_SUCCESS); }
int sh_loop(t_info *info) { int ret; int multi; ret = 1; sh_get_path(info); ft_printf("\033[31m%s\033[39m $> ", info->cursdir); while ((ret = get_next_line(0, &info->line)) > 0) { UNSET(info->sig, BIT_A); UNSET(info->opt, OPT_A); if (!(multi = sh_multi(info))) { sh_parse(info); info->status = sh_exec(info, info->env, &info->args[1]); } sh_get_path(info); ft_printf("\033[31m%s\033[39m $> ", info->cursdir); ft_tabdel(&info->args); free(info->line); } if (ret == -1) return (EXIT_FAILURE); return (EXIT_SUCCESS); }
static void sh_mult_exec(int nbcmd, t_info *info) { while (nbcmd != 0) { sh_parse(info); info->status = sh_exec(info, info->env, &info->args[1]); info->line = ft_strchr(info->line, '\0') + 1; nbcmd--; } }
int sh_execute(char *name, char **av, t_env *env) { if ((env->pid = fork()) < 0) ft_puterr("fork fail"); else if (env->pid == 0) { sh_exec(name, av , env); } else { wait(NULL); } return (1); }
int main(int argc, const char **argv) { t_sh_env env; int error; t_ckbt *tree; t_cks line; if (sh_init_env(&env, argc, argv) == 0) { g_sh_env = &env; while ((sh_prompt(&env), (line = cks_get_line(0))) != NULL) { tree = ckbt_new(t_sh_command); error = sh_parse(line, tree); if (!error) sh_exec(&env, tree); else printf("42sh: syntax error\n"); cks_free(line); } } return (0); }
struct argnod *sh_argprocsub(Shell_t *shp, struct argnod *argp) { // Argument of the form <(cmd) or >(cmd). struct argnod *ap; int nn, monitor, fd, pv[3]; int subshell = shp->subshell; pid_t pid0; ap = (struct argnod *)stkseek(shp->stk, ARGVAL); ap->argflag |= ARG_MAKE; ap->argflag &= ~ARG_RAW; fd = argp->argflag & ARG_RAW; if (fd == 0 && shp->subshell) sh_subtmpfile(shp); #if has_dev_fd sfwrite(shp->stk, e_devfdNN, 8); pv[2] = 0; sh_pipe(pv); sfputr(shp->stk, fmtbase((long)pv[fd], 10, 0), 0); #else // has_dev_fd pv[0] = -1; shp->fifo = ast_temp_path("ksh.fifo"); if (mkfifo(shp->fifo, S_IRUSR | S_IWUSR)) abort(); sfputr(shp->stk, shp->fifo, 0); #endif // has_dev_fd ap = (struct argnod *)stkfreeze(shp->stk, 0); shp->inpipe = shp->outpipe = NULL; monitor = (sh_isstate(shp, SH_MONITOR) != 0); if (monitor) sh_offstate(shp, SH_MONITOR); shp->subshell = 0; #if has_dev_fd #if USE_SPAWN if (shp->vex || (shp->vex = spawnvex_open(0))) { spawnvex_add(shp->vex, pv[fd], pv[fd], 0, 0); } else #endif // USE_SPAWN fcntl(pv[fd], F_SETFD, 0); shp->fdstatus[pv[fd]] &= ~IOCLEX; #endif // has_dev_fd pid0 = shp->procsub ? *shp->procsub : 0; if (fd) { if (!shp->procsub) { shp->nprocsub = 4; shp->procsub = procsub = calloc(1, shp->nprocsub * sizeof(pid_t)); } else { nn = procsub - shp->procsub; if (nn >= shp->nprocsub) { shp->nprocsub += 3; shp->procsub = realloc(shp->procsub, shp->nprocsub * sizeof(pid_t)); procsub = shp->procsub + nn; } } if (pid0) *shp->procsub = 0; shp->inpipe = pv; sh_exec(shp, (Shnode_t *)argp->argchn.ap, (int)sh_isstate(shp, SH_ERREXIT)); if (pid0) *shp->procsub = pid0; *procsub++ = job.lastpost; } else { shp->outpipe = pv; sh_exec(shp, (Shnode_t *)argp->argchn.ap, (int)sh_isstate(shp, SH_ERREXIT)); } shp->subshell = subshell; if (monitor) sh_onstate(shp, SH_MONITOR); #if has_dev_fd sh_close(pv[1 - fd]); sh_iosave(shp, -pv[fd], shp->topfd, NULL); #else free(shp->fifo); shp->fifo = NULL; #endif // has_dev_fd return ap; }
int main(int argc, char** argv, char** env) { static struct option long_options[] = { { "command", required_argument, NULL, 'c'}, { "help", no_argument, NULL, 'h'}, { "version", no_argument, NULL, 'v'}, { NULL, 0, NULL, 0 } }; char* command = NULL; int c, idx; while((c = getopt_long(argc, argv, "c:hv", long_options, &idx)) != -1) { switch(c) { case 'c': if(strcmp(optarg, "-c") != 0) command = strdup(optarg); else command = strdup(argv[optind]); break; case 'v': show_version(argc, argv); break; case 'h': case '?': show_usage(argc, argv); break; default: abort(); } } char buf[BUFSIZ]; setenv("PWD", getcwd(buf, BUFSIZ), 1); if(command) sh_exec(command); setsid(); sh_reset_tty(); char* user = getuser(); char* host = gethost(); int e = 0; do { char line[BUFSIZ]; memset(line, 0, sizeof(line)); if(!sh_prompt(line, user, host, e)) continue; sh_history_add(line); e = sh_exec(line); } while(1); return 0; }
int main(int argc, char *argv[]) { Sfio_t *in, *out; Shell_t *shp; Namval_t *np; Shnode_t *t; char *cp; int n, nflag=0, vflag=0, dflag=0; error_info.id = argv[0]; while(n = optget(argv, usage )) switch(n) { case 'D': dflag=1; break; case 'v': vflag=1; break; case 'n': nflag=1; break; case ':': errormsg(SH_DICT,2,"%s",opt_info.arg); break; case '?': errormsg(SH_DICT,ERROR_usage(2),"%s",opt_info.arg); break; } shp = sh_init(argc,argv,(Shinit_f)0); shp->shcomp = 1; argv += opt_info.index; argc -= opt_info.index; if(error_info.errors || argc>2) errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0)); if(cp= *argv) { argv++; in = sh_pathopen(cp); } else in = sfstdin; if(cp= *argv) { struct stat statb; if(!(out = sfopen((Sfio_t*)0,cp,"w"))) errormsg(SH_DICT,ERROR_system(1),"%s: cannot create",cp); if(fstat(sffileno(out),&statb) >=0) chmod(cp,(statb.st_mode&~S_IFMT)|S_IXUSR|S_IXGRP|S_IXOTH); } else out = sfstdout; if(dflag) { sh_onoption(SH_DICTIONARY); sh_onoption(SH_NOEXEC); } if(nflag) sh_onoption(SH_NOEXEC); if(vflag) sh_onoption(SH_VERBOSE); if(!dflag) sfwrite(out,header,sizeof(header)); shp->inlineno = 1; #if SHOPT_BRACEPAT sh_onoption(SH_BRACEEXPAND); #endif while(1) { stakset((char*)0,0); if(t = (Shnode_t*)sh_parse(shp,in,0)) { if((t->tre.tretyp&(COMMSK|COMSCAN))==0 && t->com.comnamp && strcmp(nv_name((Namval_t*)t->com.comnamp),"alias")==0) sh_exec(t,0); if(!dflag && sh_tdump(out,t) < 0) errormsg(SH_DICT,ERROR_exit(1),"dump failed"); } else if(sfeof(in)) break; if(sferror(in)) errormsg(SH_DICT,ERROR_system(1),"I/O error"); if(t && ((t->tre.tretyp&COMMSK)==TCOM) && (np=t->com.comnamp) && (cp=nv_name(np))) { if(strcmp(cp,"exit")==0) break; /* check for exec of a command */ if(strcmp(cp,"exec")==0) { if(t->com.comtyp&COMSCAN) { if(t->com.comarg->argnxt.ap) break; } else { struct dolnod *ap = (struct dolnod*)t->com.comarg; if(ap->dolnum>1) break; } } } } /* copy any remaining input */ sfmove(in,out,SF_UNBOUND,-1); if(in!=sfstdin) sfclose(in); if(out!=sfstdout) sfclose(out); return(0); }
int b_dot_cmd(int n, char *argv[], Shbltin_t *context) { char *script; Namval_t *np; int jmpval; Shell_t *shp = context->shp; struct sh_scoped savst, *prevscope = shp->st.self; int fd; char *filename = NULL; char *buffer = NULL; struct dolnod *saveargfor = NULL; volatile struct dolnod *argsave = NULL; checkpt_t buff; Sfio_t *iop = NULL; short level; Optdisc_t disc; memset(&disc, 0, sizeof(disc)); disc.version = OPT_VERSION; opt_info.disc = &disc; while ((n = optget(argv, sh_optdot))) { switch (n) { case ':': { errormsg(SH_DICT, 2, "%s", opt_info.arg); break; } case '?': { errormsg(SH_DICT, ERROR_usage(0), "%s", opt_info.arg); return 2; } default: { break; } } } argv += opt_info.index; script = *argv; if (error_info.errors || !script) { errormsg(SH_DICT, ERROR_usage(2), "%s", optusage(NULL)); __builtin_unreachable(); } if (shp->dot_depth + 1 > DOTMAX) { errormsg(SH_DICT, ERROR_exit(1), e_toodeep, script); __builtin_unreachable(); } np = shp->posix_fun; if (!np) { // Check for KornShell style function first. np = nv_search(script, shp->fun_tree, 0); if (np && is_afunction(np) && !nv_isattr(np, NV_FPOSIX)) { if (!FETCH_VT(np->nvalue, ip)) { // TODO: Replace this with a comment explaining why the return value of this // path_search() call is ignored. At the time I wrote this (2019-03-16) no unit test // exercises this statement. I added the void cast to silence Coverity Scan 253792. (void)path_search(shp, script, NULL, 0); if (FETCH_VT(np->nvalue, ip)) { if (nv_isattr(np, NV_FPOSIX)) np = NULL; } else { errormsg(SH_DICT, ERROR_exit(1), e_found, script); __builtin_unreachable(); } } } else { np = NULL; } if (!np) { fd = path_open(shp, script, path_get(shp, script)); if (fd < 0) { errormsg(SH_DICT, ERROR_system(1), e_open, script); __builtin_unreachable(); } filename = path_fullname(shp, stkptr(shp->stk, PATH_OFFSET)); } } *prevscope = shp->st; shp->st.lineno = np ? ((struct functnod *)nv_funtree(np))->functline : 1; shp->st.var_local = shp->st.save_tree = shp->var_tree; if (filename) { shp->st.filename = filename; shp->st.lineno = 1; } level = shp->fn_depth + shp->dot_depth + 1; nv_putval(SH_LEVELNOD, (char *)&level, NV_INT16); shp->st.prevst = prevscope; shp->st.self = &savst; shp->topscope = (Shscope_t *)shp->st.self; prevscope->save_tree = shp->var_tree; if (np) { struct Ufunction *rp = FETCH_VT(np->nvalue, rp); shp->st.filename = strdup(rp->fname ? rp->fname : ""); } nv_putval(SH_PATHNAMENOD, shp->st.filename, NV_NOFREE); shp->posix_fun = NULL; if (np || argv[1]) argsave = sh_argnew(shp, argv, &saveargfor); sh_pushcontext(shp, &buff, SH_JMPDOT); jmpval = sigsetjmp(buff.buff, 0); if (jmpval == 0) { shp->dot_depth++; if (np) { sh_exec(shp, (Shnode_t *)(nv_funtree(np)), sh_isstate(shp, SH_ERREXIT)); } else { buffer = malloc(IOBSIZE + 1); iop = sfnew(NULL, buffer, IOBSIZE, fd, SF_READ); sh_offstate(shp, SH_NOFORK); sh_eval(shp, iop, sh_isstate(shp, SH_PROFILE) ? SH_FUNEVAL : 0); } } sh_popcontext(shp, &buff); if (buffer) free(buffer); if (!np) { free(shp->st.filename); shp->st.filename = NULL; } shp->dot_depth--; if ((np || argv[1]) && jmpval != SH_JMPSCRIPT) { sh_argreset(shp, (struct dolnod *)argsave, saveargfor); } else { prevscope->dolc = shp->st.dolc; prevscope->dolv = shp->st.dolv; } if (shp->st.self != &savst) *shp->st.self = shp->st; // Only restore the top Shscope_t portion for posix functions. memcpy(&shp->st, prevscope, sizeof(Shscope_t)); shp->topscope = (Shscope_t *)prevscope; nv_putval(SH_PATHNAMENOD, shp->st.filename, NV_NOFREE); if (jmpval && jmpval != SH_JMPFUN) siglongjmp(shp->jmplist->buff, jmpval); return shp->exitval; }