sh.h revision 1.1.1.3 1 /*
2 * Public Domain Bourne/Korn shell
3 */
4
5 /* $Id: sh.h,v 1.1.1.3 1999/10/20 14:27:32 hubertf Exp $ */
6
7 #include "config.h" /* system and option configuration info */
8
9 #ifdef HAVE_PROTOTYPES
10 # define ARGS(args) args /* prototype declaration */
11 #else
12 # define ARGS(args) () /* K&R declaration */
13 #endif
14
15
16 /* Start of common headers */
17
18 #include <stdio.h>
19 #include <sys/types.h>
20 #include <setjmp.h>
21 #ifdef HAVE_STDDEF_H
22 # include <stddef.h>
23 #endif
24
25 #ifdef HAVE_STDLIB_H
26 # include <stdlib.h>
27 #else
28 /* just a useful subset of what stdlib.h would have */
29 extern char * getenv ARGS((const char *));
30 extern void * malloc ARGS((size_t));
31 extern void * realloc ARGS((void *, size_t));
32 extern int free ARGS((void *));
33 extern int exit ARGS((int));
34 extern int rand ARGS((void));
35 extern void srand ARGS((unsigned int));
36 extern int atoi ARGS((const char *));
37 #endif /* HAVE_STDLIB_H */
38
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #else
42 /* just a useful subset of what unistd.h would have */
43 extern int access ARGS((const char *, int));
44 extern int open ARGS((const char *, int, ...));
45 extern int creat ARGS((const char *, mode_t));
46 extern int read ARGS((int, char *, unsigned));
47 extern int write ARGS((int, const char *, unsigned));
48 extern off_t lseek ARGS((int, off_t, int));
49 extern int close ARGS((int));
50 extern int pipe ARGS((int []));
51 extern int dup2 ARGS((int, int));
52 extern int unlink ARGS((const char *));
53 extern int fork ARGS((void));
54 extern int execve ARGS((const char *, char * const[], char * const[]));
55 extern int chdir ARGS((const char *));
56 extern int kill ARGS((pid_t, int));
57 extern char *getcwd(); /* no ARGS here - differs on different machines */
58 extern int geteuid ARGS((void));
59 extern int readlink ARGS((const char *, char *, int));
60 extern int getegid ARGS((void));
61 extern int getpid ARGS((void));
62 extern int getppid ARGS((void));
63 extern unsigned int sleep ARGS((unsigned int));
64 extern int isatty ARGS((int));
65 # ifdef POSIX_PGRP
66 extern int getpgrp ARGS((void));
67 extern int setpgid ARGS((pid_t, pid_t));
68 # endif /* POSIX_PGRP */
69 # ifdef BSD_PGRP
70 extern int getpgrp ARGS((pid_t));
71 extern int setpgrp ARGS((pid_t, pid_t));
72 # endif /* BSD_PGRP */
73 # ifdef SYSV_PGRP
74 extern int getpgrp ARGS((void));
75 extern int setpgrp ARGS((void));
76 # endif /* SYSV_PGRP */
77 #endif /* HAVE_UNISTD_H */
78
79 #ifdef HAVE_STRING_H
80 # include <string.h>
81 #else
82 # include <strings.h>
83 # define strchr index
84 # define strrchr rindex
85 #endif /* HAVE_STRING_H */
86 #ifndef HAVE_STRSTR
87 char *strstr ARGS((const char *s, const char *p));
88 #endif /* HAVE_STRSTR */
89 #ifndef HAVE_STRCASECMP
90 int strcasecmp ARGS((const char *s1, const char *s2));
91 int strncasecmp ARGS((const char *s1, const char *s2, int n));
92 #endif /* HAVE_STRCASECMP */
93
94 #ifdef HAVE_MEMORY_H
95 # include <memory.h>
96 #endif
97 #ifndef HAVE_MEMSET
98 # define memcpy(d, s, n) bcopy(s, d, n)
99 # define memcmp(s1, s2, n) bcmp(s1, s2, n)
100 void *memset ARGS((void *d, int c, size_t n));
101 #endif /* HAVE_MEMSET */
102 #ifndef HAVE_MEMMOVE
103 # ifdef HAVE_BCOPY
104 # define memmove(d, s, n) bcopy(s, d, n)
105 # else
106 void *memmove ARGS((void *d, const void *s, size_t n));
107 # endif
108 #endif /* HAVE_MEMMOVE */
109
110 #ifdef HAVE_PROTOTYPES
111 # include <stdarg.h>
112 # define SH_VA_START(va, argn) va_start(va, argn)
113 #else
114 # include <varargs.h>
115 # define SH_VA_START(va, argn) va_start(va)
116 #endif /* HAVE_PROTOTYPES */
117
118 #include <errno.h>
119 extern int errno;
120
121 #ifdef HAVE_FCNTL_H
122 # include <fcntl.h>
123 #else
124 # include <sys/file.h>
125 #endif /* HAVE_FCNTL_H */
126 #ifndef O_ACCMODE
127 # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
128 #endif /* !O_ACCMODE */
129
130 #ifndef F_OK /* access() arguments */
131 # define F_OK 0
132 # define X_OK 1
133 # define W_OK 2
134 # define R_OK 4
135 #endif /* !F_OK */
136
137 #ifndef SEEK_SET
138 # ifdef L_SET
139 # define SEEK_SET L_SET
140 # define SEEK_CUR L_INCR
141 # define SEEK_END L_XTND
142 # else /* L_SET */
143 # define SEEK_SET 0
144 # define SEEK_CUR 1
145 # define SEEK_END 2
146 # endif /* L_SET */
147 #endif /* !SEEK_SET */
148
149 /* Some machines (eg, FreeBSD 1.1.5) define CLK_TCK in limits.h
150 * (ksh_limval.h assumes limits has been included, if available)
151 */
152 #ifdef HAVE_LIMITS_H
153 # include <limits.h>
154 #endif /* HAVE_LIMITS_H */
155
156 #include <signal.h>
157 #ifdef NSIG
158 # define SIGNALS NSIG
159 #else
160 # ifdef _MINIX
161 # define SIGNALS (_NSIG+1) /* _NSIG is # of signals used, excluding 0. */
162 # else
163 # ifdef _SIGMAX /* QNX */
164 # define SIGNALS _SIGMAX
165 # else /* _SIGMAX */
166 # define SIGNALS 32
167 # endif /* _SIGMAX */
168 # endif /* _MINIX */
169 #endif /* NSIG */
170 #ifndef SIGCHLD
171 # define SIGCHLD SIGCLD
172 #endif
173 /* struct sigaction.sa_flags is set to KSH_SA_FLAGS. Used to ensure
174 * system calls are interrupted
175 */
176 #ifdef SA_INTERRUPT
177 # define KSH_SA_FLAGS SA_INTERRUPT
178 #else /* SA_INTERRUPT */
179 # define KSH_SA_FLAGS 0
180 #endif /* SA_INTERRUPT */
181
182 typedef RETSIGTYPE (*handler_t) ARGS((int)); /* signal handler */
183
184 #ifdef USE_FAKE_SIGACT
185 # include "sigact.h" /* use sjg's fake sigaction() */
186 #endif
187
188 #ifdef HAVE_PATHS_H
189 # include <paths.h>
190 #endif /* HAVE_PATHS_H */
191 #ifdef _PATH_DEFPATH
192 # define DEFAULT__PATH _PATH_DEFPATH
193 #else /* _PATH_DEFPATH */
194 # define DEFAULT__PATH DEFAULT_PATH
195 #endif /* _PATH_DEFPATH */
196
197 #ifndef offsetof
198 # define offsetof(type,id) ((size_t)&((type*)NULL)->id)
199 #endif
200
201 #ifndef HAVE_KILLPG
202 # define killpg(p, s) kill(-(p), (s))
203 #endif /* !HAVE_KILLPG */
204
205 /* Special cases for execve(2) */
206 #ifdef OS2
207 extern int ksh_execve(char *cmd, char **args, char **env, int flags);
208 #else /* OS2 */
209 # if defined(OS_ISC) && defined(_POSIX_SOURCE)
210 /* Kludge for ISC 3.2 (and other versions?) so programs will run correctly. */
211 # define ksh_execve(p, av, ev, flags) \
212 do { \
213 __setostype(0); \
214 execve(p, av, ev); \
215 __setostype(1); \
216 } while (0)
217 # else /* OS_ISC && _POSIX */
218 # define ksh_execve(p, av, ev, flags) execve(p, av, ev)
219 # endif /* OS_ISC && _POSIX */
220 #endif /* OS2 */
221
222 /* this is a hang-over from older versions of the os2 port */
223 #define ksh_dupbase(fd, base) fcntl(fd, F_DUPFD, base)
224
225 #ifdef HAVE_SIGSETJMP
226 # define ksh_sigsetjmp(env,sm) sigsetjmp((env), (sm))
227 # define ksh_siglongjmp(env,v) siglongjmp((env), (v))
228 # define ksh_jmp_buf sigjmp_buf
229 #else /* HAVE_SIGSETJMP */
230 # ifdef HAVE__SETJMP
231 # define ksh_sigsetjmp(env,sm) _setjmp(env)
232 # define ksh_siglongjmp(env,v) _longjmp((env), (v))
233 # else /* HAVE__SETJMP */
234 # define ksh_sigsetjmp(env,sm) setjmp(env)
235 # define ksh_siglongjmp(env,v) longjmp((env), (v))
236 # endif /* HAVE__SETJMP */
237 # define ksh_jmp_buf jmp_buf
238 #endif /* HAVE_SIGSETJMP */
239
240 #ifndef HAVE_DUP2
241 extern int dup2 ARGS((int, int));
242 #endif /* !HAVE_DUP2 */
243
244 /* Find a integer type that is at least 32 bits (or die) - SIZEOF_* defined
245 * by autoconf (assumes an 8 bit byte, but I'm not concerned).
246 * NOTE: INT32 may end up being more than 32 bits.
247 */
248 #if SIZEOF_INT >= 4
249 # define INT32 int
250 #else /* SIZEOF_INT */
251 # if SIZEOF_LONG >= 4
252 # define INT32 long
253 # else /* SIZEOF_LONG */
254 #error cannot find 32 bit type...
255 # endif /* SIZEOF_LONG */
256 #endif /* SIZEOF_INT */
257
258 /* end of common headers */
259
260 /* Stop gcc and lint from complaining about possibly uninitialized variables */
261 #if defined(__GNUC__) || defined(lint)
262 # define UNINITIALIZED(var) var = 0
263 #else
264 # define UNINITIALIZED(var) var
265 #endif /* GNUC || lint */
266
267 /* some useful #defines */
268 #ifdef EXTERN
269 # define I__(i) = i
270 #else
271 # define I__(i)
272 # define EXTERN extern
273 # define EXTERN_DEFINED
274 #endif
275
276 #ifdef OS2
277 # define inDOS() (!(_emx_env & 0x200))
278 #endif
279
280 #ifndef EXECSHELL
281 /* shell to exec scripts (see also $SHELL initialization in main.c) */
282 # ifdef OS2
283 # define EXECSHELL (inDOS() ? "c:\\command.com" : "c:\\os2\\cmd.exe")
284 # define EXECSHELL_STR (inDOS() ? "COMSPEC" : "OS2_SHELL")
285 # else /* OS2 */
286 # define EXECSHELL "/bin/sh"
287 # define EXECSHELL_STR "EXECSHELL"
288 # endif /* OS2 */
289 #endif
290
291 /* ISABSPATH() means path is fully and completely specified,
292 * ISROOTEDPATH() means a .. as the first component is a no-op,
293 * ISRELPATH() means $PWD can be tacked on to get an absolute path.
294 *
295 * OS Path ISABSPATH ISROOTEDPATH ISRELPATH
296 * unix /foo yes yes no
297 * unix foo no no yes
298 * unix ../foo no no yes
299 * os2+cyg a:/foo yes yes no
300 * os2+cyg a:foo no no no
301 * os2+cyg /foo no yes no
302 * os2+cyg foo no no yes
303 * os2+cyg ../foo no no yes
304 * cyg //foo yes yes no
305 */
306 #ifdef OS2
307 # define PATHSEP ';'
308 # define DIRSEP '/' /* even though \ is native */
309 # define DIRSEPSTR "\\"
310 # define ISDIRSEP(c) ((c) == '\\' || (c) == '/')
311 # define ISABSPATH(s) (((s)[0] && (s)[1] == ':' && ISDIRSEP((s)[2])))
312 # define ISROOTEDPATH(s) (ISDIRSEP((s)[0]) || ISABSPATH(s))
313 # define ISRELPATH(s) (!(s)[0] || ((s)[1] != ':' && !ISDIRSEP((s)[0])))
314 # define FILECHCONV(c) (isascii(c) && isupper(c) ? tolower(c) : c)
315 # define FILECMP(s1, s2) stricmp(s1, s2)
316 # define FILENCMP(s1, s2, n) strnicmp(s1, s2, n)
317 extern char *ksh_strchr_dirsep(const char *path);
318 extern char *ksh_strrchr_dirsep(const char *path);
319 # define chdir _chdir2
320 # define getcwd _getcwd2
321 #else
322 # define PATHSEP ':'
323 # define DIRSEP '/'
324 # define DIRSEPSTR "/"
325 # define ISDIRSEP(c) ((c) == '/')
326 #ifdef __CYGWIN__
327 # define ISABSPATH(s) \
328 (((s)[0] && (s)[1] == ':' && ISDIRSEP((s)[2])) || ISDIRSEP((s)[0]))
329 # define ISRELPATH(s) (!(s)[0] || ((s)[1] != ':' && !ISDIRSEP((s)[0])))
330 #else /* __CYGWIN__ */
331 # define ISABSPATH(s) ISDIRSEP((s)[0])
332 # define ISRELPATH(s) (!ISABSPATH(s))
333 #endif /* __CYGWIN__ */
334 # define ISROOTEDPATH(s) ISABSPATH(s)
335 # define FILECHCONV(c) c
336 # define FILECMP(s1, s2) strcmp(s1, s2)
337 # define FILENCMP(s1, s2, n) strncmp(s1, s2, n)
338 # define ksh_strchr_dirsep(p) strchr(p, DIRSEP)
339 # define ksh_strrchr_dirsep(p) strrchr(p, DIRSEP)
340 #endif
341
342 typedef int bool_t;
343 #define FALSE 0
344 #define TRUE 1
345
346 #define NELEM(a) (sizeof(a) / sizeof((a)[0]))
347 #define sizeofN(type, n) (sizeof(type) * (n))
348 #define BIT(i) (1<<(i)) /* define bit in flag */
349
350 /* Table flag type - needs > 16 and < 32 bits */
351 typedef INT32 Tflag;
352
353 #define NUFILE 10 /* Number of user-accessible files */
354 #define FDBASE 10 /* First file usable by Shell */
355
356 /* you're not going to run setuid shell scripts, are you? */
357 #define eaccess(path, mode) access(path, mode)
358
359 /* Make MAGIC a char that might be printed to make bugs more obvious, but
360 * not a char that is used often. Also, can't use the high bit as it causes
361 * portability problems (calling strchr(x, 0x80|'x') is error prone).
362 */
363 #define MAGIC (7)/* prefix for *?[!{,} during expand */
364 #define ISMAGIC(c) ((unsigned char)(c) == MAGIC)
365 #define NOT '!' /* might use ^ (ie, [!...] vs [^..]) */
366
367 #define LINE 1024 /* input line size */
368 #define PATH 1024 /* pathname size (todo: PATH_MAX/pathconf()) */
369 #define ARRAYMAX 1023 /* max array index */
370
371 EXTERN const char *kshname; /* $0 */
372 EXTERN pid_t kshpid; /* $$, shell pid */
373 EXTERN pid_t procpid; /* pid of executing process */
374 EXTERN int ksheuid; /* effective uid of shell */
375 EXTERN int exstat; /* exit status */
376 EXTERN int subst_exstat; /* exit status of last $(..)/`..` */
377 EXTERN const char *safe_prompt; /* safe prompt if PS1 substitution fails */
378
379
380 /*
381 * Area-based allocation built on malloc/free
382 */
383
384 typedef struct Area {
385 struct Block *freelist; /* free list */
386 } Area;
387
388 EXTERN Area aperm; /* permanent object space */
389 #define APERM &aperm
390 #define ATEMP &e->area
391
392 #ifdef MEM_DEBUG
393 # include "chmem.h" /* a debugging front end for malloc et. al. */
394 #endif /* MEM_DEBUG */
395
396 #ifdef KSH_DEBUG
397 # define kshdebug_init() kshdebug_init_()
398 # define kshdebug_printf(a) kshdebug_printf_ a
399 # define kshdebug_dump(a) kshdebug_dump_ a
400 #else /* KSH_DEBUG */
401 # define kshdebug_init()
402 # define kshdebug_printf(a)
403 # define kshdebug_dump(a)
404 #endif /* KSH_DEBUG */
405
406
407 /*
408 * parsing & execution environment
409 */
410 EXTERN struct env {
411 short type; /* enviroment type - see below */
412 short flags; /* EF_* */
413 Area area; /* temporary allocation area */
414 struct block *loc; /* local variables and functions */
415 short *savefd; /* original redirected fd's */
416 struct env *oenv; /* link to previous enviroment */
417 ksh_jmp_buf jbuf; /* long jump back to env creator */
418 struct temp *temps; /* temp files */
419 } *e;
420
421 /* struct env.type values */
422 #define E_NONE 0 /* dummy enviroment */
423 #define E_PARSE 1 /* parsing command # */
424 #define E_FUNC 2 /* executing function # */
425 #define E_INCL 3 /* including a file via . # */
426 #define E_EXEC 4 /* executing command tree */
427 #define E_LOOP 5 /* executing for/while # */
428 #define E_ERRH 6 /* general error handler # */
429 /* # indicates env has valid jbuf (see unwind()) */
430
431 /* struct env.flag values */
432 #define EF_FUNC_PARSE BIT(0) /* function being parsed */
433 #define EF_BRKCONT_PASS BIT(1) /* set if E_LOOP must pass break/continue on */
434 #define EF_FAKE_SIGDIE BIT(2) /* hack to get info from unwind to quitenv */
435
436 /* Do breaks/continues stop at env type e? */
437 #define STOP_BRKCONT(t) ((t) == E_NONE || (t) == E_PARSE \
438 || (t) == E_FUNC || (t) == E_INCL)
439 /* Do returns stop at env type e? */
440 #define STOP_RETURN(t) ((t) == E_FUNC || (t) == E_INCL)
441
442 /* values for ksh_siglongjmp(e->jbuf, 0) */
443 #define LRETURN 1 /* return statement */
444 #define LEXIT 2 /* exit statement */
445 #define LERROR 3 /* errorf() called */
446 #define LLEAVE 4 /* untrappable exit/error */
447 #define LINTR 5 /* ^C noticed */
448 #define LBREAK 6 /* break statement */
449 #define LCONTIN 7 /* continue statement */
450 #define LSHELL 8 /* return to interactive shell() */
451 #define LAEXPR 9 /* error in arithmetic expression */
452
453
454 /* option processing */
455 #define OF_CMDLINE 0x01 /* command line */
456 #define OF_SET 0x02 /* set builtin */
457 #define OF_SPECIAL 0x04 /* a special variable changing */
458 #define OF_INTERNAL 0x08 /* set internally by shell */
459 #define OF_ANY (OF_CMDLINE | OF_SET | OF_SPECIAL | OF_INTERNAL)
460
461 struct option {
462 const char *name; /* long name of option */
463 char c; /* character flag (if any) */
464 short flags; /* OF_* */
465 };
466 extern const struct option options[];
467
468 /*
469 * flags (the order of these enums MUST match the order in misc.c(options[]))
470 */
471 enum sh_flag {
472 FEXPORT = 0, /* -a: export all */
473 #ifdef BRACE_EXPAND
474 FBRACEEXPAND, /* enable {} globbing */
475 #endif
476 FBGNICE, /* bgnice */
477 FCOMMAND, /* -c: (invocation) execute specified command */
478 #ifdef EMACS
479 FEMACS, /* emacs command editing */
480 #endif
481 FERREXIT, /* -e: quit on error */
482 #ifdef EMACS
483 FGMACS, /* gmacs command editing */
484 #endif
485 FIGNOREEOF, /* eof does not exit */
486 FTALKING, /* -i: interactive */
487 FKEYWORD, /* -k: name=value anywere */
488 FLOGIN, /* -l: a login shell */
489 FMARKDIRS, /* mark dirs with / in file name completion */
490 FMONITOR, /* -m: job control monitoring */
491 FNOCLOBBER, /* -C: don't overwrite existing files */
492 FNOEXEC, /* -n: don't execute any commands */
493 FNOGLOB, /* -f: don't do file globbing */
494 FNOHUP, /* -H: don't kill running jobs when login shell exits */
495 FNOLOG, /* don't save functions in history (ignored) */
496 #ifdef JOBS
497 FNOTIFY, /* -b: asynchronous job completion notification */
498 #endif
499 FNOUNSET, /* -u: using an unset var is an error */
500 FPHYSICAL, /* -o physical: don't do logical cd's/pwd's */
501 FPOSIX, /* -o posix: be posixly correct */
502 FPRIVILEGED, /* -p: use suid_profile */
503 FRESTRICTED, /* -r: restricted shell */
504 FSTDIN, /* -s: (invocation) parse stdin */
505 FTRACKALL, /* -h: create tracked aliases for all commands */
506 FVERBOSE, /* -v: echo input */
507 #ifdef VI
508 FVI, /* vi command editing */
509 FVIRAW, /* always read in raw mode (ignored) */
510 FVISHOW8, /* display chars with 8th bit set as is (versus M-) */
511 FVITABCOMPLETE, /* enable tab as file name completion char */
512 FVIESCCOMPLETE, /* enable ESC as file name completion in command mode */
513 #endif
514 FXTRACE, /* -x: execution trace */
515 FTALKING_I, /* (internal): initial shell was interactive */
516 FNFLAGS /* (place holder: how many flags are there) */
517 };
518
519 #define Flag(f) (shell_flags[(int) (f)])
520
521 EXTERN char shell_flags [FNFLAGS];
522
523 EXTERN char null [] I__(""); /* null value for variable */
524 EXTERN char space [] I__(" ");
525 EXTERN char newline [] I__("\n");
526 EXTERN char slash [] I__("/");
527
528 enum temp_type {
529 TT_HEREDOC_EXP, /* expanded heredoc */
530 TT_HIST_EDIT /* temp file used for history editing (fc -e) */
531 };
532 typedef enum temp_type Temp_type;
533 /* temp/heredoc files. The file is removed when the struct is freed. */
534 struct temp {
535 struct temp *next;
536 struct shf *shf;
537 int pid; /* pid of process parsed here-doc */
538 Temp_type type;
539 char *name;
540 };
541
542 /*
543 * stdio and our IO routines
544 */
545
546 #define shl_spare (&shf_iob[0]) /* for c_read()/c_print() */
547 #define shl_stdout (&shf_iob[1])
548 #define shl_out (&shf_iob[2])
549 EXTERN int shl_stdout_ok;
550
551 /*
552 * trap handlers
553 */
554 typedef struct trap {
555 int signal; /* signal number */
556 const char *name; /* short name */
557 const char *mess; /* descriptive name */
558 char *trap; /* trap command */
559 int volatile set; /* trap pending */
560 int flags; /* TF_* */
561 handler_t cursig; /* current handler (valid if TF_ORIG_* set) */
562 handler_t shtrap; /* shell signal handler */
563 } Trap;
564
565 /* values for Trap.flags */
566 #define TF_SHELL_USES BIT(0) /* shell uses signal, user can't change */
567 #define TF_USER_SET BIT(1) /* user has (tried to) set trap */
568 #define TF_ORIG_IGN BIT(2) /* original action was SIG_IGN */
569 #define TF_ORIG_DFL BIT(3) /* original action was SIG_DFL */
570 #define TF_EXEC_IGN BIT(4) /* restore SIG_IGN just before exec */
571 #define TF_EXEC_DFL BIT(5) /* restore SIG_DFL just before exec */
572 #define TF_DFL_INTR BIT(6) /* when received, default action is LINTR */
573 #define TF_TTY_INTR BIT(7) /* tty generated signal (see j_waitj) */
574 #define TF_CHANGED BIT(8) /* used by runtrap() to detect trap changes */
575 #define TF_FATAL BIT(9) /* causes termination if not trapped */
576
577 /* values for setsig()/setexecsig() flags argument */
578 #define SS_RESTORE_MASK 0x3 /* how to restore a signal before an exec() */
579 #define SS_RESTORE_CURR 0 /* leave current handler in place */
580 #define SS_RESTORE_ORIG 1 /* restore original handler */
581 #define SS_RESTORE_DFL 2 /* restore to SIG_DFL */
582 #define SS_RESTORE_IGN 3 /* restore to SIG_IGN */
583 #define SS_FORCE BIT(3) /* set signal even if original signal ignored */
584 #define SS_USER BIT(4) /* user is doing the set (ie, trap command) */
585 #define SS_SHTRAP BIT(5) /* trap for internal use (CHLD,ALRM,WINCH) */
586
587 #define SIGEXIT_ 0 /* for trap EXIT */
588 #define SIGERR_ SIGNALS /* for trap ERR */
589
590 EXTERN int volatile trap; /* traps pending? */
591 EXTERN int volatile intrsig; /* pending trap interrupts executing command */
592 EXTERN int volatile fatal_trap;/* received a fatal signal */
593 #ifndef FROM_TRAP_C
594 /* Kludge to avoid bogus re-declaration of sigtraps[] error on AIX 3.2.5 */
595 extern Trap sigtraps[SIGNALS+1];
596 #endif /* !FROM_TRAP_C */
597
598
599 #ifdef KSH
600 /*
601 * TMOUT support
602 */
603 /* values for ksh_tmout_state */
604 enum tmout_enum {
605 TMOUT_EXECUTING = 0, /* executing commands */
606 TMOUT_READING, /* waiting for input */
607 TMOUT_LEAVING /* have timed out */
608 };
609 EXTERN unsigned int ksh_tmout;
610 EXTERN enum tmout_enum ksh_tmout_state I__(TMOUT_EXECUTING);
611 #endif /* KSH */
612
613
614 /* For "You have stopped jobs" message */
615 EXTERN int really_exit;
616
617
618 /*
619 * fast character classes
620 */
621 #define C_ALPHA BIT(0) /* a-z_A-Z */
622 #define C_DIGIT BIT(1) /* 0-9 */
623 #define C_LEX1 BIT(2) /* \0 \t\n|&;<>() */
624 #define C_VAR1 BIT(3) /* *@#!$-? */
625 #define C_IFSWS BIT(4) /* \t \n (IFS white space) */
626 #define C_SUBOP1 BIT(5) /* "=-+?" */
627 #define C_SUBOP2 BIT(6) /* "#%" */
628 #define C_IFS BIT(7) /* $IFS */
629 #define C_QUOTE BIT(8) /* \n\t"#$&'()*;<>?[\`| (needing quoting) */
630
631 extern short ctypes [];
632
633 #define ctype(c, t) !!(ctypes[(unsigned char)(c)]&(t))
634 #define letter(c) ctype(c, C_ALPHA)
635 #define digit(c) ctype(c, C_DIGIT)
636 #define letnum(c) ctype(c, C_ALPHA|C_DIGIT)
637
638 EXTERN int ifs0 I__(' '); /* for "$*" */
639
640
641 /* Argument parsing for built-in commands and getopts command */
642
643 /* Values for Getopt.flags */
644 #define GF_ERROR BIT(0) /* call errorf() if there is an error */
645 #define GF_PLUSOPT BIT(1) /* allow +c as an option */
646 #define GF_NONAME BIT(2) /* don't print argv[0] in errors */
647
648 /* Values for Getopt.info */
649 #define GI_MINUS BIT(0) /* an option started with -... */
650 #define GI_PLUS BIT(1) /* an option started with +... */
651 #define GI_MINUSMINUS BIT(2) /* arguments were ended with -- */
652
653 typedef struct {
654 int optind;
655 int uoptind;/* what user sees in $OPTIND */
656 char *optarg;
657 int flags; /* see GF_* */
658 int info; /* see GI_* */
659 unsigned int p; /* 0 or index into argv[optind - 1] */
660 char buf[2]; /* for bad option OPTARG value */
661 } Getopt;
662
663 EXTERN Getopt builtin_opt; /* for shell builtin commands */
664 EXTERN Getopt user_opt; /* parsing state for getopts builtin command */
665
666
667 #ifdef KSH
668 /* This for co-processes */
669
670 typedef INT32 Coproc_id; /* something that won't (realisticly) wrap */
671 struct coproc {
672 int read; /* pipe from co-process's stdout */
673 int readw; /* other side of read (saved temporarily) */
674 int write; /* pipe to co-process's stdin */
675 Coproc_id id; /* id of current output pipe */
676 int njobs; /* number of live jobs using output pipe */
677 void *job; /* 0 or job of co-process using input pipe */
678 };
679 EXTERN struct coproc coproc;
680 #endif /* KSH */
681
682 /* Used in jobs.c and by coprocess stuff in exec.c */
683 #ifdef JOB_SIGS
684 EXTERN sigset_t sm_default, sm_sigchld;
685 #endif /* JOB_SIGS */
686
687 extern const char ksh_version[];
688
689 /* name of called builtin function (used by error functions) */
690 EXTERN char *builtin_argv0;
691 EXTERN Tflag builtin_flag; /* flags of called builtin (SPEC_BI, etc.) */
692
693 /* current working directory, and size of memory allocated for same */
694 EXTERN char *current_wd;
695 EXTERN int current_wd_size;
696
697 #ifdef EDIT
698 /* Minimium required space to work with on a line - if the prompt leaves less
699 * space than this on a line, the prompt is truncated.
700 */
701 # define MIN_EDIT_SPACE 7
702 /* Minimium allowed value for x_cols: 2 for prompt, 3 for " < " at end of line
703 */
704 # define MIN_COLS (2 + MIN_EDIT_SPACE + 3)
705 EXTERN int x_cols I__(80); /* tty columns */
706 #else
707 # define x_cols 80 /* for pr_menu(exec.c) */
708 #endif
709
710
711 /* These to avoid bracket matching problems */
712 #define OPAREN '('
713 #define CPAREN ')'
714 #define OBRACK '['
715 #define CBRACK ']'
716 #define OBRACE '{'
717 #define CBRACE '}'
718
719 /* Determine the location of the system (common) profile */
720 #ifndef KSH_SYSTEM_PROFILE
721 # ifdef __NeXT
722 # define KSH_SYSTEM_PROFILE "/etc/profile.std"
723 # else /* __NeXT */
724 # define KSH_SYSTEM_PROFILE "/etc/profile"
725 # endif /* __NeXT */
726 #endif /* KSH_SYSTEM_PROFILE */
727
728 /* Used by v_evaluate() and setstr() to control action when error occurs */
729 #define KSH_UNWIND_ERROR 0 /* unwind the stack (longjmp) */
730 #define KSH_RETURN_ERROR 1 /* return 1/0 for success/failure */
731
732 #include "shf.h"
733 #include "table.h"
734 #include "tree.h"
735 #include "expand.h"
736 #include "lex.h"
737 #include "proto.h"
738
739 /* be sure not to interfere with anyone else's idea about EXTERN */
740 #ifdef EXTERN_DEFINED
741 # undef EXTERN_DEFINED
742 # undef EXTERN
743 #endif
744 #undef I__
745