Fix a bug in 2.95.x prototypes that prevents us from building the world with
[dragonfly.git] / contrib / gcc / cppmain.c
1 /* CPP main program, using CPP Library.
2    Copyright (C) 1995, 1997, 1998, 1999 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 #ifndef EMACS
24 #include "config.h"
25 #include "system.h"
26 #else
27 #include <stdio.h>
28
29 extern char *getenv ();
30 #endif /* not EMACS */
31
32 #include "cpplib.h"
33 #include "intl.h"
34
35 char *progname;
36
37 cpp_reader parse_in;
38 cpp_options options;
39
40 \f
41 int
42 main (argc, argv)
43      int argc;
44      char **argv;
45 {
46   char *p;
47   int argi = 1;  /* Next argument to handle.  */
48   struct cpp_options *opts = &options;
49   enum cpp_token kind;
50
51   p = argv[0] + strlen (argv[0]);
52   while (p != argv[0] && p[-1] != '/') --p;
53   progname = p;
54
55 #ifdef HAVE_LC_MESSAGES
56   setlocale (LC_MESSAGES, "");
57 #endif
58   (void) bindtextdomain (PACKAGE, localedir);
59   (void) textdomain (PACKAGE);
60
61   cpp_reader_init (&parse_in);
62   parse_in.opts = opts;
63
64   cpp_options_init (opts);
65   
66   argi += cpp_handle_options (&parse_in, argc - argi , argv + argi);
67   if (argi < argc && ! CPP_FATAL_ERRORS (&parse_in))
68     cpp_fatal (&parse_in, "Invalid option `%s'", argv[argi]);
69   if (CPP_FATAL_ERRORS (&parse_in))
70     exit (FATAL_EXIT_CODE);
71       
72   parse_in.show_column = 1;
73
74   if (! cpp_start_read (&parse_in, opts->in_fname))
75     exit (FATAL_EXIT_CODE);
76
77   /* Now that we know the input file is valid, open the output.  */
78
79   if (!opts->out_fname || !strcmp (opts->out_fname, ""))
80     opts->out_fname = "stdout";
81   else if (! freopen (opts->out_fname, "w", stdout))
82     cpp_pfatal_with_name (&parse_in, opts->out_fname);
83
84   do
85     {
86       kind = cpp_get_token (&parse_in);
87       if (CPP_WRITTEN (&parse_in) >= BUFSIZ || kind == CPP_EOF)
88         {
89           if (! opts->no_output)
90             {
91               size_t rem, count = CPP_WRITTEN (&parse_in);
92
93               rem = fwrite (parse_in.token_buffer, 1, count, stdout);
94               if (rem < count)
95                 /* Write error. */
96                 cpp_pfatal_with_name (&parse_in, opts->out_fname);
97             }
98
99           CPP_SET_WRITTEN (&parse_in, 0);
100         }
101     }
102   while (kind != CPP_EOF);
103
104   cpp_finish (&parse_in);
105   if (fwrite (parse_in.token_buffer, 1, CPP_WRITTEN (&parse_in), stdout)
106       < CPP_WRITTEN (&parse_in))
107     cpp_pfatal_with_name (&parse_in, opts->out_fname);
108
109   if (parse_in.errors)
110     exit (FATAL_EXIT_CODE);
111   exit (SUCCESS_EXIT_CODE);
112 }