|
|
1.1 ! root 1: /* @(#)main.c 1.7 */ ! 2: /* ! 3: * UNIX shell ! 4: * ! 5: * Bell Telephone Laboratories ! 6: * ! 7: */ ! 8: ! 9: #include "defs.h" ! 10: #include "sym.h" ! 11: #include "timeout.h" ! 12: #include <sys/types.h> ! 13: #include <sys/stat.h> ! 14: #include <sgtty.h> ! 15: ! 16: static BOOL beenhere = FALSE; ! 17: char tmpout[20] = "/tmp/sh-"; ! 18: struct fileblk stdfile; ! 19: struct fileblk *standin = &stdfile; ! 20: ! 21: static int mailchk = 180; ! 22: static char *mailp; ! 23: static long mod_time = 0; ! 24: ! 25: #ifdef pdp11 ! 26: #include <execargs.h> ! 27: #endif ! 28: ! 29: extern int exfile(); ! 30: extern char *simple(); ! 31: ! 32: ! 33: ! 34: main(c, v, e) ! 35: int c; ! 36: char *v[]; ! 37: char *e[]; ! 38: { ! 39: struct namnod *n; ! 40: ! 41: stdsigs(); ! 42: ! 43: /* ! 44: * initialise storage allocation ! 45: */ ! 46: ! 47: stakbot = 0; ! 48: addblok((unsigned)0); ! 49: ! 50: /* ! 51: * set names from userenv ! 52: */ ! 53: ! 54: setup_env(); ! 55: ! 56: /* ! 57: * look for options ! 58: * dolc is $# ! 59: */ ! 60: dolc = options(c, v); ! 61: ! 62: if (dolc < 2) ! 63: { ! 64: flags |= stdflg; ! 65: { ! 66: register char *flagc = flagadr; ! 67: ! 68: while (*flagc) ! 69: flagc++; ! 70: *flagc++ = STDFLG; ! 71: *flagc = 0; ! 72: } ! 73: } ! 74: if ((flags & stdflg) == 0) ! 75: dolc--; ! 76: dolv = v + c - dolc; ! 77: dolc--; ! 78: ! 79: /* ! 80: * return here for shell file execution ! 81: * but not for parenthesis subshells ! 82: */ ! 83: setjmp(subshell); ! 84: ! 85: /* ! 86: * number of positional parameters ! 87: */ ! 88: replace(&cmdadr, dolv[0]); /* cmdadr is $0 */ ! 89: ! 90: /* ! 91: * set pidname '$$' ! 92: */ ! 93: assnum(&pidadr, getpid()); ! 94: ! 95: /* ! 96: * set up temp file names ! 97: */ ! 98: settmp(); ! 99: ! 100: /* ! 101: * default internal field separators - $IFS ! 102: */ ! 103: dfault(&ifsnod, sptbnl); ! 104: ! 105: if ((beenhere++) == FALSE) /* ? profile */ ! 106: { ! 107: if (*(simple(cmdadr)) == '-') ! 108: { ! 109: if ((input = pathopen(nullstr, profile)) >= 0) ! 110: { ! 111: exfile(ttyflg); ! 112: flags &= ~ttyflg; ! 113: } ! 114: } ! 115: /* ! 116: * open input file if specified ! 117: */ ! 118: if (comdiv) ! 119: { ! 120: estabf(comdiv); ! 121: input = -1; ! 122: } ! 123: else ! 124: { ! 125: input = ((flags & stdflg) ? 0 : chkopen(cmdadr)); ! 126: ! 127: #ifdef ACCT ! 128: if (input != 0) ! 129: preacct(cmdadr); ! 130: #endif ! 131: comdiv--; ! 132: } ! 133: } ! 134: #ifdef pdp11 ! 135: else ! 136: *execargs = (char *)dolv; /* for `ps' cmd */ ! 137: #endif ! 138: ! 139: exfile(0); ! 140: done(); ! 141: } ! 142: ! 143: static int ! 144: exfile(prof) ! 145: BOOL prof; ! 146: { ! 147: long mailtime = 0; /* Must not be a register variable */ ! 148: long curtime = 0; ! 149: register int userid; ! 150: ! 151: /* ! 152: * move input ! 153: */ ! 154: if (input > 0) ! 155: { ! 156: Ldup(input, INIO); ! 157: input = INIO; ! 158: } ! 159: ! 160: userid = geteuid(); ! 161: ! 162: /* ! 163: * decide whether interactive ! 164: */ ! 165: if ((flags & intflg) || ! 166: ((flags&oneflg) == 0 && ! 167: isatty(output) && ! 168: isatty(input)) ) ! 169: ! 170: { ! 171: dfault(&ps1nod, (userid ? stdprompt : supprompt)); ! 172: dfault(&ps2nod, readmsg); ! 173: flags |= ttyflg | prompt; ! 174: ignsig(SIGTERM); ! 175: setmail(mailnod.namval.val); ! 176: } ! 177: else ! 178: { ! 179: flags |= prof; ! 180: flags &= ~prompt; ! 181: } ! 182: ! 183: if (setjmp(errshell) && prof) ! 184: { ! 185: close(input); ! 186: return; ! 187: } ! 188: /* ! 189: * error return here ! 190: */ ! 191: ! 192: loopcnt = peekc = peekn = 0; ! 193: fndef = 0; ! 194: iopend = 0; ! 195: ! 196: if (input >= 0) ! 197: initf(input); ! 198: /* ! 199: * command loop ! 200: */ ! 201: for (;;) ! 202: { ! 203: tdystak(0); ! 204: stakchk(); /* may reduce sbrk */ ! 205: exitset(); ! 206: ! 207: if ((flags & prompt) && standin->fstak == 0 && !eof) ! 208: { ! 209: ! 210: if (mailp) ! 211: { ! 212: time(&curtime); ! 213: ! 214: if ((curtime - mailtime) >= mailchk) ! 215: { ! 216: chkmail(); ! 217: mailtime = curtime; ! 218: } ! 219: } ! 220: ! 221: prs(ps1nod.namval.val); ! 222: ! 223: #ifdef TIME_OUT ! 224: alarm(TIMEOUT); ! 225: #endif ! 226: ! 227: flags |= waiting; ! 228: } ! 229: ! 230: trapnote = 0; ! 231: peekc = readc(); ! 232: if (eof) ! 233: return; ! 234: ! 235: #ifdef TIME_OUT ! 236: alarm(0); ! 237: #endif ! 238: ! 239: flags &= ~waiting; ! 240: ! 241: execute(cmd(NL, MTFLG), 0, eflag); ! 242: eof |= (flags & oneflg); ! 243: } ! 244: } ! 245: ! 246: chkpr() ! 247: { ! 248: if ((flags & prompt) && standin->fstak == 0) ! 249: prs(ps2nod.namval.val); ! 250: } ! 251: ! 252: settmp() ! 253: { ! 254: itos(getpid()); ! 255: serial = 0; ! 256: tmpnam = movstr(numbuf, &tmpout[TMPNAM]); ! 257: } ! 258: ! 259: Ldup(fa, fb) ! 260: register int fa, fb; ! 261: { ! 262: ! 263: #ifndef SYSV ! 264: dup2(fa, fb); ! 265: close(fa); ! 266: ioctl(fb, FIOCLEX, 0); ! 267: #else ! 268: if (fa >= 0) ! 269: { close(fb); ! 270: fcntl(fa,0,fb); /* normal dup */ ! 271: close(fa); ! 272: fcntl(fb, 2, 1); /* autoclose for fb */ ! 273: } ! 274: #endif ! 275: } ! 276: ! 277: ! 278: chkmail() ! 279: { ! 280: struct stat statb; ! 281: ! 282: if (mailp && stat(mailp, &statb) >= 0) ! 283: { ! 284: if(statb.st_size && mod_time ! 285: && statb.st_mtime != mod_time) ! 286: { ! 287: prs(mailmsg); ! 288: } ! 289: mod_time = statb.st_mtime; ! 290: } ! 291: else if (mod_time == 0) ! 292: mod_time = 1; ! 293: } ! 294: ! 295: ! 296: setmail(mailpath) ! 297: char *mailpath; ! 298: { ! 299: ! 300: if (mailp = mailpath) ! 301: mod_time=0; ! 302: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.