Pristine Ack-5.5
[Ack-5.5.git] / util / cpp / main.c
1 /* $Id: main.c,v 1.22 1995/08/17 13:33:55 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /* MAIN PROGRAM */
7
8 #include <alloc.h>
9 #include <em_arith.h>
10 #include <assert.h>
11 #include <system.h>
12 #include "file_info.h"
13 #include "idfsize.h"
14 #include "idf.h"
15 #include "macro.h"
16
17 extern char *symbol2str();
18 extern char *getwdir();
19 extern int err_occurred;
20 extern int do_dependencies;
21 extern char *dep_file;
22 int idfsize = IDFSIZE;
23 extern char options[];
24 static File *dep_fd = STDOUT;
25
26 arith ifval;
27
28 char *prog_name;
29
30 extern char **inctable;
31 extern int inc_max, inc_total;
32
33 main(argc, argv)
34         char *argv[];
35 {
36         /* parse and interpret the command line options */
37         prog_name = argv[0];
38
39         init_idf();
40
41         inctable = (char **) Malloc(10 * sizeof(char *));
42         inc_max = 10;
43         inc_total = 3;
44         inctable[0] = "";
45         inctable[1] = "/usr/include";
46         inctable[2] = 0;
47         init_pp();      /* initialise the preprocessor macros   */
48
49         /*      Note: source file "-" indicates that the source is supplied
50                 as standard input.  This is only allowed if INP_READ_IN_ONE is
51                 not defined!
52         */
53         while (argc > 1 && *argv[1] == '-' && argv[1][1] != '\0')       {
54                 char *par = &argv[1][1];
55
56                 if (*par == '-')
57                         par++;
58                 do_option(par);
59                 argc--, argv++;
60         }
61         compile(argc - 1, &argv[1]);
62         exit(err_occurred);
63 }
64
65 compile(argc, argv)
66         char *argv[];
67 {
68         register char *source = 0;
69         char *dummy;
70
71         switch (argc) {
72         case 1:
73                 source = argv[0];
74                 FileName = source;
75                 break;
76         case 0:
77                 FileName = "";
78                 WorkingDir = "";
79                 break;
80         default:
81                 FileName = argv[0];
82                 fatal("use: %s [options] [source]", prog_name);
83                 break;
84         }
85
86         if (!InsertFile(source, (char **) 0, &dummy)) /* read the source file   */
87                 fatal("%s: no source file %s", prog_name, 
88                         source ? source : "stdin");
89         if (source) WorkingDir = getwdir(dummy);
90         preprocess(source);
91         if (do_dependencies) list_dependencies(source);
92 }
93
94 struct idf      *file_head;
95 extern char *strrindex();
96
97 list_dependencies(source)
98         char *source;
99 {
100         register struct idf *p = file_head;
101
102         if (source) {
103                 register char *s = strrindex(source, '.');
104
105                 if (s && *(s+1)) {
106                         s++;
107                         *s++ = 'o';
108                         *s = '\0';
109                         /* the source may be in another directory than the
110                          * object generated, so don't include the pathname
111                          * leading to it.
112                          */
113                         if (s = strrindex(source, '/')) {
114                                 source = s + 1;
115                         }
116                 }
117                 else source = 0; 
118         }
119         if (dep_file && !sys_open(dep_file, OP_WRITE, &dep_fd)) {
120                 fatal("could not open %s", dep_file);
121         }
122         while (p) {
123                 assert(p->id_resmac == K_FILE);
124                 dependency(p->id_text, source);
125                 p = p->id_file;
126         }
127 }
128
129 add_dependency(s)
130         char *s;
131 {
132         register struct idf *p = str2idf(s, 0);
133
134         if (! p->id_resmac) {
135                 p->id_resmac = K_FILE;
136                 p->id_file = file_head;
137                 file_head = p;
138         }
139 }
140
141 dependency(s, source)
142         char *s, *source;
143 {
144         if (options['i'] && !strncmp(s, "/usr/include/", 13)) {
145                 return;
146         }
147         if (options['m'] && source) {
148                 fprint(dep_fd, "%s: %s\n", source, s);
149         }
150         else    fprint(dep_fd, "%s\n", s);
151 }