From 7eb4e8959784bd25892ca166a1ea07a920e9777b Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 11 Jun 2022 09:17:07 +0100 Subject: [PATCH] Allow non-POSIX features to be turned off at runtime The -C option (GitHub PR #8) and enhanced treatment of the MAKE macro (GitHub PR #10) should be turned off in POSIX mode. The .POSIX special target doesn't work for these features as they're processed before any makefiles are read. Instead: - Use the --posix command line option, which must be the first given. - Set the environment variable PDPMAKE_POSIXLY_CORRECT. In addition, if pdpmake is run with extensions enabled and a valid .POSIX special target is present, then PDPMAKE_POSIXLY_CORRECT is set in the environment. Thus, recursive invocations of pdpmake will have non-POSIX features disabled. --- input.c | 4 +++- main.c | 50 ++++++++++++++++++++++++++++++++++++-------------- make.h | 17 +++++++++++++---- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/input.c b/input.c index c8542fa..1715f4c 100644 --- a/input.c +++ b/input.c @@ -984,8 +984,10 @@ input(FILE *fd) free(copy); free(expanded); #if ENABLE_FEATURE_MAKE_EXTENSIONS - if (first_line && findname(".POSIX")) + if (first_line && findname(".POSIX")) { + setenv("PDPMAKE_POSIXLY_CORRECT", "", 1); posix = TRUE; + } first_line = FALSE; #endif } diff --git a/main.c b/main.c index 56e3640..2d1fc4f 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,8 @@ /* - * make [-C path] [-f makefile] [-eiknpqrsSt] [macro=val ...] [target ...] + * make [--posix] [-C path] [-f makefile] [-eiknpqrsSt] [macro=val ...] [target ...] * - * -C Change directory to path + * --posix Enforce POSIX mode (non-POSIX) + * -C Change directory to path (non-POSIX) * -f Makefile name * -e Environment variables override macros in makefiles * -i Ignore exit status @@ -29,8 +30,11 @@ static void usage(void) { fprintf(stderr, - "Usage: %s [-C path] [-f makefile] [-eiknpqrsSt] [macro=val ...]" - "[target ...]\n", myname); + "Usage: %s" + IF_FEATURE_MAKE_EXTENSIONS(" [--posix] [-C path]") + " [-f makefile] [-eiknpqrsSt] [macro=val ...]" + IF_FEATURE_MAKE_EXTENSIONS("\n\t") + " [target ...]\n", myname); exit(2); } @@ -46,14 +50,18 @@ process_options(int argc, char **argv, int from_env) while ((opt = getopt(argc, argv, OPTSTR1 OPTSTR2)) != -1) { switch(opt) { +#if ENABLE_FEATURE_MAKE_EXTENSIONS case 'C': - if (!from_env) { + if (!posix && !from_env) { if (chdir(optarg) == -1) { error("can't chdir to %s: %s", optarg, strerror(errno)); } flags |= OPT_C; + break; } + error("-C not allowed"); break; +#endif case 'f': // Alternate file name if (!from_env) { makefiles = newcmd(optarg, makefiles); @@ -302,7 +310,7 @@ int main(int argc, char **argv) { #if ENABLE_FEATURE_MAKE_EXTENSIONS - const char *path, *argv0; + const char *path, *newpath = NULL; #else const char *path = "make"; #endif @@ -312,15 +320,30 @@ main(int argc, char **argv) struct cmd *mp; myname = basename(*argv); +#if ENABLE_FEATURE_MAKE_EXTENSIONS + if (argv[1] && strcmp(argv[1], "--posix") == 0) { + argv[1] = argv[0]; + ++argv; + --argc; + setenv("PDPMAKE_POSIXLY_CORRECT", "", 1); + posix = TRUE; + } else { + posix = getenv("PDPMAKE_POSIXLY_CORRECT") != NULL; + } +#endif #if ENABLE_FEATURE_MAKE_EXTENSIONS - path = argv0 = argv[0]; - if (argv[0][0] != '/' && strchr(argv[0], '/')) { - // Make relative path absolute - path = realpath(argv[0], NULL); - if (!path) { - error("can't resolve path for %s: %s", argv[0], strerror(errno)); + if (!posix) { + path = argv[0]; + if (argv[0][0] != '/' && strchr(argv[0], '/')) { + // Make relative path absolute + path = newpath = realpath(argv[0], NULL); + if (!path) { + error("can't resolve path for %s: %s", argv[0], strerror(errno)); + } } + } else { + path = "make"; } #endif @@ -371,8 +394,7 @@ main(int argc, char **argv) setmacro("SHELL", "/bin/sh", 4); setmacro("MAKE", path, 4); #if ENABLE_FEATURE_MAKE_EXTENSIONS - if (path != argv0) - free((void *)path); + free((void *)newpath); #endif #if ENABLE_FEATURE_MAKE_EXTENSIONS diff --git a/make.h b/make.h index ab3c675..2e90a7a 100644 --- a/make.h +++ b/make.h @@ -49,9 +49,18 @@ extern char **environ; #define MAX(a,b) ((a)>(b)?(a):(b)) #define OPTSTR1 "eiknqrsSt" +#if ENABLE_FEATURE_MAKE_EXTENSIONS #define OPTSTR2 "pf:C:" +#else +#define OPTSTR2 "pf:" +#endif enum { + OPTBIT_p = 9, + OPTBIT_f, + IF_FEATURE_MAKE_EXTENSIONS(OPTBIT_C,) + OPTBIT_precious, + OPT_e = (1 << 0), OPT_i = (1 << 1), OPT_k = (1 << 2), @@ -62,11 +71,11 @@ enum { OPT_S = (1 << 7), OPT_t = (1 << 8), // These options aren't allowed in MAKEFLAGS - OPT_p = (1 << 9), - OPT_f = (1 << 10), - OPT_C = (1 << 11), + OPT_p = (1 << OPTBIT_p), + OPT_f = (1 << OPTBIT_f), + OPT_C = IF_FEATURE_MAKE_EXTENSIONS((1 << OPTBIT_C)) + 0, // OPT_precious isn't a command line option and must be last - OPT_precious = (1 << 12), + OPT_precious = (1 << OPTBIT_precious), }; // Options that aren't included in MAKEFLAGS -- 2.36.1