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