table.h revision 1.1 1 /* $Id: table.h,v 1.1 1996/09/21 23:35:17 jtc Exp $ */
2
3 /*
4 * generic hashed associative table for commands and variables.
5 */
6
7 struct table {
8 Area *areap; /* area to allocate entries */
9 short size, nfree; /* hash size (always 2^^n), free entries */
10 struct tbl **tbls; /* hashed table items */
11 };
12
13 struct tbl { /* table item */
14 Tflag flag; /* flags */
15 int type; /* command type (see below), base (if INTEGER),
16 * or offset from val.s of value (if EXPORT) */
17 Area *areap; /* area to allocate from */
18 union {
19 char *s; /* string */
20 long i; /* integer */
21 int (*f) ARGS((char **)); /* int function */
22 struct op *t; /* "function" tree */
23 } val; /* value */
24 int index; /* index for an array */
25 union {
26 int field; /* field with for -L/-R/-Z */
27 int errno_; /* CEXEC/CTALIAS */
28 } u2;
29 union {
30 struct tbl *array; /* array values */
31 char *fpath; /* temporary path to undef function */
32 } u;
33 char name[4]; /* name -- variable length */
34 };
35
36 /* common flag bits */
37 #define ALLOC BIT(0) /* val.s has been allocated */
38 #define DEFINED BIT(1) /* is defined in block */
39 #define ISSET BIT(2) /* has value, vp->val.[si] */
40 #define EXPORT BIT(3) /* exported variable/function */
41 #define TRACE BIT(4) /* var: user flagged, func: execution tracing */
42 /* (start non-common flags at 8) */
43 /* flag bits used for variables */
44 #define SPECIAL BIT(8) /* PATH, IFS, SECONDS, etc */
45 #define INTEGER BIT(9) /* val.i contains integer value */
46 #define RDONLY BIT(10) /* read-only variable */
47 #define LOCAL BIT(11) /* for local typeset() */
48 #define ARRAY BIT(13) /* array */
49 #define LJUST BIT(14) /* left justify */
50 #define RJUST BIT(15) /* right justify */
51 #define ZEROFIL BIT(16) /* 0 filled if RJUSTIFY, strip 0s if LJUSTIFY */
52 #define LCASEV BIT(17) /* convert to lower case */
53 #define UCASEV_AL BIT(18)/* convert to upper case / autoload function */
54 #define INT_U BIT(19) /* unsigned integer */
55 #define INT_L BIT(20) /* long integer (no-op) */
56 #define IMPORT BIT(21) /* flag to typeset(): no arrays, must have = */
57 #define LOCAL_COPY BIT(22) /* with LOCAL - copy attrs from existing var */
58 #define EXPRINEVAL BIT(23) /* contents currently being evaluated */
59 #define EXPRLVALUE BIT(24) /* useable as lvalue (temp flag) */
60 /* flag bits used for taliases/builtins/aliases/keywords/functions */
61 #define KEEPASN BIT(8) /* keep command assignments (eg, var=x cmd) */
62 #define FINUSE BIT(9) /* function being executed */
63 #define FDELETE BIT(10) /* function deleted while it was executing */
64 #define FKSH BIT(11) /* function defined with function x (vs x()) */
65 #define SPEC_BI BIT(12) /* a POSIX special builtin */
66 #define REG_BI BIT(13) /* a POSIX regular builtin */
67
68 /* command types */
69 #define CNONE 0 /* undefined */
70 #define CSHELL 1 /* built-in */
71 #define CFUNC 2 /* function */
72 #define CEXEC 4 /* executable command */
73 #define CALIAS 5 /* alias */
74 #define CKEYWD 6 /* keyword */
75 #define CTALIAS 7 /* tracked alias */
76
77 /* Flags for findcom()/comexec() */
78 #define FC_SPECBI BIT(0) /* special builtin */
79 #define FC_FUNC BIT(1) /* function builtin */
80 #define FC_REGBI BIT(2) /* regular builtin */
81 #define FC_UNREGBI BIT(3) /* un-regular builtin (!special,!regular) */
82 #define FC_BI (FC_SPECBI|FC_REGBI|FC_UNREGBI)
83 #define FC_PATH BIT(4) /* do path search */
84 #define FC_DEFPATH BIT(5) /* use default path in path search */
85
86
87 #define AF_ARGV_ALLOC 0x1 /* argv[] array allocated */
88 #define AF_ARGS_ALLOCED 0x2 /* argument strings allocated */
89 #define AI_ARGV(a, i) ((i) == 0 ? (a).argv[0] : (a).argv[(i) - (a).skip])
90 #define AI_ARGC(a) ((a).argc_ - (a).skip)
91
92 /* Argument info. Used for $#, $* for shell, functions, includes, etc. */
93 struct arg_info {
94 int flags; /* AF_* */
95 char **argv;
96 int argc_;
97 int skip; /* first arg is argv[0], second is argv[1 + skip] */
98 };
99
100 /*
101 * activation record for function blocks
102 */
103 struct block {
104 Area area; /* area to allocate things */
105 /*struct arg_info argi;*/
106 char **argv;
107 int argc;
108 struct table vars; /* local variables */
109 struct table funs; /* local functions */
110 #if 1
111 char * error; /* error handler */
112 char * exit; /* exit handler */
113 #else
114 Trap error, exit;
115 #endif
116 struct block *next; /* enclosing block */
117 };
118
119 /*
120 * Used by twalk() and tnext() routines.
121 */
122 struct tstate {
123 int left;
124 struct tbl **next;
125 };
126
127
128 EXTERN struct table taliases; /* tracked aliases */
129 EXTERN struct table builtins; /* built-in commands */
130 EXTERN struct table aliases; /* aliases */
131 EXTERN struct table keywords; /* keywords */
132 EXTERN struct table homedirs; /* homedir() cache */
133
134 struct builtin {
135 const char *name;
136 int (*func) ARGS((char **));
137 };
138
139 /* these really are externs! Look in table.c for them */
140 extern const struct builtin shbuiltins [], kshbuiltins [];
141
142 /* var spec values */
143 #define V_NONE 0
144 #define V_PATH 1
145 #define V_IFS 2
146 #define V_SECONDS 3
147 #define V_OPTIND 4
148 #define V_MAIL 5
149 #define V_MAILPATH 6
150 #define V_MAILCHECK 7
151 #define V_RANDOM 8
152 #define V_HISTSIZE 9
153 #define V_HISTFILE 10
154 #define V_VISUAL 11
155 #define V_EDITOR 12
156 #define V_COLUMNS 13
157 #define V_POSIXLY_CORRECT 14
158 #define V_TMOUT 15
159 #define V_TMPDIR 16
160
161 /* values for set_prompt() */
162 #define PS1 0 /* command */
163 #define PS2 1 /* command continuation */
164
165 EXTERN const char *path; /* PATH value */
166 EXTERN const char *def_path; /* path to use if PATH not set */
167 EXTERN char *tmpdir; /* TMPDIR value */
168 EXTERN const char *prompt;
169 EXTERN int cur_prompt; /* PS1 or PS2 */
170