Lines Matching refs:shp

120     Shell_t *shp = handle;  in fifo_check()  local
123 unlink(shp->fifo); in fifo_check()
124 sh_done(shp, 0); in fifo_check()
181 static_fn void iousepipe(Shell_t *shp) { in iousepipe() argument
185 if (!sh_iovalidfd(shp, fd)) abort(); in iousepipe()
188 sh_iounpipe(shp); in iousepipe()
192 if (shp->comsub != 1) { in iousepipe()
198 if (!sh_iovalidfd(shp, subpipe[2])) abort(); in iousepipe()
199 shp->fdstatus[subpipe[2]] = shp->fdstatus[1]; in iousepipe()
205 shp->fdstatus[1] = shp->fdstatus[subpipe[1]] & ~IOCLEX; in iousepipe()
207 subdup = shp->subdup; in iousepipe()
213 shp->fdstatus[i] = shp->fdstatus[1]; in iousepipe()
219 void sh_iounpipe(Shell_t *shp) { in sh_iounpipe() argument
226 if (shp->comsub > 1) { in sh_iounpipe()
238 shp->fdstatus[1] = shp->fdstatus[subpipe[2]]; in sh_iounpipe()
244 shp->fdstatus[n] = shp->fdstatus[1]; in sh_iounpipe()
248 shp->subdup = 0; in sh_iounpipe()
300 static_fn void p_time(Shell_t *shp, Sfio_t *out, const char *format, struct timeval tm[3]) { in p_time() argument
301 int c, n, offset = stktell(shp->stk); in p_time()
305 Stk_t *stkp = shp->stk; in p_time()
386 static_fn int xec_p_comarg(Shell_t *shp, struct comnod *com) { in xec_p_comarg() argument
388 int n = xec_p_arg(shp, com->comset, ARG_ASSIGN); in xec_p_comarg()
389 if (com->comarg && (com->comtyp & COMSCAN)) n += xec_p_arg(shp, com->comarg, 0); in xec_p_comarg()
392 Shbltin_t *bp = &shp->bltindata; in xec_p_comarg()
413 static_fn int sh_tclear(Shell_t *shp, Shnode_t *t) { in sh_tclear() argument
420 return sh_tclear(shp, t->par.partre); in sh_tclear()
423 return xec_p_comarg(shp, (struct comnod *)t); in sh_tclear()
427 return sh_tclear(shp, t->fork.forktre); in sh_tclear()
430 n = sh_tclear(shp, t->if_.iftre); in sh_tclear()
431 n += sh_tclear(shp, t->if_.thtre); in sh_tclear()
432 n += sh_tclear(shp, t->if_.eltre); in sh_tclear()
437 if (t->wh.whinc) n = sh_tclear(shp, (Shnode_t *)(t->wh.whinc)); in sh_tclear()
438 n += sh_tclear(shp, t->wh.whtre); in sh_tclear()
439 n += sh_tclear(shp, t->wh.dotre); in sh_tclear()
446 n = sh_tclear(shp, t->lst.lstlef); in sh_tclear()
447 return n + sh_tclear(shp, t->lst.lstrit); in sh_tclear()
450 return xec_p_arg(shp, t->ar.arexpr, ARG_ARITH); in sh_tclear()
453 n = sh_tclear(shp, t->for_.fortre); in sh_tclear()
454 return n + sh_tclear(shp, (Shnode_t *)t->for_.forlst); in sh_tclear()
457 n = xec_p_arg(shp, t->sw.swarg, 0); in sh_tclear()
458 return n + xec_p_switch(shp, t->sw.swlst); in sh_tclear()
461 n = sh_tclear(shp, t->funct.functtre); in sh_tclear()
462 return n + sh_tclear(shp, (Shnode_t *)t->funct.functargs); in sh_tclear()
466 return sh_tclear(shp, t->lst.lstlef); in sh_tclear()
468 n = xec_p_arg(shp, &(t->lst.lstlef->arg), 0); in sh_tclear()
469 if (t->tre.tretyp & TBINARY) n += xec_p_arg(shp, &(t->lst.lstrit->arg), 0); in sh_tclear()
476 static_fn int xec_p_arg(Shell_t *shp, struct argnod *arg, int flag) { in xec_p_arg() argument
481 sh_tclear(shp, (Shnode_t *)arg->argchn.ap); in xec_p_arg()
483 sh_tclear(shp, ((struct fornod *)arg->argchn.ap)->fortre); in xec_p_arg()
490 static_fn int xec_p_switch(Shell_t *shp, struct regnod *reg) { in xec_p_switch() argument
493 n += xec_p_arg(shp, reg->regptr, 0); in xec_p_switch()
494 n += sh_tclear(shp, reg->regcom); in xec_p_switch()
556 Shell_t *shp = sh_ptr(np); in put_level() local
574 sp = sh_getscope(shp, level, SEEK_SET); in put_level()
576 sh_setscope(shp, sp); in put_level()
583 static_fn struct Level *init_level(Shell_t *shp, int level) { in init_level() argument
589 shp->last_root = nv_dict(VAR_sh); in init_level()
599 int sh_debug(Shell_t *shp, const char *trap, const char *name, const char *subscript, in sh_debug() argument
601 Stk_t *stkp = shp->stk; in sh_debug()
610 if (shp->indebug) return 0; in sh_debug()
611 shp->indebug = 1; in sh_debug()
638 shp->st.lineno = error_info.line; in sh_debug()
639 level = shp->fn_depth + shp->dot_depth; in sh_debug()
640 shp->last_root = nv_dict(VAR_sh); in sh_debug()
643 init_level(shp, level); in sh_debug()
647 savst = shp->st; in sh_debug()
648 shp->st.trap[SH_DEBUGTRAP] = 0; in sh_debug()
649 n = sh_trap(shp, trap, 0); in sh_debug()
651 shp->indebug = 0; in sh_debug()
652 if (shp->st.cmdname) error_info.id = shp->st.cmdname; in sh_debug()
653 nv_putval(VAR_sh_file, shp->st.filename, NV_NOFREE); in sh_debug()
654 nv_putval(VAR_sh_fun, shp->st.funname, NV_NOFREE); in sh_debug()
655 shp->st = savst; in sh_debug()
692 static_fn nvflag_t set_instance(Shell_t *shp, Namval_t *nq, Namval_t *node, struct Namref *nr) { in set_instance() argument
707 nr->root = shp->var_tree; in set_instance()
708 nr->table = shp->last_table; in set_instance()
709 if (!nr->table && shp->namespace) nr->table = shp->namespace; in set_instance()
710 shp->instance = 1; in set_instance()
716 shp->instance = 0; in set_instance()
717 if (shp->var_tree != shp->var_base && !nv_search_namval(nq, nr->root, NV_NOSCOPE)) { in set_instance()
718 nr->root = shp->namespace ? nv_dict(shp->namespace) : shp->var_base; in set_instance()
747 static_fn Sfio_t *openstream(Shell_t *shp, struct ionod *iop, int *save) { in openstream() argument
750 int fd = sh_redirect(shp, iop, 3); in openstream()
757 shp->offsets[0] = -1; in openstream()
758 shp->offsets[1] = 0; in openstream()
763 static_fn Namval_t *enter_namespace(Shell_t *shp, Namval_t *nsp) { in enter_namespace() argument
764 Namval_t *path = nsp, *fpath = nsp, *onsp = shp->namespace; in enter_namespace()
772 } else if (nv_dict(nsp)->view != shp->var_base) { in enter_namespace()
784 if (shp->var_tree == oroot) { in enter_namespace()
785 shp->var_tree = shp->var_tree->view; in enter_namespace()
786 oroot = shp->var_base; in enter_namespace()
790 if (shp->var_tree == shp->var_base) { in enter_namespace()
791 shp->var_tree = nv_dict(nsp); in enter_namespace()
793 for (root = shp->var_tree; root->view != oroot; root = root->view) { in enter_namespace()
799 shp->namespace = nsp; in enter_namespace()
800 if (path && (path = nv_search(VAR_PATH->nvname, shp->var_tree, NV_NOSCOPE)) && in enter_namespace()
804 if (fpath && (fpath = nv_search(VAR_FPATH->nvname, shp->var_tree, NV_NOSCOPE)) && in enter_namespace()
811 __attribute__((noreturn)) static_fn void forked_child(Shell_t *shp, const Shnode_t *t, int flags, in forked_child() argument
820 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in forked_child()
823 if (no_fork) sh_sigreset(shp, 2); in forked_child()
824 sh_pushcontext(shp, buffp, SH_JMPEXIT); in forked_child()
827 if ((type & FINT) && !sh_isstate(shp, SH_MONITOR)) { in forked_child()
831 shp->sigflag[SIGINT] = SH_SIGOFF; in forked_child()
832 shp->sigflag[SIGQUIT] = SH_SIGOFF; in forked_child()
833 if (!shp->st.ioset) { in forked_child()
840 sh_offstate(shp, SH_MONITOR); in forked_child()
842 if ((type & FAMP) && sh_isoption(shp, SH_BGNICE)) nice(4); in forked_child()
845 if (shp->fifo && (type & (FPIN | FPOU))) { in forked_child()
847 Timer_t *fifo_timer = sh_timeradd(500, 1, fifo_check, shp); in forked_child()
848 fn = sh_open(shp->fifo, fd ? O_WRONLY : O_RDONLY); in forked_child()
850 sh_iorenumber(shp, fn, fd); in forked_child()
853 unlink(shp->fifo); in forked_child()
854 free(shp->fifo); in forked_child()
855 shp->fifo = NULL; in forked_child()
860 sh_iorenumber(shp, shp->inpipe[0], 0); in forked_child()
861 if (!(type & FPOU) || (type & FCOOP)) sh_close(shp->inpipe[1]); in forked_child()
864 sh_iorenumber(shp, shp->outpipe[1], 1); in forked_child()
865 sh_pclose(shp->outpipe); in forked_child()
868 error_info.line = t->fork.forkline - shp->st.firstline; in forked_child()
870 if (shp->topfd) sh_iounsave(shp); in forked_child()
871 topfd = shp->topfd; in forked_child()
877 sh_redirect(shp, t->tre.treio, 1 | IOUSEVEX); in forked_child()
880 while ((parent = fork()) < 0) _sh_fork(shp, parent, 0, NULL); in forked_child()
883 job_post(shp, parent, 0); in forked_child()
885 sh_iorestore(shp, topfd, SH_JMPCMD); in forked_child()
887 if (shp->vexp->cur > vexi) sh_vexrestore(shp, vexi); in forked_child()
889 sh_done(shp, (shp->exitval & SH_EXITSIG) ? (shp->exitval & SH_EXITMASK) : 0); in forked_child()
896 if (!no_fork && !(type & FPOU)) job_clear(shp); in forked_child()
897 sh_exec(shp, t->fork.forktre, flags | sh_state(SH_NOFORK) | sh_state(SH_FORKED)); in forked_child()
899 sh_offoption(shp, SH_ERREXIT); in forked_child()
900 sh_freeup(shp); in forked_child()
901 path_exec(shp, com0, com, t->com.comset); in forked_child()
904 sh_popcontext(shp, buffp); in forked_child()
905 if (jmpval > SH_JMPEXIT) siglongjmp(shp->jmplist->buff, jmpval); in forked_child()
906 sh_done(shp, 0); in forked_child()
909 int sh_exec(Shell_t *shp, const Shnode_t *t, int flags) { in sh_exec() argument
910 sh_sigcheck(shp); in sh_exec()
912 if (!t) return shp->exitval; in sh_exec()
913 if (shp->st.execbrk) return shp->exitval; in sh_exec()
914 if (sh_isoption(shp, SH_NOEXEC)) return shp->exitval; in sh_exec()
916 Stk_t *stkp = shp->stk; in sh_exec()
928 int topfd = shp->topfd; in sh_exec()
936 int vexi = shp->vexp->cur; in sh_exec()
942 volatile int was_errexit = sh_isstate(shp, SH_ERREXIT); in sh_exec()
943 volatile int was_monitor = sh_isstate(shp, SH_MONITOR); in sh_exec()
954 sh_offstate(shp, SH_ERREXIT); in sh_exec()
955 sh_offstate(shp, SH_DEFPATH); in sh_exec()
956 if (was_errexit & flags) sh_onstate(shp, SH_ERREXIT); in sh_exec()
957 if (was_monitor & flags) sh_onstate(shp, SH_MONITOR); in sh_exec()
959 if (!shp->intrap) shp->oldexit = shp->exitval; in sh_exec()
960 shp->exitval = 0; in sh_exec()
961 shp->lastsig = 0; in sh_exec()
962 shp->lastpath = NULL; in sh_exec()
963 if (shp->exittrap || shp->errtrap) execflg = 0; in sh_exec()
972 shp->bltindata.invariant = type >> (COMBITS + 2); in sh_exec()
973 shp->bltindata.pwdfd = shp->pwdfd; in sh_exec()
976 error_info.line = t->com.comline - shp->st.firstline; in sh_exec()
978 spawnvex_add(shp->vex, SPAWN_frame, 0, 0, 0); in sh_exec()
980 com = sh_argbuild(shp, &argn, &(t->com), flags & ARG_OPTIMIZE); in sh_exec()
981 procsub = shp->procsub; in sh_exec()
982 shp->procsub = NULL; in sh_exec()
986 if (argp && *com && !(argp->argflag & ARG_RAW)) sh_sigcheck(shp); in sh_exec()
990 if (np && shp->namespace && nq != shp->namespace && in sh_exec()
993 mp = sh_fsearch(shp, com[0], 0); in sh_exec()
995 nq = shp->namespace; in sh_exec()
1000 shp->xargexit = 0; in sh_exec()
1002 int n = b_command(0, com, &shp->bltindata); in sh_exec()
1007 np = nv_bfsearch(com0, shp->bltin_tree, &nq, &cp); in sh_exec()
1009 if (shp->xargexit) { in sh_exec()
1010 shp->xargmin -= command; in sh_exec()
1011 shp->xargmax -= command; in sh_exec()
1013 shp->xargmin = 0; in sh_exec()
1019 if (shp->namespace && (mp = sh_fsearch(shp, np->nvname, 0))) { in sh_exec()
1022 np = dtsearch(shp->fun_tree, np); in sh_exec()
1028 Dt_t *root = command ? shp->bltin_tree : shp->fun_tree; in sh_exec()
1030 if (shp->namespace && !nq && !cp) np = sh_fsearch(shp, com0, 0); in sh_exec()
1036 shp->envlist = argp = t->com.comset; in sh_exec()
1037 if (shp->envlist) { in sh_exec()
1055 if (!shp->st.var_local) { in sh_exec()
1056 sh_scope(shp, NULL, 0); in sh_exec()
1057 shp->st.var_local = shp->var_tree; in sh_exec()
1063 shp->typeinit = np; in sh_exec()
1072 if (shp->namespace) { in sh_exec()
1074 if (!shp->strbuf2) shp->strbuf2 = sfstropen(); in sh_exec()
1075 sfprintf(shp->strbuf2, "%s%s%c", NV_CLASS, nv_name(shp->namespace), in sh_exec()
1077 char *p = sfstruse(shp->strbuf2); in sh_exec()
1079 shp->prefix = strdup(p); in sh_exec()
1080 xp = shp->prefix + strlen(NV_CLASS); in sh_exec()
1084 nv_open(shp->prefix, shp->var_base, NV_VARNAME); in sh_exec()
1088 shp->prefix = NV_CLASS; in sh_exec()
1092 if ((shp->fn_depth && !shp->prefix) || np == SYSLOCAL) { in sh_exec()
1105 shp->nodelist = sh_setlist(shp, argp, nvflags, tp); in sh_exec()
1106 if (np == shp->typeinit) shp->typeinit = NULL; in sh_exec()
1107 shp->envlist = argp; in sh_exec()
1111 last_table = shp->last_table; in sh_exec()
1112 shp->last_table = NULL; in sh_exec()
1125 else if ((t->tre.tretyp & FSHOWME) && sh_isoption(shp, SH_SHOWME)) { in sh_exec()
1126 int ison = sh_isoption(shp, SH_XTRACE); in sh_exec()
1127 if (!ison) sh_onoption(shp, SH_XTRACE); in sh_exec()
1128 sh_trace(shp, com - command, tflags); in sh_exec()
1129 if (io) sh_redirect(shp, io, SH_SHOWME | IOHERESTRING); in sh_exec()
1130 if (!ison) sh_offoption(shp, SH_XTRACE); in sh_exec()
1132 } else if ((np != SYSSET) && sh_isoption(shp, SH_XTRACE)) { in sh_exec()
1133 sh_trace(shp, com - command, tflags); in sh_exec()
1135 trap = shp->st.trap[SH_DEBUGTRAP]; in sh_exec()
1137 int n = sh_debug(shp, trap, NULL, NULL, com, ARG_RAW); in sh_exec()
1138 if (n == 255 && shp->fn_depth + shp->dot_depth) { in sh_exec()
1149 if (io) sfsync(shp->outpool); in sh_exec()
1150 shp->lastpath = NULL; in sh_exec()
1152 if (path_search(shp, com0, NULL, 1)) { in sh_exec()
1153 error_info.line = t->com.comline - shp->st.firstline; in sh_exec()
1154 if (!shp->namespace || !(np = sh_fsearch(shp, com0, 0))) { in sh_exec()
1155 np = nv_search(com0, shp->fun_tree, 0); in sh_exec()
1158 Namval_t *mp = nv_search(com0, shp->bltin_tree, 0); in sh_exec()
1165 if ((np = nv_search(com0, shp->track_tree, 0)) && in sh_exec()
1167 np = nv_search(nv_getval(np), shp->bltin_tree, 0); in sh_exec()
1174 if (shp->comsub == 1 && np && is_abuiltin(np) && *np->nvname == '/') { in sh_exec()
1188 int was_nofork = execflg ? sh_isstate(shp, SH_NOFORK) : 0; in sh_exec()
1189 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in sh_exec()
1194 bp = &shp->bltindata; in sh_exec()
1202 was_vi = sh_isoption(shp, SH_VI); in sh_exec()
1203 was_emacs = sh_isoption(shp, SH_EMACS); in sh_exec()
1204 was_gmacs = sh_isoption(shp, SH_GMACS); in sh_exec()
1205 sh_offoption(shp, SH_VI); in sh_exec()
1206 sh_offoption(shp, SH_EMACS); in sh_exec()
1207 sh_offoption(shp, SH_GMACS); in sh_exec()
1209 if (execflg) sh_onstate(shp, SH_NOFORK); in sh_exec()
1210 sh_pushcontext(shp, buffp, SH_JMPCMD); in sh_exec()
1220 type = (execflg && !shp->subshell && !shp->st.trapcom[0]); in sh_exec()
1222 shp->redir0 = 1; in sh_exec()
1223 sh_redirect(shp, io, in sh_exec()
1230 if (!shp->pwd) { in sh_exec()
1231 path_pwd(shp); in sh_exec()
1233 } else if (shp->pwdfd >= 0) { in sh_exec()
1234 fstat(shp->pwdfd, &statb); in sh_exec()
1235 } else if (shp->pwd) { in sh_exec()
1241 sh_onstate(shp, SH_STOPOK); in sh_exec()
1244 save_prompt = shp->nextprompt; in sh_exec()
1245 shp->nextprompt = 0; in sh_exec()
1249 sh_scope(shp, argp, 0); in sh_exec()
1254 if (argn) shp->exitval = 0; in sh_exec()
1255 shp->bltinfun = funptr(np); in sh_exec()
1263 if (shp->subshell && nv_isattr(np, BLT_NOSFIO)) sh_subtmpfile(shp); in sh_exec()
1264 if (execflg && !shp->subshell && !shp->st.trapcom[0] && in sh_exec()
1265 !shp->st.trap[SH_ERRTRAP] && shp->fn_depth == 0 && in sh_exec()
1269 for (fd = 0; fd < shp->gd->lim.open_max; fd++) { in sh_exec()
1270 if ((shp->fdstatus[fd] & IOCLEX) && fd != shp->infd && in sh_exec()
1271 (fd != shp->pwdfd)) { in sh_exec()
1277 shp->exitval = (*shp->bltinfun)(argn, com, bp); in sh_exec()
1281 ((Shnode_t *)t)->com.comstate = shp->bltindata.data; in sh_exec()
1283 if (shp->exitval && errno == EINTR && shp->lastsig) { in sh_exec()
1284 shp->exitval = SH_EXITSIG | shp->lastsig; in sh_exec()
1286 shp->exitval &= SH_EXITMASK; in sh_exec()
1293 if (shp->gd->hist_ptr && item->strm == shp->gd->hist_ptr->histfp) { in sh_exec()
1294 hist_close(shp->gd->hist_ptr); in sh_exec()
1300 if (shp->bltinfun && (error_info.flags & ERROR_NOTIFY)) { in sh_exec()
1301 (*shp->bltinfun)(-2, com, bp); in sh_exec()
1310 if (np != SYSEXEC && shp->vex->cur) { in sh_exec()
1312 spawnvex_apply(shp->vex, 0, SPAWN_RESET | SPAWN_FRAME); in sh_exec()
1315 spawnvex_apply(shp->vex, 0, SPAWN_RESET | SPAWN_FRAME); in sh_exec()
1316 if (shp->comsub && (fd = sffileno(sfstdout)) != 1 && fd >= 0) in sh_exec()
1317 spawnvex_add(shp->vex, fd, 1, 0, 0); in sh_exec()
1323 if (execflg && !was_nofork) sh_offstate(shp, SH_NOFORK); in sh_exec()
1326 while ((sh_fchdir(shp->pwdfd) < 0) && errno == EINTR) errno = 0; in sh_exec()
1328 if (shp->pwd || (shp->pwdfd >= 0)) { in sh_exec()
1335 if (shp->pwdfd >= 0) { in sh_exec()
1336 while ((fchdir(shp->pwdfd) < 0) && errno == EINTR) { in sh_exec()
1340 while ((chdir(shp->pwd) < 0) && errno == EINTR) errno = err; in sh_exec()
1345 sh_offstate(shp, SH_STOPOK); in sh_exec()
1348 sfpool(sfstderr, shp->outpool, SF_WRITE); in sh_exec()
1350 shp->nextprompt = save_prompt; in sh_exec()
1352 sh_popcontext(shp, buffp); in sh_exec()
1355 shp->bltinfun = 0; in sh_exec()
1358 sh_onoption(shp, SH_VI); in sh_exec()
1360 sh_onoption(shp, SH_EMACS); in sh_exec()
1362 sh_onoption(shp, SH_GMACS); in sh_exec()
1364 if (scope) sh_unscope(shp); in sh_exec()
1368 if ((shp->topfd > topfd) && !(shp->subshell && np == SYSEXEC)) { in sh_exec()
1369 sh_iorestore(shp, topfd, jmpval); in sh_exec()
1372 if (shp->vexp->cur > vexi) sh_vexrestore(shp, vexi); in sh_exec()
1374 shp->redir0 = 0; in sh_exec()
1375 if (jmpval) siglongjmp(shp->jmplist->buff, jmpval); in sh_exec()
1376 if (shp->exitval >= 0) goto setexit; in sh_exec()
1384 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in sh_exec()
1391 indx = path_search(shp, com0, NULL, 0); in sh_exec()
1393 if (shp->namespace) { in sh_exec()
1394 np = sh_fsearch(shp, com0, 0); in sh_exec()
1396 np = nv_search(com0, shp->fun_tree, NV_NOSCOPE); in sh_exec()
1404 shp->exitval = ERROR_NOEXEC; in sh_exec()
1407 shp->exitval = ERROR_NOENT; in sh_exec()
1419 shp->last_table = last_table; in sh_exec()
1420 mode = set_instance(shp, nq, &node, &nr); in sh_exec()
1423 indx = shp->topfd; in sh_exec()
1424 sh_pushcontext(shp, buffp, SH_JMPCMD); in sh_exec()
1428 if (io) indx = sh_redirect(shp, io, execflg | IOUSEVEX); in sh_exec()
1445 namespace = nv_search(cp - 1, shp->var_base, NV_NOSCOPE); in sh_exec()
1449 namespace = enter_namespace(shp, namespace); in sh_exec()
1450 sh_funct(shp, np, argn, com, t->com.comset, (flags & ~ARG_OPTIMIZE)); in sh_exec()
1452 enter_namespace(shp, namespace); in sh_exec()
1454 spawnvex_apply(shp->vex, 0, SPAWN_RESET | SPAWN_FRAME); in sh_exec()
1455 if (shp->vexp->cur > vexi) sh_vexrestore(shp, vexi); in sh_exec()
1459 sh_popcontext(shp, buffp); in sh_exec()
1460 sh_iorestore(shp, indx, jmpval); in sh_exec()
1466 siglongjmp(shp->jmplist->buff, jmpval); in sh_exec()
1472 spawnvex_apply(shp->vex, 0, SPAWN_RESET | SPAWN_FRAME); in sh_exec()
1475 exitset(shp); in sh_exec()
1489 if (shp->subshell) { in sh_exec()
1490 sh_subtmpfile(shp); in sh_exec()
1492 if (shp->comsub && !(shp->fdstatus[1] & IONOSEEK)) { in sh_exec()
1493 iousepipe(shp); in sh_exec()
1502 !ntflag && !(type & (FAMP | FPOU)) && !shp->subshell && in sh_exec()
1503 !(shp->st.trapcom[SIGINT] && *shp->st.trapcom[SIGINT]) && !shp->st.trapcom[0] && in sh_exec()
1504 !shp->st.trap[SH_ERRTRAP] && shp->jmplist->mode != SH_JMPEVAL && in sh_exec()
1506 (execflg && shp->fn_depth == 0 && !(pipejob && sh_isoption(shp, SH_PIPEFAIL)))); in sh_exec()
1507 if (sh_isstate(shp, SH_PROFILE) || shp->dot_depth) { in sh_exec()
1510 sh_offstate(shp, SH_MONITOR); in sh_exec()
1513 sh_offstate(shp, SH_MONITOR); in sh_exec()
1529 restorefd = shp->topfd; in sh_exec()
1531 restorevex = shp->vexp->cur; in sh_exec()
1535 coproc_init(shp, pipes); in sh_exec()
1540 parent = sh_ntfork(shp, t, com, &jobid, ntflag); in sh_exec()
1542 parent = sh_fork(shp, type, &jobid); in sh_exec()
1547 if (shp->comsub == 1 && usepipe && unpipe) sh_iounpipe(shp); in sh_exec()
1552 parent = sh_fork(shp, type, &jobid); in sh_exec()
1557 forked_child(shp, t, flags, type, no_fork, com0, com, parent, topfd, vexi); in sh_exec()
1566 if (shp->subshell) shp->spid = parent; in sh_exec()
1567 if (type & FPCL) sh_close(shp->inpipe[0]); in sh_exec()
1569 shp->bckpid = parent; in sh_exec()
1571 if (!sh_isoption(shp, SH_MONITOR)) { in sh_exec()
1572 if (!(shp->sigflag[SIGINT] & (SH_SIGFAULT | SH_SIGOFF))) { in sh_exec()
1573 sh_sigtrap(shp, SIGINT); in sh_exec()
1575 shp->trapnote |= SH_SIGIGNORE; in sh_exec()
1577 if (shp->pipepid) { in sh_exec()
1578 shp->pipepid = parent; in sh_exec()
1581 if (parent == shp->spid) shp->spid = 0; in sh_exec()
1583 if (shp->topfd > topfd) sh_iorestore(shp, topfd, 0); in sh_exec()
1585 if (shp->vexp->cur > vexi) sh_vexrestore(shp, vexi); in sh_exec()
1587 if (usepipe && tsetio && subdup) sh_iounpipe(shp); in sh_exec()
1588 if (!sh_isoption(shp, SH_MONITOR)) { in sh_exec()
1589 shp->trapnote &= ~SH_SIGIGNORE; in sh_exec()
1590 if (shp->exitval == (SH_EXITSIG | SIGINT)) kill(getpid(), SIGINT); in sh_exec()
1594 if (sh_isstate(shp, SH_PROFILE) || sh_isstate(shp, SH_INTERACTIVE)) { in sh_exec()
1610 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in sh_exec()
1611 if (shp->subshell) execflg = 0; in sh_exec()
1612 sh_pushcontext(shp, buffp, SH_JMPIO); in sh_exec()
1614 was_interactive = sh_isstate(shp, SH_INTERACTIVE); in sh_exec()
1615 sh_offstate(shp, SH_INTERACTIVE); in sh_exec()
1616 shp->pipepid = simple; in sh_exec()
1617 sh_iosave(shp, 0, shp->topfd, NULL); in sh_exec()
1618 sh_iorenumber(shp, shp->inpipe[0], 0); in sh_exec()
1626 error_info.line = t->fork.forkline - shp->st.firstline; in sh_exec()
1630 if (shp->comsub) tsetio = 1; in sh_exec()
1631 sh_redirect(shp, t->fork.forkio, execflg); in sh_exec()
1635 if ((t->tre.tretyp & COMMSK) == TCOM && sh_isoption(shp, SH_BASH) && in sh_exec()
1636 !sh_isoption(shp, SH_LASTPIPE)) { in sh_exec()
1637 Shnode_t *tt = stkalloc(shp->stk, sizeof(Shnode_t)); in sh_exec()
1643 sh_exec(shp, t, flags & ~simple); in sh_exec()
1645 sfsync(shp->outpool); in sh_exec()
1647 sh_popcontext(shp, buffp); in sh_exec()
1648 sh_iorestore(shp, buffp->topfd, jmpval); in sh_exec()
1650 if (shp->vexp->cur > vexi) sh_vexrestore(shp, buffp->vexi); in sh_exec()
1655 type = shp->exitval; in sh_exec()
1658 if (shp->pipepid > 1 && shp->comsub != 1) { in sh_exec()
1659 job_wait(shp->pipepid); in sh_exec()
1660 type = shp->exitval; in sh_exec()
1664 if (type || !sh_isoption(shp, SH_PIPEFAIL)) shp->exitval = type; in sh_exec()
1666 shp->pipepid = 0; in sh_exec()
1667 shp->st.ioset = 0; in sh_exec()
1670 sh_onstate(shp, SH_ERREXIT); in sh_exec()
1673 if (jmpval > SH_JMPIO) siglongjmp(shp->jmplist->buff, jmpval); in sh_exec()
1679 if (!shp->subshell && !shp->st.trapcom[0] && !shp->st.trap[SH_ERRTRAP] && in sh_exec()
1683 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in sh_exec()
1684 shp->st.otrapcom = NULL; in sh_exec()
1686 nsig = shp->st.trapmax; in sh_exec()
1687 if (nsig > 0 || shp->st.trapcom[0]) { in sh_exec()
1688 int trapcom_size = (shp->st.trapmax + 1) * sizeof(char *); in sh_exec()
1690 memcpy(savsig, shp->st.trapcom, trapcom_size); in sh_exec()
1691 shp->st.otrapcom = (char **)savsig; in sh_exec()
1693 sh_sigreset(shp, 0); in sh_exec()
1694 sh_pushcontext(shp, buffp, SH_JMPEXIT); in sh_exec()
1696 if (jmpval == 0) sh_exec(shp, t->par.partre, flags); in sh_exec()
1697 sh_popcontext(shp, buffp); in sh_exec()
1698 if (jmpval > SH_JMPEXIT) siglongjmp(shp->jmplist->buff, jmpval); in sh_exec()
1699 if (shp->exitval > 256) shp->exitval -= 128; in sh_exec()
1700 sh_done(shp, 0); in sh_exec()
1704 while ((pid = fork()) < 0) _sh_fork(shp, pid, 0, 0); in sh_exec()
1706 sh_exec(shp, t->par.partre, flags); in sh_exec()
1707 shp->st.trapcom[0] = 0; in sh_exec()
1708 sh_offoption(shp, SH_INTERACTIVE); in sh_exec()
1709 sh_done(shp, 0); in sh_exec()
1712 sh_subshell(shp, t->par.partre, flags, 0); in sh_exec()
1730 if (shp->subshell) { in sh_exec()
1731 sh_subtmpfile(shp); in sh_exec()
1732 if (shp->comsub == 1 && !(shp->fdstatus[1] & IONOSEEK)) iousepipe(shp); in sh_exec()
1734 shp->inpipe = pvo; in sh_exec()
1735 shp->outpipe = pvn; in sh_exec()
1737 if (sh_isoption(shp, SH_PIPEFAIL)) { in sh_exec()
1742 exitval = job.exitval = stkalloc(shp->stk, job.waitall * sizeof(int)); in sh_exec()
1745 job.waitall |= !pipejob && sh_isstate(shp, SH_MONITOR); in sh_exec()
1754 type = sh_exec(shp, t->lst.lstlef, errorflg); in sh_exec()
1765 shp->inpipe = pvn; in sh_exec()
1766 shp->outpipe = NULL; in sh_exec()
1773 sh_exec(shp, t, flags); in sh_exec()
1780 n = shp->exitval; in sh_exec()
1783 if (sh_isstate(shp, SH_MONITOR)) { in sh_exec()
1786 shp->intrap++; in sh_exec()
1788 shp->intrap--; in sh_exec()
1799 shp->exitval = n; in sh_exec()
1801 if (!pipejob && sh_isstate(shp, SH_MONITOR) && sh_isoption(shp, SH_INTERACTIVE)) { in sh_exec()
1802 tcsetpgrp(JOBTTY, shp->gd->pid); in sh_exec()
1814 sh_exec(shp, t->lst.lstlef, errorflg | (flags & ARG_OPTIMIZE)); in sh_exec()
1817 sh_exec(shp, t, flags); in sh_exec()
1822 if (sh_exec(shp, t->lst.lstlef, (flags & ARG_OPTIMIZE)) == 0) { in sh_exec()
1823 sh_exec(shp, t->lst.lstrit, flags); in sh_exec()
1829 if (sh_exec(shp, t->lst.lstlef, flags & ARG_OPTIMIZE) != 0) { in sh_exec()
1830 sh_exec(shp, t->lst.lstrit, flags); in sh_exec()
1845 int jmpval = shp->jmplist->mode; in sh_exec()
1846 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in sh_exec()
1847 void *optlist = shp->optlist; in sh_exec()
1848 shp->optlist = NULL; in sh_exec()
1849 sh_tclear(shp, t->for_.fortre); in sh_exec()
1850 sh_pushcontext(shp, buffp, jmpval); in sh_exec()
1853 error_info.line = t->for_.forline - shp->st.firstline; in sh_exec()
1855 args = shp->st.dolv + 1; in sh_exec()
1856 nargs = shp->st.dolc; in sh_exec()
1857 argsav = sh_arguse(shp); in sh_exec()
1859 args = sh_argbuild(shp, &argn, tp, 0); in sh_exec()
1862 np = nv_open(t->for_.fornam, shp->var_tree, NV_NOARRAY | NV_VARNAME | NV_NOREF); in sh_exec()
1864 shp->st.loopcnt++; in sh_exec()
1866 while (cp && shp->st.execbrk == 0) { in sh_exec()
1872 sh_menu(shp, sfstderr, nargs, args); in sh_exec()
1875 save_prompt = shp->nextprompt; in sh_exec()
1876 shp->nextprompt = 3; in sh_exec()
1877 shp->timeout = 0; in sh_exec()
1878 shp->exitval = in sh_exec()
1879 sh_readline(shp, &nullptr, NULL, 0, 1, (size_t)0, 1000 * shp->st.tmout); in sh_exec()
1880 shp->nextprompt = save_prompt; in sh_exec()
1881 if (shp->exitval || sfeof(sfstdin) || sferror(sfstdin)) { in sh_exec()
1882 shp->exitval = 1; in sh_exec()
1885 if (!(val = nv_getval(sh_scoped(shp, VAR_REPLY)))) { in sh_exec()
1917 trap = shp->st.trap[SH_DEBUGTRAP]; in sh_exec()
1924 sh_debug(shp, trap, NULL, NULL, av, 0); in sh_exec()
1926 sh_exec(shp, t->for_.fortre, flag); in sh_exec()
1929 if ((cp = nv_getval(sh_scoped(shp, VAR_REPLY))) && *cp == 0) refresh++; in sh_exec()
1934 if (shp->st.breakcnt < 0) shp->st.execbrk = (++shp->st.breakcnt != 0); in sh_exec()
1938 sh_popcontext(shp, buffp); in sh_exec()
1939 sh_tclear(shp, t->for_.fortre); in sh_exec()
1940 sh_optclear(shp, optlist); in sh_exec()
1941 if (jmpval) siglongjmp(shp->jmplist->buff, jmpval); in sh_exec()
1942 if (shp->st.breakcnt > 0) shp->st.execbrk = (--shp->st.breakcnt != 0); in sh_exec()
1943 shp->st.loopcnt--; in sh_exec()
1944 sh_argfree(shp, argsav); in sh_exec()
1954 int jmpval = shp->jmplist->mode; in sh_exec()
1955 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in sh_exec()
1956 void *optlist = shp->optlist; in sh_exec()
1957 shp->optlist = NULL; in sh_exec()
1958 sh_tclear(shp, t->wh.whtre); in sh_exec()
1959 sh_tclear(shp, t->wh.dotre); in sh_exec()
1960 sh_pushcontext(shp, buffp, jmpval); in sh_exec()
1964 iop = openstream(shp, tt->com.comio, &savein); in sh_exec()
1965 if (tt->com.comset) sh_setlist(shp, tt->com.comset, NV_IDENT | NV_ASSIGN, 0); in sh_exec()
1967 shp->st.loopcnt++; in sh_exec()
1968 while (shp->st.execbrk == 0) { in sh_exec()
1970 if (!(shp->cur_line = sfgetr(iop, '\n', SF_STRING))) break; in sh_exec()
1971 } else if ((sh_exec(shp, tt, first) == 0) != (type == TWH)) { in sh_exec()
1974 r = sh_exec(shp, t->wh.dotre, first | errorflg); in sh_exec()
1975 if (shp->st.breakcnt < 0) shp->st.execbrk = (++shp->st.breakcnt != 0); in sh_exec()
1977 if (shp->st.execbrk == 0 && t->wh.whinc) { in sh_exec()
1978 sh_exec(shp, (Shnode_t *)t->wh.whinc, first); in sh_exec()
1982 shp->offsets[0] = -1; in sh_exec()
1983 shp->offsets[1] = 0; in sh_exec()
1986 sh_popcontext(shp, buffp); in sh_exec()
1987 sh_tclear(shp, t->wh.whtre); in sh_exec()
1988 sh_tclear(shp, t->wh.dotre); in sh_exec()
1989 sh_optclear(shp, optlist); in sh_exec()
1990 if (jmpval) siglongjmp(shp->jmplist->buff, jmpval); in sh_exec()
1991 if (shp->st.breakcnt > 0) shp->st.execbrk = (--shp->st.breakcnt != 0); in sh_exec()
1992 shp->st.loopcnt--; in sh_exec()
1993 shp->exitval = r; in sh_exec()
2000 shp->cur_line = NULL; in sh_exec()
2007 error_info.line = t->ar.arline - shp->st.firstline; in sh_exec()
2010 arg[1] = sh_macpat(shp, t->ar.arexpr, (flags & ARG_OPTIMIZE) | ARG_ARITH); in sh_exec()
2016 trap = shp->st.trap[SH_DEBUGTRAP]; in sh_exec()
2018 sh_debug(shp, trap, NULL, NULL, arg, ARG_ARITH); in sh_exec()
2020 if (sh_isoption(shp, SH_XTRACE)) { in sh_exec()
2021 sh_trace(shp, NULL, 0); in sh_exec()
2025 shp->exitval = !arith_exec((Arith_t *)t->ar.arcomp); in sh_exec()
2027 shp->exitval = !sh_arith(shp, arg[1]); in sh_exec()
2032 if (sh_exec(shp, t->if_.iftre, flags & ARG_OPTIMIZE) == 0) { in sh_exec()
2033 sh_exec(shp, t->if_.thtre, flags); in sh_exec()
2035 sh_exec(shp, t->if_.eltre, flags); in sh_exec()
2037 shp->exitval = 0; // force zero exit for if-then-fi in sh_exec()
2043 char *trap, *r = sh_macpat(shp, tt->sw.swarg, flags & ARG_OPTIMIZE); in sh_exec()
2044 error_info.line = t->sw.swline - shp->st.firstline; in sh_exec()
2046 trap = shp->st.trap[SH_DEBUGTRAP]; in sh_exec()
2053 sh_debug(shp, trap, NULL, NULL, av, 0); in sh_exec()
2060 s = sh_macpat(shp, rex, (flags & ARG_OPTIMIZE) | ARG_EXP | ARG_CASE); in sh_exec()
2068 sh_exec(shp, t->reg.regcom, in sh_exec()
2087 sh_exec(shp, t->par.partre, flags & ARG_OPTIMIZE); in sh_exec()
2088 shp->exitval = !shp->exitval; in sh_exec()
2094 if (shp->subshell && shp->comsub == 1) sh_subfork(); in sh_exec()
2095 long timer_on = sh_isstate(shp, SH_TIMING); in sh_exec()
2097 sh_onstate(shp, SH_TIMING); in sh_exec()
2098 sh_exec(shp, t->par.partre, flags & ARG_OPTIMIZE); in sh_exec()
2099 if (!timer_on) sh_offstate(shp, SH_TIMING); in sh_exec()
2109 Namval_t *np = nv_open("TIMEFORMAT", shp->var_tree, NV_NOADD); in sh_exec()
2118 if (format && *format) p_time(shp, sfstderr, sh_translate(format), tm); in sh_exec()
2138 np = nv_open(xp, shp->var_tree, nvflags); in sh_exec()
2143 dtuserdata(root, shp, 1); in sh_exec()
2146 dtview(root, shp->var_base); in sh_exec()
2148 oldnspace = enter_namespace(shp, np); in sh_exec()
2149 sh_exec(shp, t->for_.fortre, nvflags | sh_state(SH_ERREXIT)); in sh_exec()
2150 enter_namespace(shp, oldnspace); in sh_exec()
2154 error_info.line = t->funct.functline - shp->st.firstline; in sh_exec()
2156 if (cp || shp->prefix) { in sh_exec()
2158 if (shp->prefix) { in sh_exec()
2159 cp = shp->prefix; in sh_exec()
2160 shp->prefix = NULL; in sh_exec()
2161 npv = nv_open(cp, shp->var_tree, NV_NOARRAY | NV_VARNAME); in sh_exec()
2162 shp->prefix = cp; in sh_exec()
2167 npv = nv_open(stkptr(stkp, offset), shp->var_tree, NV_NOARRAY | NV_VARNAME); in sh_exec()
2172 } else if ((mp = nv_search(fname, shp->bltin_tree, 0)) && nv_isattr(mp, BLT_SPC)) { in sh_exec()
2176 if (shp->namespace && !shp->prefix && *fname != '.') { in sh_exec()
2177 np = sh_fsearch(shp, fname, NV_ADD | NV_NOSCOPE); in sh_exec()
2180 np = nv_open(fname, sh_subfuntree(shp, true), NV_NOARRAY | NV_VARNAME | NV_NOSCOPE); in sh_exec()
2183 if (!shp->mktype) cp = nv_setdisc(npv, cp, np, (Namfun_t *)npv); in sh_exec()
2196 shp->last_root = rp->sdict; in sh_exec()
2205 if (shp->funload) { in sh_exec()
2206 if (!shp->fpathdict) free(FETCH_VT(np->nvalue, rp)); in sh_exec()
2212 calloc(1, sizeof(struct Ufunction) + (shp->funload ? sizeof(Dtlink_t) : 0)); in sh_exec()
2228 FETCH_VT(np->nvalue, rp)->nspace = shp->namespace; in sh_exec()
2232 FETCH_VT(np->nvalue, rp)->fdict = shp->fun_tree; in sh_exec()
2237 if (shp->funload) { in sh_exec()
2240 if (!shp->fpathdict) shp->fpathdict = dtopen(&_Rpdisc, Dtobag); in sh_exec()
2241 if (shp->fpathdict) { in sh_exec()
2242 dtuserdata(shp->fpathdict, shp, 1); in sh_exec()
2243 dtinsert(shp->fpathdict, rp); in sh_exec()
2264 error_info.line = t->tst.tstline - shp->st.firstline; in sh_exec()
2267 sh_exec(shp, t->lst.lstlef, flags & ARG_OPTIMIZE); in sh_exec()
2268 n = !shp->exitval; in sh_exec()
2274 int savexit = shp->savexit; in sh_exec()
2276 left = sh_macpat(shp, &(t->lst.lstlef->arg), flags & ARG_OPTIMIZE); in sh_exec()
2279 shp, &(t->lst.lstrit->arg), in sh_exec()
2282 shp->savexit = savexit; in sh_exec()
2283 trap = shp->st.trap[SH_DEBUGTRAP]; in sh_exec()
2287 if (sh_isoption(shp, SH_XTRACE)) { in sh_exec()
2288 traceon = sh_trace(shp, NULL, 0); in sh_exec()
2302 sh_debug(shp, trap, NULL, NULL, argv, 0); in sh_exec()
2304 n = test_unop(shp, n, left); in sh_exec()
2317 sh_debug(shp, trap, NULL, NULL, argv, pattern); in sh_exec()
2319 n = test_binop(shp, n, left, right); in sh_exec()
2331 shp->exitval = ((!n) ^ negate); in sh_exec()
2332 if (!skipexitset) exitset(shp); in sh_exec()
2340 int exitval = shp->exitval; in sh_exec()
2342 shp->exitval = exitval; in sh_exec()
2344 if (shp->trapnote || (shp->exitval && sh_isstate(shp, SH_ERREXIT) && t && echeck)) { in sh_exec()
2345 sh_chktrap(shp); in sh_exec()
2351 if (sh_isstate(shp, SH_FORKED)) sh_done(shp, 0); in sh_exec()
2352 if (shp->lastarg != lastarg && shp->lastarg) free(shp->lastarg); in sh_exec()
2357 shp->lastarg = strcpy(lastarg, comn); in sh_exec()
2359 shp->lastarg = strdup(comn); in sh_exec()
2362 if (!skipexitset) exitset(shp); in sh_exec()
2370 if (shp->trapnote & SH_SIGSET) sh_exit(shp, SH_EXITSIG | shp->lastsig); in sh_exec()
2371 if (was_interactive) sh_onstate(shp, SH_INTERACTIVE); in sh_exec()
2372 if (was_monitor && sh_isoption(shp, SH_MONITOR)) sh_onstate(shp, SH_MONITOR); in sh_exec()
2373 if (was_errexit) sh_onstate(shp, SH_ERREXIT); in sh_exec()
2375 return shp->exitval; in sh_exec()
2381 bool sh_trace(Shell_t *shp, char *argv[], int nl) { in sh_trace() argument
2386 if (sh_isoption(shp, SH_XTRACE)) { in sh_trace()
2389 if (!(cp = nv_getval(sh_scoped(shp, VAR_PS4)))) { in sh_trace()
2392 shp->intrace = 1; in sh_trace()
2393 sh_offoption(shp, SH_XTRACE); in sh_trace()
2394 cp = sh_mactry(shp, cp); in sh_trace()
2395 sh_onoption(shp, SH_XTRACE); in sh_trace()
2396 shp->intrace = 0; in sh_trace()
2410 if (decl && shp->prefix && cp != argv0 && *cp != '-') { in sh_trace()
2412 cp = shp->prefix; in sh_trace()
2414 sfputr(sfstderr, shp->prefix, '.'); in sh_trace()
2446 pid_t _sh_fork(Shell_t *shp, pid_t parent, int flags, int *jobid) { in _sh_fork() argument
2453 sh_sigcheck(shp); in _sh_fork()
2476 shp->gd->nforks++; in _sh_fork()
2477 if (job.toclear) job_clear(shp); in _sh_fork()
2481 if (sh_isstate(shp, SH_MONITOR)) { in _sh_fork()
2490 if (!sh_isstate(shp, SH_MONITOR) && job.waitall && postid == 0) job.curpgid = parent; in _sh_fork()
2491 if (flags & FCOOP) shp->cpid = parent; in _sh_fork()
2494 myjob = job_post(shp, parent, postid); in _sh_fork()
2501 if (shp->comsub == 1 && usepipe) { in _sh_fork()
2504 if (shp->vexp->cur > restorevex) sh_vexrestore(shp, restorevex); in _sh_fork()
2506 if (shp->topfd > restorefd) sh_iorestore(shp, restorefd, 0); in _sh_fork()
2507 sh_iounpipe(shp); in _sh_fork()
2512 shp->outpipepid = ((flags & FPOU) ? getpid() : 0); in _sh_fork()
2514 if (shp->trapnote & SH_SIGTERM) sh_exit(shp, SH_EXITSIG | SIGTERM); in _sh_fork()
2515 shp->gd->nforks = 0; in _sh_fork()
2518 if (!job.jobcontrol && !(flags & FAMP)) sh_offstate(shp, SH_MONITOR); in _sh_fork()
2519 if (sh_isstate(shp, SH_MONITOR)) { in _sh_fork()
2537 shp->login_sh = 0; in _sh_fork()
2538 sh_offoption(shp, SH_LOGIN_SHELL); in _sh_fork()
2539 sh_onstate(shp, SH_FORKED); in _sh_fork()
2540 sh_onstate(shp, SH_NOLOG); in _sh_fork()
2541 if (shp->fn_reset) shp->fn_depth = shp->fn_reset = 0; in _sh_fork()
2543 if (!(flags & FSHOWME)) sh_sigreset(shp, 2); in _sh_fork()
2544 shp->subshell = 0; in _sh_fork()
2545 shp->comsub = 0; in _sh_fork()
2546 shp->spid = 0; in _sh_fork()
2547 if ((flags & FAMP) && shp->coutpipe > 1) sh_close(shp->coutpipe); in _sh_fork()
2548 sig = shp->savesig; in _sh_fork()
2549 shp->savesig = 0; in _sh_fork()
2551 sh_sigcheck(shp); in _sh_fork()
2556 pid_t sh_fork(Shell_t *shp, int flags, int *jobid) { in sh_fork() argument
2560 if (!shp->pathlist) path_get(shp, ""); in sh_fork()
2562 shp->trapnote &= ~SH_SIGTERM; in sh_fork()
2566 while (_sh_fork(shp, parent = fork(), flags, jobid) < 0) { in sh_fork()
2571 if (parent == 0 && shp->vex) { in sh_fork()
2572 spawnvex_apply(shp->vex, 0, 0); in sh_fork()
2573 spawnvex_apply(shp->vexp, 0, SPAWN_RESET); in sh_fork()
2591 Shell_t *shp = ((struct Tdata *)data)->sh; in local_exports() local
2597 if (cp && (mp = nv_search(nv_name(np), shp->var_tree, NV_ADD | NV_NOSCOPE)) && nv_isnull(mp)) { in local_exports()
2606 Sfdouble_t sh_mathfun(Shell_t *shp, void *fp, int nargs, Sfdouble_t *arg) { in sh_mathfun() argument
2626 *nr++ = mp = nv_namptr(shp->mathnodes, i); in sh_mathfun()
2633 sh_funscope(shp, 1, argv, 0, &funenv, 0); in sh_mathfun()
2642 static_fn void sh_funct(Shell_t *shp, Namval_t *np, int argn, char *argv[], struct argnod *envlist, in sh_funct() argument
2647 int level, pipepid = shp->pipepid; in sh_funct()
2649 shp->pipepid = 0; in sh_funct()
2651 if (!lp->namfun.disc) lp = init_level(shp, 0); in sh_funct()
2652 if ((struct sh_scoped *)shp->topscope != shp->st.self) sh_setscope(shp, shp->topscope); in sh_funct()
2653 level = lp->maxlevel = shp->dot_depth + shp->fn_depth + 1; in sh_funct()
2655 shp->st.lineno = error_info.line; in sh_funct()
2657 if (nv_isattr(np, NV_FPOSIX) && !sh_isoption(shp, SH_BASH)) { in sh_funct()
2658 int loopcnt = shp->st.loopcnt; in sh_funct()
2659 shp->posix_fun = np; in sh_funct()
2660 shp->st.funname = nv_name(np); in sh_funct()
2661 shp->last_root = nv_dict(VAR_sh); in sh_funct()
2665 shp->st.loopcnt = 0; in sh_funct()
2670 b_source(2, source_argv, &shp->bltindata); in sh_funct()
2676 b_source(argn + 1, source_argv, &shp->bltindata); in sh_funct()
2679 shp->st.loopcnt = loopcnt; in sh_funct()
2684 sh_funscope(shp, argn, argv, 0, &fun, execflg); in sh_funct()
2687 Shscope_t *sp = sh_getscope(shp, 0, SEEK_END); in sh_funct()
2688 sh_setscope(shp, sp); in sh_funct()
2692 shp->last_root = nv_dict(VAR_sh); in sh_funct()
2694 nv_putval(VAR_sh_file, shp->st.filename, NV_NOFREE); in sh_funct()
2695 shp->pipepid = pipepid; in sh_funct()
2707 int sh_fun(Shell_t *shp, Namval_t *np, Namval_t *nq, char *argv[]) { in sh_fun() argument
2713 char *prefix = shp->prefix; in sh_fun()
2719 offset = stktell(shp->stk); in sh_fun()
2720 if (offset > 0) base = stkfreeze(shp->stk, 0); in sh_fun()
2721 shp->prefix = NULL; in sh_fun()
2728 if (nq) mode = set_instance(shp, nq, &node, &nr); in sh_fun()
2731 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in sh_fun()
2732 Shbltin_t *bp = &shp->bltindata; in sh_fun()
2733 sh_pushcontext(shp, buffp, SH_JMPCMD); in sh_fun()
2742 shp->exitval = 0; in sh_fun()
2743 shp->exitval = (funptr(np))(n, argv, bp); in sh_fun()
2745 sh_popcontext(shp, buffp); in sh_fun()
2746 if (jmpval > SH_JMPCMD) siglongjmp(shp->jmplist->buff, jmpval); in sh_fun()
2748 sh_funct(shp, np, n, argv, NULL, sh_isstate(shp, SH_ERREXIT)); in sh_fun()
2752 if (offset > 0) stkset(shp->stk, base, offset); in sh_fun()
2753 shp->prefix = prefix; in sh_fun()
2754 return shp->exitval; in sh_fun()
2760 static_fn void coproc_init(Shell_t *shp, int pipes[]) { in coproc_init() argument
2762 if (shp->coutpipe >= 0 && shp->cpid) { in coproc_init()
2766 shp->cpid = 0; in coproc_init()
2767 if (shp->cpipe[0] <= 0 || shp->cpipe[1] <= 0) { in coproc_init()
2769 sh_pclose(shp->cpipe); in coproc_init()
2770 sh_pipe(shp->cpipe); in coproc_init()
2771 outfd = shp->cpipe[1]; in coproc_init()
2773 int fd = sh_fcntl(shp->cpipe[1], F_DUPFD_CLOEXEC, 10); in coproc_init()
2775 shp->fdstatus[fd] = (shp->fdstatus[outfd] & ~IOCLEX); in coproc_init()
2777 shp->fdstatus[outfd] = IOCLOSE; in coproc_init()
2778 shp->cpipe[1] = fd; in coproc_init()
2781 shp->fdptrs[shp->cpipe[0]] = shp->cpipe; in coproc_init()
2783 shp->outpipe = shp->cpipe; in coproc_init()
2784 sh_pipe(shp->inpipe = pipes); in coproc_init()
2785 shp->coutpipe = shp->inpipe[1]; in coproc_init()
2786 shp->fdptrs[shp->coutpipe] = &shp->coutpipe; in coproc_init()
2791 static_fn void sigreset(Shell_t *shp, int mode) { in sigreset() argument
2793 int sig = shp->st.trapmax; in sigreset()
2802 if (shp->sigflag[sig] & SH_SIGOFF) return; in sigreset()
2803 trap = shp->st.trapcom[sig]; in sigreset()
2813 static_fn pid_t sh_ntfork(Shell_t *shp, const Shnode_t *t, char *argv[], int *jobid, int flag) { in sh_ntfork() argument
2817 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in sh_ntfork()
2818 int otype = 0, jmpval, jobfork = 0, lineno = shp->st.firstline; in sh_ntfork()
2828 sh_pushcontext(shp, buffp, SH_JMPCMD); in sh_ntfork()
2832 if ((otype & FINT) && !sh_isstate(shp, SH_MONITOR)) { in sh_ntfork()
2838 shp->errorfd = error_info.fd; in sh_ntfork()
2840 sh_redirect(shp, t->com.comio, io_usevex(t->com.comio)); in sh_ntfork()
2842 sh_redirect(shp, t->com.comio, 0); in sh_ntfork()
2848 sh_scope(shp, t->com.comset, 0); in sh_ntfork()
2852 np = nv_search(path, shp->track_tree, 0); in sh_ntfork()
2855 } else if (path_absolute(shp, path, NULL)) { in sh_ntfork()
2856 path = stkptr(shp->stk, PATH_OFFSET); in sh_ntfork()
2857 stkfreeze(shp->stk, 0); in sh_ntfork()
2859 pp = path_get(shp, path); in sh_ntfork()
2866 } else if (sh_isoption(shp, SH_RESTRICTED)) { in sh_ntfork()
2874 arge = sh_envgen(shp); in sh_ntfork()
2876 shp->st.firstline = lineno; in sh_ntfork()
2877 shp->exitval = 0; in sh_ntfork()
2879 if (sh_isstate(shp, SH_MONITOR) && (job.jobcontrol || (otype & FAMP))) { in sh_ntfork()
2889 sigreset(shp, 0); // set signals to ignore in sh_ntfork()
2892 for (pp = path_get(shp, argv[0]); pp && !pp->lib; pp = pp->next) { in sh_ntfork()
2897 spawnpid = path_spawn(shp, path, argv, arge, pp, (grp << 1) | 1); in sh_ntfork()
2905 sfprintf(shp->strbuf, "/dev/fd/%d", fd); in sh_ntfork()
2906 if (stat(devfd = sfstruse(shp->strbuf), &statb) >= 0) argv[0] = devfd; in sh_ntfork()
2908 if (!shp->gd->shpath) shp->gd->shpath = pathshell(); in sh_ntfork()
2909 spawnpid = path_spawn(shp, shp->gd->shpath, &argv[-1], arge, pp, (grp << 1) | 1); in sh_ntfork()
2916 switch (errno = shp->path_err) { in sh_ntfork()
2928 exitset(shp); in sh_ntfork()
2930 sh_popcontext(shp, buffp); in sh_ntfork()
2932 if (sigwasset) sigreset(shp, 1); /* restore ignored signals */ in sh_ntfork()
2934 sh_unscope(shp); in sh_ntfork()
2936 sh_setlist(shp, t->com.comset, NV_EXPORT | NV_IDENT | NV_ASSIGN, 0); in sh_ntfork()
2940 sh_iorestore(shp, buffp->topfd, jmpval); in sh_ntfork()
2942 if (shp->vexp->cur > buffp->vexi) sh_vexrestore(shp, buffp->vexi); in sh_ntfork()
2945 if (jmpval > SH_JMPCMD) siglongjmp(shp->jmplist->buff, jmpval); in sh_ntfork()
2947 _sh_fork(shp, spawnpid, otype, jobid); in sh_ntfork()
2972 int sh_funscope(Shell_t *shp, int argn, char *argv[], int (*fun)(void *), void *arg, int execflg) { in sh_funscope() argument
2985 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in sh_funscope()
2986 Namval_t *nspace = shp->namespace; in sh_funscope()
2987 Dt_t *last_root = shp->last_root; in sh_funscope()
2990 options = shp->options; in sh_funscope()
2991 if (shp->fn_depth == 0) { in sh_funscope()
2992 shp->glob_options = shp->options; in sh_funscope()
2994 shp->options = shp->glob_options; in sh_funscope()
2996 prevscope = shp->st.self; in sh_funscope()
2997 *prevscope = shp->st; in sh_funscope()
2998 sh_offoption(shp, SH_ERREXIT); in sh_funscope()
2999 shp->st.prevst = prevscope; in sh_funscope()
3001 shp->st.self = &savst; in sh_funscope()
3002 shp->topscope = (Shscope_t *)shp->st.self; in sh_funscope()
3003 shp->st.opterror = shp->st.optchar = 0; in sh_funscope()
3004 shp->st.optindex = 1; in sh_funscope()
3005 shp->st.loopcnt = 0; in sh_funscope()
3008 shp->st.real_fun = FETCH_VT((fp->node)->nvalue, rp); in sh_funscope()
3011 prevscope->save_tree = shp->var_tree; in sh_funscope()
3012 n = dtvnext(prevscope->save_tree) != (shp->namespace ? shp->var_base : 0); in sh_funscope()
3013 sh_scope(shp, envlist, 1); in sh_funscope()
3017 tdata.sh = shp; in sh_funscope()
3021 shp->st.save_tree = shp->var_tree; in sh_funscope()
3024 sh_onoption(shp, SH_XTRACE); in sh_funscope()
3026 sh_offoption(shp, SH_XTRACE); in sh_funscope()
3029 shp->st.cmdname = argv[0]; in sh_funscope()
3031 nsig = shp->st.trapmax; in sh_funscope()
3032 if (nsig > 0 || shp->st.trapcom[0]) { in sh_funscope()
3036 savsig[isig] = shp->st.trapcom[isig] ? strdup(shp->st.trapcom[isig]) : NULL; in sh_funscope()
3039 sh_sigreset(shp, 0); in sh_funscope()
3040 argsav = sh_argnew(shp, argv, &saveargfor); in sh_funscope()
3041 sh_pushcontext(shp, buffp, SH_JMPFUN); in sh_funscope()
3044 shp->st.var_local = shp->var_tree; in sh_funscope()
3046 shp->st.filename = FETCH_VT(fp->node->nvalue, rp)->fname; in sh_funscope()
3047 shp->st.funname = nv_name(fp->node); in sh_funscope()
3048 shp->last_root = nv_dict(VAR_sh); in sh_funscope()
3049 nv_putval(VAR_sh_file, shp->st.filename, NV_NOFREE); in sh_funscope()
3050 nv_putval(VAR_sh_fun, shp->st.funname, NV_NOFREE); in sh_funscope()
3052 if ((execflg & sh_state(SH_NOFORK))) shp->end_fn = 1; in sh_funscope()
3055 if (shp->fn_depth++ > MAXDEPTH) { in sh_funscope()
3056 shp->toomany = 1; in sh_funscope()
3057 siglongjmp(shp->jmplist->buff, SH_JMPERRFN); in sh_funscope()
3061 char **args = shp->st.real_fun->argv; in sh_funscope()
3065 shp->last_root = NULL; in sh_funscope()
3067 np = nv_search(args[r], shp->var_tree, NV_NOSCOPE | NV_ADD); in sh_funscope()
3086 sh_exec(shp, (Shnode_t *)(nv_funtree((fp->node))), execflg | SH_ERREXIT); in sh_funscope()
3087 r = shp->exitval; in sh_funscope()
3090 if (shp->topscope != (Shscope_t *)shp->st.self) sh_setscope(shp, shp->topscope); in sh_funscope()
3091 if (--shp->fn_depth == 1 && jmpval == SH_JMPERRFN) { in sh_funscope()
3095 sh_popcontext(shp, buffp); in sh_funscope()
3096 sh_unscope(shp); in sh_funscope()
3097 shp->namespace = nspace; in sh_funscope()
3098 shp->var_tree = prevscope->save_tree; in sh_funscope()
3099 sh_argreset(shp, argsav, saveargfor); in sh_funscope()
3100 trap = shp->st.trapcom[0]; in sh_funscope()
3101 shp->st.trapcom[0] = 0; in sh_funscope()
3102 sh_sigreset(shp, 1); in sh_funscope()
3103 shp->st = *prevscope; in sh_funscope()
3104 shp->topscope = (Shscope_t *)prevscope; in sh_funscope()
3105 nv_getval(sh_scoped(shp, VAR_IFS)); in sh_funscope()
3106 shp->end_fn = 0; in sh_funscope()
3109 if (shp->st.trapcom[isig]) { in sh_funscope()
3110 free(shp->st.trapcom[isig]); in sh_funscope()
3113 memcpy(shp->st.trapcom, savsig, nsig * sizeof(char *)); in sh_funscope()
3116 shp->trapnote = 0; in sh_funscope()
3117 shp->options = options; in sh_funscope()
3118 shp->last_root = last_root; in sh_funscope()
3119 if (jmpval == SH_JMPSUB) siglongjmp(shp->jmplist->buff, jmpval); in sh_funscope()
3121 sh_trap(shp, trap, 0); in sh_funscope()
3124 if (jmpval) r = shp->exitval; in sh_funscope()
3125 if (!sh_isstate(shp, SH_IOPROMPT) && r > SH_EXITSIG && in sh_funscope()
3130 sh_chktrap(shp); in sh_funscope()
3131 siglongjmp(shp->jmplist->buff, jmpval); in sh_funscope()
3139 int sh_eval(Shell_t *shp, Sfio_t *iop, int mode) { in sh_eval() argument
3141 struct slnod *saveslp = shp->st.staklist; in sh_eval()
3143 checkpt_t *pp = shp->jmplist; in sh_eval()
3144 checkpt_t *buffp = stkalloc(shp->stk, sizeof(checkpt_t)); in sh_eval()
3148 int binscript = shp->binscript; in sh_eval()
3149 char comsub = shp->comsub; in sh_eval()
3153 shp->binscript = 0; in sh_eval()
3154 shp->comsub = 0; in sh_eval()
3158 shp->fn_reset = 1; in sh_eval()
3160 sh_pushcontext(shp, buffp, SH_JMPEVAL); in sh_eval()
3165 lineno = shp->inlineno; in sh_eval()
3166 traceon = sh_isoption(shp, SH_XTRACE); in sh_eval()
3167 if (traceon) sh_offoption(shp, SH_XTRACE); in sh_eval()
3169 t = sh_parse(shp, iop, (mode & (SH_READEVAL | SH_FUNEVAL)) ? mode & SH_FUNEVAL : SH_NL); in sh_eval()
3176 if (!sh_isoption(shp, SH_VERBOSE)) sh_offstate(shp, SH_VERBOSE); in sh_eval()
3177 if ((mode & ~SH_FUNEVAL) && shp->gd->hist_ptr) { in sh_eval()
3178 hist_flush(shp->gd->hist_ptr); in sh_eval()
3181 sh_exec(shp, t, in sh_eval()
3182 sh_isstate(shp, SH_ERREXIT) | sh_isstate(shp, SH_NOFORK) | (mode & ~SH_FUNEVAL)); in sh_eval()
3185 sh_popcontext(shp, buffp); in sh_eval()
3186 shp->binscript = binscript; in sh_eval()
3187 shp->comsub = comsub; in sh_eval()
3188 if (traceon) sh_onoption(shp, SH_XTRACE); in sh_eval()
3189 if (lineno) shp->inlineno = lineno; in sh_eval()
3192 sh_freeup(shp); in sh_eval()
3193 shp->st.staklist = saveslp; in sh_eval()
3194 shp->fn_reset = 0; in sh_eval()
3195 if (jmpval > SH_JMPEVAL) siglongjmp(shp->jmplist->buff, jmpval); in sh_eval()
3196 return shp->exitval; in sh_eval()
3199 int sh_run(Shell_t *shp, int argn, char *argv[]) { in sh_run() argument
3201 struct comnod *t = stkalloc(shp->stk, sizeof(struct comnod)); in sh_run()
3202 int savtop = stktell(shp->stk); in sh_run()
3206 bltindata = shp->bltindata; in sh_run()
3208 dp = stkalloc(shp->stk, in sh_run()
3215 t->comnamp = nv_bfsearch(argv[0], shp->fun_tree, (Namval_t **)&t->comnamq, NULL); in sh_run()
3217 argn = sh_exec(shp, (Shnode_t *)t, sh_isstate(shp, SH_ERREXIT)); in sh_run()
3218 shp->bltindata = bltindata; in sh_run()
3219 if (savptr != stkptr(shp->stk, 0)) { in sh_run()
3220 stkset(shp->stk, savptr, savtop); in sh_run()
3222 stkseek(shp->stk, savtop); in sh_run()