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