1f1b9d127SSheldon Hearn /*
2f1b9d127SSheldon Hearn * Copyright (c) 2000, Boris Popov
3f1b9d127SSheldon Hearn * All rights reserved.
4f1b9d127SSheldon Hearn *
5f1b9d127SSheldon Hearn * Redistribution and use in source and binary forms, with or without
6f1b9d127SSheldon Hearn * modification, are permitted provided that the following conditions
7f1b9d127SSheldon Hearn * are met:
8f1b9d127SSheldon Hearn * 1. Redistributions of source code must retain the above copyright
9f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer.
10f1b9d127SSheldon Hearn * 2. Redistributions in binary form must reproduce the above copyright
11f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer in the
12f1b9d127SSheldon Hearn * documentation and/or other materials provided with the distribution.
13f1b9d127SSheldon Hearn * 3. All advertising materials mentioning features or use of this software
14f1b9d127SSheldon Hearn * must display the following acknowledgement:
15f1b9d127SSheldon Hearn * This product includes software developed by Boris Popov.
16f1b9d127SSheldon Hearn * 4. Neither the name of the author nor the names of any co-contributors
17f1b9d127SSheldon Hearn * may be used to endorse or promote products derived from this software
18f1b9d127SSheldon Hearn * without specific prior written permission.
19f1b9d127SSheldon Hearn *
20f1b9d127SSheldon Hearn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21f1b9d127SSheldon Hearn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22f1b9d127SSheldon Hearn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f1b9d127SSheldon Hearn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24f1b9d127SSheldon Hearn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25f1b9d127SSheldon Hearn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26f1b9d127SSheldon Hearn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27f1b9d127SSheldon Hearn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28f1b9d127SSheldon Hearn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29f1b9d127SSheldon Hearn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30f1b9d127SSheldon Hearn * SUCH DAMAGE.
31f1b9d127SSheldon Hearn *
32f1b9d127SSheldon Hearn * $Id: cfopt.c,v 1.3 2000/07/11 01:51:49 bp Exp $
33f1b9d127SSheldon Hearn */
34f1b9d127SSheldon Hearn #include <sys/param.h>
35f1b9d127SSheldon Hearn
36f1b9d127SSheldon Hearn #include <stdio.h>
37f1b9d127SSheldon Hearn #include <string.h>
38f1b9d127SSheldon Hearn
39f1b9d127SSheldon Hearn #include <cflib.h>
40f1b9d127SSheldon Hearn
41f1b9d127SSheldon Hearn extern char *__progname;
42f1b9d127SSheldon Hearn
43f1b9d127SSheldon Hearn int cf_opterr = 1, /* if error message should be printed */
44f1b9d127SSheldon Hearn cf_optind = 1, /* index into parent argv vector */
45f1b9d127SSheldon Hearn cf_optopt, /* character checked for validity */
46f1b9d127SSheldon Hearn cf_optreset; /* reset getopt */
47f1b9d127SSheldon Hearn const char* cf_optarg; /* argument associated with option */
48f1b9d127SSheldon Hearn
49f1b9d127SSheldon Hearn #define BADCH (int)'?'
50f1b9d127SSheldon Hearn #define BADARG (int)':'
51f1b9d127SSheldon Hearn #define EMSG ""
52f1b9d127SSheldon Hearn
53f1b9d127SSheldon Hearn int
cf_getopt(int nargc,char * const * nargv,const char * ostr)54*f6fd5356SJohn Baldwin cf_getopt(int nargc, char * const *nargv, const char *ostr)
55f1b9d127SSheldon Hearn {
56f1b9d127SSheldon Hearn static const char *place = EMSG; /* option letter processing */
57f1b9d127SSheldon Hearn char *oli; /* option letter list index */
58f1b9d127SSheldon Hearn int tmpind;
59f1b9d127SSheldon Hearn
60f1b9d127SSheldon Hearn if (cf_optreset || !*place) { /* update scanning pointer */
61f1b9d127SSheldon Hearn cf_optreset = 0;
62f1b9d127SSheldon Hearn tmpind = cf_optind;
63f1b9d127SSheldon Hearn while (1) {
64f1b9d127SSheldon Hearn if (tmpind >= nargc) {
65f1b9d127SSheldon Hearn place = EMSG;
66f1b9d127SSheldon Hearn return (-1);
67f1b9d127SSheldon Hearn }
68f1b9d127SSheldon Hearn if (*(place = nargv[tmpind]) != '-') {
69f1b9d127SSheldon Hearn tmpind++;
70f1b9d127SSheldon Hearn continue; /* lookup next option */
71f1b9d127SSheldon Hearn }
72f1b9d127SSheldon Hearn if (place[1] && *++place == '-') { /* found "--" */
73f1b9d127SSheldon Hearn cf_optind = ++tmpind;
74f1b9d127SSheldon Hearn place = EMSG;
75f1b9d127SSheldon Hearn return (-1);
76f1b9d127SSheldon Hearn }
77f1b9d127SSheldon Hearn cf_optind = tmpind;
78f1b9d127SSheldon Hearn break;
79f1b9d127SSheldon Hearn }
80f1b9d127SSheldon Hearn } /* option letter okay? */
81f1b9d127SSheldon Hearn if ((cf_optopt = (int)*place++) == (int)':' ||
82f1b9d127SSheldon Hearn !(oli = strchr(ostr, cf_optopt))) {
83f1b9d127SSheldon Hearn /*
84f1b9d127SSheldon Hearn * if the user didn't specify '-' as an option,
85f1b9d127SSheldon Hearn * assume it means -1.
86f1b9d127SSheldon Hearn */
87f1b9d127SSheldon Hearn if (cf_optopt == (int)'-')
88f1b9d127SSheldon Hearn return (-1);
89f1b9d127SSheldon Hearn if (!*place)
90f1b9d127SSheldon Hearn ++cf_optind;
91f1b9d127SSheldon Hearn if (cf_opterr && *ostr != ':')
92f1b9d127SSheldon Hearn (void)fprintf(stderr,
93f1b9d127SSheldon Hearn "%s: illegal option -- %c\n", __progname, cf_optopt);
94f1b9d127SSheldon Hearn return (BADCH);
95f1b9d127SSheldon Hearn }
96f1b9d127SSheldon Hearn if (*++oli != ':') { /* don't need argument */
97f1b9d127SSheldon Hearn cf_optarg = NULL;
98f1b9d127SSheldon Hearn if (!*place)
99f1b9d127SSheldon Hearn ++cf_optind;
100f1b9d127SSheldon Hearn }
101f1b9d127SSheldon Hearn else { /* need an argument */
102f1b9d127SSheldon Hearn if (*place) /* no white space */
103f1b9d127SSheldon Hearn cf_optarg = place;
104f1b9d127SSheldon Hearn else if (nargc <= ++cf_optind) { /* no arg */
105f1b9d127SSheldon Hearn place = EMSG;
106f1b9d127SSheldon Hearn if (*ostr == ':')
107f1b9d127SSheldon Hearn return (BADARG);
108f1b9d127SSheldon Hearn if (cf_opterr)
109f1b9d127SSheldon Hearn (void)fprintf(stderr,
110f1b9d127SSheldon Hearn "%s: option requires an argument -- %c\n",
111f1b9d127SSheldon Hearn __progname, cf_optopt);
112f1b9d127SSheldon Hearn return (BADCH);
113f1b9d127SSheldon Hearn }
114f1b9d127SSheldon Hearn else /* white space */
115f1b9d127SSheldon Hearn cf_optarg = nargv[cf_optind];
116f1b9d127SSheldon Hearn place = EMSG;
117f1b9d127SSheldon Hearn ++cf_optind;
118f1b9d127SSheldon Hearn }
119f1b9d127SSheldon Hearn return (cf_optopt); /* dump back option letter */
120f1b9d127SSheldon Hearn }
121