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.18 (Berkeley) 08/04/91 8 */ 9 10 /* 11 * Fundamental definitions which may vary from system to system. 12 * 13 * BUFSIZ The i/o buffering size; also limits word size 14 * MAILINTVL How often to mailcheck; more often is more expensive 15 */ 16 #ifndef BUFSIZ 17 #define BUFSIZ 1024 /* default buffer size */ 18 #endif /* BUFSIZ */ 19 20 #define FORKSLEEP 10 /* delay loop on non-interactive fork failure */ 21 #define MAILINTVL 600 /* 10 minutes */ 22 23 /* 24 * The shell moves std in/out/diag and the old std input away from units 25 * 0, 1, and 2 so that it is easy to set up these standards for invoked 26 * commands. 27 */ 28 #define FSHTTY 15 /* /dev/tty when manip pgrps */ 29 #define FSHIN 16 /* Preferred desc for shell input */ 30 #define FSHOUT 17 /* ... shell output */ 31 #define FSHERR 18 /* ... shell diagnostics */ 32 #define FOLDSTD 19 /* ... old std input */ 33 34 #ifdef PROF 35 #define xexit(n) done(n) 36 #endif 37 38 #ifdef SHORT_STRINGS 39 typedef short Char; 40 41 #define SAVE(a) (Strsave(str2short(a))) 42 #else 43 typedef char Char; 44 45 #define SAVE(a) (strsave(a)) 46 #endif 47 48 typedef void *ioctl_t; /* Third arg of ioctl */ 49 50 typedef void *ptr_t; 51 52 #include "const.h" 53 #include "char.h" 54 #include "err.h" 55 56 #ifdef SYSMALLOC 57 #define xmalloc(i) Malloc(i) 58 #define xrealloc(p, i) Realloc(p, i) 59 #define xcalloc(n, s) Calloc(n, s) 60 #define xfree(p) Free(p) 61 #else 62 #define xmalloc(i) malloc(i) 63 #define xrealloc(p, i) realloc(p, i) 64 #define xcalloc(n, s) calloc(n, s) 65 #define xfree(p) free(p) 66 #endif /* SYSMALLOC */ 67 68 #include <stdio.h> 69 FILE *cshin, *cshout, *csherr; 70 71 #define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR) 72 73 #define SIGN_EXTEND_CHAR(a) \ 74 ((a) & 0x80 ? ((int) (a)) | 0xffffff00 : ((int) a) & 0x000000ff) 75 76 77 typedef int bool; 78 79 #define eq(a, b) (Strcmp(a, b) == 0) 80 81 /* globone() flags */ 82 #define G_ERROR 0 /* default action: error if multiple words */ 83 #define G_IGNORE 1 /* ignore the rest of the words */ 84 #define G_APPEND 2 /* make a sentence by cat'ing the words */ 85 86 /* 87 * Global flags 88 */ 89 bool chkstop; /* Warned of stopped jobs... allow exit */ 90 bool didfds; /* Have setup i/o fd's for child */ 91 bool doneinp; /* EOF indicator after reset from readc */ 92 bool exiterr; /* Exit if error or non-zero exit status */ 93 bool child; /* Child shell ... errors cause exit */ 94 bool haderr; /* Reset was because of an error */ 95 bool intty; /* Input is a tty */ 96 bool intact; /* We are interactive... therefore prompt */ 97 bool justpr; /* Just print because of :p hist mod */ 98 bool loginsh; /* We are a loginsh -> .login/.logout */ 99 bool neednote; /* Need to pnotify() */ 100 bool noexec; /* Don't execute, just syntax check */ 101 bool pjobs; /* want to print jobs if interrupted */ 102 bool setintr; /* Set interrupts on/off -> Wait intr... */ 103 bool timflg; /* Time the next waited for command */ 104 bool havhash; /* path hashing is available */ 105 106 #ifdef FILEC 107 bool filec; /* doing filename expansion */ 108 #endif 109 110 /* 111 * Global i/o info 112 */ 113 Char *arginp; /* Argument input for sh -c and internal `xx` */ 114 int onelflg; /* 2 -> need line for -t, 1 -> exit on read */ 115 Char *ffile; /* Name of shell file for $0 */ 116 117 char *seterr; /* Error message from scanner/parser */ 118 Char *shtemp; /* Temp name for << shell files in /tmp */ 119 120 #include <sys/time.h> 121 #include <sys/resource.h> 122 123 struct timeval time0; /* Time at which the shell started */ 124 struct rusage ru0; 125 126 /* 127 * Miscellany 128 */ 129 Char *doldol; /* Character pid for $$ */ 130 int uid; /* Invokers uid */ 131 int gid; /* Invokers gid */ 132 time_t chktim; /* Time mail last checked */ 133 int shpgrp; /* Pgrp of shell */ 134 int tpgrp; /* Terminal process group */ 135 136 /* If tpgrp is -1, leave tty alone! */ 137 int opgrp; /* Initial pgrp and tty pgrp */ 138 139 140 /* 141 * To be able to redirect i/o for builtins easily, the shell moves the i/o 142 * descriptors it uses away from 0,1,2. 143 * Ideally these should be in units which are closed across exec's 144 * (this saves work) but for version 6, this is not usually possible. 145 * The desired initial values for these descriptors are defined in 146 * local.h. 147 */ 148 int SHIN; /* Current shell input (script) */ 149 int SHOUT; /* Shell output */ 150 int SHERR; /* Diagnostic output... shell errs go here */ 151 int OLDSTD; /* Old standard input (def for cmds) */ 152 153 /* 154 * Error control 155 * 156 * Errors in scanning and parsing set up an error message to be printed 157 * at the end and complete. Other errors always cause a reset. 158 * Because of source commands and .cshrc we need nested error catches. 159 */ 160 161 #include <setjmp.h> 162 jmp_buf reslab; 163 164 #define setexit() (setjmp(reslab)) 165 #define reset() longjmp(reslab, 1) 166 /* Should use structure assignment here */ 167 #define getexit(a) bcopy((char *)reslab, ((char *)(a)), sizeof reslab) 168 #define resexit(a) bcopy((char *)(a), (char *)reslab, sizeof reslab) 169 170 Char *gointr; /* Label for an onintr transfer */ 171 172 #include <signal.h> 173 sig_t parintr; /* Parents interrupt catch */ 174 sig_t parterm; /* Parents terminate catch */ 175 176 /* 177 * Lexical definitions. 178 * 179 * All lexical space is allocated dynamically. 180 * The eighth/sixteenth bit of characters is used to prevent recognition, 181 * and eventually stripped. 182 */ 183 #define META 0200 184 #define ASCII 0177 185 #ifdef SHORT_STRINGS 186 #define CHAR 0377 187 #define QUOTE 0100000 /* 16nth char bit used for 'ing */ 188 #define TRIM 0077777 /* Mask to strip quote bit */ 189 #else 190 #define CHAR 0177 191 #define QUOTE 0200 /* Eighth char bit used for 'ing */ 192 #define TRIM 0177 /* Mask to strip quote bit */ 193 #endif 194 195 int AsciiOnly; /* If set only 7 bits is expected in characters */ 196 197 /* 198 * Each level of input has a buffered input structure. 199 * There are one or more blocks of buffered input for each level, 200 * exactly one if the input is seekable and tell is available. 201 * In other cases, the shell buffers enough blocks to keep all loops 202 * in the buffer. 203 */ 204 struct Bin { 205 off_t Bfseekp; /* Seek pointer */ 206 off_t Bfbobp; /* Seekp of beginning of buffers */ 207 off_t Bfeobp; /* Seekp of end of buffers */ 208 int Bfblocks; /* Number of buffer blocks */ 209 Char **Bfbuf; /* The array of buffer blocks */ 210 } B; 211 212 #define fseekp B.Bfseekp 213 #define fbobp B.Bfbobp 214 #define feobp B.Bfeobp 215 #define fblocks B.Bfblocks 216 #define fbuf B.Bfbuf 217 218 /* 219 * The shell finds commands in loops by reseeking the input 220 * For whiles, in particular, it reseeks to the beginning of the 221 * line the while was on; hence the while placement restrictions. 222 */ 223 off_t lineloc; 224 225 bool cantell; /* Is current source tellable ? */ 226 227 /* 228 * Input lines are parsed into doubly linked circular 229 * lists of words of the following form. 230 */ 231 struct wordent { 232 Char *word; 233 struct wordent *prev; 234 struct wordent *next; 235 }; 236 237 /* 238 * During word building, both in the initial lexical phase and 239 * when expanding $ variable substitutions, expansion by `!' and `$' 240 * must be inhibited when reading ahead in routines which are themselves 241 * processing `!' and `$' expansion or after characters such as `\' or in 242 * quotations. The following flags are passed to the getC routines 243 * telling them which of these substitutions are appropriate for the 244 * next character to be returned. 245 */ 246 #define DODOL 1 247 #define DOEXCL 2 248 #define DOALL DODOL|DOEXCL 249 250 /* 251 * Labuf implements a general buffer for lookahead during lexical operations. 252 * Text which is to be placed in the input stream can be stuck here. 253 * We stick parsed ahead $ constructs during initial input, 254 * process id's from `$$', and modified variable values (from qualifiers 255 * during expansion in sh.dol.c) here. 256 */ 257 Char *lap; 258 259 /* 260 * Parser structure 261 * 262 * Each command is parsed to a tree of command structures and 263 * flags are set bottom up during this process, to be propagated down 264 * as needed during the semantics/exeuction pass (sh.sem.c). 265 */ 266 struct command { 267 short t_dtyp; /* Type of node */ 268 #define NODE_COMMAND 1 /* t_dcom <t_dlef >t_drit */ 269 #define NODE_PAREN 2 /* ( t_dspr ) <t_dlef >t_drit */ 270 #define NODE_PIPE 3 /* t_dlef | t_drit */ 271 #define NODE_LIST 4 /* t_dlef ; t_drit */ 272 #define NODE_OR 5 /* t_dlef || t_drit */ 273 #define NODE_AND 6 /* t_dlef && t_drit */ 274 short t_dflg; /* Flags, e.g. F_AMPERSAND|... */ 275 #define F_SAVE (F_NICE|F_TIME|F_NOHUP) /* save these when re-doing */ 276 277 #define F_AMPERSAND (1<<0) /* executes in background */ 278 #define F_APPEND (1<<1) /* output is redirected >> */ 279 #define F_PIPEIN (1<<2) /* input is a pipe */ 280 #define F_PIPEOUT (1<<3) /* output is a pipe */ 281 #define F_NOFORK (1<<4) /* don't fork, last ()ized cmd */ 282 #define F_NOINTERRUPT (1<<5) /* should be immune from intr's */ 283 /* spare */ 284 #define F_STDERR (1<<7) /* redirect unit 2 with unit 1 */ 285 #define F_OVERWRITE (1<<8) /* output was ! */ 286 #define F_READ (1<<9) /* input redirection is << */ 287 #define F_REPEAT (1<<10) /* reexec aft if, repeat,... */ 288 #define F_NICE (1<<11) /* t_nice is meaningful */ 289 #define F_NOHUP (1<<12) /* nohup this command */ 290 #define F_TIME (1<<13) /* time this command */ 291 union { 292 Char *T_dlef; /* Input redirect word */ 293 struct command *T_dcar; /* Left part of list/pipe */ 294 } L; 295 union { 296 Char *T_drit; /* Output redirect word */ 297 struct command *T_dcdr; /* Right part of list/pipe */ 298 } R; 299 #define t_dlef L.T_dlef 300 #define t_dcar L.T_dcar 301 #define t_drit R.T_drit 302 #define t_dcdr R.T_dcdr 303 Char **t_dcom; /* Command/argument vector */ 304 struct command *t_dspr; /* Pointer to ()'d subtree */ 305 int t_nice; 306 }; 307 308 309 /* 310 * These are declared here because they want to be 311 * initialized in sh.init.c (to allow them to be made readonly) 312 */ 313 314 extern struct biltins { 315 char *bname; 316 void (*bfunct) __P((Char **, struct command *)); 317 short minargs, maxargs; 318 } bfunc[]; 319 extern int nbfunc; 320 321 extern struct srch { 322 char *s_name; 323 short s_value; 324 } srchn[]; 325 extern int nsrchn; 326 327 /* 328 * The keywords for the parser 329 */ 330 #define T_BREAK 0 331 #define T_BRKSW 1 332 #define T_CASE 2 333 #define T_DEFAULT 3 334 #define T_ELSE 4 335 #define T_END 5 336 #define T_ENDIF 6 337 #define T_ENDSW 7 338 #define T_EXIT 8 339 #define T_FOREACH 9 340 #define T_GOTO 10 341 #define T_IF 11 342 #define T_LABEL 12 343 #define T_LET 13 344 #define T_SET 14 345 #define T_SWITCH 15 346 #define T_TEST 16 347 #define T_THEN 17 348 #define T_WHILE 18 349 350 /* 351 * Structure defining the existing while/foreach loops at this 352 * source level. Loops are implemented by seeking back in the 353 * input. For foreach (fe), the word list is attached here. 354 */ 355 struct whyle { 356 off_t w_start; /* Point to restart loop */ 357 off_t w_end; /* End of loop (0 if unknown) */ 358 Char **w_fe, **w_fe0; /* Current/initial wordlist for fe */ 359 Char *w_fename; /* Name for fe */ 360 struct whyle *w_next; /* Next (more outer) loop */ 361 } *whyles; 362 363 /* 364 * Variable structure 365 * 366 * Aliases and variables are stored in AVL balanced binary trees. 367 */ 368 struct varent { 369 Char **vec; /* Array of words which is the value */ 370 Char *v_name; /* Name of variable/alias */ 371 struct varent *v_link[3]; /* The links, see below */ 372 int v_bal; /* Balance factor */ 373 } shvhed, aliases; 374 375 #define v_left v_link[0] 376 #define v_right v_link[1] 377 #define v_parent v_link[2] 378 379 struct varent *adrof1(); 380 381 #define adrof(v) adrof1(v, &shvhed) 382 #define value(v) value1(v, &shvhed) 383 384 /* 385 * The following are for interfacing redo substitution in 386 * aliases to the lexical routines. 387 */ 388 struct wordent *alhistp; /* Argument list (first) */ 389 struct wordent *alhistt; /* Node after last in arg list */ 390 Char **alvec; /* The (remnants of) alias vector */ 391 392 /* 393 * Filename/command name expansion variables 394 */ 395 int gflag; /* After tglob -> is globbing needed? */ 396 397 #define MAXVARLEN 30 /* Maximum number of char in a variable name */ 398 399 /* 400 * Variables for filename expansion 401 */ 402 extern Char **gargv; /* Pointer to the (stack) arglist */ 403 extern long gargc; /* Number args in gargv */ 404 405 /* 406 * Variables for command expansion. 407 */ 408 extern Char **pargv; /* Pointer to the argv list space */ 409 extern long pargc; /* Count of arguments in pargv */ 410 Char *pargs; /* Pointer to start current word */ 411 long pnleft; /* Number of chars left in pargs */ 412 Char *pargcp; /* Current index into pargs */ 413 414 /* 415 * History list 416 * 417 * Each history list entry contains an embedded wordlist 418 * from the scanner, a number for the event, and a reference count 419 * to aid in discarding old entries. 420 * 421 * Essentially "invisible" entries are put on the history list 422 * when history substitution includes modifiers, and thrown away 423 * at the next discarding since their event numbers are very negative. 424 */ 425 struct Hist { 426 struct wordent Hlex; 427 int Hnum; 428 int Href; 429 struct Hist *Hnext; 430 } Histlist; 431 432 struct wordent paraml; /* Current lexical word list */ 433 int eventno; /* Next events number */ 434 int lastev; /* Last event reference (default) */ 435 436 Char HIST; /* history invocation character */ 437 Char HISTSUB; /* auto-substitute character */ 438 439 /* 440 * strings.h: 441 */ 442 #ifndef SHORT_STRINGS 443 #define Strchr(a, b) strchr(a, b) 444 #define Strrchr(a, b) strrchr(a, b) 445 #define Strcat(a, b) strcat(a, b) 446 #define Strncat(a, b, c) strncat(a, b, c) 447 #define Strcpy(a, b) strcpy(a, b) 448 #define Strncpy(a, b, c) strncpy(a, b, c) 449 #define Strlen(a) strlen(a) 450 #define Strcmp(a, b) strcmp(a, b) 451 #define Strncmp(a, b, c) strncmp(a, b, c) 452 453 #define Strspl(a, b) strspl(a, b) 454 #define Strsave(a) strsave(a) 455 #define Strend(a) strend(a) 456 #define Strstr(a, b) strstr(a, b) 457 458 #define str2short(a) (a) 459 #define blk2short(a) saveblk(a) 460 #define short2blk(a) saveblk(a) 461 #define short2str(a) strip(a) 462 #else 463 #define Strchr(a, b) s_strchr(a, b) 464 #define Strrchr(a, b) s_strrchr(a, b) 465 #define Strcat(a, b) s_strcat(a, b) 466 #define Strncat(a, b, c) s_strncat(a, b, c) 467 #define Strcpy(a, b) s_strcpy(a, b) 468 #define Strncpy(a, b, c) s_strncpy(a, b, c) 469 #define Strlen(a) s_strlen(a) 470 #define Strcmp(a, b) s_strcmp(a, b) 471 #define Strncmp(a, b, c) s_strncmp(a, b, c) 472 473 #define Strspl(a, b) s_strspl(a, b) 474 #define Strsave(a) s_strsave(a) 475 #define Strend(a) s_strend(a) 476 #define Strstr(a, b) s_strstr(a, b) 477 #endif 478 479 /* 480 * setname is a macro to save space (see sh.err.c) 481 */ 482 char *bname; 483 484 #define setname(a) (bname = (a)) 485 486 Char *Vsav; 487 Char *Vdp; 488 Char *Vexpath; 489 char **Vt; 490 491 Char **evalvec; 492 Char *evalp; 493 494 /* word_chars is set by default to WORD_CHARS but can be overridden by 495 the worchars variable--if unset, reverts to WORD_CHARS */ 496 497 Char *word_chars; 498 499 #define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */ 500 501 Char *STR_SHELLPATH; 502 503 #include <paths.h> 504 #ifdef _PATH_BSHELL 505 Char *STR_BSHELL; 506 #endif 507 Char *STR_WORD_CHARS; 508 Char **STR_environ; 509