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