commit - fa71e446879f1e5ba4dca2913d4e89198f60cbd3
commit + 0a03edcc6fe538f35b9724e03d3bfc65cbf88e7b
blob - 3c1abe33f1000494febea44bb9643b5e74ea3516
blob + c52619858262af4e19a93002894418c2e2736ccb
--- make/make.c
+++ make/make.c
/* MACRO EXPANSION */
struct expand_ctx {
- struct path *prefix;
char *target;
struct dep *deps, *infdeps;
struct dep *dep0;
int free_target;
};
-ectx_init (ctx, prefix, target, dep0, deps, infdeps)
+ectx_init (ctx, target, dep0, deps, infdeps)
struct expand_ctx *ctx;
-struct path *prefix;
char *target;
struct dep *dep0;
struct dep *deps, *infdeps;
{
- ctx->prefix = prefix;
ctx->target = target;
ctx->dep0 = dep0;
ctx->deps = deps;
return 0;
}
-ectx_file (ctx, sc, prefix, f)
+ectx_file (ctx, sc, f)
struct expand_ctx *ctx;
struct scope *sc;
-struct path *prefix;
struct file *f;
{
str_t tmp;
- ctx->prefix = prefix;
if (objdir != NULL) {
str_new (&tmp);
write_objdir (&tmp, sc);
return 0;
}
-pr_export (out, sc, m, ctx)
+pr_export (out, sc, prefix, m, ctx)
str_t *out;
struct scope *sc;
+struct path *prefix;
struct macro *m;
struct expand_ctx *ctx;
{
str_puts (out, m->name);
str_puts (out, "='");
- expand_macro_into (out, sc, m, m->name, ctx);
+ expand_macro_into (out, sc, prefix, m, m->name, ctx);
str_putc (out, '\'');
return 0;
}
return 0;
}
-expand_special_into (out, sc, name, ctx)
+expand_special_into (out, sc, prefix, name, ctx)
str_t *out;
struct scope *sc;
+struct path *prefix;
char *name;
struct expand_ctx *ctx;
{
if (m == NULL)
return 0;
- pr_export (out, sc, m, ctx);
+ pr_export (out, sc, prefix, m, ctx);
for (m = m->enext; m != NULL; m = m->enext) {
str_putc (out, ' ');
- pr_export (out, sc, m, ctx);
+ pr_export (out, sc, prefix, m, ctx);
}
} else if (strcmp (name, ".OBJDIR") == 0) {
* ${name:F} try searching for files in either ${.OBJDIR} or source directory
* ${name:m1:m2...} multiple modifiers can be combined
*/
-subst2 (out, sc, s, ctx)
+subst2 (out, sc, prefix, s, ctx)
str_t *out;
struct scope *sc;
+struct path *prefix;
char **s;
struct expand_ctx *ctx;
{
++*s;
} else if (**s == '$') {
++*s;
- subst (&name, sc, s, ctx);
+ subst (&name, sc, prefix, s, ctx);
} else {
break;
}
m = find_macro (sc, str_get (&name));
if (**s == '}') {
++*s;
- expand_macro_into (out, sc, m, str_get (&name), ctx);
+ expand_macro_into (out, sc, prefix, m, str_get (&name), ctx);
str_free (&name);
return 0;
}
- v = expand_macro (sc, m, str_get (&name), ctx);
+ v = expand_macro (sc, prefix, m, str_get (&name), ctx);
str_free (&name);
str_new (&old);
switch (**s) {
case '$':
++*s;
- subst (&old, sc, s, ctx);
+ subst (&old, sc, prefix, s, ctx);
break;
case '\\':
++*s;
switch (**s) {
case '$':
++*s;
- subst (&new, sc, s, ctx);
+ subst (&new, sc, prefix, s, ctx);
break;
case '\\':
++*s;
if (*w == '\0')
continue;
- if (get_mtime (&ft, sc, ctx->prefix, w) == 0 && ft.obj) {
+ if (get_mtime (&ft, sc, prefix, w) == 0 && ft.obj) {
write_objdir (&new, sc);
str_putc (&new, '/');
}
errx (1, "%s: invalid macro expansion: '${%s', s = '%s'", sc_path_str (sc), orig, *s);
}
-subst (out, sc, s, ctx)
+subst (out, sc, prefix, s, ctx)
str_t *out;
struct scope *sc;
+struct path *prefix;
char **s;
struct expand_ctx *ctx;
{
str_putc (out, '$');
break;
case '.':
- expand_special_into (out, sc, ".TOPDIR", ctx);
+ expand_special_into (out, sc, prefix, ".TOPDIR", ctx);
break;
case '@':
- expand_special_into (out, sc, ".TARGET", ctx);
+ expand_special_into (out, sc, prefix, ".TARGET", ctx);
break;
case '<':
- expand_special_into (out, sc, ".IMPSRC", ctx);
+ expand_special_into (out, sc, prefix, ".IMPSRC", ctx);
break;
case '^':
- expand_special_into (out, sc, ".ALLSRC", ctx);
+ expand_special_into (out, sc, prefix, ".ALLSRC", ctx);
break;
case '{':
- subst2 (out, sc, s, ctx);
+ subst2 (out, sc, prefix, s, ctx);
break;
case '(':
errx (1, "%s: syntax error: $(...) syntax is reserved for future use, please use ${...} instead.", sc_path_str (sc));
return 0;
}
-expand_into (out, sc, s, ctx)
+expand_into (out, sc, prefix, s, ctx)
str_t *out;
struct scope *sc;
+struct path *prefix;
char *s;
struct expand_ctx *ctx;
{
continue;
}
++s;
- subst (out, sc, &s, ctx);
+ subst (out, sc, prefix, &s, ctx);
}
return 0;
}
-expand_macro_into (out, sc, m, name, ctx)
+expand_macro_into (out, sc, prefix, m, name, ctx)
str_t *out;
struct scope *sc;
+struct path *prefix;
struct macro *m;
char *name;
struct expand_ctx *ctx;
{
if (m == NULL)
- return expand_special_into (out, sc, name, ctx);
+ return expand_special_into (out, sc, prefix, name, ctx);
if (m->prepend != NULL) {
- expand_macro_into (out, sc, m->prepend, m->prepend->name, ctx);
+ expand_macro_into (out, sc, prefix, m->prepend, m->prepend->name, ctx);
str_putc (out, ' ');
}
return 0;
if (m->lazy) {
- expand_into (out, sc, m->value, ctx);
+ expand_into (out, sc, prefix, m->value, ctx);
} else {
str_puts (out, m->value);
}
}
char *
-expand_macro (sc, m, name, ctx)
+expand_macro (sc, prefix, m, name, ctx)
struct scope *sc;
+struct path *prefix;
struct macro *m;
char *name;
struct expand_ctx *ctx;
str_t tmp;
str_new (&tmp);
- expand_macro_into (&tmp, sc, m, name, ctx);
+ expand_macro_into (&tmp, sc, prefix, m, name, ctx);
return str_release (&tmp);
}
char *
-expand (sc, s, ctx)
+expand (sc, prefix, s, ctx)
struct scope *sc;
+struct path *prefix;
char *s;
struct expand_ctx *ctx;
{
str_t out;
str_new (&out);
- expand_into (&out, sc, s, ctx);
+ expand_into (&out, sc, prefix, s, ctx);
str_trim (&out);
return str_release (&out);
}
char *args[] = {
m_shell.value,
"-c",
- expand (sc, cmd, NULL),
+ expand (sc, dir, cmd, NULL),
NULL,
};
ssize_t i, n;
q = 1;
}
- ecmd = expand (sc, cmd, ctx);
+ ecmd = expand (sc, prefix, cmd, ctx);
if (!q) {
printf ("[%s%s%s] $ %s\n",
return *endp != '\0' || x != 0;
}
-e_command (sc, s, cmd, arg)
+e_command (sc, prefix, s, cmd, arg)
struct scope *sc;
+struct path *prefix;
char **s, *cmd;
str_t *arg;
{
return 0;
}
-e_atom (sc, s, val)
+e_atom (sc, prefix, s, val)
struct scope *sc;
+struct path *prefix;
char **s;
str_t *val;
{
while (**s != '"') {
if (**s == '$') {
++*s;
- subst (val, sc, s, NULL);
+ subst (val, sc, prefix, s, NULL);
} else {
str_putc (val, **s);
++*s;
}
++*s;
} else if (starts_with (*s, "defined")) {
- e_command (sc, s, "defined", &arg);
+ e_command (sc, prefix, s, "defined", &arg);
x = find_macro (sc, str_get (&arg)) != NULL;
comm:
str_putc (val, x ? '1' : '0');
str_free (&arg);
} else if (starts_with (*s, "target")) {
- e_command (sc, s, "target", &arg);
+ e_command (sc, prefix, s, "target", &arg);
x = find_file (sc->dir, str_get (&arg)) != NULL;
goto comm;
} else {
return 0;
}
-e_unary (sc, s, val)
+e_unary (sc, prefix, s, val)
struct scope *sc;
+struct path *prefix;
char **s;
str_t *val;
{
skip_ws (s);
if (**s != '!')
- return e_atom (sc, s, val);
+ return e_atom (sc, prefix, s, val);
++*s;
- e_unary (sc, s, val);
+ e_unary (sc, prefix, s, val);
if (is_truthy (str_get (val))) {
str_reset (val);
COMP_GE,
};
-e_comp (sc, s)
+e_comp (sc, prefix, s)
struct scope *sc;
+struct path *prefix;
char **s;
{
str_t left, right;
int cmp, x, icmp;
str_new (&left);
- e_unary (sc, s, &left);
+ e_unary (sc, prefix, s, &left);
skip_ws (s);
skip_ws (s);
str_new (&right);
- e_unary (sc, s, &right);
+ e_unary (sc, prefix, s, &right);
str_trim (&left);
str_trim (&right);
return x;
}
-e_and (sc, s)
+e_and (sc, prefix, s)
struct scope *sc;
+struct path *prefix;
char **s;
{
int x;
- x = e_comp (sc, s);
+ x = e_comp (sc, prefix, s);
while (skip_ws (s), starts_with (*s, "&&")) {
*s += 2;
- x &= e_comp (sc, s);
+ x &= e_comp (sc, prefix, s);
}
return x;
}
-e_or (sc, s)
+e_or (sc, prefix, s)
struct scope *sc;
+struct path *prefix;
char **s;
{
int x;
- x = e_and (sc, s);
+ x = e_and (sc, prefix, s);
while (skip_ws (s), starts_with (*s, "||")) {
*s += 2;
- x |= e_and (sc, s);
+ x |= e_and (sc, prefix, s);
}
return x;
}
-parse_expr (sc, s)
+parse_expr (sc, prefix, s)
struct scope *sc;
+struct path *prefix;
char *s;
{
s = trim (s);
- return e_or (sc, &s);
+ return e_or (sc, prefix, &s);
}
/* PARSER */
*t = '\0';
/* parse deps */
- v = u = expand (sc, t + 1, NULL);
+ v = u = expand (sc, dir, t + 1, NULL);
while ((p = strsep (&v, " \t")) != NULL) {
if (*p == '\0')
continue;
free (u);
/* parse targets */
- u = expand (sc, s, NULL);
+ u = expand (sc, dir, s, NULL);
flag = 1;
if (is_inf (u)) {
p = strchr (u + 1, '.');
/* handle both `:=` and `::=` */
t[t[-2] == ':' ? -2 : -1] = '\0';
m->lazy = 0;
- m->value = expand (sc, trim (t + 1), NULL);
+ m->value = expand (sc, dir, trim (t + 1), NULL);
} else if (t[-1] == '+') {
t[-1] = '\0';
m->value = strdup (trim (t + 1));
for (; (s = readline (file, &cline)) != NULL; free (s)) {
run = walkifstack (ifstack, iflen);
if (s[0] == '#' && s[1] == '#') {
- help = expand (sc, trim (s + 2), NULL);
+ help = expand (sc, dir, trim (s + 2), NULL);
continue;
} else if (s[0] == '#' || *trim (s) == '\0') {
continue;
if (!run)
goto cont;
- t = expand (sc, s + 8, NULL);
+ t = expand (sc, dir, s + 8, NULL);
if (*t == '/') {
u = t;
} else {
} else if (is_directive (&t, s, "if")) {
if (iflen == MAX_IFSTACK)
errx (1, "%s:%d: maximum .if depth of %d reached", path, cline, MAX_IFSTACK);
- x = parse_expr (sc, t) & 0x01;
+ x = parse_expr (sc, dir, t) & 0x01;
ifstack[iflen++] = x * (IF_VAL | IF_HAS);
} else if (is_directive (NULL, s, "else")) {
if (iflen == 0)
} else if (is_directive (&t, s, "elif")) {
if (iflen == 0)
errx (1, "%s:%d: not in .if", path, cline);
- x = parse_expr (sc, t);
+ x = parse_expr (sc, dir, t);
t = &ifstack[iflen - 1];
*t = ((!(*t & IF_HAS) && x) * (IF_VAL | IF_HAS)) | (*t & IF_HAS);
} else if (is_directive (NULL, s, "endif")) {
}
/* run commands */
- ectx_file (&ctx, sc, prefix, f);
+ ectx_file (&ctx, sc, f);
for (; *s != NULL; ++s) {
if (runcom (sc, prefix, *s, &ctx, name) != 0) {
fprintf (stderr, "%s: command failed: %s\n", sc_path_str (sc), *s);
ectx_init (
/* ctx */ &ctx,
- /* prefix */ new_prefix,
/* target */ prefix[path_len (prefix) - 1].name,
/* dep0 */ name != NULL ? &xdep : NULL,
/* deps */ f->dhead,
ectx_init (
/* ctx */ &ctx,
- /* prefix */ new_prefix,
/* target */ prefix[path_len (prefix) - 1].name,
/* dep0 */ name != NULL ? &xdep : NULL,
/* deps */ f->dhead,
s[len + 3] = '\0';
}
- puts (expand (sc, s, NULL));
+ puts (expand (sc, &path_null, s, NULL));
return 0;
}