/* ibicc - The Ibi C Compiler Copyright (C) 2004 Antti-Juhani Kaijanaho Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #define _XOPEN_SOURCE 600 #include "diag.h" #include "ibicpp.h" #include "paths.h" #include #include #include static int print_defs = 0; static void help(char *prog) { fprintf(stderr, "Usage: %s {-Dname|-Dname=value|-Idir|-I-|-Uname|-undef} input output\n", prog); } struct symdef { char *sym; char *def; struct symdef *next; }; static void preprocess(char const *inf, char const *outf, struct symdef * sdefs) { struct symdef *old_sdef = 0; ibicpp_source_t is = ibicpp_open_source(inf); struct ibicpp_token buf[2048]; size_t toks, i; FILE *fp = fopen(outf, "w"); if (is == 0) exit(1); if (fp == 0) { fprintf(stderr, "cannot open output file\n"); exit(1); } ibicpp_append_system_include_dir(is, "/usr/local/include"); ibicpp_append_system_include_dir(is, "/usr/include"); ibicpp_append_system_include_dir(is, OWN_INCLUDE_DIR); for (; sdefs != 0; old_sdef = sdefs, sdefs = old_sdef->next) { free(old_sdef); ibicpp_define_symbol(is, sdefs->sym, sdefs->def, ""); free(sdefs->sym); } for (i = 0; i < sizeof buf / sizeof *buf; i++) { buf[i].semval = empty_substr; } while (1) { toks = ibicpp_get_tokens(is, buf, 0, sizeof buf / sizeof *buf); if (toks == 0) break; ibicpp_token_print(fp, buf, 0, toks); } if (print_defs) ibicpp_dump_macros(stdout, is); ibicpp_close_source(is); fclose(fp); } int main(const int argc, char **argv) { int argi = 1; int error = 0; char *inf = 0, *outf = 0; struct symdef *sdefs = 0; while (argi < argc && argv[argi][0] == '-') { char *optarg = 0; switch (argv[argi][1]) { case 'd': if (strcmp(argv[argi], "-defs") != 0) goto unrecognized_option; print_defs = 1; break; case 'D': { struct symdef *sd = malloc(sizeof *sd); if (sd == 0) enomem(); if (argv[argi][2] == 0) { optarg = argv[++argi]; } else { optarg = argv[argi] + 1; } sd->sym = strdup(optarg); if (sd->sym == 0) enomem(); sd->def = strchr(sd->sym, '='); if (sd->def == 0) { sd->def = "1"; } else { if (sd->def == sd->sym) { fprintf(stderr, "%s: -D argument syntax error\n", argv[0]); error = 1; break; } *sd->def = '\0'; ++sd->def; } sd->next = sdefs; sdefs = sd; break; } case 'I': fprintf(stderr, "%s: ignoring -I (not implemented)\n", argv[0]); if (argv[argi][2] == 0) ++argi; break; case 'U': fprintf(stderr, "%s: ignoring -U (not implemented)\n", argv[0]); if (argv[argi][2] == 0) ++argi; break; case 'u': if (strcmp(argv[argi], "-undef") != 0) goto unrecognized_option; fprintf(stderr, "%s: ignoring -undef (not implemented)\n", argv[0]); break; case 'M': fprintf(stderr, "%s: ignoring -M (not implemented)\n", argv[0]); break; default: goto unrecognized_option; } ++argi; continue; unrecognized_option: fprintf(stderr, "%s: option '%s' unrecognized\n", argv[0], argv[argi]); error = 1; ++argi; } if (argi < argc) { inf = argv[argi++]; } else { fprintf(stderr, "%s: input file name required\n", argv[0]); error = 1; } if (argi < argc) { outf = argv[argi++]; } else { fprintf(stderr, "%s: output file name required\n", argv[0]); error = 1; } if (argi < argc) { fprintf(stderr, "%s: trailing arguments on command line\n", argv[0]); error = 1; } if (error) { help(argv[0]); return 1; } preprocess(inf, outf, sdefs); /*for (;;);*/ return 0; }