17c478bd9Sstevel@tonic-gate /* 26c02b4a4Smuffin * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 77c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 87c478bd9Sstevel@tonic-gate 97c478bd9Sstevel@tonic-gate /* 107c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley Software License Agreement 127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 137c478bd9Sstevel@tonic-gate */ 147c478bd9Sstevel@tonic-gate 157c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 167c478bd9Sstevel@tonic-gate 177c478bd9Sstevel@tonic-gate #include <stdlib.h> /* MB_xxx, mbxxx(), wcxxx() etc. */ 187c478bd9Sstevel@tonic-gate #include <limits.h> 197c478bd9Sstevel@tonic-gate #include <sys/time.h> 207c478bd9Sstevel@tonic-gate #include <sys/types.h> 217c478bd9Sstevel@tonic-gate #include <sys/siginfo.h> 227c478bd9Sstevel@tonic-gate #include <sys/ucontext.h> 237c478bd9Sstevel@tonic-gate #include <sys/param.h> 247c478bd9Sstevel@tonic-gate #include <sys/stat.h> 257c478bd9Sstevel@tonic-gate #include <sys/termios.h> 267c478bd9Sstevel@tonic-gate #include <sys/ttold.h> 277c478bd9Sstevel@tonic-gate #include <errno.h> 287c478bd9Sstevel@tonic-gate #include <signal.h> /* std sysV signal.h */ 297c478bd9Sstevel@tonic-gate #include <setjmp.h> 307c478bd9Sstevel@tonic-gate #include <sys/resource.h> 317c478bd9Sstevel@tonic-gate #include "signal.h" /* mainly BSD related signal.h */ 327c478bd9Sstevel@tonic-gate #include "sh.local.h" 337c478bd9Sstevel@tonic-gate #include "sh.char.h" 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate /* 367c478bd9Sstevel@tonic-gate * MAXHOSTNAMELEN is defined in param.h under SunOS 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate #define MAXHOSTNAMELEN 64 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #ifdef MBCHAR 417c478bd9Sstevel@tonic-gate #if !defined(MB_LEN_MAX) || !defined(MB_CUR_MAX) 427c478bd9Sstevel@tonic-gate Error: I need both ANSI macros! 437c478bd9Sstevel@tonic-gate #endif 447c478bd9Sstevel@tonic-gate #else 457c478bd9Sstevel@tonic-gate #if !defined(MB_LEN_MAX) 467c478bd9Sstevel@tonic-gate #define MB_LEN_MAX 1 477c478bd9Sstevel@tonic-gate #endif 487c478bd9Sstevel@tonic-gate #if !defined(MB_CUR_MAX) 497c478bd9Sstevel@tonic-gate #define MB_CUR_MAX 1 507c478bd9Sstevel@tonic-gate #endif 517c478bd9Sstevel@tonic-gate #endif 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #ifndef MBCHAR /* Let's replace the ANSI functions with our own macro 547c478bd9Sstevel@tonic-gate * for efficiency! 557c478bd9Sstevel@tonic-gate */ 567c478bd9Sstevel@tonic-gate #define mbtowc(pwc, pmb, n_is_ignored) ((*(pwc) = *(pmb)), 1) 577c478bd9Sstevel@tonic-gate #define wctomb(pmb, wc) ((*(pmb) = ((char)wc)), 1) 587c478bd9Sstevel@tonic-gate #endif /* !MBCHAR */ 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * C shell 627c478bd9Sstevel@tonic-gate * 637c478bd9Sstevel@tonic-gate * Bill Joy, UC Berkeley 647c478bd9Sstevel@tonic-gate * October, 1978; May 1980 657c478bd9Sstevel@tonic-gate * 667c478bd9Sstevel@tonic-gate * Jim Kulp, IIASA, Laxenburg Austria 677c478bd9Sstevel@tonic-gate * April, 1980 687c478bd9Sstevel@tonic-gate */ 697c478bd9Sstevel@tonic-gate 70*65b0c20eSnakanon /* 71*65b0c20eSnakanon * If we are setting the $cwd variable becuz we did a 72*65b0c20eSnakanon * cd, chdir, pushd, popd command, then set didchdir to 73*65b0c20eSnakanon * 1. This prevents globbing down when setting $cwd. 74*65b0c20eSnakanon * However, if the user set $cwd, we want the globbing 75*65b0c20eSnakanon * done; so, didchdir would be equal to 0 in that case. 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate int didchdir; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate #define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR) 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate typedef char bool; 827c478bd9Sstevel@tonic-gate 83*65b0c20eSnakanon /* 84*65b0c20eSnakanon * tchar (Tagged CHARacter) is a place holder to keep a QUOTE bit and 857c478bd9Sstevel@tonic-gate * a character. 867c478bd9Sstevel@tonic-gate * For European language handling, lower 8 bits of tchar is used 877c478bd9Sstevel@tonic-gate * to store a character. For other languages, especially Asian, 16 bits 887c478bd9Sstevel@tonic-gate * are used to store a character. 897c478bd9Sstevel@tonic-gate * Following typedef's assume short int is a 16-bit entity and long int is 907c478bd9Sstevel@tonic-gate * a 32-bit entity. 917c478bd9Sstevel@tonic-gate * The QUOTE bit tells whether the character is subject to further 927c478bd9Sstevel@tonic-gate * interpretation such as history substitution, file mathing, command 937c478bd9Sstevel@tonic-gate * subsitution. TRIM is a mask to strip off the QUOTE bit. 947c478bd9Sstevel@tonic-gate */ 957c478bd9Sstevel@tonic-gate #ifdef MBCHAR /* For multibyte character handling. */ 967c478bd9Sstevel@tonic-gate typedef long int tchar; 977c478bd9Sstevel@tonic-gate #define QUOTE 0x80000000 987c478bd9Sstevel@tonic-gate #define TRIM 0x7fffffff 997c478bd9Sstevel@tonic-gate #else /* !MBCHAR */ /* European language requires only 8 bits. */ 1007c478bd9Sstevel@tonic-gate typedef unsigned short int tchar; 1017c478bd9Sstevel@tonic-gate #define QUOTE 0x8000 1027c478bd9Sstevel@tonic-gate #define TRIM 0x00ff 1037c478bd9Sstevel@tonic-gate #endif /* !MBCHAR */ 1047c478bd9Sstevel@tonic-gate #define eq(a, b) (strcmp_(a, b) == 0) 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * Global flags 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate bool chkstop; /* Warned of stopped jobs... allow exit */ 1117c478bd9Sstevel@tonic-gate bool didfds; /* Have setup i/o fd's for child */ 1127c478bd9Sstevel@tonic-gate bool doneinp; /* EOF indicator after reset from readc */ 1137c478bd9Sstevel@tonic-gate bool exiterr; /* Exit if error or non-zero exit status */ 1147c478bd9Sstevel@tonic-gate bool child; /* Child shell ... errors cause exit */ 1157c478bd9Sstevel@tonic-gate bool haderr; /* Reset was because of an error */ 1167c478bd9Sstevel@tonic-gate bool intty; /* Input is a tty */ 1177c478bd9Sstevel@tonic-gate bool cflg; /* invoked with -c option */ 1187c478bd9Sstevel@tonic-gate bool intact; /* We are interactive... therefore prompt */ 1197c478bd9Sstevel@tonic-gate bool justpr; /* Just print because of :p hist mod */ 1207c478bd9Sstevel@tonic-gate bool loginsh; /* We are a loginsh -> .login/.logout */ 1217c478bd9Sstevel@tonic-gate bool neednote; /* Need to pnotify() */ 1227c478bd9Sstevel@tonic-gate bool noexec; /* Don't execute, just syntax check */ 1237c478bd9Sstevel@tonic-gate bool pjobs; /* want to print jobs if interrupted */ 1247c478bd9Sstevel@tonic-gate bool pfcshflag; /* set to 0 for pfcsh */ 1257c478bd9Sstevel@tonic-gate bool setintr; /* Set interrupts on/off -> Wait intr... */ 1267c478bd9Sstevel@tonic-gate bool timflg; /* Time the next waited for command */ 1277c478bd9Sstevel@tonic-gate bool havhash; /* path hashing is available */ 1287c478bd9Sstevel@tonic-gate bool havhash2; /* cdpath hashing is available */ 1297c478bd9Sstevel@tonic-gate #ifdef FILEC 1307c478bd9Sstevel@tonic-gate bool filec; /* doing filename expansion */ 1317c478bd9Sstevel@tonic-gate #endif 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate /* 1347c478bd9Sstevel@tonic-gate * Global i/o info 1357c478bd9Sstevel@tonic-gate */ 1367c478bd9Sstevel@tonic-gate tchar *arginp; /* Argument input for sh -c and internal `xx` */ 1377c478bd9Sstevel@tonic-gate int onelflg; /* 2 -> need line for -t, 1 -> exit on read */ 1387c478bd9Sstevel@tonic-gate tchar *file; /* Name of shell file for $0 */ 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate char *err; /* Error message from scanner/parser */ 1417c478bd9Sstevel@tonic-gate struct timeval time0; /* Time at which the shell started */ 1427c478bd9Sstevel@tonic-gate struct rusage ru0; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * Miscellany 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate tchar *doldol; /* Character pid for $$ */ 1487c478bd9Sstevel@tonic-gate int uid; /* Invokers uid */ 1497c478bd9Sstevel@tonic-gate time_t chktim; /* Time mail last checked */ 1507c478bd9Sstevel@tonic-gate int shpgrp; /* Pgrp of shell */ 1517c478bd9Sstevel@tonic-gate int tpgrp; /* Terminal process group */ 1527c478bd9Sstevel@tonic-gate /* If tpgrp is -1, leave tty alone! */ 1537c478bd9Sstevel@tonic-gate int opgrp; /* Initial pgrp and tty pgrp */ 1547c478bd9Sstevel@tonic-gate int oldisc; /* Initial line discipline or -1 */ 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * These are declared here because they want to be 1587c478bd9Sstevel@tonic-gate * initialized in sh.init.c (to allow them to be made readonly) 1597c478bd9Sstevel@tonic-gate */ 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate extern struct biltins { 1627c478bd9Sstevel@tonic-gate tchar *bname; 1637c478bd9Sstevel@tonic-gate int (*bfunct)(); 1647c478bd9Sstevel@tonic-gate short minargs, maxargs; 1657c478bd9Sstevel@tonic-gate } bfunc[]; 1667c478bd9Sstevel@tonic-gate extern int nbfunc; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate extern struct srch { 1697c478bd9Sstevel@tonic-gate tchar *s_name; 1707c478bd9Sstevel@tonic-gate short s_value; 1717c478bd9Sstevel@tonic-gate } srchn[]; 1727c478bd9Sstevel@tonic-gate extern int nsrchn; 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate /* 1757c478bd9Sstevel@tonic-gate * To be able to redirect i/o for builtins easily, the shell moves the i/o 1767c478bd9Sstevel@tonic-gate * descriptors it uses away from 0,1,2. 1777c478bd9Sstevel@tonic-gate * Ideally these should be in units which are closed across exec's 1787c478bd9Sstevel@tonic-gate * (this saves work) but for version 6, this is not usually possible. 1797c478bd9Sstevel@tonic-gate * The desired initial values for these descriptors are defined in 1807c478bd9Sstevel@tonic-gate * sh.local.h. 1817c478bd9Sstevel@tonic-gate */ 1827c478bd9Sstevel@tonic-gate short SHIN; /* Current shell input (script) */ 1837c478bd9Sstevel@tonic-gate short SHOUT; /* Shell output */ 1847c478bd9Sstevel@tonic-gate short SHDIAG; /* Diagnostic output... shell errs go here */ 1857c478bd9Sstevel@tonic-gate short OLDSTD; /* Old standard input (def for cmds) */ 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate /* 1887c478bd9Sstevel@tonic-gate * Error control 1897c478bd9Sstevel@tonic-gate * 1907c478bd9Sstevel@tonic-gate * Errors in scanning and parsing set up an error message to be printed 1917c478bd9Sstevel@tonic-gate * at the end and complete. Other errors always cause a reset. 1927c478bd9Sstevel@tonic-gate * Because of source commands and .cshrc we need nested error catches. 1937c478bd9Sstevel@tonic-gate */ 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate jmp_buf reslab; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate #define setexit() ((void) setjmp(reslab)) 1987c478bd9Sstevel@tonic-gate #define reset() longjmp(reslab, 0) 1997c478bd9Sstevel@tonic-gate /* Should use structure assignment here */ 2007c478bd9Sstevel@tonic-gate #define getexit(a) copy((void *)(a), (void *)reslab, sizeof reslab) 2017c478bd9Sstevel@tonic-gate #define resexit(a) copy((void *)reslab, ((void *)(a)), sizeof reslab) 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate tchar *gointr; /* Label for an onintr transfer */ 2047c478bd9Sstevel@tonic-gate void (*parintr)(); /* Parents interrupt catch */ 2057c478bd9Sstevel@tonic-gate void (*parterm)(); /* Parents terminate catch */ 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate /* 2097c478bd9Sstevel@tonic-gate * Each level of input has a buffered input structure. 2107c478bd9Sstevel@tonic-gate * There are one or more blocks of buffered input for each level, 2117c478bd9Sstevel@tonic-gate * exactly one if the input is seekable and tell is available. 2127c478bd9Sstevel@tonic-gate * In other cases, the shell buffers enough blocks to keep all loops 2137c478bd9Sstevel@tonic-gate * in the buffer. 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate struct Bin { 2167c478bd9Sstevel@tonic-gate off_t Bfseekp; /* Seek pointer */ 2177c478bd9Sstevel@tonic-gate off_t Bfbobp; /* Seekp of beginning of buffers */ 2187c478bd9Sstevel@tonic-gate off_t Bfeobp; /* Seekp of end of buffers */ 2197c478bd9Sstevel@tonic-gate short Bfblocks; /* Number of buffer blocks */ 2207c478bd9Sstevel@tonic-gate tchar **Bfbuf; /* The array of buffer blocks */ 2217c478bd9Sstevel@tonic-gate } B; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate #define fseekp B.Bfseekp 2247c478bd9Sstevel@tonic-gate #define fbobp B.Bfbobp 2257c478bd9Sstevel@tonic-gate #define feobp B.Bfeobp 2267c478bd9Sstevel@tonic-gate #define fblocks B.Bfblocks 2277c478bd9Sstevel@tonic-gate #define fbuf B.Bfbuf 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate #define btell() fseekp 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate #ifndef btell 2326c02b4a4Smuffin off_t btell(void); 2337c478bd9Sstevel@tonic-gate #endif 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate /* 2367c478bd9Sstevel@tonic-gate * The shell finds commands in loops by reseeking the input 2377c478bd9Sstevel@tonic-gate * For whiles, in particular, it reseeks to the beginning of the 2387c478bd9Sstevel@tonic-gate * line the while was on; hence the while placement restrictions. 2397c478bd9Sstevel@tonic-gate */ 2407c478bd9Sstevel@tonic-gate off_t lineloc; 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate #ifdef TELL 2437c478bd9Sstevel@tonic-gate bool cantell; /* Is current source tellable ? */ 2447c478bd9Sstevel@tonic-gate #endif 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate /* 2477c478bd9Sstevel@tonic-gate * Input lines are parsed into doubly linked circular 2487c478bd9Sstevel@tonic-gate * lists of words of the following form. 2497c478bd9Sstevel@tonic-gate */ 2507c478bd9Sstevel@tonic-gate struct wordent { 2517c478bd9Sstevel@tonic-gate tchar *word; 2527c478bd9Sstevel@tonic-gate struct wordent *prev; 2537c478bd9Sstevel@tonic-gate struct wordent *next; 2547c478bd9Sstevel@tonic-gate }; 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate /* 2577c478bd9Sstevel@tonic-gate * During word building, both in the initial lexical phase and 2587c478bd9Sstevel@tonic-gate * when expanding $ variable substitutions, expansion by `!' and `$' 2597c478bd9Sstevel@tonic-gate * must be inhibited when reading ahead in routines which are themselves 2607c478bd9Sstevel@tonic-gate * processing `!' and `$' expansion or after characters such as `\' or in 2617c478bd9Sstevel@tonic-gate * quotations. The following flags are passed to the getC routines 2627c478bd9Sstevel@tonic-gate * telling them which of these substitutions are appropriate for the 2637c478bd9Sstevel@tonic-gate * next character to be returned. 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate #define DODOL 1 2667c478bd9Sstevel@tonic-gate #define DOEXCL 2 2677c478bd9Sstevel@tonic-gate #define DOALL DODOL|DOEXCL 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* 2707c478bd9Sstevel@tonic-gate * Labuf implements a general buffer for lookahead during lexical operations. 2717c478bd9Sstevel@tonic-gate * Text which is to be placed in the input stream can be stuck here. 2727c478bd9Sstevel@tonic-gate * We stick parsed ahead $ constructs during initial input, 2737c478bd9Sstevel@tonic-gate * process id's from `$$', and modified variable values (from qualifiers 2747c478bd9Sstevel@tonic-gate * during expansion in sh.dol.c) here. 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate tchar *labuf; 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate tchar *lap; 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate /* 2817c478bd9Sstevel@tonic-gate * Parser structure 2827c478bd9Sstevel@tonic-gate * 2837c478bd9Sstevel@tonic-gate * Each command is parsed to a tree of command structures and 2847c478bd9Sstevel@tonic-gate * flags are set bottom up during this process, to be propagated down 2857c478bd9Sstevel@tonic-gate * as needed during the semantics/exeuction pass (sh.sem.c). 2867c478bd9Sstevel@tonic-gate */ 2877c478bd9Sstevel@tonic-gate struct command { 2887c478bd9Sstevel@tonic-gate short t_dtyp; /* Type of node */ 2897c478bd9Sstevel@tonic-gate short t_dflg; /* Flags, e.g. FAND|... */ 2907c478bd9Sstevel@tonic-gate union { 2917c478bd9Sstevel@tonic-gate tchar *T_dlef; /* Input redirect word */ 2927c478bd9Sstevel@tonic-gate struct command *T_dcar; /* Left part of list/pipe */ 2937c478bd9Sstevel@tonic-gate } L; 2947c478bd9Sstevel@tonic-gate union { 2957c478bd9Sstevel@tonic-gate tchar *T_drit; /* Output redirect word */ 2967c478bd9Sstevel@tonic-gate struct command *T_dcdr; /* Right part of list/pipe */ 2977c478bd9Sstevel@tonic-gate } R; 2987c478bd9Sstevel@tonic-gate #define t_dlef L.T_dlef 2997c478bd9Sstevel@tonic-gate #define t_dcar L.T_dcar 3007c478bd9Sstevel@tonic-gate #define t_drit R.T_drit 3017c478bd9Sstevel@tonic-gate #define t_dcdr R.T_dcdr 3027c478bd9Sstevel@tonic-gate tchar **t_dcom; /* Command/argument vector */ 3037c478bd9Sstevel@tonic-gate char *cfname; /* char pathname for execv */ 3047c478bd9Sstevel@tonic-gate char **cargs; /* char arg vec for execv */ 3057c478bd9Sstevel@tonic-gate struct command *t_dspr; /* Pointer to ()'d subtree */ 3067c478bd9Sstevel@tonic-gate short t_nice; 3077c478bd9Sstevel@tonic-gate }; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate #define TCOM 1 /* t_dcom <t_dlef >t_drit */ 3107c478bd9Sstevel@tonic-gate #define TPAR 2 /* ( t_dspr ) <t_dlef >t_drit */ 3117c478bd9Sstevel@tonic-gate #define TFIL 3 /* t_dlef | t_drit */ 3127c478bd9Sstevel@tonic-gate #define TLST 4 /* t_dlef ; t_drit */ 3137c478bd9Sstevel@tonic-gate #define TOR 5 /* t_dlef || t_drit */ 3147c478bd9Sstevel@tonic-gate #define TAND 6 /* t_dlef && t_drit */ 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate #define FSAVE (FNICE|FTIME|FNOHUP) /* save these when re-doing */ 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate #define FAND (1<<0) /* executes in background */ 3197c478bd9Sstevel@tonic-gate #define FCAT (1<<1) /* output is redirected >> */ 3207c478bd9Sstevel@tonic-gate #define FPIN (1<<2) /* input is a pipe */ 3217c478bd9Sstevel@tonic-gate #define FPOU (1<<3) /* output is a pipe */ 3227c478bd9Sstevel@tonic-gate #define FPAR (1<<4) /* don't fork, last ()ized cmd */ 3237c478bd9Sstevel@tonic-gate #define FINT (1<<5) /* should be immune from intr's */ 3247c478bd9Sstevel@tonic-gate /* spare */ 3257c478bd9Sstevel@tonic-gate #define FDIAG (1<<7) /* redirect unit 2 with unit 1 */ 3267c478bd9Sstevel@tonic-gate #define FANY (1<<8) /* output was ! */ 3277c478bd9Sstevel@tonic-gate #define FHERE (1<<9) /* input redirection is << */ 3287c478bd9Sstevel@tonic-gate #define FREDO (1<<10) /* reexec aft if, repeat,... */ 3297c478bd9Sstevel@tonic-gate #define FNICE (1<<11) /* t_nice is meaningful */ 3307c478bd9Sstevel@tonic-gate #define FNOHUP (1<<12) /* nohup this command */ 3317c478bd9Sstevel@tonic-gate #define FTIME (1<<13) /* time this command */ 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * The keywords for the parser 3357c478bd9Sstevel@tonic-gate */ 3367c478bd9Sstevel@tonic-gate #define ZBREAK 0 3377c478bd9Sstevel@tonic-gate #define ZBRKSW 1 3387c478bd9Sstevel@tonic-gate #define ZCASE 2 3397c478bd9Sstevel@tonic-gate #define ZDEFAULT 3 3407c478bd9Sstevel@tonic-gate #define ZELSE 4 3417c478bd9Sstevel@tonic-gate #define ZEND 5 3427c478bd9Sstevel@tonic-gate #define ZENDIF 6 3437c478bd9Sstevel@tonic-gate #define ZENDSW 7 3447c478bd9Sstevel@tonic-gate #define ZEXIT 8 3457c478bd9Sstevel@tonic-gate #define ZFOREACH 9 3467c478bd9Sstevel@tonic-gate #define ZGOTO 10 3477c478bd9Sstevel@tonic-gate #define ZIF 11 3487c478bd9Sstevel@tonic-gate #define ZLABEL 12 3497c478bd9Sstevel@tonic-gate #define ZLET 13 3507c478bd9Sstevel@tonic-gate #define ZSET 14 3517c478bd9Sstevel@tonic-gate #define ZSWITCH 15 3527c478bd9Sstevel@tonic-gate #define ZTEST 16 3537c478bd9Sstevel@tonic-gate #define ZTHEN 17 3547c478bd9Sstevel@tonic-gate #define ZWHILE 18 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate /* 3577c478bd9Sstevel@tonic-gate * Structure defining the existing while/foreach loops at this 3587c478bd9Sstevel@tonic-gate * source level. Loops are implemented by seeking back in the 3597c478bd9Sstevel@tonic-gate * input. For foreach (fe), the word list is attached here. 3607c478bd9Sstevel@tonic-gate */ 3617c478bd9Sstevel@tonic-gate struct whyle { 3627c478bd9Sstevel@tonic-gate off_t w_start; /* Point to restart loop */ 3637c478bd9Sstevel@tonic-gate off_t w_end; /* End of loop (0 if unknown) */ 3647c478bd9Sstevel@tonic-gate tchar **w_fe, **w_fe0; /* Current/initial wordlist for fe */ 3657c478bd9Sstevel@tonic-gate tchar *w_fename; /* Name for fe */ 3667c478bd9Sstevel@tonic-gate struct whyle *w_next; /* Next (more outer) loop */ 3677c478bd9Sstevel@tonic-gate } *whyles; 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate /* 3707c478bd9Sstevel@tonic-gate * Variable structure 3717c478bd9Sstevel@tonic-gate * 3727c478bd9Sstevel@tonic-gate * Aliases and variables are stored in AVL balanced binary trees. 3737c478bd9Sstevel@tonic-gate */ 3747c478bd9Sstevel@tonic-gate struct varent { 3757c478bd9Sstevel@tonic-gate tchar **vec; /* Array of words which is the value */ 3767c478bd9Sstevel@tonic-gate tchar *v_name; /* Name of variable/alias */ 3777c478bd9Sstevel@tonic-gate struct varent *v_link[3]; /* The links, see below */ 3787c478bd9Sstevel@tonic-gate int v_bal; /* Balance factor */ 3797c478bd9Sstevel@tonic-gate } shvhed, aliases; 3807c478bd9Sstevel@tonic-gate #define v_left v_link[0] 3817c478bd9Sstevel@tonic-gate #define v_right v_link[1] 3827c478bd9Sstevel@tonic-gate #define v_parent v_link[2] 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate struct varent *adrof1(); 3857c478bd9Sstevel@tonic-gate #define adrof(v) adrof1(v, &shvhed) 3867c478bd9Sstevel@tonic-gate #define value(v) value1(v, &shvhed) 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate /* 3897c478bd9Sstevel@tonic-gate * MAX_VAR_LEN - maximum variable name defined by csh man page to be 20 3907c478bd9Sstevel@tonic-gate */ 3917c478bd9Sstevel@tonic-gate #define MAX_VAR_LEN 20 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate /* 3947c478bd9Sstevel@tonic-gate * MAX_VREF_LEN - maximum variable reference $name[...] 3957c478bd9Sstevel@tonic-gate * it can be as big as a csh word, which is 1024 3967c478bd9Sstevel@tonic-gate */ 3977c478bd9Sstevel@tonic-gate #define MAX_VREF_LEN 1024 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate /* 4017c478bd9Sstevel@tonic-gate * The following are for interfacing redo substitution in 4027c478bd9Sstevel@tonic-gate * aliases to the lexical routines. 4037c478bd9Sstevel@tonic-gate */ 4047c478bd9Sstevel@tonic-gate struct wordent *alhistp; /* Argument list (first) */ 4057c478bd9Sstevel@tonic-gate struct wordent *alhistt; /* Node after last in arg list */ 4067c478bd9Sstevel@tonic-gate tchar **alvec; /* The (remnants of) alias vector */ 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate /* 4097c478bd9Sstevel@tonic-gate * Filename/command name expansion variables 4107c478bd9Sstevel@tonic-gate */ 4117c478bd9Sstevel@tonic-gate short gflag; /* After tglob -> is globbing needed? */ 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate /* 4147c478bd9Sstevel@tonic-gate * A reasonable limit on number of arguments would seem to be 4157c478bd9Sstevel@tonic-gate * the maximum number of characters in an arg list / 6. 4167c478bd9Sstevel@tonic-gate * 4177c478bd9Sstevel@tonic-gate * XXX: With the new VM system, NCARGS has become enormous, making 4187c478bd9Sstevel@tonic-gate * it impractical to allocate arrays with NCARGS / 6 entries on 4197c478bd9Sstevel@tonic-gate * the stack. The proper fix is to revamp code elsewhere (in 4207c478bd9Sstevel@tonic-gate * sh.dol.c and sh.glob.c) to use a different technique for handling 4217c478bd9Sstevel@tonic-gate * command line arguments. In the meantime, we simply fall back 4227c478bd9Sstevel@tonic-gate * on using the old value of NCARGS. 4237c478bd9Sstevel@tonic-gate */ 4247c478bd9Sstevel@tonic-gate #ifdef notyet 4257c478bd9Sstevel@tonic-gate #define GAVSIZ (NCARGS / 6) 4266c02b4a4Smuffin #else /* notyet */ 4277c478bd9Sstevel@tonic-gate #define GAVSIZ (10240 / 6) 4286c02b4a4Smuffin #endif /* notyet */ 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate /* 4317c478bd9Sstevel@tonic-gate * Variables for filename expansion 4327c478bd9Sstevel@tonic-gate */ 4337c478bd9Sstevel@tonic-gate tchar **gargv; /* Pointer to the (stack) arglist */ 4347c478bd9Sstevel@tonic-gate long gargc; /* Number args in gargv */ 4357c478bd9Sstevel@tonic-gate long gnleft; 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate /* 4387c478bd9Sstevel@tonic-gate * Variables for command expansion. 4397c478bd9Sstevel@tonic-gate */ 4407c478bd9Sstevel@tonic-gate tchar **pargv; /* Pointer to the argv list space */ 4417c478bd9Sstevel@tonic-gate tchar *pargs; /* Pointer to start current word */ 4427c478bd9Sstevel@tonic-gate long pargc; /* Count of arguments in pargv */ 4437c478bd9Sstevel@tonic-gate long pnleft; /* Number of chars left in pargs */ 4447c478bd9Sstevel@tonic-gate tchar *pargcp; /* Current index into pargs */ 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate /* 4477c478bd9Sstevel@tonic-gate * History list 4487c478bd9Sstevel@tonic-gate * 4497c478bd9Sstevel@tonic-gate * Each history list entry contains an embedded wordlist 4507c478bd9Sstevel@tonic-gate * from the scanner, a number for the event, and a reference count 4517c478bd9Sstevel@tonic-gate * to aid in discarding old entries. 4527c478bd9Sstevel@tonic-gate * 4537c478bd9Sstevel@tonic-gate * Essentially "invisible" entries are put on the history list 4547c478bd9Sstevel@tonic-gate * when history substitution includes modifiers, and thrown away 4557c478bd9Sstevel@tonic-gate * at the next discarding since their event numbers are very negative. 4567c478bd9Sstevel@tonic-gate */ 4577c478bd9Sstevel@tonic-gate struct Hist { 4587c478bd9Sstevel@tonic-gate struct wordent Hlex; 4597c478bd9Sstevel@tonic-gate int Hnum; 4607c478bd9Sstevel@tonic-gate int Href; 4617c478bd9Sstevel@tonic-gate struct Hist *Hnext; 4627c478bd9Sstevel@tonic-gate } Histlist; 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate struct wordent paraml; /* Current lexical word list */ 4657c478bd9Sstevel@tonic-gate int eventno; /* Next events number */ 4667c478bd9Sstevel@tonic-gate int lastev; /* Last event reference (default) */ 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate tchar HIST; /* history invocation character */ 4697c478bd9Sstevel@tonic-gate tchar HISTSUB; /* auto-substitute character */ 4707c478bd9Sstevel@tonic-gate 471*65b0c20eSnakanon extern void *xalloc(size_t); 472*65b0c20eSnakanon extern void *xcalloc(size_t, size_t); 473*65b0c20eSnakanon extern void *xrealloc(void *, size_t); 474*65b0c20eSnakanon extern void xfree(void *); 475*65b0c20eSnakanon 4766c02b4a4Smuffin extern void Putchar(tchar); 4776c02b4a4Smuffin extern void bferr(char *) __NORETURN; 4786c02b4a4Smuffin extern void error() __NORETURN; 4796c02b4a4Smuffin extern void exitstat(void) __NORETURN; 4806c02b4a4Smuffin extern tchar *Dfix1(tchar *); 4816c02b4a4Smuffin extern tchar **blkcpy(tchar **, tchar **); 4826c02b4a4Smuffin extern tchar **blkspl(tchar **, tchar **); 4836c02b4a4Smuffin extern char **blkspl_(char **, char **); 4846c02b4a4Smuffin extern tchar **copyblk(tchar **); 4856c02b4a4Smuffin extern tchar **dobackp(tchar *, bool); 4866c02b4a4Smuffin extern tchar *domod(tchar *, int); 4876c02b4a4Smuffin extern struct Hist *enthist(int, struct wordent *, bool); 4886c02b4a4Smuffin extern tchar *getenv_(tchar *); 4896c02b4a4Smuffin extern tchar *getenvs_(char *); 4906c02b4a4Smuffin extern tchar *getwd_(tchar *); 4916c02b4a4Smuffin extern tchar **glob(tchar **); 4926c02b4a4Smuffin extern tchar *globone(tchar *); 4936c02b4a4Smuffin extern tchar *index_(tchar *, tchar); 4946c02b4a4Smuffin extern struct biltins *isbfunc(struct command *); 4956c02b4a4Smuffin extern void pintr(void); 4966c02b4a4Smuffin extern void pchild(void); 4976c02b4a4Smuffin extern tchar *putn(int); 4986c02b4a4Smuffin extern tchar *rindex_(tchar *, tchar); 4996c02b4a4Smuffin extern tchar **saveblk(tchar **); 5006c02b4a4Smuffin extern tchar *savestr(tchar *); 5016c02b4a4Smuffin extern tchar *strcat_(tchar *, tchar *); 5026c02b4a4Smuffin extern int strlen_(tchar *); 5036c02b4a4Smuffin extern tchar *strcpy_(tchar *, tchar *); 5046c02b4a4Smuffin extern tchar *strend(tchar *); 5056c02b4a4Smuffin extern tchar *strip(tchar *); 5066c02b4a4Smuffin extern tchar *strspl(tchar *, tchar *); 5076c02b4a4Smuffin extern struct command *syntax(struct wordent *, struct wordent *, int); 5086c02b4a4Smuffin extern tchar *value1(tchar *, struct varent *); 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate #define NOSTR ((tchar *) 0) 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate /* 5137c478bd9Sstevel@tonic-gate * setname is a macro to copy the path in bname. (see sh.err.c) 5147c478bd9Sstevel@tonic-gate * Here we are dynamically reallocating the bname to the new length 5157c478bd9Sstevel@tonic-gate * to store the new path 5167c478bd9Sstevel@tonic-gate */ 5177c478bd9Sstevel@tonic-gate tchar *bname; 5187c478bd9Sstevel@tonic-gate #define setname(a) { \ 5197c478bd9Sstevel@tonic-gate bname = xrealloc(bname, (strlen_(a)+1) * sizeof (tchar)); \ 5207c478bd9Sstevel@tonic-gate strcpy_(bname, a); \ 5217c478bd9Sstevel@tonic-gate bname[strlen_(a)] = '\0'; \ 5227c478bd9Sstevel@tonic-gate } 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate #ifdef VFORK 5257c478bd9Sstevel@tonic-gate tchar *Vsav; 5267c478bd9Sstevel@tonic-gate tchar **Vav; 5277c478bd9Sstevel@tonic-gate tchar *Vdp; 5287c478bd9Sstevel@tonic-gate #endif 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate tchar **evalvec; 5317c478bd9Sstevel@tonic-gate tchar *evalp; 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate /* Conversion functions between char and tchar strings. */ 5347c478bd9Sstevel@tonic-gate tchar *strtots(/* tchar * , char * */); 5357c478bd9Sstevel@tonic-gate char *tstostr(/* char * , tchar * */); 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate #ifndef NULL 5387c478bd9Sstevel@tonic-gate #define NULL 0 5397c478bd9Sstevel@tonic-gate #endif 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate /* 5437c478bd9Sstevel@tonic-gate * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used 5447c478bd9Sstevel@tonic-gate * to hash execs. If it is allocated (havhash true), then to tell 5457c478bd9Sstevel@tonic-gate * whether ``name'' is (possibly) present in the i'th component 5467c478bd9Sstevel@tonic-gate * of the variable path, you look at the bit in xhash indexed by 5477c478bd9Sstevel@tonic-gate * hash(hashname("name"), i). This is setup automatically 5487c478bd9Sstevel@tonic-gate * after .login is executed, and recomputed whenever ``path'' is 5497c478bd9Sstevel@tonic-gate * changed. 5507c478bd9Sstevel@tonic-gate * The two part hash function is designed to let texec() call the 5517c478bd9Sstevel@tonic-gate * more expensive hashname() only once and the simple hash() several 5527c478bd9Sstevel@tonic-gate * times (once for each path component checked). 5537c478bd9Sstevel@tonic-gate * Byte size is assumed to be 8. 5547c478bd9Sstevel@tonic-gate */ 5557c478bd9Sstevel@tonic-gate #define HSHSIZ (32*1024) /* 4k bytes */ 5567c478bd9Sstevel@tonic-gate #define HSHMASK (HSHSIZ - 1) 5577c478bd9Sstevel@tonic-gate #define HSHMUL 243 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * The following two arrays are used for caching. xhash 5617c478bd9Sstevel@tonic-gate * is for caching path variable and xhash2 is for cdpath 5627c478bd9Sstevel@tonic-gate * variable. 5637c478bd9Sstevel@tonic-gate */ 5647c478bd9Sstevel@tonic-gate 5657c478bd9Sstevel@tonic-gate char xhash[HSHSIZ / 8]; 5667c478bd9Sstevel@tonic-gate char xhash2[HSHSIZ / 8]; 5677c478bd9Sstevel@tonic-gate #define hash(a, b) ((a) * HSHMUL + (b) & HSHMASK) 5687c478bd9Sstevel@tonic-gate #define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */ 5697c478bd9Sstevel@tonic-gate #define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7)) /* bit set */ 5707c478bd9Sstevel@tonic-gate #ifdef VFORK 5717c478bd9Sstevel@tonic-gate int hits, misses; 5727c478bd9Sstevel@tonic-gate #endif 573