home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / k / ksh48.zip / sh / sh.h < prev    next >
C/C++ Source or Header  |  1992-09-09  |  8KB  |  320 lines

  1. /*
  2.  * Public Domain Bourne/Korn shell
  3.  */
  4.  
  5. /* $Id: sh.h,v 1.5 1992/08/12 14:15:48 sjg Exp $ */
  6.  
  7. #include "config.h"
  8.  
  9. /* some useful #defines */
  10. #ifdef EXTERN
  11. # define _I_(i) = i
  12. #else
  13. # define _I_(i)
  14. # define EXTERN extern
  15. # define EXTERN_DEFINED
  16. #endif
  17.  
  18. #ifndef SHELL
  19. #define    SHELL    "cmd.exe"    /* shell to exec scripts */
  20. #endif
  21.  
  22. #ifdef _BSD
  23. #define memmove(dst, src, n)    bcopy((src), (dst), (n))
  24. #endif
  25.  
  26. /* some people object to this use of __STDC__ */
  27. #ifdef __STDC__
  28. #define    ARGS(args)    args    /* prototype declaration */
  29. #else
  30. #define    ARGS(args)    ()    /* K&R declaration */
  31. #ifdef VOID
  32. #define    void    VOID
  33. #endif
  34. #define    const
  35. #define    volatile
  36. #endif
  37.  
  38. #ifdef _ULTRIX            /* Ultrix 2.x gets dup2 wrong */
  39. int dup2 ARGS ((int, int));
  40. /* assumes we don't want dup2 return value */
  41. #define    dup2(ofd, nfd) \
  42.         (void) ((dup2)(ofd, nfd), fcntl(nfd, F_SETFD, 0))
  43. #endif
  44.  
  45. #if defined(EMACS) || defined(VI)
  46. #define    EDIT
  47. #endif
  48.  
  49. typedef int bool_t;
  50. #define    FALSE    0
  51. #define    TRUE    1
  52.  
  53. #define    sizeofN(type, n) (sizeof(type) * (n))
  54. #define    BIT(i)    (1<<(i))    /* define bit in flag */
  55.  
  56. #define    NUFILE    10        /* Number of user-accessible files */
  57. #define    FDBASE    10        /* First file usable by Shell */
  58.  
  59. /* you're not going to run setuid shell scripts, are you? */
  60. #define    eaccess(path, mode)    (path ? access(path, mode) : -1)
  61.  
  62. #define    MAGIC    (char)0x80    /* prefix for ~*?[ during expand */
  63. #define    NOT    '!'    /* might use ^ */
  64.  
  65. #define    LINE    256        /* input line size */
  66. #define    PATH    256        /* pathname size */
  67.  
  68. EXTERN    int    kshpid;        /* $$, shell pid */
  69. EXTERN    int    exstat;        /* exit status */
  70. EXTERN    int    async;        /* $!, last &'d pid */
  71. EXTERN    volatile int sigchld_caught;    /* count of dead children */
  72.  
  73.  
  74. /*
  75.  * Area-based allocation built on malloc/free
  76.  */
  77.  
  78. typedef struct Area {
  79.     struct Block *free;    /* free list */
  80. } Area;
  81.  
  82. extern    Area    aperm;        /* permanent object space */
  83. #define    APERM    &aperm
  84. #define    ATEMP    &e.area
  85.  
  86. /*
  87.  * parsing & execution environment
  88.  */
  89. EXTERN    struct    env {
  90.     int    type;            /* enviroment type - see below */
  91.     Area    area;            /* temporary allocation area */
  92.     struct    block *loc;        /* local variables and functions */
  93.     short  *savefd;            /* original redirected fd's */
  94.     struct    env *oenv;        /* link to previous enviroment */
  95.     jmp_buf    jbuf;            /* long jump back to env creator */
  96.     int    interactive;        /* fd's 0,1,2 are tty */
  97.     struct temp *temps;        /* temp files */
  98. } e;
  99.  
  100. #define    E_NONE    0        /* dummy enviroment */
  101. #define    E_PARSE    1        /* parsing command # */
  102. #define    E_EXEC    2        /* executing command tree */
  103. #define    E_LOOP    3        /* executing for/while # */
  104. #define    E_TCOM    5        /* executing simple command */
  105. #define    E_FUNC    6        /* executing function */
  106. #define    E_ERRH    7        /* general error handler # */
  107. /* # indicates env has valid jbuf */
  108.  
  109. /*
  110.  * flags
  111.  */
  112. #define    FEXPORT    FLAG('a')    /* -a: allexport */
  113. #define    FERREXIT FLAG('e')    /* -e: errexit (quit on error) */
  114. #define    FBGNICE    29        /* bgnice */
  115. #define    FEMACS 30        /* emacs command editing */
  116. #define    FIGNEOF    27        /* ignoreeof (eof does not exit) */
  117. #define    FHASHALL FLAG('h')    /* -h: trackall, hashall */
  118. #define    FTALKING FLAG('i')    /* -i: interactive (talking type wireless) */
  119. #define    FKEYWORD FLAG('k')    /* -k: keyword (name=value anywhere) */
  120. #define    FMARKDIRS 28        /* markdirs */
  121. #define    FMONITOR FLAG('m')    /* -m: monitor */
  122. #define    FNOEXEC    FLAG('n')    /* -n: noexec */
  123. #define    FNOGLOB    FLAG('f')    /* -f: noglob */
  124. #define    FPRIVILEGED FLAG('p')    /* -p: privileged */
  125. #define    FSTDIN    FLAG('s')    /* -s (invocation): parse stdin */
  126. #define    FNOUNSET FLAG('u')    /* -u: nounset (unset vars is error) */
  127. #define    FVERBOSE FLAG('v')    /* -v: verbose (echo input) */
  128. #define    FVI 31            /* vi command editing */
  129. #define    FXTRACE    FLAG('x')    /* -x: (execute) xtrace */
  130.  
  131. #define    FLAG(c)    (1 + c - 'a')    /* map char to flags index */
  132. #define    FLAGS    32
  133. EXTERN    char flag [FLAGS];
  134. int    option ARGS((const char *name));
  135. char   *getoptions ARGS((void));
  136. void    printoptions ARGS((void));
  137.  
  138. extern    char    null [];    /* null value for variable */
  139.  
  140. /*
  141.  * other functions
  142.  */
  143. char * substitute ARGS((char const *, int));
  144. char   *search();
  145. struct tbl *findcom();
  146. char   *strsave ARGS((char *, Area *));
  147. char   *ulton ARGS((unsigned long n, int base));
  148. int    xstrcmp();
  149. void    qsortp ARGS((void **base, size_t n, int (*compare)(void *, void *)));
  150. long    evaluate ARGS((const char *expr));
  151. void    resetopts();
  152. void    histsave();
  153. void    histlist();
  154. int    pr_menu ARGS((char **, int));
  155.  
  156. void    j_init ARGS((void));
  157. void    j_exit ARGS((void));
  158. void    j_notify ARGS((void));
  159. void    j_kill ARGS((int job, int sig));
  160. #ifdef JOBS
  161. void    j_change ARGS((void));
  162. int    j_resume ARGS((int job, int bg));
  163. #endif
  164.  
  165. /*
  166.  * error handling
  167.  */
  168. void    leave();    /* abort shell (or fail in subshell) */
  169.  
  170. /*
  171.  * library functions
  172.  */
  173. typedef    void (*handler_t)();    /* signal handler */
  174.  
  175. /* temp/here files. the file is removed when the struct is freed */
  176. struct    temp {
  177.     struct    temp * next;
  178.     char   *name;
  179. };
  180. struct temp *maketemp ARGS((Area *ap));
  181.  
  182. /*
  183.  * stdio and our IO routines
  184.  */
  185.  
  186. #ifdef    BUFSIZ            /* <stdio.h> included? */
  187. extern    FILE *    shf [NUFILE];    /* map shell fd to FILE */
  188. #endif
  189. void    fopenshf();
  190. void    flushshf();
  191.  
  192. #undef    stdin
  193. #undef    stdout
  194.  
  195. #define    stdin    shf[0]        /* standard input */
  196. #define    stdout    shf[1]        /* standard output */
  197. #define    shlout    shf[2]        /* shell output */
  198.  
  199. int    shellf ARGS((const char *fmt, ...)); /* fprintf(shlout, ); */
  200. int    errorf ARGS((const char *fmt, ...)); /* fprintf(shlout, ); error(); */
  201.  
  202. /*
  203.  * IO control
  204.  */
  205. extern    int ttyfd;        /* tty fd (original fd 0) */
  206.  
  207. int    savefd ARGS((int fd));    /* save user fd */
  208. void    restfd ARGS((int fd, int ofd));
  209. void    openpipe ARGS((int [2]));
  210. void    closepipe ARGS((int [2]));
  211.  
  212. /*
  213.  * trap handlers
  214.  */
  215. typedef struct trap {
  216.     int    signal;        /* signal number */
  217.     char   *name;        /* short name */
  218.     char   *mess;        /* descriptive name */
  219.     char   *trap;        /* trap command */
  220.     int    volatile set;    /* trap pending */
  221.     int    ourtrap;    /* not ignored (?) */
  222.     int    sig_dfl;    /* originally SIG_DFL */
  223. } Trap;
  224.  
  225. #ifndef    SIGKILL
  226. #include <signal.h>
  227. #endif    /* SIGKILL */
  228. #ifdef    NSIG
  229. #define    SIGNALS    NSIG
  230. #else
  231. #ifdef    _MINIX
  232. #define    SIGNALS    (_NSIG+1)    /* _NSIG is # of signals used, excluding 0. */
  233. #else
  234. #define    SIGNALS    32
  235. #endif    /* _MINIX */
  236. #endif    /* NSIG */
  237.  
  238. #ifdef USE_SIGACT            /* always use it? */
  239. #ifndef  SA_NOCLDSTOP
  240. # include "sigact.h"            /* use sjg's fake sigaction() */
  241. #endif
  242. EXTERN struct sigaction Sigact, Sigact_dfl, Sigact_ign, Sigact_trap;
  243. #endif
  244.  
  245. EXTERN    int volatile trap;    /* traps pending? */
  246. extern    Trap    sigtraps[SIGNALS];
  247. Trap    *gettrap ARGS((char *)); /* search for struct trap by number or name */
  248. void    trapsig ARGS((int sig)); /* trap signal handler */
  249.  
  250. /*
  251.  * fast character classes
  252.  */
  253. #define    C_ALPHA    0x01        /* a-z_A-Z */
  254. #define    C_DIGIT    0x02        /* 0-9 */
  255. #define    C_LEX1    0x04        /* \0 \t\n|&;<>() */
  256. #define    C_VAR1    0x08        /* *@#!$-? */
  257. #define    C_SUBOP    0x40        /* "=-+?#%" */
  258. #define    C_IFS    0x80        /* $IFS */
  259.  
  260. extern    char ctypes [];
  261. #if 0
  262. void    initctypes ARGS((void));
  263. void    setctypes ARGS((const char*, int type));
  264. #endif
  265.  
  266. #define    ctype(c, t)    !!(ctypes[(unsigned char)(c)]&(t))
  267. #define    letter(c)    ctype(c, C_ALPHA)
  268. #define    digit(c)    ctype(c, C_DIGIT)
  269. #define    letnum(c)    ctype(c, C_ALPHA|C_DIGIT)
  270.  
  271. #include "table.h"
  272. #include "tree.h"
  273. #include "lex.h"
  274. #include "proto.h"
  275.  
  276. #ifndef offsetof
  277. #define        offsetof(type,id) ((size_t)&((type*)NULL)->id)
  278. #endif
  279.  
  280. /*
  281.  * 91-07-06 <sjg@sun0>
  282.  * use my simple debug tracing...
  283.  */
  284. #include "trace.h"
  285.  
  286. #ifndef fileno
  287. #define fileno(p)    ((p)->handle)
  288. #endif
  289.  
  290. /* be sure not to interfere with anyone else's idea about EXTERN */
  291. #ifdef EXTERN_DEFINED
  292. # undef EXTERN_DEFINED
  293. # undef EXTERN
  294. #endif
  295. #undef _I_
  296. /*
  297.  * Local Variables:
  298.  * version-control:t
  299.  * comment-column:40
  300.  * End:
  301.  */
  302.  
  303. #ifdef OS2
  304. #define PATHSEP        ';'
  305. #define DIRSEP         '\\'
  306. #define DIRSEPSTR      "\\"
  307. #define ISDIRSEP(c)    ((c) == '\\' || (c) == '/')
  308. extern char *index_sep(char *path);
  309. extern char *rindex_sep(char *path);
  310. #define chdir          _chdir2
  311. #define getcwd         _getcwd2
  312. #else
  313. #define PATHSEP        ':'
  314. #define DIRSEP         '/'
  315. #define DIRSEPSTR      "/"
  316. #define ISDIRSEP(c)    ((c) == '/')
  317. #define index_sep(p)   strchr(p, DIRSEP)
  318. #define rindex_sep(p)  strrchr(p, DIRSEP)
  319. #endif
  320.