2 * Public Domain Bourne/Korn shell
5 #include "config.h" /* system and option configuration info */
7 # define ARGS(args) args /* prototype declaration */
10 # define UNUSED(x) x __attribute__((__unused__))
15 /* Start of common headers */
18 #include <sys/types.h>
28 #define timerclear(tvp) do { (tvp)->tv_sec = (tvp)->tv_usec = 0; } while (0)
31 #define timeradd(tvp, uvp, vvp) \
33 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
34 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
35 if ((vvp)->tv_usec >= 1000000) { \
37 (vvp)->tv_usec -= 1000000; \
42 #define timersub(tvp, uvp, vvp) \
44 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
45 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
46 if ((vvp)->tv_usec < 0) { \
48 (vvp)->tv_usec += 1000000; \
61 #define SIZE_MAX SIZE_T_MAX
63 #define SIZE_MAX ((size_t)-1)
68 #define SH_VA_START(va, argn) va_start(va, argn)
76 # include <sys/file.h>
77 #endif /* HAVE_FCNTL_H */
79 # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
80 #endif /* !O_ACCMODE */
82 #ifndef F_OK /* access() arguments */
91 # define SEEK_SET L_SET
92 # define SEEK_CUR L_INCR
93 # define SEEK_END L_XTND
99 #endif /* !SEEK_SET */
101 /* Some machines (eg, FreeBSD 1.1.5) define CLK_TCK in limits.h
102 * (ksh_limval.h assumes limits has been included, if available)
106 #endif /* HAVE_LIMITS_H */
110 # define SIGNALS NSIG
113 # define SIGNALS (_NSIG+1) /* _NSIG is # of signals used, excluding 0. */
115 # ifdef _SIGMAX /* QNX */
116 # define SIGNALS _SIGMAX
119 # endif /* _SIGMAX */
123 # define SIGCHLD SIGCLD
125 /* struct sigaction.sa_flags is set to KSH_SA_FLAGS. Used to ensure
126 * system calls are interrupted
129 # define KSH_SA_FLAGS SA_INTERRUPT
130 #else /* SA_INTERRUPT */
131 # define KSH_SA_FLAGS 0
132 #endif /* SA_INTERRUPT */
134 typedef RETSIGTYPE (*handler_t
) ARGS((int)); /* signal handler */
138 #endif /* HAVE_PATHS_H */
140 # define DEFAULT__PATH _PATH_DEFPATH
141 #else /* _PATH_DEFPATH */
142 # define DEFAULT__PATH DEFAULT_PATH
143 #endif /* _PATH_DEFPATH */
146 # define offsetof(type,id) ((size_t)&((type*)NULL)->id)
150 # define killpg(p, s) kill(-(p), (s))
151 #endif /* !HAVE_KILLPG */
153 /* Special cases for execve(2) */
155 extern int ksh_execve(char *cmd
, char **args
, char **env
, int flags
);
157 # if defined(OS_ISC) && defined(_POSIX_SOURCE)
158 /* Kludge for ISC 3.2 (and other versions?) so programs will run correctly. */
159 # define ksh_execve(p, av, ev, flags) \
165 # else /* OS_ISC && _POSIX */
166 # define ksh_execve(p, av, ev, flags) execve(p, av, ev)
167 # endif /* OS_ISC && _POSIX */
170 /* this is a hang-over from older versions of the os2 port */
171 #define ksh_dupbase(fd, base) fcntl(fd, F_DUPFD, base)
173 #define ksh_siglongjmp(env,v) siglongjmp((env), (v))
174 #define ksh_jmp_buf sigjmp_buf
177 extern int dup2
ARGS((int, int));
178 #endif /* !HAVE_DUP2 */
180 /* end of common headers */
182 /* Stop gcc and lint from complaining about possibly uninitialized variables */
183 #if defined(__GNUC__) || defined(lint)
184 # define UNINITIALIZED(var) var = 0
186 # define UNINITIALIZED(var) var
187 #endif /* GNUC || lint */
189 /* some useful #defines */
194 # define EXTERN extern
195 # define EXTERN_DEFINED
199 /* shell to exec scripts (see also $SHELL initialization in main.c) */
201 # define EXECSHELL (inDOS() ? "c:\\command.com" : "c:\\os2\\cmd.exe")
202 # define EXECSHELL_STR (inDOS() ? "COMSPEC" : "OS2_SHELL")
204 # define EXECSHELL "/bin/sh"
205 # define EXECSHELL_STR "EXECSHELL"
209 /* ISABSPATH() means path is fully and completely specified,
210 * ISROOTEDPATH() means a .. as the first component is a no-op,
211 * ISRELPATH() means $PWD can be tacked on to get an absolute path.
213 * OS Path ISABSPATH ISROOTEDPATH ISRELPATH
214 * unix /foo yes yes no
216 * unix ../foo no no yes
217 * os2+cyg a:/foo yes yes no
218 * os2+cyg a:foo no no no
219 * os2+cyg /foo no yes no
220 * os2+cyg foo no no yes
221 * os2+cyg ../foo no no yes
222 * cyg //foo yes yes no
226 # define DIRSEPSTR "/"
227 # define ISDIRSEP(c) ((c) == '/')
229 # define ISABSPATH(s) \
230 (((s)[0] && (s)[1] == ':' && ISDIRSEP((s)[2])) || ISDIRSEP((s)[0]))
231 # define ISRELPATH(s) (!(s)[0] || ((s)[1] != ':' && !ISDIRSEP((s)[0])))
232 #else /* __CYGWIN__ */
233 # define ISABSPATH(s) ISDIRSEP((s)[0])
234 # define ISRELPATH(s) (!ISABSPATH(s))
235 #endif /* __CYGWIN__ */
236 # define ISROOTEDPATH(s) ISABSPATH(s)
237 # define FILECHCONV(c) c
238 # define FILECMP(s1, s2) strcmp(s1, s2)
239 # define FILENCMP(s1, s2, n) strncmp(s1, s2, n)
240 # define ksh_strchr_dirsep(p) strchr(p, DIRSEP)
241 # define ksh_strrchr_dirsep(p) strrchr(p, DIRSEP)
247 #define NELEM(a) (sizeof(a) / sizeof((a)[0]))
248 #define sizeofN(type, n) (sizeof(type) * (n))
249 #define BIT(i) (1<<(i)) /* define bit in flag */
251 /* Table flag type - needs > 16 and < 32 bits */
252 typedef int32_t Tflag
;
254 #define NUFILE 10 /* Number of user-accessible files */
255 #define FDBASE 10 /* First file usable by Shell */
257 /* you're not going to run setuid shell scripts, are you? */
258 #define eaccess(path, mode) access(path, mode)
260 /* Make MAGIC a char that might be printed to make bugs more obvious, but
261 * not a char that is used often. Also, can't use the high bit as it causes
262 * portability problems (calling strchr(x, 0x80|'x') is error prone).
264 #define MAGIC (7)/* prefix for *?[!{,} during expand */
265 #define ISMAGIC(c) ((unsigned char)(c) == MAGIC)
266 #define NOT '!' /* might use ^ (ie, [!...] vs [^..]) */
268 #define LINE 1024 /* input line size */
269 #define PATH 1024 /* pathname size (todo: PATH_MAX/pathconf()) */
270 #define ARRAYMAX 1023 /* max array index */
272 EXTERN
const char *poshname
; /* $0 */
273 EXTERN pid_t kshpid
; /* $$, shell pid */
274 EXTERN pid_t procpid
; /* pid of executing process */
275 EXTERN uid_t ksheuid
; /* effective uid of shell */
276 EXTERN
int exstat
; /* exit status */
277 EXTERN
int subst_exstat
; /* exit status of last $(..)/`..` */
278 EXTERN
const char *safe_prompt
; /* safe prompt if PS1 substitution fails */
282 * Evil hack for const correctness due to API brokenness
294 * simple grouping allocator
297 /* 1. internal structure */
303 #define ALLOC_ITEM struct lalloc
304 #define ALLOC_SIZE (sizeof(ALLOC_ITEM))
306 /* 3. group structure (only the same for lalloc.c) */
307 typedef struct lalloc Area
;
310 EXTERN Area aperm
; /* permanent object space */
312 #define ATEMP &e->area
315 * parsing & execution environment
318 ALLOC_ITEM __alloc_i
; /* internal, do not touch */
319 Area area
; /* temporary allocation area */
320 struct env
*oenv
; /* link to previous environment */
321 struct block
*loc
; /* local variables and functions */
322 short *savefd
; /* original redirected fds */
323 struct temp
*temps
; /* temp files */
324 sigjmp_buf jbuf
; /* long jump back to env creator */
325 short type
; /* environment type - see below */
326 short flags
; /* EF_* */
329 /* struct env.type values */
330 #define E_NONE 0 /* dummy environment */
331 #define E_PARSE 1 /* parsing command # */
332 #define E_FUNC 2 /* executing function # */
333 #define E_INCL 3 /* including a file via . # */
334 #define E_EXEC 4 /* executing command tree */
335 #define E_LOOP 5 /* executing for/while # */
336 #define E_ERRH 6 /* general error handler # */
337 /* # indicates env has valid jbuf (see unwind()) */
339 /* struct env.flag values */
340 #define EF_FUNC_PARSE BIT(0) /* function being parsed */
341 #define EF_BRKCONT_PASS BIT(1) /* set if E_LOOP must pass break/continue on */
342 #define EF_FAKE_SIGDIE BIT(2) /* hack to get info from unwind to quitenv */
344 /* Do breaks/continues stop at env type e? */
345 #define STOP_BRKCONT(t) ((t) == E_NONE || (t) == E_PARSE \
346 || (t) == E_FUNC || (t) == E_INCL)
347 /* Do returns stop at env type e? */
348 #define STOP_RETURN(t) ((t) == E_FUNC || (t) == E_INCL)
350 /* values for siglongjmp(e->jbuf, 0) */
351 #define LRETURN 1 /* return statement */
352 #define LEXIT 2 /* exit statement */
353 #define LERROR 3 /* errorf() called */
354 #define LLEAVE 4 /* untrappable exit/error */
355 #define LINTR 5 /* ^C noticed */
356 #define LBREAK 6 /* break statement */
357 #define LCONTIN 7 /* continue statement */
358 #define LSHELL 8 /* return to interactive shell() */
359 #define LAEXPR 9 /* error in arithmetic expression */
361 /* option processing */
362 #define OF_CMDLINE 0x01 /* command line */
363 #define OF_SET 0x02 /* set builtin */
364 #define OF_SPECIAL 0x04 /* a special variable changing */
365 #define OF_INTERNAL 0x08 /* set internally by shell */
366 #define OF_ANY (OF_CMDLINE | OF_SET | OF_SPECIAL | OF_INTERNAL)
369 const char *name
; /* long name of option */
370 char c
; /* character flag (if any) */
371 short flags
; /* OF_* */
373 extern const struct posh_option options
[];
376 * flags (the order of these enums MUST match the order in misc.c(options[]))
379 FEXPORT
= 0, /* -a: export all */
381 FBRACEEXPAND
, /* enable {} globbing */
383 FCOMMAND
, /* -c: (invocation) execute specified command */
385 FEMACS
, /* emacs command editing */
387 FERREXIT
, /* -e: quit on error */
389 FGMACS
, /* gmacs command editing */
391 FIGNOREEOF
, /* eof does not exit */
392 FTALKING
, /* -i: interactive */
393 FLOGIN
, /* -l: a login shell */
394 FMONITOR
, /* -m: job control monitoring */
395 FNOCLOBBER
, /* -C: don't overwrite existing files */
396 FNOEXEC
, /* -n: don't execute any commands */
397 FNOGLOB
, /* -f: don't do file globbing */
398 FNOHUP
, /* -H: don't kill running jobs when login shell exits */
399 FNOLOG
, /* don't save functions in history (ignored) */
401 FNOTIFY
, /* -b: asynchronous job completion notification */
403 FNOUNSET
, /* -u: using an unset var is an error */
404 FPRIVILEGED
, /* -p: use suid_profile */
405 FSTDIN
, /* -s: (invocation) parse stdin */
406 #ifdef SILLY_FEATURES
407 FTRACKALL
, /* -h: create tracked aliases for all commands */
408 #endif /* SILLY_FEATURES */
409 FVERBOSE
, /* -v: echo input */
411 FVI
, /* vi command editing */
412 FVIRAW
, /* always read in raw mode (ignored) */
413 FVISHOW8
, /* display chars with 8th bit set as is (versus M-) */
414 FVITABCOMPLETE
, /* enable tab as file name completion char */
415 FVIESCCOMPLETE
, /* enable ESC as file name completion in command mode */
417 FXTRACE
, /* -x: execution trace */
418 FTALKING_I
, /* (internal): initial shell was interactive */
419 FNFLAGS
/* (place holder: how many flags are there) */
422 #define Flag(f) (shell_flags[(int) (f)])
424 EXTERN
char shell_flags
[FNFLAGS
];
426 EXTERN
char null
[] I__(""); /* null value for variable */
429 TT_HEREDOC_EXP
, /* expanded heredoc */
430 TT_HIST_EDIT
/* temp file used for history editing (fc -e) */
432 typedef enum temp_type Temp_type
;
433 /* temp/heredoc files. The file is removed when the struct is freed. */
437 int pid
; /* pid of process parsed here-doc */
443 * stdio and our IO routines
446 #define shl_spare (&shf_iob[0]) /* for c_read()/c_print() */
447 #define shl_stdout (&shf_iob[1])
448 #define shl_out (&shf_iob[2])
449 EXTERN
int shl_stdout_ok
;
454 typedef struct trap
{
455 int signal
; /* signal number */
456 const char *name
; /* short name */
457 const char *mess
; /* descriptive name */
458 char *trap
; /* trap command */
459 int volatile set
; /* trap pending */
460 int flags
; /* TF_* */
461 handler_t cursig
; /* current handler (valid if TF_ORIG_* set) */
462 handler_t shtrap
; /* shell signal handler */
465 /* values for Trap.flags */
466 #define TF_SHELL_USES BIT(0) /* shell uses signal, user can't change */
467 #define TF_USER_SET BIT(1) /* user has (tried to) set trap */
468 #define TF_ORIG_IGN BIT(2) /* original action was SIG_IGN */
469 #define TF_ORIG_DFL BIT(3) /* original action was SIG_DFL */
470 #define TF_EXEC_IGN BIT(4) /* restore SIG_IGN just before exec */
471 #define TF_EXEC_DFL BIT(5) /* restore SIG_DFL just before exec */
472 #define TF_DFL_INTR BIT(6) /* when received, default action is LINTR */
473 #define TF_TTY_INTR BIT(7) /* tty generated signal (see j_waitj) */
474 #define TF_CHANGED BIT(8) /* used by runtrap() to detect trap changes */
475 #define TF_FATAL BIT(9) /* causes termination if not trapped */
477 /* values for setsig()/setexecsig() flags argument */
478 #define SS_RESTORE_MASK 0x3 /* how to restore a signal before an exec() */
479 #define SS_RESTORE_CURR 0 /* leave current handler in place */
480 #define SS_RESTORE_ORIG 1 /* restore original handler */
481 #define SS_RESTORE_DFL 2 /* restore to SIG_DFL */
482 #define SS_RESTORE_IGN 3 /* restore to SIG_IGN */
483 #define SS_FORCE BIT(3) /* set signal even if original signal ignored */
484 #define SS_USER BIT(4) /* user is doing the set (ie, trap command) */
485 #define SS_SHTRAP BIT(5) /* trap for internal use (CHLD,ALRM,WINCH) */
487 #define SIGEXIT_ 0 /* for trap EXIT */
489 EXTERN
int volatile trap
; /* traps pending? */
490 EXTERN
int volatile intrsig
; /* pending trap interrupts executing command */
491 EXTERN
int volatile fatal_trap
;/* received a fatal signal */
493 /* Kludge to avoid bogus re-declaration of sigtraps[] error on AIX 3.2.5 */
494 extern Trap sigtraps
[SIGNALS
+1];
495 #endif /* !FROM_TRAP_C */
502 /* values for ksh_tmout_state */
504 TMOUT_EXECUTING
= 0, /* executing commands */
505 TMOUT_READING
, /* waiting for input */
506 TMOUT_LEAVING
/* have timed out */
508 EXTERN
unsigned int ksh_tmout
;
509 EXTERN
enum tmout_enum ksh_tmout_state
I__(TMOUT_EXECUTING
);
513 /* For "You have stopped jobs" message */
514 EXTERN
int really_exit
;
518 * fast character classes
520 #define C_LEX1 BIT(2) /* \0 \t\n|&;<>() */
521 #define C_VAR1 BIT(3) /* *@#!$-? */
522 #define C_IFSWS BIT(4) /* \t \n (IFS white space) */
523 #define C_SUBOP1 BIT(5) /* "=-+?" */
524 #define C_SUBOP2 BIT(6) /* "#%" */
525 #define C_IFS BIT(7) /* $IFS */
526 #define C_QUOTE BIT(8) /* \n\t"#$&'()*;<>?[\`| (needing quoting) */
528 extern short ctypes
[];
530 #define ctype(c, t) !!(ctypes[(unsigned char)(c)]&(t))
532 EXTERN
int ifs0
I__(' '); /* for "$*" */
535 /* Argument parsing for built-in commands and getopts command */
537 /* Values for Getopt.flags */
538 #define GF_ERROR BIT(0) /* call errorf() if there is an error */
539 #define GF_PLUSOPT BIT(1) /* allow +c as an option */
540 #define GF_NONAME BIT(2) /* don't print argv[0] in errors */
542 /* Values for Getopt.info */
543 #define GI_MINUS BIT(0) /* an option started with -... */
544 #define GI_PLUS BIT(1) /* an option started with +... */
545 #define GI_MINUSMINUS BIT(2) /* arguments were ended with -- */
549 int uoptind
;/* what user sees in $OPTIND */
551 int flags
; /* see GF_* */
552 int info
; /* see GI_* */
553 unsigned int p
; /* 0 or index into argv[optind - 1] */
554 char buf
[2]; /* for bad option OPTARG value */
557 EXTERN LameGetopt builtin_opt
; /* for shell builtin commands */
558 EXTERN LameGetopt user_opt
; /* parsing state for getopts builtin command */
562 /* This for co-processes */
564 typedef INT32 Coproc_id
; /* something that won't (realisticly) wrap */
566 int read
; /* pipe from co-process's stdout */
567 int readw
; /* other side of read (saved temporarily) */
568 int write
; /* pipe to co-process's stdin */
569 Coproc_id id
; /* id of current output pipe */
570 int njobs
; /* number of live jobs using output pipe */
571 void *job
; /* 0 or job of co-process using input pipe */
573 EXTERN
struct coproc coproc
;
576 /* Used in jobs.c and by coprocess stuff in exec.c */
577 EXTERN sigset_t sm_default
, sm_sigchld
;
579 /* name of called builtin function (used by error functions) */
580 EXTERN
char *builtin_argv0
;
581 EXTERN Tflag builtin_flag
; /* flags of called builtin (SPEC_BI, etc.) */
583 /* current working directory, and size of memory allocated for same */
584 EXTERN
char *current_wd
;
585 EXTERN
int current_wd_size
;
588 /* Minimium required space to work with on a line - if the prompt leaves less
589 * space than this on a line, the prompt is truncated.
591 # define MIN_EDIT_SPACE 7
592 /* Minimium allowed value for x_cols: 2 for prompt, 3 for " < " at end of line
594 # define MIN_COLS (2 + MIN_EDIT_SPACE + 3)
595 EXTERN
int x_cols
I__(80); /* tty columns */
597 # define x_cols 80 /* for pr_menu(exec.c) */
601 /* These to avoid bracket matching problems */
609 /* Determine the location of the system (common) profile */
610 #ifndef KSH_SYSTEM_PROFILE
612 # define KSH_SYSTEM_PROFILE "/etc/profile.std"
614 # define KSH_SYSTEM_PROFILE "/etc/profile"
616 #endif /* KSH_SYSTEM_PROFILE */
618 /* Used by v_evaluate() and setstr() to control action when error occurs */
619 #define KSH_UNWIND_ERROR 0 /* unwind the stack (longjmp) */
620 #define KSH_RETURN_ERROR 1 /* return 1/0 for success/failure */
629 /* user and system time of last j_waitjed job */
630 EXTERN
struct timeval j_usrtime
, j_systime
;
633 #define alloc(n, ap) aresize(NULL, (n), (ap))
635 /* be sure not to interfere with anyone else's idea about EXTERN */
636 #ifdef EXTERN_DEFINED
637 # undef EXTERN_DEFINED