| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2015 Red Hat |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include <popt.h> |
| 25 | |
| 26 | #include "common/common.h"cxx . . $ `<file>/src/common/common.h`/ |
| 27 | #include "cli/cli_tool.h"cxx . . $ `<file>/src/cli/cli_tool.h`/ |
| 28 | #include "authselect.h"cxx . . $ `<file>/include/authselect.h`/ |
| 29 | |
| 30 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! enable_tracecxx . . $ enable_trace.; |
| 31 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! enable_warningcxx . . $ enable_warning.; |
| 32 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! enable_debugcxx . . $ enable_debug.; |
| 33 | |
| 34 | void |
| 35 | print_debugcxx . . $ print_debug(f2816ff7bbfdf8a4).(void *pvt, |
| 36 | enum authselect_debugcxx . . $ authselect_debug# level, |
| 37 | const char *file, |
| 38 | unsigned long line, |
| 39 | const char *function, |
| 40 | const char *msg) |
| 41 | { |
| 42 | const char *category = "unknown"; |
| 43 | |
| 44 | switch (level) { |
| 45 | case AUTHSELECT_INFOcxx . . $ AUTHSELECT_INFO.: |
| 46 | if (!enable_tracecxx . . $ enable_trace.) { |
| 47 | return; |
| 48 | } |
| 49 | category = "info"; |
| 50 | break; |
| 51 | case AUTHSELECT_WARNINGcxx . . $ AUTHSELECT_WARNING.: |
| 52 | if (!enable_warningcxx . . $ enable_warning.) { |
| 53 | return; |
| 54 | } |
| 55 | category = "warn"; |
| 56 | break; |
| 57 | case AUTHSELECT_ERRORcxx . . $ AUTHSELECT_ERROR.: |
| 58 | category = "error"; |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | if (!enable_debugcxx . . $ enable_debug.) { |
| 63 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "[%s] %s\n", category, msg); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "[%s] [%s] %s\n", category, function, msg); |
| 68 | } |
| 69 | |
| 70 | static void cli_tool_print_common_optscxx . . $ cli_tool_print_common_opts(d4f767463ce0a6b3).(int min_len) |
| 71 | { |
| 72 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("Common options:\n")); |
| 73 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, " %-*s\t %s\n", min_len, "--debug", |
| 74 | _cxx . . $ `src/common/common.h:33:9`!("Print error messages")); |
| 75 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, " %-*s\t %s\n", min_len, "--trace", |
| 76 | _cxx . . $ `src/common/common.h:33:9`!("Print trace messages")); |
| 77 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, " %-*s\t %s\n", min_len, "--warn", |
| 78 | _cxx . . $ `src/common/common.h:33:9`!("Print warning messages")); |
| 79 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\n"); |
| 80 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("Help options:\n")); |
| 81 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, " %-*s\t %s\n", min_len, "-?, --help", |
| 82 | _cxx . . $ `src/common/common.h:33:9`!("Show this for a command")); |
| 83 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, " %-*s\t %s\n", min_len, "--usage", |
| 84 | _cxx . . $ `src/common/common.h:33:9`!("Show brief usage message for a command")); |
| 85 | } |
| 86 | |
| 87 | static struct poptOptioncxx . . $ poptOption# *cli_tool_common_opts_tablecxx . . $ cli_tool_common_opts_table(6c5ec6439ca87f0d).(void) |
| 88 | { |
| 89 | static struct poptOptioncxx . . $ poptOption# options[] = { |
| 90 | {"debug", '\0', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`! | POPT_ARGFLAG_STRIPcxx . . $ `<external>/16232926639102580118/popt.h:49:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 'd', NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 91 | {"trace", '\0', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`! | POPT_ARGFLAG_STRIPcxx . . $ `<external>/16232926639102580118/popt.h:49:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 't', NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 92 | {"warn", '\0', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`! | POPT_ARGFLAG_STRIPcxx . . $ `<external>/16232926639102580118/popt.h:49:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 'w', NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 93 | POPT_TABLEENDcxx . . $ `<external>/16232926639102580118/popt.h:161:9`! |
| 94 | }; |
| 95 | |
| 96 | options[0].descripcxx . . $ poptOption#descrip. = _cxx . . $ `src/common/common.h:33:9`!("Print error messages"); |
| 97 | options[1].descripcxx . . $ poptOption#descrip. = _cxx . . $ `src/common/common.h:33:9`!("Print trace messages"); |
| 98 | options[2].descripcxx . . $ poptOption#descrip. = _cxx . . $ `src/common/common.h:33:9`!("Print warning messages"); |
| 99 | |
| 100 | return options; |
| 101 | } |
| 102 | |
| 103 | static void cli_tool_common_optscxx . . $ cli_tool_common_opts(a4c963044a66329d).(int *argc, const char **argv) |
| 104 | { |
| 105 | poptContextcxx . . $ poptContext# pc; |
| 106 | struct poptOptioncxx . . $ poptOption# *options; |
| 107 | int orig_argc = *argc; |
| 108 | int opt; |
| 109 | |
| 110 | options = cli_tool_common_opts_tablecxx . . $ cli_tool_common_opts_table(6c5ec6439ca87f0d).(); |
| 111 | |
| 112 | pc = poptGetContextcxx . . $ poptGetContext(69e83c0752e8078).(argv[0], orig_argc, argv, options, 0); |
| 113 | while ((opt = poptGetNextOptcxx . . $ poptGetNextOpt(8c14591e39307bbe).(pc)) != -1) { |
| 114 | switch (opt) { |
| 115 | case 'd': |
| 116 | enable_debugcxx . . $ enable_debug. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 117 | break; |
| 118 | case 't': |
| 119 | enable_tracecxx . . $ enable_trace. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 120 | break; |
| 121 | case 'w': |
| 122 | enable_warningcxx . . $ enable_warning. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 123 | break; |
| 124 | default: |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | set_debug_fncxx . . $ set_debug_fn(10083e02f417e2d6).(print_debugcxx . . $ print_debug(f2816ff7bbfdf8a4)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 130 | authselect_set_debug_fncxx . . $ authselect_set_debug_fn(10083e02f417e2d6).(print_debugcxx . . $ print_debug(f2816ff7bbfdf8a4)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 131 | |
| 132 | /* Strip common options from arguments. We will discard_const here, |
| 133 | * since it is not worth the trouble to convert it back and forth. */ |
| 134 | *argc = poptStrippedArgvcxx . . $ poptStrippedArgv(6f3a22431fdddb05).(pc, orig_argc, (char **)argv); |
| 135 | |
| 136 | poptFreeContextcxx . . $ poptFreeContext(c32b9b4db48a8347).(pc); |
| 137 | } |
| 138 | |
| 139 | static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! cli_tool_is_delimitercxx . . $ cli_tool_is_delimiter(cb17940344e8e149).(struct cli_route_cmdcxx . . $ cli_route_cmd# *command) |
| 140 | { |
| 141 | if (command->commandcxx . . $ cli_route_cmd#command. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! && command->commandcxx . . $ cli_route_cmd#command.[0] == '\0') { |
| 142 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 143 | } |
| 144 | |
| 145 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 146 | } |
| 147 | |
| 148 | static size_t cli_tool_max_lengthcxx . . $ cli_tool_max_length(7e9b3eeac59cf7e1).(struct cli_route_cmdcxx . . $ cli_route_cmd# *commands) |
| 149 | { |
| 150 | size_t max = 0; |
| 151 | size_t len; |
| 152 | int i; |
| 153 | |
| 154 | for (i = 0; commands[i].commandcxx . . $ cli_route_cmd#command. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) { |
| 155 | if (cli_tool_is_delimitercxx . . $ cli_tool_is_delimiter(cb17940344e8e149).(&commands[i])) { |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | len = strlencxx . . $ strlen(751346f8406fd082).(commands[i].commandcxx . . $ cli_route_cmd#command.); |
| 160 | if (max < len) { |
| 161 | max = len; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return max; |
| 166 | } |
| 167 | |
| 168 | void cli_tool_usagecxx . . $ cli_tool_usage(9ffe2780c1253a7a).(const char *tool_name, struct cli_route_cmdcxx . . $ cli_route_cmd# *commands) |
| 169 | { |
| 170 | int min_len; |
| 171 | int i; |
| 172 | |
| 173 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("Usage:\n%s COMMAND COMMAND-ARGS\n\n"), tool_name); |
| 174 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("Available commands:\n")); |
| 175 | |
| 176 | min_len = cli_tool_max_lengthcxx . . $ cli_tool_max_length(7e9b3eeac59cf7e1).(commands); |
| 177 | |
| 178 | for (i = 0; commands[i].commandcxx . . $ cli_route_cmd#command. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) { |
| 179 | if (commands[i].flagscxx . . $ cli_route_cmd#flags. & CLI_CMD_HIDDENcxx . . $ `src/cli/cli_tool.h:34:9`!) { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | if (cli_tool_is_delimitercxx . . $ cli_tool_is_delimiter(cb17940344e8e149).(&commands[i])) { |
| 184 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\n%s\n", commands[i].descriptioncxx . . $ cli_route_cmd#description.); |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | if (commands[i].descriptioncxx . . $ cli_route_cmd#description. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 189 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "- %40s\n", commands[i].commandcxx . . $ cli_route_cmd#command.); |
| 190 | } else { |
| 191 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "- %-*s\t %s\n", |
| 192 | min_len, commands[i].commandcxx . . $ cli_route_cmd#command., commands[i].descriptioncxx . . $ cli_route_cmd#description.); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("\n")); |
| 197 | cli_tool_print_common_optscxx . . $ cli_tool_print_common_opts(d4f767463ce0a6b3).(min_len); |
| 198 | } |
| 199 | |
| 200 | static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! |
| 201 | cli_tool_check_root_accesscxx . . $ cli_tool_check_root_access(f0eba26fa0f2c404).(uint32_tcxx . . $ uint32_t# flags, |
| 202 | const char *command, |
| 203 | int argc, |
| 204 | const char **argv) |
| 205 | { |
| 206 | uid_tcxx . . $ uid_t# uid; |
| 207 | int i; |
| 208 | |
| 209 | if (!(flags & CLI_CMD_REQUIRE_ROOTcxx . . $ `src/cli/cli_tool.h:33:9`!)) { |
| 210 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 211 | } |
| 212 | |
| 213 | /* If help was requested, we allow the access. */ |
| 214 | for (i = 0; i < argc; i++) { |
| 215 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[i], "-?") == 0) { |
| 216 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 217 | } |
| 218 | |
| 219 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[i], "--help") == 0) { |
| 220 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 221 | } |
| 222 | |
| 223 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[i], "--usage") == 0) { |
| 224 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | uid = getuidcxx . . $ getuid(db52760e79bf9054).(); |
| 229 | if (uid != 0) { |
| 230 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("Authselect command '%s' can only be run as root!\n"), |
| 231 | command); |
| 232 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 233 | } |
| 234 | |
| 235 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 236 | } |
| 237 | |
| 238 | errno_tcxx . . $ errno_t# cli_tool_routecxx . . $ cli_tool_route(32ed8e01bf0ad9de).(int argc, const char **argv, |
| 239 | struct cli_route_cmdcxx . . $ cli_route_cmd# *commands) |
| 240 | { |
| 241 | struct cli_cmdlinecxx . . $ cli_cmdline# cmdline; |
| 242 | const char *cmd; |
| 243 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! bret; |
| 244 | int i; |
| 245 | |
| 246 | if (commands == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 247 | ERRORcxx . . $ `src/common/common.h:71:9`!("Bug: commands can't be NULL!\n"); |
| 248 | return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 249 | } |
| 250 | |
| 251 | if (argc < 2) { |
| 252 | cli_tool_usagecxx . . $ cli_tool_usage(9ffe2780c1253a7a).(argv[0], commands); |
| 253 | return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 254 | } |
| 255 | |
| 256 | cmd = argv[1]; |
| 257 | for (i = 0; commands[i].commandcxx . . $ cli_route_cmd#command. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) { |
| 258 | if (cli_tool_is_delimitercxx . . $ cli_tool_is_delimiter(cb17940344e8e149).(&commands[i])) { |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(commands[i].commandcxx . . $ cli_route_cmd#command., cmd) == 0) { |
| 263 | bret = cli_tool_check_root_accesscxx . . $ cli_tool_check_root_access(f0eba26fa0f2c404).(commands[i].flagscxx . . $ cli_route_cmd#flags., |
| 264 | commands[i].commandcxx . . $ cli_route_cmd#command., |
| 265 | argc, argv); |
| 266 | if (!bret) { |
| 267 | return EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`!; |
| 268 | } |
| 269 | |
| 270 | cmdline.execcxx . . $ cli_cmdline#exec. = argv[0]; |
| 271 | cmdline.commandcxx . . $ cli_cmdline#command. = argv[1]; |
| 272 | cmdline.argccxx . . $ cli_cmdline#argc. = argc - 2; |
| 273 | cmdline.argvcxx . . $ cli_cmdline#argv. = argv + 2; |
| 274 | |
| 275 | return commands[i].fncxx . . $ cli_route_cmd#fn.(&cmdline); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | cli_tool_usagecxx . . $ cli_tool_usage(9ffe2780c1253a7a).(argv[0], commands); |
| 280 | |
| 281 | return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 282 | } |
| 283 | |
| 284 | static struct poptOptioncxx . . $ poptOption# *nonnull_popt_tablecxx . . $ nonnull_popt_table(16cbfeefcfe0495a).(struct poptOptioncxx . . $ poptOption# *options) |
| 285 | { |
| 286 | static struct poptOptioncxx . . $ poptOption# empty[] = { |
| 287 | POPT_TABLEENDcxx . . $ `<external>/16232926639102580118/popt.h:161:9`! |
| 288 | }; |
| 289 | |
| 290 | if (options == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 291 | return empty; |
| 292 | } |
| 293 | |
| 294 | return options; |
| 295 | } |
| 296 | |
| 297 | errno_tcxx . . $ errno_t# cli_tool_popt_excxx . . $ cli_tool_popt_ex(6e0400684c960c82).(struct cli_cmdlinecxx . . $ cli_cmdline# *cmdline, |
| 298 | struct poptOptioncxx . . $ poptOption# *options, |
| 299 | enum cli_tool_optcxx . . $ cli_tool_opt# require_option, |
| 300 | cli_popt_fncxx . . $ cli_popt_fn# popt_fn, |
| 301 | void *popt_fn_pvt, |
| 302 | const char *fopt_name, |
| 303 | const char *fopt_help, |
| 304 | char **_fopt, |
| 305 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! allow_more_free_opts, |
| 306 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! *_opt_set) |
| 307 | { |
| 308 | struct poptOptioncxx . . $ poptOption# opts_table[] = { |
| 309 | {NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, '\0', POPT_ARG_INCLUDE_TABLEcxx . . $ `<external>/16232926639102580118/popt.h:22:9`!, nonnull_popt_tablecxx . . $ nonnull_popt_table(16cbfeefcfe0495a).(options), \ |
| 310 | 0, _cxx . . $ `src/common/common.h:33:9`!("Command options:"), NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 311 | {NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, '\0', POPT_ARG_INCLUDE_TABLEcxx . . $ `<external>/16232926639102580118/popt.h:22:9`!, cli_tool_common_opts_tablecxx . . $ cli_tool_common_opts_table(6c5ec6439ca87f0d).(), \ |
| 312 | 0, _cxx . . $ `src/common/common.h:33:9`!("Common options:"), NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 313 | POPT_AUTOHELPcxx . . $ `<external>/16232926639102580118/popt.h:158:9`! |
| 314 | POPT_TABLEENDcxx . . $ `<external>/16232926639102580118/popt.h:161:9`! |
| 315 | }; |
| 316 | const char *fopt; |
| 317 | char *help; |
| 318 | poptContextcxx . . $ poptContext# pc; |
| 319 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! opt_set; |
| 320 | int ret; |
| 321 | |
| 322 | /* Set output parameter _fopt to NULL value if present. */ |
| 323 | if (_fopt != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 324 | *_fopt = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 325 | } |
| 326 | |
| 327 | /* Create help option string. We always need to append command name since |
| 328 | * we use POPT_CONTEXT_KEEP_FIRST. */ |
| 329 | if (fopt_name == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 330 | help = formatcxx . . $ format(a849077ff1b8d812).("%s %s %s", cmdline->execcxx . . $ cli_cmdline#exec., |
| 331 | cmdline->commandcxx . . $ cli_cmdline#command., _cxx . . $ `src/common/common.h:33:9`!("[OPTIONS...]")); |
| 332 | } else { |
| 333 | help = formatcxx . . $ format(a849077ff1b8d812).("%s %s %s %s", cmdline->execcxx . . $ cli_cmdline#exec., |
| 334 | cmdline->commandcxx . . $ cli_cmdline#command., fopt_name, _cxx . . $ `src/common/common.h:33:9`!("[OPTIONS...]")); |
| 335 | } |
| 336 | if (help == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 337 | ERRORcxx . . $ `src/common/common.h:71:9`!("Out of memory!"); |
| 338 | return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 339 | } |
| 340 | |
| 341 | /* Create popt context. This function is supposed to be called on |
| 342 | * command argv which does not contain executable (argv[0]), therefore |
| 343 | * we need to use KEEP_FIRST that ensures argv[0] is also processed. */ |
| 344 | pc = poptGetContextcxx . . $ poptGetContext(69e83c0752e8078).(cmdline->execcxx . . $ cli_cmdline#exec., cmdline->argccxx . . $ cli_cmdline#argc., cmdline->argvcxx . . $ cli_cmdline#argv., |
| 345 | opts_table, POPT_CONTEXT_KEEP_FIRSTcxx . . $ `<external>/16232926639102580118/popt.h:105:9`!); |
| 346 | |
| 347 | poptSetOtherOptionHelpcxx . . $ poptSetOtherOptionHelp(fc627e009555550b).(pc, help); |
| 348 | |
| 349 | /* Parse options. Invoke custom function if provided. If no parsing |
| 350 | * function is provided, print error on unknown option. */ |
| 351 | while ((ret = poptGetNextOptcxx . . $ poptGetNextOpt(8c14591e39307bbe).(pc)) != -1) { |
| 352 | if (popt_fn != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 353 | ret = popt_fn(pc, ret, popt_fn_pvt); |
| 354 | if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) { |
| 355 | goto done; |
| 356 | } |
| 357 | } else { |
| 358 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("Invalid option %s: %s\n\n"), |
| 359 | poptBadOptioncxx . . $ poptBadOption(abdebb552147fd6f).(pc, 0), poptStrerrorcxx . . $ poptStrerror(f7ae7e56280f18ae).(ret)); |
| 360 | poptPrintHelpcxx . . $ poptPrintHelp(692800addc75c9c3).(pc, stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, 0); |
| 361 | ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 362 | goto done; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /* Parse free option which is always required if requested. */ |
| 367 | fopt = poptGetArgcxx . . $ poptGetArg(86c70cf25448eb00).(pc); |
| 368 | if (_fopt != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 369 | if (fopt == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 370 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("Missing option: %s\n\n"), fopt_help); |
| 371 | poptPrintHelpcxx . . $ poptPrintHelp(692800addc75c9c3).(pc, stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, 0); |
| 372 | ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 373 | goto done; |
| 374 | } |
| 375 | |
| 376 | if (!allow_more_free_opts) { |
| 377 | /* No more arguments expected. |
| 378 | * If something follows it is an error. */ |
| 379 | if (poptGetArgcxx . . $ poptGetArg(86c70cf25448eb00).(pc)) { |
| 380 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("Only one free argument is expected!\n\n")); |
| 381 | poptPrintHelpcxx . . $ poptPrintHelp(692800addc75c9c3).(pc, stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, 0); |
| 382 | ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 383 | goto done; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | *_fopt = strdupcxx . . $ strdup(a8b8e32347182212).(fopt); |
| 388 | if (*_fopt == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 389 | ERRORcxx . . $ `src/common/common.h:71:9`!("Out of memory!"); |
| 390 | ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 391 | goto done; |
| 392 | } |
| 393 | } else if (_fopt == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! && fopt != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 394 | /* Unexpected free argument. */ |
| 395 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("Unexpected parameter: %s\n\n"), fopt); |
| 396 | poptPrintHelpcxx . . $ poptPrintHelp(692800addc75c9c3).(pc, stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, 0); |
| 397 | ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 398 | goto done; |
| 399 | } |
| 400 | |
| 401 | opt_set = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 402 | if ((_fopt != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! && cmdline->argccxx . . $ cli_cmdline#argc. < 2) || cmdline->argccxx . . $ cli_cmdline#argc. < 1) { |
| 403 | opt_set = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 404 | |
| 405 | /* If at least one option is required and not provided, print error. */ |
| 406 | if (require_option == CLI_TOOL_OPT_REQUIREDcxx . . $ CLI_TOOL_OPT_REQUIRED.) { |
| 407 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, _cxx . . $ `src/common/common.h:33:9`!("At least one option is required!\n\n")); |
| 408 | poptPrintHelpcxx . . $ poptPrintHelp(692800addc75c9c3).(pc, stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, 0); |
| 409 | ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 410 | goto done; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (_opt_set != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 415 | *_opt_set = opt_set; |
| 416 | } |
| 417 | |
| 418 | ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!; |
| 419 | |
| 420 | done: |
| 421 | poptFreeContextcxx . . $ poptFreeContext(c32b9b4db48a8347).(pc); |
| 422 | freecxx . . $ free(5d22bdacc48458e8).(help); |
| 423 | if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`! && _fopt != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 424 | freecxx . . $ free(5d22bdacc48458e8).(*_fopt); |
| 425 | *_fopt = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 426 | } |
| 427 | |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | errno_tcxx . . $ errno_t# cli_tool_poptcxx . . $ cli_tool_popt(56747616750755be).(struct cli_cmdlinecxx . . $ cli_cmdline# *cmdline, |
| 432 | struct poptOptioncxx . . $ poptOption# *options, |
| 433 | enum cli_tool_optcxx . . $ cli_tool_opt# require_option, |
| 434 | cli_popt_fncxx . . $ cli_popt_fn# popt_fn, |
| 435 | void *popt_fn_pvt) |
| 436 | { |
| 437 | return cli_tool_popt_excxx . . $ cli_tool_popt_ex(6e0400684c960c82).(cmdline, options, require_option, |
| 438 | popt_fn, popt_fn_pvt, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, |
| 439 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 440 | } |
| 441 | |
| 442 | int cli_tool_maincxx . . $ cli_tool_main(d9b5b3c5de31f8fa).(int argc, const char **argv, |
| 443 | struct cli_route_cmdcxx . . $ cli_route_cmd# *commands, |
| 444 | void *pvt) |
| 445 | { |
| 446 | errno_tcxx . . $ errno_t# ret; |
| 447 | |
| 448 | cli_tool_common_optscxx . . $ cli_tool_common_opts(a4c963044a66329d).(&argc, argv); |
| 449 | |
| 450 | ret = cli_tool_routecxx . . $ cli_tool_route(32ed8e01bf0ad9de).(argc, argv, commands); |
| 451 | |
| 452 | switch (ret) { |
| 453 | case EOKcxx . . $ `src/common/errno_t.h:34:9`!: |
| 454 | return 0; |
| 455 | case ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!: |
| 456 | return 2; |
| 457 | case EBADFcxx . . $ `<external>/8987895939074593727/errno-base.h:13:9`!: |
| 458 | return 3; |
| 459 | case EEXISTcxx . . $ `<external>/8987895939074593727/errno-base.h:21:9`!: |
| 460 | return 4; |
| 461 | case EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`!: |
| 462 | return 5; |
| 463 | case ENODEVcxx . . $ `<external>/8987895939074593727/errno-base.h:23:9`!: |
| 464 | return 6; |
| 465 | } |
| 466 | |
| 467 | /* Generic error. */ |
| 468 | return 1; |
| 469 | } |