aboutsummaryrefslogtreecommitdiff
path: root/src/prc1/prc1.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2026-09-26 23:30:40 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2026-09-27 23:57:29 +0200
commit204f6f332318056a9b950e71e4a958da0c10ea72 (patch)
tree5d7938d9ed44dbe9455d2a88b2ef93a4beef4397 /src/prc1/prc1.c
parent88d1bca43a8412b285ea2dd54dfa98d08ecc7849 (diff)
downloadprc-204f6f332318056a9b950e71e4a958da0c10ea72.tar.gz
prc1: Rework arg handling + dbg
Diffstat (limited to 'src/prc1/prc1.c')
-rw-r--r--src/prc1/prc1.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/src/prc1/prc1.c b/src/prc1/prc1.c
index 6378907..2822cc5 100644
--- a/src/prc1/prc1.c
+++ b/src/prc1/prc1.c
@@ -1,3 +1,4 @@
+#include "arg.h"
#include "cgen.h"
#include "lex.h"
#include "parse.h"
@@ -8,34 +9,40 @@
int main(int argc, char *argv[])
{
+ const char *fname = "<standard input>";
+ struct args args = {.name = *argv};
struct lex l = {0};
struct ast a = {0};
struct cgen c = {0};
FILE *f = stdin;
- int opened = 0, ret = EXIT_FAILURE;
+ int ret = EXIT_FAILURE;
- for (int i = 1; i < argc; i++)
- fprintf(stderr, "[%d]=%s\n", i, argv[i]);
-
- if (argc < 2)
+ if (argc == 1)
{
fprintf(stderr, "%s [options ...] file\n", *argv);
goto end;
}
- else if (strcmp(argv[1], "-"))
+
+ arg_defaults(&args);
+
+ for (int i = 1; i < argc; i++)
+ if (arg(&args, argv[i]))
+ goto end;
+
+ if (args.src)
{
- if (!(f = fopen(argv[1], "rb")))
+ if (!(f = fopen(args.src, "rb")))
{
- fprintf(stderr, "%s: failed to open %s: %s\n", *argv, argv[1],
+ fprintf(stderr, "%s: fopen(3) %s: %s\n", *argv, args.src,
strerror(errno));
goto end;
}
- opened = 1;
- l.loc.f = argv[1];
+ fname = args.src;
}
- else
- l.loc.f = "stdin";
+
+ l.loc.f = fname;
+ l.args = a.args = &args;
if (lex(&l, f) || parse(&l, &a) || cgen(&a, &c))
goto end;
@@ -43,7 +50,7 @@ int main(int argc, char *argv[])
ret = EXIT_SUCCESS;
end:
- if (opened && fclose(f))
+ if (f && f != stdin && fclose(f))
{
perror("fclose(3)");
ret = EXIT_FAILURE;
@@ -52,5 +59,6 @@ end:
cgen_free(&c);
ast_free(&a);
lex_free(&l);
+ arg_free(&args);
return ret;
}