1 /* $Id: cdecl.c,v 1.18 2001/07/13 19:09:55 sandro Exp $ */
2
3 /*
4 * Copyright (c) 1995-2001 Sandro Sigala. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <err.h>
32
33 #include "config.h"
34
35 extern int lineno;
36 extern FILE *yyin;
37 extern int yyparse(void);
38
39 FILE *output_file;
40
process_file(char * filename)41 static void process_file(char *filename)
42 {
43 if (filename != NULL && strcmp(filename, "-") != 0) {
44 if ((yyin = fopen(filename, "r")) == NULL)
45 err(1, "%s", filename);
46 } else
47 yyin = stdin;
48
49 lineno = 1;
50 yyparse();
51
52 if (yyin != stdin)
53 fclose(yyin);
54 }
55
56 /*
57 * Output the program syntax then exit.
58 */
usage(void)59 static void usage(void)
60 {
61 fprintf(stderr, "usage: cdecl [-V] [-o file] [file ...]\n");
62 exit(1);
63 }
64
65 /*
66 * Used by the err() functions.
67 */
68 char *progname;
69
main(int argc,char ** argv)70 int main(int argc, char **argv)
71 {
72 int c;
73
74 progname = argv[0];
75 output_file = stdout;
76
77 while ((c = getopt(argc, argv, "o:V")) != -1)
78 switch (c) {
79 case 'o':
80 if (output_file != stdout)
81 fclose(output_file);
82 if ((output_file = fopen(optarg, "w")) == NULL)
83 err(1, "%s", optarg);
84 break;
85 case 'V':
86 fprintf(stderr, "%s\n", CUTILS_VERSION);
87 exit(0);
88 case '?':
89 default:
90 usage();
91 /* NOTREACHED */
92 }
93 argc -= optind;
94 argv += optind;
95
96 if (argc < 1)
97 process_file(NULL);
98 else
99 while (*argv)
100 process_file(*argv++);
101
102 return 0;
103 }
104