static void comsubst(void) { /* command substn */ FILEBLK cb; register char d; register STKPTR savptr = fixstak(); usestak(); while ((d = readc()) != SQUOTE && d) pushstak(d); { register char *argc; trim(argc = fixstak()); push(&cb); estabf(argc); } { register TREPTR t = makefork(FPOU, cmd(EOFSYM, MTFLG | NLFLG)); int pv[2]; /* this is done like this so that the pipe * is open only when needed */ chkpipe(pv); initf(pv[INPIPE]); execute(t, 0, 0, pv); close(pv[OTPIPE]); } tdystak(savptr); staktop = movstr(savptr, stakbot); while (d = readc()) pushstak(d | quote); await(0); while (stakbot != staktop) { if ((*--staktop & STRIP) != NL) { ++staktop; break; } } pop(); }
int readc(void) { register char c; register int len; register FILE f; retry: if (peekc) { c = peekc; peekc = 0; } else if ((f = standin, f->fnxt != f->fend)) { if ((c = *f->fnxt++) == 0) { if (f->feval) { if (estabf(*f->feval++)) c = EOF; else c = SP; } else { goto retry; /* = c=readc(); */ } } if (flags & readpr && standin->fstak == 0) { prc(c); } if (c == NL) { f->flin++; } } else if (f->feof || f->fdes < 0) { c = EOF; f->feof++; } else if ((len = readb()) <= 0) { close(f->fdes); f->fdes = -1; c = EOF; f->feof++; } else { f->fend = (f->fnxt = f->fbuf) + len; goto retry; } return c; }
char *macro(char *as) { /* Strip "" and do $ substitution * Leaves result on top of stack */ register BOOL savqu = quoted; register CHAR savq = quote; FILEHDR fb; push((void *)&fb);/*FIXME*/ estabf(as); usestak(); quote = 0; quoted = 0; copyto(0); pop(); if (quoted && (stakbot == staktop)) { pushstak(QUOTE); } quote = savq; quoted = savqu; return (fixstak()); }
int main(int c, char *v[], char *e[]) { int rflag = ttyflg; int rsflag = 1; /* local restricted flag */ unsigned char *flagc = flagadr; struct namnod *n; mypid = getpid(); mypgid = getpgid(mypid); mysid = getsid(mypid); /* * Do locale processing only if /usr is mounted. */ localedir_exists = (access(localedir, F_OK) == 0); /* * initialize storage allocation */ if (stakbot == 0) { addblok((unsigned)0); } /* * If the first character of the last path element of v[0] is "-" * (ex. -sh, or /bin/-sh), this is a login shell */ if (*simple(v[0]) == '-') { signal(SIGXCPU, SIG_DFL); signal(SIGXFSZ, SIG_DFL); /* * As the previous comment states, this is a login shell. * Therefore, we set the login_shell flag to explicitly * indicate this condition. */ login_shell = TRUE; } stdsigs(); /* * set names from userenv */ setup_env(); /* * LC_MESSAGES is set here so that early error messages will * come out in the right style. * Note that LC_CTYPE is done later on and is *not* * taken from the previous environ */ /* * Do locale processing only if /usr is mounted. */ if (localedir_exists) (void) setlocale(LC_ALL, ""); #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ #endif (void) textdomain(TEXT_DOMAIN); /* * 'rsflag' is zero if SHELL variable is * set in environment and * the simple file part of the value. * is rsh */ if (n = findnam("SHELL")) { if (eq("rsh", simple(n->namval))) rsflag = 0; } /* * a shell is also restricted if the simple name of argv(0) is * rsh or -rsh in its simple name */ #ifndef RES if (c > 0 && (eq("rsh", simple(*v)) || eq("-rsh", simple(*v)))) rflag = 0; #endif if (eq("jsh", simple(*v)) || eq("-jsh", simple(*v))) flags |= monitorflg; hcreate(); set_dotpath(); /* * look for options * dolc is $# */ dolc = options(c, v); if (dolc < 2) { flags |= stdflg; { while (*flagc) flagc++; *flagc++ = STDFLG; *flagc = 0; } } if ((flags & stdflg) == 0) dolc--; if ((flags & privflg) == 0) { uid_t euid; gid_t egid; uid_t ruid; gid_t rgid; /* * Determine all of the user's id #'s for this process and * then decide if this shell is being entered as a result * of a fork/exec. * If the effective uid/gid do NOT match and the euid/egid * is < 100 and the egid is NOT 1, reset the uid and gid to * the user originally calling this process. */ euid = geteuid(); ruid = getuid(); egid = getegid(); rgid = getgid(); if ((euid != ruid) && (euid < 100)) setuid(ruid); /* reset the uid to the orig user */ if ((egid != rgid) && ((egid < 100) && (egid != 1))) setgid(rgid); /* reset the gid to the orig user */ } dolv = (unsigned char **)v + c - dolc; dolc--; /* * return here for shell file execution * but not for parenthesis subshells */ if (setjmp(subshell)) { freejobs(); flags |= subsh; } /* * number of positional parameters */ replace(&cmdadr, dolv[0]); /* cmdadr is $0 */ /* * set pidname '$$' */ assnum(&pidadr, (long)mypid); /* * set up temp file names */ settmp(); /* * default internal field separators * Do not allow importing of IFS from parent shell. * setup_env() may have set anything from parent shell to IFS. * Always set the default ifs to IFS. */ assign(&ifsnod, (unsigned char *)sptbnl); dfault(&mchknod, MAILCHECK); mailchk = stoi(mchknod.namval); /* initialize OPTIND for getopt */ n = lookup("OPTIND"); assign(n, (unsigned char *)"1"); /* * make sure that option parsing starts * at first character */ _sp = 1; if ((beenhere++) == FALSE) /* ? profile */ { if ((login_shell == TRUE) && (flags & privflg) == 0) { /* system profile */ #ifndef RES if ((input = pathopen(nullstr, sysprofile)) >= 0) exfile(rflag); /* file exists */ #endif /* user profile */ if ((input = pathopen(homenod.namval, profile)) >= 0) { exfile(rflag); flags &= ~ttyflg; } } if (rsflag == 0 || rflag == 0) { if ((flags & rshflg) == 0) { while (*flagc) flagc++; *flagc++ = 'r'; *flagc = '\0'; } flags |= rshflg; } /* * open input file if specified */ if (comdiv) { estabf(comdiv); input = -1; } else { if (flags & stdflg) { input = 0; } else { /* * If the command file specified by 'cmdadr' * doesn't exist, chkopen() will fail calling * exitsh(). If this is a login shell and * the $HOME/.profile file does not exist, the * above statement "flags &= ~ttyflg" does not * get executed and this makes exitsh() call * longjmp() instead of exiting. longjmp() will * return to the location specified by the last * active jmpbuffer, which is the one set up in * the function exfile() called after the system * profile file is executed (see lines above). * This would cause an infinite loop, because * chkopen() will continue to fail and exitsh() * to call longjmp(). To make exitsh() exit instead * of calling longjmp(), we then set the flag forcexit * at this stage. */ flags |= forcexit; input = chkopen(cmdadr, 0); flags &= ~forcexit; } #ifdef ACCT if (input != 0) preacct(cmdadr); #endif comdiv--; } } #ifdef pdp11 else *execargs = (char *)dolv; /* for `ps' cmd */ #endif exfile(0); done(0); }
unsigned int readwc(void) { wchar_t c; int len; struct fileblk *f; int i, mlen = 0; if (peekn) { c = peekn & 0x7fffffff; peekn = 0; return (c); } if (peekc) { c = peekc & 0x7fffffff; peekc = 0; return (c); } f = standin; retry: if (f->fend > f->fnxt) { /* * something in buffer */ if (*f->fnxt == 0) { f->fnxt++; f->nxtoff++; if (f->feval == 0) goto retry; /* = c = readc(); */ if (estabf(*f->feval++)) c = EOF; else c = SPACE; if (flags & readpr && standin->fstak == 0) prc(c); if (c == NL) f->flin++; return (c); } if (isascii(c = (unsigned char)*f->fnxt)) { f->fnxt++; f->nxtoff++; if (flags & readpr && standin->fstak == 0) prc(c); if (c == NL) f->flin++; return (c); } for (i = 1; i <= mb_cur_max; i++) { int rest; if ((rest = f->fend - f->fnxt) < i) { /* * not enough bytes available * f->fsiz could be BUFFERSIZE or 1 * since mb_cur_max is enough smaller than * BUFFERSIZE, this loop won't overrun * the f->fbuf buffer. */ len = readb(f, (f->fsiz == 1) ? 1 : (f->fsiz - rest), rest); if (len == 0) break; } mlen = mb_cur_max > 1 ? mbtowc(&c, (char *)f->fnxt, i) : (c = *f->fnxt & 0377, c != 0); if (mlen > 0) break; if (mb_cur_max > 1) mbtowc(NULL, NULL, 0); } if (i > mb_cur_max) { /* * enough bytes available but cannot be converted to * a valid wchar. */ c = (unsigned char)*f->fnxt; mlen = 1; } f->fnxt += mlen; f->nxtoff += mlen; if (flags & readpr && standin->fstak == 0) prwc(c); if (c == NL) f->flin++; return (c); } if (f->feof || f->fdes < 0){ c = EOF; f->feof++; return (c); } if (readb(f, f->fsiz, 0) <= 0){ if (f->fdes != input || !isatty(input)) { close(f->fdes); f->fdes = -1; } f->feof++; c = EOF; return (c); } goto retry; }
int main(int c, const char *v[]) { register int rflag = ttyflg; /* initialise storage allocation */ blokinit(); stdsigs(); setbrk(BRKINCR); addblok((POS) 0); /* set names from userenv */ sh_getenv(); /* look for restricted */ /* if(c>0 && any('r', *v) ) { rflag=0 ;} */ /* look for options */ dolc = options(c, v); if (dolc < 2) flags |= stdflg; if ((flags & stdflg) == 0) dolc--; dolv = v + c - dolc; dolc--; /* return here for shell file execution */ setjmp(subshell); /* number of positional parameters */ assnum(&dolladr, dolc); cmdadr = (char *)dolv[0]; /* set pidname */ assnum(&pidadr, getpid()); /* set up temp file names */ settmp(); /* default ifs */ dfault(&ifsnod, sptbnl); if ((beenhere++) == FALSE) { /* ? profile */ if (*cmdadr == '-' && (input = pathopen(nullstr, profile)) >= 0) { exfile(rflag); flags &= ~ttyflg; ; } if (rflag == 0) { flags |= rshflg; } /* open input file if specified */ if (comdiv) { estabf(comdiv); input = -1; } else { input = ((flags & stdflg) ? 0 : chkopen(cmdadr)); comdiv--; ; } // } else { *execargs=(char *)dolv; /* for `ps' cmd */ ; } exfile(0); done(); }