home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Shells / zsh-3.0.5-MIHS / src / Src / zsh.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-25  |  39.1 KB  |  1,385 lines

  1. /*
  2.  * $Id: zsh.h,v 2.49 1996/10/15 20:16:35 hzoli Exp $
  3.  *
  4.  * zsh.h - standard header file
  5.  *
  6.  * This file is part of zsh, the Z shell.
  7.  *
  8.  * Copyright (c) 1992-1996 Paul Falstad
  9.  * All rights reserved.
  10.  *
  11.  * Permission is hereby granted, without written agreement and without
  12.  * license or royalty fees, to use, copy, modify, and distribute this
  13.  * software and to distribute modified versions of this software for any
  14.  * purpose, provided that the above copyright notice and the following
  15.  * two paragraphs appear in all copies of this software.
  16.  *
  17.  * In no event shall Paul Falstad or the Zsh Development Group be liable
  18.  * to any party for direct, indirect, special, incidental, or consequential
  19.  * damages arising out of the use of this software and its documentation,
  20.  * even if Paul Falstad and the Zsh Development Group have been advised of
  21.  * the possibility of such damage.
  22.  *
  23.  * Paul Falstad and the Zsh Development Group specifically disclaim any
  24.  * warranties, including, but not limited to, the implied warranties of
  25.  * merchantability and fitness for a particular purpose.  The software
  26.  * provided hereunder is on an "as is" basis, and Paul Falstad and the
  27.  * Zsh Development Group have no obligation to provide maintenance,
  28.  * support, updates, enhancements, or modifications.
  29.  *
  30.  */
  31.  
  32. /* We use <config.h> instead of "config.h" so that a compilation        *
  33.  * using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h *
  34.  * (which it would do because it found this file in $srcdir).           */
  35. #include <config.h>
  36. #include <system.h>
  37.  
  38. /* A few typical macros */
  39. #define minimum(a,b)  ((a) < (b) ? (a) : (b))
  40.  
  41. /* math.c */
  42. typedef int LV;
  43.  
  44. #include "zle.h"
  45.  
  46. /* Character tokens are sometimes casted to (unsigned char)'s.         * 
  47.  * Unfortunately, some compilers don't correctly cast signed to        * 
  48.  * unsigned promotions; i.e. (int)(unsigned char)((char) -1) evaluates * 
  49.  * to -1, instead of 255 like it should.  We circumvent the troubles   * 
  50.  * of such shameful delinquency by casting to a larger unsigned type   * 
  51.  * then back down to unsigned char.                                    */
  52.  
  53. #ifdef BROKEN_SIGNED_TO_UNSIGNED_CASTING
  54. # define STOUC(X)    ((unsigned char)(unsigned short)(X))
  55. #else
  56. # define STOUC(X)    ((unsigned char)(X))
  57. #endif
  58.  
  59. /* Meta together with the character following Meta denotes the character *
  60.  * which is the exclusive or of 32 and the character following Meta.     *
  61.  * This is used to represent characters which otherwise has special      *
  62.  * meaning for zsh.  These are the characters for which the imeta() test *
  63.  * is true: the null character, and the characters from Meta to Marker.  */
  64.  
  65. #define Meta        ((char) 0x83)
  66.  
  67. /* Note that the fourth character in DEFAULT_IFS is Meta *
  68.  * followed by a space which denotes the null character. */
  69.  
  70. #define DEFAULT_IFS    " \t\n\203 "
  71.  
  72. /* Character tokens */
  73. #define Pound        ((char) 0x84)
  74. #define String        ((char) 0x85)
  75. #define Hat        ((char) 0x86)
  76. #define Star        ((char) 0x87)
  77. #define Inpar        ((char) 0x88)
  78. #define Outpar        ((char) 0x89)
  79. #define Qstring            ((char) 0x8a)
  80. #define Equals        ((char) 0x8b)
  81. #define Bar              ((char) 0x8c)
  82. #define Inbrace            ((char) 0x8d)
  83. #define Outbrace    ((char) 0x8e)
  84. #define Inbrack            ((char) 0x8f)
  85. #define Outbrack    ((char) 0x90)
  86. #define Tick        ((char) 0x91)
  87. #define Inang        ((char) 0x92)
  88. #define Outang        ((char) 0x93)
  89. #define Quest        ((char) 0x94)
  90. #define Tilde        ((char) 0x95)
  91. #define Qtick        ((char) 0x96)
  92. #define Comma        ((char) 0x97)
  93. #define Snull        ((char) 0x98)
  94. #define Dnull        ((char) 0x99)
  95. #define Bnull        ((char) 0x9a)
  96. #define Nularg        ((char) 0x9b)
  97.  
  98. #define INULL(x)    (((x) & 0xfc) == 0x98)
  99.  
  100. /* Marker used in paramsubst for rc_expand_param */
  101. #define Marker        ((char) 0x9c)
  102.  
  103. /* chars that need to be quoted if meant literally */
  104.  
  105. #define SPECCHARS "#$^*()=|{}[]`<>?~;&\n\t \\\'\""
  106.  
  107. enum {
  108.     NULLTOK,        /* 0  */
  109.     SEPER,
  110.     NEWLIN,
  111.     SEMI,
  112.     DSEMI,
  113.     AMPER,        /* 5  */
  114.     INPAR,
  115.     OUTPAR,
  116.     DBAR,
  117.     DAMPER,
  118.     OUTANG,        /* 10 */
  119.     OUTANGBANG,
  120.     DOUTANG,
  121.     DOUTANGBANG,
  122.     INANG,
  123.     INOUTANG,        /* 15 */
  124.     DINANG,
  125.     DINANGDASH,
  126.     INANGAMP,
  127.     OUTANGAMP,
  128.     AMPOUTANG,        /* 20 */
  129.     OUTANGAMPBANG,
  130.     DOUTANGAMP,
  131.     DOUTANGAMPBANG,
  132.     TRINANG,
  133.     BAR,        /* 25 */
  134.     BARAMP,
  135.     INOUTPAR,
  136.     DINPAR,
  137.     DOUTPAR,
  138.     AMPERBANG,        /* 30 */
  139.     DOUTBRACK,
  140.     STRING,
  141.     ENVSTRING,
  142.     ENVARRAY,
  143.     ENDINPUT,        /* 35 */
  144.     LEXERR,
  145.  
  146.     /* Tokens for reserved words */
  147.     BANG,    /* !         */
  148.     DINBRACK,    /* [[        */
  149.     INBRACE,    /* {         */
  150.     OUTBRACE,   /* }         */    /* 40 */
  151.     CASE,    /* case      */
  152.     COPROC,    /* coproc    */
  153.     DO,        /* do        */
  154.     DONE,    /* done      */
  155.     ELIF,    /* elif      */ /* 45 */
  156.     ELSE,    /* else      */
  157.     ZEND,    /* end       */
  158.     ESAC,    /* esac      */
  159.     FI,        /* fi        */
  160.     FOR,    /* for       */ /* 50 */
  161.     FOREACH,    /* foreach   */
  162.     FUNC,    /* function  */
  163.     IF,        /* if        */
  164.     NOCORRECT,    /* nocorrect */
  165.     REPEAT,    /* repeat    */ /* 55 */
  166.     SELECT,    /* select    */
  167.     THEN,    /* then      */
  168.     TIME,    /* time      */
  169.     UNTIL,    /* until     */
  170.     WHILE    /* while     */ /* 60 */
  171. };
  172.  
  173. /* Redirection types.  If you modify this, you may also have to modify *
  174.  * redirtab in globals.h and getredirs() in text.c and the IS_* macros *
  175.  * below.                                                              */
  176.  
  177. enum {
  178.     WRITE,        /* > */
  179.     WRITENOW,        /* >| */
  180.     APP,        /* >> */
  181.     APPNOW,        /* >>| */
  182.     ERRWRITE,        /* &>, >& */
  183.     ERRWRITENOW,    /* >&| */
  184.     ERRAPP,        /* >>& */
  185.     ERRAPPNOW,        /* >>&| */
  186.     READ,        /* < */
  187.     READWRITE,        /* <> */
  188.     HEREDOC,        /* << */
  189.     HEREDOCDASH,    /* <<- */
  190.     HERESTR,        /* <<< */
  191.     MERGEIN,        /* <&n */
  192.     MERGEOUT,        /* >&n */
  193.     CLOSE,        /* >&-, <&- */
  194.     INPIPE,        /* < <(...) */
  195.     OUTPIPE        /* > >(...) */
  196. };
  197.  
  198. #define IS_APPEND_REDIR(X)    ((X)>=WRITE && (X)<=ERRAPPNOW && ((X) & 2))
  199. #define IS_CLOBBER_REDIR(X)   ((X)>=WRITE && (X)<=ERRAPPNOW && ((X) & 1))
  200. #define IS_ERROR_REDIR(X)     ((X)>=ERRWRITE && (X)<=ERRAPPNOW)
  201. #define IS_READFD(X)          (((X)>=READ && (X)<=MERGEIN) || (X)==INPIPE)
  202. #define IS_REDIROP(X)         ((X)>=OUTANG && (X)<=TRINANG)
  203.  
  204. /* Flags for input stack */
  205. #define INP_FREE      (1<<0)    /* current buffer can be free'd            */
  206. #define INP_ALIAS     (1<<1)    /* expanding alias or history              */
  207. #define INP_HIST      (1<<2)    /* expanding history                       */
  208. #define INP_CONT      (1<<3)    /* continue onto previously stacked input  */
  209. #define INP_ALCONT    (1<<4)    /* stack is continued from alias expn.     */
  210.  
  211. /* Flags for metafy */
  212. #define META_REALLOC    0
  213. #define META_USEHEAP    1
  214. #define META_STATIC    2
  215. #define META_DUP    3
  216. #define META_ALLOC    4
  217. #define META_NOALLOC    5
  218. #define META_HEAPDUP    6
  219.  
  220.  
  221. /**************************/
  222. /* Abstract types for zsh */
  223. /**************************/
  224.  
  225. typedef struct linknode  *LinkNode;
  226. typedef struct linklist  *LinkList;
  227. typedef struct hashnode  *HashNode;
  228. typedef struct hashtable *HashTable;
  229.  
  230. typedef struct reswd     *Reswd;
  231. typedef struct alias     *Alias;
  232. typedef struct param     *Param;
  233. typedef struct cmdnam    *Cmdnam;
  234. typedef struct shfunc    *Shfunc;
  235. typedef struct builtin   *Builtin;
  236. typedef struct nameddir  *Nameddir;
  237.  
  238. typedef struct schedcmd  *Schedcmd;
  239. typedef struct process   *Process;
  240. typedef struct job       *Job;
  241. typedef struct value     *Value;
  242. typedef struct varasg    *Varasg;
  243. typedef struct cond      *Cond;
  244. typedef struct cmd       *Cmd;
  245. typedef struct pline     *Pline;
  246. typedef struct sublist   *Sublist;
  247. typedef struct list      *List;
  248. typedef struct comp      *Comp;
  249. typedef struct redir     *Redir;
  250. typedef struct complist  *Complist;
  251. typedef struct heap      *Heap;
  252. typedef struct heapstack *Heapstack;
  253. typedef struct histent   *Histent;
  254. typedef struct compctlp  *Compctlp;
  255. typedef struct compctl   *Compctl;
  256. typedef struct compcond  *Compcond;
  257. typedef struct forcmd    *Forcmd;
  258.  
  259. typedef struct asgment  *Asgment;
  260.  
  261.  
  262. /********************************/
  263. /* Definitions for linked lists */
  264. /********************************/
  265.  
  266. /* linked list abstract data type */
  267.  
  268. struct linknode {
  269.     LinkNode next;
  270.     LinkNode last;
  271.     void *dat;
  272. };
  273.  
  274. struct linklist {
  275.     LinkNode first;
  276.     LinkNode last;
  277. };
  278.  
  279. /* Macros for manipulating link lists */
  280.  
  281. #define addlinknode(X,Y) insertlinknode(X,(X)->last,Y)
  282. #define empty(X)     ((X)->first == NULL)
  283. #define nonempty(X)  ((X)->first != NULL)
  284. #define firstnode(X) ((X)->first)
  285. #define getaddrdata(X) (&((X)->dat))
  286. #define getdata(X)   ((X)->dat)
  287. #define setdata(X,Y) ((X)->dat = (Y))
  288. #define lastnode(X)  ((X)->last)
  289. #define nextnode(X)  ((X)->next)
  290. #define prevnode(X)  ((X)->last)
  291. #define peekfirst(X) ((X)->first->dat)
  292. #define pushnode(X,Y) insertlinknode(X,(LinkNode) X,Y)
  293. #define incnode(X) (X = nextnode(X))
  294. #define gethistent(X) (histentarr+((X)%histentct))
  295.  
  296.  
  297. /********************************/
  298. /* Definitions for syntax trees */
  299. /********************************/
  300.  
  301. /* struct list, struct sublist, struct pline, etc.  all fit the form *
  302.  * of this structure and are used interchangably. The ptrs may hold  *
  303.  * integers or pointers, depending on the type of the node.          */
  304.  
  305. /* Generic node structure for syntax trees */
  306. struct node {
  307.     int ntype;            /* node type */
  308. };
  309.  
  310. #define N_LIST    0
  311. #define N_SUBLIST 1
  312. #define N_PLINE   2
  313. #define N_CMD     3
  314. #define N_REDIR   4
  315. #define N_COND    5
  316. #define N_FOR     6
  317. #define N_CASE    7
  318. #define N_IF      8
  319. #define N_WHILE   9
  320. #define N_VARASG 10
  321. #define N_COUNT  11
  322.  
  323. /* values for types[4] */
  324.  
  325. #define NT_EMPTY 0
  326. #define NT_NODE  1
  327. #define NT_STR   2
  328. #define NT_LIST  4
  329. #define NT_ARR   8
  330.  
  331. #define NT_TYPE(T) ((T) & 0xff)
  332. #define NT_N(T, N) (((T) >> (8 + (N) * 4)) & 0xf)
  333. #define NT_SET(T0, N, T1, T2, T3, T4) \
  334.     ((T0) | ((N) << 24) | \
  335.      ((T1) << 8) | ((T2) << 12) | ((T3) << 16) | ((T4) << 20))
  336. #define NT_NUM(T) (((T) >> 24) & 7)
  337. #define NT_HEAP   (1 << 30)
  338.  
  339. /* tree element for lists */
  340.  
  341. struct list {
  342.     int ntype;            /* node type */
  343.     int type;
  344.     Sublist left;
  345.     List right;
  346. };
  347.  
  348. /* These are control flags that are passed *
  349.  * down the execution pipeline.            */
  350. #define Z_TIMED    (1<<0)    /* pipeline is being timed                   */
  351. #define Z_SYNC    (1<<1)    /* run this sublist synchronously       (;)  */
  352. #define Z_ASYNC    (1<<2)    /* run this sublist asynchronously      (&)  */
  353. #define Z_DISOWN (1<<3)    /* run this sublist without job control (&|) */
  354.  
  355. /* tree element for sublists */
  356.  
  357. struct sublist {
  358.     int ntype;            /* node type */
  359.     int type;
  360.     int flags;            /* see PFLAGs below */
  361.     Pline left;
  362.     Sublist right;
  363. };
  364.  
  365. #define ORNEXT  10        /* || */
  366. #define ANDNEXT 11        /* && */
  367.  
  368. #define PFLAG_NOT     1        /* ! ... */
  369. #define PFLAG_COPROC 32        /* coproc ... */
  370.  
  371. /* tree element for pipes */
  372.  
  373. struct pline {
  374.     int ntype;            /* node type */
  375.     int type;
  376.     Cmd left;
  377.     Pline right;
  378. };
  379.  
  380. #define END    0        /* pnode *right is null                     */
  381. #define PIPE    1        /* pnode *right is the rest of the pipeline */
  382.  
  383. /* tree element for commands */
  384.  
  385. struct cmd {
  386.     int ntype;            /* node type                    */
  387.     int type;
  388.     int flags;            /* see CFLAGs below             */
  389.     int lineno;            /* lineno of script for command */
  390.     union {
  391.     List list;        /* for SUBSH/CURSH/SHFUNC       */
  392.     Forcmd forcmd;
  393.     struct casecmd *casecmd;
  394.     struct ifcmd *ifcmd;
  395.     struct whilecmd *whilecmd;
  396.     Sublist pline;
  397.     Cond cond;
  398.     void *generic;
  399.     } u;
  400.     LinkList args;        /* command & argmument List (char *'s)   */
  401.     LinkList redir;        /* i/o redirections (struct redir *'s)   */
  402.     LinkList vars;        /* param assignments (struct varasg *'s) */
  403. };
  404.  
  405. /* cmd types */
  406. #define SIMPLE   0
  407. #define SUBSH    1
  408. #define CURSH    2
  409. #define ZCTIME   3
  410. #define FUNCDEF  4
  411. #define CFOR     5
  412. #define CWHILE   6
  413. #define CREPEAT  7
  414. #define CIF      8
  415. #define CCASE    9
  416. #define CSELECT 10
  417. #define COND    11
  418. #define CARITH  12
  419.  
  420. /* flags for command modifiers */
  421. #define CFLAG_EXEC    (1<<0)    /* exec ...    */
  422.  
  423. /* tree element for redirection lists */
  424.  
  425. struct redir {
  426.     int ntype;            /* node type */
  427.     int type;
  428.     int fd1, fd2;
  429.     char *name;
  430. };
  431.  
  432. /* tree element for conditionals */
  433.  
  434. struct cond {
  435.     int ntype;        /* node type                     */
  436.     int type;        /* can be cond_type, or a single */
  437.             /* letter (-a, -b, ...)          */
  438.     void *left, *right;
  439. };
  440.  
  441. #define COND_NOT    0
  442. #define COND_AND    1
  443. #define COND_OR     2
  444. #define COND_STREQ  3
  445. #define COND_STRNEQ 4
  446. #define COND_STRLT  5
  447. #define COND_STRGTR 6
  448. #define COND_NT     7
  449. #define COND_OT     8
  450. #define COND_EF     9
  451. #define COND_EQ    10
  452. #define COND_NE    11
  453. #define COND_LT    12
  454. #define COND_GT    13
  455. #define COND_LE    14
  456. #define COND_GE    15
  457.  
  458. struct forcmd {            /* for/select */
  459. /* Cmd->args contains list of words to loop thru */
  460.     int ntype;            /* node type                          */
  461.     int inflag;            /* if there is an in ... clause       */
  462.     char *name;            /* parameter to assign values to      */
  463.     List list;            /* list to look through for each name */
  464. };
  465.  
  466. struct casecmd {
  467. /* Cmd->args contains word to test */
  468.     int ntype;            /* node type       */
  469.     char **pats;
  470.     List *lists;        /* list to execute */
  471. };
  472.  
  473.  
  474. /*  A command like "if foo then bar elif baz then fubar else fooble"  */
  475. /*  generates a tree like:                                            */
  476. /*                                                                    */
  477. /*  struct ifcmd a = { next =  &b,  ifl = "foo", thenl = "bar" }      */
  478. /*  struct ifcmd b = { next =  &c,  ifl = "baz", thenl = "fubar" }    */
  479. /*  struct ifcmd c = { next = NULL, ifl = NULL, thenl = "fooble" }    */
  480.  
  481. struct ifcmd {
  482.     int ntype;            /* node type */
  483.     List *ifls;
  484.     List *thenls;
  485. };
  486.  
  487. struct whilecmd {
  488.     int ntype;            /* node type                           */
  489.     int cond;            /* 0 for while, 1 for until            */
  490.     List cont;            /* condition                           */
  491.     List loop;            /* list to execute until condition met */
  492. };
  493.  
  494. /* The number of fds space is allocated for  *
  495.  * each time a multio must increase in size. */
  496. #define MULTIOUNIT 8
  497.  
  498. /* A multio is a list of fds associated with a certain fd.       *
  499.  * Thus if you do "foo >bar >ble", the multio for fd 1 will have *
  500.  * two fds, the result of open("bar",...), and the result of     *
  501.  * open("ble",....).                                             */
  502.  
  503. /* structure used for multiple i/o redirection */
  504. /* one for each fd open                        */
  505.  
  506. struct multio {
  507.     int ct;            /* # of redirections on this fd                 */
  508.     int rflag;            /* 0 if open for reading, 1 if open for writing */
  509.     int pipe;            /* fd of pipe if ct > 1                         */
  510.     int fds[MULTIOUNIT];    /* list of src/dests redirected to/from this fd */
  511. };
  512.  
  513. /* variable assignment tree element */
  514.  
  515. struct varasg {
  516.     int ntype;            /* node type                             */
  517.     int type;            /* nonzero means array                   */
  518.     char *name;
  519.     char *str;            /* should've been a union here.  oh well */
  520.     LinkList arr;
  521. };
  522.  
  523. /* lvalue for variable assignment/expansion */
  524.  
  525. struct value {
  526.     int isarr;
  527.     Param pm;        /* parameter node                      */
  528.     int inv;        /* should we return the index ?        */
  529.     int a;        /* first element of array slice, or -1 */
  530.     int b;        /* last element of array slice, or -1  */
  531. };
  532.  
  533. /* structure for foo=bar assignments */
  534.  
  535. struct asgment {
  536.     struct asgment *next;
  537.     char *name;
  538.     char *value;
  539. };
  540.  
  541. #define MAX_ARRLEN    262144
  542.  
  543.  
  544. /********************************************/
  545. /* Defintions for job table and job control */
  546. /********************************************/
  547.  
  548. /* size of job table */
  549. #define MAXJOB 50
  550.  
  551. /* entry in the job table */
  552.  
  553. struct job {
  554.     pid_t gleader;        /* process group leader of this job  */
  555.     pid_t other;        /* subjob id or subshell pid         */
  556.     int stat;                   /* see STATs below                   */
  557.     char *pwd;            /* current working dir of shell when *
  558.                  * this job was spawned              */
  559.     struct process *procs;    /* list of processes                 */
  560.     LinkList filelist;        /* list of files to delete when done */
  561.     int stty_in_env;        /* if STTY=... is present            */
  562.     struct ttyinfo *ty;        /* the modes specified by STTY       */
  563. };
  564.  
  565. #define STAT_CHANGED    (1<<0)    /* status changed and not reported      */
  566. #define STAT_STOPPED    (1<<1)    /* all procs stopped or exited          */
  567. #define STAT_TIMED    (1<<2)    /* job is being timed                   */
  568. #define STAT_DONE    (1<<3)    /* job is done                          */
  569. #define STAT_LOCKED    (1<<4)    /* shell is finished creating this job, */
  570.                                 /*   may be deleted from job table      */
  571. #define STAT_NOPRINT    (1<<5)    /* job was killed internally,           */
  572.                                 /*   we don't want to show that         */
  573. #define STAT_INUSE    (1<<6)    /* this job entry is in use             */
  574. #define STAT_SUPERJOB    (1<<7)    /* job has a subjob                     */
  575. #define STAT_SUBJOB    (1<<8)    /* job is a subjob                      */
  576. #define STAT_CURSH    (1<<9)    /* last command is in current shell     */
  577. #define STAT_NOSTTY    (1<<10)    /* the tty settings are not inherited   */
  578.                 /* from this job when it exits.         */
  579.  
  580. #define SP_RUNNING -1        /* fake status for jobs currently running */
  581.  
  582. struct timeinfo {
  583.     long ut;                    /* user space time   */
  584.     long st;                    /* system space time */
  585. };
  586.  
  587. #define JOBTEXTSIZE 80
  588.  
  589. /* node in job process lists */
  590.  
  591. struct process {
  592.     struct process *next;
  593.     pid_t pid;                  /* process id                       */
  594.     char text[JOBTEXTSIZE];    /* text to print when 'jobs' is run */
  595.     int status;            /* return code from waitpid/wait3() */
  596.     struct timeinfo ti;
  597.     struct timeval bgtime;    /* time job was spawned             */
  598.     struct timeval endtime;    /* time job exited                  */
  599. };
  600.  
  601. struct execstack {
  602.     struct execstack *next;
  603.  
  604.     LinkList args;
  605.     pid_t list_pipe_pid;
  606.     int nowait;
  607.     int pline_level;
  608.     int list_pipe_child;
  609.     int list_pipe_job;
  610.     char list_pipe_text[JOBTEXTSIZE];
  611.     int lastval;
  612.     int noeval;
  613.     int badcshglob;
  614.     pid_t cmdoutpid;
  615.     int cmdoutval;
  616.     int trapreturn;
  617.     int noerrs;
  618.     int subsh_close;
  619.     char *underscore;
  620. };
  621.  
  622. struct heredocs {
  623.     struct heredocs *next;
  624.     Redir rd;
  625. };
  626.  
  627. /*******************************/
  628. /* Definitions for Hash Tables */
  629. /*******************************/
  630.  
  631. typedef void *(*VFunc) _((void *));
  632. typedef void (*FreeFunc) _((void *));
  633.  
  634. typedef unsigned (*HashFunc)       _((char *));
  635. typedef void     (*TableFunc)      _((HashTable));
  636. typedef void     (*AddNodeFunc)    _((HashTable, char *, void *));
  637. typedef HashNode (*GetNodeFunc)    _((HashTable, char *));
  638. typedef HashNode (*RemoveNodeFunc) _((HashTable, char *));
  639. typedef void     (*FreeNodeFunc)   _((HashNode));
  640.  
  641. /* type of function that is passed to *
  642.  * scanhashtable or scanmatchtable    */
  643. typedef void     (*ScanFunc)       _((HashNode, int));
  644.  
  645. #ifdef ZSH_HASH_DEBUG
  646. typedef void (*PrintTableStats) _((HashTable));
  647. #endif
  648.  
  649. /* hash table for standard open hashing */
  650.  
  651. struct hashtable {
  652.     /* HASHTABLE DATA */
  653.     int hsize;            /* size of nodes[]  (number of hash values)   */
  654.     int ct;            /* number of elements                         */
  655.     HashNode *nodes;        /* array of size hsize                        */
  656.  
  657. #ifdef ZSH_HASH_DEBUG
  658.     char *tablename;        /* string containing name of the hash table */
  659. #endif
  660.  
  661.     /* HASHTABLE METHODS */
  662.     HashFunc hash;        /* pointer to hash function for this table    */
  663.     TableFunc emptytable;    /* pointer to function to empty table         */
  664.     TableFunc filltable;    /* pointer to function to fill table          */
  665.     AddNodeFunc addnode;    /* pointer to function to add new node        */
  666.     GetNodeFunc getnode;    /* pointer to function to get an enabled node */
  667.     GetNodeFunc getnode2;    /* pointer to function to get node            */
  668.                 /* (getnode2 will ignore DISABLED flag)       */
  669.     RemoveNodeFunc removenode;    /* pointer to function to delete a node       */
  670.     ScanFunc disablenode;    /* pointer to function to disable a node      */
  671.     ScanFunc enablenode;    /* pointer to function to enable a node       */
  672.     FreeNodeFunc freenode;    /* pointer to function to free a node         */
  673.     ScanFunc printnode;        /* pointer to function to print a node        */
  674.  
  675. #ifdef ZSH_HASH_DEBUG
  676.     PrintTableStats printinfo;    /* pointer to function to print table stats */
  677. #endif
  678. };
  679.  
  680. /* generic hash table node */
  681.  
  682. struct hashnode {
  683.     HashNode next;        /* next in hash chain */
  684.     char *nam;            /* hash key           */
  685.     int flags;            /* various flags      */
  686. };
  687.  
  688. /* The flag to disable nodes in a hash table.  Currently  *
  689.  * you can disable builtins, shell functions, aliases and *
  690.  * reserved words.                                        */
  691. #define DISABLED    (1<<0)
  692.  
  693. /* node in shell reserved word hash table (reswdtab) */
  694.  
  695. struct reswd {
  696.     HashNode next;        /* next in hash chain        */
  697.     char *nam;            /* name of reserved word     */
  698.     int flags;            /* flags                     */
  699.     int token;            /* corresponding lexer token */
  700. };
  701.  
  702. /* node in alias hash table (aliastab) */
  703.  
  704. struct alias {
  705.     HashNode next;        /* next in hash chain       */
  706.     char *nam;            /* hash data                */
  707.     int flags;            /* flags for alias types    */
  708.     char *text;            /* expansion of alias       */
  709.     int inuse;            /* alias is being expanded  */
  710. };
  711.  
  712. /* is this alias global */
  713. #define ALIAS_GLOBAL    (1<<1)
  714.  
  715. /* node in command path hash table (cmdnamtab) */
  716.  
  717. struct cmdnam {
  718.     HashNode next;        /* next in hash chain */
  719.     char *nam;            /* hash data          */
  720.     int flags;
  721.     union {
  722.     char **name;        /* full pathname for external commands */
  723.     char *cmd;        /* file name for hashed commands       */
  724.     }
  725.     u;
  726. };
  727.  
  728. /* flag for nodes explicitly added to *
  729.  * cmdnamtab with hash builtin        */
  730. #define HASHED        (1<<1)
  731.  
  732. /* node in shell function hash table (shfunctab) */
  733.  
  734. struct shfunc {
  735.     HashNode next;        /* next in hash chain     */
  736.     char *nam;            /* name of shell function */
  737.     int flags;            /* various flags          */
  738.     List funcdef;        /* function definition    */
  739. };
  740.  
  741. /* node in builtin command hash table (builtintab) */
  742.  
  743. typedef int (*HandlerFunc) _((char *, char **, char *, int));
  744.  
  745. struct builtin {
  746.     HashNode next;        /* next in hash chain                                 */
  747.     char *nam;            /* name of builtin                                    */
  748.     int flags;            /* various flags                                      */
  749.     HandlerFunc handlerfunc;    /* pointer to function that executes this builtin     */
  750.     int minargs;        /* minimum number of arguments                        */
  751.     int maxargs;        /* maximum number of arguments, or -1 for no limit    */
  752.     int funcid;            /* xbins (see above) for overloaded handlerfuncs      */
  753.     char *optstr;        /* string of legal options                            */
  754.     char *defopts;        /* options set by default for overloaded handlerfuncs */
  755. };
  756.  
  757. /* builtin flags */
  758. /* DISABLE IS DEFINED AS (1<<0) */
  759. #define BINF_PLUSOPTS        (1<<1)    /* +xyz legal */
  760. #define BINF_R            (1<<2)    /* this is the builtin `r' (fc -e -) */
  761. #define BINF_PRINTOPTS        (1<<3)
  762. #define BINF_FCOPTS        (1<<5)
  763. #define BINF_TYPEOPT        (1<<6)
  764. #define BINF_ECHOPTS        (1<<7)
  765. #define BINF_MAGICEQUALS    (1<<8)  /* needs automatic MAGIC_EQUAL_SUBST substitution */
  766. #define BINF_PREFIX        (1<<9)
  767. #define BINF_DASH        (1<<10)
  768. #define BINF_BUILTIN        (1<<11)
  769. #define BINF_COMMAND        (1<<12)
  770. #define BINF_EXEC        (1<<13)
  771. #define BINF_NOGLOB        (1<<14)
  772. #define BINF_PSPECIAL        (1<<15)
  773.  
  774. #define BINF_TYPEOPTS   (BINF_TYPEOPT|BINF_PLUSOPTS)
  775.  
  776. /* node used in parameter hash table (paramtab) */
  777.  
  778. struct param {
  779.     HashNode next;        /* next in hash chain */
  780.     char *nam;            /* hash data          */
  781.     int flags;            /* PM_* flags         */
  782.  
  783.     /* the value of this parameter */
  784.     union {
  785.     char **arr;        /* value if declared array   (PM_ARRAY)   */
  786.     char *str;        /* value if declared string  (PM_SCALAR)  */
  787.     long val;        /* value if declared integer (PM_INTEGER) */
  788.     } u;
  789.  
  790.     /* pointer to function to set value of this parameter */
  791.     union {
  792.     void (*cfn) _((Param, char *));
  793.     void (*ifn) _((Param, long));
  794.     void (*afn) _((Param, char **));
  795.     } sets;
  796.  
  797.     /* pointer to function to get value of this parameter */
  798.     union {
  799.     char *(*cfn) _((Param));
  800.     long (*ifn) _((Param));
  801.     char **(*afn) _((Param));
  802.     } gets;
  803.  
  804.     int ct;            /* output base or field width            */
  805.     void *data;            /* used by getfns                        */
  806.     char *env;            /* location in environment, if exported  */
  807.     char *ename;        /* name of corresponding environment var */
  808.     Param old;            /* old struct for use with local         */
  809.     int level;            /* if (old != NULL), level of localness  */
  810. };
  811.  
  812. /* flags for parameters */
  813.  
  814. /* parameter types */
  815. #define PM_SCALAR    0    /* scalar                                     */
  816. #define PM_ARRAY    (1<<0)    /* array                                      */
  817. #define PM_INTEGER    (1<<1)    /* integer                                    */
  818.  
  819. #define PM_TYPE(X) (X & (PM_SCALAR|PM_INTEGER|PM_ARRAY))
  820.  
  821. #define PM_LEFT        (1<<2)    /* left justify and remove leading blanks     */
  822. #define PM_RIGHT_B    (1<<3)    /* right justify and fill with leading blanks */
  823. #define PM_RIGHT_Z    (1<<4)    /* right justify and fill with leading zeros  */
  824. #define PM_LOWER    (1<<5)    /* all lower case                             */
  825.  
  826. /* The following are the same since they *
  827.  * both represent -u option to typeset   */
  828. #define PM_UPPER    (1<<6)    /* all upper case                             */
  829. #define PM_UNDEFINED    (1<<6)    /* undefined (autoloaded) shell function      */
  830.  
  831. #define PM_READONLY    (1<<7)    /* readonly                                   */
  832. #define PM_TAGGED    (1<<8)    /* tagged                                     */
  833. #define PM_EXPORTED    (1<<9)    /* exported                                   */
  834. #define PM_UNIQUE    (1<<10)    /* remove duplicates                          */
  835. #define PM_SPECIAL    (1<<11) /* special builtin parameter                  */
  836. #define PM_DONTIMPORT    (1<<12)    /* do not import this variable                */
  837. #define PM_UNSET    (1<<13)
  838.  
  839. /* node for compctl hash table (compctltab) */
  840.  
  841. struct compctlp {
  842.     HashNode next;        /* next in hash chain               */
  843.     char *nam;            /* command name                     */
  844.     int flags;            /* CURRENTLY UNUSED                 */
  845.     Compctl cc;            /* pointer to the compctl desc.     */
  846. };
  847.  
  848. /* node for named directory hash table (nameddirtab) */
  849.  
  850. struct nameddir {
  851.     HashNode next;        /* next in hash chain               */
  852.     char *nam;            /* directory name                   */
  853.     int flags;            /* see below                        */
  854.     char *dir;            /* the directory in full            */
  855.     int diff;            /* strlen(.dir) - strlen(.nam)      */
  856. };
  857.  
  858. /* flags for named directories */
  859. /* DISABLED is defined (1<<0) */
  860. #define ND_USERNAME    (1<<1)    /* nam is actually a username       */
  861.  
  862.  
  863. /* flags for controlling printing of hash table nodes */
  864. #define PRINT_NAMEONLY        (1<<0)
  865. #define PRINT_TYPE        (1<<1)
  866. #define PRINT_LIST        (1<<2)
  867.  
  868. /* flags for printing for the whence builtin */
  869. #define PRINT_WHENCE_CSH    (1<<3)
  870. #define PRINT_WHENCE_VERBOSE    (1<<4)
  871. #define PRINT_WHENCE_SIMPLE    (1<<5)
  872. #define PRINT_WHENCE_FUNCDEF    (1<<6)
  873.  
  874.  
  875. /******************************/
  876. /* Definitions for sched list */
  877. /******************************/
  878.  
  879. /* node in sched list */
  880.  
  881. struct schedcmd {
  882.     struct schedcmd *next;
  883.     char *cmd;            /* command to run */
  884.     time_t time;        /* when to run it */
  885. };
  886.  
  887.  
  888. /***********************************/
  889. /* Definitions for history control */
  890. /***********************************/
  891.  
  892. /* history entry */
  893.  
  894. struct histent {
  895.     char *text;            /* the history line itself          */
  896.     char *zle_text;        /* the edited history line          */
  897.     time_t stim;        /* command started time (datestamp) */
  898.     time_t ftim;        /* command finished time            */
  899.     short *words;        /* Position of words in history     */
  900.                 /*   line:  as pairs of start, end  */
  901.     int nwords;            /* Number of words in history line  */
  902.     int flags;            /* Misc flags                       */
  903. };
  904.  
  905. #define HIST_OLD    0x00000001    /* Command is already written to disk*/
  906.  
  907. /* Parts of the code where history expansion is disabled *
  908.  * should be within a pair of STOPHIST ... ALLOWHIST     */
  909.  
  910. #define STOPHIST (stophist += 4);
  911. #define ALLOWHIST (stophist -= 4);
  912.  
  913. #define HISTFLAG_DONE   1
  914. #define HISTFLAG_NOEXEC 2
  915. #define HISTFLAG_RECALL 4
  916.  
  917.  
  918. /******************************************/
  919. /* Definitions for programable completion */
  920. /******************************************/
  921.  
  922. struct compcond {
  923.     struct compcond *and, *or;    /* the next or'ed/and'ed conditions    */
  924.     int type;            /* the type (CCT_*)                    */
  925.     int n;            /* the array length                    */
  926.     union {            /* these structs hold the data used to */
  927.     struct {        /* test this condition                 */
  928.         int *a, *b;        /* CCT_POS, CCT_NUMWORDS               */
  929.     }
  930.     r;
  931.     struct {        /* CCT_CURSTR, CCT_CURPAT,... */
  932.         int *p;
  933.         char **s;
  934.     }
  935.     s;
  936.     struct {        /* CCT_RANGESTR,... */
  937.         char **a, **b;
  938.     }
  939.     l;
  940.     }
  941.     u;
  942. };
  943.  
  944. #define CCT_UNUSED     0
  945. #define CCT_POS        1
  946. #define CCT_CURSTR     2
  947. #define CCT_CURPAT     3
  948. #define CCT_WORDSTR    4
  949. #define CCT_WORDPAT    5
  950. #define CCT_CURSUF     6
  951. #define CCT_CURPRE     7
  952. #define CCT_CURSUB     8
  953. #define CCT_CURSUBC    9
  954. #define CCT_NUMWORDS  10
  955. #define CCT_RANGESTR  11
  956. #define CCT_RANGEPAT  12
  957.  
  958. /* Contains the real description for compctls */
  959.  
  960. struct compctl {
  961.     int refc;            /* reference count                         */
  962.     struct compctl *next;    /* next compctl for -x                     */
  963.     unsigned long mask;        /* mask of things to complete (CC_*)       */
  964.     char *keyvar;        /* for -k (variable)                       */
  965.     char *glob;            /* for -g (globbing)                       */
  966.     char *str;            /* for -s (expansion)                      */
  967.     char *func;            /* for -K (function)                       */
  968.     char *explain;        /* for -X (explanation)                    */
  969.     char *prefix, *suffix;    /* for -P and -S (prefix, suffix)          */
  970.     char *subcmd;        /* for -l (command name to use)            */
  971.     char *hpat;            /* for -H (history pattern)                */
  972.     int hnum;            /* for -H (number of events to search)     */
  973.     struct compctl *ext;    /* for -x (first of the compctls after -x) */
  974.     struct compcond *cond;    /* for -x (condition for this compctl)     */
  975.     struct compctl *xor;    /* for + (next of the xor'ed compctls)     */
  976. };
  977.  
  978. /* objects to complete */
  979. #define CC_FILES    (1<<0)
  980. #define CC_COMMPATH    (1<<1)
  981. #define CC_REMOVE    (1<<2)
  982. #define CC_OPTIONS    (1<<3)
  983. #define CC_VARS        (1<<4)
  984. #define CC_BINDINGS    (1<<5)
  985. #define CC_ARRAYS    (1<<6)
  986. #define CC_INTVARS    (1<<7)
  987. #define CC_SHFUNCS    (1<<8)
  988. #define CC_PARAMS    (1<<9)
  989. #define CC_ENVVARS    (1<<10)
  990. #define CC_JOBS        (1<<11)
  991. #define CC_RUNNING    (1<<12)
  992. #define CC_STOPPED    (1<<13)
  993. #define CC_BUILTINS    (1<<14)
  994. #define CC_ALREG    (1<<15)
  995. #define CC_ALGLOB    (1<<16)
  996. #define CC_USERS    (1<<17)
  997. #define CC_DISCMDS    (1<<18)
  998. #define CC_EXCMDS    (1<<19)
  999. #define CC_SCALARS    (1<<20)
  1000. #define CC_READONLYS    (1<<21)
  1001. #define CC_SPECIALS    (1<<22)
  1002. #define CC_DELETE    (1<<23)
  1003. #define CC_NAMED    (1<<24)
  1004. #define CC_QUOTEFLAG    (1<<25)
  1005. #define CC_EXTCMDS    (1<<26)
  1006. #define CC_RESWDS    (1<<27)
  1007.  
  1008. #define CC_RESERVED    (1<<31)
  1009.  
  1010.  
  1011. /******************************/
  1012. /* Definition for zsh options */
  1013. /******************************/
  1014.  
  1015. /* Possible values of emulation: the order must match the OPT_*SH values *
  1016.  * below.                                                                */
  1017.  
  1018. enum {
  1019.   EMULATE_CSH, /* C shell */
  1020.   EMULATE_KSH, /* Korn shell */
  1021.   EMULATE_SH,  /* Bourne shell */
  1022.   EMULATE_ZSH  /* `native' mode */
  1023. };
  1024.  
  1025. /* the option name table */
  1026.  
  1027. struct option {
  1028.     char *name;            /* full name */
  1029.     char id_zsh;        /* single letter name in zsh/csh mode */
  1030.     char id_ksh;        /* single letter name in ksh/sh mode */
  1031.     unsigned char flags;    /* see below */
  1032. };
  1033.  
  1034. #define optid(X) ( isset(SHOPTIONLETTERS) ? (X).id_ksh : (X).id_zsh )
  1035.  
  1036. /* option flags: the order of the shells here must match the order of the *
  1037.  * EMULATE_ values above                                                  */
  1038.  
  1039. #define OPT_CSH        0x01    /* option is set by default for csh */
  1040. #define OPT_KSH        0x02    /* option is set by default for ksh */
  1041. #define OPT_SH        0x04    /* option is set by default for sh */
  1042. #define OPT_ZSH        0x08    /* option is set by default for zsh */
  1043.  
  1044. #define OPT_ALL        (OPT_CSH|OPT_KSH|OPT_SH|OPT_ZSH)
  1045. #define OPT_BOURNE    (OPT_KSH|OPT_SH)
  1046. #define OPT_BSHELL    (OPT_KSH|OPT_SH|OPT_ZSH)
  1047. #define OPT_NONBOURNE    (OPT_ALL & ~OPT_BOURNE)
  1048. #define OPT_NONZSH    (OPT_ALL & ~OPT_ZSH)
  1049.  
  1050. #define OPT_EMULATE    0x40    /* option is relevant to emulation */
  1051. #define OPT_SPECIAL    0x80    /* option should never be set by emulate() */
  1052.  
  1053. /* this can be ored with an option letter to invert its sense */
  1054.  
  1055. #define OPT_REV        ((char) 0x80)
  1056.  
  1057. /* option indices */
  1058.  
  1059. enum {
  1060.     OPT_INVALID,
  1061.     ALLEXPORT,
  1062.     ALWAYSLASTPROMPT,
  1063.     ALWAYSTOEND,
  1064.     APPENDHISTORY,
  1065.     AUTOCD,
  1066.     AUTOLIST,
  1067.     AUTOMENU,
  1068.     AUTONAMEDIRS,
  1069.     AUTOPARAMKEYS,
  1070.     AUTOPARAMSLASH,
  1071.     AUTOPUSHD,
  1072.     AUTOREMOVESLASH,
  1073.     AUTORESUME,
  1074.     BADPATTERN,
  1075.     BANGHIST,
  1076.     BEEP,
  1077.     BGNICE,
  1078.     BRACECCL,
  1079.     BSDECHO,
  1080.     CDABLEVARS,
  1081.     CHASELINKS,
  1082.     CLOBBER,
  1083.     COMPLETEALIASES,
  1084.     COMPLETEINWORD,
  1085.     CORRECT,
  1086.     CORRECTALL,
  1087.     CSHJUNKIEHISTORY,
  1088.     CSHJUNKIELOOPS,
  1089.     CSHJUNKIEQUOTES,
  1090.     CSHNULLGLOB,
  1091.     EQUALS,
  1092.     ERREXIT,
  1093.     EXECOPT,
  1094.     EXTENDEDGLOB,
  1095.     EXTENDEDHISTORY,
  1096.     FLOWCONTROL,
  1097.     FUNCTIONARGZERO,
  1098.     GLOBOPT,
  1099.     GLOBASSIGN,
  1100.     GLOBCOMPLETE,
  1101.     GLOBDOTS,
  1102.     GLOBSUBST,
  1103.     HASHCMDS,
  1104.     HASHDIRS,
  1105.     HASHLISTALL,
  1106.     HISTALLOWCLOBBER,
  1107.     HISTBEEP,
  1108.     HISTIGNOREDUPS,
  1109.     HISTIGNORESPACE,
  1110.     HISTNOSTORE,
  1111.     HISTVERIFY,
  1112.     HUP,
  1113.     IGNOREBRACES,
  1114.     IGNOREEOF,
  1115.     INTERACTIVE,
  1116.     INTERACTIVECOMMENTS,
  1117.     KSHARRAYS,
  1118.     KSHOPTIONPRINT,
  1119.     LISTAMBIGUOUS,
  1120.     LISTBEEP,
  1121.     LISTTYPES,
  1122.     LOCALOPTIONS,
  1123.     LOGINSHELL,
  1124.     LONGLISTJOBS,
  1125.     MAGICEQUALSUBST,
  1126.     MAILWARNING,
  1127.     MARKDIRS,
  1128.     MENUCOMPLETE,
  1129.     MONITOR,
  1130.     MULTIOS,
  1131.     NOMATCH,
  1132.     NOTIFY,
  1133.     NULLGLOB,
  1134.     NUMERICGLOBSORT,
  1135.     OVERSTRIKE,
  1136.     PATHDIRS,
  1137.     POSIXBUILTINS,
  1138.     PRINTEXITVALUE,
  1139.     PRIVILEGED,
  1140.     PROMPTCR,
  1141.     PROMPTSUBST,
  1142.     PUSHDIGNOREDUPS,
  1143.     PUSHDMINUS,
  1144.     PUSHDSILENT,
  1145.     PUSHDTOHOME,
  1146.     RCEXPANDPARAM,
  1147.     RCQUOTES,
  1148.     RCS,
  1149.     RECEXACT,
  1150.     RMSTARSILENT,
  1151.     SHFILEEXPANSION,
  1152.     SHGLOB,
  1153.     SHINSTDIN,
  1154.     SHOPTIONLETTERS,
  1155.     SHORTLOOPS,
  1156.     SHWORDSPLIT,
  1157.     SINGLECOMMAND,
  1158.     SINGLELINEZLE,
  1159.     SUNKEYBOARDHACK,
  1160.     UNSET,
  1161.     VERBOSE,
  1162.     XTRACE,
  1163.     USEZLE,
  1164.     OPT_SIZE
  1165. };
  1166.  
  1167. #undef isset
  1168. #define isset(X) (opts[X])
  1169. #define unset(X) (!opts[X])
  1170. #define defset(X) (!!(optns[X].flags & (1 << emulation)))
  1171.  
  1172. #define interact (isset(INTERACTIVE))
  1173. #define jobbing  (isset(MONITOR))
  1174. #define islogin  (isset(LOGINSHELL))
  1175.  
  1176. /***********************************************/
  1177. /* Defintions for terminal and display control */
  1178. /***********************************************/
  1179.  
  1180. /* tty state structure */
  1181.  
  1182. struct ttyinfo {
  1183. #ifdef HAVE_TERMIOS_H
  1184.     struct termios tio;
  1185. #else
  1186. # ifdef HAVE_TERMIO_H
  1187.     struct termio tio;
  1188. # else
  1189.     struct sgttyb sgttyb;
  1190.     int lmodes;
  1191.     struct tchars tchars;
  1192.     struct ltchars ltchars;
  1193. # endif
  1194. #endif
  1195. #ifdef TIOCGWINSZ
  1196.     struct winsize winsize;
  1197. #endif
  1198. };
  1199.  
  1200. /* defines for whether tabs expand to spaces */
  1201. #if defined(HAVE_TERMIOS_H) || defined(HAVE_TERMIO_H)
  1202. #define SGTTYFLAG       shttyinfo.tio.c_oflag
  1203. #else   /* we're using sgtty */
  1204. #define SGTTYFLAG       shttyinfo.sgttyb.sg_flags
  1205. #endif
  1206. # ifdef TAB3
  1207. #define SGTABTYPE       TAB3
  1208. # else
  1209. #  ifdef OXTABS
  1210. #define SGTABTYPE       OXTABS
  1211. #  else
  1212. #define SGTABTYPE       XTABS
  1213. #  endif
  1214. # endif
  1215.  
  1216. /* flags for termflags */
  1217.  
  1218. #define TERM_BAD    0x01    /* terminal has extremely basic capabilities */
  1219. #define TERM_UNKNOWN    0x02    /* unknown terminal type */
  1220. #define TERM_NOUP    0x04    /* terminal has no up capability */
  1221. #define TERM_SHORT    0x08    /* terminal is < 3 lines high */
  1222. #define TERM_NARROW    0x10    /* terminal is < 3 columns wide */
  1223.  
  1224. /* interesting termcap strings */
  1225.  
  1226. #define TCCLEARSCREEN   0
  1227. #define TCLEFT          1
  1228. #define TCMULTLEFT      2
  1229. #define TCRIGHT         3
  1230. #define TCMULTRIGHT     4
  1231. #define TCUP            5
  1232. #define TCMULTUP        6
  1233. #define TCDOWN          7
  1234. #define TCMULTDOWN      8
  1235. #define TCDEL           9
  1236. #define TCMULTDEL      10
  1237. #define TCINS          11
  1238. #define TCMULTINS      12
  1239. #define TCCLEAREOD     13
  1240. #define TCCLEAREOL     14
  1241. #define TCINSLINE      15
  1242. #define TCDELLINE      16
  1243. #define TCNEXTTAB      17
  1244. #define TCBOLDFACEBEG  18
  1245. #define TCSTANDOUTBEG  19
  1246. #define TCUNDERLINEBEG 20
  1247. #define TCALLATTRSOFF  21
  1248. #define TCSTANDOUTEND  22
  1249. #define TCUNDERLINEEND 23
  1250. #define TC_COUNT       24
  1251.  
  1252. #define tccan(X) (tclen[X])
  1253.  
  1254. #define TXTBOLDFACE   0x01
  1255. #define TXTSTANDOUT   0x02
  1256. #define TXTUNDERLINE  0x04
  1257. #define TXTDIRTY      0x80
  1258.  
  1259. #define txtisset(X)  (txtattrmask & (X))
  1260. #define txtset(X)    (txtattrmask |= (X))
  1261. #define txtunset(X)  (txtattrmask &= ~(X))
  1262.  
  1263. #define TXTNOBOLDFACE    0x10
  1264. #define TXTNOSTANDOUT    0x20
  1265. #define TXTNOUNDERLINE    0x40
  1266.  
  1267. #define txtchangeisset(X)    (txtchange & (X))
  1268. #define txtchangeset(X, Y)    (txtchange |= (X), txtchange &= ~(Y))
  1269.  
  1270. /****************************************/
  1271. /* Definitions for the %_ prompt escape */
  1272. /****************************************/
  1273.  
  1274. #include "ztype.h"
  1275.  
  1276. #define cmdpush(X) if (!(cmdsp >= 0 && cmdsp < 256)) {;} else cmdstack[cmdsp++]=(X)
  1277. #ifdef DEBUG
  1278. # define cmdpop()  if (cmdsp <= 0) { \
  1279.             fputs("BUG: cmdstack empty\n", stderr); \
  1280.             fflush(stderr); \
  1281.            } else cmdsp--
  1282. #else
  1283. # define cmdpop()   if (cmdsp <= 0) {;} else cmdsp--
  1284. #endif
  1285.  
  1286. #define CS_FOR          0
  1287. #define CS_WHILE        1
  1288. #define CS_REPEAT       2
  1289. #define CS_SELECT       3
  1290. #define CS_UNTIL        4
  1291. #define CS_IF           5
  1292. #define CS_IFTHEN       6
  1293. #define CS_ELSE         7
  1294. #define CS_ELIF         8
  1295. #define CS_MATH         9
  1296. #define CS_COND        10
  1297. #define CS_CMDOR       11
  1298. #define CS_CMDAND      12
  1299. #define CS_PIPE        13
  1300. #define CS_ERRPIPE     14
  1301. #define CS_FOREACH     15
  1302. #define CS_CASE        16
  1303. #define CS_FUNCDEF     17
  1304. #define CS_SUBSH       18
  1305. #define CS_CURSH       19
  1306. #define CS_ARRAY       20
  1307. #define CS_QUOTE       21
  1308. #define CS_DQUOTE      22
  1309. #define CS_BQUOTE      23
  1310. #define CS_CMDSUBST    24
  1311. #define CS_MATHSUBST   25
  1312. #define CS_ELIFTHEN    26
  1313. #define CS_HEREDOC     27
  1314. #define CS_HEREDOCD    28
  1315. #define CS_BRACE       29
  1316. #define CS_BRACEPAR    30
  1317.  
  1318. /*********************
  1319.  * Memory management *
  1320.  *********************/
  1321.  
  1322. #ifndef DEBUG
  1323. # define HEAPALLOC    do { int nonlocal_useheap = global_heapalloc(); do
  1324.  
  1325. # define PERMALLOC    do { int nonlocal_useheap = global_permalloc(); do
  1326.  
  1327. # define LASTALLOC    while (0); \
  1328.             if (nonlocal_useheap) global_heapalloc(); \
  1329.             else global_permalloc(); \
  1330.         } while(0)
  1331.  
  1332. # define LASTALLOC_RETURN \
  1333.             if ((nonlocal_useheap ? global_heapalloc() : \
  1334.                  global_permalloc()), 0) {;} else return
  1335. #else
  1336. # define HEAPALLOC    do { int nonlocal_useheap = global_heapalloc(); \
  1337.             alloc_stackp++; do
  1338.  
  1339. # define PERMALLOC    do { int nonlocal_useheap = global_permalloc(); \
  1340.             alloc_stackp++; do
  1341.  
  1342. # define LASTALLOC    while (0); alloc_stackp--; \
  1343.             if (nonlocal_useheap) global_heapalloc(); \
  1344.             else global_permalloc(); \
  1345.         } while(0)
  1346.  
  1347. # define LASTALLOC_RETURN \
  1348.             if ((nonlocal_useheap ? global_heapalloc() : \
  1349.                 global_permalloc()),alloc_stackp--,0){;}else return
  1350. #endif
  1351.  
  1352. /****************/
  1353. /* Debug macros */
  1354. /****************/
  1355.  
  1356. #ifdef DEBUG
  1357. # define DPUTS(X,Y) if (!(X)) {;} else dputs(Y)
  1358. # define MUSTUSEHEAP(X) if (useheap) {;} else \
  1359.         fprintf(stderr, "BUG: permanent allocation in %s\n", X), \
  1360.         fflush(stderr)
  1361. #else
  1362. # define DPUTS(X,Y)
  1363. # define MUSTUSEHEAP(X)
  1364. #endif
  1365.  
  1366. /**************************/
  1367. /* Signal handling macros */
  1368. /**************************/
  1369.  
  1370. /* These used in the sigtrapped[] array */
  1371.  
  1372. #define ZSIG_TRAPPED    (1<<0)
  1373. #define ZSIG_IGNORED    (1<<1)
  1374. #define ZSIG_FUNC    (1<<2)
  1375.  
  1376. /***********************/
  1377. /* Shared header files */
  1378. /***********************/
  1379.  
  1380. #include "version.h"
  1381. #include "signals.h"
  1382. #include "globals.h"
  1383. #include "prototypes.h"
  1384. #include "hashtable.h"
  1385.