OSDN Git Service

* cpplib.h: Merge struct cpp_options into struct cpp_reader.
[pf3gnuchains/gcc-fork.git] / gcc / cppmain.c
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.
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
32 \f
33 extern int main                         PARAMS ((int, char **));
34 int
35 main (argc, argv)
36      int argc;
37      char **argv;
38 {
39   char *p;
40   cpp_reader *pfile = &parse_in;
41   int argi = 1;  /* Next argument to handle.  */
42   enum cpp_token kind;
43   FILE *out;
44   const char *out_fname;
45
46   p = argv[0] + strlen (argv[0]);
47   while (p != argv[0] && p[-1] != '/') --p;
48   progname = p;
49
50   xmalloc_set_program_name (progname);
51
52 #ifdef HAVE_LC_MESSAGES
53   setlocale (LC_MESSAGES, "");
54 #endif
55   (void) bindtextdomain (PACKAGE, localedir);
56   (void) textdomain (PACKAGE);
57
58   cpp_reader_init (pfile);
59   
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))
64     return (FATAL_EXIT_CODE);
65
66   if (! cpp_start_read (pfile, CPP_OPTION (pfile, in_fname)))
67     return (FATAL_EXIT_CODE);
68
69   /* Now that we know the input file is valid, open the output.  */
70   out_fname = CPP_OPTION (pfile, out_fname);
71   if (*out_fname == '\0')
72     {
73       out_fname = "stdout";
74       out = stdout;
75     }
76   else
77     {
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         }
84     }
85
86   if (! CPP_OPTION (pfile, no_output))
87     {
88       do
89         {
90           kind = cpp_get_token (pfile);
91           if (CPP_WRITTEN (pfile) >= BUFSIZ || kind == CPP_EOF)
92             {
93               size_t rem, count = CPP_WRITTEN (pfile);
94
95               rem = fwrite (parse_in.token_buffer, 1, count, out);
96               if (rem < count)
97                 /* Write error. */
98                 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
99
100               CPP_SET_WRITTEN (pfile, 0);
101             }
102         }
103       while (kind != CPP_EOF);
104     }
105   else
106     {
107       do
108         {
109           cpp_scan_buffer (pfile);
110           kind = cpp_get_token (pfile);
111         }
112       while (kind != CPP_EOF);
113       CPP_SET_WRITTEN (pfile, 0);
114     }
115
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));
123
124   cpp_cleanup (pfile);
125
126   if (parse_in.errors)
127     return (FATAL_EXIT_CODE);
128   return (SUCCESS_EXIT_CODE);
129 }