get Borland makefiles working yet again
[fw/sdcc] / support / cpp / cppmain.c
1 /* CPP main program, using CPP Library.
2    Copyright (C) 1995 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 "cpplib.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #define SUCCESS_EXIT_CODE 0
29 #define FATAL_EXIT_CODE   33
30 #define EMACS
31 #ifndef EMACS
32 #include "config.h"
33 #endif /* not EMACS */
34
35 extern char *getenv ();
36 char *version_string = " for SDC51";
37 char *progname;
38
39 cpp_reader parse_in;
40 cpp_options options;
41
42 /* More 'friendly' abort that prints the line and file.
43    config.h can #define abort fancy_abort if you like that sort of thing.  */
44
45 void
46 fancy_abort ()
47 {
48   fatal ("Internal gcc abort.");
49 }
50
51
52 int
53 main (argc, argv)
54      int argc;
55      char **argv;
56 {
57   char *p;
58   int i;
59   int argi = 1;  /* Next argument to handle. */
60   struct cpp_options *opts = &options;
61
62   p = argv[0] + strlen (argv[0]);
63   while (p != argv[0] && p[-1] != '/') --p;
64   progname = p;
65
66   init_parse_file (&parse_in);
67   parse_in.data = opts;
68
69   init_parse_options (opts);
70   
71   argi += cpp_handle_options (&parse_in, argc - argi , argv + argi);
72   if (argi < argc)
73     fatal ("Invalid option `%s'", argv[argi]);
74   parse_in.show_column = 1;
75
76   i = push_parse_file (&parse_in, opts->in_fname);
77   if (i != SUCCESS_EXIT_CODE)
78     return i;
79
80   /* Now that we know the input file is valid, open the output.  */
81
82   if (!opts->out_fname || !strcmp (opts->out_fname, ""))
83     opts->out_fname = "stdout";
84   else if (! freopen (opts->out_fname, "w", stdout))
85     cpp_pfatal_with_name (&parse_in, opts->out_fname);
86
87   for (;;)
88     {
89       enum cpp_token kind;
90       if (! opts->no_output)
91         {
92           fwrite (parse_in.token_buffer, 1, CPP_WRITTEN (&parse_in), stdout);
93         }
94       parse_in.limit = parse_in.token_buffer;
95       kind = cpp_get_token (&parse_in);
96       if (kind == CPP_EOF)
97         break;
98     }
99
100   cpp_finish (&parse_in);
101
102   if (parse_in.errors)
103     exit (FATAL_EXIT_CODE);
104   exit (SUCCESS_EXIT_CODE);
105 }