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