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