aboutsummaryrefslogtreecommitdiffhomepage
path: root/main.c
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2023-05-26 09:20:58 +0200
committerlemon <lsof@mailbox.org>2023-05-26 09:20:58 +0200
commit640a3dac2b18d037169af15dfd5502c386c7e828 (patch)
tree79e7ee3fa81e73855ce1bc78d7c4bf1ad3ac8f0d /main.c
parent9100ed2b5dd01df8e6b766c7bc2a12c0dd44f1ff (diff)
hm
Diffstat (limited to 'main.c')
-rw-r--r--main.c38
1 files changed, 36 insertions, 2 deletions
diff --git a/main.c b/main.c
index 6dcd6ea..edea3de 100644
--- a/main.c
+++ b/main.c
@@ -1,3 +1,4 @@
+#include "common.h"
#include "parse.h"
#include <stdlib.h>
@@ -10,18 +11,51 @@ flushstd(void)
ioflush(&bstderr);
}
+static const char *
+optval(const char *arg, const char *opt)
+{
+ /* arg="foo=bar123"; opt="foo"; returns "bar123" */
+ uint n1 = strlen(arg), n2 = strlen(opt);
+ if (n1 < n2+1 || memcmp(arg, opt, n2) != 0 || arg[n2] != '=')
+ return NULL;
+ return arg + n2 + 1;
+}
+
+static void
+optparse(const char **file, const char **targ, char **args)
+{
+ const char *arg, *x;
+ *file = *targ = NULL;
+ while ((arg = *++args)) {
+ if (*arg++ != '-') {
+ *file = arg-1;
+ continue;
+ }
+ if ((x = optval(arg, "std"))) {
+ if (!strcmp(x, "c89")) ccopt.cstd = STDC89;
+ else if (!strcmp(x, "c99")) ccopt.cstd = STDC99;
+ else if (!strcmp(x, "c11")) ccopt.cstd = STDC11;
+ else if (!strcmp(x, "c2x")) ccopt.cstd = STDC23;
+ else if (!strcmp(x, "c23")) ccopt.cstd = STDC23;
+ else goto Bad;
+ } else Bad: warn(NULL, "invalid option: %'s", arg-1);
+ }
+}
+
int
main(int argc, char **argv)
{
struct parser pr;
+ const char *file, *targ;
atexit(flushstd);
if (argc < 2) {
efmt("usage: %s [options] <file>\n", *argv);
return 1;
}
+ optparse(&file, &targ, argv);
- targ_init("amd64-sysv");
- initparser(&pr, argv[1]);
+ targ_init(targ ? targ : "amd64-sysv");
+ initparser(&pr, file);
parse(&pr);
}