xref: /openbsd-src/bin/ksh/table.h (revision a3c80d85437c1dedb2e5a01dd9f05902643919e6)
1*a3c80d85Smillert /*	$OpenBSD: table.h,v 1.15 2018/06/18 17:03:58 millert Exp $	*/
27cb960a2Sdownsj 
37cb960a2Sdownsj /* $From: table.h,v 1.3 1994/05/31 13:34:34 michael Exp $ */
47cb960a2Sdownsj 
57cb960a2Sdownsj /*
67cb960a2Sdownsj  * generic hashed associative table for commands and variables.
77cb960a2Sdownsj  */
87cb960a2Sdownsj 
97cb960a2Sdownsj struct table {
107cb960a2Sdownsj 	Area   *areap;		/* area to allocate entries */
110d4df784Sotto 	int	size, nfree;	/* hash size (always 2^^n), free entries */
127cb960a2Sdownsj 	struct	tbl **tbls;	/* hashed table items */
137cb960a2Sdownsj };
147cb960a2Sdownsj 
157cb960a2Sdownsj struct tbl {			/* table item */
168f5d53feSnicm 	int	flag;		/* flags */
177cb960a2Sdownsj 	int	type;		/* command type (see below), base (if INTEGER),
187cb960a2Sdownsj 				 * or offset from val.s of value (if EXPORT) */
197cb960a2Sdownsj 	Area	*areap;		/* area to allocate from */
207cb960a2Sdownsj 	union {
217cb960a2Sdownsj 		char *s;	/* string */
22517d3880Stobias 		int64_t i;	/* integer */
2369b9f96bSmillert 		int (*f)(char **);	/* int function */
247cb960a2Sdownsj 		struct op *t;	/* "function" tree */
257cb960a2Sdownsj 	} val;			/* value */
267cb960a2Sdownsj 	int	index;		/* index for an array */
27dcacb757Sdownsj 	union {
287cb960a2Sdownsj 	    int	field;		/* field with for -L/-R/-Z */
29dcacb757Sdownsj 	    int errno_;		/* CEXEC/CTALIAS */
30dcacb757Sdownsj 	} u2;
317cb960a2Sdownsj 	union {
327cb960a2Sdownsj 		struct tbl *array;	/* array values */
337cb960a2Sdownsj 		char *fpath;		/* temporary path to undef function */
347cb960a2Sdownsj 	} u;
357cb960a2Sdownsj 	char	name[4];	/* name -- variable length */
367cb960a2Sdownsj };
377cb960a2Sdownsj 
387cb960a2Sdownsj /* common flag bits */
397cb960a2Sdownsj #define	ALLOC		BIT(0)	/* val.s has been allocated */
407cb960a2Sdownsj #define	DEFINED		BIT(1)	/* is defined in block */
417cb960a2Sdownsj #define	ISSET		BIT(2)	/* has value, vp->val.[si] */
427cb960a2Sdownsj #define	EXPORT		BIT(3)	/* exported variable/function */
437cb960a2Sdownsj #define	TRACE		BIT(4)	/* var: user flagged, func: execution tracing */
447cb960a2Sdownsj /* (start non-common flags at 8) */
457cb960a2Sdownsj /* flag bits used for variables */
467cb960a2Sdownsj #define	SPECIAL		BIT(8)	/* PATH, IFS, SECONDS, etc */
477cb960a2Sdownsj #define	INTEGER		BIT(9)	/* val.i contains integer value */
487cb960a2Sdownsj #define	RDONLY		BIT(10)	/* read-only variable */
497cb960a2Sdownsj #define	LOCAL		BIT(11)	/* for local typeset() */
507cb960a2Sdownsj #define ARRAY		BIT(13)	/* array */
517cb960a2Sdownsj #define LJUST		BIT(14)	/* left justify */
527cb960a2Sdownsj #define RJUST		BIT(15)	/* right justify */
537cb960a2Sdownsj #define ZEROFIL		BIT(16)	/* 0 filled if RJUSTIFY, strip 0s if LJUSTIFY */
547cb960a2Sdownsj #define LCASEV		BIT(17)	/* convert to lower case */
557cb960a2Sdownsj #define UCASEV_AL	BIT(18)/* convert to upper case / autoload function */
567cb960a2Sdownsj #define INT_U		BIT(19)	/* unsigned integer */
577cb960a2Sdownsj #define INT_L		BIT(20)	/* long integer (no-op) */
587cb960a2Sdownsj #define IMPORT		BIT(21)	/* flag to typeset(): no arrays, must have = */
597cb960a2Sdownsj #define LOCAL_COPY	BIT(22)	/* with LOCAL - copy attrs from existing var */
60dcacb757Sdownsj #define EXPRINEVAL	BIT(23)	/* contents currently being evaluated */
61dcacb757Sdownsj #define EXPRLVALUE	BIT(24)	/* useable as lvalue (temp flag) */
627cb960a2Sdownsj /* flag bits used for taliases/builtins/aliases/keywords/functions */
637cb960a2Sdownsj #define KEEPASN		BIT(8)	/* keep command assignments (eg, var=x cmd) */
647cb960a2Sdownsj #define FINUSE		BIT(9)	/* function being executed */
657cb960a2Sdownsj #define FDELETE		BIT(10)	/* function deleted while it was executing */
66dcacb757Sdownsj #define FKSH		BIT(11)	/* function defined with function x (vs x()) */
67dcacb757Sdownsj #define SPEC_BI		BIT(12)	/* a POSIX special builtin */
68dcacb757Sdownsj #define REG_BI		BIT(13)	/* a POSIX regular builtin */
69e7bc3c65Sdownsj /* Attributes that can be set by the user (used to decide if an unset param
70e7bc3c65Sdownsj  * should be repoted by set/typeset).  Does not include ARRAY or LOCAL.
71e7bc3c65Sdownsj  */
72e7bc3c65Sdownsj #define USERATTRIB	(EXPORT|INTEGER|RDONLY|LJUST|RJUST|ZEROFIL\
73e7bc3c65Sdownsj 			 |LCASEV|UCASEV_AL|INT_U|INT_L)
747cb960a2Sdownsj 
757cb960a2Sdownsj /* command types */
767cb960a2Sdownsj #define	CNONE	0		/* undefined */
777cb960a2Sdownsj #define	CSHELL	1		/* built-in */
787cb960a2Sdownsj #define	CFUNC	2		/* function */
797cb960a2Sdownsj #define	CEXEC	4		/* executable command */
807cb960a2Sdownsj #define	CALIAS	5		/* alias */
817cb960a2Sdownsj #define	CKEYWD	6		/* keyword */
827cb960a2Sdownsj #define CTALIAS	7		/* tracked alias */
837cb960a2Sdownsj 
847cb960a2Sdownsj /* Flags for findcom()/comexec() */
857cb960a2Sdownsj #define FC_SPECBI	BIT(0)	/* special builtin */
867cb960a2Sdownsj #define FC_FUNC		BIT(1)	/* function builtin */
877cb960a2Sdownsj #define FC_REGBI	BIT(2)	/* regular builtin */
887cb960a2Sdownsj #define FC_UNREGBI	BIT(3)	/* un-regular builtin (!special,!regular) */
897cb960a2Sdownsj #define FC_BI		(FC_SPECBI|FC_REGBI|FC_UNREGBI)
907cb960a2Sdownsj #define FC_PATH		BIT(4)	/* do path search */
917cb960a2Sdownsj #define FC_DEFPATH	BIT(5)	/* use default path in path search */
927cb960a2Sdownsj 
937cb960a2Sdownsj 
947cb960a2Sdownsj #define AF_ARGV_ALLOC	0x1	/* argv[] array allocated */
957cb960a2Sdownsj #define AF_ARGS_ALLOCED	0x2	/* argument strings allocated */
967cb960a2Sdownsj #define AI_ARGV(a, i)	((i) == 0 ? (a).argv[0] : (a).argv[(i) - (a).skip])
977cb960a2Sdownsj #define AI_ARGC(a)	((a).argc_ - (a).skip)
987cb960a2Sdownsj 
997cb960a2Sdownsj /* Argument info.  Used for $#, $* for shell, functions, includes, etc. */
1007cb960a2Sdownsj struct arg_info {
1017cb960a2Sdownsj 	int flags;	/* AF_* */
1027cb960a2Sdownsj 	char **argv;
1037cb960a2Sdownsj 	int argc_;
1047cb960a2Sdownsj 	int skip;	/* first arg is argv[0], second is argv[1 + skip] */
1057cb960a2Sdownsj };
1067cb960a2Sdownsj 
1077cb960a2Sdownsj /*
1087cb960a2Sdownsj  * activation record for function blocks
1097cb960a2Sdownsj  */
1107cb960a2Sdownsj struct block {
1117cb960a2Sdownsj 	Area	area;		/* area to allocate things */
1127cb960a2Sdownsj 	/*struct arg_info argi;*/
1137cb960a2Sdownsj 	char	**argv;
1147cb960a2Sdownsj 	int	argc;
1153b015934Smillert 	int	flags;		/* see BF_* */
1167cb960a2Sdownsj 	struct	table vars;	/* local variables */
1177cb960a2Sdownsj 	struct	table funs;	/* local functions */
1183b015934Smillert 	Getopt	getopts_state;
1197cb960a2Sdownsj #if 1
1207cb960a2Sdownsj 	char *	error;		/* error handler */
1217cb960a2Sdownsj 	char *	exit;		/* exit handler */
1227cb960a2Sdownsj #else
1237cb960a2Sdownsj 	Trap	error, exit;
1247cb960a2Sdownsj #endif
1257cb960a2Sdownsj 	struct	block *next;	/* enclosing block */
1267cb960a2Sdownsj };
1277cb960a2Sdownsj 
1283b015934Smillert /* Values for struct block.flags */
1293b015934Smillert #define BF_DOGETOPTS	BIT(0)	/* save/restore getopts state */
1303b015934Smillert 
1317cb960a2Sdownsj /*
1326df3ee40Sotto  * Used by ktwalk() and ktnext() routines.
1337cb960a2Sdownsj  */
1347cb960a2Sdownsj struct tstate {
1357cb960a2Sdownsj 	int left;
1367cb960a2Sdownsj 	struct tbl **next;
1377cb960a2Sdownsj };
1387cb960a2Sdownsj 
1397df1fdf4Snicm extern	struct table taliases;	/* tracked aliases */
1407df1fdf4Snicm extern	struct table builtins;	/* built-in commands */
1417df1fdf4Snicm extern	struct table aliases;	/* aliases */
1427df1fdf4Snicm extern	struct table keywords;	/* keywords */
1437df1fdf4Snicm extern	struct table homedirs;	/* homedir() cache */
1447cb960a2Sdownsj 
1457cb960a2Sdownsj struct builtin {
1467cb960a2Sdownsj 	const char   *name;
14769b9f96bSmillert 	int  (*func)(char **);
1487cb960a2Sdownsj };
1497cb960a2Sdownsj 
1507cb960a2Sdownsj /* these really are externs! Look in table.c for them */
1517cb960a2Sdownsj extern const struct builtin shbuiltins [], kshbuiltins [];
1527cb960a2Sdownsj 
1537cb960a2Sdownsj /* var spec values */
1547cb960a2Sdownsj #define	V_NONE			0
1557cb960a2Sdownsj #define	V_PATH			1
1567cb960a2Sdownsj #define	V_IFS			2
1577cb960a2Sdownsj #define	V_SECONDS		3
1587cb960a2Sdownsj #define	V_OPTIND		4
1597cb960a2Sdownsj #define	V_MAIL			5
1607cb960a2Sdownsj #define	V_MAILPATH		6
1617cb960a2Sdownsj #define	V_MAILCHECK		7
1627cb960a2Sdownsj #define	V_RANDOM		8
163a725701fSjca #define	V_HISTCONTROL		9
164a725701fSjca #define	V_HISTSIZE		10
165a725701fSjca #define	V_HISTFILE		11
166a725701fSjca #define	V_VISUAL		12
167a725701fSjca #define	V_EDITOR		13
168a725701fSjca #define	V_COLUMNS		14
169a725701fSjca #define	V_POSIXLY_CORRECT	15
170a725701fSjca #define	V_TMOUT			16
171a725701fSjca #define	V_TMPDIR		17
172a725701fSjca #define	V_LINENO		18
173*a3c80d85Smillert #define	V_TERM			19
1747cb960a2Sdownsj 
1757cb960a2Sdownsj /* values for set_prompt() */
1767cb960a2Sdownsj #define PS1	0		/* command */
1777cb960a2Sdownsj #define PS2	1		/* command continuation */
1787cb960a2Sdownsj 
179201e0776Smillert extern char *search_path;	/* copy of either PATH or def_path */
1807df1fdf4Snicm extern const char *def_path;	/* path to use if PATH not set */
1817df1fdf4Snicm extern char *tmpdir;		/* TMPDIR value */
1827df1fdf4Snicm extern const char *prompt;
1837df1fdf4Snicm extern int cur_prompt;		/* PS1 or PS2 */
1847df1fdf4Snicm extern int current_lineno;	/* LINENO value */
1851fbcea4aSnicm 
1861fbcea4aSnicm unsigned int	hash(const char *);
1871fbcea4aSnicm void		ktinit(struct table *, Area *, int);
1881fbcea4aSnicm struct tbl *	ktsearch(struct table *, const char *, unsigned int);
1891fbcea4aSnicm struct tbl *	ktenter(struct table *, const char *, unsigned int);
1901fbcea4aSnicm void		ktdelete(struct tbl *);
1911fbcea4aSnicm void		ktwalk(struct tstate *, struct table *);
1921fbcea4aSnicm struct tbl *	ktnext(struct tstate *);
1931fbcea4aSnicm struct tbl **	ktsort(struct table *);
194