1 /* $Header: /src/pub/tcsh/sh.h,v 3.88 2000/06/10 22:06:27 kim 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 79 typedef short Char; 80 typedef unsigned short uChar; 81 # define SAVE(a) (Strsave(str2short(a))) 82 #else 83 typedef char Char; 84 typedef 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__) */ 101 typedef void sigret_t; 102 # endif /* (vax || uts || MULTIFLOW || PCC) && !__GNUC__ */ 103 #else /* !SIGVOID */ 104 typedef 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 < 4096 125 # undef BUFSIZE 126 # define BUFSIZE 4096 /* buffer size should be no less than this */ 127 # endif 128 #else 129 # define BUFSIZE 4096 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) && !defined(IRIS4D) 328 # include <sys/file.h> 329 #endif /* !_MINIX && !COHERENT && !supermax && !WINNT && !defined(IRIS4D) */ 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 425 typedef 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__) */ 437 typedef void pret_t; 438 # endif /* (vax || uts || MULTIFLOW || PCC) && !__GNUC__ */ 439 #endif /* PURIFY */ 440 441 typedef int bool; 442 443 #include "sh.types.h" 444 445 #ifndef WINNT 446 # ifndef POSIX 447 extern pid_t getpgrp __P((int)); 448 # else /* POSIX */ 449 # if (defined(BSD) && !defined(BSD4_4)) || defined(SUNOS4) || defined(IRIS4D) || defined(DGUX) 450 extern pid_t getpgrp __P((int)); 451 # else /* !(BSD || SUNOS4 || IRIS4D || DGUX) */ 452 extern pid_t getpgrp __P((void)); 453 # endif /* BSD || SUNOS4 || IRISD || DGUX */ 454 # endif /* POSIX */ 455 extern pid_t setpgrp __P((pid_t, pid_t)); 456 #endif /* !WINNT */ 457 458 typedef sigret_t (*signalfun_t) __P((int)); 459 460 #ifndef lint 461 typedef ptr_t memalign_t; 462 #else 463 typedef 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 479 extern memalign_t DebugMalloc __P((unsigned, char *, int)); 480 extern memalign_t DebugRealloc __P((ptr_t, unsigned, char *, int)); 481 extern memalign_t DebugCalloc __P((unsigned, unsigned, char *, int)); 482 extern 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 */ 551 EXTERN 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 556 EXTERN bool didcch IZERO; /* Have closed unused fd's for child */ 557 #endif /* (FIOCLEX && FIONCLEX) || F_SETFD */ 558 559 EXTERN bool didfds IZERO; /* Have setup i/o fd's for child */ 560 EXTERN bool doneinp IZERO; /* EOF indicator after reset from readc */ 561 EXTERN bool exiterr IZERO; /* Exit if error or non-zero exit status */ 562 EXTERN bool child IZERO; /* Child shell ... errors cause exit */ 563 EXTERN bool haderr IZERO; /* Reset was because of an error */ 564 EXTERN bool intty IZERO; /* Input is a tty */ 565 EXTERN bool intact IZERO; /* We are interactive... therefore prompt */ 566 EXTERN bool justpr IZERO; /* Just print because of :p hist mod */ 567 EXTERN bool loginsh IZERO; /* We are a loginsh -> .login/.logout */ 568 EXTERN bool neednote IZERO; /* Need to pnotify() */ 569 EXTERN bool noexec IZERO; /* Don't execute, just syntax check */ 570 EXTERN bool pjobs IZERO; /* want to print jobs if interrupted */ 571 EXTERN bool setintr IZERO; /* Set interrupts on/off -> Wait intr... */ 572 EXTERN bool timflg IZERO; /* Time the next waited for command */ 573 EXTERN bool havhash IZERO; /* path hashing is available */ 574 EXTERN bool editing IZERO; /* doing filename expansion and line editing */ 575 EXTERN bool noediting IZERO; /* initial $term defaulted to noedit */ 576 EXTERN bool bslash_quote IZERO;/* PWP: tcsh-style quoting? (in sh.c) */ 577 EXTERN bool isoutatty IZERO; /* is SHOUT a tty */ 578 EXTERN bool isdiagatty IZERO;/* is SHDIAG a tty */ 579 EXTERN bool is1atty IZERO; /* is file descriptor 1 a tty (didfds mode) */ 580 EXTERN bool is2atty IZERO; /* is file descriptor 2 a tty (didfds mode) */ 581 EXTERN bool arun IZERO; /* Currently running multi-line-aliases */ 582 EXTERN int implicit_cd IZERO;/* implicit cd enabled?(1=enabled,2=verbose) */ 583 EXTERN bool inheredoc IZERO; /* Currently parsing a heredoc */ 584 585 /* 586 * Global i/o info 587 */ 588 EXTERN Char *arginp IZERO; /* Argument input for sh -c and internal `xx` */ 589 EXTERN int onelflg IZERO; /* 2 -> need line for -t, 1 -> exit on read */ 590 extern Char *ffile; /* Name of shell file for $0 */ 591 extern bool dolzero; /* if $?0 should return true... */ 592 593 extern char *seterr; /* Error message from scanner/parser */ 594 extern int errno; /* Error from C library routines */ 595 EXTERN Char *shtemp IZERO; /* Temp name for << shell files in /tmp */ 596 597 #ifdef BSDTIMES 598 EXTERN struct timeval time0; /* Time at which the shell started */ 599 EXTERN struct sysrusage ru0; 600 #else 601 # ifdef _SEQUENT_ 602 EXTERN timeval_t time0; /* time at which shell started */ 603 EXTERN struct process_stats ru0; 604 # else /* _SEQUENT_ */ 605 # ifndef POSIX 606 EXTERN time_t time0; /* time at which shell started */ 607 # else /* POSIX */ 608 EXTERN clock_t time0; /* time at which shell started */ 609 EXTERN clock_t clk_tck; 610 # endif /* POSIX */ 611 EXTERN struct tms shtimes; /* shell and child times for process timing */ 612 # endif /* _SEQUENT_ */ 613 EXTERN long seconds0; 614 #endif /* BSDTIMES */ 615 616 #ifndef HZ 617 # define HZ 100 /* for division into seconds */ 618 #endif 619 620 /* 621 * Miscellany 622 */ 623 EXTERN Char *doldol; /* Character pid for $$ */ 624 EXTERN int backpid; /* pid of the last background job */ 625 626 /* 627 * Ideally these should be uid_t, gid_t, pid_t. I cannot do that right now 628 * cause pid's could be unsigned and that would break our -1 flag, and 629 * uid_t and gid_t are not defined in all the systems so I would have to 630 * make special cases for them. In the future... 631 */ 632 EXTERN int uid, euid, /* Invokers real and effective */ 633 gid, egid; /* User and group ids */ 634 EXTERN int opgrp, /* Initial pgrp and tty pgrp */ 635 shpgrp, /* Pgrp of shell */ 636 tpgrp; /* Terminal process group */ 637 /* If tpgrp is -1, leave tty alone! */ 638 639 EXTERN Char PromptBuf[INBUFSIZE*2]; /* buffer for the actual printed prompt. 640 * this must be large enough to contain 641 * the input line and the prompt, in 642 * case a correction occurred... 643 */ 644 EXTERN Char RPromptBuf[INBUFSIZE]; /* buffer for right-hand side prompt */ 645 646 /* 647 * To be able to redirect i/o for builtins easily, the shell moves the i/o 648 * descriptors it uses away from 0,1,2. 649 * Ideally these should be in units which are closed across exec's 650 * (this saves work) but for version 6, this is not usually possible. 651 * The desired initial values for these descriptors are defined in 652 * sh.local.h. 653 */ 654 EXTERN int SHIN IZERO; /* Current shell input (script) */ 655 EXTERN int SHOUT IZERO; /* Shell output */ 656 EXTERN int SHDIAG IZERO; /* Diagnostic output... shell errs go here */ 657 EXTERN int OLDSTD IZERO; /* Old standard input (def for cmds) */ 658 659 660 #if SYSVREL == 4 && defined(_UTS) 661 /* 662 * From: fadden@uts.amdahl.com (Andy McFadden) 663 * we need sigsetjmp for UTS4, but not UTS2.1 664 */ 665 # define SIGSETJMP 666 #endif 667 668 /* 669 * Error control 670 * 671 * Errors in scanning and parsing set up an error message to be printed 672 * at the end and complete. Other errors always cause a reset. 673 * Because of source commands and .cshrc we need nested error catches. 674 */ 675 676 #ifdef NO_STRUCT_ASSIGNMENT 677 678 # ifdef SIGSETJMP 679 typedef sigjmp_buf jmp_buf_t; 680 /* bugfix by Jak Kirman @ Brown U.: remove the (void) cast here, see sh.c */ 681 # define setexit() sigsetjmp(reslab) 682 # define reset() siglongjmp(reslab, 1) 683 # else 684 typedef jmp_buf jmp_buf_t; 685 /* bugfix by Jak Kirman @ Brown U.: remove the (void) cast here, see sh.c */ 686 # define setexit() setjmp(reslab) 687 # define reset() longjmp(reslab, 1) 688 # endif 689 # define getexit(a) (void) memmove((ptr_t)&(a), (ptr_t)&reslab, sizeof(reslab)) 690 # define resexit(a) (void) memmove((ptr_t)&reslab, (ptr_t)&(a), sizeof(reslab)) 691 692 # define cpybin(a, b) (void) memmove((ptr_t)&(a), (ptr_t)&(b), sizeof(Bin)) 693 694 #else 695 696 # ifdef SIGSETJMP 697 typedef struct { sigjmp_buf j; } jmp_buf_t; 698 # define setexit() sigsetjmp(reslab.j) 699 # define reset() siglongjmp(reslab.j, 1) 700 # else 701 typedef struct { jmp_buf j; } jmp_buf_t; 702 # define setexit() setjmp(reslab.j) 703 # define reset() longjmp(reslab.j, 1) 704 # endif 705 706 # define getexit(a) (void) ((a) = reslab) 707 # define resexit(a) (void) (reslab = (a)) 708 709 # define cpybin(a, b) (void) ((a) = (b)) 710 711 #endif /* NO_STRUCT_ASSIGNMENT */ 712 713 extern jmp_buf_t reslab; 714 715 EXTERN Char *gointr; /* Label for an onintr transfer */ 716 717 extern signalfun_t parintr; /* Parents interrupt catch */ 718 extern signalfun_t parterm; /* Parents terminate catch */ 719 720 /* 721 * Lexical definitions. 722 * 723 * All lexical space is allocated dynamically. 724 * The eighth/sixteenth bit of characters is used to prevent recognition, 725 * and eventually stripped. 726 */ 727 #define META 0200 728 #define ASCII 0177 729 #ifdef SHORT_STRINGS 730 # define QUOTE ((Char) 0100000)/* 16nth char bit used for 'ing */ 731 # define TRIM 0077777 /* Mask to strip quote bit */ 732 # define UNDER 0040000 /* Underline flag */ 733 # define BOLD 0020000 /* Bold flag */ 734 # define STANDOUT 0010000 /* Standout flag */ 735 # define LITERAL 0004000 /* Literal character flag */ 736 # define ATTRIBUTES 0074000 /* The bits used for attributes */ 737 # define CHAR 0000377 /* Mask to mask out the character */ 738 #else 739 # define QUOTE ((Char) 0200) /* Eighth char bit used for 'ing */ 740 # define TRIM 0177 /* Mask to strip quote bit */ 741 # define UNDER 0000000 /* No extra bits to do both */ 742 # define BOLD 0000000 /* Bold flag */ 743 # define STANDOUT META /* Standout flag */ 744 # define LITERAL 0000000 /* Literal character flag */ 745 # define ATTRIBUTES 0200 /* The bits used for attributes */ 746 # define CHAR 0000177 /* Mask to mask out the character */ 747 #endif 748 749 EXTERN int AsciiOnly; /* If set only 7 bits expected in characters */ 750 751 /* 752 * Each level of input has a buffered input structure. 753 * There are one or more blocks of buffered input for each level, 754 * exactly one if the input is seekable and tell is available. 755 * In other cases, the shell buffers enough blocks to keep all loops 756 * in the buffer. 757 */ 758 EXTERN struct Bin { 759 off_t Bfseekp; /* Seek pointer */ 760 off_t Bfbobp; /* Seekp of beginning of buffers */ 761 off_t Bfeobp; /* Seekp of end of buffers */ 762 int Bfblocks; /* Number of buffer blocks */ 763 Char **Bfbuf; /* The array of buffer blocks */ 764 } B; 765 766 /* 767 * This structure allows us to seek inside aliases 768 */ 769 struct Ain { 770 int type; 771 #define I_SEEK -1 /* Invalid seek */ 772 #define A_SEEK 0 /* Alias seek */ 773 #define F_SEEK 1 /* File seek */ 774 #define E_SEEK 2 /* Eval seek */ 775 union { 776 off_t _f_seek; 777 Char* _c_seek; 778 } fc; 779 #define f_seek fc._f_seek 780 #define c_seek fc._c_seek 781 Char **a_seek; 782 } ; 783 784 extern int aret; /* Type of last char returned */ 785 #define SEEKEQ(a, b) ((a)->type == (b)->type && \ 786 (a)->f_seek == (b)->f_seek && \ 787 (a)->a_seek == (b)->a_seek) 788 789 #define fseekp B.Bfseekp 790 #define fbobp B.Bfbobp 791 #define feobp B.Bfeobp 792 #define fblocks B.Bfblocks 793 #define fbuf B.Bfbuf 794 795 /* 796 * The shell finds commands in loops by reseeking the input 797 * For whiles, in particular, it reseeks to the beginning of the 798 * line the while was on; hence the while placement restrictions. 799 */ 800 EXTERN struct Ain lineloc; 801 802 EXTERN bool cantell; /* Is current source tellable ? */ 803 804 /* 805 * Input lines are parsed into doubly linked circular 806 * lists of words of the following form. 807 */ 808 struct wordent { 809 Char *word; 810 struct wordent *prev; 811 struct wordent *next; 812 }; 813 814 /* 815 * During word building, both in the initial lexical phase and 816 * when expanding $ variable substitutions, expansion by `!' and `$' 817 * must be inhibited when reading ahead in routines which are themselves 818 * processing `!' and `$' expansion or after characters such as `\' or in 819 * quotations. The following flags are passed to the getC routines 820 * telling them which of these substitutions are appropriate for the 821 * next character to be returned. 822 */ 823 #define DODOL 1 824 #define DOEXCL 2 825 #define DOALL DODOL|DOEXCL 826 827 /* 828 * Labuf implements a general buffer for lookahead during lexical operations. 829 * Text which is to be placed in the input stream can be stuck here. 830 * We stick parsed ahead $ constructs during initial input, 831 * process id's from `$$', and modified variable values (from qualifiers 832 * during expansion in sh.dol.c) here. 833 */ 834 EXTERN Char *lap; 835 836 /* 837 * Parser structure 838 * 839 * Each command is parsed to a tree of command structures and 840 * flags are set bottom up during this process, to be propagated down 841 * as needed during the semantics/exeuction pass (sh.sem.c). 842 */ 843 struct command { 844 unsigned char t_dtyp; /* Type of node */ 845 #define NODE_COMMAND 1 /* t_dcom <t_dlef >t_drit */ 846 #define NODE_PAREN 2 /* ( t_dspr ) <t_dlef >t_drit */ 847 #define NODE_PIPE 3 /* t_dlef | t_drit */ 848 #define NODE_LIST 4 /* t_dlef ; t_drit */ 849 #define NODE_OR 5 /* t_dlef || t_drit */ 850 #define NODE_AND 6 /* t_dlef && t_drit */ 851 unsigned char t_nice; /* Nice value */ 852 #ifdef apollo 853 unsigned char t_systype; /* System environment */ 854 #endif 855 unsigned long t_dflg; /* Flags, e.g. F_AMPERSAND|... */ 856 /* save these when re-doing */ 857 #ifndef apollo 858 #define F_SAVE (F_NICE|F_TIME|F_NOHUP|F_HUP) 859 #else 860 #define F_SAVE (F_NICE|F_TIME|F_NOHUP||F_HUP|F_VER) 861 #endif 862 #define F_AMPERSAND (1<<0) /* executes in background */ 863 #define F_APPEND (1<<1) /* output is redirected >> */ 864 #define F_PIPEIN (1<<2) /* input is a pipe */ 865 #define F_PIPEOUT (1<<3) /* output is a pipe */ 866 #define F_NOFORK (1<<4) /* don't fork, last ()ized cmd */ 867 #define F_NOINTERRUPT (1<<5) /* should be immune from intr's */ 868 /* spare */ 869 #define F_STDERR (1<<7) /* redirect unit 2 with unit 1 */ 870 #define F_OVERWRITE (1<<8) /* output was ! */ 871 #define F_READ (1<<9) /* input redirection is << */ 872 #define F_REPEAT (1<<10) /* reexec aft if, repeat,... */ 873 #define F_NICE (1<<11) /* t_nice is meaningful */ 874 #define F_NOHUP (1<<12) /* nohup this command */ 875 #define F_TIME (1<<13) /* time this command */ 876 #define F_BACKQ (1<<14) /* command is in `` */ 877 #define F_HUP (1<<15) /* hup this command */ 878 #ifdef apollo 879 #define F_VER (1<<16) /* execute command under SYSTYPE */ 880 #endif 881 union { 882 Char *T_dlef; /* Input redirect word */ 883 struct command *T_dcar; /* Left part of list/pipe */ 884 } L; 885 union { 886 Char *T_drit; /* Output redirect word */ 887 struct command *T_dcdr; /* Right part of list/pipe */ 888 } R; 889 #define t_dlef L.T_dlef 890 #define t_dcar L.T_dcar 891 #define t_drit R.T_drit 892 #define t_dcdr R.T_dcdr 893 Char **t_dcom; /* Command/argument vector */ 894 struct command *t_dspr; /* Pointer to ()'d subtree */ 895 }; 896 897 898 /* 899 * The keywords for the parser 900 */ 901 #define TC_BREAK 0 902 #define TC_BRKSW 1 903 #define TC_CASE 2 904 #define TC_DEFAULT 3 905 #define TC_ELSE 4 906 #define TC_END 5 907 #define TC_ENDIF 6 908 #define TC_ENDSW 7 909 #define TC_EXIT 8 910 #define TC_FOREACH 9 911 #define TC_GOTO 10 912 #define TC_IF 11 913 #define TC_LABEL 12 914 #define TC_LET 13 915 #define TC_SET 14 916 #define TC_SWITCH 15 917 #define TC_TEST 16 918 #define TC_THEN 17 919 #define TC_WHILE 18 920 921 /* 922 * These are declared here because they want to be 923 * initialized in sh.init.c (to allow them to be made readonly) 924 */ 925 926 #if defined(hpux) && defined(__STDC__) && !defined(__GNUC__) 927 /* Avoid hpux ansi mode spurious warnings */ 928 typedef void (*bfunc_t) (); 929 #else 930 typedef void (*bfunc_t) __P((Char **, struct command *)); 931 #endif /* hpux && __STDC__ && !__GNUC__ */ 932 933 extern struct biltins { 934 char *bname; 935 bfunc_t bfunct; 936 int minargs, maxargs; 937 } bfunc[]; 938 extern int nbfunc; 939 #ifdef WINNT 940 extern struct biltins nt_bfunc[]; 941 extern int nt_nbfunc; 942 #endif /* WINNT*/ 943 944 extern struct srch { 945 char *s_name; 946 int s_value; 947 } srchn[]; 948 extern int nsrchn; 949 950 /* 951 * Structure defining the existing while/foreach loops at this 952 * source level. Loops are implemented by seeking back in the 953 * input. For foreach (fe), the word list is attached here. 954 */ 955 EXTERN struct whyle { 956 struct Ain w_start; /* Point to restart loop */ 957 struct Ain w_end; /* End of loop (0 if unknown) */ 958 Char **w_fe, **w_fe0; /* Current/initial wordlist for fe */ 959 Char *w_fename; /* Name for fe */ 960 struct whyle *w_next; /* Next (more outer) loop */ 961 } *whyles; 962 963 /* 964 * Variable structure 965 * 966 * Aliases and variables are stored in AVL balanced binary trees. 967 */ 968 EXTERN struct varent { 969 Char **vec; /* Array of words which is the value */ 970 Char *v_name; /* Name of variable/alias */ 971 int v_flags; /* Flags */ 972 #define VAR_ALL -1 973 #define VAR_READONLY 1 974 #define VAR_READWRITE 2 975 #define VAR_NOGLOB 4 976 #define VAR_FIRST 32 977 #define VAR_LAST 64 978 struct varent *v_link[3]; /* The links, see below */ 979 int v_bal; /* Balance factor */ 980 } shvhed IZERO_STRUCT, aliases IZERO_STRUCT; 981 982 #define v_left v_link[0] 983 #define v_right v_link[1] 984 #define v_parent v_link[2] 985 986 #define adrof(v) adrof1(v, &shvhed) 987 #define varval(v) value1(v, &shvhed) 988 989 /* 990 * The following are for interfacing redo substitution in 991 * aliases to the lexical routines. 992 */ 993 EXTERN struct wordent *alhistp IZERO_STRUCT;/* Argument list (first) */ 994 EXTERN struct wordent *alhistt IZERO_STRUCT;/* Node after last in arg list */ 995 EXTERN Char **alvec IZERO_STRUCT, 996 *alvecp IZERO_STRUCT;/* The (remnants of) alias vector */ 997 998 /* 999 * Filename/command name expansion variables 1000 */ 1001 EXTERN int gflag; /* After tglob -> is globbing needed? */ 1002 1003 #define MAXVARLEN 30 /* Maximum number of char in a variable name */ 1004 1005 #ifndef MAXPATHLEN 1006 # define MAXPATHLEN 2048 1007 #endif /* MAXPATHLEN */ 1008 1009 #ifndef MAXNAMLEN 1010 # define MAXNAMLEN 512 1011 #endif /* MAXNAMLEN */ 1012 1013 #ifndef HAVENOLIMIT 1014 /* 1015 * resource limits 1016 */ 1017 extern struct limits { 1018 int limconst; 1019 char *limname; 1020 int limdiv; 1021 char *limscale; 1022 } limits[]; 1023 #endif /* !HAVENOLIMIT */ 1024 1025 /* 1026 * Variables for filename expansion 1027 */ 1028 extern Char **gargv; /* Pointer to the (stack) arglist */ 1029 extern int gargc; /* Number args in gargv */ 1030 1031 /* 1032 * Variables for command expansion. 1033 */ 1034 extern Char **pargv; /* Pointer to the argv list space */ 1035 EXTERN Char *pargs; /* Pointer to start current word */ 1036 EXTERN long pnleft; /* Number of chars left in pargs */ 1037 EXTERN Char *pargcp; /* Current index into pargs */ 1038 1039 /* 1040 * History list 1041 * 1042 * Each history list entry contains an embedded wordlist 1043 * from the scanner, a number for the event, and a reference count 1044 * to aid in discarding old entries. 1045 * 1046 * Essentially "invisible" entries are put on the history list 1047 * when history substitution includes modifiers, and thrown away 1048 * at the next discarding since their event numbers are very negative. 1049 */ 1050 EXTERN struct Hist { 1051 struct wordent Hlex; 1052 int Hnum; 1053 int Href; 1054 time_t Htime; 1055 Char *histline; 1056 struct Hist *Hnext; 1057 } Histlist IZERO_STRUCT; 1058 1059 EXTERN struct wordent paraml; /* Current lexical word list */ 1060 EXTERN int eventno; /* Next events number */ 1061 EXTERN int lastev; /* Last event reference (default) */ 1062 1063 EXTERN Char HIST; /* history invocation character */ 1064 EXTERN Char HISTSUB; /* auto-substitute character */ 1065 EXTERN Char PRCH; /* Prompt symbol for regular users */ 1066 EXTERN Char PRCHROOT; /* Prompt symbol for root */ 1067 1068 /* 1069 * For operating systems with single case filenames (OS/2) 1070 */ 1071 #ifdef CASE_INSENSITIVE 1072 # define samecase(x) (isupper((unsigned char)(x)) ? \ 1073 tolower((unsigned char)(x)) : (x)) 1074 #else 1075 # define samecase(x) (x) 1076 #endif /* CASE_INSENSITIVE */ 1077 1078 /* 1079 * strings.h: 1080 */ 1081 #ifndef SHORT_STRINGS 1082 #define Strchr(a, b) strchr(a, b) 1083 #define Strrchr(a, b) strrchr(a, b) 1084 #define Strcat(a, b) strcat(a, b) 1085 #define Strncat(a, b, c) strncat(a, b, c) 1086 #define Strcpy(a, b) strcpy(a, b) 1087 #define Strncpy(a, b, c) strncpy(a, b, c) 1088 #define Strlen(a) strlen(a) 1089 #define Strcmp(a, b) strcmp(a, b) 1090 #define Strncmp(a, b, c) strncmp(a, b, c) 1091 1092 #define Strspl(a, b) strspl(a, b) 1093 #define Strsave(a) strsave(a) 1094 #define Strend(a) strend(a) 1095 #define Strstr(a, b) strstr(a, b) 1096 1097 #define str2short(a) (a) 1098 #define blk2short(a) saveblk(a) 1099 #define short2blk(a) saveblk(a) 1100 #define short2str(a) strip(a) 1101 #else 1102 #define Strchr(a, b) s_strchr(a, b) 1103 #define Strrchr(a, b) s_strrchr(a, b) 1104 #define Strcat(a, b) s_strcat(a, b) 1105 #define Strncat(a, b, c) s_strncat(a, b, c) 1106 #define Strcpy(a, b) s_strcpy(a, b) 1107 #define Strncpy(a, b, c) s_strncpy(a, b, c) 1108 #define Strlen(a) s_strlen(a) 1109 #define Strcmp(a, b) s_strcmp(a, b) 1110 #define Strncmp(a, b, c) s_strncmp(a, b, c) 1111 1112 #define Strspl(a, b) s_strspl(a, b) 1113 #define Strsave(a) s_strsave(a) 1114 #define Strend(a) s_strend(a) 1115 #define Strstr(a, b) s_strstr(a, b) 1116 #endif 1117 1118 /* 1119 * setname is a macro to save space (see sh.err.c) 1120 */ 1121 EXTERN char *bname; 1122 1123 #define setname(a) (bname = (a)) 1124 1125 #ifdef VFORK 1126 EXTERN Char *Vsav; 1127 EXTERN Char *Vdp; 1128 EXTERN Char *Vexpath; 1129 EXTERN char **Vt; 1130 #endif /* VFORK */ 1131 1132 EXTERN Char **evalvec; 1133 EXTERN Char *evalp; 1134 1135 extern struct mesg { 1136 char *iname; /* name from /usr/include */ 1137 char *pname; /* print name */ 1138 } mesg[]; 1139 1140 /* word_chars is set by default to WORD_CHARS but can be overridden by 1141 the worchars variable--if unset, reverts to WORD_CHARS */ 1142 1143 EXTERN Char *word_chars; 1144 1145 #define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */ 1146 1147 EXTERN Char *STR_SHELLPATH; 1148 1149 #ifdef _PATH_BSHELL 1150 EXTERN Char *STR_BSHELL; 1151 #endif 1152 EXTERN Char *STR_WORD_CHARS; 1153 EXTERN Char **STR_environ IZERO; 1154 1155 extern int dont_free; /* Tell free that we are in danger if we free */ 1156 1157 extern Char *INVPTR; 1158 extern Char **INVPPTR; 1159 1160 extern char *progname; 1161 extern int tcsh; 1162 1163 #include "tc.h" 1164 #include "sh.decls.h" 1165 1166 /* 1167 * To print system call errors... 1168 */ 1169 #ifdef BSD4_4 1170 # include <errno.h> 1171 #else 1172 # ifndef linux 1173 # ifdef NEEDstrerror 1174 extern char *sys_errlist[]; 1175 # endif 1176 extern int errno, sys_nerr; 1177 # endif /* !linux */ 1178 #endif 1179 1180 #ifndef WINNT 1181 # ifdef NLS_CATALOGS 1182 # ifdef linux 1183 # include <locale.h> 1184 # ifdef notdef 1185 # include <localeinfo.h> /* Has this changed ? */ 1186 # endif 1187 # include <features.h> 1188 # endif 1189 # ifdef SUNOS4 1190 /* Who stole my nl_types.h? :-( 1191 * All this stuff is in the man pages, but nowhere else? 1192 * This does not link right now... 1193 */ 1194 typedef void *nl_catd; 1195 extern const char * catgets __P((nl_catd, int, int, const char *)); 1196 nl_catd catopen __P((const char *, int)); 1197 int catclose __P((nl_catd)); 1198 # else 1199 # ifdef __uxps__ 1200 # define gettxt gettxt_ds 1201 # endif 1202 # include <nl_types.h> 1203 # ifdef __uxps__ 1204 # undef gettxt 1205 # endif 1206 # endif 1207 # ifndef MCLoadBySet 1208 # define MCLoadBySet 0 1209 # endif 1210 EXTERN nl_catd catd; 1211 # define CGETS(b, c, d) catgets(catd, b, c, d) 1212 # define CSAVS(b, c, d) strsave(CGETS(b, c, d)) 1213 # else 1214 # define CGETS(b, c, d) d 1215 # define CSAVS(b, c, d) d 1216 # endif 1217 #else /* WINNT */ 1218 # define CGETS(b, c, d) nt_cgets( b, c, d) 1219 # define CSAVS(b, c, d) strsave(CGETS(b, c, d)) 1220 #endif /* WINNT */ 1221 1222 /* 1223 * Since on some machines characters are unsigned, and the signed 1224 * keyword is not universally implemented, we treat all characters 1225 * as unsigned and sign extend them where we need. 1226 */ 1227 #define SIGN_EXTEND_CHAR(a) (((a) & 0x80) ? ((a) | ~0x7f) : (a)) 1228 1229 #endif /* _h_sh */ 1230