Fix for assertion error when expanding macro.
[iverilog.git] / ivlpp / main.c
blobd50f9c7048861c887d1bfd3316e0a971ebe9604d
1 const char COPYRIGHT[] =
2 "Copyright (c) 1999 Stephen Williams (steve@icarus.com)";
3 /*
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
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.
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, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: main.c,v 1.23 2006/10/02 18:16:18 steve Exp $"
21 #endif
23 # include "config.h"
25 const char NOTICE[] =
26 " This program is free software; you can redistribute it and/or modify\n"
27 " it under the terms of the GNU General Public License as published by\n"
28 " the Free Software Foundation; either version 2 of the License, or\n"
29 " (at your option) any later version.\n"
30 "\n"
31 " This program is distributed in the hope that it will be useful,\n"
32 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
33 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
34 " GNU General Public License for more details.\n"
35 "\n"
36 " You should have received a copy of the GNU General Public License\n"
37 " along with this program; if not, write to the Free Software\n"
38 " Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA\n"
41 const char VERSION[] = "$Name: $ $State: Exp $";
43 # include <stdio.h>
44 # include <stdlib.h>
45 #ifdef HAVE_MALLOC_H
46 # include <malloc.h>
47 #endif
48 # include <unistd.h>
49 # include <string.h>
50 # include <ctype.h>
51 #if defined(HAVE_GETOPT_H)
52 # include <getopt.h>
53 #endif
54 # include "globals.h"
56 #if defined(__MINGW32__) && !defined(HAVE_GETOPT_H)
57 extern int getopt(int argc, char*argv[], const char*fmt);
58 extern int optind;
59 extern const char*optarg;
60 #endif
61 /* Path to the dependency file, if there is one. */
62 char *dep_path = NULL;
65 * Keep in source_list an array of pointers to file names. The array
66 * is terminated by a pointer to null.
68 static char**source_list = 0;
69 static unsigned source_cnt = 0;
71 void add_source_file(const char*name)
73 if (source_list == 0) {
74 source_list = calloc(2, sizeof(char*));
75 source_list[0] = strdup(name);
76 source_list[1] = 0;
77 source_cnt = 1;
78 } else {
79 source_list = realloc(source_list, sizeof(char*) * (source_cnt+2));
80 source_list[source_cnt+0] = strdup(name);
81 source_list[source_cnt+1] = 0;
82 source_cnt += 1;
86 char**include_dir = 0;
87 unsigned include_cnt = 0;
89 int line_direct_flag = 0;
91 unsigned error_count = 0;
92 FILE *depend_file = NULL;
94 static int flist_read_flags(const char*path)
96 char line_buf[2048];
97 FILE*fd = fopen(path, "r");
98 if (fd == 0) {
99 fprintf(stderr, "%s: unable to open for reading.\n", path);
100 return -1;
103 while (fgets(line_buf, sizeof line_buf, fd) != 0) {
104 /* Skip leading white space. */
105 char*cp = line_buf + strspn(line_buf, " \t\r\b\f");
106 /* Remove trailing white space. */
107 char*tail = cp + strlen(cp);
108 while (tail > cp) {
109 if (! isspace(tail[-1]))
110 break;
111 tail -= 1;
112 tail[0] = 0;
115 /* Skip empty lines */
116 if (*cp == 0)
117 continue;
118 /* Skip comment lines */
119 if (cp[0] == '#')
120 continue;
122 /* The arg points to the argument to the keyword. */
123 char*arg = strchr(cp, ':');
124 if (arg) *arg++ = 0;
126 if (strcmp(cp,"D") == 0) {
127 char*val = strchr(arg, '=');
128 if (val)
129 *val++ = 0;
130 else
131 val = "1";
133 define_macro(arg, val, 0, 0);
135 } else if (strcmp(cp,"I") == 0) {
136 include_dir = realloc(include_dir,
137 (include_cnt+1)*sizeof(char*));
138 include_dir[include_cnt] = strdup(arg);
139 include_cnt += 1;
141 } else if (strcmp(cp,"keyword") == 0) {
142 char*buf = malloc(strlen(arg) + 2);
143 buf[0] = '`';
144 strcpy(buf+1, optarg);
145 define_macro(optarg, buf, 1, 0);
146 free(buf);
148 } else if (strcmp(cp,"M") == 0) {
149 if (dep_path) {
150 fprintf(stderr, "duplicate -M flag.\n");
151 } else {
152 dep_path = strdup(arg);
155 } else {
156 fprintf(stderr, "%s: Invalid keyword %s\n", path, cp);
160 fclose(fd);
161 return 0;
165 * This function reads from a file a list of file names. Each name
166 * starts with the first non-space character, and ends with the last
167 * non-space character. Spaces in the middle are OK.
169 static int flist_read_names(const char*path)
171 char line_buf[2048];
173 FILE*fd = fopen(path, "r");
174 if (fd == 0) {
175 fprintf(stderr, "%s: unable to open for reading.\n", path);
176 return 1;
179 while (fgets(line_buf, sizeof line_buf, fd) != 0) {
180 char*cp = line_buf + strspn(line_buf, " \t\r\b\f");
181 char*tail = cp + strlen(cp);
182 while (tail > cp) {
183 if (! isspace(tail[-1]))
184 break;
185 tail -= 1;
186 tail[0] = 0;
189 if (cp < tail)
190 add_source_file(cp);
193 return 0;
196 int main(int argc, char*argv[])
198 int opt, idx;
199 const char*flist_path = 0;
200 unsigned flag_errors = 0;
201 char*out_path = 0;
202 FILE*out;
204 /* Define preprocessor keywords that I plan to just pass. */
205 define_macro("celldefine", "`celldefine", 1, 0);
206 define_macro("default_nettype", "`default_nettype", 1, 0);
207 define_macro("delay_mode_distributed", "`delay_mode_distributed", 1, 0);
208 define_macro("delay_mode_unit", "`delay_mode_unit", 1, 0);
209 define_macro("delay_mode_path", "`delay_mode_path", 1, 0);
210 define_macro("disable_portfaults", "`disable_portfaults", 1, 0);
211 define_macro("enable_portfaults", "`enable_portfaults", 1, 0);
212 define_macro("endcelldefine", "`endcelldefine", 1, 0);
213 define_macro("endprotect", "`endprotect", 1, 0);
214 define_macro("nosuppress_faults", "`nosuppress_faults", 1, 0);
215 define_macro("nounconnected_drive", "`nounconnected_drive", 1, 0);
216 define_macro("protect", "`protect", 1, 0);
217 define_macro("resetall", "`resetall", 1, 0);
218 define_macro("suppress_faults", "`suppress_faults", 1, 0);
219 define_macro("timescale", "`timescale", 1, 0);
220 define_macro("unconnected_drive", "`unconnected_drive", 1, 0);
221 define_macro("uselib", "`uselib", 1, 0);
223 include_cnt = 2;
224 include_dir = malloc(include_cnt*sizeof(char*));
225 include_dir[0] = 0; /* 0 is reserved for the current files path. */
226 include_dir[1] = strdup(".");
228 while ((opt=getopt(argc, argv, "F:f:K:Lo:v")) != EOF) switch (opt) {
230 case 'F':
231 flist_read_flags(optarg);
232 break;
234 case 'f':
235 if (flist_path) {
236 fprintf(stderr, "%s: duplicate -f flag\n", argv[0]);
237 flag_errors += 1;
239 flist_path = optarg;
240 break;
242 case 'K': {
243 char*buf = malloc(strlen(optarg) + 2);
244 buf[0] = '`';
245 strcpy(buf+1, optarg);
246 define_macro(optarg, buf, 1, 0);
247 free(buf);
248 break;
251 case 'L':
252 line_direct_flag = 1;
253 break;
255 case 'o':
256 if (out_path) {
257 fprintf(stderr, "duplicate -o flag.\n");
258 } else {
259 out_path = optarg;
261 break;
263 case 'v':
264 fprintf(stderr, "Icarus Verilog Preprocessor version %s\n",
265 VERSION);
266 fprintf(stderr, "%s\n", COPYRIGHT);
267 fputs(NOTICE, stderr);
268 break;
270 default:
271 flag_errors += 1;
272 break;
275 if (flag_errors) {
276 fprintf(stderr, "\nUsage: %s [-v][-L][-F<fil>][-f<fil>] <file>...\n"
277 " -F<fil> - Get defines and includes from file\n"
278 " -f<fil> - Read the sources listed in the file\n"
279 " -K<def> - Define a keyword macro that I just pass\n"
280 " -L - Emit line number directives\n"
281 " -o<fil> - Send the output to <fil>\n"
282 " -v - Print version information\n",
283 argv[0]);
284 return flag_errors;
287 /* Collect the file names on the command line in the source
288 file list, then if there is a file list, read more file
289 names from there. */
290 for (idx = optind ; idx < argc ; idx += 1)
291 add_source_file(argv[idx]);
294 if (flist_path) {
295 int rc = flist_read_names(flist_path);
296 if (rc != 0)
297 return rc;
300 /* Figure out what to use for an output file. Write to stdout
301 if no path is specified. */
302 if (out_path) {
303 out = fopen(out_path, "w");
304 if (out == 0) {
305 perror(out_path);
306 exit(1);
308 } else {
309 out = stdout;
312 if(dep_path) {
313 depend_file = fopen(dep_path, "w");
314 if (depend_file == 0) {
315 perror(dep_path);
316 exit(1);
320 if (source_cnt == 0) {
321 fprintf(stderr, "%s: No input files given.\n", argv[0]);
322 return 1;
325 /* Pass to the lexical analyzer the list of input file, and
326 start the parser. */
327 reset_lexor(out, source_list);
328 if (yyparse()) return -1;
330 if(depend_file) {
331 fclose(depend_file);
334 return error_count;