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