xref: /netbsd/bin/ksh/table.h (revision 822c36bb)
1 /* $NetBSD: table.h,v 1.4 2018/06/03 12:18:29 kamil 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 /* Attributes that can be set by the user (used to decide if an unset param
68  * should be repoted by set/typeset).  Does not include ARRAY or LOCAL.
69  */
70 #define USERATTRIB	(EXPORT|INTEGER|RDONLY|LJUST|RJUST|ZEROFIL\
71 			 |LCASEV|UCASEV_AL|INT_U|INT_L)
72 
73 /* command types */
74 #define	CNONE	0		/* undefined */
75 #define	CSHELL	1		/* built-in */
76 #define	CFUNC	2		/* function */
77 #define	CEXEC	4		/* executable command */
78 #define	CALIAS	5		/* alias */
79 #define	CKEYWD	6		/* keyword */
80 #define CTALIAS	7		/* tracked alias */
81 
82 /* Flags for findcom()/comexec() */
83 #define FC_SPECBI	BIT(0)	/* special builtin */
84 #define FC_FUNC		BIT(1)	/* function builtin */
85 #define FC_REGBI	BIT(2)	/* regular builtin */
86 #define FC_UNREGBI	BIT(3)	/* un-regular builtin (!special,!regular) */
87 #define FC_BI		(FC_SPECBI|FC_REGBI|FC_UNREGBI)
88 #define FC_PATH		BIT(4)	/* do path search */
89 #define FC_DEFPATH	BIT(5)	/* use default path in path search */
90 
91 
92 #define AF_ARGV_ALLOC	0x1	/* argv[] array allocated */
93 #define AF_ARGS_ALLOCED	0x2	/* argument strings allocated */
94 #define AI_ARGV(a, i)	((i) == 0 ? (a).argv[0] : (a).argv[(i) - (a).skip])
95 #define AI_ARGC(a)	((a).argc_ - (a).skip)
96 
97 /* Argument info.  Used for $#, $* for shell, functions, includes, etc. */
98 struct arg_info {
99 	int flags;	/* AF_* */
100 	char **argv;
101 	int argc_;
102 	int skip;	/* first arg is argv[0], second is argv[1 + skip] */
103 };
104 
105 /*
106  * activation record for function blocks
107  */
108 struct block {
109 	Area	area;		/* area to allocate things */
110 	/*struct arg_info argi;*/
111 	char	**argv;
112 	int	argc;
113 	int	flags;		/* see BF_* */
114 	struct	table vars;	/* local variables */
115 	struct	table funs;	/* local functions */
116 	Getopt	getopts_state;
117 #if 1
118 	char *	error;		/* error handler */
119 	char *	exit;		/* exit handler */
120 #else
121 	Trap	error, exit;
122 #endif
123 	struct	block *next;	/* enclosing block */
124 };
125 
126 /* Values for struct block.flags */
127 #define BF_DOGETOPTS	BIT(0)	/* save/restore getopts state */
128 
129 /*
130  * Used by ksh_twalk() and tnext() routines.
131  */
132 struct tstate {
133 	int left;
134 	struct tbl **next;
135 };
136 
137 
138 EXTERN	struct table taliases;	/* tracked aliases */
139 EXTERN	struct table builtins;	/* built-in commands */
140 EXTERN	struct table aliases;	/* aliases */
141 EXTERN	struct table keywords;	/* keywords */
142 EXTERN	struct table homedirs;	/* homedir() cache */
143 
144 struct builtin {
145 	const char   *name;
146 	int  (*func) ARGS((char **));
147 };
148 
149 /* these really are externs! Look in table.c for them */
150 extern const struct builtin shbuiltins [], kshbuiltins [];
151 
152 /* var spec values */
153 #define	V_NONE			0
154 #define	V_PATH			1
155 #define	V_IFS			2
156 #define	V_SECONDS		3
157 #define	V_OPTIND		4
158 #define	V_MAIL			5
159 #define	V_MAILPATH		6
160 #define	V_MAILCHECK		7
161 #define	V_RANDOM		8
162 #define V_HISTSIZE		9
163 #define V_HISTFILE		10
164 #define V_VISUAL		11
165 #define V_EDITOR		12
166 #define V_COLUMNS		13
167 #define V_POSIXLY_CORRECT	14
168 #define V_TMOUT			15
169 #define V_TMPDIR		16
170 #define V_LINENO		17
171 
172 /* values for set_prompt() */
173 #define PS1	0		/* command */
174 #define PS2	1		/* command continuation */
175 
176 EXTERN char *path;		/* copy of either PATH or def_path */
177 EXTERN const char *def_path;	/* path to use if PATH not set */
178 EXTERN char *tmpdir;		/* TMPDIR value */
179 EXTERN const char *prompt;
180 EXTERN int cur_prompt;		/* PS1 or PS2 */
181 EXTERN int current_lineno;	/* LINENO value */
182