From 165e53b4b875d09a7bf30886fa2376f3b53796ae Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Mon, 15 Sep 2025 01:55:51 +0100 Subject: [PATCH] Changed to execvp and output cleanup --- Makefile.pdw | 2 +- Makefile.unix | 2 +- Makefile.w32 | 2 +- Makefile.wat | 2 +- include/xmake/command.h => command.h | 0 cstr.c | 3 +- include/xmake/cstr.h => cstr.h | 0 include/xmake/dep.h => dep.h | 0 hashtab.c | 2 +- include/xmake/hashtab.h => hashtab.h | 0 lib.c | 10 +- include/xmake/lib.h => lib.h | 0 read.c | 14 +-- include/xmake/read.h => read.h | 0 report.c | 3 +- include/xmake/report.h => report.h | 0 rule.c | 10 +- include/xmake/rule.h => rule.h | 4 +- variable.c | 12 +-- include/xmake/variable.h => variable.h | 0 xmake.c | 122 ++++++++++++++++++++++--- include/xmake/xmake.h => xmake.h | 0 22 files changed, 139 insertions(+), 49 deletions(-) rename include/xmake/command.h => command.h (100%) rename include/xmake/cstr.h => cstr.h (100%) rename include/xmake/dep.h => dep.h (100%) rename include/xmake/hashtab.h => hashtab.h (100%) rename include/xmake/lib.h => lib.h (100%) rename include/xmake/read.h => read.h (100%) rename include/xmake/report.h => report.h (100%) rename include/xmake/rule.h => rule.h (94%) rename include/xmake/variable.h => variable.h (100%) rename include/xmake/xmake.h => xmake.h (100%) diff --git a/Makefile.pdw b/Makefile.pdw index a6256df..b269b7f 100644 --- a/Makefile.pdw +++ b/Makefile.pdw @@ -5,7 +5,7 @@ AS=aswin CC=gccwin LD=ldwin -COPTS=-S -O2 -fno-common -ansi -I. -I./include -I../pdos/pdpclib -I../pdos/src -D__WIN32__ -D__NOBIVA__ -D__PDOS__ +COPTS=-S -O2 -fno-common -ansi -I. -I../pdos/pdpclib -I../pdos/src -D__WIN32__ -D__NOBIVA__ -D__PDOS__ COBJ=cstr.o hashtab.o lib.o report.o read.o rule.o variable.o xmake.o all: clean xmake.exe diff --git a/Makefile.unix b/Makefile.unix index 9c4310b..18faf04 100644 --- a/Makefile.unix +++ b/Makefile.unix @@ -5,7 +5,7 @@ SRCDIR ?= $(CURDIR) VPATH := $(SRCDIR) CC := gcc -CFLAGS := -D_FILE_OFFSET_BITS=64 -I$(SRCDIR)/include -O2 -Wall -Werror -Wextra -ansi -pedantic -std=c90 +CFLAGS := -D_FILE_OFFSET_BITS=64 -O2 -Wall -Werror -Wextra -ansi -pedantic -std=c90 CSRC := cstr.c hashtab.c lib.c read.c report.c rule.c variable.c xmake.c diff --git a/Makefile.w32 b/Makefile.w32 index 7d64d9e..29e873e 100644 --- a/Makefile.w32 +++ b/Makefile.w32 @@ -5,7 +5,7 @@ SRCDIR ?= $(CURDIR) VPATH := $(SRCDIR) CC := gcc -CFLAGS := -D_FILE_OFFSET_BITS=64 -I$(SRCDIR)/include -O2 -Wall -Werror -Wextra -ansi -pedantic -std=c90 +CFLAGS := -D_FILE_OFFSET_BITS=64 -O2 -Wall -Werror -Wextra -ansi -pedantic -std=c90 CSRC := cstr.c hashtab.c lib.c read.c report.c rule.c variable.c xmake.c diff --git a/Makefile.wat b/Makefile.wat index 29f7ade..42bddbd 100644 --- a/Makefile.wat +++ b/Makefile.wat @@ -10,7 +10,7 @@ all: xmake.exe xmake.exe: $(SRC) - wcl -I.\\include -fe=$@ $^ + wcl -fe=$@ $^ clean: diff --git a/include/xmake/command.h b/command.h similarity index 100% rename from include/xmake/command.h rename to command.h diff --git a/cstr.c b/cstr.c index 803ee72..a4a219d 100644 --- a/cstr.c +++ b/cstr.c @@ -4,8 +4,7 @@ #include #include -#include - +#include "cstr.h" extern void *xrealloc (void *__ptr, unsigned long __size); static void cstr_realloc (CString *cstr, long new_size) { diff --git a/include/xmake/cstr.h b/cstr.h similarity index 100% rename from include/xmake/cstr.h rename to cstr.h diff --git a/include/xmake/dep.h b/dep.h similarity index 100% rename from include/xmake/dep.h rename to dep.h diff --git a/hashtab.c b/hashtab.c index 32f3c40..abd6ad6 100644 --- a/hashtab.c +++ b/hashtab.c @@ -5,7 +5,7 @@ #include #include -#include +#include "hashtab.h" static struct hashtab_entry *find_entry (struct hashtab_entry *entries, unsigned long capacity, struct hashtab_name *key); static int adjust_capacity (struct hashtab *table, unsigned long new_capacity) { diff --git a/include/xmake/hashtab.h b/hashtab.h similarity index 100% rename from include/xmake/hashtab.h rename to hashtab.h diff --git a/lib.c b/lib.c index 01bf0b3..94c69a5 100644 --- a/lib.c +++ b/lib.c @@ -7,11 +7,11 @@ #include #include -#include -#include -#include -#include -#include +#include "cstr.h" +#include "lib.h" +#include "report.h" +#include "variable.h" +#include "xmake.h" size_t len = 0; diff --git a/include/xmake/lib.h b/lib.h similarity index 100% rename from include/xmake/lib.h rename to lib.h diff --git a/read.c b/read.c index 5e0eab8..b19c453 100644 --- a/read.c +++ b/read.c @@ -6,13 +6,13 @@ #include #include -#include -#include -#include -#include -#include -#include -#include +#include "command.h" +#include "dep.h" +#include "lib.h" +#include "read.h" +#include "rule.h" +#include "variable.h" +#include "xmake.h" extern struct variable *default_goal_var; diff --git a/include/xmake/read.h b/read.h similarity index 100% rename from include/xmake/read.h rename to read.h diff --git a/report.c b/report.c index d36483b..db2c9b1 100644 --- a/report.c +++ b/report.c @@ -5,8 +5,7 @@ #include #include -#include - +#include "report.h" unsigned long errors = 0; #ifndef __PDOS__ diff --git a/include/xmake/report.h b/report.h similarity index 100% rename from include/xmake/report.h rename to report.h diff --git a/rule.c b/rule.c index 19c7484..58470f3 100644 --- a/rule.c +++ b/rule.c @@ -3,11 +3,11 @@ *****************************************************************************/ #include -#include -#include -#include -#include -#include +#include "command.h" +#include "dep.h" +#include "hashtab.h" +#include "lib.h" +#include "rule.h" static struct hashtab hashtab_rules = { 0 }; struct suffix_rule *suffix_rules = 0; diff --git a/include/xmake/rule.h b/rule.h similarity index 94% rename from include/xmake/rule.h rename to rule.h index 9e01c7b..862bd4d 100644 --- a/include/xmake/rule.h +++ b/rule.h @@ -4,8 +4,8 @@ #ifndef _RULE_H #define _RULE_H -#include -#include +#include "command.h" +#include "dep.h" struct rule { diff --git a/variable.c b/variable.c index e6cd868..ef98a44 100644 --- a/variable.c +++ b/variable.c @@ -11,12 +11,12 @@ #include -#include -#include -#include -#include -#include -#include +#include "hashtab.h" +#include "lib.h" +#include "read.h" +#include "report.h" +#include "variable.h" +#include "xmake.h" struct linebuf { diff --git a/include/xmake/variable.h b/variable.h similarity index 100% rename from include/xmake/variable.h rename to variable.h diff --git a/xmake.c b/xmake.c index 4878820..4b1d5af 100644 --- a/xmake.c +++ b/xmake.c @@ -11,12 +11,12 @@ #include -#include -#include -#include -#include -#include -#include +#include "cstr.h" +#include "lib.h" +#include "read.h" +#include "rule.h" +#include "variable.h" +#include "xmake.h" struct variable *default_goal_var = 0; @@ -302,8 +302,7 @@ static int pipe_command (const char *filename, unsigned long line_no, char *inpu p = xstrndup (p, space - p); } - fprintf (stderr, "%s", lbuf.start); - fprintf (stderr, "%s: *** [%s:%lu: %s] Error %ld\n", program_name, filename, line_no, name, dwExitCode); + fprintf (stderr, "%s%s: *** [%s:%lu: %s] Error %ld\n", lbuf.start, program_name, filename, line_no, name, dwExitCode); if (state->keep_going) { return dwExitCode; @@ -349,18 +348,112 @@ static int pipe_command (const char *filename, unsigned long line_no, char *inpu if (pid == 0) { - char *prog = input, *space = 0, *slash; + char **argv; + int argc = 1; + + char *prog = input, *space = 0, *ptr; + int offset = 0, quote, i; if ((space = strchr (prog, ' '))) { + + ptr = space + 1; + + for (i = 0; ptr[i]; i++) { + + if (isspace ((int) ptr[i])) { + argc++; + } + + } + prog = xstrndup (prog, space - prog); + argv = xmalloc (sizeof (*argv) * (argc + 1)); + + for (i = 0; ; i++) { + + if (!isspace ((int) ptr[i])) { + break; + } + + } + + argv[1] = ptr + i; + argc = 1; + quote = 0; + + while (ptr[i]) { + + ptr[i - offset] = ptr[i]; + + if (quote) { + + if (ptr[i] == '\\' && (ptr[i + 1] == quote || ptr[i + 1] == '\\')) { + + offset++; + i++; + + ptr[i - offset] = ptr[i]; + + } else if (ptr[i] == quote) { + + quote = 0; + offset++; + + } + + i++; + + } else if (ptr[i] == '\'' || ptr[i] == '"') { + + if (argv[argc] == ptr + i - offset) { + argv[argc]++; + } + + quote = ptr[i]; + i++; + + } else if (isspace ((int) ptr[i])) { + + ptr[i - offset] = '\0'; + argc++; + + for (i = i + 1; ; i++) { + + if (!isspace ((int) ptr[i])) { + break; + } + + } + + argv[argc] = ptr + i - offset; + + } else { + i++; + } + + } + + if (argv[argc] != ptr + i - offset) { + + ptr[i - offset] = '\0'; + argc++; + + } + + } else { + argv = xmalloc (sizeof (*argv) * (argc + 1)); } + argv[0] = prog; + argv[argc] = 0; + dup2 (pipefd[1], STDOUT_FILENO); close (pipefd[0]); close (pipefd[1]); - execlp (prog, (slash = strrchr (prog, '/')) ? (slash + 1) : prog, space ? skip_whitespace (space) : NULL, NULL); + execvp (prog, argv); + free (argv); printf ("%s: %s: %s\n", program_name, prog, strerror (errno)); exit (EXIT_FAILURE); @@ -411,7 +504,7 @@ static int pipe_command (const char *filename, unsigned long line_no, char *inpu } - { + /*{ char *p = lbuf.start, ch; @@ -438,14 +531,13 @@ static int pipe_command (const char *filename, unsigned long line_no, char *inpu *(p - 1) = '\0'; } - } + }*/ if (status) { if (!is_ignoring_errors) { - fprintf (stderr, "%s\n", lbuf.start); - fprintf (stderr, "%s: *** [%s:%lu: %s] Error %d\n", program_name, filename, line_no, name, status); + fprintf (stderr, "%s%s: *** [%s:%lu: %s] Error %d\n", lbuf.start, program_name, filename, line_no, name, status); if (state->keep_going) { return status; @@ -456,7 +548,7 @@ static int pipe_command (const char *filename, unsigned long line_no, char *inpu } } else { - printf ("%s\n", lbuf.start); + printf ("%s", lbuf.start); } return 0; diff --git a/include/xmake/xmake.h b/xmake.h similarity index 100% rename from include/xmake/xmake.h rename to xmake.h -- 2.34.1