Pristine Ack-5.5
[Ack-5.5.git] / util / cmisc / mkdep.c
1 /* $Id: mkdep.c,v 0.10 1994/06/24 10:16:42 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 /* make dependencies; Date: jan 07, 1986; Author: Erik Baalbergen */
7 /* Log:
8         [Thu Oct  6 09:56:30 MET 1988; erikb]
9         Added option '-d' which suppresses "file.c :" be printed
10  */
11
12 #include <stdio.h>
13
14 #define BSIZ 1024
15 char *prog;
16
17 int dflag = 0;  /* suppress "file.c :" */
18
19 struct namelist {
20         struct namelist *next;
21         char *name;
22 };
23
24 struct namelist *freelist;
25 struct namelist *new_namelist();
26 struct namelist *nl = 0;
27
28 char *Malloc(u)
29         unsigned u;
30 {
31         char *sp, *malloc();
32
33         if ((sp = malloc(u)) == 0) {
34                 fprintf(stderr, "%s: out of space\n");
35                 exit(1);
36         }
37         return sp;
38 }
39
40 struct namelist *
41 new_namelist()
42 {
43         register struct namelist *nlp = freelist;
44
45         if (nlp) {
46                 freelist = nlp->next;
47                 return nlp;
48         }
49
50         return (struct namelist *) Malloc(sizeof(struct namelist));
51 }
52
53 free_namelist(nlp)
54         struct namelist *nlp;
55 {
56         if (nlp) {
57                 free_namelist(nlp->next);
58                 nlp->next = freelist;
59                 freelist = nlp;
60         }
61 }
62
63 add_name(nm)
64         char *nm;
65 {
66         struct namelist *nlp = nl, *lnlp = 0, *nnlp;
67         char *strcpy();
68
69         while (nlp) {
70                 register i = strcmp(nm, nlp->name);
71                 if (i < 0)
72                         break;
73                 if (i == 0)     /* already present */
74                         return;
75                 lnlp = nlp;
76                 nlp = nlp->next;
77         }
78
79         (nnlp = new_namelist())->name = strcpy(Malloc((unsigned)strlen(nm) + 1), nm);
80
81         if (lnlp) {
82                 nnlp->next = lnlp->next;
83                 lnlp->next = nnlp;
84         }
85         else {
86                 nnlp->next = nl;
87                 nl = nnlp;
88         }
89 }
90
91 print_namelist(nm, nlp)
92         char *nm;
93         struct namelist *nlp;
94 {
95         while (nlp) {
96                 if (!dflag)
97                         printf("%s: ", nm);
98                 printf("%s\n", nlp->name);
99                 nlp = nlp->next;
100         }
101 }
102
103 /*ARGSUSED*/
104 main(argc, argv)
105         char *argv[];
106 {
107         int err = 0;
108
109         prog = *argv++;
110         if (*argv && **argv == '-') {
111                 char *opt = &(*argv++)[1];
112
113                 if (*opt++ != 'd' || *opt) {
114                         fprintf(stderr, "use: %s [-d] [file ...]\n", prog);
115                         exit(1);
116                 }
117                 dflag = 1;
118         }
119
120         while (*argv) {
121                 free_namelist(nl);
122                 nl = 0;
123                 if (dofile(*argv) == 0)
124                         ++err;
125                 print_namelist(*argv++, nl);
126         }
127         exit(err ? 1 : 0);
128 }
129
130 int
131 contains_slash(s)
132         register char *s;
133 {
134         while (*s)
135                 if (*s++ == '/') return 1;
136         return 0;
137 }
138
139 extern char *fgets();
140
141 dofile(fn)
142         char *fn;
143 {
144         char buf[BSIZ];
145         FILE *fp;
146         char *nm, *include_line();
147
148         if ((fp = fopen(fn, "r")) == 0) {
149                 fprintf(stderr, "%s: cannot read %s\n", prog, fn);
150                 return 0;
151         }
152
153         if (contains_slash(fn)) {
154                 fprintf(stderr, "%s: (warning) %s not in current directory; not checked\n", prog, fn);
155                 fclose(fp);
156                 return 1;
157         }
158
159         while (fgets(buf, BSIZ, fp) != NULL)
160                 if (nm = include_line(buf)) {
161                         add_name(nm);
162                         if (dofile(nm)) ;
163                 }
164
165         fclose(fp);
166         return 1;
167 }
168
169 char *
170 include_line(s)
171         char *s;
172 {
173         while ((*s == '\t') || (*s == ' '))
174                 s++;
175         
176         if (*s++ == '#') {
177                 while ((*s == '\t') || (*s == ' '))
178                         s++;
179                 if (
180                         (*s++ == 'i') &&
181                         (*s++ == 'n') &&
182                         (*s++ == 'c') &&
183                         (*s++ == 'l') &&
184                         (*s++ == 'u') &&
185                         (*s++ == 'd') &&
186                         (*s++ == 'e')
187                 ) {
188                         while ((*s == '\t') || (*s == ' '))
189                                 s++;
190                         if (*s++ == '"') {
191                                 char *nm = s;
192
193                                 while (*s != 0 && *s != '"')
194                                         s++;
195                                 *s = '\0';
196                                 return nm;
197                         }
198                 }
199         }
200         return (char *) 0;
201 }