From 8a0920ea0a73200fe96348634f67c57aa6ae6ecd Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Mon, 4 Jul 2022 00:16:05 +0200 Subject: [PATCH] Yet another round of accumulated tweaks and cleanups Notable changes: - sh/timers.c: Rename timerdel() to sh_timerdel() for consistency; we had timerdel() but sh_timeradd(). - include/shell.h: Remove unused sh struct members: - sh.lastpath (this was still being saved/restored in several places, but not actually used since 2008-06-02 ksh93t-) - sh.lastbase (unused since 2000-10-31 ksh93k) - sh.inpool (unused since libcoshell was removed in 3613da42) - sh/xec.c: sh_exec(): Add comments for the various command types. --- src/cmd/ksh93/bltins/cd_pwd.c | 5 +- src/cmd/ksh93/bltins/read.c | 4 +- src/cmd/ksh93/edit/edit.c | 2 +- src/cmd/ksh93/edit/vi.c | 4 +- src/cmd/ksh93/include/fault.h | 2 +- src/cmd/ksh93/include/shell.h | 11 ++--- src/cmd/ksh93/sh/args.c | 1 - src/cmd/ksh93/sh/array.c | 1 - src/cmd/ksh93/sh/io.c | 4 +- src/cmd/ksh93/sh/name.c | 1 - src/cmd/ksh93/sh/nvtype.c | 1 - src/cmd/ksh93/sh/path.c | 2 +- src/cmd/ksh93/sh/timers.c | 2 +- src/cmd/ksh93/sh/xec.c | 86 ++++++++++++++++++++++++++------- src/lib/libast/port/mnt.c | 4 +- src/lib/libast/vmalloc/vmbest.c | 4 +- 16 files changed, 87 insertions(+), 47 deletions(-) diff --git a/src/cmd/ksh93/bltins/cd_pwd.c b/src/cmd/ksh93/bltins/cd_pwd.c index 43f3fc7e4e90..8dc2fdad6a32 100644 --- a/src/cmd/ksh93/bltins/cd_pwd.c +++ b/src/cmd/ksh93/bltins/cd_pwd.c @@ -167,14 +167,13 @@ int b_cd(int argc, char *argv[],Shbltin_t *context) dp = cdpath?cdpath->name:""; cdpath = path_nextcomp(cdpath,dir,0); #if _WINIX - if(*stakptr(PATH_OFFSET+1)==':' && isalpha(*stakptr(PATH_OFFSET))) + if(*stakptr(PATH_OFFSET+1)==':' && isalpha(*stakptr(PATH_OFFSET))) { *stakptr(PATH_OFFSET+1) = *stakptr(PATH_OFFSET); *stakptr(PATH_OFFSET)='/'; } #endif /* _WINIX */ - if(*stakptr(PATH_OFFSET)!='/') - + if(*stakptr(PATH_OFFSET)!='/') { char *last=(char*)stakfreeze(1); stakseek(PATH_OFFSET); diff --git a/src/cmd/ksh93/bltins/read.c b/src/cmd/ksh93/bltins/read.c index 9d2969b87ae7..d78f206deb34 100644 --- a/src/cmd/ksh93/bltins/read.c +++ b/src/cmd/ksh93/bltins/read.c @@ -464,7 +464,7 @@ int sh_readline(char **names, volatile int fd, int flags, ssize_t size, long tim } } if(timeslot) - timerdel(timeslot); + sh_timerdel(timeslot); if(binary && !((size=nv_size(np)) && nv_isarray(np) && c!=size)) { if((c==size) && np->nvalue.cp && !nv_isarray(np)) @@ -500,7 +500,7 @@ int sh_readline(char **names, volatile int fd, int flags, ssize_t size, long tim } } if(timeslot) - timerdel(timeslot); + sh_timerdel(timeslot); if((flags&S_FLAG) && !sh.hist_ptr) { sh_histinit(); diff --git a/src/cmd/ksh93/edit/edit.c b/src/cmd/ksh93/edit/edit.c index 8e95ca0e4796..e40c95002e81 100644 --- a/src/cmd/ksh93/edit/edit.c +++ b/src/cmd/ksh93/edit/edit.c @@ -211,7 +211,7 @@ int tty_set(int fd, int action, struct termios *tty) /*{ TTY_COOKED( fd ) * * This routine will set the tty in cooked mode. - * It is also called by error.done(). + * It is also called by sh_done(). * }*/ diff --git a/src/cmd/ksh93/edit/vi.c b/src/cmd/ksh93/edit/vi.c index c36c945c753c..f0c3a7cd2974 100644 --- a/src/cmd/ksh93/edit/vi.c +++ b/src/cmd/ksh93/edit/vi.c @@ -193,10 +193,10 @@ static int search(Vi_t*,int); static void sync_cursor(Vi_t*); static int textmod(Vi_t*,int,int); -/*+ VI_READ( fd, shbuf, nchar ) +/*+ ED_VIREAD( context, fd, shbuf, nchar, reedit ) * * This routine implements a one line version of vi and is - * called by _filbuf.c + * called by slowread() in io.c * -*/ diff --git a/src/cmd/ksh93/include/fault.h b/src/cmd/ksh93/include/fault.h index 8943e98f2f6c..f8682323a74f 100644 --- a/src/cmd/ksh93/include/fault.h +++ b/src/cmd/ksh93/include/fault.h @@ -124,7 +124,7 @@ extern void sh_siginit(void); extern void sh_sigtrap(int); extern void sh_sigreset(int); extern void *sh_timeradd(unsigned long,int ,void (*)(void*),void*); -extern void timerdel(void*); +extern void sh_timerdel(void*); extern const char e_alarm[]; diff --git a/src/cmd/ksh93/include/shell.h b/src/cmd/ksh93/include/shell.h index 96ad0ca73014..33aca423d72f 100644 --- a/src/cmd/ksh93/include/shell.h +++ b/src/cmd/ksh93/include/shell.h @@ -207,10 +207,10 @@ struct sh_scoped int ioset; unsigned short trapmax; char trapdontexec; /* stop exec optimization if any non-DEBUG/SIGKILL/SIGSTOP trap is set and non-empty */ - char *trap[SH_DEBUGTRAP+1]; - char **otrap; - char **trapcom; - char **otrapcom; + char *trap[SH_DEBUGTRAP+1]; /* pseudosignals (except EXIT) */ + char **otrap; /* save parent pseudosignals for v=$(trap) */ + char **trapcom; /* EXIT and signals */ + char **otrapcom; /* save parent EXIT and signals for v=$(trap) */ struct Ufunction *real_fun; /* current 'function name' function */ int repl_index; char *repl_arg; @@ -276,7 +276,6 @@ struct Shell_s Sfio_t *funlog; /* for logging function definitions */ int **fdptrs; /* pointer to file numbers */ char *lastarg; - char *lastpath; /* last absolute path found */ int path_err; /* last error on path search */ Dt_t *var_base; /* global level variables */ Dt_t *fun_base; /* global level functions */ @@ -308,7 +307,6 @@ struct Shell_s unsigned char *sigflag; /* pointer to signal states */ char intrap; char login_sh; - char lastbase; char forked; char binscript; char funload; @@ -375,7 +373,6 @@ struct Shell_s Dt_t *prev_root; Dt_t *fpathdict; Dt_t *typedict; - Dt_t *inpool; char ifstable[256]; Shopt_t offoptions; /* options that were explicitly disabled by the user on the command line */ Shopt_t glob_options; diff --git a/src/cmd/ksh93/sh/args.c b/src/cmd/ksh93/sh/args.c index 27aa9b4003c7..b37f32f3ff84 100644 --- a/src/cmd/ksh93/sh/args.c +++ b/src/cmd/ksh93/sh/args.c @@ -650,7 +650,6 @@ char **sh_argbuild(int *nargs, const struct comnod *comptr,int flag) *nargs = ap->dolnum; return(ap->dolval+ap->dolbot); } - sh.lastpath = 0; *nargs = 0; if(ac) { diff --git a/src/cmd/ksh93/sh/array.c b/src/cmd/ksh93/sh/array.c index ded9dbfd8039..bf2e84387a55 100644 --- a/src/cmd/ksh93/sh/array.c +++ b/src/cmd/ksh93/sh/array.c @@ -608,7 +608,6 @@ static void array_putval(Namval_t *np, const char *string, int flags, Namfun_t * if(mp && mp!=np) { if(!is_associative(ap) && string && !(flags&NV_APPEND) && !nv_type(np) && nv_isvtree(mp) && !(ap->nelem&ARRAY_TREE)) - { if(!nv_isattr(np,NV_NOFREE)) _nv_unset(mp,flags&NV_RDONLY); diff --git a/src/cmd/ksh93/sh/io.c b/src/cmd/ksh93/sh/io.c index 5db81ba3ef4c..e02c4ee6d1e0 100644 --- a/src/cmd/ksh93/sh/io.c +++ b/src/cmd/ksh93/sh/io.c @@ -1607,9 +1607,7 @@ static int io_heredoc(register struct ionod *iop, const char *name, int traceon) } if(!(iop->iofile&IOQUOTE)) { - char *lastpath = sh.lastpath; sh_machere(infile,outfile,iop->ioname); - sh.lastpath = lastpath; if(infile) sfclose(infile); } @@ -1989,7 +1987,7 @@ static ssize_t slowread(Sfio_t *iop,void *buff,register size_t size,Sfdisc_t *ha timeout = (void*)sh_timeradd(sh_isstate(SH_GRACE)?1000L*TGRACE:1000L*sh.timeout,0,time_grace,&sh); rsize = (*readf)(sh.ed_context, fno, (char*)buff, size, reedit); if(timeout) - timerdel(timeout); + sh_timerdel(timeout); timeout=0; #if SHOPT_HISTEXPAND if(rsize && *(char*)buff != '\n' && sh.nextprompt==1 && sh_isoption(SH_HISTEXPAND)) diff --git a/src/cmd/ksh93/sh/name.c b/src/cmd/ksh93/sh/name.c index 4841f655500b..f645f01b1f9b 100644 --- a/src/cmd/ksh93/sh/name.c +++ b/src/cmd/ksh93/sh/name.c @@ -2421,7 +2421,6 @@ static void table_unset(register Dt_t *root, int flags, Dt_t *oroot) int len = strlen(np->nvname); npnext = (Namval_t*)dtnext(root,np); while((nq=npnext) && strncmp(np->nvname,nq->nvname,len)==0 && nq->nvname[len]=='.') - { _nv_unset(nq,flags); npnext = (Namval_t*)dtnext(root,nq); diff --git a/src/cmd/ksh93/sh/nvtype.c b/src/cmd/ksh93/sh/nvtype.c index 0032e288bc0d..94302c199f3c 100644 --- a/src/cmd/ksh93/sh/nvtype.c +++ b/src/cmd/ksh93/sh/nvtype.c @@ -532,7 +532,6 @@ static void put_type(Namval_t* np, const char* val, int flag, Namfun_t* fp) { Namfun_t *pp; if((pp=nv_hasdisc(nq,fp->disc)) && pp->type==fp->type) - { if(!nq->nvenv) flag |= NV_EXPORT; diff --git a/src/cmd/ksh93/sh/path.c b/src/cmd/ksh93/sh/path.c index 1937ff0d4508..6ec3822f4b97 100644 --- a/src/cmd/ksh93/sh/path.c +++ b/src/cmd/ksh93/sh/path.c @@ -996,7 +996,7 @@ noreturn void path_exec(register const char *arg0,register char *argv[],struct a pp=path_get(arg0); sh.path_err= ENOENT; sfsync(NIL(Sfio_t*)); - timerdel(NIL(void*)); + sh_timerdel(NIL(void*)); /* find first path that has a library component */ while(pp && (pp->flags&PATH_SKIP)) pp = pp->next; diff --git a/src/cmd/ksh93/sh/timers.c b/src/cmd/ksh93/sh/timers.c index f61393f277d4..525a4b8ee3f8 100644 --- a/src/cmd/ksh93/sh/timers.c +++ b/src/cmd/ksh93/sh/timers.c @@ -229,7 +229,7 @@ void *sh_timeradd(unsigned long msec,int flags,void (*action)(void*),void *handl /* * delete timer . If is NULL, all timers are deleted */ -void timerdel(void *handle) +void sh_timerdel(void *handle) { register Timer_t *tp = (Timer_t*)handle; if(tp) diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index 319b77db4822..0dd00bb808f3 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -916,6 +916,9 @@ static int check_exec_optimization(int type, int execflg, int execflg2, struct i return(1); } +/* + * Main execution function: execute any type of command. + */ int sh_exec(register const Shnode_t *t, int flags) { Stk_t *stkp = sh.stk; @@ -953,9 +956,11 @@ int sh_exec(register const Shnode_t *t, int flags) sh.exitval=0; sh.lastsig = 0; sh.chldexitsig = 0; - sh.lastpath = 0; switch(type&COMMSK) { + /* + * Simple command + */ case TCOM: { register struct argnod *argp; @@ -1172,7 +1177,6 @@ int sh_exec(register const Shnode_t *t, int flags) } if(io) sfsync(sh.outpool); - sh.lastpath = 0; if(!np) { if(*com0 == '/' && !sh_isoption(SH_RESTRICTED)) @@ -1442,7 +1446,6 @@ int sh_exec(register const Shnode_t *t, int flags) jmpval = sigsetjmp(buffp->buff,0); } if(jmpval == 0) - { if(io) indx = sh_redirect(io,execflg); @@ -1483,6 +1486,7 @@ int sh_exec(register const Shnode_t *t, int flags) siglongjmp(*sh.jmplist,jmpval); goto setexit; } + /* not a built-in or function: external command, fall through to TFORK */ } else if(!io) { @@ -1497,6 +1501,10 @@ int sh_exec(register const Shnode_t *t, int flags) } } /* FALLTHROUGH */ + + /* + * Any command that needs the shell to fork (e.g. background or external) + */ case TFORK: { register pid_t parent; @@ -1650,7 +1658,7 @@ int sh_exec(register const Shnode_t *t, int flags) fd = (type&FPIN) ? 0 : 1; fn = sh_open(sh.fifo,fd?O_WRONLY:O_RDONLY); save_errno = errno; - timerdel(fifo_timer); + sh_timerdel(fifo_timer); sh.fifo = 0; if(fn<0) { @@ -1738,12 +1746,13 @@ int sh_exec(register const Shnode_t *t, int flags) } } - case TSETIO: - { /* + * Redirection: * don't create a new process, just * save and restore io-streams */ + case TSETIO: + { pid_t pid = 0; int jmpval, waitall = 0; int simple = (t->fork.forktre->tre.tretyp&COMMSK)==TCOM; @@ -1827,6 +1836,9 @@ int sh_exec(register const Shnode_t *t, int flags) break; } + /* + * Parentheses subshell block + */ case TPAR: echeck = 1; flags &= ~OPTIMIZE_FLAG; @@ -1861,13 +1873,13 @@ int sh_exec(register const Shnode_t *t, int flags) sh_subshell(t->par.partre,flags,0); break; - case TFIL: - { /* - * This code sets up a pipe. + * Pipe: command | command * All elements of the pipe are started by the parent. - * The last element executes in current environment + * The last element is executed in the current environment. */ + case TFIL: + { int pvo[3]; /* old pipe for multi-stage */ int pvn[3]; /* current set up pipe */ int savepipe = pipejob; @@ -1976,9 +1988,11 @@ int sh_exec(register const Shnode_t *t, int flags) break; } + /* + * List of semicolon-separated commands + */ case TLST: { - /* a list of commands is executed here */ do { sh_exec(t->lst.lstlef,errorflg|OPTIMIZE); @@ -1993,6 +2007,9 @@ int sh_exec(register const Shnode_t *t, int flags) break; } + /* + * Logical and: command && command + */ case TAND: if(type&TTEST) skipexitset++; @@ -2000,6 +2017,9 @@ int sh_exec(register const Shnode_t *t, int flags) sh_exec(t->lst.lstrit,flags); break; + /* + * Logical or: command || command + */ case TORF: if(type&TTEST) skipexitset++; @@ -2007,7 +2027,10 @@ int sh_exec(register const Shnode_t *t, int flags) sh_exec(t->lst.lstrit,flags); break; - case TFOR: /* for and select */ + /* + * Loop: 'for' or 'select' + */ + case TFOR: { register char **args; register int nargs; @@ -2135,7 +2158,10 @@ int sh_exec(register const Shnode_t *t, int flags) break; } - case TWH: /* while and until */ + /* + * Loop: 'while' or 'until' + */ + case TWH: { volatile int r=0; int first = OPTIMIZE_FLAG; @@ -2226,7 +2252,11 @@ int sh_exec(register const Shnode_t *t, int flags) #endif /* SHOPT_FILESCAN */ break; } - case TARITH: /* (( expression )) */ + + /* + * Arithmetic command: ((expression)) + */ + case TARITH: { register char *trap; char *arg[4]; @@ -2252,6 +2282,9 @@ int sh_exec(register const Shnode_t *t, int flags) break; } + /* + * Conditional block: if ... fi + */ case TIF: if(sh_exec(t->if_.iftre,OPTIMIZE)==0) sh_exec(t->if_.thtre,flags); @@ -2261,6 +2294,9 @@ int sh_exec(register const Shnode_t *t, int flags) sh.exitval=0; /* force zero exit for if-then-fi */ break; + /* + * Switch block: case ... esac + */ case TSW: { Shnode_t *tt = (Shnode_t*)t; @@ -2309,9 +2345,11 @@ int sh_exec(register const Shnode_t *t, int flags) break; } + /* + * The 'time' keyword: time a pipeline + */ case TTIME: { - /* time the command */ const char *format = e_timeformat; struct timeval ta, tb; #ifdef timeofday @@ -2391,6 +2429,10 @@ int sh_exec(register const Shnode_t *t, int flags) p_time(sfstderr,sh_translate(format),tm); break; } + + /* + * Function definition + */ case TFUN: { register Namval_t *np=0; @@ -2399,6 +2441,7 @@ int sh_exec(register const Shnode_t *t, int flags) register char *cp = strrchr(fname,'.'); register Namval_t *npv=0,*mp; #if SHOPT_NAMESPACE + /* Namespace definition: a modified function definition */ if(t->tre.tretyp==TNSPACE) { Dt_t *root; @@ -2572,7 +2615,9 @@ int sh_exec(register const Shnode_t *t, int flags) break; } - /* new test compound command */ + /* + * The [[ keyword: new test compound command + */ case TTST: { register int n; @@ -2703,6 +2748,11 @@ int sh_exec(register const Shnode_t *t, int flags) return(sh.exitval); } +/* + * Public API function: run the command given by by the argument list argv, + * containing argn elements. If argv[0] does not contain a /, check for a + * built-in or function before performing a path search. + */ int sh_run(int argn, char *argv[]) { register struct dolnod *dp; @@ -2830,7 +2880,7 @@ pid_t _sh_fork(register pid_t parent,int flags,int *jobid) pause(); else if(forkcnt>1000L) forkcnt /= 2; - timerdel(timeout); + sh_timerdel(timeout); timeout = 0; } return(-1); @@ -2895,7 +2945,7 @@ pid_t _sh_fork(register pid_t parent,int flags,int *jobid) sh.outpipepid = ((flags&FPOU)?sh.current_pid:0); if(sh.trapnote&SH_SIGTERM) sh_exit(SH_EXITSIG|SIGTERM); - timerdel(NIL(void*)); + sh_timerdel(NIL(void*)); #ifdef JOBS if(sh_isstate(SH_MONITOR)) { diff --git a/src/lib/libast/port/mnt.c b/src/lib/libast/port/mnt.c index 64e678dde517..606a0ff884ff 100644 --- a/src/lib/libast/port/mnt.c +++ b/src/lib/libast/port/mnt.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1985-2011 AT&T Intellectual Property * -* Copyright (c) 2020-2021 Contributors to ksh 93u+m * +* Copyright (c) 2020-2022 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 1.0 * * by AT&T Intellectual Property * @@ -518,13 +518,13 @@ extern struct mntent* getmntent(FILE*); #if _lib_getmntent -typedef struct #if _mem_mnt_opts_mntent #define OPTIONS(p) ((p)->mnt_opts) #else #define OPTIONS(p) NiL #endif +typedef struct { Header_t hdr; FILE* fp; diff --git a/src/lib/libast/vmalloc/vmbest.c b/src/lib/libast/vmalloc/vmbest.c index bc9cc671da7c..18ff1cd16df0 100644 --- a/src/lib/libast/vmalloc/vmbest.c +++ b/src/lib/libast/vmalloc/vmbest.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1985-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2021 Contributors to ksh 93u+m * +* Copyright (c) 2020-2022 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 1.0 * * by AT&T Intellectual Property * @@ -766,7 +766,7 @@ static int bestfree(Vmalloc_t* vm, void* data, int local ) } static void* bestresize(Vmalloc_t* vm, /* region allocating from */ - void* data, /* old block of data */ + void* data, /* old block of data */ reg size_t size, /* new size */ int type, /* !=0 to move, <0 for not copy */ int local)