aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..3731eec
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,38 @@
+#define _POSIX_C_SOURCE 200809L
+#include "aup.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int usage(void) {
+ fputs("aup check FILE\naup build FILE -o OUT.pmod\n", stderr);
+ return 2;
+}
+
+int main(int argc, char **argv) {
+ bool build = argc == 5 && !strcmp(argv[1], "build") && !strcmp(argv[3], "-o");
+ if (!build && !(argc == 3 && !strcmp(argv[1], "check"))) return usage();
+ const char *path = argv[2];
+ FILE *file = fopen(path, "rb");
+ if (!file) { perror(path); return 2; }
+ static char source[AUP_SOURCE_LIMIT + 1];
+ size_t length = fread(source, 1, sizeof(source), file);
+ bool unreadable = ferror(file);
+ fclose(file);
+ if (unreadable) { fprintf(stderr, "%s: cannot read\n", path); return 2; }
+ unsigned char *module;
+ size_t size;
+ AupDiagnostic error;
+ if (!aup_compile(source, length, &module, &size, &error)) {
+ fprintf(stderr, "%s:%d:%d: %s\n", path, error.line, error.column, error.message);
+ return 1;
+ }
+ int status = 0;
+ if (build) {
+ FILE *out = fopen(argv[4], "wb");
+ if (!out || fwrite(module, 1, size, out) != size) { perror(argv[4]); status = 2; }
+ if (out && fclose(out)) { perror(argv[4]); status = 2; }
+ }
+ free(module);
+ return status;
+}