Improved parsing of flags in main program.
h3rald h3rald@h3rald.com
Wed, 07 Jan 2026 14:32:18 +0100
1 files changed,
42 insertions(+),
25 deletions(-)
jump to
M
xyw.c
→
xyw.c
@@ -15,50 +15,67 @@ "AND", "IOR", "XOR", "NOT",
"SWP", "DUP", "OVR", "ROT", "JMP", "JCN", "JSR", "RTS"}; +int flag(const char *arg, const char *str) +{ + if (!str || !arg || !*str) + return 0; + if (strncmp(arg, "--", 2) == 0 && strcmp(arg + 2, str) == 0) + return 1; + if (arg[0] == '-' && arg[1] != '\0' && arg[2] == '\0' && arg[1] == str[0]) + return 1; + return 0; +} int main(int argc, char *argv[]) { (void)argc; + for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--debug") == 0) + if (flag(argv[i], "debug")) { xyw_debug = 1; } - else + } + + for (int i = 1; i < argc; i++) + { + if (flag(argv[i], "debug")) { - const char *input_file = argv[i]; - char output_file[256]; - snprintf(output_file, sizeof(output_file), "%.*s.xim", (int)(strlen(input_file) - 4), input_file); - const char *ext = strrchr(input_file, '.'); - if (ext && strcmp(ext, ".xyw") == 0) + continue; // already handled + } + + const char *input_file = argv[i]; + char output_file[256]; + snprintf(output_file, sizeof(output_file), "%.*s.xim", (int)(strlen(input_file) - 4), input_file); + const char *ext = strrchr(input_file, '.'); + if (ext && strcmp(ext, ".xyw") == 0) + { + if (xyw_assemble(input_file, output_file) != 0) { - if (xyw_assemble(input_file, output_file) != 0) - { - fprintf(stderr, "Assembly failed for file: %s\n", input_file); - return -1; - } - else - { - if (xyw_debug) - { - printf("Assembled %s to %s\n", input_file, output_file); - } - } + fprintf(stderr, "Assembly failed for file: %s\n", input_file); + return -1; } - else if (ext && strcmp(ext, ".xim") == 0) + else { - if (xyw_run(input_file) != 0) + if (xyw_debug) { - fprintf(stderr, "Execution failed for file: %s\n", input_file); - return -1; + printf("Assembled %s to %s\n", input_file, output_file); } } - else + } + else if (ext && strcmp(ext, ".xim") == 0) + { + if (xyw_run(input_file) != 0) { - fprintf(stderr, "Unsupported file type: %s\n", input_file); + fprintf(stderr, "Execution failed for file: %s\n", input_file); return -1; } + } + else + { + fprintf(stderr, "Unsupported file type: %s\n", input_file); + return -1; } } }