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