ack/util/make/input.c

301 lines
6.4 KiB
C
Raw Permalink Normal View History

1988-07-22 19:01:13 +00:00
/*
* Parse a makefile
1988-07-22 19:01:13 +00:00
*
* $Header$
1988-07-22 19:01:13 +00:00
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
1988-07-22 19:01:13 +00:00
#include "h.h"
struct name namehead;
struct name * firstname;
1988-07-22 19:01:13 +00:00
char str1[LZ]; /* General store */
char str2[LZ];
1988-07-22 19:01:13 +00:00
/*
* Intern a name. Return a pointer to the name struct
1988-07-22 19:01:13 +00:00
*/
struct name *newname(char *name)
1988-07-22 19:01:13 +00:00
{
register struct name * rp;
register struct name * rrp;
register char * cp;
1988-07-22 19:01:13 +00:00
for (rp = namehead.n_next, rrp = &namehead; rp;
rp = rp->n_next, rrp = rrp->n_next)
1988-07-22 19:01:13 +00:00
if (strcmp(name, rp->n_name) == 0)
return rp;
if ((rp = (struct name *) malloc(sizeof(struct name))) == (struct name *) 0)
fatal("No memory for name",NULL);
1988-07-22 19:01:13 +00:00
rrp->n_next = rp;
rp->n_next = (struct name *) 0;
if ((cp = malloc((unsigned) (strlen(name) + 1))) == (char *) 0)
fatal("No memory for name",NULL);
1988-07-22 19:01:13 +00:00
strcpy(cp, name);
rp->n_name = cp;
rp->n_line = (struct line *) 0;
rp->n_time = (time_t) 0;
1988-07-22 19:01:13 +00:00
rp->n_flag = 0;
return rp;
}
/*
* Add a dependant to the end of the supplied list of dependants.
* Return the new head pointer for that list.
1988-07-22 19:01:13 +00:00
*/
struct depend *newdep(struct name *np, struct depend *dp)
1988-07-22 19:01:13 +00:00
{
register struct depend * rp;
register struct depend * rrp;
1988-07-22 19:01:13 +00:00
if ((rp = (struct depend *) malloc(sizeof(struct depend)))
== (struct depend *) 0)
fatal("No memory for dependant",NULL);
rp->d_next = (struct depend *) 0;
1988-07-22 19:01:13 +00:00
rp->d_name = np;
if (dp == (struct depend *) 0)
1988-07-22 19:01:13 +00:00
return rp;
for (rrp = dp; rrp->d_next; rrp = rrp->d_next)
;
rrp->d_next = rp;
return dp;
}
/*
* Add a command to the end of the supplied list of commands.
* Return the new head pointer for that list.
1988-07-22 19:01:13 +00:00
*/
struct cmd *newcmd(char *str, struct cmd *cp)
1988-07-22 19:01:13 +00:00
{
register struct cmd * rp;
register struct cmd * rrp;
register char * rcp;
1988-07-22 19:01:13 +00:00
if ((rcp = strrchr(str, '\n')))
*rcp = '\0'; /* Loose newline */
1988-07-22 19:01:13 +00:00
while (isspace(*str))
str++;
if (*str == '\0') /* If nothing left, the exit */
1988-07-22 19:01:13 +00:00
return 0;
if ((rp = (struct cmd *) malloc(sizeof(struct cmd))) == (struct cmd *) 0)
fatal("No memory for command",NULL);
rp->c_next = (struct cmd *) 0;
if ((rcp = malloc((unsigned) (strlen(str) + 1))) == (char *) 0)
fatal("No memory for command",NULL);
1988-07-22 19:01:13 +00:00
strcpy(rcp, str);
rp->c_cmd = rcp;
if (cp == (struct cmd *) 0)
1988-07-22 19:01:13 +00:00
return rp;
for (rrp = cp; rrp->c_next; rrp = rrp->c_next)
;
rrp->c_next = rp;
return cp;
}
/*
* Add a new 'line' of stuff to a target. This check to see
* if commands already exist for the target. If flag is set,
* the line is a double colon target.
1988-07-22 19:01:13 +00:00
*
* Kludges:
* i) If the new name begins with a '.', and there are no dependents,
* then the target must cease to be a target. This is for .SUFFIXES.
* ii) If the new name begins with a '.', with no dependents and has
* commands, then replace the current commands. This is for
* redefining commands for a default rule.
* Neither of these free the space used by dependents or commands,
* since they could be used by another target.
1988-07-22 19:01:13 +00:00
*/
void newline(struct name *np, struct depend *dp, struct cmd *cp, int flag)
1988-07-22 19:01:13 +00:00
{
bool hascmds = FALSE; /* Target has commands */
register struct line * rp;
register struct line * rrp;
1988-07-22 19:01:13 +00:00
/* Handle the .SUFFIXES case */
if (!strcmp(np->n_name, ".SUFFIXES") && !dp && !cp)
1988-07-22 19:01:13 +00:00
{
for (rp = np->n_line; rp; rp = rrp)
{
rrp = rp->l_next;
free((char *) rp);
1988-07-22 19:01:13 +00:00
}
np->n_line = (struct line *) 0;
np->n_flag &= (uchar)~N_TARG;
1988-07-22 19:01:13 +00:00
return;
}
/* This loop must happen since rrp is used later. */
for (rp = np->n_line, rrp = (struct line *) 0; rp;
rrp = rp, rp = rp->l_next)
1988-07-22 19:01:13 +00:00
if (rp->l_cmd)
hascmds = TRUE;
if (hascmds && cp && !(np->n_flag & N_DOUBLE))
/* Handle the implicit rules redefinition case */
if (np->n_name[0] == '.' && dp == (struct depend *) 0)
1988-07-22 19:01:13 +00:00
{
np->n_line->l_cmd = cp;
return;
}
else
error("Commands defined twice for target %s", np->n_name);
if (np->n_flag & N_TARG)
if (!(np->n_flag & N_DOUBLE) != !flag) /* like xor */
1988-07-22 19:01:13 +00:00
error("Inconsistent rules for target %s", np->n_name);
if ((rp = (struct line *) malloc(sizeof(struct line))) == (struct line *) 0)
fatal("No memory for line",NULL);
rp->l_next = (struct line *) 0;
1988-07-22 19:01:13 +00:00
rp->l_dep = dp;
rp->l_cmd = cp;
if (rrp)
rrp->l_next = rp;
else
np->n_line = rp;
np->n_flag |= N_TARG;
if (flag)
np->n_flag |= N_DOUBLE;
}
/*
* Parse input from the makefile, and construct a tree structure
* of it.
1988-07-22 19:01:13 +00:00
*/
void input(FILE *fd)
1988-07-22 19:01:13 +00:00
{
char * p; /* General */
char * q;
struct name * np;
struct depend * dp;
struct cmd * cp;
bool dbl;
if (mgetline(str1, fd)) /* Read the first line */
1988-07-22 19:01:13 +00:00
return;
for (;;)
1988-07-22 19:01:13 +00:00
{
if (*str1 == '\t') /* Rules without targets */
error("Rules not allowed here", NULL );
1988-07-22 19:01:13 +00:00
p = str1;
while (isspace(*p)) /* Find first target */
1988-07-22 19:01:13 +00:00
p++;
while (((q = strchr(p, '=')) != (char *) 0) && (p != q)
&& (q[-1] == '\\')) /* Find value */
1988-07-22 19:01:13 +00:00
{
register char * a;
1988-07-22 19:01:13 +00:00
a = q - 1; /* Del \ chr; move rest back */
1988-07-22 19:01:13 +00:00
p = q;
while ((*a++ = *q++))
1988-07-22 19:01:13 +00:00
;
}
if (q != (char *) 0)
1988-07-22 19:01:13 +00:00
{
register char * a;
1988-07-22 19:01:13 +00:00
*q++ = '\0'; /* Separate name and val */
1988-07-22 19:01:13 +00:00
while (isspace(*q))
q++;
if ((p = strrchr(q, '\n')))
1988-07-22 19:01:13 +00:00
*p = '\0';
p = str1;
if ((a = gettok(&p)) == (char *) 0)
error("No macro name", NULL );
1988-07-22 19:01:13 +00:00
setmacro(a, q, 2);
if (mgetline(str1, fd))
1988-07-22 19:01:13 +00:00
return;
continue;
}
expand(str1);
p = str1;
while (((q = strchr(p, ':')) != (char *) 0) && (p != q)
&& (q[-1] == '\\')) /* Find dependents */
1988-07-22 19:01:13 +00:00
{
register char * a;
1988-07-22 19:01:13 +00:00
a = q - 1; /* Del \ chr; move rest back */
1988-07-22 19:01:13 +00:00
p = q;
while ((*a++ = *q++))
1988-07-22 19:01:13 +00:00
;
}
if (q == (char *) 0)
error("No targets provided", NULL );
1988-07-22 19:01:13 +00:00
*q++ = '\0'; /* Separate targets and dependents */
1988-07-22 19:01:13 +00:00
if (*q == ':') /* Double colon */
1988-07-22 19:01:13 +00:00
{
dbl = 1;
q++;
}
else
dbl = 0;
for (dp = (struct depend *) 0; ((p = gettok(&q)) != (char *) 0);)
/* get list of dep's */
1988-07-22 19:01:13 +00:00
{
np = newname(p); /* Intern name */
dp = newdep(np, dp); /* Add to dep list */
1988-07-22 19:01:13 +00:00
}
*((q = str1) + strlen(str1) + 1) = '\0';
/* Need two nulls for gettok (Remember separation) */
1988-07-22 19:01:13 +00:00
cp = (struct cmd *) 0;
if (mgetline(str2, fd) == FALSE) /* Get commands */
1988-07-22 19:01:13 +00:00
{
while (*str2 == '\t')
{
cp = newcmd(&str2[0], cp);
if (mgetline(str2, fd))
1988-07-22 19:01:13 +00:00
break;
}
}
while ((p = gettok(&q)) != (char *) 0) /* Get list of targ's */
1988-07-22 19:01:13 +00:00
{
np = newname(p); /* Intern name */
1988-07-22 19:01:13 +00:00
newline(np, dp, cp, dbl);
if (!firstname && p[0] != '.')
firstname = np;
}
if (feof(fd)) /* EOF? */
1988-07-22 19:01:13 +00:00
return;
strcpy(str1, str2);
}
}