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