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