From 2253da61e9b3dd5408bed182ea08e5270156c17e Mon Sep 17 00:00:00 2001 From: Kimplul Date: Tue, 3 Dec 2024 22:04:38 +0200 Subject: initial commit + Lots of code copied from ek, so didn't have to start from scratch, but might mean there are some quirks here and there that made sense in ek but not necessarily here. --- src/main.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/main.c (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..204a920 --- /dev/null +++ b/src/main.c @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ +/* Copyright 2024 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ + +/** + * @file main.c + * + * Compiler main file, controls compilation and command line + * handling. + */ + +#include +#include +#include + +#include +#include + +/** + * String describing compiler usage. + * @todo I suspect backends might want more flags, come up with + * some way to make flag handling more generic + */ +static const char *cmdline_usage = + "fwd frontend usage:\n" + " fwd infile\n" + " -h Show usage (this)\n" + " infile Top file(s) to compile\n" +; + +/** Print usage of compiler. */ +static void usage() +{ + fputs(cmdline_usage, stderr); +} + +/** + * Main entry to compiler. + * Checks command line parameters and drives the rest of the compiler. + * Feels kind of weird documenting main, but doxygen warns about not + * doing it so whatever. + * + * @param argc Number of command line arguments. + * @param argv Array of command line arguments. + * @return \c 0 when succesful, non-zero otherwise. + */ +int main(int argc, char *argv[]) +{ + int opt; + while ((opt = getopt(argc, argv, "hI:")) != -1) { + switch (opt) { + case 'h': + usage(); + exit(EXIT_SUCCESS); + + default: + usage(); + exit(EXIT_FAILURE); + } + } + + if (optind >= argc) { + error("no input file"); + usage(); + exit(EXIT_FAILURE); + } + + if (optind != argc - 1) { + error("too many arguments"); + usage(); + exit(EXIT_FAILURE); + } + + const char *input = argv[optind]; + return compile(input); +} -- cgit v1.2.3