sh.h revision 59243
1/* $Header: /src/pub/tcsh/sh.h,v 3.86 1998/10/25 15:10:16 christos Exp $ */ 2/* 3 * sh.h: Catch it all globals and includes file! 4 */ 5/*- 6 * Copyright (c) 1980, 1991 The Regents of the University of California. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37#ifndef _h_sh 38#define _h_sh 39 40#include "config.h" 41 42#ifndef EXTERN 43# define EXTERN extern 44#else /* !EXTERN */ 45# ifdef WINNT 46# define IZERO = 0 47# define IZERO_STRUCT = {0} 48# endif /* WINNT */ 49#endif /* EXTERN */ 50 51#ifndef IZERO 52# define IZERO 53#endif /* IZERO */ 54#ifndef IZERO_STRUCT 55# define IZERO_STRUCT 56# endif /* IZERO_STRUCT */ 57 58#ifndef WINNT 59# define INIT_ZERO 60# define INIT_ZERO_STRUCT 61# define force_read read 62#endif /*!WINNT */ 63/* 64 * Sanity 65 */ 66#if defined(_POSIX_SOURCE) && !defined(POSIX) 67# define POSIX 68#endif 69 70#if defined(POSIXJOBS) && !defined(BSDJOBS) 71# define BSDJOBS 72#endif 73 74#if defined(POSIXSIGS) && !defined(BSDSIGS) 75# define BSDSIGS 76#endif 77 78#ifdef SHORT_STRINGS 79typedef short Char; 80typedef unsigned short uChar; 81# define SAVE(a) (Strsave(str2short(a))) 82#else 83typedef char Char; 84typedef unsigned char uChar; 85# define SAVE(a) (strsave(a)) 86#endif 87 88/* Elide unused argument warnings */ 89#define USE(a) (void) (a) 90/* 91 * If your compiler complains, then you can either 92 * throw it away and get gcc or, use the following define 93 * and get rid of the typedef. 94 * [The 4.2/3BSD vax compiler does not like that] 95 * Both MULTIFLOW and PCC compilers exhbit this bug. -- sterling@netcom.com 96 */ 97#ifdef SIGVOID 98# if (defined(vax) || defined(uts) || defined(MULTIFLOW) || defined(PCC)) && !defined(__GNUC__) 99# define sigret_t void 100# else /* !((vax || uts || MULTIFLOW || PCC) && !__GNUC__) */ 101typedef void sigret_t; 102# endif /* (vax || uts || MULTIFLOW || PCC) && !__GNUC__ */ 103#else /* !SIGVOID */ 104typedef int sigret_t; 105#endif /* SIGVOID */ 106 107/* 108 * Return true if the path is absolute 109 */ 110#ifndef WINNT 111# define ABSOLUTEP(p) (*(p) == '/') 112#else /* WINNT */ 113# define ABSOLUTEP(p) ((p)[0] == '/' || \ 114 (Isalpha((p)[0]) && (p)[1] == ':' && (p)[2] == '/')) 115#endif /* WINNT */ 116 117/* 118 * Fundamental definitions which may vary from system to system. 119 * 120 * BUFSIZE The i/o buffering size; also limits word size 121 * MAILINTVL How often to mailcheck; more often is more expensive 122 */ 123#ifdef BUFSIZE 124# if BUFSIZE < 1024 125# undef BUFSIZE 126# define BUFSIZE 1024 /* buffer size should be no less than this */ 127# endif 128#else 129# define BUFSIZE 1024 130#endif /* BUFSIZE */ 131 132#define FORKSLEEP 10 /* delay loop on non-interactive fork failure */ 133#define MAILINTVL 600 /* 10 minutes */ 134 135#ifndef INBUFSIZE 136# define INBUFSIZE 2*BUFSIZE /* Num input characters on the command line */ 137#endif /* INBUFSIZE */ 138 139 140/* 141 * What our builtin echo looks like 142 */ 143#define NONE_ECHO 0 144#define BSD_ECHO 1 145#define SYSV_ECHO 2 146#define BOTH_ECHO (BSD_ECHO|SYSV_ECHO) 147 148#ifndef ECHO_STYLE 149# if SYSVREL > 0 150# define ECHO_STYLE SYSV_ECHO 151# else /* SYSVREL == 0 */ 152# define ECHO_STYLE BSD_ECHO 153# endif /* SYSVREL */ 154#endif /* ECHO_STYLE */ 155 156/* 157 * The shell moves std in/out/diag and the old std input away from units 158 * 0, 1, and 2 so that it is easy to set up these standards for invoked 159 * commands. 160 */ 161#define FSHTTY 15 /* /dev/tty when manip pgrps */ 162#define FSHIN 16 /* Preferred desc for shell input */ 163#define FSHOUT 17 /* ... shell output */ 164#define FSHDIAG 18 /* ... shell diagnostics */ 165#define FOLDSTD 19 /* ... old std input */ 166 167#ifdef PROF 168#define xexit(n) done(n) 169#endif 170 171#ifdef cray 172# define word word_t /* sys/types.h defines word.. bad move! */ 173#endif 174 175#include <sys/types.h> 176 177#ifdef cray 178# undef word 179#endif 180 181/* 182 * Path separator in environment variables 183 */ 184#ifndef PATHSEP 185# if defined(__EMX__) || defined(WINNT) 186# define PATHSEP ';' 187# else /* unix */ 188# define PATHSEP ':' 189# endif /* __EMX__ || WINNT */ 190#endif /* !PATHSEP */ 191 192#ifdef __HP_CXD_SPP 193# include <sys/cnx_stat.h> 194# define stat stat64 195# define fstat fstat64 196# define lstat lstat64 197#endif /* __HP_CXD_SPP */ 198 199/* 200 * This macro compares the st_dev field of struct stat. On aix on ibmESA 201 * st_dev is a structure, so comparison does not work. 202 */ 203#ifndef DEV_DEV_COMPARE 204# define DEV_DEV_COMPARE(x,y) ((x) == (y)) 205#endif /* DEV_DEV_COMPARE */ 206 207#ifdef _SEQUENT_ 208# include <sys/procstats.h> 209#endif /* _SEQUENT_ */ 210#if (defined(POSIX) || SYSVREL > 0) && !WINNT 211# include <sys/times.h> 212#endif /* (POSIX || SYSVREL > 0) && !WINNT */ 213 214#ifdef NLS 215# include <locale.h> 216#endif /* NLS */ 217 218 219#if !defined(_MINIX) && !defined(_VMS_POSIX) && !defined(WINNT) 220# include <sys/param.h> 221#endif /* !_MINIX && !_VMS_POSIX && !WINNT */ 222#include <sys/stat.h> 223 224#if defined(BSDTIMES) || defined(BSDLIMIT) 225# include <sys/time.h> 226# if SYSVREL>3 && !defined(SCO) && !defined(sgi) && !defined(SNI) && !defined(sun) && !(defined(__alpha) && defined(__osf__)) && !defined(_SX) 227# include "/usr/ucbinclude/sys/resource.h" 228# else 229# ifdef convex 230# define sysrusage cvxrusage 231# include <sys/sysinfo.h> 232# else 233# define sysrusage rusage 234# include <sys/resource.h> 235# endif /* convex */ 236# endif /* SYSVREL>3 */ 237#endif /* BSDTIMES */ 238 239#ifndef WINNT 240# ifndef POSIX 241# ifdef TERMIO 242# include <termio.h> 243# else /* SGTTY */ 244# include <sgtty.h> 245# endif /* TERMIO */ 246# else /* POSIX */ 247# ifndef _UWIN 248# include <termios.h> 249# else 250# include <termio.h> 251# endif /* _UWIN */ 252# if SYSVREL > 3 253# undef TIOCGLTC /* we don't need those, since POSIX has them */ 254# undef TIOCSLTC 255# undef CSWTCH 256# define CSWTCH _POSIX_VDISABLE /* So job control works */ 257# endif /* SYSVREL > 3 */ 258# endif /* POSIX */ 259#endif /* WINNT */ 260 261#ifdef sonyrisc 262# include <sys/ttold.h> 263#endif /* sonyrisc */ 264 265#if defined(POSIX) && !defined(WINNT) 266/* 267 * We should be using setpgid and setpgid 268 * by now, but in some systems we use the 269 * old routines... 270 */ 271# define getpgrp __getpgrp 272# define setpgrp __setpgrp 273# include <unistd.h> 274# undef getpgrp 275# undef setpgrp 276 277/* 278 * the gcc+protoize version of <stdlib.h> 279 * redefines malloc(), so we define the following 280 * to avoid it. 281 */ 282# if defined(linux) || defined(sgi) || defined(_OSD_POSIX) 283# define NO_FIX_MALLOC 284# include <stdlib.h> 285# else /* linux */ 286# define _GNU_STDLIB_H 287# define malloc __malloc 288# define free __free 289# define calloc __calloc 290# define realloc __realloc 291# include <stdlib.h> 292# undef malloc 293# undef free 294# undef calloc 295# undef realloc 296# endif /* linux || sgi */ 297# include <limits.h> 298#endif /* POSIX && !WINNT */ 299 300#if SYSVREL > 0 || defined(_IBMR2) || defined(_MINIX) 301# if !defined(pyr) && !defined(stellar) 302# include <time.h> 303# ifdef _MINIX 304# define HZ CLOCKS_PER_SEC 305# endif /* _MINIX */ 306# endif /* !pyr && !stellar */ 307#endif /* SYSVREL > 0 || _IBMR2 */ 308 309/* In the following ifdef the DECOSF1 has been commented so that later 310 * versions of DECOSF1 will get TIOCGWINSZ. This might break older versions... 311 */ 312#if !((defined(SUNOS4) || defined(_MINIX) /* || defined(DECOSF1) */) && defined(TERMIO)) 313# if !defined(COHERENT) && !defined(_VMS_POSIX) && !defined(WINNT) 314# include <sys/ioctl.h> 315# endif 316#endif 317 318#if (defined(__DGUX__) && defined(POSIX)) || defined(DGUX) 319#undef CSWTCH 320#define CSWTCH _POSIX_VDISABLE 321#endif 322 323#if (!defined(FIOCLEX) && defined(SUNOS4)) || ((SYSVREL == 4) && !defined(_SEQUENT_) && !defined(SCO) && !defined(_SX)) 324# include <sys/filio.h> 325#endif /* (!FIOCLEX && SUNOS4) || (SYSVREL == 4 && !_SEQUENT_ && !SCO && !_SX ) */ 326 327#if !defined(_MINIX) && !defined(COHERENT) && !defined(supermax) && !defined(WINNT) 328# include <sys/file.h> 329#endif /* !_MINIX && !COHERENT && !supermax && !WINNT */ 330 331#if !defined(O_RDONLY) || !defined(O_NDELAY) 332# include <fcntl.h> 333#endif 334 335#include <errno.h> 336 337#include <setjmp.h> 338 339#if __STDC__ || defined(FUNCPROTO) 340# include <stdarg.h> 341#else 342#ifdef _MINIX 343# include "mi.varargs.h" 344#else 345# include <varargs.h> 346#endif /* _MINIX */ 347#endif 348 349#ifdef DIRENT 350# include <dirent.h> 351#else 352# ifdef hp9000s500 353# include <ndir.h> 354# else 355# include <sys/dir.h> 356# endif 357# define dirent direct 358#endif /* DIRENT */ 359#if defined(hpux) || defined(sgi) || defined(OREO) || defined(COHERENT) 360# include <stdio.h> /* So the fgetpwent() prototypes work */ 361#endif /* hpux || sgi || OREO || COHERENT */ 362#ifndef WINNT 363#include <pwd.h> 364#include <grp.h> 365#endif /* WINNT */ 366#ifdef PW_SHADOW 367# include <shadow.h> 368#endif /* PW_SHADOW */ 369#ifdef PW_AUTH 370# include <auth.h> 371#endif /* PW_AUTH */ 372#if defined(BSD) && !defined(POSIX) 373# include <strings.h> 374# define strchr(a, b) index(a, b) 375# define strrchr(a, b) rindex(a, b) 376#else 377# include <string.h> 378#endif /* BSD */ 379 380/* 381 * IRIX-5.0 has <sys/cdefs.h>, but most system include files do not 382 * include it yet, so we include it here 383 */ 384#if defined(sgi) && SYSVREL > 3 385# include <sys/cdefs.h> 386#endif /* sgi && SYSVREL > 3 */ 387 388#ifdef REMOTEHOST 389# ifdef ISC 390# undef MAXHOSTNAMELEN /* Busted headers? */ 391# endif 392 393# include <netinet/in.h> 394# include <arpa/inet.h> 395# include <sys/socket.h> 396# include <sys/uio.h> /* For struct iovec */ 397#endif /* REMOTEHOST */ 398 399/* 400 * ANSIisms... These must be *after* the system include and 401 * *before* our includes, so that BSDreno has time to define __P 402 */ 403#undef __P 404#ifndef __P 405# if __STDC__ || defined(FUNCPROTO) 406# ifndef FUNCPROTO 407# define FUNCPROTO 408# endif 409# define __P(a) a 410# else 411# define __P(a) () 412# if !__STDC__ 413# define const 414# ifndef apollo 415# define volatile /* Apollo 'c' extensions need this */ 416# endif /* apollo */ 417# endif 418# endif 419#endif 420 421 422#ifdef PURIFY 423/* exit normally, allowing purify to trace leaks */ 424# define _exit exit 425typedef int pret_t; 426#else /* !PURIFY */ 427/* 428 * If your compiler complains, then you can either 429 * throw it away and get gcc or, use the following define 430 * and get rid of the typedef. 431 * [The 4.2/3BSD vax compiler does not like that] 432 * Both MULTIFLOW and PCC compilers exhbit this bug. -- sterling@netcom.com 433 */ 434# if (defined(vax) || defined(uts) || defined(MULTIFLOW) || defined(PCC)) && !defined(__GNUC__) 435# define pret_t void 436# else /* !((vax || uts || MULTIFLOW || PCC) && !__GNUC__) */ 437typedef void pret_t; 438# endif /* (vax || uts || MULTIFLOW || PCC) && !__GNUC__ */ 439#endif /* PURIFY */ 440 441typedef int bool; 442 443#include "sh.types.h" 444 445#ifndef WINNT 446# ifndef POSIX 447extern pid_t getpgrp __P((int)); 448# else /* POSIX */ 449# if (defined(BSD) && !defined(BSD4_4)) || defined(SUNOS4) || defined(IRIS4D) || defined(DGUX) 450extern pid_t getpgrp __P((int)); 451# else /* !(BSD || SUNOS4 || IRIS4D || DGUX) */ 452extern pid_t getpgrp __P((void)); 453# endif /* BSD || SUNOS4 || IRISD || DGUX */ 454# endif /* POSIX */ 455extern pid_t setpgrp __P((pid_t, pid_t)); 456#endif /* !WINNT */ 457 458typedef sigret_t (*signalfun_t) __P((int)); 459 460#ifndef lint 461typedef ptr_t memalign_t; 462#else 463typedef union { 464 char am_char, *am_char_p; 465 short am_short, *am_short_p; 466 int am_int, *am_int_p; 467 long am_long, *am_long_p; 468 float am_float, *am_float_p; 469 double am_double, *am_double_p; 470} *memalign_t; 471 472# define malloc lint_malloc 473# define free lint_free 474# define realloc lint_realloc 475# define calloc lint_calloc 476#endif 477 478#ifdef MDEBUG 479extern memalign_t DebugMalloc __P((unsigned, char *, int)); 480extern memalign_t DebugRealloc __P((ptr_t, unsigned, char *, int)); 481extern memalign_t DebugCalloc __P((unsigned, unsigned, char *, int)); 482extern void DebugFree __P((ptr_t, char *, int)); 483# define xmalloc(i) DebugMalloc(i, __FILE__, __LINE__) 484# define xrealloc(p, i)((p) ? DebugRealloc(p, i, __FILE__, __LINE__) : \ 485 DebugMalloc(i, __FILE__, __LINE__)) 486# define xcalloc(n, s) DebugCalloc(n, s, __FILE__, __LINE__) 487# define xfree(p) if (p) DebugFree(p, __FILE__, __LINE__) 488#else 489# ifdef SYSMALLOC 490# define xmalloc(i) smalloc(i) 491# define xrealloc(p, i) srealloc(p, i) 492# define xcalloc(n, s) scalloc(n, s) 493# define xfree(p) sfree(p) 494# else 495# define xmalloc(i) malloc(i) 496# define xrealloc(p, i) realloc(p, i) 497# define xcalloc(n, s) calloc(n, s) 498# define xfree(p) free(p) 499# endif /* SYSMALLOC */ 500#endif /* MDEBUG */ 501#include "sh.char.h" 502#include "sh.err.h" 503#include "sh.dir.h" 504#include "sh.proc.h" 505 506#include "pathnames.h" 507 508 509/* 510 * C shell 511 * 512 * Bill Joy, UC Berkeley 513 * October, 1978; May 1980 514 * 515 * Jim Kulp, IIASA, Laxenburg Austria 516 * April, 1980 517 */ 518 519#if !defined(MAXNAMLEN) && defined(_D_NAME_MAX) 520# define MAXNAMLEN _D_NAME_MAX 521#endif /* MAXNAMLEN */ 522 523#ifdef HESIOD 524# include <hesiod.h> 525#endif /* HESIOD */ 526 527#ifdef REMOTEHOST 528# include <netdb.h> 529#endif /* REMOTEHOST */ 530 531#ifndef MAXHOSTNAMELEN 532# if defined(SCO) && (SYSVREL > 3) 533# include <sys/socket.h> 534# else 535# define MAXHOSTNAMELEN 255 536# endif 537#endif /* MAXHOSTNAMELEN */ 538 539 540 541#define eq(a, b) (Strcmp(a, b) == 0) 542 543/* globone() flags */ 544#define G_ERROR 0 /* default action: error if multiple words */ 545#define G_IGNORE 1 /* ignore the rest of the words */ 546#define G_APPEND 2 /* make a sentence by cat'ing the words */ 547 548/* 549 * Global flags 550 */ 551EXTERN bool chkstop IZERO; /* Warned of stopped jobs... allow exit */ 552 553#if (defined(FIOCLEX) && defined(FIONCLEX)) || defined(F_SETFD) 554# define CLOSE_ON_EXEC 555#else 556EXTERN bool didcch IZERO; /* Have closed unused fd's for child */ 557#endif /* (FIOCLEX && FIONCLEX) || F_SETFD */ 558 559EXTERN bool didfds IZERO; /* Have setup i/o fd's for child */ 560EXTERN bool doneinp IZERO; /* EOF indicator after reset from readc */ 561EXTERN bool exiterr IZERO; /* Exit if error or non-zero exit status */ 562EXTERN bool child IZERO; /* Child shell ... errors cause exit */ 563EXTERN bool haderr IZERO; /* Reset was because of an error */ 564EXTERN bool intty IZERO; /* Input is a tty */ 565EXTERN bool intact IZERO; /* We are interactive... therefore prompt */ 566EXTERN bool justpr IZERO; /* Just print because of :p hist mod */ 567EXTERN bool loginsh IZERO; /* We are a loginsh -> .login/.logout */ 568EXTERN bool neednote IZERO; /* Need to pnotify() */ 569EXTERN bool noexec IZERO; /* Don't execute, just syntax check */ 570EXTERN bool pjobs IZERO; /* want to print jobs if interrupted */ 571EXTERN bool setintr IZERO; /* Set interrupts on/off -> Wait intr... */ 572EXTERN bool timflg IZERO; /* Time the next waited for command */ 573EXTERN bool havhash IZERO; /* path hashing is available */ 574EXTERN bool editing IZERO; /* doing filename expansion and line editing */ 575EXTERN bool noediting IZERO; /* initial $term defaulted to noedit */ 576EXTERN bool bslash_quote IZERO;/* PWP: tcsh-style quoting? (in sh.c) */ 577EXTERN bool isoutatty IZERO; /* is SHOUT a tty */ 578EXTERN bool isdiagatty IZERO;/* is SHDIAG a tty */ 579EXTERN bool is1atty IZERO; /* is file descriptor 1 a tty (didfds mode) */ 580EXTERN bool is2atty IZERO; /* is file descriptor 2 a tty (didfds mode) */ 581EXTERN bool arun IZERO; /* Currently running multi-line-aliases */ 582EXTERN int implicit_cd IZERO;/* implicit cd enabled?(1=enabled,2=verbose) */ 583EXTERN bool inheredoc IZERO; /* Currently parsing a heredoc */ 584 585/* 586 * Global i/o info 587 */ 588EXTERN Char *arginp IZERO; /* Argument input for sh -c and internal `xx` */ 589EXTERN int onelflg IZERO; /* 2 -> need line for -t, 1 -> exit on read */ 590extern Char *ffile; /* Name of shell file for $0 */ 591extern bool dolzero; /* if $?0 should return true... */ 592 593extern char *seterr; /* Error message from scanner/parser */ 594extern int errno; /* Error from C library routines */ 595EXTERN Char *shtemp IZERO; /* Temp name for << shell files in /tmp */ 596 597#ifdef BSDTIMES 598EXTERN struct timeval time0; /* Time at which the shell started */ 599EXTERN struct sysrusage ru0; 600#else 601# ifdef _SEQUENT_ 602EXTERN timeval_t time0; /* time at which shell started */ 603EXTERN struct process_stats ru0; 604# else /* _SEQUENT_ */ 605# ifndef POSIX 606EXTERN time_t time0; /* time at which shell started */ 607# else /* POSIX */ 608EXTERN clock_t time0; /* time at which shell started */ 609EXTERN clock_t clk_tck; 610# endif /* POSIX */ 611EXTERN struct tms shtimes; /* shell and child times for process timing */ 612# endif /* _SEQUENT_ */ 613#endif /* BSDTIMES */ 614 615#ifndef HZ 616# define HZ 100 /* for division into seconds */ 617#endif 618 619/* 620 * Miscellany 621 */ 622EXTERN Char *doldol; /* Character pid for $$ */ 623EXTERN int backpid; /* pid of the last background job */ 624 625/* 626 * Ideally these should be uid_t, gid_t, pid_t. I cannot do that right now 627 * cause pid's could be unsigned and that would break our -1 flag, and 628 * uid_t and gid_t are not defined in all the systems so I would have to 629 * make special cases for them. In the future... 630 */ 631EXTERN int uid, euid, /* Invokers real and effective */ 632 gid, egid; /* User and group ids */ 633EXTERN int opgrp, /* Initial pgrp and tty pgrp */ 634 shpgrp, /* Pgrp of shell */ 635 tpgrp; /* Terminal process group */ 636 /* If tpgrp is -1, leave tty alone! */ 637 638EXTERN Char PromptBuf[INBUFSIZE*2]; /* buffer for the actual printed prompt. 639 * this must be large enough to contain 640 * the input line and the prompt, in 641 * case a correction occurred... 642 */ 643EXTERN Char RPromptBuf[INBUFSIZE]; /* buffer for right-hand side prompt */ 644 645/* 646 * To be able to redirect i/o for builtins easily, the shell moves the i/o 647 * descriptors it uses away from 0,1,2. 648 * Ideally these should be in units which are closed across exec's 649 * (this saves work) but for version 6, this is not usually possible. 650 * The desired initial values for these descriptors are defined in 651 * sh.local.h. 652 */ 653EXTERN int SHIN IZERO; /* Current shell input (script) */ 654EXTERN int SHOUT IZERO; /* Shell output */ 655EXTERN int SHDIAG IZERO; /* Diagnostic output... shell errs go here */ 656EXTERN int OLDSTD IZERO; /* Old standard input (def for cmds) */ 657 658 659#if SYSVREL == 4 && defined(_UTS) 660/* 661 * From: fadden@uts.amdahl.com (Andy McFadden) 662 * we need sigsetjmp for UTS4, but not UTS2.1 663 */ 664# define SIGSETJMP 665#endif 666 667/* 668 * Error control 669 * 670 * Errors in scanning and parsing set up an error message to be printed 671 * at the end and complete. Other errors always cause a reset. 672 * Because of source commands and .cshrc we need nested error catches. 673 */ 674 675#ifdef NO_STRUCT_ASSIGNMENT 676 677# ifdef SIGSETJMP 678 typedef sigjmp_buf jmp_buf_t; 679 /* bugfix by Jak Kirman @ Brown U.: remove the (void) cast here, see sh.c */ 680# define setexit() sigsetjmp(reslab) 681# define reset() siglongjmp(reslab, 1) 682# else 683 typedef jmp_buf jmp_buf_t; 684 /* bugfix by Jak Kirman @ Brown U.: remove the (void) cast here, see sh.c */ 685# define setexit() setjmp(reslab) 686# define reset() longjmp(reslab, 1) 687# endif 688# define getexit(a) (void) memmove((ptr_t)&(a), (ptr_t)&reslab, sizeof(reslab)) 689# define resexit(a) (void) memmove((ptr_t)&reslab, (ptr_t)&(a), sizeof(reslab)) 690 691# define cpybin(a, b) (void) memmove((ptr_t)&(a), (ptr_t)&(b), sizeof(Bin)) 692 693#else 694 695# ifdef SIGSETJMP 696 typedef struct { sigjmp_buf j; } jmp_buf_t; 697# define setexit() sigsetjmp(reslab.j) 698# define reset() siglongjmp(reslab.j, 1) 699# else 700 typedef struct { jmp_buf j; } jmp_buf_t; 701# define setexit() setjmp(reslab.j) 702# define reset() longjmp(reslab.j, 1) 703# endif 704 705# define getexit(a) (void) ((a) = reslab) 706# define resexit(a) (void) (reslab = (a)) 707 708# define cpybin(a, b) (void) ((a) = (b)) 709 710#endif /* NO_STRUCT_ASSIGNMENT */ 711 712extern jmp_buf_t reslab; 713 714EXTERN Char *gointr; /* Label for an onintr transfer */ 715 716extern signalfun_t parintr; /* Parents interrupt catch */ 717extern signalfun_t parterm; /* Parents terminate catch */ 718 719/* 720 * Lexical definitions. 721 * 722 * All lexical space is allocated dynamically. 723 * The eighth/sixteenth bit of characters is used to prevent recognition, 724 * and eventually stripped. 725 */ 726#define META 0200 727#define ASCII 0177 728#ifdef SHORT_STRINGS 729# define QUOTE ((Char) 0100000)/* 16nth char bit used for 'ing */ 730# define TRIM 0077777 /* Mask to strip quote bit */ 731# define UNDER 0040000 /* Underline flag */ 732# define BOLD 0020000 /* Bold flag */ 733# define STANDOUT 0010000 /* Standout flag */ 734# define LITERAL 0004000 /* Literal character flag */ 735# define ATTRIBUTES 0074000 /* The bits used for attributes */ 736# define CHAR 0000377 /* Mask to mask out the character */ 737#else 738# define QUOTE ((Char) 0200) /* Eighth char bit used for 'ing */ 739# define TRIM 0177 /* Mask to strip quote bit */ 740# define UNDER 0000000 /* No extra bits to do both */ 741# define BOLD 0000000 /* Bold flag */ 742# define STANDOUT META /* Standout flag */ 743# define LITERAL 0000000 /* Literal character flag */ 744# define ATTRIBUTES 0200 /* The bits used for attributes */ 745# define CHAR 0000177 /* Mask to mask out the character */ 746#endif 747 748EXTERN int AsciiOnly; /* If set only 7 bits expected in characters */ 749 750/* 751 * Each level of input has a buffered input structure. 752 * There are one or more blocks of buffered input for each level, 753 * exactly one if the input is seekable and tell is available. 754 * In other cases, the shell buffers enough blocks to keep all loops 755 * in the buffer. 756 */ 757EXTERN struct Bin { 758 off_t Bfseekp; /* Seek pointer */ 759 off_t Bfbobp; /* Seekp of beginning of buffers */ 760 off_t Bfeobp; /* Seekp of end of buffers */ 761 int Bfblocks; /* Number of buffer blocks */ 762 Char **Bfbuf; /* The array of buffer blocks */ 763} B; 764 765/* 766 * This structure allows us to seek inside aliases 767 */ 768struct Ain { 769 int type; 770#define I_SEEK -1 /* Invalid seek */ 771#define A_SEEK 0 /* Alias seek */ 772#define F_SEEK 1 /* File seek */ 773#define E_SEEK 2 /* Eval seek */ 774 union { 775 off_t _f_seek; 776 Char* _c_seek; 777 } fc; 778#define f_seek fc._f_seek 779#define c_seek fc._c_seek 780 Char **a_seek; 781} ; 782 783extern int aret; /* Type of last char returned */ 784#define SEEKEQ(a, b) ((a)->type == (b)->type && \ 785 (a)->f_seek == (b)->f_seek && \ 786 (a)->a_seek == (b)->a_seek) 787 788#define fseekp B.Bfseekp 789#define fbobp B.Bfbobp 790#define feobp B.Bfeobp 791#define fblocks B.Bfblocks 792#define fbuf B.Bfbuf 793 794/* 795 * The shell finds commands in loops by reseeking the input 796 * For whiles, in particular, it reseeks to the beginning of the 797 * line the while was on; hence the while placement restrictions. 798 */ 799EXTERN struct Ain lineloc; 800 801EXTERN bool cantell; /* Is current source tellable ? */ 802 803/* 804 * Input lines are parsed into doubly linked circular 805 * lists of words of the following form. 806 */ 807struct wordent { 808 Char *word; 809 struct wordent *prev; 810 struct wordent *next; 811}; 812 813/* 814 * During word building, both in the initial lexical phase and 815 * when expanding $ variable substitutions, expansion by `!' and `$' 816 * must be inhibited when reading ahead in routines which are themselves 817 * processing `!' and `$' expansion or after characters such as `\' or in 818 * quotations. The following flags are passed to the getC routines 819 * telling them which of these substitutions are appropriate for the 820 * next character to be returned. 821 */ 822#define DODOL 1 823#define DOEXCL 2 824#define DOALL DODOL|DOEXCL 825 826/* 827 * Labuf implements a general buffer for lookahead during lexical operations. 828 * Text which is to be placed in the input stream can be stuck here. 829 * We stick parsed ahead $ constructs during initial input, 830 * process id's from `$$', and modified variable values (from qualifiers 831 * during expansion in sh.dol.c) here. 832 */ 833EXTERN Char *lap; 834 835/* 836 * Parser structure 837 * 838 * Each command is parsed to a tree of command structures and 839 * flags are set bottom up during this process, to be propagated down 840 * as needed during the semantics/exeuction pass (sh.sem.c). 841 */ 842struct command { 843 unsigned char t_dtyp; /* Type of node */ 844#define NODE_COMMAND 1 /* t_dcom <t_dlef >t_drit */ 845#define NODE_PAREN 2 /* ( t_dspr ) <t_dlef >t_drit */ 846#define NODE_PIPE 3 /* t_dlef | t_drit */ 847#define NODE_LIST 4 /* t_dlef ; t_drit */ 848#define NODE_OR 5 /* t_dlef || t_drit */ 849#define NODE_AND 6 /* t_dlef && t_drit */ 850 unsigned char t_nice; /* Nice value */ 851#ifdef apollo 852 unsigned char t_systype; /* System environment */ 853#endif 854 unsigned long t_dflg; /* Flags, e.g. F_AMPERSAND|... */ 855/* save these when re-doing */ 856#ifndef apollo 857#define F_SAVE (F_NICE|F_TIME|F_NOHUP|F_HUP) 858#else 859#define F_SAVE (F_NICE|F_TIME|F_NOHUP||F_HUP|F_VER) 860#endif 861#define F_AMPERSAND (1<<0) /* executes in background */ 862#define F_APPEND (1<<1) /* output is redirected >> */ 863#define F_PIPEIN (1<<2) /* input is a pipe */ 864#define F_PIPEOUT (1<<3) /* output is a pipe */ 865#define F_NOFORK (1<<4) /* don't fork, last ()ized cmd */ 866#define F_NOINTERRUPT (1<<5) /* should be immune from intr's */ 867/* spare */ 868#define F_STDERR (1<<7) /* redirect unit 2 with unit 1 */ 869#define F_OVERWRITE (1<<8) /* output was ! */ 870#define F_READ (1<<9) /* input redirection is << */ 871#define F_REPEAT (1<<10) /* reexec aft if, repeat,... */ 872#define F_NICE (1<<11) /* t_nice is meaningful */ 873#define F_NOHUP (1<<12) /* nohup this command */ 874#define F_TIME (1<<13) /* time this command */ 875#define F_BACKQ (1<<14) /* command is in `` */ 876#define F_HUP (1<<15) /* hup this command */ 877#ifdef apollo 878#define F_VER (1<<16) /* execute command under SYSTYPE */ 879#endif 880 union { 881 Char *T_dlef; /* Input redirect word */ 882 struct command *T_dcar; /* Left part of list/pipe */ 883 } L; 884 union { 885 Char *T_drit; /* Output redirect word */ 886 struct command *T_dcdr; /* Right part of list/pipe */ 887 } R; 888#define t_dlef L.T_dlef 889#define t_dcar L.T_dcar 890#define t_drit R.T_drit 891#define t_dcdr R.T_dcdr 892 Char **t_dcom; /* Command/argument vector */ 893 struct command *t_dspr; /* Pointer to ()'d subtree */ 894}; 895 896 897/* 898 * The keywords for the parser 899 */ 900#define TC_BREAK 0 901#define TC_BRKSW 1 902#define TC_CASE 2 903#define TC_DEFAULT 3 904#define TC_ELSE 4 905#define TC_END 5 906#define TC_ENDIF 6 907#define TC_ENDSW 7 908#define TC_EXIT 8 909#define TC_FOREACH 9 910#define TC_GOTO 10 911#define TC_IF 11 912#define TC_LABEL 12 913#define TC_LET 13 914#define TC_SET 14 915#define TC_SWITCH 15 916#define TC_TEST 16 917#define TC_THEN 17 918#define TC_WHILE 18 919 920/* 921 * These are declared here because they want to be 922 * initialized in sh.init.c (to allow them to be made readonly) 923 */ 924 925#if defined(hpux) && defined(__STDC__) && !defined(__GNUC__) 926 /* Avoid hpux ansi mode spurious warnings */ 927typedef void (*bfunc_t) (); 928#else 929typedef void (*bfunc_t) __P((Char **, struct command *)); 930#endif /* hpux && __STDC__ && !__GNUC__ */ 931 932extern struct biltins { 933 char *bname; 934 bfunc_t bfunct; 935 int minargs, maxargs; 936} bfunc[]; 937extern int nbfunc; 938#ifdef WINNT 939extern struct biltins nt_bfunc[]; 940extern int nt_nbfunc; 941#endif /* WINNT*/ 942 943extern struct srch { 944 char *s_name; 945 int s_value; 946} srchn[]; 947extern int nsrchn; 948 949/* 950 * Structure defining the existing while/foreach loops at this 951 * source level. Loops are implemented by seeking back in the 952 * input. For foreach (fe), the word list is attached here. 953 */ 954EXTERN struct whyle { 955 struct Ain w_start; /* Point to restart loop */ 956 struct Ain w_end; /* End of loop (0 if unknown) */ 957 Char **w_fe, **w_fe0; /* Current/initial wordlist for fe */ 958 Char *w_fename; /* Name for fe */ 959 struct whyle *w_next; /* Next (more outer) loop */ 960} *whyles; 961 962/* 963 * Variable structure 964 * 965 * Aliases and variables are stored in AVL balanced binary trees. 966 */ 967EXTERN struct varent { 968 Char **vec; /* Array of words which is the value */ 969 Char *v_name; /* Name of variable/alias */ 970 int v_flags; /* Flags */ 971#define VAR_ALL -1 972#define VAR_READONLY 1 973#define VAR_READWRITE 2 974#define VAR_NOGLOB 4 975#define VAR_FIRST 32 976#define VAR_LAST 64 977 struct varent *v_link[3]; /* The links, see below */ 978 int v_bal; /* Balance factor */ 979} shvhed IZERO_STRUCT, aliases IZERO_STRUCT; 980 981#define v_left v_link[0] 982#define v_right v_link[1] 983#define v_parent v_link[2] 984 985#define adrof(v) adrof1(v, &shvhed) 986#define varval(v) value1(v, &shvhed) 987 988/* 989 * The following are for interfacing redo substitution in 990 * aliases to the lexical routines. 991 */ 992EXTERN struct wordent *alhistp IZERO_STRUCT;/* Argument list (first) */ 993EXTERN struct wordent *alhistt IZERO_STRUCT;/* Node after last in arg list */ 994EXTERN Char **alvec IZERO_STRUCT, 995 *alvecp IZERO_STRUCT;/* The (remnants of) alias vector */ 996 997/* 998 * Filename/command name expansion variables 999 */ 1000EXTERN int gflag; /* After tglob -> is globbing needed? */ 1001 1002#define MAXVARLEN 30 /* Maximum number of char in a variable name */ 1003 1004#ifndef MAXPATHLEN 1005# define MAXPATHLEN 2048 1006#endif /* MAXPATHLEN */ 1007 1008#ifndef MAXNAMLEN 1009# define MAXNAMLEN 512 1010#endif /* MAXNAMLEN */ 1011 1012#ifndef HAVENOLIMIT 1013/* 1014 * resource limits 1015 */ 1016extern struct limits { 1017 int limconst; 1018 char *limname; 1019 int limdiv; 1020 char *limscale; 1021} limits[]; 1022#endif /* !HAVENOLIMIT */ 1023 1024/* 1025 * Variables for filename expansion 1026 */ 1027extern Char **gargv; /* Pointer to the (stack) arglist */ 1028extern int gargc; /* Number args in gargv */ 1029 1030/* 1031 * Variables for command expansion. 1032 */ 1033extern Char **pargv; /* Pointer to the argv list space */ 1034EXTERN Char *pargs; /* Pointer to start current word */ 1035EXTERN long pnleft; /* Number of chars left in pargs */ 1036EXTERN Char *pargcp; /* Current index into pargs */ 1037 1038/* 1039 * History list 1040 * 1041 * Each history list entry contains an embedded wordlist 1042 * from the scanner, a number for the event, and a reference count 1043 * to aid in discarding old entries. 1044 * 1045 * Essentially "invisible" entries are put on the history list 1046 * when history substitution includes modifiers, and thrown away 1047 * at the next discarding since their event numbers are very negative. 1048 */ 1049EXTERN struct Hist { 1050 struct wordent Hlex; 1051 int Hnum; 1052 int Href; 1053 time_t Htime; 1054 Char *histline; 1055 struct Hist *Hnext; 1056} Histlist IZERO_STRUCT; 1057 1058EXTERN struct wordent paraml; /* Current lexical word list */ 1059EXTERN int eventno; /* Next events number */ 1060EXTERN int lastev; /* Last event reference (default) */ 1061 1062EXTERN Char HIST; /* history invocation character */ 1063EXTERN Char HISTSUB; /* auto-substitute character */ 1064EXTERN Char PRCH; /* Prompt symbol for regular users */ 1065EXTERN Char PRCHROOT; /* Prompt symbol for root */ 1066 1067/* 1068 * For operating systems with single case filenames (OS/2) 1069 */ 1070#ifdef CASE_INSENSITIVE 1071# define samecase(x) (isupper((unsigned char)(x)) ? \ 1072 tolower((unsigned char)(x)) : (x)) 1073#else 1074# define samecase(x) (x) 1075#endif /* CASE_INSENSITIVE */ 1076 1077/* 1078 * strings.h: 1079 */ 1080#ifndef SHORT_STRINGS 1081#define Strchr(a, b) strchr(a, b) 1082#define Strrchr(a, b) strrchr(a, b) 1083#define Strcat(a, b) strcat(a, b) 1084#define Strncat(a, b, c) strncat(a, b, c) 1085#define Strcpy(a, b) strcpy(a, b) 1086#define Strncpy(a, b, c) strncpy(a, b, c) 1087#define Strlen(a) strlen(a) 1088#define Strcmp(a, b) strcmp(a, b) 1089#define Strncmp(a, b, c) strncmp(a, b, c) 1090 1091#define Strspl(a, b) strspl(a, b) 1092#define Strsave(a) strsave(a) 1093#define Strend(a) strend(a) 1094#define Strstr(a, b) strstr(a, b) 1095 1096#define str2short(a) (a) 1097#define blk2short(a) saveblk(a) 1098#define short2blk(a) saveblk(a) 1099#define short2str(a) strip(a) 1100#else 1101#define Strchr(a, b) s_strchr(a, b) 1102#define Strrchr(a, b) s_strrchr(a, b) 1103#define Strcat(a, b) s_strcat(a, b) 1104#define Strncat(a, b, c) s_strncat(a, b, c) 1105#define Strcpy(a, b) s_strcpy(a, b) 1106#define Strncpy(a, b, c) s_strncpy(a, b, c) 1107#define Strlen(a) s_strlen(a) 1108#define Strcmp(a, b) s_strcmp(a, b) 1109#define Strncmp(a, b, c) s_strncmp(a, b, c) 1110 1111#define Strspl(a, b) s_strspl(a, b) 1112#define Strsave(a) s_strsave(a) 1113#define Strend(a) s_strend(a) 1114#define Strstr(a, b) s_strstr(a, b) 1115#endif 1116 1117/* 1118 * setname is a macro to save space (see sh.err.c) 1119 */ 1120EXTERN char *bname; 1121 1122#define setname(a) (bname = (a)) 1123 1124#ifdef VFORK 1125EXTERN Char *Vsav; 1126EXTERN Char *Vdp; 1127EXTERN Char *Vexpath; 1128EXTERN char **Vt; 1129#endif /* VFORK */ 1130 1131EXTERN Char **evalvec; 1132EXTERN Char *evalp; 1133 1134extern struct mesg { 1135 char *iname; /* name from /usr/include */ 1136 char *pname; /* print name */ 1137} mesg[]; 1138 1139/* word_chars is set by default to WORD_CHARS but can be overridden by 1140 the worchars variable--if unset, reverts to WORD_CHARS */ 1141 1142EXTERN Char *word_chars; 1143 1144#define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */ 1145 1146EXTERN Char *STR_SHELLPATH; 1147 1148#ifdef _PATH_BSHELL 1149EXTERN Char *STR_BSHELL; 1150#endif 1151EXTERN Char *STR_WORD_CHARS; 1152EXTERN Char **STR_environ IZERO; 1153 1154extern int dont_free; /* Tell free that we are in danger if we free */ 1155 1156extern Char *INVPTR; 1157extern Char **INVPPTR; 1158 1159extern char *progname; 1160extern int tcsh; 1161 1162#include "tc.h" 1163#include "sh.decls.h" 1164 1165/* 1166 * To print system call errors... 1167 */ 1168#ifdef BSD4_4 1169# include <errno.h> 1170#else 1171# ifndef linux 1172# ifdef NEEDstrerror 1173extern char *sys_errlist[]; 1174# endif 1175extern int errno, sys_nerr; 1176# endif /* !linux */ 1177#endif 1178 1179#ifndef WINNT 1180# ifdef NLS_CATALOGS 1181# ifdef linux 1182# include <locale.h> 1183# ifdef notdef 1184# include <localeinfo.h> /* Has this changed ? */ 1185# endif 1186# include <features.h> 1187# endif 1188# ifdef SUNOS4 1189 /* Who stole my nl_types.h? :-( 1190 * All this stuff is in the man pages, but nowhere else? 1191 * This does not link right now... 1192 */ 1193 typedef void *nl_catd; 1194 extern const char * catgets __P((nl_catd, int, int, const char *)); 1195 nl_catd catopen __P((const char *, int)); 1196 int catclose __P((nl_catd)); 1197# else 1198# ifdef __uxps__ 1199# define gettxt gettxt_ds 1200# endif 1201# include <nl_types.h> 1202# ifdef __uxps__ 1203# undef gettxt 1204# endif 1205# endif 1206# ifndef MCLoadBySet 1207# define MCLoadBySet 0 1208# endif 1209EXTERN nl_catd catd; 1210# define CGETS(b, c, d) catgets(catd, b, c, d) 1211# define CSAVS(b, c, d) strsave(CGETS(b, c, d)) 1212# else 1213# define CGETS(b, c, d) d 1214# define CSAVS(b, c, d) d 1215# endif 1216#else /* WINNT */ 1217# define CGETS(b, c, d) nt_cgets( b, c, d) 1218# define CSAVS(b, c, d) strsave(CGETS(b, c, d)) 1219#endif /* WINNT */ 1220 1221/* 1222 * Since on some machines characters are unsigned, and the signed 1223 * keyword is not universally implemented, we treat all characters 1224 * as unsigned and sign extend them where we need. 1225 */ 1226#define SIGN_EXTEND_CHAR(a) (((a) & 0x80) ? ((a) | ~0x7f) : (a)) 1227 1228#endif /* _h_sh */ 1229