chiark / gitweb /
Imported Debian patch 1.0.0-5
[e16] / epp / 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, 675 Mass Ave, Cambridge, MA 02139, 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 <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "config.h"
28 #include "cpplib.h"
29
30 #define EPP_DEBUG 0
31
32 cpp_reader          parse_in;
33 cpp_options         options;
34
35 int
36 main(int argc, char **argv)
37 {
38    char               *p;
39    int                 i;
40    int                 argi = 1;        /* Next argument to handle. */
41    struct cpp_options *opts = &options;
42    enum cpp_token      kind;
43    int                 got_text;
44
45    p = argv[0] + strlen(argv[0]);
46 #ifndef __EMX__
47    while (p != argv[0] && p[-1] != '/')
48 #else
49    while (p != argv[0] && p[-1] != '/' && p[-1] != '\\')
50 #endif
51       --p;
52    progname = p;
53
54    init_parse_file(&parse_in);
55    parse_in.data = opts;
56
57    init_parse_options(opts);
58
59    argi += cpp_handle_options(&parse_in, argc - argi, argv + argi);
60    if (argi < argc)
61       cpp_fatal("Invalid option `%s'", argv[argi]);
62    parse_in.show_column = 1;
63
64    i = push_parse_file(&parse_in, opts->in_fname);
65    if (i != SUCCESS_EXIT_CODE)
66       return i;
67
68    /* Now that we know the input file is valid, open the output.  */
69
70    if (!opts->out_fname || !strcmp(opts->out_fname, ""))
71       opts->out_fname = "stdout";
72    else if (!freopen(opts->out_fname, "w", stdout))
73       cpp_pfatal_with_name(&parse_in, opts->out_fname);
74
75    got_text = 0;
76    for (i = 0;; i++)
77      {
78         kind = cpp_get_token(&parse_in);
79 #if EPP_DEBUG
80         fprintf(stderr, "%03d: kind=%d len=%d out=%d text=%d\n", i,
81                 kind, CPP_WRITTEN(&parse_in), !opts->no_output, got_text);
82 #endif
83         switch (kind)
84           {
85           case CPP_EOF:
86              goto done;
87
88           case CPP_HSPACE:
89              continue;
90
91           case CPP_VSPACE:
92              if (!got_text)
93                 goto next;
94              break;
95
96           default:
97           case CPP_OTHER:
98           case CPP_NAME:
99           case CPP_NUMBER:
100           case CPP_CHAR:
101           case CPP_STRING:
102           case CPP_LPAREN:
103           case CPP_RPAREN:
104           case CPP_LBRACE:
105           case CPP_RBRACE:
106           case CPP_COMMA:
107           case CPP_SEMICOLON:
108           case CPP_3DOTS:
109              got_text = 1;
110              continue;
111
112           case CPP_COMMENT:
113           case CPP_DIRECTIVE:
114           case CPP_POP:
115              continue;
116           }
117 #if EPP_DEBUG
118         fprintf(stderr, "'");
119         fwrite(parse_in.token_buffer, 1, CPP_WRITTEN(&parse_in), stderr);
120         fprintf(stderr, "'\n");
121 #endif
122         if (!opts->no_output)
123           {
124              fwrite(parse_in.token_buffer, 1, CPP_WRITTEN(&parse_in), stdout);
125           }
126       next:
127         parse_in.limit = parse_in.token_buffer;
128         got_text = 0;
129      }
130
131  done:
132    cpp_finish(&parse_in);
133
134    if (parse_in.errors)
135       exit(FATAL_EXIT_CODE);
136    exit(SUCCESS_EXIT_CODE);
137 }