]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cppmain.c
cpplib.c (my_strerror, [...]): Move to cpperror.c.
[thirdparty/gcc.git] / gcc / cppmain.c
CommitLineData
7f2935c7 1/* CPP main program, using CPP Library.
40ea76de 2 Copyright (C) 1995, 1997-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
PB
30cpp_reader parse_in;
31cpp_options options;
32
7f2935c7 33\f
bcc5cac9 34extern int main PARAMS ((int, char **));
7f2935c7
PB
35int
36main (argc, argv)
37 int argc;
38 char **argv;
39{
40 char *p;
0f41302f 41 int argi = 1; /* Next argument to handle. */
7f2935c7 42 struct cpp_options *opts = &options;
2c826217 43 enum cpp_token kind;
7f2935c7
PB
44
45 p = argv[0] + strlen (argv[0]);
46 while (p != argv[0] && p[-1] != '/') --p;
47 progname = p;
48
d9b53430 49#ifdef HAVE_LC_MESSAGES
ab87f8c8 50 setlocale (LC_MESSAGES, "");
d9b53430 51#endif
735396d9
KG
52 (void) bindtextdomain (PACKAGE, localedir);
53 (void) textdomain (PACKAGE);
ab87f8c8 54
59de0311 55 cpp_reader_init (&parse_in);
c50bca08 56 parse_in.opts = opts;
7f2935c7 57
59de0311 58 cpp_options_init (opts);
7f2935c7
PB
59
60 argi += cpp_handle_options (&parse_in, argc - argi , argv + argi);
6c8e6d0c
PB
61 if (argi < argc && ! CPP_FATAL_ERRORS (&parse_in))
62 cpp_fatal (&parse_in, "Invalid option `%s'", argv[argi]);
59de0311 63 if (CPP_FATAL_ERRORS (&parse_in))
bcc5cac9 64 return (FATAL_EXIT_CODE);
59de0311 65
7f2935c7
PB
66 parse_in.show_column = 1;
67
59de0311 68 if (! cpp_start_read (&parse_in, opts->in_fname))
bcc5cac9 69 return (FATAL_EXIT_CODE);
7f2935c7
PB
70
71 /* Now that we know the input file is valid, open the output. */
72
73 if (!opts->out_fname || !strcmp (opts->out_fname, ""))
74 opts->out_fname = "stdout";
75 else if (! freopen (opts->out_fname, "w", stdout))
c1212d2f
ZW
76 {
77 cpp_notice_from_errno (&parse_in, opts->out_fname);
78 return (FATAL_EXIT_CODE);
79 }
7f2935c7 80
5d83f44b 81 if (! opts->no_output)
7f2935c7 82 {
5d83f44b 83 do
7f2935c7 84 {
5d83f44b
ZW
85 kind = cpp_get_token (&parse_in);
86 if (CPP_WRITTEN (&parse_in) >= BUFSIZ || kind == CPP_EOF)
2c826217
ZW
87 {
88 size_t rem, count = CPP_WRITTEN (&parse_in);
89
90 rem = fwrite (parse_in.token_buffer, 1, count, stdout);
91 if (rem < count)
92 /* Write error. */
c1212d2f 93 cpp_notice_from_errno (&parse_in, opts->out_fname);
2c826217 94
5d83f44b
ZW
95 CPP_SET_WRITTEN (&parse_in, 0);
96 }
97 }
98 while (kind != CPP_EOF);
99 }
100 else
101 {
102 do
103 {
104 cpp_scan_buffer (&parse_in);
105 kind = cpp_get_token (&parse_in);
7f2935c7 106 }
5d83f44b
ZW
107 while (kind != CPP_EOF);
108 CPP_SET_WRITTEN (&parse_in, 0);
7f2935c7
PB
109 }
110
111 cpp_finish (&parse_in);
2c826217
ZW
112 if (fwrite (parse_in.token_buffer, 1, CPP_WRITTEN (&parse_in), stdout)
113 < CPP_WRITTEN (&parse_in))
c1212d2f 114 cpp_notice_from_errno (&parse_in, opts->out_fname);
7f2935c7 115
a9ae4483
ZW
116 cpp_cleanup (&parse_in);
117
7f2935c7 118 if (parse_in.errors)
bcc5cac9
KG
119 return (FATAL_EXIT_CODE);
120 return (SUCCESS_EXIT_CODE);
7f2935c7 121}