1 /* CPP main program, using CPP Library.
2 Copyright (C) 1995, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994-95.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding! */
31 cpp_printer parse_out;
33 int main PARAMS ((int, char **));
35 /* Callback routines for the parser. Most of these are active only
37 static void cb_define PARAMS ((cpp_reader *, cpp_hashnode *));
38 static void cb_undef PARAMS ((cpp_reader *, cpp_hashnode *));
39 static void cb_include PARAMS ((cpp_reader *, const unsigned char *,
40 const unsigned char *, unsigned int, int));
42 static void cb_ident PARAMS ((cpp_reader *, const unsigned char *,
44 static void cb_enter_file PARAMS ((cpp_reader *));
45 static void cb_leave_file PARAMS ((cpp_reader *));
46 static void cb_rename_file PARAMS ((cpp_reader *));
47 static void cb_def_pragma PARAMS ((cpp_reader *));
49 static void do_pragma_implementation PARAMS ((cpp_reader *));
50 static int dump_macros_helper PARAMS ((cpp_reader *, cpp_hashnode *));
58 cpp_reader *pfile = &parse_in;
60 int argi = 1; /* Next argument to handle. */
62 p = argv[0] + strlen (argv[0]);
63 while (p != argv[0] && ! IS_DIR_SEPARATOR (p[-1])) --p;
66 xmalloc_set_program_name (progname);
68 #ifdef HAVE_LC_MESSAGES
69 setlocale (LC_MESSAGES, "");
71 (void) bindtextdomain (PACKAGE, localedir);
72 (void) textdomain (PACKAGE);
75 cpp_reader_init (pfile);
77 argi += cpp_handle_options (pfile, argc - argi , argv + argi);
78 if (argi < argc && ! CPP_FATAL_ERRORS (pfile))
79 cpp_fatal (pfile, "Invalid option %s", argv[argi]);
80 if (CPP_FATAL_ERRORS (pfile))
81 return (FATAL_EXIT_CODE);
83 /* Open the output now. We must do so even if no_output is on,
84 because there may be other output than from the actual
85 preprocessing (e.g. from -dM). */
86 print = cpp_printer_init (pfile, &parse_out);
88 return (FATAL_EXIT_CODE);
91 if (! CPP_OPTION (pfile, no_line_commands)
92 && ! CPP_OPTION (pfile, no_output))
94 pfile->cb.enter_file = cb_enter_file;
95 pfile->cb.leave_file = cb_leave_file;
96 pfile->cb.rename_file = cb_rename_file;
98 if (CPP_OPTION (pfile, dump_includes))
99 pfile->cb.include = cb_include;
100 if (CPP_OPTION (pfile, debug_output)
101 || CPP_OPTION (pfile, dump_macros) == dump_names
102 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
104 pfile->cb.define = cb_define;
105 pfile->cb.undef = cb_undef;
106 pfile->cb.poison = cb_def_pragma;
108 pfile->cb.ident = cb_ident;
109 pfile->cb.def_pragma = cb_def_pragma;
111 /* Register one #pragma which needs special handling. */
112 cpp_register_pragma(pfile, 0, "implementation", do_pragma_implementation);
113 cpp_register_pragma(pfile, "GCC", "implementation", do_pragma_implementation);
115 if (! cpp_start_read (pfile, print, CPP_OPTION (pfile, in_fname)))
116 return (FATAL_EXIT_CODE);
118 if (CPP_OPTION (pfile, no_output))
119 while (CPP_BUFFER (pfile) != NULL)
120 cpp_scan_buffer_nooutput (pfile);
122 while (CPP_BUFFER (pfile) != NULL)
123 cpp_scan_buffer (pfile, print);
125 if (CPP_OPTION (pfile, dump_macros) == dump_only)
126 cpp_forall_identifiers (pfile, dump_macros_helper);
128 cpp_finish (pfile, print);
132 return (FATAL_EXIT_CODE);
133 return (SUCCESS_EXIT_CODE);
139 cb_ident (pfile, str, len)
141 const unsigned char *str;
144 cpp_printf (pfile, &parse_out, "#ident \"%.*s\"\n", (int) len, str);
149 cb_define (pfile, hash)
153 if (pfile->done_initializing)
155 cpp_printf (pfile, &parse_out, "#define %s", hash->name);
156 if (CPP_OPTION (pfile, debug_output)
157 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
158 cpp_dump_definition (pfile, parse_out.outf, hash);
159 putc ('\n', parse_out.outf);
165 cb_undef (pfile, hash)
169 if (pfile->done_initializing)
171 cpp_printf (pfile, &parse_out, "#undef %s\n", hash->name);
177 cb_include (pfile, dir, str, len, ab)
179 const unsigned char *dir;
180 const unsigned char *str;
190 cpp_printf (pfile, &parse_out, "#%s %c%.*s%c\n", dir, l, (int) len, str, r);
195 cb_enter_file (pfile)
198 cpp_buffer *ip = CPP_BUFFER (pfile);
200 cpp_printf (pfile, &parse_out, "# 1 \"%s\"%s%s\n", ip->nominal_fname,
201 pfile->done_initializing ? " 1" : "",
202 cpp_syshdr_flags (pfile, ip));
204 parse_out.lineno = 1;
205 parse_out.last_fname = ip->nominal_fname;
209 cb_leave_file (pfile)
212 cpp_buffer *ip = CPP_BUFFER (pfile);
214 cpp_printf (pfile, &parse_out, "# %u \"%s\" 2%s\n", ip->lineno,
215 ip->nominal_fname, cpp_syshdr_flags (pfile, ip));
217 parse_out.lineno = ip->lineno;
218 parse_out.last_fname = ip->nominal_fname;
222 cb_rename_file (pfile)
225 cpp_buffer *ip = CPP_BUFFER (pfile);
227 cpp_printf (pfile, &parse_out, "# %u \"%s\"%s\n", ip->lineno,
228 ip->nominal_fname, cpp_syshdr_flags (pfile, ip));
230 parse_out.lineno = ip->lineno;
231 parse_out.last_fname = ip->nominal_fname;
235 cb_def_pragma (pfile)
238 cpp_printf (pfile, &parse_out, "#pragma ");
239 cpp_output_list (pfile, parse_out.outf, &pfile->token_list,
240 pfile->first_directive_token + 2);
241 putc ('\n', parse_out.outf);
246 do_pragma_implementation (pfile)
249 /* Be quiet about `#pragma implementation' for a file only if it hasn't
250 been included yet. */
251 const cpp_token *tok = cpp_get_token (pfile);
254 if (tok->type != CPP_EOF)
256 if (tok->type != CPP_STRING || cpp_get_token (pfile)->type != CPP_EOF)
258 cpp_error (pfile, "malformed #pragma implementation");
262 /* Make a NUL-terminated copy of the string. */
263 copy = alloca (tok->val.str.len + 1);
264 memcpy (copy, tok->val.str.text, tok->val.str.len);
265 copy[tok->val.str.len] = '\0';
267 if (cpp_included (pfile, copy))
269 "#pragma implementation for %s appears after file is included",
273 /* forward to default-pragma handler. */
274 cb_def_pragma (pfile);
277 /* Dump out the hash table. */
279 dump_macros_helper (pfile, hp)
283 if (hp->type == T_MACRO)
285 cpp_printf (pfile, &parse_out, "#define %s", hp->name);
286 cpp_dump_definition (pfile, parse_out.outf, hp);
287 putc ('\n', parse_out.outf);