Imported Upstream version 1.7.1
[platform/upstream/edje.git] / src / bin / 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  * Copyright (C) 2003-2011 Kim Woelders
5  * 
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2, or (at your option) any
9  * later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19  * 
20  * In other words, you are welcome to use, share and improve this program.
21  * You are forbidden to forbid anyone else to use, share and improve
22  * what you give them.   Help stamp out software-hoarding!  */
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "cpplib.h"
33
34 #define EPP_DEBUG 0
35
36 cpp_reader          parse_in;
37 cpp_options         options;
38
39 int
40 main(int argc, char **argv)
41 {
42    char               *p;
43    int                 i;
44    int                 argi = 1;        /* Next argument to handle. */
45    struct cpp_options *opts = &options;
46    enum cpp_token      kind;
47 #if EPP_DEBUG
48    int                 got_text = 0;
49 #endif
50
51    p = argv[0] + strlen(argv[0]);
52 #ifndef __EMX__
53    while (p != argv[0] && p[-1] != '/')
54 #else
55    while (p != argv[0] && p[-1] != '/' && p[-1] != '\\')
56 #endif
57       --p;
58    progname = p;
59
60    init_parse_file(&parse_in);
61    parse_in.data = opts;
62
63    init_parse_options(opts);
64
65    argi += cpp_handle_options(&parse_in, argc - argi, argv + argi);
66    if (argi < argc)
67       cpp_fatal("Invalid option `%s'", argv[argi]);
68    parse_in.show_column = 1;
69
70    i = push_parse_file(&parse_in, opts->in_fname);
71    if (i != SUCCESS_EXIT_CODE)
72       return i;
73
74    /* Now that we know the input file is valid, open the output.  */
75
76    if (!opts->out_fname || !strcmp(opts->out_fname, ""))
77       opts->out_fname = "stdout";
78    else if (!freopen(opts->out_fname, "w", stdout))
79       cpp_pfatal_with_name(&parse_in, opts->out_fname);
80
81    for (i = 0;; i++)
82      {
83         kind = cpp_get_token(&parse_in);
84 #if EPP_DEBUG
85         fprintf(stderr, "%03d: kind=%d len=%d out=%d text=%d\n", i,
86                 kind, CPP_WRITTEN(&parse_in), !opts->no_output, got_text);
87 #endif
88         switch (kind)
89           {
90           case CPP_EOF:
91              goto done;
92
93           case CPP_HSPACE:
94              continue;
95
96           case CPP_VSPACE:
97              break;
98
99           default:
100           case CPP_OTHER:
101           case CPP_NAME:
102           case CPP_NUMBER:
103           case CPP_CHAR:
104           case CPP_STRING:
105           case CPP_LPAREN:
106           case CPP_RPAREN:
107           case CPP_LBRACE:
108           case CPP_RBRACE:
109           case CPP_COMMA:
110           case CPP_SEMICOLON:
111           case CPP_3DOTS:
112 #if EPP_DEBUG
113              got_text = 1;
114 #endif
115              continue;
116
117           case CPP_COMMENT:
118           case CPP_DIRECTIVE:
119           case CPP_POP:
120              continue;
121           }
122 #if EPP_DEBUG
123         fprintf(stderr, "'");
124         fwrite(parse_in.token_buffer, 1, CPP_WRITTEN(&parse_in), stderr);
125         fprintf(stderr, "'\n");
126 #endif
127         if (!opts->no_output)
128           {
129              size_t n;
130
131              n = CPP_WRITTEN(&parse_in);
132              if (fwrite(parse_in.token_buffer, 1, n, stdout) != n)
133                 exit(FATAL_EXIT_CODE);
134           }
135         parse_in.limit = parse_in.token_buffer;
136 #if EPP_DEBUG
137         got_text = 0;
138 #endif
139      }
140
141  done:
142    cpp_finish(&parse_in);
143
144    if (parse_in.errors)
145       exit(FATAL_EXIT_CODE);
146    exit(SUCCESS_EXIT_CODE);
147 }