1*11be35a1SLionel Sambuc /* $NetBSD: h_getopt.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2002 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 <stdio.h>
33*11be35a1SLionel Sambuc #include <string.h>
34*11be35a1SLionel Sambuc #include <stdlib.h>
35*11be35a1SLionel Sambuc #include <unistd.h>
36*11be35a1SLionel Sambuc #include <err.h>
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #define WS "\t\n "
39*11be35a1SLionel Sambuc #define debug 0
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc int
main(int argc,char * argv[])42*11be35a1SLionel Sambuc main(int argc, char *argv[])
43*11be35a1SLionel Sambuc {
44*11be35a1SLionel Sambuc size_t len, lineno = 0;
45*11be35a1SLionel Sambuc char *line, *ptr, *optstring = NULL, *result = NULL;
46*11be35a1SLionel Sambuc char buf[1024];
47*11be35a1SLionel Sambuc char *args[100];
48*11be35a1SLionel Sambuc char arg[100];
49*11be35a1SLionel Sambuc int nargs = -1;
50*11be35a1SLionel Sambuc int c;
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
53*11be35a1SLionel Sambuc if (strncmp(line, "load:", 5) == 0) {
54*11be35a1SLionel Sambuc if (optstring)
55*11be35a1SLionel Sambuc free(optstring);
56*11be35a1SLionel Sambuc optstring = strtok(&line[6], WS);
57*11be35a1SLionel Sambuc if (optstring == NULL)
58*11be35a1SLionel Sambuc errx(1, "missing optstring at line %ld",
59*11be35a1SLionel Sambuc (unsigned long)lineno);
60*11be35a1SLionel Sambuc optstring = strdup(optstring);
61*11be35a1SLionel Sambuc if (debug)
62*11be35a1SLionel Sambuc fprintf(stderr, "optstring = %s\n", optstring);
63*11be35a1SLionel Sambuc } else if (strncmp(line, "args:", 5) == 0) {
64*11be35a1SLionel Sambuc for (; nargs >= 0; nargs--) {
65*11be35a1SLionel Sambuc if (args[nargs] != NULL)
66*11be35a1SLionel Sambuc free(args[nargs]);
67*11be35a1SLionel Sambuc }
68*11be35a1SLionel Sambuc args[nargs = 0] = strtok(&line[6], WS);
69*11be35a1SLionel Sambuc if (args[nargs] == NULL)
70*11be35a1SLionel Sambuc errx(1, "missing args at line %ld",
71*11be35a1SLionel Sambuc (unsigned long)lineno);
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc args[nargs] = strdup(args[nargs]);
74*11be35a1SLionel Sambuc while ((args[++nargs] = strtok(NULL, WS)) != NULL)
75*11be35a1SLionel Sambuc args[nargs] = strdup(args[nargs]);
76*11be35a1SLionel Sambuc if (debug) {
77*11be35a1SLionel Sambuc int i = 0;
78*11be35a1SLionel Sambuc for (i = 0; i < nargs; i++)
79*11be35a1SLionel Sambuc fprintf(stderr, "argv[%d] = %s\n", i,
80*11be35a1SLionel Sambuc args[i]);
81*11be35a1SLionel Sambuc }
82*11be35a1SLionel Sambuc } else if (strncmp(line, "result:", 7) == 0) {
83*11be35a1SLionel Sambuc buf[0] = '\0';
84*11be35a1SLionel Sambuc optind = optreset = 1;
85*11be35a1SLionel Sambuc if (result)
86*11be35a1SLionel Sambuc free(result);
87*11be35a1SLionel Sambuc result = strtok(&line[8], WS);
88*11be35a1SLionel Sambuc if (result == NULL)
89*11be35a1SLionel Sambuc errx(1, "missing result at line %ld",
90*11be35a1SLionel Sambuc (unsigned long)lineno);
91*11be35a1SLionel Sambuc result = strdup(result);
92*11be35a1SLionel Sambuc if (nargs == -1)
93*11be35a1SLionel Sambuc errx(1, "result: without args:");
94*11be35a1SLionel Sambuc if (debug)
95*11be35a1SLionel Sambuc fprintf(stderr, "result = %s\n", result);
96*11be35a1SLionel Sambuc while ((c = getopt(nargs, args, optstring)) != -1) {
97*11be35a1SLionel Sambuc if (c == ':')
98*11be35a1SLionel Sambuc err(1, "`:' found as argument char");
99*11be35a1SLionel Sambuc if ((ptr = strchr(optstring, c)) == NULL) {
100*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg), "!%c,", c);
101*11be35a1SLionel Sambuc strcat(buf, arg);
102*11be35a1SLionel Sambuc continue;
103*11be35a1SLionel Sambuc }
104*11be35a1SLionel Sambuc if (ptr[1] != ':')
105*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg), "%c,", c);
106*11be35a1SLionel Sambuc else
107*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg), "%c=%s,",
108*11be35a1SLionel Sambuc c, optarg);
109*11be35a1SLionel Sambuc strcat(buf, arg);
110*11be35a1SLionel Sambuc }
111*11be35a1SLionel Sambuc len = strlen(buf);
112*11be35a1SLionel Sambuc if (len > 0) {
113*11be35a1SLionel Sambuc buf[len - 1] = '|';
114*11be35a1SLionel Sambuc buf[len] = '\0';
115*11be35a1SLionel Sambuc } else {
116*11be35a1SLionel Sambuc buf[0] = '|';
117*11be35a1SLionel Sambuc buf[1] = '\0';
118*11be35a1SLionel Sambuc }
119*11be35a1SLionel Sambuc snprintf(arg, sizeof(arg), "%d", nargs - optind);
120*11be35a1SLionel Sambuc strcat(buf, arg);
121*11be35a1SLionel Sambuc if (strcmp(buf, result) != 0)
122*11be35a1SLionel Sambuc errx(1, "`%s' != `%s'", buf, result);
123*11be35a1SLionel Sambuc }
124*11be35a1SLionel Sambuc free(line);
125*11be35a1SLionel Sambuc }
126*11be35a1SLionel Sambuc return 0;
127*11be35a1SLionel Sambuc }
128