1176771Sraj/* $NetBSD: csh.h,v 1.21 2007/07/16 18:26:09 christos Exp $ */ 2176771Sraj 3176771Sraj/*- 4176771Sraj * Copyright (c) 1980, 1991, 1993 5176771Sraj * The Regents of the University of California. All rights reserved. 6176771Sraj * 7176771Sraj * Redistribution and use in source and binary forms, with or without 8176771Sraj * modification, are permitted provided that the following conditions 9176771Sraj * are met: 10176771Sraj * 1. Redistributions of source code must retain the above copyright 11176771Sraj * notice, this list of conditions and the following disclaimer. 12176771Sraj * 2. Redistributions in binary form must reproduce the above copyright 13176771Sraj * notice, this list of conditions and the following disclaimer in the 14176771Sraj * documentation and/or other materials provided with the distribution. 15176771Sraj * 3. Neither the name of the University nor the names of its contributors 16176771Sraj * may be used to endorse or promote products derived from this software 17176771Sraj * without specific prior written permission. 18176771Sraj * 19266312Sian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20176771Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21176771Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22176771Sraj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23176771Sraj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24176771Sraj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25176771Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26176771Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27176771Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28176771Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29176771Sraj * SUCH DAMAGE. 30176771Sraj * 31176771Sraj * @(#)csh.h 8.1 (Berkeley) 5/31/93 32176771Sraj */ 33176771Sraj 34176771Sraj#ifndef _CSH_H_ 35176771Sraj#define _CSH_H_ 36176771Sraj 37176771Sraj/* 38176771Sraj * Fundamental definitions which may vary from system to system. 39176771Sraj * 40176771Sraj * BUFSIZE The i/o buffering size; also limits word size 41176771Sraj * MAILINTVL How often to mailcheck; more often is more expensive 42253367Sae */ 43176771Sraj#ifndef BUFSIZE 44176771Sraj#define BUFSIZE 4096 /* default buffer size */ 45176771Sraj#endif /* BUFSIZE */ 46176771Sraj 47176771Sraj#define FORKSLEEP 10 /* delay loop on non-interactive fork failure */ 48176771Sraj#define MAILINTVL 600 /* 10 minutes */ 49176771Sraj 50176771Sraj/* 51176771Sraj * The shell moves std in/out/diag and the old std input away from units 52176771Sraj * 0, 1, and 2 so that it is easy to set up these standards for invoked 53176771Sraj * commands. 54176771Sraj */ 55176771Sraj#define FSHTTY 15 /* /dev/tty when manip pgrps */ 56176771Sraj#define FSHIN 16 /* Preferred desc for shell input */ 57176771Sraj#define FSHOUT 17 /* ... shell output */ 58176771Sraj#define FSHERR 18 /* ... shell diagnostics */ 59176771Sraj#define FOLDSTD 19 /* ... old std input */ 60176771Sraj 61176771Sraj#ifdef PROF 62176771Sraj#define xexit(n) done(n) 63176771Sraj#endif 64176771Sraj 65176771Sraj#ifdef SHORT_STRINGS 66176771Srajtypedef short Char; 67176771Sraj 68176771Sraj#define SAVE(a) (Strsave(str2short(a))) 69176771Sraj#else 70176771Srajtypedef char Char; 71176771Sraj 72176771Sraj#define SAVE(a) (strsave(a)) 73176771Sraj#endif 74176771Sraj 75176771Sraj/* 76176771Sraj * Make sure a variable is not stored in a register by taking its address 77176771Sraj * This is used where variables might be clobbered by longjmp. 78176771Sraj */ 79176771Sraj#define UNREGISTER(a) (void) &a 80176771Sraj 81176771Srajtypedef void *ioctl_t; /* Third arg of ioctl */ 82176771Sraj 83176771Srajtypedef void *ptr_t; 84176771Sraj 85176771Sraj#include "const.h" 86176771Sraj#include "char.h" 87176771Sraj#include "errnum.h" 88176771Sraj 89176771Sraj#define xmalloc(i) Malloc(i) 90176771Sraj#define xrealloc(p, i) Realloc(p, i) 91176771Sraj#define xcalloc(n, s) Calloc(n, s) 92176771Sraj#define xfree(p) Free(p) 93176771Sraj 94176771Sraj#include <stdio.h> 95176771SrajFILE *cshin, *cshout, *csherr; 96176771Sraj 97176771Sraj#define isdir(d) (S_ISDIR(d.st_mode)) 98176771Sraj 99176771Sraj#define eq(a, b) (Strcmp(a, b) == 0) 100218195Smdf 101176771Sraj/* globone() flags */ 102176771Sraj#define G_ERROR 0 /* default action: error if multiple words */ 103176771Sraj#define G_IGNORE 1 /* ignore the rest of the words */ 104176771Sraj#define G_APPEND 2 /* make a sentence by cat'ing the words */ 105176771Sraj 106176771Sraj/* 107176771Sraj * Global flags 108176771Sraj */ 109176771Srajint child; /* Child shell ... errors cause exit */ 110176771Srajint chkstop; /* Warned of stopped jobs... allow exit */ 111176771Srajint didfds; /* Have setup i/o fd's for child */ 112176771Srajint doneinp; /* EOF indicator after reset from readc */ 113176771Srajint exiterr; /* Exit if error or non-zero exit status */ 114176771Srajint haderr; /* Reset was because of an error */ 115176771Srajint havhash; /* path hashing is available */ 116176771Srajint intact; /* We are interactive... therefore prompt */ 117176771Srajint intty; /* Input is a tty */ 118176771Srajint justpr; /* Just print because of :p hist mod */ 119176771Srajint loginsh; /* We are a loginsh -> .login/.logout */ 120176771Srajint neednote; /* Need to pnotify() */ 121176771Srajint noexec; /* Don't execute, just syntax check */ 122176771Srajint pjobs; /* want to print jobs if interrupted */ 123176771Srajint setintr; /* Set interrupts on/off -> Wait intr... */ 124176771Srajint timflg; /* Time the next waited for command */ 125176771Sraj 126176771Sraj#ifdef FILEC 127176771Srajextern int filec; /* doing filename expansion */ 128176771Sraj#endif 129176771Sraj 130176771Sraj/* 131176771Sraj * Global i/o info 132 */ 133Char *arginp; /* Argument input for sh -c and internal `xx` */ 134Char *ffile; /* Name of shell file for $0 */ 135int onelflg; /* 2 -> need line for -t, 1 -> exit on read */ 136 137extern char *seterr; /* Error message from scanner/parser */ 138Char *shtemp; /* Temp name for << shell files in /tmp */ 139 140#include <sys/resource.h> 141#include <sys/time.h> 142#include <sys/types.h> 143 144struct timespec time0; /* Time at which the shell started */ 145struct rusage ru0; 146 147/* 148 * Miscellany 149 */ 150time_t chktim; /* Time mail last checked */ 151Char *doldol; /* Character pid for $$ */ 152int backpid; /* Pid of the last background process */ 153int egid, gid; /* Invokers gid */ 154int euid, uid; /* Invokers uid */ 155int shpgrp; /* Pgrp of shell */ 156int tpgrp; /* Terminal process group */ 157 158/* If tpgrp is -1, leave tty alone! */ 159int opgrp; /* Initial pgrp and tty pgrp */ 160 161 162/* 163 * To be able to redirect i/o for builtins easily, the shell moves the i/o 164 * descriptors it uses away from 0,1,2. 165 * Ideally these should be in units which are closed across exec's 166 * (this saves work) but for version 6, this is not usually possible. 167 * The desired initial values for these descriptors are F{SHIN,...}. 168 */ 169int SHIN; /* Current shell input (script) */ 170int SHOUT; /* Shell output */ 171int SHERR; /* Diagnostic output... shell errs go here */ 172int OLDSTD; /* Old standard input (def for cmds) */ 173 174/* 175 * Error control 176 * 177 * Errors in scanning and parsing set up an error message to be printed 178 * at the end and complete. Other errors always cause a reset. 179 * Because of source commands and .cshrc we need nested error catches. 180 */ 181 182#include <setjmp.h> 183jmp_buf reslab; 184 185#define setexit() (setjmp(reslab)) 186#define reset() longjmp(reslab, 1) 187 /* Should use structure assignment here */ 188#define getexit(a) (void)memcpy((a), reslab, sizeof reslab) 189#define resexit(a) (void)memcpy(reslab, (a), sizeof reslab) 190 191Char *gointr; /* Label for an onintr transfer */ 192 193#include <signal.h> 194sig_t parintr; /* Parents interrupt catch */ 195sig_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 0100000 /* 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 216int 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 */ 225struct 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 */ 236struct 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} ; 250extern 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 reseeking the input 263 * For whiles, in particular, it reseeks to the beginning of the 264 * line the while was on; hence the while placement restrictions. 265 */ 266struct Ain lineloc; 267 268int 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 */ 274struct 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 */ 300Char *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 */ 309struct 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 357extern struct biltins { 358 const char *bname; 359 void (*bfunct)(Char **, struct command *); 360 short minargs, maxargs; 361} bfunc[]; 362 363extern int nbfunc; 364extern int nsrchn; 365 366extern struct srch { 367 const char *s_name; 368 short s_value; 369} srchn[]; 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 423#define adrof(v) adrof1(v, &shvhed) 424#define value(v) value1(v, &shvhed) 425 426/* 427 * The following are for interfacing redo substitution in 428 * aliases to the lexical routines. 429 */ 430struct wordent *alhistp; /* Argument list (first) */ 431struct wordent *alhistt; /* Node after last in arg list */ 432extern Char **alvec, *alvecp; /* The (remnants of) alias vector */ 433 434/* 435 * Filename/command name expansion variables 436 */ 437int gflag; /* After tglob -> is globbing needed? */ 438 439#define MAXVARLEN 30 /* Maximum number of char in a variable name */ 440 441/* 442 * Variables for filename expansion 443 */ 444extern long gargc; /* Number args in gargv */ 445extern Char **gargv; /* Pointer to the (stack) arglist */ 446 447/* 448 * Variables for command expansion. 449 */ 450extern Char **pargv; /* Pointer to the argv list space */ 451extern long pargc; /* Count of arguments in pargv */ 452long pnleft; /* Number of chars left in pargs */ 453Char *pargs; /* Pointer to start current word */ 454Char *pargcp; /* Current index into pargs */ 455 456/* 457 * History list 458 * 459 * Each history list entry contains an embedded wordlist 460 * from the scanner, a number for the event, and a reference count 461 * to aid in discarding old entries. 462 * 463 * Essentially "invisible" entries are put on the history list 464 * when history substitution includes modifiers, and thrown away 465 * at the next discarding since their event numbers are very negative. 466 */ 467struct Hist { 468 struct wordent Hlex; 469 int Hnum; 470 int Href; 471 struct Hist *Hnext; 472} Histlist; 473 474struct wordent paraml; /* Current lexical word list */ 475int eventno; /* Next events number */ 476int lastev; /* Last event reference (default) */ 477 478Char HIST; /* history invocation character */ 479Char HISTSUB; /* auto-substitute character */ 480 481/* 482 * strings.h: 483 */ 484#ifndef SHORT_STRINGS 485#define Strchr(a, b) strchr(a, b) 486#define Strrchr(a, b) strrchr(a, b) 487#define Strcat(a, b) strcat(a, b) 488#define Strncat(a, b, c) strncat(a, b, c) 489#define Strcpy(a, b) strcpy(a, b) 490#define Strncpy(a, b, c) strncpy(a, b, c) 491#define Strlen(a) strlen(a) 492#define Strcmp(a, b) strcmp(a, b) 493#define Strncmp(a, b, c) strncmp(a, b, c) 494 495#define Strspl(a, b) strspl(a, b) 496#define Strsave(a) strsave(a) 497#define Strend(a) strend(a) 498#define Strstr(a, b) strstr(a, b) 499 500#define str2short(a) (a) 501#define blk2short(a) saveblk(a) 502#define short2blk(a) saveblk(a) 503#define short2str(a) strip(a) 504#else 505#define Strchr(a, b) s_strchr(a, b) 506#define Strrchr(a, b) s_strrchr(a, b) 507#define Strcat(a, b) s_strcat(a, b) 508#define Strncat(a, b, c) s_strncat(a, b, c) 509#define Strcpy(a, b) s_strcpy(a, b) 510#define Strncpy(a, b, c) s_strncpy(a, b, c) 511#define Strlen(a) s_strlen(a) 512#define Strcmp(a, b) s_strcmp(a, b) 513#define Strncmp(a, b, c) s_strncmp(a, b, c) 514 515#define Strspl(a, b) s_strspl(a, b) 516#define Strsave(a) s_strsave(a) 517#define Strend(a) s_strend(a) 518#define Strstr(a, b) s_strstr(a, b) 519#endif 520 521/* 522 * setname is a macro to save space (see sh.err.c) 523 */ 524const char *bname; 525 526#define setname(a) (bname = (a)) 527 528Char *Vsav; 529Char *Vdp; 530Char *Vexpath; 531char **Vt; 532 533Char **evalvec; 534Char *evalp; 535 536/* word_chars is set by default to WORD_CHARS but can be overridden by 537 the worchars variable--if unset, reverts to WORD_CHARS */ 538 539Char *word_chars; 540 541#define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */ 542 543Char *STR_SHELLPATH; 544 545#include <paths.h> 546#ifdef _PATH_BSHELL 547Char *STR_BSHELL; 548#endif 549Char *STR_WORD_CHARS; 550Char **STR_environ; 551 552#endif /* !_CSH_H_ */ 553