sh.h 22 KB

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