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