table.h
1/* $OpenBSD: table.h,v 1.15 2018/06/18 17:03:58 millert Exp $ */
2
3/* $From: table.h,v 1.3 1994/05/31 13:34:34 michael Exp $ */
4
5/*
6 * generic hashed associative table for commands and variables.
7 */
8
9struct 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 */
13};
14
15struct tbl { /* table item */
16 int 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 int64_t 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 */
36};
37
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.
71 */
72#define USERATTRIB (EXPORT|INTEGER|RDONLY|LJUST|RJUST|ZEROFIL\
73 |LCASEV|UCASEV_AL|INT_U|INT_L)
74
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 */
83
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 */
92
93
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)
98
99/* Argument info. Used for $#, $* for shell, functions, includes, etc. */
100struct 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] */
105};
106
107/*
108 * activation record for function blocks
109 */
110struct 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 */
126};
127
128/* Values for struct block.flags */
129#define BF_DOGETOPTS BIT(0) /* save/restore getopts state */
130
131/*
132 * Used by ktwalk() and ktnext() routines.
133 */
134struct tstate {
135 int left;
136 struct tbl **next;
137};
138
139extern struct table taliases; /* tracked aliases */
140extern struct table builtins; /* built-in commands */
141extern struct table aliases; /* aliases */
142extern struct table keywords; /* keywords */
143extern struct table homedirs; /* homedir() cache */
144
145struct builtin {
146 const char *name;
147 int (*func)(char **);
148};
149
150/* these really are externs! Look in table.c for them */
151extern const struct builtin shbuiltins [], kshbuiltins [];
152
153/* var spec values */
154#define V_NONE 0
155#define V_PATH 1
156#define V_IFS 2
157#define V_SECONDS 3
158#define V_OPTIND 4
159#define V_MAIL 5
160#define V_MAILPATH 6
161#define V_MAILCHECK 7
162#define V_RANDOM 8
163#define V_HISTCONTROL 9
164#define V_HISTSIZE 10
165#define V_HISTFILE 11
166#define V_VISUAL 12
167#define V_EDITOR 13
168#define V_COLUMNS 14
169#define V_POSIXLY_CORRECT 15
170#define V_TMOUT 16
171#define V_TMPDIR 17
172#define V_LINENO 18
173#define V_TERM 19
174
175/* values for set_prompt() */
176#define PS1 0 /* command */
177#define PS2 1 /* command continuation */
178
179extern char *search_path; /* copy of either PATH or def_path */
180extern const char *def_path; /* path to use if PATH not set */
181extern char *tmpdir; /* TMPDIR value */
182extern const char *prompt;
183extern int cur_prompt; /* PS1 or PS2 */
184extern int current_lineno; /* LINENO value */
185
186unsigned int hash(const char *);
187void ktinit(struct table *, Area *, int);
188struct tbl * ktsearch(struct table *, const char *, unsigned int);
189struct tbl * ktenter(struct table *, const char *, unsigned int);
190void ktdelete(struct tbl *);
191void ktwalk(struct tstate *, struct table *);
192struct tbl * ktnext(struct tstate *);
193struct tbl ** ktsort(struct table *);