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