xref: /titanic_50/usr/src/lib/libast/common/comp/getoptl.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 #include <ast.h>
25 
26 #undef	_BLD_ast	/* enable ast imports since we're user static */
27 
28 #include <error.h>
29 #include <option.h>
30 #include <getopt.h>
31 #include <ctype.h>
32 
33 static const char*		lastoptstring;
34 static const struct option*	lastlongopts;
35 static char*			usage;
36 static Sfio_t*			up;
37 
38 static int			lastoptind;
39 
40 static int
41 golly(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex, int flags)
42 {
43 	register char*			s;
44 	register const struct option*	o;
45 	register int			c;
46 	char*				t;
47 
48 	if (!up || optstring != lastoptstring || longopts != lastlongopts)
49 	{
50 		if (!up && !(up = sfstropen()))
51 			return -1;
52 		sfprintf(up, "[-1p%d]", flags);
53 		t = strdup(optstring);
54 		for (o = longopts; o->name; o++)
55 		{
56 			if (o->flag || o->val <= 0 || o->val > UCHAR_MAX || !isalnum(o->val))
57 				sfprintf(up, "\n[%d:%s]", UCHAR_MAX + 1 + (o - longopts), o->name);
58 			else
59 			{
60 				sfprintf(up, "\n[%c:%s]", o->val, o->name);
61 				if (s = strchr(t, o->val))
62 				{
63 					*s++ = ' ';
64 					if (*s == ':')
65 					{
66 						*s++ = ' ';
67 						if (*s == ':')
68 							*s = ' ';
69 					}
70 				}
71 			}
72 			if (o->has_arg)
73 			{
74 				sfputc(up, ':');
75 				if (o->has_arg == optional_argument)
76 					sfputc(up, '?');
77 				sfprintf(up, "[string]");
78 			}
79 		}
80 		s = t;
81 		while (c = *s++)
82 			if (c != ' ')
83 			{
84 				sfprintf(up, "\n[%c]", c);
85 				if (*s == ':')
86 				{
87 					sfputc(up, *s);
88 					if (*++s == ':')
89 					{
90 						sfputc(up, '?');
91 						s++;
92 					}
93 					sfputc(up, '[');
94 					sfputc(up, ']');
95 				}
96 			}
97 		sfputc(up, '\n');
98 		if (!(usage = sfstruse(up)))
99 			return -1;
100 		lastoptstring = optstring;
101 		lastlongopts = longopts;
102 	}
103 	opt_info.index = (optind > 1 || optind == lastoptind) ? optind : 0;
104 	if (opt_info.index >= argc || !(c = optget((char**)argv, usage)))
105 	{
106 		sfstrclose(up);
107 		up = 0;
108 		c = -1;
109 	}
110 	else
111 	{
112 		if (c == ':' || c == '?')
113 		{
114 			if (opterr && (!optstring || *optstring != ':'))
115 			{
116 				if (!error_info.id)
117 					error_info.id = argv[0];
118 				errormsg(NiL, c == '?' ? (ERROR_USAGE|4) : 2, "%s", opt_info.arg);
119 			}
120 			optopt = opt_info.option[1];
121 			c = '?';
122 		}
123 		optarg = opt_info.arg;
124 		if (c < 0)
125 		{
126 			o = longopts - c - UCHAR_MAX - 1;
127 			if (o->flag)
128 			{
129 				*o->flag = o->val;
130 				c = 0;
131 			}
132 			else
133 				c = o->val;
134 		}
135 	}
136 	lastoptind = optind = opt_info.index;
137 	return c;
138 }
139 
140 extern int
141 getopt_long(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex)
142 {
143 	return golly(argc, argv, optstring, longopts, longindex, 2);
144 }
145 
146 extern int
147 getopt_long_only(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex)
148 {
149 	return golly(argc, argv, optstring, longopts, longindex, 1);
150 }
151