Add a small regression test for getopt.
This commit is contained in:
parent
7dc4401b11
commit
85c9292513
|
@ -1,8 +1,8 @@
|
|||
# $NetBSD: Makefile,v 1.29 2002/07/05 15:42:41 itojun Exp $
|
||||
# $NetBSD: Makefile,v 1.30 2002/07/15 22:04:00 christos Exp $
|
||||
|
||||
SUBDIR+= _setjmp clone db div divrem \
|
||||
gen getaddrinfo hsearch int_fmtio md5sha popen regex rpc \
|
||||
setjmp sigsetjmp string sys time
|
||||
setjmp sigsetjmp stdlib string sys time
|
||||
.if (${MACHINE_ARCH} != "vax" && ${MACHINE_ARCH} != "m68000")
|
||||
SUBDIR+= ieeefp
|
||||
.endif
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# $NetBSD: Makefile,v 1.1 2002/07/15 22:04:00 christos Exp $
|
||||
|
||||
SUBDIR+= getopt
|
||||
|
||||
.include <bsd.subdir.mk>
|
|
@ -0,0 +1,12 @@
|
|||
# $NetBSD: Makefile,v 1.1 2002/07/15 22:04:00 christos Exp $
|
||||
|
||||
PROG= getopt
|
||||
NOMAN= # defined
|
||||
|
||||
regress: ${PROG}
|
||||
./${PROG} < ${.CURDIR}/opttest
|
||||
|
||||
LDADD+= -lutil
|
||||
DPADD+= ${LIBUTIL}
|
||||
|
||||
.include <bsd.prog.mk>
|
|
@ -0,0 +1,133 @@
|
|||
/* $NetBSD: getopt.c,v 1.1 2002/07/15 22:04:00 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Christos Zoulas.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the NetBSD
|
||||
* Foundation, Inc. and its contributors.
|
||||
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <util.h>
|
||||
#include <err.h>
|
||||
|
||||
#define WS "\t\n "
|
||||
#define debug 0
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
size_t len, lineno;
|
||||
char *line, *ptr, *optstring = NULL, *result = NULL;
|
||||
char buf[1024];
|
||||
char *args[100];
|
||||
char arg[100];
|
||||
char nargs = -1;
|
||||
int c;
|
||||
|
||||
while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
|
||||
if (strncmp(line, "load:", 5) == 0) {
|
||||
if (optstring)
|
||||
free(optstring);
|
||||
optstring = strtok(&line[6], WS);
|
||||
if (optstring == NULL)
|
||||
err(1, "missing optstring at line %ld",
|
||||
(unsigned long) line);
|
||||
optstring = strdup(optstring);
|
||||
if (debug)
|
||||
fprintf(stderr, "optstring = %s\n", optstring);
|
||||
} else if (strncmp(line, "args:", 5) == 0) {
|
||||
for (; nargs >= 0; nargs--) {
|
||||
if (args[nargs] != NULL)
|
||||
free(args[nargs]);
|
||||
}
|
||||
args[nargs = 0] = strtok(&line[6], WS);
|
||||
if (args[nargs] == NULL)
|
||||
err(1, "Missing args");
|
||||
|
||||
args[nargs] = strdup(args[nargs]);
|
||||
while ((args[++nargs] = strtok(NULL, WS)) != NULL)
|
||||
args[nargs] = strdup(args[nargs]);
|
||||
if (debug) {
|
||||
int i = 0;
|
||||
for (i = 0; i < nargs; i++)
|
||||
fprintf(stderr, "argv[%d] = %s\n", i,
|
||||
args[i]);
|
||||
}
|
||||
} else if (strncmp(line, "result:", 7) == NULL) {
|
||||
buf[0] = '\0';
|
||||
optind = optreset = 1;
|
||||
if (result)
|
||||
free(result);
|
||||
result = strtok(&line[8], WS);
|
||||
if (result == NULL)
|
||||
err(1, "result: without load:");
|
||||
result = strdup(result);
|
||||
if (nargs == -1)
|
||||
err(1, "result: without args:");
|
||||
if (debug)
|
||||
fprintf(stderr, "result = %s\n", result);
|
||||
while ((c = getopt(nargs, args, optstring)) != -1) {
|
||||
if (c == ':')
|
||||
err(1, "`:' found as argument char");
|
||||
if ((ptr = strchr(optstring, c)) == NULL) {
|
||||
snprintf(arg, sizeof(arg), "!%c,", c);
|
||||
strcat(buf, arg);
|
||||
continue;
|
||||
}
|
||||
if (ptr[1] != ':')
|
||||
snprintf(arg, sizeof(arg), "%c,", c);
|
||||
else
|
||||
snprintf(arg, sizeof(arg), "%c=%s,",
|
||||
c, optarg);
|
||||
strcat(buf, arg);
|
||||
}
|
||||
len = strlen(buf);
|
||||
if (len > 0) {
|
||||
buf[len - 1] = '|';
|
||||
buf[len] = '\0';
|
||||
} else {
|
||||
buf[0] = '|';
|
||||
buf[1] = '\0';
|
||||
}
|
||||
snprintf(arg, sizeof(arg), "%d", nargs - optind);
|
||||
strcat(buf, arg);
|
||||
if (strcmp(buf, result) != 0)
|
||||
err(1, "`%s' != `%s'", buf, result);
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
load: c:d
|
||||
#
|
||||
args: foo -c 1 -d foo
|
||||
result: c=1,d|1
|
||||
#
|
||||
args: foo -d foo bar
|
||||
result: d|2
|
||||
#
|
||||
args: foo -c 2 foo bar
|
||||
result: c=2|2
|
||||
#
|
||||
args: foo -e 1 foo bar
|
||||
result: !?|3
|
||||
#
|
||||
args: foo -d -- -c 1
|
||||
result: d|2
|
||||
#
|
||||
args: foo -c- 1
|
||||
result: c=-|1
|
||||
#
|
||||
args: foo -d - 1
|
||||
result: d|2
|
Loading…
Reference in New Issue