14887Schin /*********************************************************************** 24887Schin * * 34887Schin * This software is part of the ast package * 4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1982-2010 AT&T Intellectual Property * 54887Schin * and is licensed under the * 64887Schin * Common Public License, Version 1.0 * 78462SApril.Chin@Sun.COM * by AT&T Intellectual Property * 84887Schin * * 94887Schin * A copy of the License is available at * 104887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 124887Schin * * 134887Schin * Information and Software Systems Research * 144887Schin * AT&T Research * 154887Schin * Florham Park NJ * 164887Schin * * 174887Schin * David Korn <dgk@research.att.com> * 184887Schin * * 194887Schin ***********************************************************************/ 204887Schin #pragma prototyped 214887Schin #ifndef NV_DEFAULT 224887Schin /* 234887Schin * David Korn 244887Schin * AT&T Labs 254887Schin * 264887Schin * Interface definitions of structures for name-value pairs 274887Schin * These structures are used for named variables, functions and aliases 284887Schin * 294887Schin */ 304887Schin 314887Schin 324887Schin #include <ast.h> 334887Schin #include <cdt.h> 348462SApril.Chin@Sun.COM #include <option.h> 354887Schin 364887Schin /* for compatibility with old hash library */ 374887Schin #define Hashtab_t Dt_t 384887Schin #define HASH_BUCKET 1 394887Schin #define HASH_NOSCOPE 2 404887Schin #define HASH_SCOPE 4 414887Schin #define hashscope(x) dtvnext(x) 424887Schin 434887Schin typedef struct Namval Namval_t; 444887Schin typedef struct Namfun Namfun_t; 454887Schin typedef struct Namdisc Namdisc_t; 464887Schin typedef struct Nambfun Nambfun_t; 474887Schin typedef struct Namarray Namarr_t; 488462SApril.Chin@Sun.COM typedef struct Namdecl Namdecl_t; 494887Schin 504887Schin /* 514887Schin * This defines the template for nodes that have their own assignment 524887Schin * and or lookup functions 534887Schin */ 544887Schin struct Namdisc 554887Schin { 564887Schin size_t dsize; 574887Schin void (*putval)(Namval_t*, const char*, int, Namfun_t*); 584887Schin char *(*getval)(Namval_t*, Namfun_t*); 594887Schin Sfdouble_t (*getnum)(Namval_t*, Namfun_t*); 604887Schin char *(*setdisc)(Namval_t*, const char*, Namval_t*, Namfun_t*); 614887Schin Namval_t *(*createf)(Namval_t*, const char*, int, Namfun_t*); 624887Schin Namfun_t *(*clonef)(Namval_t*, Namval_t*, int, Namfun_t*); 634887Schin char *(*namef)(Namval_t*, Namfun_t*); 644887Schin Namval_t *(*nextf)(Namval_t*, Dt_t*, Namfun_t*); 654887Schin Namval_t *(*typef)(Namval_t*, Namfun_t*); 664887Schin int (*readf)(Namval_t*, Sfio_t*, int, Namfun_t*); 678462SApril.Chin@Sun.COM int (*writef)(Namval_t*, Sfio_t*, int, Namfun_t*); 684887Schin }; 694887Schin 704887Schin struct Namfun 714887Schin { 724887Schin const Namdisc_t *disc; 734887Schin char nofree; 748462SApril.Chin@Sun.COM unsigned char subshell; 754887Schin unsigned short dsize; 764887Schin Namfun_t *next; 774887Schin char *last; 784887Schin Namval_t *type; 794887Schin }; 804887Schin 814887Schin struct Nambfun 824887Schin { 834887Schin Namfun_t fun; 844887Schin int num; 854887Schin const char **bnames; 864887Schin Namval_t *bltins[1]; 874887Schin }; 884887Schin 894887Schin /* This is an array template header */ 904887Schin struct Namarray 914887Schin { 924887Schin Namfun_t hdr; 934887Schin long nelem; /* number of elements */ 944887Schin void *(*fun)(Namval_t*,const char*,int); /* associative arrays */ 954887Schin Namval_t *parent; /* for multi-dimensional */ 968462SApril.Chin@Sun.COM Dt_t *table; /* for subscripts */ 978462SApril.Chin@Sun.COM void *scope; /* non-zerp when scoped */ 984887Schin }; 994887Schin 1008462SApril.Chin@Sun.COM /* The context pointer for declaration command */ 1018462SApril.Chin@Sun.COM struct Namdecl 1024887Schin { 1038462SApril.Chin@Sun.COM Namval_t *tp; /* point to type */ 1044887Schin const char *optstring; 1054887Schin void *optinfof; 1064887Schin }; 1074887Schin 1084887Schin /* attributes of name-value node attribute flags */ 1094887Schin 1104887Schin #define NV_DEFAULT 0 1114887Schin /* This defines the attributes for an attributed name-value pair node */ 1124887Schin struct Namval 1134887Schin { 1144887Schin Dtlink_t nvlink; /* space for cdt links */ 1154887Schin char *nvname; /* pointer to name of the node */ 1164887Schin unsigned short nvflag; /* attributes */ 1174887Schin unsigned short nvsize; /* size or base */ 1184887Schin #ifdef _NV_PRIVATE 1194887Schin _NV_PRIVATE 1204887Schin #else 1214887Schin Namfun_t *nvfun; 1224887Schin char *nvalue; 1234887Schin char *nvprivate; 1244887Schin #endif /* _NV_PRIVATE */ 1254887Schin }; 1264887Schin 1274887Schin #define NV_CLASS ".sh.type" 1288462SApril.Chin@Sun.COM #define NV_DATA "_" /* special class or instance variable */ 1294887Schin #define NV_MINSZ (sizeof(struct Namval)-sizeof(Dtlink_t)-sizeof(char*)) 1304887Schin #define nv_namptr(p,n) ((Namval_t*)((char*)(p)+(n)*NV_MINSZ-sizeof(Dtlink_t))) 1314887Schin 1324887Schin /* The following attributes are for internal use */ 1334887Schin #define NV_NOFREE 0x200 /* don't free the space when releasing value */ 1344887Schin #define NV_ARRAY 0x400 /* node is an array */ 1354887Schin #define NV_REF 0x4000 /* reference bit */ 1364887Schin #define NV_TABLE 0x800 /* node is a dictionary table */ 1374887Schin #define NV_IMPORT 0x1000 /* value imported from environment */ 1384887Schin #define NV_MINIMAL NV_IMPORT /* node does not contain all fields */ 1394887Schin 1404887Schin #define NV_INTEGER 0x2 /* integer attribute */ 1414887Schin /* The following attributes are valid only when NV_INTEGER is off */ 1424887Schin #define NV_LTOU 0x4 /* convert to uppercase */ 1434887Schin #define NV_UTOL 0x8 /* convert to lowercase */ 1444887Schin #define NV_ZFILL 0x10 /* right justify and fill with leading zeros */ 1454887Schin #define NV_RJUST 0x20 /* right justify and blank fill */ 1464887Schin #define NV_LJUST 0x40 /* left justify and blank fill */ 1474887Schin #define NV_BINARY 0x100 /* fixed size data buffer */ 1484887Schin #define NV_RAW NV_LJUST /* used only with NV_BINARY */ 1494887Schin #define NV_HOST (NV_RJUST|NV_LJUST) /* map to host filename */ 1504887Schin 1514887Schin /* The following attributes do not effect the value */ 1524887Schin #define NV_RDONLY 0x1 /* readonly bit */ 1534887Schin #define NV_EXPORT 0x2000 /* export bit */ 1544887Schin #define NV_TAGGED 0x8000 /* user define tag bit */ 1554887Schin 1564887Schin /* The following are used with NV_INTEGER */ 1574887Schin #define NV_SHORT (NV_RJUST) /* when integers are not long */ 1584887Schin #define NV_LONG (NV_UTOL) /* for long long and long double */ 1594887Schin #define NV_UNSIGN (NV_LTOU) /* for unsigned quantities */ 1608462SApril.Chin@Sun.COM #define NV_DOUBLE (NV_INTEGER|NV_ZFILL) /* for floating point */ 1614887Schin #define NV_EXPNOTE (NV_LJUST) /* for scientific notation */ 1628462SApril.Chin@Sun.COM #define NV_HEXFLOAT (NV_LTOU) /* for C99 base16 float notation */ 1634887Schin 1644887Schin /* options for nv_open */ 1654887Schin 1664887Schin #define NV_APPEND 0x10000 /* append value */ 1678462SApril.Chin@Sun.COM #define NV_MOVE 0x8000000 /* for use with nv_clone */ 1684887Schin #define NV_ADD 8 1694887Schin /* add node if not found */ 1704887Schin #define NV_ASSIGN NV_NOFREE /* assignment is possible */ 1714887Schin #define NV_NOASSIGN 0 /* backward compatibility */ 1724887Schin #define NV_NOARRAY 0x200000 /* array name not possible */ 1734887Schin #define NV_IARRAY 0x400000 /* for indexed array */ 1744887Schin #define NV_NOREF NV_REF /* don't follow reference */ 1754887Schin #define NV_IDENT 0x80 /* name must be identifier */ 1764887Schin #define NV_VARNAME 0x20000 /* name must be ?(.)id*(.id) */ 1774887Schin #define NV_NOADD 0x40000 /* do not add node */ 1784887Schin #define NV_NOSCOPE 0x80000 /* look only in current scope */ 1794887Schin #define NV_NOFAIL 0x100000 /* return 0 on failure, no msg */ 1804887Schin #define NV_NODISC NV_IDENT /* ignore disciplines */ 1814887Schin 1824887Schin #define NV_FUNCT NV_IDENT /* option for nv_create */ 1838462SApril.Chin@Sun.COM #define NV_BLTINOPT NV_ZFILL /* mark builtins in libcmd */ 1844887Schin 1854887Schin #define NV_PUBLIC (~(NV_NOSCOPE|NV_ASSIGN|NV_IDENT|NV_VARNAME|NV_NOADD)) 1864887Schin 1874887Schin /* numeric types */ 1888462SApril.Chin@Sun.COM #define NV_INT16P (NV_LJUST|NV_SHORT|NV_INTEGER) 1894887Schin #define NV_INT16 (NV_SHORT|NV_INTEGER) 1904887Schin #define NV_UINT16 (NV_UNSIGN|NV_SHORT|NV_INTEGER) 1918462SApril.Chin@Sun.COM #define NV_UINT16P (NV_LJUSTNV_UNSIGN|NV_SHORT|NV_INTEGER) 1924887Schin #define NV_INT32 (NV_INTEGER) 1934887Schin #define NV_UNT32 (NV_UNSIGN|NV_INTEGER) 1944887Schin #define NV_INT64 (NV_LONG|NV_INTEGER) 1954887Schin #define NV_UINT64 (NV_UNSIGN|NV_LONG|NV_INTEGER) 1968462SApril.Chin@Sun.COM #define NV_FLOAT (NV_SHORT|NV_DOUBLE) 1978462SApril.Chin@Sun.COM #define NV_LDOUBLE (NV_LONG|NV_DOUBLE) 1984887Schin 1994887Schin /* name-value pair macros */ 2004887Schin #define nv_isattr(np,f) ((np)->nvflag & (f)) 2014887Schin #define nv_onattr(n,f) ((n)->nvflag |= (f)) 2024887Schin #define nv_offattr(n,f) ((n)->nvflag &= ~(f)) 2034887Schin #define nv_isarray(np) (nv_isattr((np),NV_ARRAY)) 2044887Schin 2054887Schin /* The following are operations for associative arrays */ 2064887Schin #define NV_AINIT 1 /* initialize */ 2074887Schin #define NV_AFREE 2 /* free array */ 2084887Schin #define NV_ANEXT 3 /* advance to next subscript */ 2094887Schin #define NV_ANAME 4 /* return subscript name */ 2104887Schin #define NV_ADELETE 5 /* delete current subscript */ 2114887Schin #define NV_AADD 6 /* add subscript if not found */ 2124887Schin #define NV_ACURRENT 7 /* return current subscript Namval_t* */ 2138462SApril.Chin@Sun.COM #define NV_ASETSUB 8 /* set current subscript */ 2144887Schin 2154887Schin /* The following are for nv_disc */ 2164887Schin #define NV_FIRST 1 2174887Schin #define NV_LAST 2 2184887Schin #define NV_POP 3 2194887Schin #define NV_CLONE 4 2204887Schin 2214887Schin /* The following are operations for nv_putsub() */ 2228462SApril.Chin@Sun.COM #define ARRAY_BITS 22 2234887Schin #define ARRAY_ADD (1L<<ARRAY_BITS) /* add subscript if not found */ 2244887Schin #define ARRAY_SCAN (2L<<ARRAY_BITS) /* For ${array[@]} */ 2254887Schin #define ARRAY_UNDEF (4L<<ARRAY_BITS) /* For ${array} */ 2264887Schin 2274887Schin 2284887Schin /* These are disciplines provided by the library for use with nv_discfun */ 2294887Schin #define NV_DCADD 0 /* used to add named disciplines */ 2304887Schin #define NV_DCRESTRICT 1 /* variable that are restricted in rsh */ 2314887Schin 2324887Schin #if defined(__EXPORT__) && defined(_DLL) 2334887Schin # ifdef _BLD_shell 2344887Schin # define extern __EXPORT__ 2354887Schin # else 2364887Schin # define extern __IMPORT__ 2374887Schin # endif /* _BLD_shell */ 2384887Schin #endif /* _DLL */ 2394887Schin /* prototype for array interface*/ 2408462SApril.Chin@Sun.COM extern Namarr_t *nv_arrayptr(Namval_t*); 2414887Schin extern Namarr_t *nv_setarray(Namval_t*,void*(*)(Namval_t*,const char*,int)); 2428462SApril.Chin@Sun.COM extern int nv_arraynsub(Namarr_t*); 2434887Schin extern void *nv_associative(Namval_t*,const char*,int); 2444887Schin extern int nv_aindex(Namval_t*); 2454887Schin extern int nv_nextsub(Namval_t*); 2464887Schin extern char *nv_getsub(Namval_t*); 2474887Schin extern Namval_t *nv_putsub(Namval_t*, char*, long); 2484887Schin extern Namval_t *nv_opensub(Namval_t*); 2494887Schin 2504887Schin /* name-value pair function prototypes */ 2514887Schin extern int nv_adddisc(Namval_t*, const char**, Namval_t**); 2524887Schin extern int nv_clone(Namval_t*, Namval_t*, int); 2534887Schin extern void nv_close(Namval_t*); 2544887Schin extern void *nv_context(Namval_t*); 2554887Schin extern Namval_t *nv_create(const char*, Dt_t*, int,Namfun_t*); 2568462SApril.Chin@Sun.COM extern void nv_delete(Namval_t*, Dt_t*, int); 2574887Schin extern Dt_t *nv_dict(Namval_t*); 2584887Schin extern Sfdouble_t nv_getn(Namval_t*, Namfun_t*); 2594887Schin extern Sfdouble_t nv_getnum(Namval_t*); 2604887Schin extern char *nv_getv(Namval_t*, Namfun_t*); 2614887Schin extern char *nv_getval(Namval_t*); 2624887Schin extern Namfun_t *nv_hasdisc(Namval_t*, const Namdisc_t*); 2634887Schin extern int nv_isnull(Namval_t*); 2648462SApril.Chin@Sun.COM extern Namfun_t *nv_isvtree(Namval_t*); 2654887Schin extern Namval_t *nv_lastdict(void); 2668462SApril.Chin@Sun.COM extern Namval_t *nv_mkinttype(char*, size_t, int, const char*, Namdisc_t*); 2674887Schin extern void nv_newattr(Namval_t*,unsigned,int); 26810898Sroland.mainz@nrubsig.org extern void nv_newtype(Namval_t*); 2694887Schin extern Namval_t *nv_open(const char*,Dt_t*,int); 2704887Schin extern void nv_putval(Namval_t*,const char*,int); 2714887Schin extern void nv_putv(Namval_t*,const char*,int,Namfun_t*); 2728462SApril.Chin@Sun.COM extern int nv_rename(Namval_t*,int); 2734887Schin extern int nv_scan(Dt_t*,void(*)(Namval_t*,void*),void*,int,int); 2744887Schin extern char *nv_setdisc(Namval_t*,const char*,Namval_t*,Namfun_t*); 2754887Schin extern void nv_setref(Namval_t*, Dt_t*,int); 2764887Schin extern int nv_settype(Namval_t*, Namval_t*, int); 2774887Schin extern void nv_setvec(Namval_t*,int,int,char*[]); 2784887Schin extern void nv_setvtree(Namval_t*); 2794887Schin extern int nv_setsize(Namval_t*,int); 2804887Schin extern Namfun_t *nv_disc(Namval_t*,Namfun_t*,int); 2818462SApril.Chin@Sun.COM extern void nv_unset(Namval_t*); /*obsolete */ 2828462SApril.Chin@Sun.COM extern void _nv_unset(Namval_t*,int); 2834887Schin extern Namval_t *nv_search(const char *, Dt_t*, int); 2844887Schin extern char *nv_name(Namval_t*); 2854887Schin extern Namval_t *nv_type(Namval_t*); 2868462SApril.Chin@Sun.COM extern void nv_addtype(Namval_t*,const char*, Optdisc_t*, size_t); 2874887Schin extern const Namdisc_t *nv_discfun(int); 2884887Schin 2894887Schin #ifdef _DLL 2904887Schin # undef extern 2914887Schin #endif /* _DLL */ 2924887Schin 2938462SApril.Chin@Sun.COM #define nv_unset(np) _nv_unset(np,0) 2944887Schin #define nv_size(np) nv_setsize((np),-1) 2954887Schin #define nv_stack(np,nf) nv_disc(np,nf,0) 2964887Schin 2974887Schin #if 0 2984887Schin /* 2994887Schin * The names of many functions were changed in early '95 3004887Schin * Here is a mapping to the old names 3014887Schin */ 3024887Schin # define nv_istype(np) nv_isattr(np) 3034887Schin # define nv_newtype(np) nv_newattr(np) 3044887Schin # define nv_namset(np,a,b) nv_open(np,a,b) 3058462SApril.Chin@Sun.COM # define nv_free(np) nv_unset(np,0) 3064887Schin # define nv_settype(np,a,b,c) nv_setdisc(np,a,b,c) 3074887Schin # define nv_search(np,a,b) nv_open(np,a,((b)?0:NV_NOADD)) 3084887Schin # define settype setdisc 3094887Schin #endif 3104887Schin 3114887Schin #endif /* NV_DEFAULT */ 312