enum.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly/***********************************************************************
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens Elkner* *
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* *
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* *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* Information and Software Systems Research *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* AT&T Research *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* Florham Park NJ *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly* *
7db74d99f2d2705558510202067b91aca1912f6fLiam O'Reilly* David Korn <dgk@research.att.com> *
7db74d99f2d2705558510202067b91aca1912f6fLiam O'Reilly* *
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly***********************************************************************/
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly#pragma prototyped
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly#include <shell.h>
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reillystatic const char enum_usage[] =
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly"[-?@(#)$Id: enum (AT&T Research) 2008-01-08 $\n]"
9738b4e358f960105062839c835bb9eff3e44588Liam O'ReillyUSAGE_LICENSE
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.]"
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly"\n"
a9cc955b0a33e631b9ce3404e4e25b62cd067a77Christian Maeder"\n\atypename\a[\b=(\b \avalue\a ... \b)\b]\n"
9738b4e358f960105062839c835bb9eff3e44588Liam O'Reilly"\n"
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder"[+EXIT STATUS]"
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder "{"
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly "[+0?Successful completion.]"
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly "[+>0?An error occurred.]"
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly "}"
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly"[+SEE ALSO?\bksh\b(1), \btypeset\b(1).]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly;
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reillystatic const char enum_type[] =
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly"[-1c?\n@(#)$Id: type (AT&T Labs Research) 2008-01-08 $\n]"
c3efd4f435e954846981cf46bca64e0485266634Liam O'ReillyUSAGE_LICENSE
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#if 0
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#endif
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"\n"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"\n[name[=value]...]\n"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder"\n"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder"[+EXIT STATUS?]{"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "[+0?Successful completion.]"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder "[+>0?An error occurred.]"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder"}"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly"[+SEE ALSO?\benum\b(1), \btypeset\b(1)]"
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly;
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reillystruct Enum
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly{
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly Namfun_t hdr;
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly short nelem;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly short iflag;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder const char *values[1];
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder};
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroederstatic int enuminfo(Opt_t* op, Sfio_t *out, const char *str, Optdisc_t *fp)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder{
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder Namval_t *np;
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly struct Enum *ep;
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly int n=0;
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly const char *v;
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly np = *(Namval_t**)(fp+1);
7db74d99f2d2705558510202067b91aca1912f6fLiam O'Reilly ep = (struct Enum*)np->nvfun;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly if(strcmp(str,"default")==0)
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly#if 0
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly sfprintf(out,"\b%s\b%c",ep->values[0],0);
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly#else
05cc55892e6c93bdd7b9c3f100ab1bb65fe6a21eLiam O'Reilly sfprintf(out,"\b%s\b",ep->values[0]);
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly#endif
05cc55892e6c93bdd7b9c3f100ab1bb65fe6a21eLiam O'Reilly else if(strcmp(str,"case")==0)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder {
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder if(ep->iflag)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder sfprintf(out,"not ");
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder }
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly else while(v=ep->values[n++])
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly {
abc67c4b359c4a5bb953c6d55fb0f9133369d707Liam O'Reilly sfprintf(out,", \b%s\b",v);
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly }
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly return(0);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder}
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder
abc67c4b359c4a5bb953c6d55fb0f9133369d707Liam O'Reillystatic Namfun_t *clone_enum(Namval_t* np, Namval_t *mp, int flags, Namfun_t *fp)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder{
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly struct Enum *ep, *pp=(struct Enum*)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*));
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder return(&ep->hdr);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder}
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reillystatic void put_enum(Namval_t* np,const char *val,int flags,Namfun_t *fp)
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly{
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly struct Enum *ep = (struct Enum*)fp;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly register const char *v;
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly unsigned short i=0, n;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder if(!val)
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly {
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder nv_putv(np, val, flags,fp);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder nv_disc(np,&ep->hdr,NV_POP);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder if(!ep->hdr.nofree)
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly free((void*)ep);
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly return;
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly }
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly if(flags&NV_INTEGER)
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly {
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly nv_putv(np,val,flags,fp);
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly return;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly }
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly while(v=ep->values[i])
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly {
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly if(ep->iflag)
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly n = strcasecmp(v,val);
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly else
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly n = strcmp(v,val);
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly if(n==0)
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly {
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly nv_putv(np, (char*)&i, NV_UINT16, fp);
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly return;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly }
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly i++;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly }
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly if(nv_isattr(np,NV_NOFREE))
7db74d99f2d2705558510202067b91aca1912f6fLiam O'Reilly error(ERROR_exit(1), "%s: invalid value %s",nv_name(np),val);
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly}
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reillystatic char* get_enum(register Namval_t* np, Namfun_t *fp)
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly{
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly static char buff[6];
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly struct Enum *ep = (struct Enum*)fp;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder long n = nv_getn(np,fp);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder if(n < ep->nelem)
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly return((char*)ep->values[n]);
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly sfsprintf(buff,sizeof(buff),"%u%c",n,0);
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly return(buff);
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly}
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly
dd7da1b5fedc05b92ba023ebd803e6f4a662503bChristian Maederstatic Sfdouble_t get_nenum(register Namval_t* np, Namfun_t *fp)
dd7da1b5fedc05b92ba023ebd803e6f4a662503bChristian Maeder{
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder return(nv_getn(np,fp));
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder}
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reillyconst Namdisc_t ENUM_disc = { 0, put_enum, get_enum, get_nenum, 0,0,clone_enum };
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reilly
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reilly#ifdef STANDALONE
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reillystatic int enum_create(int argc, char** argv, Shbltin_t *context)
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reilly#else
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reillyint b_enum(int argc, char** argv, Shbltin_t *context)
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reilly#endif
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reilly{
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly int sz,i,n,iflag = 0;
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly Namval_t *np, *tp;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder Namarr_t *ap;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder char *cp,*sp;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder struct Enum *ep;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder Shell_t *shp = context->shp;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder struct {
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder Optdisc_t opt;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder Namval_t *np;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly } optdisc;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY);
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly for (;;)
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly {
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly switch (optget(argv, enum_usage))
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly {
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly case 'i':
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly iflag = 'i';
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly continue;
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly case '?':
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly error(ERROR_USAGE|4, "%s", opt_info.arg);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly break;
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly case ':':
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly error(2, "%s", opt_info.arg);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly break;
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly }
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly break;
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly }
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly argv += opt_info.index;
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly if (error_info.errors || !*argv || *(argv + 1))
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly {
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly error(ERROR_USAGE|2, "%s", optusage(NiL));
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly return 1;
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly }
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly while(cp = *argv++)
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly {
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 n = staktell();
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 stakseek(n);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder n = sz;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder i = 0;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder nv_onattr(tp, NV_UINT16);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly nv_putval(tp, (char*)&i, NV_INTEGER);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly nv_putsub(np, (char*)0, ARRAY_SCAN);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly do
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly {
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly sz += strlen(nv_getval(np));
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly }
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly while(nv_nextsub(np));
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly sz += n*sizeof(char*);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly if(!(ep = newof(0,struct Enum,1,sz)))
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder error(ERROR_system(1), "out of space");
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder ep->iflag = iflag;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder ep->nelem = n;
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly cp = (char*)&ep->values[n+1];
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly nv_putsub(np, (char*)0, ARRAY_SCAN);
9e5f4073e948104307d43c3962d624b8416f191fLiam O'Reilly ep->values[n] = 0;
22ebb9b9649c48a21572cbaed8d7f4b8428af893Christian Maeder i = 0;
22ebb9b9649c48a21572cbaed8d7f4b8428af893Christian Maeder do
9738b4e358f960105062839c835bb9eff3e44588Liam O'Reilly {
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder ep->values[i++] = cp;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder sp = nv_getval(np);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder n = strlen(sp);
9738b4e358f960105062839c835bb9eff3e44588Liam O'Reilly memcpy(cp,sp,n+1);
9738b4e358f960105062839c835bb9eff3e44588Liam O'Reilly cp += n+1;
9738b4e358f960105062839c835bb9eff3e44588Liam O'Reilly }
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder while(nv_nextsub(np));
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder ep->hdr.dsize = sizeof(struct Enum)+sz;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly ep->hdr.disc = &ENUM_disc;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly ep->hdr.type = tp;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly nv_onattr(tp, NV_RDONLY);
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly nv_disc(tp, &ep->hdr,NV_FIRST);
7db74d99f2d2705558510202067b91aca1912f6fLiam O'Reilly memset(&optdisc,0,sizeof(optdisc));
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly optdisc.opt.infof = enuminfo;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly optdisc.np = tp;
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly nv_addtype(tp, enum_type, &optdisc.opt, sizeof(optdisc));
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly }
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly return error_info.errors != 0;
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly}
d3b4ad111a281d125659e12d6641943f29d6b3dfLiam O'Reilly
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly#ifdef STANDALONE
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroedervoid lib_init(int flag, void* context)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder{
c3efd4f435e954846981cf46bca64e0485266634Liam O'Reilly Shell_t *shp = ((Shbltin_t*)context)->shp;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder Namval_t *mp,*bp;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder if(flag)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder return;
0e51f08fb6ced8e6a9e69eb5976fcc20dbf07019Liam O'Reilly bp = sh_addbuiltin("Enum", enum_create, (void*)0);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder mp = nv_search("typeset",shp->bltin_tree,0);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder nv_onattr(bp,nv_isattr(mp,NV_PUBLIC));
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder}
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder#endif
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder