commit 4c5f04570009f42572050c8e1960d74c8fec38ea
parent 452ab77014edd523564b76cfe58b411b832ac12f
Author: xplshn <anto@xplshn.com.ar>
Date: Mon, 23 Nov 2026 13:44:06 -0300
make, head: what is bmake/gmake compat or gnu is gated, FEATURE_MAKE_COMPAT and FEATURE_HEAD_NEGATIVE
Diffstat:
5 files changed, 140 insertions(+), 40 deletions(-)
diff --git a/build.cfg b/build.cfg
@@ -35,6 +35,7 @@ posix)
FEATURE_STAT_FILESYSTEM=0; FEATURE_STAT_FORMAT=0
FEATURE_SORT_BIG=0; FEATURE_SORT_STABLE=0
FEATURE_OD_ENDIAN=0;
+ FEATURE_HEAD_NEGATIVE=0; FEATURE_MAKE_COMPAT=0
FEATURE_SH_EASTER=0; FEATURE_SH_HISTEDIT=0;
FEATURE_SH_LOCAL=0 FEATURE_SH_LET=0;
FEATURE_SH_ULIMIT=0 FEATURE_SH_SETVAR=0;
@@ -76,6 +77,7 @@ common)
FEATURE_STAT_FILESYSTEM=1; FEATURE_STAT_FORMAT=1
FEATURE_SORT_BIG=0; FEATURE_SORT_STABLE=1
FEATURE_OD_ENDIAN=0;
+ FEATURE_HEAD_NEGATIVE=1; FEATURE_MAKE_COMPAT=1
FEATURE_SH_EASTER=1; FEATURE_SH_HISTEDIT=0;
FEATURE_SH_LOCAL=1; FEATURE_SH_LET=1;
FEATURE_SH_ULIMIT=1; FEATURE_SH_SETVAR=0;
@@ -116,6 +118,7 @@ all)
FEATURE_STAT_FILESYSTEM=1; FEATURE_STAT_FORMAT=1
FEATURE_SORT_BIG=1; FEATURE_SORT_STABLE=1
FEATURE_OD_ENDIAN=1;
+ FEATURE_HEAD_NEGATIVE=1; FEATURE_MAKE_COMPAT=1
FEATURE_SH_EASTER=1; FEATURE_SH_HISTEDIT=1
FEATURE_SH_LOCAL=1; FEATURE_NOEXEC=0;
FEATURE_NOFORK=0; FEATURE_SH_LET=1;
diff --git a/cmd/posix/head.c b/cmd/posix/head.c
@@ -5,23 +5,36 @@
#include <stdlib.h>
#include <string.h>
+#include "config.h"
#include "util.h"
-// with drop set, everything but the last n lines: a line is held back
-// until n others came after it
static void
-head(FILE *fp, const char *fname, size_t n, int drop)
+head(FILE *fp, const char *fname, size_t n)
+{
+ char *buf = NULL;
+ size_t i = 0, size = 0;
+ ssize_t len;
+
+ while (i < n && (len = getline(&buf, &size, fp)) > 0) {
+ fwrite(buf, 1, len, stdout);
+ i += (len && (buf[len - 1] == '\n'));
+ }
+ free(buf);
+ if (ferror(fp))
+ eprintf("getline %s:", fname);
+}
+
+#if FEATURE_HEAD_NEGATIVE
+// gnu extension, -n -N: everything but the last n lines. a line is held
+// back until n others came after it
+static void
+head_but(FILE *fp, const char *fname, size_t n)
{
char *buf = NULL, **held = NULL;
- size_t i = 0, size = 0, nheld = 0, at = 0;
+ size_t i, size = 0, nheld = 0, at = 0;
ssize_t len;
- while ((drop || i < n) && (len = getline(&buf, &size, fp)) > 0) {
- if (!drop) {
- fwrite(buf, 1, len, stdout);
- i += (len && (buf[len - 1] == '\n'));
- continue;
- }
+ while ((len = getline(&buf, &size, fp)) > 0) {
if (nheld < n) {
held = ereallocarray(held, nheld + 1, sizeof(*held));
held[nheld++] = estrdup(buf);
@@ -39,11 +52,12 @@ head(FILE *fp, const char *fname, size_t n, int drop)
if (ferror(fp))
eprintf("getline %s:", fname);
}
+#endif
static void
usage(void)
{
- eprintf("usage: %s [-num | -n [-]num] [file ...]\n", argv0);
+ eprintf("usage: %s [-num | -n num] [file ...]\n", argv0);
}
// ?man head: output first part of files
@@ -54,18 +68,27 @@ main(int argc, char *argv[])
{
size_t n = 10;
FILE *fp;
+ int ret = 0, newline = 0, many = 0;
+#if FEATURE_HEAD_NEGATIVE
char *arg;
- int ret = 0, newline = 0, many = 0, drop = 0;
+ int drop = 0;
+#endif
ARGBEGIN
{
// ?man -n:num: print line numbers or counts
case 'n':
+#if FEATURE_HEAD_NEGATIVE
arg = EARGF(usage());
- drop = arg[0] == '-';
+ drop = arg[0] == '-' && arg[1];
n = estrtonum(
arg + drop, 0, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX)
);
+#else
+ n = estrtonum(
+ EARGF(usage()), 0, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX)
+ );
+#endif
break;
// ?man ARGNUM: specify RGNUM option
ARGNUM:
@@ -76,14 +99,21 @@ main(int argc, char *argv[])
}
ARGEND
+#if FEATURE_HEAD_NEGATIVE
// dropping none is printing all
if (drop && !n) {
drop = 0;
n = SIZE_MAX;
}
+#endif
if (!argc) {
- head(stdin, "<stdin>", n, drop);
+#if FEATURE_HEAD_NEGATIVE
+ if (drop)
+ head_but(stdin, "<stdin>", n);
+ else
+#endif
+ head(stdin, "<stdin>", n);
} else {
many = argc > 1;
for (newline = 0; *argv; argc--, argv++) {
@@ -101,7 +131,12 @@ main(int argc, char *argv[])
printf("==> %s <==\n", *argv);
}
newline = 1;
- head(fp, *argv, n, drop);
+#if FEATURE_HEAD_NEGATIVE
+ if (drop)
+ head_but(fp, *argv, n);
+ else
+#endif
+ head(fp, *argv, n);
if (fp != stdin && fshut(fp, *argv))
ret = 1;
}
diff --git a/cmd/posix/make/main.c b/cmd/posix/make/main.c
@@ -6,6 +6,7 @@
#include <stdlib.h>
#include <string.h>
+#include "config.h"
#include "make.h"
#ifndef SIGINT
@@ -149,16 +150,22 @@ hasargs(int c)
return c == 'f' || c == 'j';
}
-// a bare -j, with nothing after it or something that is not a count, asks
-// for as many jobs as there can be and takes no argument
+// bmake/gmake compat: a bare -j, with nothing after it or something that is
+// not a count, asks for as many jobs as there can be and takes no argument
static int
takesarg(int c, char **args, char ***argv)
{
- if (c != 'j')
- return hasargs(c);
- if ((*args)[1])
- return 1;
- return argv && (*argv)[1] && isdigit((unsigned char)(*argv)[1][0]);
+#if FEATURE_MAKE_COMPAT
+ if (c == 'j') {
+ if ((*args)[1])
+ return 1;
+ return argv && (*argv)[1] && isdigit((unsigned char)(*argv)[1][0]);
+ }
+#else
+ (void)args;
+ (void)argv;
+#endif
+ return hasargs(c);
}
static void
diff --git a/cmd/posix/make/parser.c b/cmd/posix/make/parser.c
@@ -9,13 +9,14 @@
#include <stdlib.h>
#include <string.h>
+#include "config.h"
#include "make.h"
#define MAXREPL 256
#define TABSIZ 64
#define MAXTOKEN FILENAME_MAX
#define ITEM 128
-#define IMMED 129
+#define IMMED 129 /* bmake/gmake compat: := */
typedef struct macro Macro;
@@ -736,16 +737,20 @@ validchar(int c)
}
/* what a target, a dependency or a macro name in a rule line may hold.
- * + and ? only end a name when they start += or ?=, so g++ and
- * the glob patterns of other makes are names */
+ * bmake/gmake compat: * [ ] ~ , and + ? that do not start += or ?= are
+ * names too, as in g++ and the glob patterns of those makes */
static int
itemchar(int c)
{
if (validchar(c))
return 1;
+#if FEATURE_MAKE_COMPAT
if (c == '*' || c == '[' || c == ']' || c == '~' || c == ',')
return 1;
return (c == '+' || c == '?') && ahead() != '=';
+#else
+ return 0;
+#endif
}
static char *
@@ -915,9 +920,10 @@ dirbase(char *name, int which)
return r;
}
-/* a macro name that holds a macro, as in $(tag-static-$(static)): the inner
+/* bmake/gmake compat: a macro name that holds a macro, as in $(tag-static-$(static)): the inner
* one is read to its closing bracket and its value joins the name. the
* pieces of a $(name:from=to) hold them too, kept as text until used */
+#if FEATURE_MAKE_COMPAT
static int
rawref(char *dst, int di, int max)
{
@@ -956,6 +962,7 @@ nested(char *name, int namei, Target *tp)
free(val);
return namei + n;
}
+#endif
static void
expansion(Target *tp)
@@ -1008,11 +1015,18 @@ expansion(Target *tp)
break;
}
+#if FEATURE_MAKE_COMPAT
if (c == '$') {
namei = nested(name, namei, tp);
break;
}
- if (!validchar(c) && c != '+')
+ // g++ in CXXFLAGS-g++
+ if (c == '+') {
+ name[namei++] = c;
+ break;
+ }
+#endif
+ if (!validchar(c))
error("invalid macro name in expansion");
name[namei++] = c;
break;
@@ -1037,10 +1051,12 @@ expansion(Target *tp)
"invalid replacement pattern in "
"expansion"
);
+#if FEATURE_MAKE_COMPAT
if (c == '$') {
repli = rawref(repl, repli, MAXREPL);
break;
}
+#endif
if (repli == MAXREPL - 1)
error("macro replacement too big");
repl[repli++] = c;
@@ -1051,10 +1067,12 @@ expansion(Target *tp)
break;
}
+#if FEATURE_MAKE_COMPAT
if (c == '$') {
toi = rawref(to, toi, MAXREPL);
break;
}
+#endif
if (toi == MAXREPL - 1)
error("macro substiturion too big");
to[toi++] = c;
@@ -1069,7 +1087,11 @@ expansion(Target *tp)
to[toi] = '\0';
erepl = expandstring(repl, tp, getloc());
- eto = expandstring(to, tp, getloc());
+#if FEATURE_MAKE_COMPAT
+ eto = expandstring(to, tp, getloc());
+#else
+ eto = estrdup(to);
+#endif
replace(s, erepl, eto);
free(eto);
@@ -1187,12 +1209,10 @@ repeat:
back(c);
expansion(NULL);
goto repeat;
- case '#':
- comment();
- c = '\n';
- /* fallthrough */
case ':':
- /* := and ::= expand their text where they are read */
+#if FEATURE_MAKE_COMPAT
+ /* bmake/gmake compat: := and ::= expand their text where they are
+ * read */
if (ahead() == ':')
nextc();
if (ahead() == '=') {
@@ -1201,7 +1221,12 @@ repeat:
tok = IMMED;
break;
}
+#endif
goto single;
+ case '#':
+ comment();
+ c = '\n';
+ /* fallthrough */
case ';':
case '=':
case '\n':
@@ -1210,6 +1235,8 @@ repeat:
token[1] = '\0';
tok = c;
break;
+#if FEATURE_MAKE_COMPAT
+ /* bmake/gmake compat: += ?= and != */
case '+':
if (ahead() == '=') {
nextc();
@@ -1239,8 +1266,11 @@ repeat:
break;
}
error("unexpected character '!'");
+#endif
default:
+#if FEATURE_MAKE_COMPAT
name:
+#endif
if (!itemchar(c))
error("unexpected character '%c'", c);
back(c);
@@ -1323,9 +1353,10 @@ readcmd(void)
return act;
}
-/* a dependency with glob characters stands for the files that match,
- * as it does in the other makes. one that matches nothing stays as it
- * is and fails as a missing file would */
+#if FEATURE_MAKE_COMPAT
+/* bmake/gmake compat: a dependency with glob characters stands for the
+ * files that match. one that matches nothing stays as it is and fails as
+ * a missing file would */
static int
globdeps(char ***deps, int n)
{
@@ -1350,6 +1381,7 @@ globdeps(char ***deps, int n)
*deps = out;
return m;
}
+#endif
static void
rule(char *targets[], int ntargets)
@@ -1358,8 +1390,13 @@ rule(char *targets[], int ntargets)
struct action *acts;
char **deps = NULL;
- // a name that expands to nothing, as ${SO}: does when SO is unset, makes
- // a rule that is not there
+#if FEATURE_MAKE_COMPAT
+ // bmake/gmake compat: a name that expands to nothing, as ${SO}: does when
+ // SO is unset, makes a rule that is not there
+#else
+ if (ntargets == 0)
+ error("missing target");
+#endif
for (ndeps = 0; next() == ITEM; ++ndeps) {
deps = erealloc(deps, (ndeps + 1) * sizeof(char *));
deps[ndeps] = estrdup(token);
@@ -1368,7 +1405,9 @@ rule(char *targets[], int ntargets)
if (tok != '\n' && tok != ';')
error("garbage at the end of the line");
+#if FEATURE_MAKE_COMPAT
ndeps = globdeps(&deps, ndeps);
+#endif
nactions = 0;
acts = NULL;
@@ -1383,10 +1422,11 @@ rule(char *targets[], int ntargets)
comment();
continue;
}
- /* a blank line does not end the commands: the makefiles of
- * other makes put one between two of them */
+#if FEATURE_MAKE_COMPAT
+ /* bmake/gmake compat: a blank line does not end the commands */
if (c == '\n')
continue;
+#endif
if (c != '\t')
break;
nactions++;
@@ -1428,6 +1468,8 @@ assign(char *macros[], int where, int n)
free(defs);
}
+/* bmake/gmake compat: += := ?= and != */
+#if FEATURE_MAKE_COMPAT
static void
assign_append(char *macros[], int where, int n)
{
@@ -1526,6 +1568,7 @@ assign_shell(char *macros[], int where, int n)
free(out);
free(cmd);
}
+#endif
void
parseinput(int where)
@@ -1555,6 +1598,7 @@ parseinput(int where)
case '=':
assign(targets, where, n);
break;
+#if FEATURE_MAKE_COMPAT
case IMMED:
assign_immediate(targets, where, n);
break;
@@ -1567,6 +1611,7 @@ parseinput(int where)
case '!':
assign_shell(targets, where, n);
break;
+#endif
default:
error("unexpected token '%s'(%d)", token, tok);
}
diff --git a/shared/config.h b/shared/config.h
@@ -52,6 +52,16 @@
#ifndef FEATURE_FIND_PRINT0
#define FEATURE_FIND_PRINT0 1
#endif
+/* head -n -N: all but the last N lines, a GNU extension */
+#ifndef FEATURE_HEAD_NEGATIVE
+#define FEATURE_HEAD_NEGATIVE 1
+#endif
+/* what makefiles written for bmake or gmake use and POSIX does not say:
+ * += ?= != := assignments, nested macro names, globs in dependencies,
+ * g++ style names, a bare -j, a blank line inside a recipe */
+#ifndef FEATURE_MAKE_COMPAT
+#define FEATURE_MAKE_COMPAT 1
+#endif
#ifndef FEATURE_SED_INPLACE
#define FEATURE_SED_INPLACE 1
#endif