diff options
Diffstat (limited to 'src/build/mkgamedata.c')
-rw-r--r-- | src/build/mkgamedata.c | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/build/mkgamedata.c b/src/build/mkgamedata.c index b05f1e5..325cda2 100644 --- a/src/build/mkgamedata.c +++ b/src/build/mkgamedata.c @@ -16,7 +16,6 @@ #include <stdio.h> #include <stdlib.h> -#include <string.h> #include "../intdefs.h" #include "../langext.h" @@ -28,7 +27,7 @@ #define fS "s" #endif -static noreturn die(int status, const char *s) { +static cold noreturn die(int status, const char *s) { fprintf(stderr, "mkentprops: fatal: %s\n", s); exit(status); } @@ -37,7 +36,7 @@ static noreturn die(int status, const char *s) { static char *sbase = 0; static const os_char *const *srcnames; -static noreturn dieparse(int file, int line, const char *s) { +static cold noreturn dieparse(int file, int line, const char *s) { fprintf(stderr, "mkentprops: %" fS ":%d: %s\n", srcnames[file], line, s); exit(2); } @@ -92,7 +91,9 @@ static inline void handleentry(char *k, char *v, int indent, */ static void parse(int file, char *s, int len) { - if (s[len - 1] != '\n') dieparse(file, 0, "invalid text file (missing EOL)"); + if_cold (s[len - 1] != '\n') { + dieparse(file, 0, "invalid text file (missing EOL)"); + } enum { BOL = 0, KEY = 4, KWS = 8, VAL = 12, COM = 16, ERR = -1 }; static const s8 statetrans[] = { // layout: any, space|tab, #, \n @@ -147,11 +148,14 @@ static void parse(int file, char *s, int len) { } } -static inline noreturn diewrite() { die(100, "couldn't write to file"); } -#define _(x) if (fprintf(out, "%s\n", x) < 0) diewrite(); -#define _i(x) for (int i = 0; i < indent; ++i) fputc('\t', out); _(x) -#define F(f, ...) if (fprintf(out, f "\n", __VA_ARGS__) < 0) diewrite(); -#define Fi(...) for (int i = 0; i < indent; ++i) fputc('\t', out); F(__VA_ARGS__) +static cold noreturn diewrite() { die(100, "couldn't write to file"); } +#define _(x) if_cold (fprintf(out, "%s\n", x) < 0) diewrite(); +#define _doindent() \ + for (int i = 0; i < indent; ++i) \ + if_cold (fputc('\t', out) == EOF) diewrite(); +#define _i(x) _doindent(); _(x) +#define F(f, ...) if_cold (fprintf(out, f "\n", __VA_ARGS__) < 0) diewrite(); +#define Fi(...) _doindent(); F(__VA_ARGS__) #define H() \ _( "/* This file is autogenerated by src/build/mkgamedata.c. DO NOT EDIT! */") \ _( "") @@ -168,7 +172,7 @@ static inline void knowngames(FILE *out) { for (++i; indents[i] != 0; ++i) if (i == nents - 1) return; } F( "#line %d \"%" fS "\"", srclines[i], srcnames[srcfiles[i]]) - if (fprintf(out, "#define _GAMES_WITH_%s (", sbase + tags[i]) < 0) { + if_cold (fprintf(out, "#define _GAMES_WITH_%s (", sbase + tags[i]) < 0) { diewrite(); } const char *pipe = ""; @@ -176,7 +180,7 @@ F( "#line %d \"%" fS "\"", srclines[i], srcnames[srcfiles[i]]) // don't attempt to optimise for nested conditionals because that's // way more complicated and also basically defeats the purpose. if (indents[j] != 1) continue; - if (fprintf(out, "%s \\\n\t _gametype_tag_%s", pipe, + if_cold (fprintf(out, "%s \\\n\t _gametype_tag_%s", pipe, sbase + tags[j]) < 0) { diewrite(); } @@ -265,37 +269,37 @@ int OS_MAIN(int argc, os_char *argv[]) { srcnames = (const os_char *const *)argv; int sbase_len = 0, sbase_max = 65536; sbase = malloc(sbase_max); - if (!sbase) die(100, "couldn't allocate memory"); + if_cold (!sbase) die(100, "couldn't allocate memory"); int n = 1; for (++argv; *argv; ++argv, ++n) { int f = os_open_read(*argv); - if (f == -1) die(100, "couldn't open file"); + if_cold (f == -1) die(100, "couldn't open file"); vlong len = os_fsize(f); - if (sbase_len + len > 1u << 29) { + if_cold (sbase_len + len > 1u << 29) { die(2, "combined input files are far too large"); } - if (sbase_len + len > sbase_max) { + if_cold (sbase_len + len > sbase_max) { fprintf(stderr, "mkgamedata: warning: need to resize string. " "increase sbase_max to avoid this extra work!\n"); sbase_max *= 4; sbase = realloc(sbase, sbase_max); - if (!sbase) die(100, "couldn't grow memory allocation"); + if_cold (!sbase) die(100, "couldn't grow memory allocation"); } char *s = sbase + sbase_len; - if (os_read(f, s, len) != len) die(100, "couldn't read file"); + if_cold (os_read(f, s, len) != len) die(100, "couldn't read file"); os_close(f); parse(n, s, len); sbase_len += len; } FILE *out = fopen(".build/include/gamedata.gen.h", "wb"); - if (!out) die(100, "couldn't open gamedata.gen.h"); + if_cold (!out) die(100, "couldn't open gamedata.gen.h"); H(); knowngames(out); decls(out); out = fopen(".build/include/gamedatainit.gen.h", "wb"); - if (!out) die(100, "couldn't open gamedatainit.gen.h"); + if_cold (!out) die(100, "couldn't open gamedatainit.gen.h"); H(); defs(out); _("") |