enum.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly/***********************************************************************
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* This software is part of the ast package *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* Copyright (c) 1982-2012 AT&T Intellectual Property *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* and is licensed under the *
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu* Eclipse Public License, Version 1.0 *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* by AT&T Intellectual Property *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* A copy of the License is available at *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* http://www.eclipse.org/org/documents/epl-v10.html *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* Information and Software Systems Research *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* AT&T Research *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* Florham Park NJ *
7db74d99f2d2705558510202067b91aca1912f6fLiam O'Reilly* David Korn <dgk@research.att.com> *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly***********************************************************************/
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reillystatic const char enum_usage[] =
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly"[-?@(#)$Id: enum (AT&T Research) 2008-01-08 $\n]"
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder"[+NAME?enum - create an enumeration type]"
abc67c4b359c4a5bb953c6d55fb0f9133369d707Liam O'Reilly"[+DESCRIPTION?\benum\b is a declaration command that creates an enumeration "
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly "type \atypename\a that can only store any one of the values in the indexed "
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly "array variable \atypename\a.]"
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly"[+?If the list of \avalue\as is omitted, then \atypename\a must name an "
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly "indexed array variable with at least two elements.]"
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly"[i:ignorecase?The values are case insensitive.]"
a9cc955b0a33e631b9ce3404e4e25b62cd067a77Christian Maeder"\n\atypename\a[\b=(\b \avalue\a ... \b)\b]\n"
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder"[+EXIT STATUS]"
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly "[+0?Successful completion.]"
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly "[+>0?An error occurred.]"
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly"[+SEE ALSO?\bksh\b(1), \btypeset\b(1).]"
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reillystatic const char enum_type[] =
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly"[-1c?\n@(#)$Id: type (AT&T Labs Research) 2008-01-08 $\n]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"[+NAME?\f?\f - create an instance of type \b\f?\f\b]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"[+DESCRIPTION?\b\f?\f\b creates a variable for each \aname\a with "
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly "enumeration type \b\f?\f\b where \b\f?\f\b is a type that has been "
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly "created with the \benum\b(1) command.]"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder"[+?The variable can have one of the following values\fvalues\f. "
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "The the values are \fcase\fcase sensitive.]"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder"[+?If \b=\b\avalue\a is omitted, the default is \fdefault\f.]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"[+?If no \aname\as are specified then the names and values of all "
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "variables of this type are written to standard output.]"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder"[+?\b\f?\f\b is built-in to the shell as a declaration command so that "
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly "field splitting and pathname expansion are not performed on "
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly "the arguments. Tilde expansion occurs on \avalue\a.]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"[r?Enables readonly. Once enabled, the value cannot be changed or unset.]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"[a?index array. Each \aname\a will converted to an index "
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "array of type \b\f?\f\b. If a variable already exists, the current "
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "value will become index \b0\b.]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"[A?Associative array. Each \aname\a will converted to an associate "
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "array of type \b\f?\f\b. If a variable already exists, the current "
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "value will become subscript \b0\b.]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"[h]:[string?Used within a type definition to provide a help string "
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly "for variable \aname\a. Otherwise, it is ignored.]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"[S?Used with a type definition to indicate that the variable is shared by "
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly "each instance of the type. When used inside a function defined "
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly "with the \bfunction\b reserved word, the specified variables "
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly "will have function static scope. Otherwise, the variable is "
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "unset prior to processing the assignment list.]"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder"[p?Causes the output to be in a form of \b\f?\f\b commands that can be "
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly "used as input to the shell to recreate the current type of "
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly "these variables.]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"\n[name[=value]...]\n"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder"[+EXIT STATUS?]{"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "[+0?Successful completion.]"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "[+>0?An error occurred.]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"[+SEE ALSO?\benum\b(1), \btypeset\b(1)]"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroederstatic int enuminfo(Opt_t* op, Sfio_t *out, const char *str, Optdisc_t *fp)
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly const char *v;
abc67c4b359c4a5bb953c6d55fb0f9133369d707Liam O'Reillystatic Namfun_t *clone_enum(Namval_t* np, Namval_t *mp, int flags, Namfun_t *fp)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder ep = newof(0,struct Enum,1,pp->nelem*sizeof(char*));
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder memcpy((void*)ep,(void*)pp,sizeof(struct Enum)+pp->nelem*sizeof(char*));
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reillystatic void put_enum(Namval_t* np,const char *val,int flags,Namfun_t *fp)
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly register const char *v;
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly unsigned short i=0, n;
7db74d99f2d2705558510202067b91aca1912f6fLiam O'Reilly error(ERROR_exit(1), "%s: invalid value %s",nv_name(np),val);
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reillystatic char* get_enum(register Namval_t* np, Namfun_t *fp)
dd7da1b5fedc05b92ba023ebd803e6f4a662503bChristian Maederstatic Sfdouble_t get_nenum(register Namval_t* np, Namfun_t *fp)
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reillyconst Namdisc_t ENUM_disc = { 0, put_enum, get_enum, get_nenum, 0,0,clone_enum };
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reillystatic int enum_create(int argc, char** argv, Shbltin_t *context)
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reillyint b_enum(int argc, char** argv, Shbltin_t *context)
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly if (error_info.errors || !*argv || *(argv + 1))
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly if(!(np = nv_open(cp, (void*)0, NV_VARNAME|NV_NOADD)) || !(ap=nv_arrayptr(np)) || ap->fun || (sz=ap->nelem&(((1L<<ARRAY_BITS)-1))) < 2)
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly error(ERROR_exit(1), "%s must name an array containing at least two elements",cp);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly sfprintf(stkstd,"%s.%s%c",NV_CLASS,np->nvname,0);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly tp = nv_open(stakptr(n), shp->var_tree, NV_VARNAME);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly sz += n*sizeof(char*);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder error(ERROR_system(1), "out of space");
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder ep->hdr.dsize = sizeof(struct Enum)+sz;
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly nv_addtype(tp, enum_type, &optdisc.opt, sizeof(optdisc));
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroedervoid lib_init(int flag, void* context)
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly bp = sh_addbuiltin("Enum", enum_create, (void*)0);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder mp = nv_search("typeset",shp->bltin_tree,0);