source: umon/main/glib/getopt.c @ a7b6f00

Last change on this file since a7b6f00 was a7b6f00, checked in by Ed Sutter <edsutterjr@…>, on 08/04/15 at 01:35:50

tree cleanup using 'astyle --unpad-paren --align-pointer=name --lineend=linux --add-brackets --convert-tabs --style=knf -A4 FILENAME'

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*  $NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $ */
2#include <string.h>
3extern int printf(char *fmt,...);
4
5/*
6 * Copyright (c) 1987, 1993, 1994
7 *  The Regents of the University of California.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34int opterr = 1,     /* if error message should be printed */
35    optind = 1,     /* index into parent argv vector */
36    optopt,         /* character checked for validity */
37    optreset;       /* reset getopt */
38char    *optarg;        /* argument associated with option */
39
40#define BADCH   (int)'?'
41#define BADARG  (int)':'
42#define EMSG    ""
43
44/*
45 * getopt --
46 *  Parse argc/argv argument vector.
47 */
48int
49getopt(int nargc, char *const nargv[], const char *ostr)
50{
51    static char *place = EMSG;      /* option letter processing */
52    char *oli;              /* option letter list index */
53
54    if(optreset || *place == 0) {       /* update scanning pointer */
55        optreset = 0;
56        place = nargv[optind];
57        if(optind >= nargc || *place++ != '-') {
58            /* Argument is absent or is not an option */
59            place = EMSG;
60            return (-1);
61        }
62        optopt = *place++;
63        if(optopt == '-' && *place == 0) {
64            /* "--" => end of options */
65            ++optind;
66            place = EMSG;
67            return (-1);
68        }
69        if(optopt == 0) {
70            /* Solitary '-', treat as a '-' option
71               if the program (eg su) is looking for it. */
72            place = EMSG;
73            if(strchr(ostr, '-') == 0) {
74                return (-1);
75            }
76            optopt = '-';
77        }
78    } else {
79        optopt = *place++;
80    }
81
82    /* See if option letter is one the caller wanted... */
83    if(optopt == ':' || (oli = strchr(ostr, optopt)) == 0) {
84        if(*place == 0) {
85            ++optind;
86        }
87        if(opterr && *ostr != ':')
88            (void)printf(
89                "illegal option -- %c\n", optopt);
90        return (BADCH);
91    }
92
93    /* Does this option need an argument? */
94    if(oli[1] != ':') {
95        /* don't need argument */
96        optarg = 0;
97        if(*place == 0) {
98            ++optind;
99        }
100    } else {
101        /* Option-argument is either the rest of this argument or the
102           entire next argument. */
103        if(*place) {
104            optarg = place;
105        } else if(oli[2] == ':')
106            /*
107             * GNU Extension, for optional arguments if the rest of
108             * the argument is empty, we return NULL
109             */
110        {
111            optarg = 0;
112        } else if(nargc > ++optind) {
113            optarg = nargv[optind];
114        } else {
115            /* option-argument absent */
116            place = EMSG;
117            if(*ostr == ':') {
118                return (BADARG);
119            }
120            if(opterr)
121                (void)printf(
122                    "option requires an argument -- %c\n",
123                    optopt);
124            return (BADCH);
125        }
126        place = EMSG;
127        ++optind;
128    }
129    return (optopt);            /* return option letter */
130}
131
132/* getoptinit():
133 * Since getopt() can be used by every command in the monitor
134 * it's variables must be reinitialized prior to each command
135 * executed through docommand()...
136 */
137void
138getoptinit(void)
139{
140    optind = 1;
141    opterr = 1;
142    optopt = 0;
143    optreset = 0;
144    optarg = (char *)0;
145}
Note: See TracBrowser for help on using the repository browser.