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