1988-07-22 19:01:13 +00:00
|
|
|
/*
|
2019-02-18 16:39:39 +00:00
|
|
|
* Macro control for make
|
1988-07-22 19:01:13 +00:00
|
|
|
*
|
2019-02-18 16:39:39 +00:00
|
|
|
* $Header$
|
1988-07-22 19:01:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2019-02-18 16:39:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1988-07-22 19:01:13 +00:00
|
|
|
#include "h.h"
|
|
|
|
|
|
|
|
|
2019-02-18 16:39:39 +00:00
|
|
|
struct macro * macrohead;
|
1988-07-22 19:01:13 +00:00
|
|
|
|
|
|
|
|
2019-02-18 16:39:39 +00:00
|
|
|
static struct macro *getmp(char *name)
|
1988-07-22 19:01:13 +00:00
|
|
|
{
|
2019-02-18 16:39:39 +00:00
|
|
|
register struct macro * rp;
|
1988-07-22 19:01:13 +00:00
|
|
|
|
2019-02-18 16:39:39 +00:00
|
|
|
for (rp = macrohead; rp; rp = rp->m_next)
|
|
|
|
if (strcmp(name, rp->m_name) == 0)
|
|
|
|
return rp;
|
|
|
|
return (struct macro *)0;
|
1988-07-22 19:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-18 16:39:39 +00:00
|
|
|
char * getmacro(char* name)
|
1988-07-22 19:01:13 +00:00
|
|
|
{
|
2019-02-18 16:39:39 +00:00
|
|
|
struct macro * mp;
|
1988-07-22 19:01:13 +00:00
|
|
|
|
2019-02-18 16:39:39 +00:00
|
|
|
if ((mp = getmp(name)))
|
|
|
|
return mp->m_val;
|
|
|
|
else
|
|
|
|
return "";
|
1988-07-22 19:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-18 16:39:39 +00:00
|
|
|
struct macro * setmacro(char* name, char* val, int prio)
|
1988-07-22 19:01:13 +00:00
|
|
|
{
|
2019-02-18 16:39:39 +00:00
|
|
|
register struct macro * rp;
|
|
|
|
register char * cp;
|
|
|
|
|
|
|
|
|
|
|
|
/* Replace macro definition if it exists */
|
|
|
|
for (rp = macrohead; rp; rp = rp->m_next)
|
|
|
|
if (strcmp(name, rp->m_name) == 0)
|
|
|
|
{
|
|
|
|
if (prio < rp->m_prio)
|
|
|
|
return rp;
|
|
|
|
free(rp->m_val); /* Free space from old */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rp) /* If not defined, allocate space for new */
|
|
|
|
{
|
|
|
|
if ((rp = (struct macro *)malloc(sizeof (struct macro)))
|
|
|
|
== (struct macro *)0)
|
|
|
|
fatal("No memory for macro",NULL);
|
|
|
|
|
|
|
|
rp->m_next = macrohead;
|
|
|
|
macrohead = rp;
|
|
|
|
rp->m_flag = FALSE;
|
|
|
|
|
|
|
|
if ((cp = malloc((unsigned)(strlen(name)+1))) == (char *)0)
|
|
|
|
fatal("No memory for macro",NULL);
|
|
|
|
strcpy(cp, name);
|
|
|
|
rp->m_name = cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((cp = malloc((unsigned)(strlen(val)+1))) == (char *)0)
|
|
|
|
fatal("No memory for macro",NULL);
|
|
|
|
strcpy(cp, val); /* Copy in new value */
|
|
|
|
rp->m_val = cp;
|
|
|
|
rp->m_prio = (uchar)prio;
|
|
|
|
|
|
|
|
return rp;
|
1988-07-22 19:01:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:39 +00:00
|
|
|
#define MBUFSIZ 128
|
1988-07-22 19:01:13 +00:00
|
|
|
|
|
|
|
/*
|
2019-02-18 16:39:39 +00:00
|
|
|
* Do the dirty work for expand
|
1988-07-22 19:01:13 +00:00
|
|
|
*/
|
2019-02-18 16:39:39 +00:00
|
|
|
static void doexp(char **to, char* from, int* len, char* buf)
|
1988-07-22 19:01:13 +00:00
|
|
|
{
|
2019-02-18 16:39:39 +00:00
|
|
|
register char * rp;
|
|
|
|
register char * p;
|
|
|
|
register char * q;
|
|
|
|
register struct macro * mp;
|
|
|
|
|
|
|
|
|
|
|
|
rp = from;
|
|
|
|
p = *to;
|
|
|
|
while (*rp)
|
|
|
|
{
|
|
|
|
if (*rp != '$')
|
|
|
|
{
|
|
|
|
*p++ = *rp++;
|
|
|
|
(*len)--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
q = buf;
|
|
|
|
if (*++rp == '{')
|
|
|
|
while (*++rp && *rp != '}') {
|
|
|
|
if (q < &buf[MBUFSIZ-1]) *q++ = *rp;
|
|
|
|
}
|
|
|
|
else if (*rp == '(')
|
|
|
|
while (*++rp && *rp != ')') {
|
|
|
|
if (q < &buf[MBUFSIZ-1]) *q++ = *rp;
|
|
|
|
}
|
|
|
|
else if (!*rp)
|
|
|
|
{
|
|
|
|
*p++ = '$';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*q++ = *rp;
|
|
|
|
*q = '\0';
|
|
|
|
if (*rp)
|
|
|
|
rp++;
|
|
|
|
if (!(mp = getmp(buf)))
|
|
|
|
mp = setmacro(buf, "", 2);
|
|
|
|
if (mp->m_flag)
|
|
|
|
fatal("Infinitely recursive macro %s", mp->m_name);
|
|
|
|
mp->m_flag = TRUE;
|
|
|
|
*to = p;
|
|
|
|
doexp(to, mp->m_val, len, buf);
|
|
|
|
p = *to;
|
|
|
|
mp->m_flag = FALSE;
|
|
|
|
}
|
|
|
|
if (*len <= 0)
|
|
|
|
error("Expanded line too line", NULL);
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
*to = p;
|
1988-07-22 19:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2019-02-18 16:39:39 +00:00
|
|
|
* Expand any macros in str.
|
1988-07-22 19:01:13 +00:00
|
|
|
*/
|
2019-02-18 16:39:39 +00:00
|
|
|
void expand(char *str)
|
1988-07-22 19:01:13 +00:00
|
|
|
{
|
2019-02-18 16:39:39 +00:00
|
|
|
char *a;
|
|
|
|
static char b[MBUFSIZ]; /* temp storage for macroname */
|
|
|
|
char * p = str;
|
|
|
|
int len = LZ-1;
|
|
|
|
|
|
|
|
a = malloc((unsigned)(strlen(str)+1));
|
|
|
|
if (!a) fatal("No memory for expand",NULL);
|
|
|
|
strcpy(a, str);
|
|
|
|
doexp(&p, a, &len, b);
|
|
|
|
free(a);
|
1988-07-22 19:01:13 +00:00
|
|
|
}
|