|
|
1.1 ! root 1: /* sh.h 4.1 10/9/80 */ ! 2: ! 3: #include "sh.local.h" ! 4: #ifdef VMUNIX ! 5: #include <sys/vtimes.h> ! 6: #endif ! 7: ! 8: /* ! 9: * C shell ! 10: * ! 11: * Bill Joy, UC Berkeley ! 12: * October, 1978; May 1980 ! 13: * ! 14: * Jim Kulp, IIASA, Laxenburg Austria ! 15: * April, 1980 ! 16: */ ! 17: #include <sys/types.h> ! 18: #include <sys/stat.h> ! 19: ! 20: #define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR) ! 21: ! 22: #include <errno.h> ! 23: #include <setjmp.h> ! 24: #include <signal.h> ! 25: #include <sys/times.h> ! 26: ! 27: typedef char bool; ! 28: ! 29: #define eq(a, b) (strcmp(a, b) == 0) ! 30: ! 31: /* ! 32: * Global flags ! 33: */ ! 34: bool chkstop; /* Warned of stopped jobs... allow exit */ ! 35: bool didcch; /* Have closed unused fd's for child */ ! 36: bool didfds; /* Have setup i/o fd's for child */ ! 37: bool doneinp; /* EOF indicator after reset from readc */ ! 38: bool exiterr; /* Exit if error or non-zero exit status */ ! 39: bool child; /* Child shell ... errors cause exit */ ! 40: bool haderr; /* Reset was because of an error */ ! 41: bool intty; /* Input is a tty */ ! 42: bool intact; /* We are interactive... therefore prompt */ ! 43: bool justpr; /* Just print because of :p hist mod */ ! 44: bool loginsh; /* We are a loginsh -> .login/.logout */ ! 45: bool neednote; /* Need to pnotify() */ ! 46: bool noexec; /* Don't execute, just syntax check */ ! 47: bool pjobs; /* want to print jobs if interrupted */ ! 48: bool setintr; /* Set interrupts on/off -> Wait intr... */ ! 49: bool timflg; /* Time the next waited for command */ ! 50: ! 51: /* ! 52: * Global i/o info ! 53: */ ! 54: char *arginp; /* Argument input for sh -c and internal `xx` */ ! 55: int onelflg; /* 2 -> need line for -t, 1 -> exit on read */ ! 56: char *file; /* Name of shell file for $0 */ ! 57: ! 58: char *err; /* Error message from scanner/parser */ ! 59: int errno; /* Error from C library routines */ ! 60: char *shtemp; /* Temp name for << shell files in /tmp */ ! 61: time_t time0; /* Time at which the shell started */ ! 62: ! 63: /* ! 64: * Miscellany ! 65: */ ! 66: char *doldol; /* Character pid for $$ */ ! 67: int uid; /* Invokers uid */ ! 68: time_t chktim; /* Time mail last checked */ ! 69: short shpgrp; /* Pgrp of shell */ ! 70: short tpgrp; /* Terminal process group */ ! 71: /* If tpgrp is -1, leave tty alone! */ ! 72: short opgrp; /* Initial pgrp and tty pgrp */ ! 73: int oldisc; /* Initial line discipline or -1 */ ! 74: struct tms shtimes; /* shell and child times for process timing */ ! 75: ! 76: /* ! 77: * These are declared here because they want to be ! 78: * initialized in sh.init.c (to allow them to be made readonly) ! 79: */ ! 80: ! 81: struct biltins { ! 82: char *bname; ! 83: int (*bfunct)(); ! 84: short minargs, maxargs; ! 85: } bfunc[]; ! 86: ! 87: #define INF 1000 ! 88: ! 89: struct srch { ! 90: char *s_name; ! 91: short s_value; ! 92: } srchn[]; ! 93: ! 94: /* ! 95: * To be able to redirect i/o for builtins easily, the shell moves the i/o ! 96: * descriptors it uses away from 0,1,2. ! 97: * Ideally these should be in units which are closed across exec's ! 98: * (this saves work) but for version 6, this is not usually possible. ! 99: * The desired initial values for these descriptors are defined in ! 100: * sh.local.h. ! 101: */ ! 102: short SHIN; /* Current shell input (script) */ ! 103: short SHOUT; /* Shell output */ ! 104: short SHDIAG; /* Diagnostic output... shell errs go here */ ! 105: short OLDSTD; /* Old standard input (def for cmds) */ ! 106: ! 107: /* ! 108: * Error control ! 109: * ! 110: * Errors in scanning and parsing set up an error message to be printed ! 111: * at the end and complete. Other errors always cause a reset. ! 112: * Because of source commands and .cshrc we need nested error catches. ! 113: */ ! 114: ! 115: jmp_buf reslab; ! 116: ! 117: #define setexit() setjmp(reslab) ! 118: #define reset() longjmp(reslab) ! 119: /* Should use structure assignment here */ ! 120: #define getexit(a) copy((char *)(a), (char *)reslab, sizeof reslab) ! 121: #define resexit(a) copy((char *)reslab, ((char *)(a)), sizeof reslab) ! 122: ! 123: char *gointr; /* Label for an onintr transfer */ ! 124: int (*parintr)(); /* Parents interrupt catch */ ! 125: int (*parterm)(); /* Parents terminate catch */ ! 126: ! 127: /* ! 128: * Lexical definitions. ! 129: * ! 130: * All lexical space is allocated dynamically. ! 131: * The eighth bit of characters is used to prevent recognition, ! 132: * and eventually stripped. ! 133: */ ! 134: #define QUOTE 0200 /* Eighth char bit used internally for 'ing */ ! 135: #define TRIM 0177 /* Mask to strip quote bit */ ! 136: ! 137: /* ! 138: * Each level of input has a buffered input structure. ! 139: * There are one or more blocks of buffered input for each level, ! 140: * exactly one if the input is seekable and tell is available. ! 141: * In other cases, the shell buffers enough blocks to keep all loops ! 142: * in the buffer. ! 143: */ ! 144: struct Bin { ! 145: off_t Bfseekp; /* Seek pointer */ ! 146: off_t Bfbobp; /* Seekp of beginning of buffers */ ! 147: off_t Bfeobp; /* Seekp of end of buffers */ ! 148: short Bfblocks; /* Number of buffer blocks */ ! 149: char **Bfbuf; /* The array of buffer blocks */ ! 150: } B; ! 151: ! 152: #define fseekp B.Bfseekp ! 153: #define fbobp B.Bfbobp ! 154: #define feobp B.Bfeobp ! 155: #define fblocks B.Bfblocks ! 156: #define fbuf B.Bfbuf ! 157: ! 158: off_t btell(); ! 159: ! 160: /* ! 161: * The shell finds commands in loops by reseeking the input ! 162: * For whiles, in particular, it reseeks to the beginning of the ! 163: * line the while was on; hence the while placement restrictions. ! 164: */ ! 165: off_t lineloc; ! 166: ! 167: #ifdef TELL ! 168: off_t tell(); ! 169: bool cantell; /* Is current source tellable ? */ ! 170: #endif ! 171: ! 172: /* ! 173: * Input lines are parsed into doubly linked circular ! 174: * lists of words of the following form. ! 175: */ ! 176: struct wordent { ! 177: char *word; ! 178: struct wordent *prev; ! 179: struct wordent *next; ! 180: }; ! 181: ! 182: /* ! 183: * During word building, both in the initial lexical phase and ! 184: * when expanding $ variable substitutions, expansion by `!' and `$' ! 185: * must be inhibited when reading ahead in routines which are themselves ! 186: * processing `!' and `$' expansion or after characters such as `\' or in ! 187: * quotations. The following flags are passed to the getC routines ! 188: * telling them which of these substitutions are appropriate for the ! 189: * next character to be returned. ! 190: */ ! 191: #define DODOL 1 ! 192: #define DOEXCL 2 ! 193: #define DOALL DODOL|DOEXCL ! 194: ! 195: /* ! 196: * Labuf implements a general buffer for lookahead during lexical operations. ! 197: * Text which is to be placed in the input stream can be stuck here. ! 198: * We stick parsed ahead $ constructs during initial input, ! 199: * process id's from `$$', and modified variable values (from qualifiers ! 200: * during expansion in sh.dol.c) here. ! 201: */ ! 202: #ifdef VMUNIX ! 203: char labuf[BUFSIZ]; ! 204: #else ! 205: char labuf[256]; ! 206: #endif ! 207: ! 208: char *lap; ! 209: ! 210: /* ! 211: * Parser structure ! 212: * ! 213: * Each command is parsed to a tree of command structures and ! 214: * flags are set bottom up during this process, to be propagated down ! 215: * as needed during the semantics/exeuction pass (sh.sem.c). ! 216: */ ! 217: struct command { ! 218: short t_dtyp; /* Type of node */ ! 219: short t_dflg; /* Flags, e.g. FAND|... */ ! 220: union { ! 221: char *T_dlef; /* Input redirect word */ ! 222: struct command *T_dcar; /* Left part of list/pipe */ ! 223: } L; ! 224: union { ! 225: char *T_drit; /* Output redirect word */ ! 226: struct command *T_dcdr; /* Right part of list/pipe */ ! 227: } R; ! 228: #define t_dlef L.T_dlef ! 229: #define t_dcar L.T_dcar ! 230: #define t_drit R.T_drit ! 231: #define t_dcdr R.T_dcdr ! 232: char **t_dcom; /* Command/argument vector */ ! 233: struct command *t_dspr; /* Pointer to ()'d subtree */ ! 234: short t_nice; ! 235: }; ! 236: ! 237: #define TCOM 1 /* t_dcom <t_dlef >t_drit */ ! 238: #define TPAR 2 /* ( t_dspr ) <t_dlef >t_drit */ ! 239: #define TFIL 3 /* t_dlef | t_drit */ ! 240: #define TLST 4 /* t_dlef ; t_drit */ ! 241: #define TOR 5 /* t_dlef || t_drit */ ! 242: #define TAND 6 /* t_dlef && t_drit */ ! 243: ! 244: #define FSAVE (FNICE|FTIME|FNOHUP) /* save these when re-doing */ ! 245: ! 246: #define FAND (1<<0) /* executes in background */ ! 247: #define FCAT (1<<1) /* output is redirected >> */ ! 248: #define FPIN (1<<2) /* input is a pipe */ ! 249: #define FPOU (1<<3) /* output is a pipe */ ! 250: #define FPAR (1<<4) /* don't fork, last ()ized cmd */ ! 251: #define FINT (1<<5) /* should be immune from intr's */ ! 252: /* spare */ ! 253: #define FDIAG (1<<7) /* redirect unit 2 with unit 1 */ ! 254: #define FANY (1<<8) /* output was ! */ ! 255: #define FHERE (1<<9) /* input redirection is << */ ! 256: #define FREDO (1<<10) /* reexec aft if, repeat,... */ ! 257: #define FNICE (1<<11) /* t_nice is meaningful */ ! 258: #define FNOHUP (1<<12) /* nohup this command */ ! 259: #define FTIME (1<<13) /* time this command */ ! 260: ! 261: /* ! 262: * The keywords for the parser ! 263: */ ! 264: #define ZBREAK 0 ! 265: #define ZBRKSW 1 ! 266: #define ZCASE 2 ! 267: #define ZDEFAULT 3 ! 268: #define ZELSE 4 ! 269: #define ZEND 5 ! 270: #define ZENDIF 6 ! 271: #define ZENDSW 7 ! 272: #define ZEXIT 8 ! 273: #define ZFOREACH 9 ! 274: #define ZGOTO 10 ! 275: #define ZIF 11 ! 276: #define ZLABEL 12 ! 277: #define ZLET 13 ! 278: #define ZSET 14 ! 279: #define ZSWITCH 15 ! 280: #define ZTEST 16 ! 281: #define ZTHEN 17 ! 282: #define ZWHILE 18 ! 283: ! 284: /* ! 285: * Structure defining the existing while/foreach loops at this ! 286: * source level. Loops are implemented by seeking back in the ! 287: * input. For foreach (fe), the word list is attached here. ! 288: */ ! 289: struct whyle { ! 290: off_t w_start; /* Point to restart loop */ ! 291: off_t w_end; /* End of loop (0 if unknown) */ ! 292: char **w_fe, **w_fe0; /* Current/initial wordlist for fe */ ! 293: char *w_fename; /* Name for fe */ ! 294: struct whyle *w_next; /* Next (more outer) loop */ ! 295: } *whyles; ! 296: ! 297: /* ! 298: * Variable structure ! 299: * ! 300: * Lists of aliases and variables are sorted alphabetically by name ! 301: */ ! 302: struct varent { ! 303: char **vec; /* Array of words which is the value */ ! 304: char *name; /* Name of variable/alias */ ! 305: struct varent *link; ! 306: } shvhed, aliases; ! 307: ! 308: /* ! 309: * The following are for interfacing redo substitution in ! 310: * aliases to the lexical routines. ! 311: */ ! 312: struct wordent *alhistp; /* Argument list (first) */ ! 313: struct wordent *alhistt; /* Node after last in arg list */ ! 314: char **alvec; /* The (remnants of) alias vector */ ! 315: ! 316: /* ! 317: * Filename/command name expansion variables ! 318: */ ! 319: short gflag; /* After tglob -> is globbing needed? */ ! 320: ! 321: /* ! 322: * A reasonable limit on number of arguments would seem to be ! 323: * the maximum number of characters in an arg list / 6. ! 324: */ ! 325: #define GAVSIZ NCARGS / 6 ! 326: ! 327: /* ! 328: * Variables for filename expansion ! 329: */ ! 330: char **gargv; /* Pointer to the (stack) arglist */ ! 331: short gargc; /* Number args in gargv */ ! 332: short gnleft; ! 333: ! 334: /* ! 335: * Variables for command expansion. ! 336: */ ! 337: char **pargv; /* Pointer to the argv list space */ ! 338: char *pargs; /* Pointer to start current word */ ! 339: short pargc; /* Count of arguments in pargv */ ! 340: short pnleft; /* Number of chars left in pargs */ ! 341: char *pargcp; /* Current index into pargs */ ! 342: ! 343: /* ! 344: * History list ! 345: * ! 346: * Each history list entry contains an embedded wordlist ! 347: * from the scanner, a number for the event, and a reference count ! 348: * to aid in discarding old entries. ! 349: * ! 350: * Essentially "invisible" entries are put on the history list ! 351: * when history substitution includes modifiers, and thrown away ! 352: * at the next discarding since their event numbers are very negative. ! 353: */ ! 354: struct Hist { ! 355: struct wordent Hlex; ! 356: int Hnum; ! 357: int Href; ! 358: struct Hist *Hnext; ! 359: } Histlist; ! 360: ! 361: struct wordent paraml; /* Current lexical word list */ ! 362: int eventno; /* Next events number */ ! 363: int lastev; /* Last event reference (default) */ ! 364: ! 365: char HIST; /* history invocation character */ ! 366: char HISTSUB; /* auto-substitute character */ ! 367: ! 368: char *Dfix1(); ! 369: struct varent *adrof(), *adrof1(); ! 370: char **blkcat(); ! 371: char **blkcpy(); ! 372: char **blkend(); ! 373: char **blkspl(); ! 374: char *calloc(); ! 375: char *cname(); ! 376: char **copyblk(); ! 377: char **dobackp(); ! 378: char *domod(); ! 379: struct wordent *dosub(); ! 380: char *exp3(); ! 381: char *exp3a(); ! 382: char *exp4(); ! 383: char *exp5(); ! 384: char *exp6(); ! 385: struct Hist *enthist(); ! 386: struct Hist *findev(); ! 387: struct wordent *freenod(); ! 388: char *getenv(); ! 389: char *getinx(); ! 390: struct varent *getvx(); ! 391: struct passwd *getpwnam(); ! 392: struct wordent *gethent(); ! 393: struct wordent *getsub(); ! 394: char *getwd(); ! 395: char *globone(); ! 396: struct biltins *isbfunc(); ! 397: char **glob(); ! 398: char *operate(); ! 399: int pintr(); ! 400: int pchild(); ! 401: char *putn(); ! 402: char **saveblk(); ! 403: char *savestr(); ! 404: char *strcat(); ! 405: char *strcpy(); ! 406: char *strend(); ! 407: char *strings(); ! 408: char *strip(); ! 409: char *strspl(); ! 410: char *subword(); ! 411: struct command *syntax(); ! 412: struct command *syn0(); ! 413: struct command *syn1(); ! 414: struct command *syn1a(); ! 415: struct command *syn1b(); ! 416: struct command *syn2(); ! 417: struct command *syn3(); ! 418: int tglob(); ! 419: int trim(); ! 420: char *value(), *value1(); ! 421: char *xhome(); ! 422: char *xname(); ! 423: char *xset(); ! 424: ! 425: #define NOSTR ((char *) 0) ! 426: ! 427: /* ! 428: * setname is a macro to save space (see sh.err.c) ! 429: */ ! 430: char *bname; ! 431: #define setname(a) bname = (a); ! 432: ! 433: #ifdef VFORK ! 434: char *Vsav; ! 435: char **Vav; ! 436: char *Vdp; ! 437: #endif ! 438: ! 439: #ifdef VMUNIX ! 440: struct vtimes zvms; ! 441: #endif ! 442: ! 443: char **evalvec; ! 444: char *evalp; ! 445: ! 446: struct mesg { ! 447: char *iname; /* name from /usr/include */ ! 448: char *pname; /* print name */ ! 449: } mesg[];
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.