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