summaryrefslogtreecommitdiffhomepage
path: root/src/build/codegen.c
blob: 38645e582dcca2de3e64aa70258ac641357e6406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright © 2021 Michael Smith <mikesmiffy128@gmail.com>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../os.h"
#include "cmeta.h"

#define MAXENT 65536 // arbitrary limit!
static struct ent {
	const char *name;
	bool unreg;
	bool isvar; // false for cmd
} ents[MAXENT];
static int nents;

static void die(const char *s) {
	fprintf(stderr, "codegen: %s\n", s);
	exit(100);
}

#define PUT(name_, isvar_, unreg_) do { \
	if (nents == sizeof(ents) / sizeof(*ents)) { \
		fprintf(stderr, "codegen: out of space; make ents bigger!\n"); \
		exit(1); \
	} \
	ents[nents].name = name_; \
	ents[nents].isvar = isvar_; ents[nents++].unreg = unreg_; \
} while (0)

static void oncondef(const char *name, bool isvar, bool unreg) {
	PUT(name, isvar, unreg);
}

#define _(x) \
	if (fprintf(out, "%s\n", x) < 0) die("couldn't write to file");
#define F(f, ...) \
	if (fprintf(out, f "\n", __VA_ARGS__) < 0) die("couldn't write to file");
#define H() \
_( "/* This file is autogenerated by src/build/codegen.c. DO NOT EDIT! */") \
_( "")

int OS_MAIN(int argc, os_char *argv[]) {
	for (++argv; *argv; ++argv) {
		const struct cmeta *cm = cmeta_loadfile(*argv);
		cmeta_conmacros(cm, &oncondef);
	}

	FILE *out = fopen(".build/include/cmdinit.gen.h", "wb");
	if (!out) die("couldn't open cmdinit.gen.h");
	H();
	for (const struct ent *p = ents; p - ents < nents; ++p) {
F( "extern struct con_%s *%s;", p->isvar ? "var" : "cmd", p->name)
	}
_( "")
_( "static void regcmds(void) {")
	for (const struct ent *p = ents; p - ents < nents; ++p) {
		if (p->isvar) {
F( "	initval(%s);", p->name)
		}
		if (!p->unreg) {
F( "	con_reg(%s);", p->name)
		}
	}
_( "}")
_( "")
_( "static void freevars(void) {")
	for (const struct ent *p = ents; p - ents < nents; ++p) {
		if (p->isvar) {
F( "	extfree(%s->strval);", p->name);
		}
	}
_( "}")
	if (fflush(out) == EOF) die("couldn't fully write cmdinit.gen.h");

	return 0;
}

// vi: sw=4 ts=4 noet tw=80 cc=80