| 1 |
#include "utils/parse_args.h" |
| 2 |
#include "utils/arch_trans.h" |
| 3 |
#include "utils/error.h" |
| 4 |
#include "utils/logger.h" |
| 5 |
#include <argp.h> |
| 6 |
#include <errno.h> |
| 7 |
#include <fcntl.h> |
| 8 |
#include <stdbool.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
static bool stop_parse = false; |
| 15 |
|
| 16 |
static uint64_t |
| 17 |
fail_fast_strtoull (const char *restrict num, const char *restrict error_msg) |
| 18 |
{ |
| 19 |
char *end; |
| 20 |
errno = 0; |
| 21 |
uint64_t result = strtoull (num, &end, 0); |
| 22 |
if (*end != '\0' || errno) |
| 23 |
error ("%s", error_msg); |
| 24 |
return result; |
| 25 |
} |
| 26 |
|
| 27 |
static FILE * |
| 28 |
fail_fast_fopen (const char *restrict filename, const char *restrict mode) |
| 29 |
{ |
| 30 |
if (!strcmp (filename, "-")) |
| 31 |
{ |
| 32 |
if (*mode == 'r') |
| 33 |
return stdin; |
| 34 |
else |
| 35 |
return stdout; |
| 36 |
} |
| 37 |
|
| 38 |
FILE *fp = fopen (filename, mode); |
| 39 |
if (fp == NULL) |
| 40 |
error (M_UNABLE_OPEN_FILE, filename, strerror (errno)); |
| 41 |
fcntl (fileno (fp), F_SETFD, FD_CLOEXEC); |
| 42 |
return fp; |
| 43 |
} |
| 44 |
|
| 45 |
static uint32_t |
| 46 |
fail_fast_resolve_arch (const char *archname) |
| 47 |
{ |
| 48 |
uint32_t arch = str_to_scmp_arch (archname, true); |
| 49 |
if (arch == (uint32_t)-1) |
| 50 |
error (M_UNSUPPORTED_ARCH, archname); |
| 51 |
return arch; |
| 52 |
} |
| 53 |
|
| 54 |
static subcommand_t |
| 55 |
parse_subcommand (const char *arg) |
| 56 |
{ |
| 57 |
if (!strcmp (arg, "asm")) |
| 58 |
return ASM_MODE; |
| 59 |
else if (!strcmp (arg, "disasm")) |
| 60 |
return DISASM_MODE; |
| 61 |
else if (!strcmp (arg, "emu")) |
| 62 |
return EMU_MODE; |
| 63 |
else if (!strcmp (arg, "trace") || !strcmp (arg, "dump")) |
| 64 |
return TRACE_MODE; |
| 65 |
else if (!strcmp (arg, "probe")) |
| 66 |
return PROBE_MODE; |
| 67 |
else if (!strcmp (arg, "version")) |
| 68 |
return VERSION_MODE; |
| 69 |
else if (!strcmp (arg, "help")) |
| 70 |
return HELP_MODE; |
| 71 |
else |
| 72 |
return HELP_ABNORMAL; |
| 73 |
} |
| 74 |
|
| 75 |
static color_mode_t |
| 76 |
parse_color_mode (const char *arg) |
| 77 |
{ |
| 78 |
if (!strcmp (arg, "always")) |
| 79 |
return ALWAYS; |
| 80 |
else if (!strcmp (arg, "auto")) |
| 81 |
return AUTO; |
| 82 |
else if (!strcmp (arg, "never")) |
| 83 |
return NEVER; |
| 84 |
else |
| 85 |
error ("%s: %s", M_INVALID_COLOR_MODE, arg); |
| 86 |
} |
| 87 |
|
| 88 |
static print_mode_t |
| 89 |
parse_print_mode (const char *arg) |
| 90 |
{ |
| 91 |
if (!strcmp (arg, "hexfmt")) |
| 92 |
return HEXFMT; |
| 93 |
else if (!strcmp (arg, "hexline")) |
| 94 |
return HEXLINE; |
| 95 |
else if (!strcmp (arg, "raw")) |
| 96 |
return RAW; |
| 97 |
else |
| 98 |
error ("%s: %s", M_INVALID_FMT_MODE, arg); |
| 99 |
} |
| 100 |
|
| 101 |
static int |
| 102 |
parse_asm (asm_arg_t *args, int key, const char *arg, struct argp_state *state) |
| 103 |
{ |
| 104 |
switch (key) |
| 105 |
{ |
| 106 |
case ARGP_KEY_ARG: |
| 107 |
if (state->arg_num == 1) |
| 108 |
args->text_file = fail_fast_fopen (arg, "r"); |
| 109 |
return 0; |
| 110 |
case 'a': |
| 111 |
args->scmp_arch = fail_fast_resolve_arch (arg); |
| 112 |
return 0; |
| 113 |
case 'f': |
| 114 |
args->mode = parse_print_mode (arg); |
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
static int |
| 122 |
parse_disasm (disasm_arg_t *args, int key, const char *arg, |
| 123 |
struct argp_state *state) |
| 124 |
{ |
| 125 |
switch (key) |
| 126 |
{ |
| 127 |
case ARGP_KEY_ARG: |
| 128 |
if (state->arg_num == 1) |
| 129 |
args->raw_file = fail_fast_fopen (arg, "r"); |
| 130 |
return 0; |
| 131 |
case 'a': |
| 132 |
args->scmp_arch = fail_fast_resolve_arch (arg); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
static int |
| 140 |
parse_emu (emu_arg_t *args, int key, const char *arg, struct argp_state *state) |
| 141 |
{ |
| 142 |
switch (key) |
| 143 |
{ |
| 144 |
case ARGP_KEY_ARG: |
| 145 |
if (state->arg_num == 1) |
| 146 |
args->text_file = fail_fast_fopen (arg, "r"); |
| 147 |
else if (state->arg_num == 2) |
| 148 |
args->sys_name = arg; |
| 149 |
else if (state->arg_num >= 3 && state->arg_num <= 8) |
| 150 |
args->args[state->arg_num - 3] |
| 151 |
= fail_fast_strtoull (arg, M_INVALID_NUMBER); |
| 152 |
else if (state->arg_num == 9) |
| 153 |
args->ip = fail_fast_strtoull (arg, M_INVALID_NUMBER); |
| 154 |
return 0; |
| 155 |
case 'a': |
| 156 |
args->scmp_arch = fail_fast_resolve_arch (arg); |
| 157 |
return 0; |
| 158 |
case 'q': |
| 159 |
args->quiet = true; |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
static int |
| 167 |
parse_trace (trace_arg_t *args, int key, const char *arg, |
| 168 |
struct argp_state *state) |
| 169 |
{ |
| 170 |
switch (key) |
| 171 |
{ |
| 172 |
case ARGP_KEY_ARG: |
| 173 |
if (state->arg_num != 1 || args->mode != UNDECIDED) |
| 174 |
return 0; |
| 175 |
args->mode = TRACE_PROG; |
| 176 |
args->prog_idx = state->next - 1; |
| 177 |
stop_parse = true; |
| 178 |
return 0; |
| 179 |
case 'p': |
| 180 |
if (args->mode != UNDECIDED) |
| 181 |
return 0; |
| 182 |
args->mode = TRACE_PID; |
| 183 |
args->pid = fail_fast_strtoull (arg, M_INVALID_NUMBER); |
| 184 |
return 0; |
| 185 |
case 'o': |
| 186 |
if (args->mode != UNDECIDED) |
| 187 |
return 0; |
| 188 |
args->output_file = fail_fast_fopen (arg, "w+"); |
| 189 |
return 0; |
| 190 |
case 'q': |
| 191 |
args->quiet = true; |
| 192 |
return 0; |
| 193 |
case 's': |
| 194 |
args->seize = true; |
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
static int |
| 202 |
parse_probe (probe_arg_t *args, int key, const char *arg, |
| 203 |
struct argp_state *state) |
| 204 |
{ |
| 205 |
switch (key) |
| 206 |
{ |
| 207 |
case ARGP_KEY_ARG: |
| 208 |
if (state->arg_num != 1) |
| 209 |
return 0; |
| 210 |
args->prog_idx = state->next - 1; |
| 211 |
stop_parse = true; |
| 212 |
return 0; |
| 213 |
case 'o': |
| 214 |
args->output_file = fail_fast_fopen (arg, "w+"); |
| 215 |
return 0; |
| 216 |
case 'q': |
| 217 |
args->quiet = true; |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
error_t |
| 225 |
parse_opt (int key, char *arg, struct argp_state *state) |
| 226 |
{ |
| 227 |
ceccomp_arg_t *args = state->input; |
| 228 |
|
| 229 |
if (stop_parse) |
| 230 |
return 0; |
| 231 |
|
| 232 |
switch (key) |
| 233 |
{ |
| 234 |
case ARGP_KEY_ARG: |
| 235 |
if (state->arg_num == 0) |
| 236 |
args->cmd = parse_subcommand (arg); |
| 237 |
break; |
| 238 |
case 'c': |
| 239 |
args->when = parse_color_mode (arg); |
| 240 |
return 0; |
| 241 |
case 'h': |
| 242 |
if (args->cmd == HELP_ABNORMAL) |
| 243 |
args->cmd = HELP_MODE; |
| 244 |
return 0; |
| 245 |
case 'u': |
| 246 |
if (args->cmd == HELP_ABNORMAL) |
| 247 |
args->cmd = HELP_MODE; |
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
if (args->cmd == ASM_MODE) |
| 252 |
return parse_asm (args->asm_arg, key, arg, state); |
| 253 |
else if (args->cmd == DISASM_MODE) |
| 254 |
return parse_disasm (args->disasm_arg, key, arg, state); |
| 255 |
else if (args->cmd == EMU_MODE) |
| 256 |
return parse_emu (args->emu_arg, key, arg, state); |
| 257 |
else if (args->cmd == TRACE_MODE) |
| 258 |
return parse_trace (args->trace_arg, key, arg, state); |
| 259 |
else if (args->cmd == PROBE_MODE) |
| 260 |
return parse_probe (args->probe_arg, key, arg, state); |
| 261 |
|
| 262 |
return 0; |
| 263 |
} |