Annotation of 40BSD/cmd/csh/sh.h, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.