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

Defined variables

B defined in line 136; used 8 times
HIST defined in line 344; used 13 times
HISTSUB defined in line 345; used 8 times
Histlist defined in line 338; used 6 times
Vav defined in line 412; used 5 times
Vdp defined in line 413; used 5 times
Vsav defined in line 411; used 5 times
alhistp defined in line 287; used 9 times
alhistt defined in line 288; used 4 times
alvec defined in line 289; used 7 times
bfunc defined in line 73; used 2 times
bname defined in line 407; used 9 times
cantell defined in line 155; used 7 times
chktim defined in line 61; used 5 times
doldol defined in line 59; used 3 times
doneinp defined in line 32; used 4 times
file defined in line 49; used 4 times
gargc defined in line 310; used 8 times
gnleft defined in line 311; used 3 times
intact defined in line 37; used 3 times
justpr defined in line 38; used 3 times
labuf defined in line 188; used 4 times
lap defined in line 189; used 12 times
lastev defined in line 342; used 6 times
lineloc defined in line 151; used 3 times
mesg defined in line 67; used 4 times
onelflg defined in line 48; used 13 times
pargc defined in line 318; used 6 times
pargcp defined in line 320; used 4 times
pargs defined in line 317; used 4 times
pnleft defined in line 319; used 3 times
reslab defined in line 101; used 6 times
shtemp defined in line 53; used 7 times
srchn defined in line 78; used 1 times
uid defined in line 60; used 7 times
whyles defined in line 270; used 34 times

Defined struct's

Bin defined in line 130; used 2 times
Hist defined in line 333; used 28 times
biltins defined in line 69; used 10 times
command defined in line 198; used 92 times
srch defined in line 75; used 2 times
varent defined in line 277; used 74 times
whyle defined in line 264; used 20 times
wordent defined in line 162; used 140 times

Defined typedef's

Defined macros

DOALL defined in line 179; used 4 times
DODOL defined in line 177; used 7 times
DOEXCL defined in line 178; used 13 times
FANY defined in line 232; used 2 times
FCAT defined in line 225; used 3 times
FDIAG defined in line 231; used 4 times
FHERE defined in line 233; used 5 times
FINT defined in line 229; used 13 times
FPIN defined in line 226; used 4 times
FPOU defined in line 227; used 9 times
FPRS defined in line 230; used 5 times
FREDO defined in line 234; used 8 times
GAVSIZ defined in line 303; used 5 times
TAND defined in line 222; used 2 times
TCOM defined in line 217; used 3 times
TFIL defined in line 219; used 1 times
TLST defined in line 220; used 3 times
TOR defined in line 221; used 1 times
TPAR defined in line 218; used 4 times
ZBREAK defined in line 239; used 5 times
ZBRKSW defined in line 240; used 3 times
ZCASE defined in line 241; never used
ZDEFAULT defined in line 242; never used
ZELSE defined in line 243; used 3 times
ZEND defined in line 244; never used
ZENDIF defined in line 245; never used
ZENDSW defined in line 246; never used
ZEXIT defined in line 247; never used
ZFOREACH defined in line 248; never used
ZGOTO defined in line 249; used 6 times
ZIF defined in line 250; used 4 times
ZLABEL defined in line 251; never used
ZLET defined in line 252; never used
ZSET defined in line 253; never used
ZSWITCH defined in line 254; used 7 times
ZTEST defined in line 255; never used
ZTHEN defined in line 256; never used
ZWHILE defined in line 257; never used
fblocks defined in line 141; used 8 times
fbobp defined in line 139; used 5 times
fbuf defined in line 142; used 17 times
getexit defined in line 106; used 3 times
isdir defined in line 17; used 3 times
resexit defined in line 107; used 5 times
setexit defined in line 103; used 4 times
t_dcar defined in line 210; used 11 times
t_dcdr defined in line 212; used 11 times
t_dlef defined in line 209; used 5 times
t_drit defined in line 211; used 4 times

Usage of this include

Last modified: 1982-09-22
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1924
Valid CSS Valid XHTML 1.0 Strict