/* SPDX-License-Identifier: MIT */ /* * kons2c.c * * Copyright (c) 2026 KonAki * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /* * usage: kons2c [-n name] [-split rows] * konscript text to C, for scripts written by hand. the converters write C themselves through konscriptc.h */ #define KONSCRIPTC_IMPLEMENTATION #include "konscriptc.h" #include #include #include static char *readFile(const char *path, size_t *size) { FILE *f = fopen(path, "rb"); long n; char *text; if (!f) return NULL; if (fseek(f, 0, SEEK_END) != 0 || (n = ftell(f)) < 0 || fseek(f, 0, SEEK_SET) != 0) { fclose(f); return NULL; } text = malloc((size_t)n + 1); if (!text) { fclose(f); return NULL; } *size = fread(text, 1, (size_t)n, f); fclose(f); text[*size] = '\0'; return text; } int main(int argc, char **argv) { char name[64] = "", error[200]; const char *input = NULL, *prefix = NULL; int split = 0, i; size_t size; char *source; konsc_result_t result; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-n") && i + 1 < argc) { snprintf(name, sizeof name, "%s", argv[++i]); } else if (!strcmp(argv[i], "-split") && i + 1 < argc) { split = atoi(argv[++i]); } else if (!input) { input = argv[i]; } else if (!prefix) { prefix = argv[i]; } } if (!input || !prefix) { fprintf(stderr, "usage: kons2c [-n name] [-split rows] \n"); return 2; } if (!name[0]) konsc_name(name, sizeof name, prefix); source = readFile(input, &size); if (!source) { fprintf(stderr, "cannot read %s\n", input); return 1; } if (konsc_write(source, size, prefix, name, split, &result, error, sizeof error) != 0) { fprintf(stderr, "%s: %s\n", input, error); free(source); return 1; } printf("%s: %d commands, %d labels%s\n", name, result.commands, result.labels, split ? ", split" : ""); free(source); return 0; }