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