00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "config.h"
00024 #include "system.h"
00025 #include "line-map.h"
00026 #include "cpplib.h"
00027 #include "getopt.h"
00028 #include "mkdeps.h"
00029
00030 const char *progname;
00031 const char *vpath;
00032
00033 static const char *output_file;
00034 static bool had_errors;
00035
00036
00037 struct cmd_line_macro
00038 {
00039 struct cmd_line_macro *next;
00040 bool is_undef;
00041 const char *macro;
00042 };
00043
00044 static struct cmd_line_macro *cmd_line_macros;
00045 static cpp_dir *cmd_line_searchpath;
00046
00047 static void
00048 add_clm (const char *macro, bool is_undef)
00049 {
00050 struct cmd_line_macro *clm = xmalloc (sizeof (struct cmd_line_macro));
00051 clm->next = cmd_line_macros;
00052 clm->is_undef = is_undef;
00053 clm->macro = macro;
00054 cmd_line_macros = clm;
00055 }
00056
00057 static void
00058 add_dir (char *name, bool sysp)
00059 {
00060 cpp_dir *dir = xmalloc (sizeof (cpp_dir));
00061 dir->next = cmd_line_searchpath;
00062 dir->name = name;
00063 dir->sysp = sysp;
00064 dir->construct = 0;
00065 dir->user_supplied_p = 1;
00066 cmd_line_searchpath = dir;
00067 }
00068
00069
00070
00071 static void ATTRIBUTE_NORETURN
00072 usage (int errcode)
00073 {
00074 fprintf (stderr,
00075 "usage: %s [-vh] [-V vpath] [-Dname[=def]...] [-Uname] [-Idir...] [-o file] sources...\n",
00076 progname);
00077 exit (errcode);
00078 }
00079
00080 static int
00081 parse_options (int argc, char **argv)
00082 {
00083 static const struct option longopts[] = {
00084 { "--help", no_argument, 0, 'h' },
00085 { 0, 0, 0, 0 }
00086 };
00087
00088 for (;;)
00089 switch (getopt_long (argc, argv, "hD:U:I:J:o:V:", longopts, 0))
00090 {
00091 case 'h': usage (0);
00092 case 'D': add_clm (optarg, false); break;
00093 case 'U': add_clm (optarg, true); break;
00094 case 'I': add_dir (optarg, false); break;
00095 case 'J': add_dir (optarg, true); break;
00096 case 'o':
00097 if (output_file)
00098 {
00099 fprintf (stderr, "%s: too many output files\n", progname);
00100 usage (2);
00101 }
00102 output_file = optarg;
00103 break;
00104 case 'V':
00105 if (vpath)
00106 {
00107 fprintf (stderr, "%s: too many vpaths\n", progname);
00108 usage (2);
00109 }
00110 vpath = optarg;
00111 break;
00112 case '?':
00113 usage (2);
00114
00115 case -1:
00116 if (optind == argc)
00117 {
00118 fprintf (stderr, "%s: no input files\n", progname);
00119 usage (2);
00120 }
00121 return optind;
00122
00123 default:
00124 abort ();
00125 }
00126 }
00127
00128
00129 static cpp_reader *
00130 reader_init (struct line_maps *line_table)
00131 {
00132 cpp_reader *reader;
00133 cpp_options *options;
00134
00135 linemap_init (line_table);
00136 reader = cpp_create_reader (CLK_GNUC89, 0, line_table);
00137
00138
00139
00140 options = cpp_get_options (reader);
00141 options->inhibit_warnings = 1;
00142 options->inhibit_errors = 1;
00143 options->deps.style = DEPS_USER;
00144
00145
00146 cpp_post_options (reader);
00147 cpp_init_iconv (reader);
00148 cpp_set_include_chains (reader, cmd_line_searchpath, cmd_line_searchpath,
00149 false);
00150 if (vpath)
00151 {
00152 struct deps *deps = cpp_get_deps (reader);
00153 deps_add_vpath (deps, vpath);
00154 }
00155
00156 return reader;
00157 }
00158
00159
00160 static void
00161 process_file (const char *file)
00162 {
00163 struct line_maps line_table;
00164 cpp_reader *reader = reader_init (&line_table);
00165
00166 if (!cpp_read_main_file (reader, file))
00167 had_errors = true;
00168 else
00169 {
00170 struct cmd_line_macro *clm;
00171
00172 cpp_init_builtins (reader, true);
00173 for (clm = cmd_line_macros; clm; clm = clm->next)
00174 (clm->is_undef ? cpp_undef : cpp_define) (reader, clm->macro);
00175
00176 cpp_scan_nooutput (reader);
00177 if (cpp_finish (reader, stdout))
00178 had_errors = true;
00179 }
00180 cpp_destroy (reader);
00181 linemap_free (&line_table);
00182 }
00183
00184
00185
00186 int
00187 main(int argc, char **argv)
00188 {
00189 int first_input, i;
00190
00191 progname = argv[0];
00192 xmalloc_set_program_name (progname);
00193
00194 first_input = parse_options (argc, argv);
00195 if (output_file)
00196 if (!freopen (output_file, "w", stdout))
00197 {
00198 perror (output_file);
00199 return 1;
00200 }
00201
00202 for (i = first_input; i < argc; i++)
00203 process_file (argv[i]);
00204
00205 return had_errors;
00206 }