aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-02-10 11:04:31 +0100
committerMattias Andrée <maandree@kth.se>2024-02-10 11:09:33 +0100
commit12f1e3ad8cd3d6915bfd32274244a800d8d3347b (patch)
tree8972c6b08f8269881313c0fb2c2f8f9f40d5f2df
parentFirst commit (diff)
downloadmakeenv-12f1e3ad8cd3d6915bfd32274244a800d8d3347b.tar.gz
makeenv-12f1e3ad8cd3d6915bfd32274244a800d8d3347b.tar.bz2
makeenv-12f1e3ad8cd3d6915bfd32274244a800d8d3347b.tar.xz
Fix bugs, and some cleanup
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--TODO5
-rw-r--r--makeenv.c109
2 files changed, 57 insertions, 57 deletions
diff --git a/TODO b/TODO
index a205668..9b957b4 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,7 @@
Write makeenv.1
Write makeenv.5
Write README
-Test
+
+Bugs:
+ Options in MAKEENV_OPTS_OPT_ARG and MAKEENV_LONG_OPTS_OPT_ARG can
+ steal argument from .makeenv if it doesn't have any argument
diff --git a/makeenv.c b/makeenv.c
index a091b49..f71bc00 100644
--- a/makeenv.c
+++ b/makeenv.c
@@ -31,18 +31,22 @@ put_option(const char *s)
exit(125);
}
}
- options[noptions] = s;
+ options[noptions++] = s;
}
static void
put_option_short(int c)
{
- char buf[3];
- buf[0] = '-';
- buf[1] = (char)c;
- buf[2] = '\0';
- put_option(buf);
+ char *opt = malloc(3);
+ if (!opt) {
+ fprintf(stderr, "%s: failed to allocate enough memory to load .makeenv\n", argv0);
+ exit(125);
+ }
+ opt[0] = '-';
+ opt[1] = (char)c;
+ opt[2] = '\0';
+ put_option(opt);
}
@@ -92,7 +96,7 @@ put_macro(const char *s)
exit(125);
}
}
- macros[nmacros] = s;
+ macros[nmacros++] = s;
}
@@ -107,7 +111,7 @@ put_target(const char *s)
exit(125);
}
}
- targets[ntargets] = s;
+ targets[ntargets++] = s;
}
@@ -197,41 +201,43 @@ create_cmdline(void)
}
-static void
+static char *
trim(char *s)
{
- size_t key_start, key_end;
- size_t val_start, val_end;
- size_t i, n;
-
- key_start = 0;
- while (isspace(s[key_start]))
- key_start += 1;
+ size_t i, last = (size_t)-1;
+ while (isspace(*s))
+ s++;
+ for (i = 0; s[i]; i++)
+ if (!isspace(s[i]))
+ last = i;
+ s[last + 1] = '\0';
+ return s;
+}
- key_end = key_start;
- while (s[key_end] != '=')
- key_end += 1;
- val_start = key_end + 1;
+static void
+trim_around_equals(char *s)
+{
+ char *equals = strchr(s, '=');
+ char *p = equals, *val = &equals[1];
- while (key_end > key_start && isspace(s[key_end - 1]))
- key_end -= 1;
+ while (p != s && isspace(p[-1]))
+ p--;
+ *p++ = '=';
- while (isspace(s[val_start]))
- key_start += 1;
+ while (isspace(*val))
+ val++;
- val_end = val_start;
- for (i = val_end; s[i]; i++)
- if (!isspace(s[i]))
- val_end = i;
+ if (val != p)
+ stpcpy(p, val);
+}
- memmove(s, &s[key_start], key_end - key_start);
- n = key_end - key_start;
- s[n++] = '=';
- memmove(&s[n], &s[val_start], val_end - val_start);
- n += val_end - val_start;
- s[n] = '\0';
+static const char *
+get(const char *var, const char *def)
+{
+ const char *ret = getenv(var);
+ return ret ? ret : def;
}
@@ -248,7 +254,6 @@ main(int argc, char *argv[])
int has_equals;
const char **args;
const char *arg;
- const char *make;
const char *unarged_opts;
const char *arged_opts;
const char *optatarged_opts;
@@ -307,11 +312,12 @@ main(int argc, char *argv[])
for (i = 0; i < envlen; i++) {
if (env[i] == '\n') {
env[i] = '\0';
- trim(line);
+ line = trim(line);
if (line[0] == '-' && line[1]) {
put_options(line);
} else if (has_equals) {
- if (*line && *line != '=' && *line != '#')
+ trim_around_equals(line);
+ if (*line != '=' && *line != '#')
put_macro(line);
} else {
if (*line && *line != '#')
@@ -326,24 +332,17 @@ main(int argc, char *argv[])
close(fd);
- env = getenv("MAKEENV_OPTS_NO_ARG");
- unarged_opts = env ? env : "eiknpqrSst";
- env = getenv("MAKEENV_OPTS_ARG");
- arged_opts = env ? env : "f";
- env = getenv("MAKEENV_OPTS_OPT_ATTACHED_ARG");
- optatarged_opts = env ? env : "";
- env = getenv("MAKEENV_OPTS_OPT_ARG");
- optarged_opts = env ? env : "";
- env = getenv("MAKEENV_LONG_OPTS_NO_ARG");
- unarged_longopts = env ? env : "";
- env = getenv("MAKEENV_LONG_OPTS_ARG");
- arged_longopts = env ? env : "";
- env = getenv("MAKEENV_LONG_OPTS_OPT_ARG");
- optarged_longopts = env ? env : "";
+ unarged_opts = get("MAKEENV_OPTS_NO_ARG", "eiknpqrSst");
+ arged_opts = get("MAKEENV_OPTS_ARG", "f");
+ optatarged_opts = get("MAKEENV_OPTS_OPT_ATTACHED_ARG", "");
+ optarged_opts = get("MAKEENV_OPTS_OPT_ARG", "");
+ unarged_longopts = get("MAKEENV_LONG_OPTS_NO_ARG", "");
+ arged_longopts = get("MAKEENV_LONG_OPTS_ARG", "");
+ optarged_longopts = get("MAKEENV_LONG_OPTS_OPT_ARG", "");
for (; *argv; argv++) {
if (!strcmp(*argv, "--")) {
- argv++;
+ put_operand(*argv++);
break;
} else if ((*argv)[0] != '-' || !(*argv)[1]) {
/* do not break, as make(1) allows mixing options and operands */
@@ -414,15 +413,13 @@ main(int argc, char *argv[])
}
}
- while (*argv)
+ for (; *argv; argv++)
put_operand(*argv);
args = create_cmdline();
exec:
- make = getenv("MAKEENV_MAKE");
- make = make ? make : getenv("MAKE");
- args[0] = make ? make : "make";
+ args[0] = get("MAKEENV_MAKE", get("MAKE", "make"));
execvp(args[0], (void *)args);
exitstatus = errno == ENOENT ? 127 : 126;
fprintf(stderr, "%s: failed to execute %s: %s\n", argv0, args[0], strerror(errno));