]> gcc.gnu.org Git - gcc.git/blame - gcc/cppmain.c
In gcc/:
[gcc.git] / gcc / cppmain.c
CommitLineData
7f2935c7 1/* CPP main program, using CPP Library.
5e7b4e25 2 Copyright (C) 1995, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
7f2935c7
PB
3 Written by Per Bothner, 1994-95.
4
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
7Free Software Foundation; either version 2, or (at your option) any
8later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
940d9d63 17Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
7f2935c7
PB
18
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! */
22
84ee6fd4 23#include "config.h"
b04cd507 24#include "system.h"
b04cd507 25#include "cpplib.h"
ab87f8c8 26#include "intl.h"
7f2935c7 27
bcc5cac9 28const char *progname;
1802bb1c 29
7f2935c7 30cpp_reader parse_in;
7f2935c7 31
7f2935c7 32\f
bcc5cac9 33extern int main PARAMS ((int, char **));
7f2935c7
PB
34int
35main (argc, argv)
36 int argc;
37 char **argv;
38{
39 char *p;
ae79697b 40 cpp_reader *pfile = &parse_in;
0f41302f 41 int argi = 1; /* Next argument to handle. */
3a2b2c7a 42 enum cpp_ttype kind;
ae79697b
ZW
43 FILE *out;
44 const char *out_fname;
7f2935c7
PB
45
46 p = argv[0] + strlen (argv[0]);
47 while (p != argv[0] && p[-1] != '/') --p;
48 progname = p;
49
f95e46b9
ZW
50 xmalloc_set_program_name (progname);
51
d9b53430 52#ifdef HAVE_LC_MESSAGES
ab87f8c8 53 setlocale (LC_MESSAGES, "");
d9b53430 54#endif
735396d9
KG
55 (void) bindtextdomain (PACKAGE, localedir);
56 (void) textdomain (PACKAGE);
ab87f8c8 57
ae79697b 58 cpp_reader_init (pfile);
7f2935c7 59
ae79697b
ZW
60 argi += cpp_handle_options (pfile, argc - argi , argv + argi);
61 if (argi < argc && ! CPP_FATAL_ERRORS (pfile))
62 cpp_fatal (pfile, "Invalid option %s", argv[argi]);
63 if (CPP_FATAL_ERRORS (pfile))
bcc5cac9 64 return (FATAL_EXIT_CODE);
7f2935c7 65
ae79697b 66 if (! cpp_start_read (pfile, CPP_OPTION (pfile, in_fname)))
bcc5cac9 67 return (FATAL_EXIT_CODE);
7f2935c7
PB
68
69 /* Now that we know the input file is valid, open the output. */
ae79697b
ZW
70 out_fname = CPP_OPTION (pfile, out_fname);
71 if (*out_fname == '\0')
72 {
73 out_fname = "stdout";
74 out = stdout;
75 }
76 else
c1212d2f 77 {
ae79697b
ZW
78 out = fopen (out_fname, "w");
79 if (!out)
80 {
81 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
82 return (FATAL_EXIT_CODE);
83 }
c1212d2f 84 }
7f2935c7 85
ae79697b 86 if (! CPP_OPTION (pfile, no_output))
7f2935c7 87 {
5d83f44b 88 do
7f2935c7 89 {
ae79697b
ZW
90 kind = cpp_get_token (pfile);
91 if (CPP_WRITTEN (pfile) >= BUFSIZ || kind == CPP_EOF)
2c826217 92 {
ae79697b 93 size_t rem, count = CPP_WRITTEN (pfile);
2c826217 94
ae79697b 95 rem = fwrite (parse_in.token_buffer, 1, count, out);
2c826217
ZW
96 if (rem < count)
97 /* Write error. */
ae79697b 98 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
2c826217 99
ae79697b 100 CPP_SET_WRITTEN (pfile, 0);
5d83f44b
ZW
101 }
102 }
103 while (kind != CPP_EOF);
104 }
105 else
106 {
107 do
108 {
ae79697b
ZW
109 cpp_scan_buffer (pfile);
110 kind = cpp_get_token (pfile);
7f2935c7 111 }
5d83f44b 112 while (kind != CPP_EOF);
ae79697b 113 CPP_SET_WRITTEN (pfile, 0);
7f2935c7
PB
114 }
115
ae79697b
ZW
116 cpp_finish (pfile);
117 if (fwrite (parse_in.token_buffer, 1, CPP_WRITTEN (pfile), out)
118 < CPP_WRITTEN (pfile))
119 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
120
121 if (ferror (out) || fclose (out))
122 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
7f2935c7 123
ae79697b 124 cpp_cleanup (pfile);
a9ae4483 125
7f2935c7 126 if (parse_in.errors)
bcc5cac9
KG
127 return (FATAL_EXIT_CODE);
128 return (SUCCESS_EXIT_CODE);
7f2935c7 129}
This page took 0.554895 seconds and 5 git commands to generate.