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