summaryrefslogtreecommitdiff
path: root/tools/kons2c.c
blob: 2a53520f659ab26eb99bab2be8752aa1eb7708ef (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
95
96
97
98
/* 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] <script.kons> <output prefix>
 * konscript text to C, for scripts written by hand. the converters write C themselves through konscriptc.h
 */

#define KONSCRIPTC_IMPLEMENTATION
#include "konscriptc.h"

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

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] <script.kons> <output prefix>\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;
}