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