1*57718be8SEnji Cooper /* $NetBSD: h_getopt.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $ */ 2*57718be8SEnji Cooper 3*57718be8SEnji Cooper /*- 4*57718be8SEnji Cooper * Copyright (c) 2002 The NetBSD Foundation, Inc. 5*57718be8SEnji Cooper * All rights reserved. 6*57718be8SEnji Cooper * 7*57718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation 8*57718be8SEnji Cooper * by Christos Zoulas. 9*57718be8SEnji Cooper * 10*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without 11*57718be8SEnji Cooper * modification, are permitted provided that the following conditions 12*57718be8SEnji Cooper * are met: 13*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright 14*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer. 15*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 16*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the 17*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution. 18*57718be8SEnji Cooper * 19*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20*57718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21*57718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22*57718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23*57718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*57718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*57718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*57718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*57718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*57718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE. 30*57718be8SEnji Cooper */ 31*57718be8SEnji Cooper 32*57718be8SEnji Cooper #include <stdio.h> 33*57718be8SEnji Cooper #include <string.h> 34*57718be8SEnji Cooper #include <stdlib.h> 35*57718be8SEnji Cooper #include <unistd.h> 36*57718be8SEnji Cooper #include <err.h> 37*57718be8SEnji Cooper 38*57718be8SEnji Cooper #define WS "\t\n " 39*57718be8SEnji Cooper #define debug 0 40*57718be8SEnji Cooper 41*57718be8SEnji Cooper int 42*57718be8SEnji Cooper main(int argc, char *argv[]) 43*57718be8SEnji Cooper { 44*57718be8SEnji Cooper size_t len, lineno = 0; 45*57718be8SEnji Cooper char *line, *ptr, *optstring = NULL, *result = NULL; 46*57718be8SEnji Cooper char buf[1024]; 47*57718be8SEnji Cooper char *args[100]; 48*57718be8SEnji Cooper char arg[100]; 49*57718be8SEnji Cooper int nargs = -1; 50*57718be8SEnji Cooper int c; 51*57718be8SEnji Cooper 52*57718be8SEnji Cooper while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) { 53*57718be8SEnji Cooper if (strncmp(line, "load:", 5) == 0) { 54*57718be8SEnji Cooper if (optstring) 55*57718be8SEnji Cooper free(optstring); 56*57718be8SEnji Cooper optstring = strtok(&line[6], WS); 57*57718be8SEnji Cooper if (optstring == NULL) 58*57718be8SEnji Cooper errx(1, "missing optstring at line %ld", 59*57718be8SEnji Cooper (unsigned long)lineno); 60*57718be8SEnji Cooper optstring = strdup(optstring); 61*57718be8SEnji Cooper if (debug) 62*57718be8SEnji Cooper fprintf(stderr, "optstring = %s\n", optstring); 63*57718be8SEnji Cooper } else if (strncmp(line, "args:", 5) == 0) { 64*57718be8SEnji Cooper for (; nargs >= 0; nargs--) { 65*57718be8SEnji Cooper if (args[nargs] != NULL) 66*57718be8SEnji Cooper free(args[nargs]); 67*57718be8SEnji Cooper } 68*57718be8SEnji Cooper args[nargs = 0] = strtok(&line[6], WS); 69*57718be8SEnji Cooper if (args[nargs] == NULL) 70*57718be8SEnji Cooper errx(1, "missing args at line %ld", 71*57718be8SEnji Cooper (unsigned long)lineno); 72*57718be8SEnji Cooper 73*57718be8SEnji Cooper args[nargs] = strdup(args[nargs]); 74*57718be8SEnji Cooper while ((args[++nargs] = strtok(NULL, WS)) != NULL) 75*57718be8SEnji Cooper args[nargs] = strdup(args[nargs]); 76*57718be8SEnji Cooper if (debug) { 77*57718be8SEnji Cooper int i = 0; 78*57718be8SEnji Cooper for (i = 0; i < nargs; i++) 79*57718be8SEnji Cooper fprintf(stderr, "argv[%d] = %s\n", i, 80*57718be8SEnji Cooper args[i]); 81*57718be8SEnji Cooper } 82*57718be8SEnji Cooper } else if (strncmp(line, "result:", 7) == 0) { 83*57718be8SEnji Cooper buf[0] = '\0'; 84*57718be8SEnji Cooper optind = optreset = 1; 85*57718be8SEnji Cooper if (result) 86*57718be8SEnji Cooper free(result); 87*57718be8SEnji Cooper result = strtok(&line[8], WS); 88*57718be8SEnji Cooper if (result == NULL) 89*57718be8SEnji Cooper errx(1, "missing result at line %ld", 90*57718be8SEnji Cooper (unsigned long)lineno); 91*57718be8SEnji Cooper result = strdup(result); 92*57718be8SEnji Cooper if (nargs == -1) 93*57718be8SEnji Cooper errx(1, "result: without args:"); 94*57718be8SEnji Cooper if (debug) 95*57718be8SEnji Cooper fprintf(stderr, "result = %s\n", result); 96*57718be8SEnji Cooper while ((c = getopt(nargs, args, optstring)) != -1) { 97*57718be8SEnji Cooper if (c == ':') 98*57718be8SEnji Cooper err(1, "`:' found as argument char"); 99*57718be8SEnji Cooper if ((ptr = strchr(optstring, c)) == NULL) { 100*57718be8SEnji Cooper snprintf(arg, sizeof(arg), "!%c,", c); 101*57718be8SEnji Cooper strcat(buf, arg); 102*57718be8SEnji Cooper continue; 103*57718be8SEnji Cooper } 104*57718be8SEnji Cooper if (ptr[1] != ':') 105*57718be8SEnji Cooper snprintf(arg, sizeof(arg), "%c,", c); 106*57718be8SEnji Cooper else 107*57718be8SEnji Cooper snprintf(arg, sizeof(arg), "%c=%s,", 108*57718be8SEnji Cooper c, optarg); 109*57718be8SEnji Cooper strcat(buf, arg); 110*57718be8SEnji Cooper } 111*57718be8SEnji Cooper len = strlen(buf); 112*57718be8SEnji Cooper if (len > 0) { 113*57718be8SEnji Cooper buf[len - 1] = '|'; 114*57718be8SEnji Cooper buf[len] = '\0'; 115*57718be8SEnji Cooper } else { 116*57718be8SEnji Cooper buf[0] = '|'; 117*57718be8SEnji Cooper buf[1] = '\0'; 118*57718be8SEnji Cooper } 119*57718be8SEnji Cooper snprintf(arg, sizeof(arg), "%d", nargs - optind); 120*57718be8SEnji Cooper strcat(buf, arg); 121*57718be8SEnji Cooper if (strcmp(buf, result) != 0) 122*57718be8SEnji Cooper errx(1, "`%s' != `%s'", buf, result); 123*57718be8SEnji Cooper } 124*57718be8SEnji Cooper free(line); 125*57718be8SEnji Cooper } 126*57718be8SEnji Cooper return 0; 127*57718be8SEnji Cooper } 128