1*11be35a1SLionel Sambuc /* $NetBSD: h_getopt_long.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2007 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*11be35a1SLionel Sambuc * by Christos Zoulas.
9*11be35a1SLionel Sambuc *
10*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
12*11be35a1SLionel Sambuc * are met:
13*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*11be35a1SLionel Sambuc *
19*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*11be35a1SLionel Sambuc */
31*11be35a1SLionel Sambuc
32*11be35a1SLionel Sambuc #include <ctype.h>
33*11be35a1SLionel Sambuc #include <err.h>
34*11be35a1SLionel Sambuc #include <getopt.h>
35*11be35a1SLionel Sambuc #include <stdio.h>
36*11be35a1SLionel Sambuc #include <string.h>
37*11be35a1SLionel Sambuc #include <stdlib.h>
38*11be35a1SLionel Sambuc #include <unistd.h>
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc #define SKIPWS(p) while (isspace((int)(*p))) p++
41*11be35a1SLionel Sambuc #define WS "\t\n "
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc int
main(int argc,char * argv[])44*11be35a1SLionel Sambuc main(int argc, char *argv[])
45*11be35a1SLionel Sambuc {
46*11be35a1SLionel Sambuc size_t len, lineno = 0;
47*11be35a1SLionel Sambuc char *line, *eptr, *longopt, *ptr, *optstring = NULL, *result = NULL;
48*11be35a1SLionel Sambuc char buf[1024];
49*11be35a1SLionel Sambuc char *args[128];
50*11be35a1SLionel Sambuc char arg[256];
51*11be35a1SLionel Sambuc int nargs = -1;
52*11be35a1SLionel Sambuc int c;
53*11be35a1SLionel Sambuc int nlongopts = 0;
54*11be35a1SLionel Sambuc int maxnlongopts = 0;
55*11be35a1SLionel Sambuc int *longopt_flags = NULL;
56*11be35a1SLionel Sambuc struct option *longopts = NULL;
57*11be35a1SLionel Sambuc
58*11be35a1SLionel Sambuc while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
59*11be35a1SLionel Sambuc if (strncmp(line, "optstring:", 10) == 0) {
60*11be35a1SLionel Sambuc if (optstring)
61*11be35a1SLionel Sambuc free(optstring);
62*11be35a1SLionel Sambuc optstring = strtok(&line[11], WS);
63*11be35a1SLionel Sambuc if (optstring == NULL)
64*11be35a1SLionel Sambuc errx(1, "missing optstring at line %ld",
65*11be35a1SLionel Sambuc (unsigned long)lineno);
66*11be35a1SLionel Sambuc optstring = strdup(optstring);
67*11be35a1SLionel Sambuc } else if (strncmp(line, "longopts:", 9) == 0) {
68*11be35a1SLionel Sambuc if (longopts) {
69*11be35a1SLionel Sambuc int i;
70*11be35a1SLionel Sambuc for (i = 0; i < nlongopts; i++)
71*11be35a1SLionel Sambuc if (longopts[i].name != NULL)
72*11be35a1SLionel Sambuc free(__UNCONST(longopts[i].name));
73*11be35a1SLionel Sambuc free(longopts);
74*11be35a1SLionel Sambuc }
75*11be35a1SLionel Sambuc if (longopt_flags)
76*11be35a1SLionel Sambuc free(longopt_flags);
77*11be35a1SLionel Sambuc nlongopts = 0;
78*11be35a1SLionel Sambuc ptr = strtok(&line[10], WS);
79*11be35a1SLionel Sambuc if (ptr == NULL)
80*11be35a1SLionel Sambuc errx(1, "missing longopts at line %ld",
81*11be35a1SLionel Sambuc (unsigned long)lineno);
82*11be35a1SLionel Sambuc maxnlongopts = strtoul(ptr, &eptr, 10);
83*11be35a1SLionel Sambuc if (*eptr != '\0')
84*11be35a1SLionel Sambuc warnx("garbage in longopts at line %ld",
85*11be35a1SLionel Sambuc (unsigned long)lineno);
86*11be35a1SLionel Sambuc maxnlongopts++; /* space for trailer */
87*11be35a1SLionel Sambuc longopts =
88*11be35a1SLionel Sambuc (struct option *)calloc(sizeof(struct option),
89*11be35a1SLionel Sambuc maxnlongopts);
90*11be35a1SLionel Sambuc if (longopts == NULL)
91*11be35a1SLionel Sambuc err(1, "calloc");
92*11be35a1SLionel Sambuc longopt_flags = (int *)calloc(sizeof(int), maxnlongopts);
93*11be35a1SLionel Sambuc if (longopt_flags == NULL)
94*11be35a1SLionel Sambuc err(1, "calloc");
95*11be35a1SLionel Sambuc } else if (strncmp(line, "longopt:", 8) == 0) {
96*11be35a1SLionel Sambuc if (longopts == NULL)
97*11be35a1SLionel Sambuc errx(1, "longopt: without longopts at line %ld",
98*11be35a1SLionel Sambuc (unsigned long)lineno);
99*11be35a1SLionel Sambuc if (nlongopts >= maxnlongopts)
100*11be35a1SLionel Sambuc errx(1, "longopt: too many options at line %ld",
101*11be35a1SLionel Sambuc (unsigned long)lineno);
102*11be35a1SLionel Sambuc /* name */
103*11be35a1SLionel Sambuc ptr = &line[9];
104*11be35a1SLionel Sambuc SKIPWS(ptr);
105*11be35a1SLionel Sambuc longopt = strsep(&ptr, ",");
106*11be35a1SLionel Sambuc if (longopt == NULL)
107*11be35a1SLionel Sambuc errx(1, "missing longopt at line %ld",
108*11be35a1SLionel Sambuc (unsigned long)lineno);
109*11be35a1SLionel Sambuc longopts[nlongopts].name = strdup(longopt);
110*11be35a1SLionel Sambuc /* has_arg */
111*11be35a1SLionel Sambuc SKIPWS(ptr);
112*11be35a1SLionel Sambuc longopt = strsep(&ptr, ",");
113*11be35a1SLionel Sambuc if (*longopt != '\0') {
114*11be35a1SLionel Sambuc if (strncmp(longopt, "0", 1) == 0 ||
115*11be35a1SLionel Sambuc strncmp(longopt, "no_argument", 2) == 0)
116*11be35a1SLionel Sambuc longopts[nlongopts].has_arg = no_argument;
117*11be35a1SLionel Sambuc else if (strncmp(longopt, "1", 1) == 0 ||
118*11be35a1SLionel Sambuc strncmp(longopt, "required_argument", 8) == 0)
119*11be35a1SLionel Sambuc longopts[nlongopts].has_arg = required_argument;
120*11be35a1SLionel Sambuc else if (strncmp(longopt, "2", 1) == 0 ||
121*11be35a1SLionel Sambuc strncmp(longopt, "optional_argument", 8) == 0)
122*11be35a1SLionel Sambuc longopts[nlongopts].has_arg = optional_argument;
123*11be35a1SLionel Sambuc else
124*11be35a1SLionel Sambuc errx(1, "unknown has_arg %s at line %ld",
125*11be35a1SLionel Sambuc longopt, (unsigned long)lineno);
126*11be35a1SLionel Sambuc }
127*11be35a1SLionel Sambuc /* flag */
128*11be35a1SLionel Sambuc SKIPWS(ptr);
129*11be35a1SLionel Sambuc longopt = strsep(&ptr, ",");
130*11be35a1SLionel Sambuc if (*longopt != '\0' &&
131*11be35a1SLionel Sambuc strncmp(longopt, "NULL", 4) != 0)
132*11be35a1SLionel Sambuc longopts[nlongopts].flag = &longopt_flags[nlongopts];
133*11be35a1SLionel Sambuc /* val */
134*11be35a1SLionel Sambuc SKIPWS(ptr);
135*11be35a1SLionel Sambuc longopt = strsep(&ptr, ",");
136*11be35a1SLionel Sambuc if (*longopt == '\0')
137*11be35a1SLionel Sambuc errx(1, "missing val at line %ld",
138*11be35a1SLionel Sambuc (unsigned long)lineno);
139*11be35a1SLionel Sambuc if (*longopt != '\'') {
140*11be35a1SLionel Sambuc longopts[nlongopts].val =
141*11be35a1SLionel Sambuc (int)strtoul(longopt, &eptr, 10);
142*11be35a1SLionel Sambuc if (*eptr != '\0')
143*11be35a1SLionel Sambuc errx(1, "invalid val at line %ld",
144*11be35a1SLionel Sambuc (unsigned long)lineno);
145*11be35a1SLionel Sambuc } else
146*11be35a1SLionel Sambuc longopts[nlongopts].val = (int)longopt[1];
147*11be35a1SLionel Sambuc nlongopts++;
148*11be35a1SLionel Sambuc } else if (strncmp(line, "args:", 5) == 0) {
149*11be35a1SLionel Sambuc for (; nargs >= 0; nargs--) {
150*11be35a1SLionel Sambuc if (args[nargs] != NULL)
151*11be35a1SLionel Sambuc free(args[nargs]);
152*11be35a1SLionel Sambuc }
153*11be35a1SLionel Sambuc args[nargs = 0] = strtok(&line[6], WS);
154*11be35a1SLionel Sambuc if (args[nargs] == NULL)
155*11be35a1SLionel Sambuc errx(1, "Missing args");
156*11be35a1SLionel Sambuc
157*11be35a1SLionel Sambuc args[nargs] = strdup(args[nargs]);
158*11be35a1SLionel Sambuc while ((args[++nargs] = strtok(NULL, WS)) != NULL)
159*11be35a1SLionel Sambuc args[nargs] = strdup(args[nargs]);
160*11be35a1SLionel Sambuc } else if (strncmp(line, "result:", 7) == 0) {
161*11be35a1SLionel Sambuc int li;
162*11be35a1SLionel Sambuc buf[0] = '\0';
163*11be35a1SLionel Sambuc optind = optreset = 1;
164*11be35a1SLionel Sambuc if (result)
165*11be35a1SLionel Sambuc free(result);
166*11be35a1SLionel Sambuc result = strtok(&line[8], WS);
167*11be35a1SLionel Sambuc if (result == NULL)
168*11be35a1SLionel Sambuc errx(1, "missing result at line %ld",
169*11be35a1SLionel Sambuc (unsigned long)lineno);
170*11be35a1SLionel Sambuc if (optstring == NULL)
171*11be35a1SLionel Sambuc errx(1, "result: without optstring");
172*11be35a1SLionel Sambuc if (longopts == NULL || nlongopts == 0)
173*11be35a1SLionel Sambuc errx(1, "result: without longopts");
174*11be35a1SLionel Sambuc result = strdup(result);
175*11be35a1SLionel Sambuc if (nargs == -1)
176*11be35a1SLionel Sambuc errx(1, "result: without args");
177*11be35a1SLionel Sambuc li = -2;
178*11be35a1SLionel Sambuc while ((c = getopt_long(nargs, args, optstring,
179*11be35a1SLionel Sambuc longopts, &li)) != -1) {
180*11be35a1SLionel Sambuc if (c == ':')
181*11be35a1SLionel Sambuc errx(1, "`:' found as argument char");
182*11be35a1SLionel Sambuc if (li == -2) {
183*11be35a1SLionel Sambuc ptr = strchr(optstring, c);
184*11be35a1SLionel Sambuc if (ptr == NULL) {
185*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg),
186*11be35a1SLionel Sambuc "!%c,", c);
187*11be35a1SLionel Sambuc strcat(buf, arg);
188*11be35a1SLionel Sambuc continue;
189*11be35a1SLionel Sambuc }
190*11be35a1SLionel Sambuc if (ptr[1] != ':')
191*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg),
192*11be35a1SLionel Sambuc "%c,", c);
193*11be35a1SLionel Sambuc else
194*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg),
195*11be35a1SLionel Sambuc "%c=%s,", c, optarg);
196*11be35a1SLionel Sambuc } else {
197*11be35a1SLionel Sambuc switch (longopts[li].has_arg) {
198*11be35a1SLionel Sambuc case no_argument:
199*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg), "-%s,",
200*11be35a1SLionel Sambuc longopts[li].name);
201*11be35a1SLionel Sambuc break;
202*11be35a1SLionel Sambuc case required_argument:
203*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg),
204*11be35a1SLionel Sambuc "-%s=%s,",
205*11be35a1SLionel Sambuc longopts[li].name, optarg);
206*11be35a1SLionel Sambuc break;
207*11be35a1SLionel Sambuc case optional_argument:
208*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg),
209*11be35a1SLionel Sambuc "-%s%s%s,",
210*11be35a1SLionel Sambuc longopts[li].name,
211*11be35a1SLionel Sambuc (optarg)? "=" : "",
212*11be35a1SLionel Sambuc (optarg)? optarg : "");
213*11be35a1SLionel Sambuc break;
214*11be35a1SLionel Sambuc default:
215*11be35a1SLionel Sambuc errx(1, "internal error");
216*11be35a1SLionel Sambuc }
217*11be35a1SLionel Sambuc }
218*11be35a1SLionel Sambuc strcat(buf, arg);
219*11be35a1SLionel Sambuc li = -2;
220*11be35a1SLionel Sambuc }
221*11be35a1SLionel Sambuc len = strlen(buf);
222*11be35a1SLionel Sambuc if (len > 0) {
223*11be35a1SLionel Sambuc buf[len - 1] = '|';
224*11be35a1SLionel Sambuc buf[len] = '\0';
225*11be35a1SLionel Sambuc } else {
226*11be35a1SLionel Sambuc buf[0] = '|';
227*11be35a1SLionel Sambuc buf[1] = '\0';
228*11be35a1SLionel Sambuc }
229*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg), "%d", nargs - optind);
230*11be35a1SLionel Sambuc strcat(buf, arg);
231*11be35a1SLionel Sambuc if (strcmp(buf, result) != 0)
232*11be35a1SLionel Sambuc errx(1, "`%s' != `%s'", buf, result);
233*11be35a1SLionel Sambuc } else
234*11be35a1SLionel Sambuc errx(1, "unknown directive at line %ld",
235*11be35a1SLionel Sambuc (unsigned long)lineno);
236*11be35a1SLionel Sambuc free(line);
237*11be35a1SLionel Sambuc }
238*11be35a1SLionel Sambuc return 0;
239*11be35a1SLionel Sambuc }
240