Mercurial > hg > index.cgi
diff lwcc/cpp/main.c @ 292:40ecbd5da481 ccdev
Part one of the C preprocessor
This is part one of the C preprocessor. It finds and then fails to intepret
directives. Also handles line splicing and trigraphs.
author | William Astle <lost@l-w.ca> |
---|---|
date | Sun, 08 Sep 2013 21:58:12 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwcc/cpp/main.c Sun Sep 08 21:58:12 2013 -0600 @@ -0,0 +1,134 @@ +/* +lwcc/cpp/main.c + +Copyright © 2013 William Astle + +This file is part of LWTOOLS. + +LWTOOLS is free software: you can redistribute it and/or modify it under the +terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <lw_stringlist.h> +#include <lw_cmdline.h> + +#include "cpp.h" + +/* command line option handling */ +#define PROGVER "lwcc-cpp from " PACKAGE_STRING +char *program_name; + +/* input files */ +lw_stringlist_t input_files; + +/* various flags */ +int trigraphs = 0; +char *output_file = NULL; +FILE *output_fp = NULL; + +static struct lw_cmdline_options options[] = +{ + { "output", 'o', "FILE", 0, "Output to FILE"}, + { "include", 'i', "FILE", 0, "Pre-include FILE" }, + { "includedir", 'I', "PATH", 0, "Add entry to the user include path" }, + { "sincludedir", 'S', "PATH", 0, "Add entry to the system include path" }, + { "define", 'D', "SYM[=VAL]",0, "Automatically define SYM to be VAL (or 1)"}, + { "trigraphs", 0x100, NULL, 0, "Enable interpretation of trigraphs" }, + { 0 } +}; + + +static int parse_opts(int key, char *arg, void *state) +{ + switch (key) + { + case 'o': + if (output_file) + do_error("Output file specified more than once."); + output_file = arg; + break; + + case 0x100: + trigraphs = 1; + break; + + case lw_cmdline_key_end: + break; + + case lw_cmdline_key_arg: + lw_stringlist_addstring(input_files, arg); + break; + + default: + return lw_cmdline_err_unknown; + } + return 0; +} + +static struct lw_cmdline_parser cmdline_parser = +{ + options, + parse_opts, + "INPUTFILE", + "lwcc-cpp - C preprocessor for lwcc", + PROGVER +}; + +int main(int argc, char **argv) +{ + program_name = argv[0]; + int retval = 0; + + input_files = lw_stringlist_create(); + + /* parse command line arguments */ + lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, NULL); + + /* set up output file */ + if (output_file == NULL || strcmp(output_file, "-") == 0) + { + output_fp = stdout; + } + else + { + output_fp = fopen(output_file, "wb"); + if (output_fp == NULL) + { + do_error("Failed to create output file %s: %s", output_file, strerror(errno)); + } + } + + if (lw_stringlist_nstrings(input_files) == 0) + { + /* if no input files, work on stdin */ + retval = process_file("-"); + } + else + { + char *s; + lw_stringlist_reset(input_files); + for (s = lw_stringlist_current(input_files); s; s = lw_stringlist_next(input_files)) + { + retval = process_file(s); + if (retval != 0) + break; + } + } + lw_stringlist_destroy(input_files); + exit(retval); +}