diff options
| author | 2026-11-06 03:30:00 +0000 | |
|---|---|---|
| committer | 2026-11-06 07:09:55 +0000 | |
| commit | 621e2a2508d290793cdc22d45e2ec1f4d53fcae8 (patch) | |
| tree | 31e2263997b685fa8cb47e1e3718862105ef4a02 /src/arg.c | |
| parent | feat(utils): add various utilities (diff) | |
| download | codebase-621e2a2508d290793cdc22d45e2ec1f4d53fcae8.tar.gz | |
feat(arg): add immediate mode argument parser
Diffstat (limited to 'src/arg.c')
| -rw-r--r-- | src/arg.c | 816 |
1 files changed, 816 insertions, 0 deletions
diff --git a/src/arg.c b/src/arg.c new file mode 100644 index 0000000..7e81e57 --- /dev/null +++ b/src/arg.c | |||
| @@ -0,0 +1,816 @@ | |||
| 1 | // arg.c --- immediate mode argument parser -*- C -*- | ||
| 2 | // | ||
| 3 | // Copyright (c) 2026 Martin Michalec <martin@michalec.dev> | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: MIT | ||
| 6 | // | ||
| 7 | // This file is part of COSMOS Codebase. | ||
| 8 | // | ||
| 9 | // Commentary: | ||
| 10 | // | ||
| 11 | // Code: | ||
| 12 | |||
| 13 | #include "arg.h" | ||
| 14 | #include "dbg.h" | ||
| 15 | #include "lgr.h" | ||
| 16 | #include "da.h" | ||
| 17 | #include "hm.h" | ||
| 18 | #include "sb.h" | ||
| 19 | |||
| 20 | #define _ARG_HELP(KIND, DESC, ...) \ | ||
| 21 | DA_PUSH (&_arg->entries, \ | ||
| 22 | ((struct arg_entry) { \ | ||
| 23 | .kind = ARG_ENTRY_KIND_ ## KIND, \ | ||
| 24 | .desc = mv_or (DESC, MV0 ("TODO: add description")), \ | ||
| 25 | __VA_ARGS__ \ | ||
| 26 | })) | ||
| 27 | |||
| 28 | #define _ARG_ERROR(fmt, ...) \ | ||
| 29 | sb_printf (&_arg->errors, " " fmt "\n", ##__VA_ARGS__) | ||
| 30 | |||
| 31 | enum arg_tok_kind { | ||
| 32 | ARG_TOK_KIND_NULL, //!< Last in `_arg->toks`. | ||
| 33 | ARG_TOK_KIND_UNCLAIMED, //!< Any of `ARGUMENT | OPERAND | SUB`. | ||
| 34 | ARG_TOK_KIND_ARGUMENT, //!< Argument of `REQ | OPT` | ||
| 35 | ARG_TOK_KIND_OPERAND, //!< Operand (not `FLG | REQ | OPT | SUB`). | ||
| 36 | ARG_TOK_KIND_SUB, //!< Sub-command. | ||
| 37 | ARG_TOK_KIND_SHORT, //!< Short `FLG | REQ | OPT`. | ||
| 38 | ARG_TOK_KIND_LONG, //!< Long `FLG | REQ | OPT`. | ||
| 39 | }; | ||
| 40 | |||
| 41 | struct arg_tok { | ||
| 42 | struct mv key; | ||
| 43 | u32 argv_idx; | ||
| 44 | u32 prev_idx; | ||
| 45 | enum arg_tok_kind kind; | ||
| 46 | int consumed; | ||
| 47 | }; | ||
| 48 | |||
| 49 | enum arg_entry_kind { | ||
| 50 | ARG_ENTRY_KIND_VERBATIM, | ||
| 51 | ARG_ENTRY_KIND_SUB, | ||
| 52 | ARG_ENTRY_KIND_FLG, | ||
| 53 | ARG_ENTRY_KIND_REQ, | ||
| 54 | ARG_ENTRY_KIND_REQ_ENUM, | ||
| 55 | ARG_ENTRY_KIND_OPT, | ||
| 56 | ARG_ENTRY_KIND_OPT_U64, | ||
| 57 | }; | ||
| 58 | |||
| 59 | struct arg_entry { | ||
| 60 | enum arg_entry_kind kind; | ||
| 61 | union { | ||
| 62 | struct mv verbatim; | ||
| 63 | struct mv desc; | ||
| 64 | }; | ||
| 65 | union { | ||
| 66 | struct { | ||
| 67 | struct mv name; | ||
| 68 | } sub; | ||
| 69 | struct { | ||
| 70 | /* */ char sh; | ||
| 71 | struct mv ln; | ||
| 72 | } flg; | ||
| 73 | struct { | ||
| 74 | /* */ char sh; | ||
| 75 | struct mv ln; | ||
| 76 | struct mv lbl; | ||
| 77 | struct mv off; | ||
| 78 | } req; | ||
| 79 | struct { | ||
| 80 | /* */ char sh; | ||
| 81 | struct mv ln; | ||
| 82 | struct mv_arr sel; | ||
| 83 | /* */ int off; | ||
| 84 | } req_enum; | ||
| 85 | struct { | ||
| 86 | /* */ char sh; | ||
| 87 | struct mv ln; | ||
| 88 | struct mv lbl; | ||
| 89 | struct mv off; | ||
| 90 | struct mv dflt; | ||
| 91 | } opt; | ||
| 92 | struct { | ||
| 93 | /* */ char sh; | ||
| 94 | struct mv ln; | ||
| 95 | struct mv lbl; | ||
| 96 | /* */ u64 off; | ||
| 97 | /* */ u64 dflt; | ||
| 98 | } opt_u64; | ||
| 99 | }; | ||
| 100 | }; | ||
| 101 | |||
| 102 | struct arg { | ||
| 103 | struct mv prog_name; | ||
| 104 | struct mv usage; | ||
| 105 | struct mv desc; | ||
| 106 | |||
| 107 | struct { struct da_hdr hdr; struct arg_tok *arr; } toks; | ||
| 108 | struct { struct da_hdr hdr; struct arg_entry *arr; } entries; | ||
| 109 | |||
| 110 | u32 sh_to_tok_idx['9' - '0' + 1 + 'Z' - 'A' + 1 + 'z' - 'a' + 1]; | ||
| 111 | struct hm ln_to_tok; | ||
| 112 | struct hm nm_to_tok; | ||
| 113 | |||
| 114 | int min_ops; | ||
| 115 | int max_ops; | ||
| 116 | |||
| 117 | struct sb errors; | ||
| 118 | int help_requested; | ||
| 119 | }; | ||
| 120 | |||
| 121 | static struct arg *_arg = NULL; | ||
| 122 | |||
| 123 | #ifdef ARG_TRACE_TOK | ||
| 124 | |||
| 125 | #define DBG_TRACE_ARG_TOK(...) _dbg_trace_arg_tok (DBG_LOC, ##__VA_ARGS__) | ||
| 126 | static void | ||
| 127 | _dbg_trace_arg_tok (struct dbg_loc loc, const struct arg_tok *tok) | ||
| 128 | { | ||
| 129 | DBG_ASSERT_NN (tok); | ||
| 130 | DBG_TRACE_BEG_LOC (loc, "info", ""); | ||
| 131 | DBG_TRACE_ADD ("key: %.*s, ", MV_FMT (tok->key)); | ||
| 132 | DBG_TRACE_ADD ("argv_idx: %u, ", tok->argv_idx); | ||
| 133 | DBG_TRACE_ADD ("prev_idx: %u, ", tok->prev_idx); | ||
| 134 | DBG_TRACE_ADD ("kind: %d, ", tok->kind); | ||
| 135 | DBG_TRACE_ADD ("consumed: %d", tok->consumed); | ||
| 136 | DBG_TRACE_END (""); | ||
| 137 | } | ||
| 138 | |||
| 139 | #else // ARG_TRACE_TOK | ||
| 140 | |||
| 141 | #define DBG_TRACE_ARG_TOK(...) NOP () | ||
| 142 | |||
| 143 | #endif // ARG_TRACE_TOK | ||
| 144 | |||
| 145 | static inline u8 | ||
| 146 | arg_norm_sh (char sh) | ||
| 147 | { | ||
| 148 | if ('0' <= sh && sh <= '9') return sh - '0'; | ||
| 149 | if ('A' <= sh && sh <= 'Z') return sh - 'A' + '9' - '0'; | ||
| 150 | if ('a' <= sh && sh <= 'z') return sh - 'a' + '9' - '0' + 'Z' - 'A'; | ||
| 151 | |||
| 152 | DBG_UNREACHABLE (); | ||
| 153 | return 0; | ||
| 154 | } | ||
| 155 | |||
| 156 | static inline void | ||
| 157 | arg_push_tok (struct mv key, u32 argv_idx, enum arg_tok_kind kind) | ||
| 158 | { | ||
| 159 | DBG_ASSERT (!mv_is_empty (key)); | ||
| 160 | DBG_ASSERT_NZ (kind, "%d"); | ||
| 161 | |||
| 162 | struct arg_tok *tok = DA_GRAB (&_arg->toks, 1); | ||
| 163 | tok->key = key; | ||
| 164 | tok->argv_idx = argv_idx; | ||
| 165 | tok->kind = kind; | ||
| 166 | |||
| 167 | u32 tok_idx = tok - _arg->toks.arr; | ||
| 168 | switch (kind) { | ||
| 169 | case ARG_TOK_KIND_SHORT: { | ||
| 170 | u8 norm = arg_norm_sh (*key.beg); | ||
| 171 | tok->prev_idx = _arg->sh_to_tok_idx[norm]; | ||
| 172 | _arg->sh_to_tok_idx[norm] = tok_idx; | ||
| 173 | } break; | ||
| 174 | |||
| 175 | case ARG_TOK_KIND_LONG: { | ||
| 176 | ptrdiff *hit = | ||
| 177 | hm_rel_upsert (&_arg->ln_to_tok, key, &_arg->toks.arr->key); | ||
| 178 | if (*hit != HM_REL_NEW) | ||
| 179 | tok->prev_idx = *hit/sizeof (*tok); | ||
| 180 | *hit = tok_idx*sizeof (*tok); | ||
| 181 | } break; | ||
| 182 | |||
| 183 | default: break; | ||
| 184 | } | ||
| 185 | |||
| 186 | DBG_TRACE_ARG_TOK (tok); | ||
| 187 | } | ||
| 188 | |||
| 189 | #include <stdlib.h> | ||
| 190 | void | ||
| 191 | arg_beg (int argc, char **argv, struct mv usage, | ||
| 192 | struct mv desc, int min_ops, int max_ops) | ||
| 193 | { | ||
| 194 | DBG_ASSERT_EN (_arg); | ||
| 195 | DBG_ASSERT_GE ( argc, 1, "%d", "%d"); | ||
| 196 | DBG_ASSERT_NN ( argv); | ||
| 197 | DBG_ASSERT_NN (*argv); | ||
| 198 | DBG_ASSERT_GE (min_ops, 0, "%d", "%d"); | ||
| 199 | DBG_ASSERT_GE (max_ops, 0, "%d", "%d"); | ||
| 200 | |||
| 201 | _arg = calloc (1, sizeof (*_arg)); // use scratch allocator | ||
| 202 | |||
| 203 | _arg->prog_name = mv_from_cstr (argv[0]); // MAYBE(cmmm): take basename? | ||
| 204 | _arg->usage = mv_or (usage, MV0 ("")); | ||
| 205 | _arg->desc = mv_or (desc, MV0 ("")); | ||
| 206 | _arg->min_ops = min_ops; | ||
| 207 | _arg->max_ops = max_ops; | ||
| 208 | _arg->toks .hdr = DA_HDR_FROM (GPA_SYS); | ||
| 209 | _arg->entries.hdr = DA_HDR_FROM (GPA_SYS); | ||
| 210 | _arg->ln_to_tok = HM_FROM (GPA_SYS); | ||
| 211 | _arg->nm_to_tok = HM_FROM (GPA_SYS); | ||
| 212 | |||
| 213 | // MAYBE(cmmm): figure out a better way to make idx 0 invalid. | ||
| 214 | // One way would be to have a shift/offset in the da. | ||
| 215 | DA_GRAB (&_arg->toks, 1); | ||
| 216 | |||
| 217 | _ARG_HELP (FLG, MV0 ("Print this message and exit."), | ||
| 218 | .flg.sh = 'h', .flg.ln = MV0 ("help")); | ||
| 219 | |||
| 220 | for (int i = 1; i < argc; ++i) { | ||
| 221 | DBG_ASSERT_NN (argv[i]); | ||
| 222 | // DBG_TRACE ("info", "argv[%d]: %s", i, argv[i]); | ||
| 223 | struct mv arg_mv = mv_from_cstr (argv[i]); | ||
| 224 | if (mv_strip_u8 (&arg_mv, '-', MV_BEG)) { | ||
| 225 | if (mv_strip_u8 (&arg_mv, '-', MV_BEG)) { | ||
| 226 | if (mv_sz (arg_mv) == 0) { | ||
| 227 | // only operands follow | ||
| 228 | while (++i < argc) | ||
| 229 | arg_push_tok (mv_from_cstr (argv[i]), i, ARG_TOK_KIND_OPERAND); | ||
| 230 | break; | ||
| 231 | } | ||
| 232 | |||
| 233 | if (mv_cmp (arg_mv, MV0 ("help")) == 0) | ||
| 234 | _arg->help_requested = 1; | ||
| 235 | |||
| 236 | arg_push_tok (arg_mv, i, ARG_TOK_KIND_LONG); | ||
| 237 | if (mv_strip_upto_u8 (&arg_mv, '=', MV_BEG | MV_INC)) | ||
| 238 | arg_push_tok (arg_mv, i, ARG_TOK_KIND_ARGUMENT); | ||
| 239 | continue; | ||
| 240 | } | ||
| 241 | |||
| 242 | // literal - | ||
| 243 | if (mv_sz (arg_mv) == 0) { | ||
| 244 | arg_push_tok (MV0 ("-"), i, ARG_TOK_KIND_UNCLAIMED); | ||
| 245 | continue; | ||
| 246 | } | ||
| 247 | |||
| 248 | // short group | ||
| 249 | while (mv_sz (arg_mv)) { | ||
| 250 | struct mv sh = mv_chop (&arg_mv, 1, 0); | ||
| 251 | // DBG_TRACE ("info", "sh: %.*s", MV_FMT (sh)); | ||
| 252 | arg_push_tok (sh, i, ARG_TOK_KIND_SHORT); | ||
| 253 | |||
| 254 | if (*sh.beg == 'h') | ||
| 255 | _arg->help_requested = 1; | ||
| 256 | } continue; | ||
| 257 | } | ||
| 258 | |||
| 259 | arg_push_tok (arg_mv, i, ARG_TOK_KIND_UNCLAIMED); | ||
| 260 | } | ||
| 261 | |||
| 262 | DA_GRAB_0 (&_arg->toks, 1); | ||
| 263 | } | ||
| 264 | |||
| 265 | struct arg_tok * | ||
| 266 | _arg_last_tok_sh (char sh) | ||
| 267 | { | ||
| 268 | if (sh == '\0') | ||
| 269 | return NULL; | ||
| 270 | |||
| 271 | u32 idx = _arg->sh_to_tok_idx[arg_norm_sh (sh)]; | ||
| 272 | if (!idx) | ||
| 273 | return NULL; | ||
| 274 | |||
| 275 | return DA_AT (_arg->toks, idx); | ||
| 276 | } | ||
| 277 | |||
| 278 | struct arg_tok * | ||
| 279 | _arg_last_tok_ln (struct mv ln) | ||
| 280 | { | ||
| 281 | if (mv_is_empty (ln)) | ||
| 282 | return NULL; | ||
| 283 | |||
| 284 | const struct mv *hit = | ||
| 285 | hm_rel_lookup (&_arg->ln_to_tok, ln, &_arg->toks.arr->key); | ||
| 286 | if (!hit) | ||
| 287 | return NULL; | ||
| 288 | |||
| 289 | return CONTAINER_OF (hit, struct arg_tok, key); | ||
| 290 | } | ||
| 291 | |||
| 292 | struct arg_tok * | ||
| 293 | _arg_prev_tok (struct arg_tok *tok) | ||
| 294 | { | ||
| 295 | DBG_ASSERT_NN (tok); | ||
| 296 | DBG_ASSERT_EZ_MSG (tok->consumed, "%d", "key: %.*s", MV_FMT (tok->key)); | ||
| 297 | tok->consumed = 1; | ||
| 298 | // DBG_TRACE ("info", "consumed: %.*s", MV_FMT (tok->key)); | ||
| 299 | |||
| 300 | if (!tok->prev_idx) | ||
| 301 | return NULL; | ||
| 302 | |||
| 303 | return DA_AT (_arg->toks, tok->prev_idx); | ||
| 304 | } | ||
| 305 | |||
| 306 | int | ||
| 307 | _arg_missing_req (struct arg_tok *tok) | ||
| 308 | { | ||
| 309 | if (tok[1].kind == ARG_TOK_KIND_NULL) { | ||
| 310 | _ARG_ERROR ("Missing required argument for %s%.*s", | ||
| 311 | tok->kind == ARG_TOK_KIND_SHORT ? "-" : "--", | ||
| 312 | MV_FMT (tok->key)); | ||
| 313 | return 1; | ||
| 314 | } | ||
| 315 | |||
| 316 | return 0; | ||
| 317 | } | ||
| 318 | |||
| 319 | struct arg_tok * | ||
| 320 | _arg_req_arg (struct arg_tok *tok) | ||
| 321 | { | ||
| 322 | struct arg_tok *arg = tok + 1; | ||
| 323 | if (arg->kind != ARG_TOK_KIND_UNCLAIMED) { | ||
| 324 | _ARG_ERROR ("Missing required argument for %s%.*s", | ||
| 325 | tok->kind == ARG_TOK_KIND_SHORT ? "-" : "--", | ||
| 326 | MV_FMT (tok->key)); | ||
| 327 | return NULL; | ||
| 328 | } | ||
| 329 | arg->consumed = 1; | ||
| 330 | arg->kind = ARG_TOK_KIND_ARGUMENT; | ||
| 331 | return arg; | ||
| 332 | } | ||
| 333 | |||
| 334 | struct arg_tok * | ||
| 335 | _arg_opt_arg (struct arg_tok *tok) | ||
| 336 | { | ||
| 337 | struct arg_tok *arg = tok + 1; | ||
| 338 | if (arg->kind != ARG_TOK_KIND_UNCLAIMED) | ||
| 339 | return NULL; | ||
| 340 | |||
| 341 | arg->consumed = 1; | ||
| 342 | arg->kind = ARG_TOK_KIND_ARGUMENT; | ||
| 343 | return arg; | ||
| 344 | } | ||
| 345 | |||
| 346 | int | ||
| 347 | arg_sub (struct mv name, struct mv usage, | ||
| 348 | struct mv desc, int min_ops, int max_ops) | ||
| 349 | { | ||
| 350 | DBG_ASSERT (!mv_is_empty (name)); | ||
| 351 | DBG_ASSERT_GE (min_ops, 0, "%d", "%d"); | ||
| 352 | DBG_ASSERT_GE (max_ops, -1, "%d", "%d"); | ||
| 353 | // FIXME(cmmm): depends on sizeof (int) == 4 | ||
| 354 | DBG_ASSERT_LE (min_ops, max_ops == -1 ? I32_MAX : max_ops, "%d", "%d"); | ||
| 355 | |||
| 356 | _ARG_HELP (SUB, desc, .sub.name = name); | ||
| 357 | |||
| 358 | const struct mv *key = | ||
| 359 | hm_rel_lookup (&_arg->nm_to_tok, name, &_arg->toks.arr->key); | ||
| 360 | if (!key) | ||
| 361 | return 0; | ||
| 362 | |||
| 363 | struct arg_tok *tok = CONTAINER_OF (key, *tok, key); | ||
| 364 | tok->kind = ARG_TOK_KIND_SUB; | ||
| 365 | tok->consumed = 1; | ||
| 366 | |||
| 367 | _arg->usage = mv_or (usage, MV0 ("")); | ||
| 368 | _arg->min_ops = min_ops; | ||
| 369 | _arg->max_ops = max_ops; | ||
| 370 | |||
| 371 | return 1; | ||
| 372 | } | ||
| 373 | |||
| 374 | int | ||
| 375 | arg_flg (char sh, struct mv ln, struct mv desc) | ||
| 376 | { | ||
| 377 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 378 | |||
| 379 | _ARG_HELP (FLG, desc, | ||
| 380 | .flg.sh = sh, | ||
| 381 | .flg.ln = mv_or (ln, MV_NULL)); | ||
| 382 | |||
| 383 | int count = 0; | ||
| 384 | |||
| 385 | for (int i = 0; i < 2; ++i) { | ||
| 386 | struct arg_tok *tok; | ||
| 387 | switch (i) { | ||
| 388 | case 0: tok = _arg_last_tok_sh (sh); break; | ||
| 389 | case 1: tok = _arg_last_tok_ln (ln); break; | ||
| 390 | } | ||
| 391 | if (!tok) continue; | ||
| 392 | do count += 1; | ||
| 393 | while (tok = _arg_prev_tok (tok)); | ||
| 394 | } | ||
| 395 | |||
| 396 | return count; | ||
| 397 | } | ||
| 398 | |||
| 399 | struct mv | ||
| 400 | arg_req (char sh, struct mv ln, struct mv lbl, struct mv desc, struct mv off) | ||
| 401 | { | ||
| 402 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 403 | _ARG_HELP (REQ, desc, | ||
| 404 | .req.sh = sh, | ||
| 405 | .req.ln = ln, | ||
| 406 | .req.lbl = lbl, | ||
| 407 | .req.off = off); | ||
| 408 | |||
| 409 | struct arg_tok *toks[2] = { | ||
| 410 | _arg_last_tok_sh (sh), | ||
| 411 | _arg_last_tok_ln (ln), | ||
| 412 | }; | ||
| 413 | for (int i = 0; i < 2; ++i) { | ||
| 414 | struct arg_tok *tok = toks[i]; | ||
| 415 | if (!tok) continue; | ||
| 416 | if (_arg_missing_req (tok)) return off; | ||
| 417 | do _arg_req_arg (tok); | ||
| 418 | while (tok = _arg_prev_tok (tok)); | ||
| 419 | } | ||
| 420 | |||
| 421 | struct arg_tok *tok = MAX (toks[0], toks[1]); | ||
| 422 | if (!tok) | ||
| 423 | return off; | ||
| 424 | |||
| 425 | return tok[1].key; | ||
| 426 | } | ||
| 427 | |||
| 428 | struct mv_da | ||
| 429 | arg_req_n (char sh, struct mv ln, struct mv lbl, | ||
| 430 | struct mv desc, struct mv_arr off) | ||
| 431 | { | ||
| 432 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 433 | |||
| 434 | struct mv_da ret = {0}; | ||
| 435 | DBG_DIE ("TODO(cmmm): implement arg_req_n"); | ||
| 436 | return ret; | ||
| 437 | } | ||
| 438 | |||
| 439 | u64 | ||
| 440 | arg_req_u64 (char sh, struct mv ln, struct mv lbl, struct mv desc, u64 off) | ||
| 441 | { | ||
| 442 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 443 | |||
| 444 | DBG_DIE ("TODO(cmmm): implement arg_req_u64"); | ||
| 445 | return off; | ||
| 446 | } | ||
| 447 | |||
| 448 | i64 | ||
| 449 | arg_req_i64 (char sh, struct mv ln, struct mv lbl, struct mv desc, i64 off) | ||
| 450 | { | ||
| 451 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 452 | |||
| 453 | DBG_DIE ("TODO(cmmm): implement arg_req_i64"); | ||
| 454 | return off; | ||
| 455 | } | ||
| 456 | |||
| 457 | f64 | ||
| 458 | arg_req_f64 (char sh, struct mv ln, struct mv lbl, struct mv desc, f64 off) | ||
| 459 | { | ||
| 460 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 461 | |||
| 462 | DBG_DIE ("TODO(cmmm): implement arg_req_f64"); | ||
| 463 | return off; | ||
| 464 | } | ||
| 465 | |||
| 466 | int | ||
| 467 | arg_req_enum (char sh, struct mv ln, struct mv desc, struct mv_arr sel, int off) | ||
| 468 | { | ||
| 469 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 470 | DBG_ASSERT_NZ (sel.len, "%u"); | ||
| 471 | |||
| 472 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 473 | |||
| 474 | _ARG_HELP (REQ_ENUM, desc, | ||
| 475 | .req_enum.sh = sh, | ||
| 476 | .req_enum.ln = ln, | ||
| 477 | .req_enum.sel = sel, | ||
| 478 | .req_enum.off = off); | ||
| 479 | |||
| 480 | struct arg_tok *toks[2] = { | ||
| 481 | _arg_last_tok_sh (sh), | ||
| 482 | _arg_last_tok_ln (ln), | ||
| 483 | }; | ||
| 484 | for (int i = 0; i < 2; ++i) { | ||
| 485 | struct arg_tok *tok = toks[i]; | ||
| 486 | if (!tok) continue; | ||
| 487 | if (_arg_missing_req (tok)) return off; | ||
| 488 | do { | ||
| 489 | struct arg_tok *arg = _arg_req_arg (tok); | ||
| 490 | if (!arg) continue; | ||
| 491 | usz e; | ||
| 492 | for (e = 0; e < sel.len; ++e) { | ||
| 493 | if (mv_equal (arg->key, sel.arr[e])) | ||
| 494 | break; | ||
| 495 | } if (e < sel.len) continue; | ||
| 496 | |||
| 497 | _ARG_ERROR ("Required argument `%.*s' of %s%.*s is not one of: %.*s", | ||
| 498 | MV_FMT (arg->key), | ||
| 499 | tok->kind == ARG_TOK_KIND_SHORT ? "-" : "--", | ||
| 500 | MV_FMT (tok->key), | ||
| 501 | MB_FMT (mv_arr_join (sel, MV0 (", ")))); | ||
| 502 | } while (tok = _arg_prev_tok (tok)); | ||
| 503 | } | ||
| 504 | |||
| 505 | struct arg_tok *tok = MAX (toks[0], toks[1]); | ||
| 506 | if (!tok) | ||
| 507 | return off; | ||
| 508 | |||
| 509 | struct mv arg = tok[1].key; | ||
| 510 | for (usz e = 0; e < sel.len; ++e) { | ||
| 511 | if (mv_equal (arg, sel.arr[e])) | ||
| 512 | return e; | ||
| 513 | } | ||
| 514 | |||
| 515 | return off; | ||
| 516 | } | ||
| 517 | |||
| 518 | struct mv | ||
| 519 | arg_opt (char sh, struct mv ln, struct mv lbl, | ||
| 520 | struct mv desc, struct mv off, struct mv dflt) | ||
| 521 | { | ||
| 522 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 523 | |||
| 524 | _ARG_HELP (OPT, desc, | ||
| 525 | .opt.sh = sh, | ||
| 526 | .opt.ln = ln, | ||
| 527 | .opt.lbl = lbl, | ||
| 528 | .opt.off = off, | ||
| 529 | .opt.dflt = dflt); | ||
| 530 | |||
| 531 | struct arg_tok *toks[2] = { | ||
| 532 | _arg_last_tok_sh (sh), | ||
| 533 | _arg_last_tok_ln (ln), | ||
| 534 | }; | ||
| 535 | for (int i = 0; i < 2; ++i) { | ||
| 536 | struct arg_tok *tok = toks[i]; | ||
| 537 | if (!tok) continue; | ||
| 538 | do _arg_opt_arg (tok); | ||
| 539 | while (tok = _arg_prev_tok (tok)); | ||
| 540 | } | ||
| 541 | |||
| 542 | struct arg_tok *tok = MAX (toks[0], toks[1]); | ||
| 543 | if (!tok) | ||
| 544 | return off; | ||
| 545 | |||
| 546 | if (tok[1].kind != ARG_TOK_KIND_ARGUMENT) | ||
| 547 | return dflt; | ||
| 548 | |||
| 549 | return tok[1].key; | ||
| 550 | } | ||
| 551 | |||
| 552 | struct mv_da | ||
| 553 | arg_opt_n (char sh, struct mv ln, struct mv lbl, | ||
| 554 | struct mv desc, struct mv_arr dflt, struct mv_arr off) | ||
| 555 | { | ||
| 556 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 557 | |||
| 558 | struct mv_da ret = {0}; | ||
| 559 | DBG_DIE ("TODO(cmmm): implement arg_opt_n"); | ||
| 560 | return ret; | ||
| 561 | } | ||
| 562 | |||
| 563 | u64 | ||
| 564 | arg_opt_u64 (char sh, struct mv ln, struct mv lbl, | ||
| 565 | struct mv desc, u64 off, u64 dflt) | ||
| 566 | { | ||
| 567 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 568 | |||
| 569 | _ARG_HELP (OPT_U64, desc, | ||
| 570 | .opt_u64.sh = sh, | ||
| 571 | .opt_u64.ln = ln, | ||
| 572 | .opt_u64.lbl = lbl, | ||
| 573 | .opt_u64.dflt = dflt, | ||
| 574 | .opt_u64.off = off); | ||
| 575 | |||
| 576 | struct arg_tok *toks[2] = { | ||
| 577 | _arg_last_tok_sh (sh), | ||
| 578 | _arg_last_tok_ln (ln), | ||
| 579 | }; | ||
| 580 | for (int i = 0; i < 2; ++i) { | ||
| 581 | struct arg_tok *tok = toks[i]; | ||
| 582 | if (!tok) continue; | ||
| 583 | do { | ||
| 584 | struct arg_tok *arg = _arg_opt_arg (tok); | ||
| 585 | if (!arg) continue; | ||
| 586 | if (mv_to_u64 (arg->key, 10) != U64_MAX) continue; | ||
| 587 | _ARG_ERROR ("Optional unsigned 64-bit argument `%.*s' for %s%.*s is out of range or invalid", | ||
| 588 | arg->key, tok->kind == ARG_TOK_KIND_SHORT ? "-" : "--", | ||
| 589 | tok->key); | ||
| 590 | } while (tok = _arg_prev_tok (tok)); | ||
| 591 | } | ||
| 592 | |||
| 593 | struct arg_tok *tok = MAX (toks[0], toks[1]); | ||
| 594 | if (!tok) | ||
| 595 | return off; | ||
| 596 | |||
| 597 | if (tok[1].kind != ARG_TOK_KIND_ARGUMENT) | ||
| 598 | return dflt; | ||
| 599 | |||
| 600 | u64 arg = mv_to_u64 (tok[1].key, 10); | ||
| 601 | if (arg == U64_MAX) | ||
| 602 | return dflt; | ||
| 603 | |||
| 604 | return arg; | ||
| 605 | } | ||
| 606 | |||
| 607 | i64 | ||
| 608 | arg_opt_i64 (char sh, struct mv ln, struct mv lbl, | ||
| 609 | struct mv desc, i64 off, i64 dflt) | ||
| 610 | { | ||
| 611 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 612 | |||
| 613 | DBG_DIE ("TODO(cmmm): implement arg_opt_i64"); | ||
| 614 | return off; | ||
| 615 | } | ||
| 616 | |||
| 617 | f64 | ||
| 618 | arg_opt_f64 (char sh, struct mv ln, struct mv lbl, | ||
| 619 | struct mv desc, f64 off, f64 dflt) | ||
| 620 | { | ||
| 621 | DBG_ASSERT (sh != '\0' || !mv_is_empty (ln)); | ||
| 622 | |||
| 623 | DBG_DIE ("TODO(cmmm): implement arg_opt_f64"); | ||
| 624 | return off; | ||
| 625 | } | ||
| 626 | |||
| 627 | void | ||
| 628 | arg_help (FILE *strm) | ||
| 629 | { | ||
| 630 | fprintf (strm, "Usage: %.*s %.*s\n", | ||
| 631 | MV_FMT (_arg->prog_name), | ||
| 632 | MV_FMT (_arg->usage)); | ||
| 633 | if (!mv_is_empty (_arg->desc)) | ||
| 634 | fprintf (strm, "%.*s\n", MV_FMT (_arg->desc)); | ||
| 635 | fprintf (strm, "\n"); | ||
| 636 | fprintf (strm, "Options:\n"); | ||
| 637 | |||
| 638 | // measure | ||
| 639 | int desc_col = 0; // TODO(cmmm): cap col | ||
| 640 | for (u32 i = 0; i < DA_LEN (_arg->entries); ++i) { | ||
| 641 | struct arg_entry *entry = DA_AT (_arg->entries, i); | ||
| 642 | int col = 4; | ||
| 643 | // TODO(cmmm): make measurements aware of \0 and MV_NULL | ||
| 644 | switch (entry->kind) { | ||
| 645 | case ARG_ENTRY_KIND_SUB: col += 0 + (mv_sz (entry->sub.name)); break; | ||
| 646 | case ARG_ENTRY_KIND_FLG: col += 6 + (mv_sz (entry->flg.ln)); break; | ||
| 647 | case ARG_ENTRY_KIND_REQ: col += 7 + (mv_sz (entry->req.ln) + | ||
| 648 | mv_sz (entry->req.lbl)); break; | ||
| 649 | case ARG_ENTRY_KIND_OPT: col += 9 + (mv_sz (entry->opt.ln) + | ||
| 650 | mv_sz (entry->opt.lbl)); break; | ||
| 651 | case ARG_ENTRY_KIND_OPT_U64: col += 9 + (mv_sz (entry->opt_u64.ln) + | ||
| 652 | mv_sz (entry->opt_u64.lbl)); break; | ||
| 653 | default: break; | ||
| 654 | } if (col > desc_col) desc_col = col; | ||
| 655 | } | ||
| 656 | |||
| 657 | // aligned output | ||
| 658 | for (u32 i = 0; i < DA_LEN (_arg->entries); ++i) { | ||
| 659 | struct arg_entry *entry = DA_AT (_arg->entries, i); | ||
| 660 | if (entry->kind == ARG_ENTRY_KIND_VERBATIM) { | ||
| 661 | fprintf (strm, "%.*s", MV_FMT (entry->verbatim)); | ||
| 662 | continue; | ||
| 663 | } | ||
| 664 | |||
| 665 | int col = 0; | ||
| 666 | col += fprintf (strm, " "); | ||
| 667 | switch (entry->kind) { | ||
| 668 | case ARG_ENTRY_KIND_SUB: | ||
| 669 | col += fprintf (strm, "%.*s", MV_FMT (entry->sub.name)); | ||
| 670 | break; | ||
| 671 | |||
| 672 | case ARG_ENTRY_KIND_FLG: | ||
| 673 | if (entry->flg.sh) { | ||
| 674 | col += fprintf (strm, "-%c", entry->flg.sh); | ||
| 675 | if (!mv_is_empty (entry->flg.ln)) | ||
| 676 | col += fprintf (strm, ", "); | ||
| 677 | } else col += fprintf (strm, " "); | ||
| 678 | if (!mv_is_empty (entry->flg.ln)) | ||
| 679 | col += fprintf (strm, "--%.*s", MV_FMT (entry->flg.ln)); | ||
| 680 | break; | ||
| 681 | |||
| 682 | case ARG_ENTRY_KIND_REQ: | ||
| 683 | col += fprintf (strm, "-%c, --%.*s=%.*s", entry->req.sh, | ||
| 684 | MV_FMT (entry->req.ln), | ||
| 685 | MV_FMT (entry->req.lbl)); | ||
| 686 | break; | ||
| 687 | |||
| 688 | case ARG_ENTRY_KIND_REQ_ENUM: | ||
| 689 | col += fprintf (strm, "-%c, --%.*s={%.*s}", entry->req_enum.sh, | ||
| 690 | MV_FMT (entry->req_enum.ln), | ||
| 691 | MB_FMT (mv_arr_join (entry->req_enum.sel, MV0 ("|")))); | ||
| 692 | break; | ||
| 693 | |||
| 694 | case ARG_ENTRY_KIND_OPT: | ||
| 695 | col += fprintf (strm, "-%c, --%.*s[=%.*s]", entry->opt.sh, | ||
| 696 | MV_FMT (entry->opt.ln), | ||
| 697 | MV_FMT (entry->opt.lbl)); | ||
| 698 | break; | ||
| 699 | |||
| 700 | case ARG_ENTRY_KIND_OPT_U64: | ||
| 701 | col += fprintf (strm, "-%c, --%.*s[=%.*s]", entry->opt_u64.sh, | ||
| 702 | MV_FMT (entry->opt_u64.ln), | ||
| 703 | MV_FMT (entry->opt_u64.lbl)); | ||
| 704 | break; | ||
| 705 | |||
| 706 | default: DBG_UNREACHABLE_MSG ("entry->kind: %d", entry->kind); | ||
| 707 | } | ||
| 708 | |||
| 709 | if (col > desc_col) | ||
| 710 | col = fprintf (strm, "\n%*s", desc_col, "") - 1; | ||
| 711 | // TODO(cmmm): wrap if too long. | ||
| 712 | fprintf (strm, "%*s%.*s", desc_col - col, "", MV_FMT (entry->desc)); | ||
| 713 | |||
| 714 | switch (entry->kind) { | ||
| 715 | case ARG_ENTRY_KIND_REQ: | ||
| 716 | col += fprintf (strm, " (off: %.*s)", MV_FMT (entry->req.off)); | ||
| 717 | break; | ||
| 718 | |||
| 719 | case ARG_ENTRY_KIND_REQ_ENUM: | ||
| 720 | col += fprintf (strm, " (off: %.*s)", | ||
| 721 | MV_FMT (entry->req_enum.sel.arr[entry->req_enum.off])); | ||
| 722 | break; | ||
| 723 | |||
| 724 | case ARG_ENTRY_KIND_OPT: | ||
| 725 | col += fprintf (strm, " ("); | ||
| 726 | if (!mv_is_empty (entry->opt.off)) | ||
| 727 | col += fprintf (strm, "off: %.*s, ", MV_FMT (entry->opt.off)); | ||
| 728 | col += fprintf (strm, "dflt: %.*s)", MV_FMT (entry->opt.dflt)); | ||
| 729 | break; | ||
| 730 | |||
| 731 | case ARG_ENTRY_KIND_OPT_U64: | ||
| 732 | col += fprintf (strm, " (off: %lu, dflt: %lu)", | ||
| 733 | entry->opt_u64.off, | ||
| 734 | entry->opt_u64.dflt); | ||
| 735 | break; | ||
| 736 | |||
| 737 | default: DBG_UNREACHABLE_MSG ("entry->kind: %d", entry->kind); | ||
| 738 | case ARG_ENTRY_KIND_FLG: | ||
| 739 | case ARG_ENTRY_KIND_SUB: | ||
| 740 | break; | ||
| 741 | } fprintf (strm, "\n"); | ||
| 742 | } | ||
| 743 | } | ||
| 744 | |||
| 745 | void | ||
| 746 | arg_help_verbatim (struct mv mv) | ||
| 747 | { | ||
| 748 | DBG_ASSERT (!mv_is_empty (mv)); | ||
| 749 | |||
| 750 | DA_PUSH_STRUCT (&_arg->entries, | ||
| 751 | .kind = ARG_ENTRY_KIND_VERBATIM, | ||
| 752 | .verbatim = mv); | ||
| 753 | } | ||
| 754 | |||
| 755 | struct mv_da | ||
| 756 | arg_end (void) | ||
| 757 | { | ||
| 758 | if (DA_LEN (_arg->errors)) { | ||
| 759 | arg_help (stderr); | ||
| 760 | fprintf (stderr, "\nErrors:\n%.*s", DA_FMT (_arg->errors)); | ||
| 761 | exit (1); | ||
| 762 | } | ||
| 763 | |||
| 764 | if (_arg->help_requested) { | ||
| 765 | arg_help (stdout); | ||
| 766 | exit (0); | ||
| 767 | } | ||
| 768 | |||
| 769 | struct mv_da opers = { .hdr.gpa = GPA_SYS }; | ||
| 770 | for (struct arg_tok *tok = _arg->toks.arr; tok->kind; ++tok) { | ||
| 771 | if (tok->kind == ARG_TOK_KIND_UNCLAIMED) { | ||
| 772 | tok->kind = ARG_TOK_KIND_OPERAND; | ||
| 773 | tok->consumed = 1; | ||
| 774 | } | ||
| 775 | |||
| 776 | if (tok->kind == ARG_TOK_KIND_OPERAND) | ||
| 777 | DA_PUSH (&opers, tok->key); | ||
| 778 | |||
| 779 | if (!tok->consumed) | ||
| 780 | DBG_DIE ("uncosumed token: %.*s", MV_FMT (tok->key)); | ||
| 781 | } | ||
| 782 | |||
| 783 | #if 0 | ||
| 784 | for (u32 i = 0; i < DA_LEN (arg->toks); ++i) { | ||
| 785 | const char *kind_to_cstr[] = { | ||
| 786 | [ARG_TOK_KIND_UNCLAIMED] = "UNCLAIMED", | ||
| 787 | [ARG_TOK_KIND_ARG] = "ARG", | ||
| 788 | [ARG_TOK_KIND_OPERAND] = "OPERAND", | ||
| 789 | [ARG_TOK_KIND_NAME] = "NAME", | ||
| 790 | [ARG_TOK_KIND_SHORT] = "SHORT", | ||
| 791 | [ARG_TOK_KIND_SHORT_WITH_POSSIBLE_ARG] = "SHORT_WITH_POSSIBLE_ARG", | ||
| 792 | [ARG_TOK_KIND_LONG] = "LONG", | ||
| 793 | [ARG_TOK_KIND_LONG_WITH_ARG] = "LONG_WITH_ARG", | ||
| 794 | }; | ||
| 795 | strct arg_tok *tok = DA_AT (arg->toks, i); | ||
| 796 | INFO ("idx: %u, key: %10.*s, val: %10.*s, argv_idx: %2d, prev_idx: %2d, consumed: %d, kind: %s", | ||
| 797 | i, | ||
| 798 | MV_FMT (tok->key), | ||
| 799 | MV_FMT (tok->val), | ||
| 800 | tok->argv_idx, | ||
| 801 | tok->prev_idx, | ||
| 802 | tok->consumed, | ||
| 803 | kind_to_cstr[tok->kind]); | ||
| 804 | } | ||
| 805 | #endif | ||
| 806 | |||
| 807 | DA_DROP (&_arg->toks); | ||
| 808 | DA_DROP (&_arg->entries); | ||
| 809 | hm_drop (&_arg->ln_to_tok); | ||
| 810 | hm_drop (&_arg->nm_to_tok); | ||
| 811 | free (_arg); | ||
| 812 | |||
| 813 | return opers; | ||
| 814 | } | ||
| 815 | |||
| 816 | // arg.c ends here | ||
