1272343Sngie/* $NetBSD: h_getopt.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2002 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Christos Zoulas. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie 32272343Sngie#include <stdio.h> 33272343Sngie#include <string.h> 34272343Sngie#include <stdlib.h> 35272343Sngie#include <unistd.h> 36272343Sngie#include <err.h> 37276478Sngie#ifdef __FreeBSD__ 38314817Sngie/* 39314817Sngie * Needed to avoid libutil.h pollution in stdio.h, which causes grief with 40314817Sngie * with hexdump(3) in lib/libc/db/h_hash.c 41314817Sngie */ 42276478Sngie#include <libutil.h> 43276478Sngie#endif 44272343Sngie 45272343Sngie#define WS "\t\n " 46272343Sngie#define debug 0 47272343Sngie 48272343Sngieint 49272343Sngiemain(int argc, char *argv[]) 50272343Sngie{ 51272343Sngie size_t len, lineno = 0; 52272343Sngie char *line, *ptr, *optstring = NULL, *result = NULL; 53272343Sngie char buf[1024]; 54272343Sngie char *args[100]; 55272343Sngie char arg[100]; 56272343Sngie int nargs = -1; 57272343Sngie int c; 58272343Sngie 59272343Sngie while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) { 60272343Sngie if (strncmp(line, "load:", 5) == 0) { 61272343Sngie if (optstring) 62272343Sngie free(optstring); 63272343Sngie optstring = strtok(&line[6], WS); 64272343Sngie if (optstring == NULL) 65272343Sngie errx(1, "missing optstring at line %ld", 66272343Sngie (unsigned long)lineno); 67272343Sngie optstring = strdup(optstring); 68272343Sngie if (debug) 69272343Sngie fprintf(stderr, "optstring = %s\n", optstring); 70272343Sngie } else if (strncmp(line, "args:", 5) == 0) { 71272343Sngie for (; nargs >= 0; nargs--) { 72272343Sngie if (args[nargs] != NULL) 73272343Sngie free(args[nargs]); 74272343Sngie } 75272343Sngie args[nargs = 0] = strtok(&line[6], WS); 76272343Sngie if (args[nargs] == NULL) 77272343Sngie errx(1, "missing args at line %ld", 78272343Sngie (unsigned long)lineno); 79272343Sngie 80272343Sngie args[nargs] = strdup(args[nargs]); 81272343Sngie while ((args[++nargs] = strtok(NULL, WS)) != NULL) 82272343Sngie args[nargs] = strdup(args[nargs]); 83272343Sngie if (debug) { 84272343Sngie int i = 0; 85272343Sngie for (i = 0; i < nargs; i++) 86272343Sngie fprintf(stderr, "argv[%d] = %s\n", i, 87272343Sngie args[i]); 88272343Sngie } 89272343Sngie } else if (strncmp(line, "result:", 7) == 0) { 90272343Sngie buf[0] = '\0'; 91272343Sngie optind = optreset = 1; 92272343Sngie if (result) 93272343Sngie free(result); 94272343Sngie result = strtok(&line[8], WS); 95272343Sngie if (result == NULL) 96272343Sngie errx(1, "missing result at line %ld", 97272343Sngie (unsigned long)lineno); 98272343Sngie result = strdup(result); 99272343Sngie if (nargs == -1) 100272343Sngie errx(1, "result: without args:"); 101272343Sngie if (debug) 102272343Sngie fprintf(stderr, "result = %s\n", result); 103272343Sngie while ((c = getopt(nargs, args, optstring)) != -1) { 104272343Sngie if (c == ':') 105272343Sngie err(1, "`:' found as argument char"); 106272343Sngie if ((ptr = strchr(optstring, c)) == NULL) { 107272343Sngie snprintf(arg, sizeof(arg), "!%c,", c); 108272343Sngie strcat(buf, arg); 109272343Sngie continue; 110272343Sngie } 111272343Sngie if (ptr[1] != ':') 112272343Sngie snprintf(arg, sizeof(arg), "%c,", c); 113272343Sngie else 114272343Sngie snprintf(arg, sizeof(arg), "%c=%s,", 115272343Sngie c, optarg); 116272343Sngie strcat(buf, arg); 117272343Sngie } 118272343Sngie len = strlen(buf); 119272343Sngie if (len > 0) { 120272343Sngie buf[len - 1] = '|'; 121272343Sngie buf[len] = '\0'; 122272343Sngie } else { 123272343Sngie buf[0] = '|'; 124272343Sngie buf[1] = '\0'; 125272343Sngie } 126272343Sngie snprintf(arg, sizeof(arg), "%d", nargs - optind); 127272343Sngie strcat(buf, arg); 128272343Sngie if (strcmp(buf, result) != 0) 129272343Sngie errx(1, "`%s' != `%s'", buf, result); 130272343Sngie } 131272343Sngie free(line); 132272343Sngie } 133272343Sngie return 0; 134272343Sngie} 135