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