// Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License. // See LICENSE.md for license information. // SPDX-License-Identifier: MIT // // gateArg.h - command lines for the gate tools: numbers, pairs of numbers, // and ports, each read whole (one that is not is the tool's usage error); // tools of several modes; and usage text. #ifndef GATE_ARG_H #define GATE_ARG_H #include #include #include // What a gate tool exits with when its command line is wrong. #define GATE_USAGE_EXIT 2 // How many entries an array has. #define GATE_COUNT_OF(a) (sizeof(a) / sizeof((a)[0])) // A tool's mode: its name, how many arguments follow the name, and what it // does with them (returning the tool's exit code). typedef int (*GateModeFnT)(char **args); typedef struct GateModeT { const char *name; int argCount; GateModeFnT run; } GateModeT; // Print ": bad NAME: TEXT" to stderr and exit GATE_USAGE_EXIT. void gateArgBad(const char *name, const char *text) __attribute__((noreturn)); // A finite number, all of text (strtod's form); anything else is // gateArgBad. double gateArgDouble(const char *name, const char *text); // A whole number, all of text, decimal, in a long's range; anything else is // gateArgBad. long gateArgLong(const char *name, const char *text); // Two whole numbers, as gateArgLong reads them, separated by separator // ("X,Y" or "WxH"); anything else is gateArgBad. void gateArgPair(const char *name, const char *text, char separator, long *first, long *second); // Whether text is all one finite number, or one whole number as // gateArgLong takes them (no white space round it); its value in out. bool gateArgParseDouble(const char *text, double *out); bool gateArgParseLong(const char *text, long *out); // A TCP port: a whole number from 0 to 65535; anything else is gateArgBad. uint16_t gateArgPort(const char *text); // Run the mode argv[1] names, given the arguments after it; false (and // nothing run) when no mode has that name and that many arguments. bool gateModeRun(const GateModeT *modes, size_t count, int argc, char **argv, int *exitCode); // Print a tool's usage, a line a string, to stderr; returns // GATE_USAGE_EXIT. int gateUsage(const char *const *lines, size_t count); #endif