Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "perfetto/ext/base/getopt_compat.h" |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "perfetto/base/logging.h" |
| 26 | |
| 27 | namespace perfetto { |
| 28 | namespace base { |
| 29 | namespace getopt_compat { |
| 30 | |
| 31 | char* optarg = nullptr; |
| 32 | int optind = 0; |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 33 | int optopt = 0; |
| 34 | int opterr = 1; |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
| 38 | char* nextchar = nullptr; |
| 39 | |
| 40 | const option* LookupLongOpt(const std::vector<option>& opts, |
| 41 | const char* name, |
| 42 | size_t len) { |
| 43 | for (const option& opt : opts) { |
| 44 | if (strncmp(opt.name, name, len) == 0 && strlen(opt.name) == len) |
| 45 | return &opt; |
| 46 | } |
| 47 | return nullptr; |
| 48 | } |
| 49 | |
| 50 | const option* LookupShortOpt(const std::vector<option>& opts, char c) { |
| 51 | for (const option& opt : opts) { |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 52 | if (!*opt.name && opt.val == c) |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 53 | return &opt; |
| 54 | } |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | bool ParseOpts(const char* shortopts, |
| 59 | const option* longopts, |
| 60 | std::vector<option>* res) { |
| 61 | // Parse long options first. |
| 62 | for (const option* lopt = longopts; lopt && lopt->name; lopt++) { |
| 63 | PERFETTO_CHECK(lopt->flag == nullptr); |
| 64 | PERFETTO_CHECK(lopt->has_arg == no_argument || |
| 65 | lopt->has_arg == required_argument); |
| 66 | res->emplace_back(*lopt); |
| 67 | } |
| 68 | |
| 69 | // Merge short options. |
| 70 | for (const char* sopt = shortopts; sopt && *sopt;) { |
| 71 | const size_t idx = static_cast<size_t>(sopt - shortopts); |
| 72 | char c = *sopt++; |
| 73 | bool valid = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || |
| 74 | (c >= '0' && c <= '9'); |
| 75 | if (!valid) { |
| 76 | fprintf(stderr, |
| 77 | "Error parsing shortopts. Unexpected char '%c' at offset %zu\n", |
| 78 | c, idx); |
| 79 | return false; |
| 80 | } |
| 81 | res->emplace_back(); |
| 82 | option& opt = res->back(); |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 83 | opt.name = ""; |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 84 | opt.val = c; |
| 85 | opt.has_arg = no_argument; |
| 86 | if (*sopt == ':') { |
| 87 | opt.has_arg = required_argument; |
| 88 | ++sopt; |
| 89 | } |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | } // namespace |
| 95 | |
| 96 | int getopt_long(int argc, |
| 97 | char** argv, |
| 98 | const char* shortopts, |
| 99 | const option* longopts, |
| 100 | std::nullptr_t /*longind*/) { |
| 101 | std::vector<option> opts; |
| 102 | optarg = nullptr; |
| 103 | |
| 104 | if (optind == 0) |
| 105 | optind = 1; |
| 106 | |
| 107 | if (optind >= argc) |
| 108 | return -1; |
| 109 | |
| 110 | if (!ParseOpts(shortopts, longopts, &opts)) |
| 111 | return '?'; |
| 112 | |
| 113 | char* arg = argv[optind]; |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 114 | optopt = 0; |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 115 | |
| 116 | if (!nextchar) { |
| 117 | // If |nextchar| is null we are NOT in the middle of a short option and we |
| 118 | // should parse the next argv. |
| 119 | if (strncmp(arg, "--", 2) == 0 && strlen(arg) > 2) { |
| 120 | // A --long option. |
| 121 | arg += 2; |
| 122 | char* sep = strchr(arg, '='); |
| 123 | optind++; |
| 124 | |
| 125 | size_t len = sep ? static_cast<size_t>(sep - arg) : strlen(arg); |
| 126 | const option* opt = LookupLongOpt(opts, arg, len); |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 127 | |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 128 | if (!opt) { |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 129 | if (opterr) |
| 130 | fprintf(stderr, "unrecognized option '--%s'\n", arg); |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 131 | return '?'; |
| 132 | } |
| 133 | |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 134 | optopt = opt->val; |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 135 | if (opt->has_arg == no_argument) { |
| 136 | if (sep) { |
| 137 | fprintf(stderr, "option '--%s' doesn't allow an argument\n", arg); |
| 138 | return '?'; |
| 139 | } else { |
| 140 | return opt->val; |
| 141 | } |
| 142 | } else if (opt->has_arg == required_argument) { |
| 143 | if (sep) { |
| 144 | optarg = sep + 1; |
| 145 | return opt->val; |
| 146 | } else if (optind >= argc) { |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 147 | if (opterr) |
| 148 | fprintf(stderr, "option '--%s' requires an argument\n", arg); |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 149 | return '?'; |
| 150 | } else { |
| 151 | optarg = argv[optind++]; |
| 152 | return opt->val; |
| 153 | } |
| 154 | } |
| 155 | // has_arg must be either |no_argument| or |required_argument|. We |
| 156 | // shoulnd't get here unless the check in ParseOpts() has a bug. |
| 157 | PERFETTO_CHECK(false); |
| 158 | } // if (arg ~= "--*"). |
| 159 | |
| 160 | if (strlen(arg) > 1 && arg[0] == '-' && arg[1] != '-') { |
| 161 | // A sequence of short options. Parsing logic continues below. |
| 162 | nextchar = &arg[1]; |
| 163 | } |
| 164 | } // if(!nextchar) |
| 165 | |
| 166 | if (nextchar) { |
| 167 | // At this point either: |
| 168 | // 1. This is the first char of a sequence of short options, and we fell |
| 169 | // through here from the lines above. |
| 170 | // 2. This is the N (>1) char of a sequence of short options, and we got |
| 171 | // here from a new getopt() call to getopt(). |
| 172 | const char cur_char = *nextchar; |
| 173 | PERFETTO_CHECK(cur_char != '\0'); |
| 174 | |
| 175 | // Advance the option char in any case, before we start reasoning on them. |
| 176 | // if we got to the end of the "-abc" sequence, increment optind so the next |
| 177 | // getopt() call resumes from the next argv argument. |
| 178 | if (*(++nextchar) == '\0') { |
| 179 | nextchar = nullptr; |
| 180 | ++optind; |
| 181 | } |
| 182 | |
| 183 | const option* opt = LookupShortOpt(opts, cur_char); |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 184 | optopt = cur_char; |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 185 | if (!opt) { |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 186 | if (opterr) |
| 187 | fprintf(stderr, "invalid option -- '%c'\n", cur_char); |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 188 | return '?'; |
| 189 | } |
| 190 | if (opt->has_arg == no_argument) { |
| 191 | return cur_char; |
| 192 | } else if (opt->has_arg == required_argument) { |
| 193 | // This is a subtle getopt behavior. Say you call `tar -fx`, there are |
| 194 | // two cases: |
| 195 | // 1. If 'f' is no_argument then 'x' (and anything else after) is |
| 196 | // interpreted as an independent argument (like `tar -f -x`). |
| 197 | // 2. If 'f' is required_argument, than everything else after the 'f' |
| 198 | // is interpreted as the option argument (like `tar -f x`) |
| 199 | if (!nextchar) { |
| 200 | // Case 1. |
| 201 | if (optind >= argc) { |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 202 | if (opterr) |
| 203 | fprintf(stderr, "option requires an argument -- '%c'\n", cur_char); |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 204 | return '?'; |
| 205 | } else { |
| 206 | optarg = argv[optind++]; |
| 207 | return cur_char; |
| 208 | } |
| 209 | } else { |
| 210 | // Case 2. |
| 211 | optarg = nextchar; |
| 212 | nextchar = nullptr; |
| 213 | optind++; |
| 214 | return cur_char; |
| 215 | } |
| 216 | } |
| 217 | PERFETTO_CHECK(false); |
| 218 | } // if (nextchar) |
| 219 | |
| 220 | // If we get here, we found the first non-option argument. Stop here. |
| 221 | |
| 222 | if (strcmp(arg, "--") == 0) |
| 223 | optind++; |
| 224 | |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | int getopt(int argc, char** argv, const char* shortopts) { |
| 229 | return getopt_long(argc, argv, shortopts, nullptr, nullptr); |
| 230 | } |
| 231 | |
| 232 | } // namespace getopt_compat |
| 233 | } // namespace base |
| 234 | } // namespace perfetto |