OSDN Git Service

* cpperror.c: Remove #ifdef EMACS block.
[pf3gnuchains/gcc-fork.git] / gcc / cppmain.c
1 /* CPP main program, using CPP Library.
2    Copyright (C) 1995, 1997-1999, 2000 Free Software Foundation, Inc.
3    Written by Per Bothner, 1994-95.
4
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
8 later version.
9
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.
14
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.
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
23 #include "config.h"
24 #include "system.h"
25 #include "cpplib.h"
26 #include "intl.h"
27
28 const char *progname;
29
30 cpp_reader parse_in;
31 cpp_options options;
32
33 \f
34 extern int main                         PARAMS ((int, char **));
35 int
36 main (argc, argv)
37      int argc;
38      char **argv;
39 {
40   char *p;
41   int argi = 1;  /* Next argument to handle.  */
42   struct cpp_options *opts = &options;
43   enum cpp_token kind;
44
45   p = argv[0] + strlen (argv[0]);
46   while (p != argv[0] && p[-1] != '/') --p;
47   progname = p;
48
49 #ifdef HAVE_LC_MESSAGES
50   setlocale (LC_MESSAGES, "");
51 #endif
52   (void) bindtextdomain (PACKAGE, localedir);
53   (void) textdomain (PACKAGE);
54
55   cpp_reader_init (&parse_in);
56   parse_in.opts = opts;
57
58   cpp_options_init (opts);
59   
60   argi += cpp_handle_options (&parse_in, argc - argi , argv + argi);
61   if (argi < argc && ! CPP_FATAL_ERRORS (&parse_in))
62     cpp_fatal (&parse_in, "Invalid option `%s'", argv[argi]);
63   if (CPP_FATAL_ERRORS (&parse_in))
64     return (FATAL_EXIT_CODE);
65       
66   parse_in.show_column = 1;
67
68   if (! cpp_start_read (&parse_in, opts->in_fname))
69     return (FATAL_EXIT_CODE);
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))
76     cpp_pfatal_with_name (&parse_in, opts->out_fname);
77
78   if (! opts->no_output)
79     {
80       do
81         {
82           kind = cpp_get_token (&parse_in);
83           if (CPP_WRITTEN (&parse_in) >= BUFSIZ || kind == CPP_EOF)
84             {
85               size_t rem, count = CPP_WRITTEN (&parse_in);
86
87               rem = fwrite (parse_in.token_buffer, 1, count, stdout);
88               if (rem < count)
89                 /* Write error. */
90                 cpp_pfatal_with_name (&parse_in, opts->out_fname);
91
92               CPP_SET_WRITTEN (&parse_in, 0);
93             }
94         }
95       while (kind != CPP_EOF);
96     }
97   else
98     {
99       do
100         {
101           cpp_scan_buffer (&parse_in);
102           kind = cpp_get_token (&parse_in);
103         }
104       while (kind != CPP_EOF);
105       CPP_SET_WRITTEN (&parse_in, 0);
106     }
107
108   cpp_finish (&parse_in);
109   if (fwrite (parse_in.token_buffer, 1, CPP_WRITTEN (&parse_in), stdout)
110       < CPP_WRITTEN (&parse_in))
111     cpp_pfatal_with_name (&parse_in, opts->out_fname);
112
113   cpp_cleanup (&parse_in);
114
115   if (parse_in.errors)
116     return (FATAL_EXIT_CODE);
117   return (SUCCESS_EXIT_CODE);
118 }