ack/util/cmisc/mkdep.c

211 lines
3.2 KiB
C
Raw Normal View History

1994-06-24 11:31:16 +00:00
/* $Id$ */
1987-03-09 19:15:41 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1986-10-20 14:42:41 +00:00
/* make dependencies; Date: jan 07, 1986; Author: Erik Baalbergen */
/* Log:
2019-05-10 17:14:01 +00:00
[Thu Oct 6 09:56:30 MET 1988; erikb]
Added option '-d' which suppresses "file.c :" be printed
*/
1986-10-20 14:42:41 +00:00
#include <stdio.h>
2019-05-10 17:14:01 +00:00
#include <string.h>
#include <stdlib.h>
1986-10-20 14:42:41 +00:00
#define BSIZ 1024
char *prog;
2019-05-10 17:14:01 +00:00
int dflag = 0; /* suppress "file.c :" */
1986-10-20 14:42:41 +00:00
2019-05-10 17:14:01 +00:00
struct namelist
{
1986-10-20 14:42:41 +00:00
struct namelist *next;
char *name;
};
struct namelist *freelist;
struct namelist *new_namelist();
struct namelist *nl = 0;
2019-05-10 17:14:01 +00:00
char *Malloc(unsigned int);
char *include_line(char *);
int dofile(char *);
1986-10-20 14:42:41 +00:00
char *Malloc(u)
unsigned u;
{
2019-05-10 17:14:01 +00:00
char *sp;
1986-10-20 14:42:41 +00:00
2019-05-10 17:14:01 +00:00
if ((sp = malloc(u)) == 0)
{
fprintf(stderr, "%s:", " out of space\n");
1986-10-20 14:42:41 +00:00
exit(1);
}
return sp;
}
struct namelist *
new_namelist()
{
register struct namelist *nlp = freelist;
2019-05-10 17:14:01 +00:00
if (nlp)
{
1986-10-20 14:42:41 +00:00
freelist = nlp->next;
return nlp;
}
return (struct namelist *) Malloc(sizeof(struct namelist));
}
2019-05-10 17:14:01 +00:00
void free_namelist(struct namelist *nlp)
1986-10-20 14:42:41 +00:00
{
2019-05-10 17:14:01 +00:00
if (nlp)
{
1986-10-20 14:42:41 +00:00
free_namelist(nlp->next);
nlp->next = freelist;
freelist = nlp;
}
}
2019-05-10 17:14:01 +00:00
void add_name(char *nm)
1986-10-20 14:42:41 +00:00
{
struct namelist *nlp = nl, *lnlp = 0, *nnlp;
1986-10-20 14:42:41 +00:00
2019-05-10 17:14:01 +00:00
while (nlp)
{
register int i = strcmp(nm, nlp->name);
1986-10-20 14:42:41 +00:00
if (i < 0)
break;
2019-05-10 17:14:01 +00:00
if (i == 0) /* already present */
1986-10-20 14:42:41 +00:00
return;
lnlp = nlp;
nlp = nlp->next;
}
2019-05-10 17:14:01 +00:00
(nnlp = new_namelist())->name = strcpy(Malloc((unsigned) strlen(nm) + 1),
nm);
1986-10-20 14:42:41 +00:00
2019-05-10 17:14:01 +00:00
if (lnlp)
{
1986-10-20 14:42:41 +00:00
nnlp->next = lnlp->next;
lnlp->next = nnlp;
}
2019-05-10 17:14:01 +00:00
else
{
1986-10-20 14:42:41 +00:00
nnlp->next = nl;
nl = nnlp;
}
}
2019-05-10 17:14:01 +00:00
void print_namelist(char *nm, struct namelist *nlp)
1986-10-20 14:42:41 +00:00
{
2019-05-10 17:14:01 +00:00
while (nlp)
{
if (!dflag)
printf("%s: ", nm);
printf("%s\n", nlp->name);
nlp = nlp->next;
1986-10-20 14:42:41 +00:00
}
}
1988-10-07 10:26:37 +00:00
/*ARGSUSED*/
2019-05-10 17:14:01 +00:00
int main(int argc, char *argv[])
1986-10-20 14:42:41 +00:00
{
int err = 0;
prog = *argv++;
2019-05-10 17:14:01 +00:00
if (*argv && **argv == '-')
{
char *opt = &(*argv++)[1];
2019-05-10 17:14:01 +00:00
if (*opt++ != 'd' || *opt)
{
fprintf(stderr, "use: %s [-d] [file ...]\n", prog);
exit(1);
}
dflag = 1;
}
2019-05-10 17:14:01 +00:00
while (*argv)
{
1986-10-20 14:42:41 +00:00
free_namelist(nl);
nl = 0;
if (dofile(*argv) == 0)
++err;
print_namelist(*argv++, nl);
}
1987-03-09 13:29:04 +00:00
exit(err ? 1 : 0);
1986-10-20 14:42:41 +00:00
}
2019-05-10 17:14:01 +00:00
int contains_slash(register char *s)
{
while (*s)
2019-05-10 17:14:01 +00:00
if (*s++ == '/')
return 1;
return 0;
}
2019-05-10 17:14:01 +00:00
int dofile(char *fn)
1986-10-20 14:42:41 +00:00
{
1988-10-07 10:26:37 +00:00
char buf[BSIZ];
1986-10-20 14:42:41 +00:00
FILE *fp;
2019-05-10 17:14:01 +00:00
char *nm;
1986-10-20 14:42:41 +00:00
2019-05-10 17:14:01 +00:00
if ((fp = fopen(fn, "r")) == 0)
{
fprintf(stderr, "%s: cannot read %s\n", prog, fn);
1986-10-20 14:42:41 +00:00
return 0;
}
2019-05-10 17:14:01 +00:00
if (contains_slash(fn))
{
fprintf(stderr,
"%s: (warning) %s not in current directory; not checked\n",
prog, fn);
fclose(fp);
return 1;
}
1986-10-20 14:42:41 +00:00
while (fgets(buf, BSIZ, fp) != NULL)
2019-05-10 17:14:01 +00:00
if ((nm = include_line(buf)))
{
1986-10-20 14:42:41 +00:00
add_name(nm);
2019-05-10 17:14:01 +00:00
if (dofile(nm))
;
1986-10-20 14:42:41 +00:00
}
fclose(fp);
return 1;
}
2019-05-10 17:14:01 +00:00
char *include_line(char *s)
1986-10-20 14:42:41 +00:00
{
while ((*s == '\t') || (*s == ' '))
s++;
2019-05-10 17:14:01 +00:00
if (*s++ == '#')
{
1986-10-20 14:42:41 +00:00
while ((*s == '\t') || (*s == ' '))
s++;
2019-05-10 17:14:01 +00:00
if ((*s++ == 'i') && (*s++ == 'n') && (*s++ == 'c') && (*s++ == 'l')
&& (*s++ == 'u') && (*s++ == 'd') && (*s++ == 'e'))
{
1986-10-20 14:42:41 +00:00
while ((*s == '\t') || (*s == ' '))
s++;
2019-05-10 17:14:01 +00:00
if (*s++ == '"')
{
char *nm = s;
1986-10-20 14:42:41 +00:00
while (*s != 0 && *s != '"')
s++;
*s = '\0';
return nm;
}
}
}
return (char *) 0;
}