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