| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "args.h"cxx . . $ `<file>/Descent3/args.h`/ |
| 20 | #include "mono.h"cxx . . $ `<file>/ddebug/mono.h`/ |
| 21 | #include <string.h> |
| 22 | |
| 23 | #if defined(_WIN32) |
| 24 | #define strcasecmp stricmp |
| 25 | #endif |
| 26 | |
| 27 | // rcg07062000 used to be static. |
| 28 | static int TotalArgscxx . . $ TotalArgs. = 0; |
| 29 | char GameArgscxx . . $ GameArgs.[MAX_ARGScxx . . $ `Descent3/args.h:22:9`!][MAX_CHARS_PER_ARGcxx . . $ `Descent3/args.h:23:9`!]; |
| 30 | |
| 31 | const char *GetArgcxx . . $ GetArg(f7ae7e56280f18ae).(int index) { |
| 32 | if (index >= TotalArgscxx . . $ TotalArgs. || index == 0) |
| 33 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 34 | return GameArgscxx . . $ GameArgs.[index]; |
| 35 | } |
| 36 | |
| 37 | // Gathers all arguments |
| 38 | void GatherArgscxx . . $ GatherArgs(85c52e162fed56f9).(const char *str) { |
| 39 | int i, t; |
| 40 | static int curarg = 1; // DAJ made static |
| 41 | int len = strlen(str); |
| 42 | bool gotquote = false; |
| 43 | for (t = 0, i = 0; i < len; i++) { |
| 44 | if (t == 0) { |
| 45 | if (str[i] == '\"') { |
| 46 | gotquote = true; |
| 47 | continue; |
| 48 | } |
| 49 | } |
| 50 | if (gotquote) { |
| 51 | if (str[i] != '\"' && str[i] != 0) |
| 52 | GameArgscxx . . $ GameArgs.[curarg][t++] = str[i]; |
| 53 | else { |
| 54 | GameArgscxx . . $ GameArgs.[curarg][t] = 0; |
| 55 | t = 0; |
| 56 | gotquote = false; |
| 57 | curarg++; |
| 58 | } |
| 59 | } else { |
| 60 | if (str[i] != ' ' && str[i] != 0) |
| 61 | GameArgscxx . . $ GameArgs.[curarg][t++] = str[i]; |
| 62 | else { |
| 63 | GameArgscxx . . $ GameArgs.[curarg][t] = 0; |
| 64 | t = 0; |
| 65 | curarg++; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | GameArgscxx . . $ GameArgs.[curarg][t] = 0; |
| 70 | curarg++; |
| 71 | TotalArgscxx . . $ TotalArgs. = curarg; |
| 72 | |
| 73 | for (int q = 0; q < TotalArgscxx . . $ TotalArgs.; q++) |
| 74 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "GatherArgs: Arg (%d) is [%s].", q, GameArgs[q]); |
| 75 | } |
| 76 | |
| 77 | void GatherArgscxx . . $ GatherArgs(3ed91490e421178f).(char **argv) { |
| 78 | TotalArgscxx . . $ TotalArgs. = 0; |
| 79 | |
| 80 | for (int i = 0; ((i < MAX_ARGScxx . . $ `Descent3/args.h:22:9`!) && (argv[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)); i++) { |
| 81 | TotalArgscxx . . $ TotalArgs.++; |
| 82 | strncpy(GameArgscxx . . $ GameArgs.[i], argv[i], MAX_CHARS_PER_ARGcxx . . $ `Descent3/args.h:23:9`!); |
| 83 | } // for |
| 84 | |
| 85 | for (int q = 0; q < TotalArgscxx . . $ TotalArgs.; q++) |
| 86 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "GatherArgs: Arg (%d) is [%s].", q, GameArgs[q]); |
| 87 | } // GatherArgs |
| 88 | |
| 89 | // Strip '-', '--', and '+' flag prefix, so --foo, -foo, +foo => foo, but pass through - -- + ++ |
| 90 | const char *SkipArgPrefixcxx . . $ SkipArgPrefix(fb58d2e09a2a1ce6).(const char *arg) { |
| 91 | if (*arg != '\0' && *(arg + 1) != '\0') { |
| 92 | if (*arg == '-') { |
| 93 | if (*(arg + 1) == '-' && *(arg + 2) != '\0') { |
| 94 | return arg + 2; |
| 95 | } |
| 96 | if (*(arg + 1) != '-') { |
| 97 | return arg + 1; |
| 98 | } |
| 99 | } else if (*arg == '+' && *(arg + 1) != '+') { |
| 100 | return arg + 1; |
| 101 | } |
| 102 | } |
| 103 | return arg; |
| 104 | } |
| 105 | |
| 106 | int FindArgcxx . . $ FindArg(5c4306d8553b3696).(const char *which) { |
| 107 | if (which == nullptr) |
| 108 | return 0; |
| 109 | |
| 110 | auto which_matches = [which = SkipArgPrefixcxx . . $ SkipArgPrefix(fb58d2e09a2a1ce6).(which)](char *arg) -> bool { |
| 111 | return strcasecmp(which, SkipArgPrefixcxx . . $ SkipArgPrefix(fb58d2e09a2a1ce6).(arg)) == 0; |
| 112 | }; |
| 113 | |
| 114 | for (int i = 1; i <= TotalArgscxx . . $ TotalArgs.; i++) { |
| 115 | if (which_matches(cxx . . $ FindArg(5c4306d8553b3696).$anonymous_type_0#`operator()`(1e6f1c46fee36e89).GameArgscxx . . $ GameArgs.[i])) { |
| 116 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "FindArg: Found [%s] at argument index (%d).", which, i); |
| 117 | return i; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "FindArg: Did not find [%s] on command line.", which); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | // Returns index of argument sought, or 0 if not found |
| 126 | int FindArgCharcxx . . $ FindArgChar(7c0a79fa02e3e627).(const char *which, char singleCharArg) { |
| 127 | for (int i = 1; i <= TotalArgscxx . . $ TotalArgs.; i++) { |
| 128 | char *str = GameArgscxx . . $ GameArgs.[i]; |
| 129 | if (str[0] == '-' && str[1] == singleCharArg && str[2] == '\0') { |
| 130 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "FindArg: Found [-%c] at argument index (%d).", singleCharArg, i); |
| 131 | return i; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return FindArgcxx . . $ FindArg(5c4306d8553b3696).(which); |
| 136 | } |