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