#ifndef lint
static char sccsid[] __attribute__((used)) = "@(#)mkheaders.c 5.5 (Berkeley) 6/18/88";
#endif
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include "config.h"
#include "parser.h"
static void do_count(const char *dev, const char *hname, int search);
static void do_header(const char *dev, const char *hname, int count);
static char *toheader(const char *dev);
static char *tomacro(const char *dev);
void
headers(void)
{
struct file_list *fl;
for (fl = ftab; fl != 0; fl = fl->f_next)
if (fl->f_needs != 0)
do_count(fl->f_needs, fl->f_needs, 1);
}
void
do_count(const char *dev, const char *hname, int search)
{
struct device *dp;
int count;
for (count = 0,dp = dtab; dp != 0; dp = dp->d_next)
if (eq(dp->d_name, dev)) {
if (dp->d_type == PSEUDO_DEVICE) {
count =
dp->d_slave != UNKNOWN ? dp->d_slave : 1;
if (dp->d_flags)
dev = NULL;
break;
}
}
do_header(dev, hname, count);
}
static void
do_header(const char *dev, const char *hname, int count)
{
char *file, *name;
const char *inw;
char *inwcopy;
struct file_list *fl = NULL;
struct file_list *fl_head, *fl_prev;
FILE *inf, *outf;
int inc, oldcount;
file = toheader(hname);
name = tomacro(dev?dev:hname) + (dev == NULL);
inf = fopen(file, "r");
oldcount = -1;
if (inf == 0) {
(void) unlink(file);
outf = fopen(file, "w");
if (outf == 0) {
perror(file);
exit(1);
}
fprintf(outf, "#define %s %d\n", name, count);
(void) fclose(outf);
file = path("meta_features.h");
outf = fopen(file, "a");
if (outf == 0) {
perror(file);
exit(1);
}
fprintf(outf, "#include <%s.h>\n", hname);
(void) fclose(outf);
return;
}
fl_head = 0;
for (;;) {
const char *cp;
if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
break;
if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
break;
inwcopy = ns(inw);
cp = get_word(inf);
if (cp == 0 || cp == (char *)EOF)
break;
inc = atoi(cp);
if (eq(inwcopy, name)) {
oldcount = inc;
inc = count;
}
cp = get_word(inf);
if (cp == (char *)EOF)
break;
fl = (struct file_list *) malloc(sizeof *fl);
fl->f_fn = inwcopy;
fl->f_type = inc;
fl->f_next = fl_head;
fl_head = fl;
}
(void) fclose(inf);
if (count == oldcount) {
while (fl !=0) {
fl_prev = fl;
fl = fl->f_next;
free((char *)fl_prev);
}
return;
}
if (oldcount == -1) {
fl = (struct file_list *) malloc(sizeof *fl);
fl->f_fn = name;
fl->f_type = count;
fl->f_next = fl_head;
fl_head = fl;
}
unlink(file);
outf = fopen(file, "w");
if (outf == 0) {
perror(file);
exit(1);
}
for (fl = fl_head; fl != 0; fl = fl->f_next) {
fprintf(outf, "#define %s %d\n",
fl->f_fn, count ? fl->f_type : 0);
free((char *)fl);
}
(void) fclose(outf);
}
static char *
toheader(const char *dev)
{
static char hbuf[MAXPATHLEN];
(void) snprintf(hbuf, sizeof hbuf, "%s.h", path(dev));
hbuf[MAXPATHLEN-1] = '\0';
return (hbuf);
}
static char *
tomacro(const char *dev)
{
static char mbuf[FILENAME_MAX];
char *cp;
cp = mbuf;
*cp++ = 'N';
while (*dev)
if (!islower(*dev))
*cp++ = *dev++;
else
*cp++ = toupper(*dev++);
*cp++ = 0;
return (mbuf);
}