From: Robert Pengelly Date: Sat, 5 Apr 2025 15:57:07 +0000 (+0100) Subject: Bug fixes and added envronment overrides X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=6a8ab724b01df489dc49805a22da4444aa4fbd3c;p=xmake.git Bug fixes and added envronment overrides --- diff --git a/include/xmake/xmake.h b/include/xmake/xmake.h index 07e9bcf..5d2f735 100644 --- a/include/xmake/xmake.h +++ b/include/xmake/xmake.h @@ -21,7 +21,7 @@ struct xmake_state { char *path; int quiet, no_print; - int dry_run; + int dry_run, env_override; int ignore_errors; }; diff --git a/lib.c b/lib.c index cb7e4a8..94b1104 100644 --- a/lib.c +++ b/lib.c @@ -31,6 +31,7 @@ static int print_help (const char *arg) { fprintf (stderr, " -I DIRECTORY Search DIRECTORY for included makefiles.\n"); fprintf (stderr, "\n"); + fprintf (stderr, " -e Environment variables override makefiles.\n"); fprintf (stderr, " -f FILE Read FILE as a makefile.\n"); fprintf (stderr, " -s Don't echo recipes.\n"); fprintf (stderr, "\n"); @@ -92,6 +93,15 @@ static int set_no_print (const char *arg) { } +static int set_env_override (const char *arg) { + + (void) arg; + + state->env_override = 1; + return 0; + +} + static int add_include_path (const char *path) { const char *in = path; @@ -177,42 +187,43 @@ static int non_option (const char *arg) { static struct option opts[] = { - { "--help", &print_help }, - { "-h", &print_help }, - - { "--version", &print_version }, + { "--help", &print_help }, + { "-h", &print_help }, - { "--silent", &set_quiet }, - { "--quiet", &set_quiet }, - { "-s", &set_quiet }, + { "--version", &print_version }, - { "--no-print-directory", &set_no_print }, + { "--silent", &set_quiet }, + { "--quiet", &set_quiet }, + { "-s", &set_quiet }, - { "--no-builtin-rules", &ignored }, - { "-r", &ignored }, + { "--no-print-directory", &set_no_print }, - { "--include_dir=", &add_include_path }, - { "-I:", &add_include_path }, + { "--no-builtin-rules", &ignored }, + { "-r", &ignored }, - { "--always-make", &ignored }, - { "-B", &ignored }, + { "--include_dir=", &add_include_path }, + { "-I:", &add_include_path }, - { "--directory=", &add_directory }, - { "-C:", &add_directory }, + { "--always-make", &ignored }, + { "-B", &ignored }, - { "--makefile=", &add_makefile }, - { "--file=", &add_makefile }, - { "-f:", &add_makefile }, + { "--directory=", &add_directory }, + { "-C:", &add_directory }, - { "--ignore-errors", &set_ignore_errors }, - { "-i", &set_ignore_errors }, + { "--makefile=", &add_makefile }, + { "--file=", &add_makefile }, + { "-f:", &add_makefile }, - { "--dry_run", &set_dry_run }, - { "-n", &set_dry_run }, + { "--ignore-errors", &set_ignore_errors }, + { "-i", &set_ignore_errors }, + { "--dry_run", &set_dry_run }, + { "-n", &set_dry_run }, + { "--environment-overrides", &set_env_override }, + { "-e", &set_env_override }, - { 0, &non_option } + { 0, &non_option } }; diff --git a/variable.c b/variable.c index c555bc5..df0fe70 100644 --- a/variable.c +++ b/variable.c @@ -131,7 +131,7 @@ static char *func_shell (const char *filename, unsigned long line_no, char *inpu lpCommandLine = xstrdup (skip_whitespace (p)); } else { - lpCommandLine = xstrdup (input); + lpApplicationName = xstrdup (skip_whitespace (input)); } if (!(p = strchr (lpApplicationName, '\\'))) { @@ -246,7 +246,7 @@ static char *func_shell (const char *filename, unsigned long line_no, char *inpu while (ReadFile (hStdOutPipeRead, p, end - p, &dwRead, 0)) { size_t offset; - p += strlen (p); + p += dwRead; offset = p - lbuf.start; @@ -304,6 +304,10 @@ static char *func_shell (const char *filename, unsigned long line_no, char *inpu } + if (isspace ((int) *(p - 1))) { + *(p - 1) = '\0'; + } + return lbuf.start; } @@ -410,14 +414,20 @@ static char *func_shell (const char *filename, unsigned long line_no, char *inpu p = lbuf.start; - while ((ch = *p++)) { + while ((ch = *p)) { if (ch == '\n' || ch == '\r') { - p[-1] = ' '; + *p = ' '; } + + p++; } + if (isspace ((int) *(p - 1))) { + *(p - 1) = '\0'; + } + return lbuf.start; } @@ -631,7 +641,7 @@ char *variable_expand_line (const char *filename, unsigned long line_no, char *l q++; } - if ((alloc_replacement = func->func (filename, line_no, q))) { + /*if ((alloc_replacement = func->func (filename, line_no, q))) { new = xmalloc (pos + strlen (alloc_replacement) + 1); @@ -641,7 +651,9 @@ char *variable_expand_line (const char *filename, unsigned long line_no, char *l free (alloc_replacement); return new; - } + }*/ + + alloc_replacement = func->func (filename, line_no, q); } else if ((q = strchr (content, '='))) { @@ -681,9 +693,24 @@ char *variable_expand_line (const char *filename, unsigned long line_no, char *l } } else { + + if (state->env_override) { + + if ((alloc_replacement = getenv (content))) { + + alloc_replacement = xstrdup (alloc_replacement); + goto got_value; + + } + + } + var = variable_find (content); + } + got_value: + free (content); } else if (line[pos + 1]) { @@ -720,7 +747,10 @@ char *variable_expand_line (const char *filename, unsigned long line_no, char *l pos += strlen (replacement); } - free (alloc_replacement); + if (alloc_replacement) { + free (alloc_replacement); + } + continue; @@ -737,10 +767,12 @@ char *variable_expand_line (const char *filename, unsigned long line_no, char *l void parse_var_line (const char *filename, unsigned long line_no, char *line, enum variable_origin origin) { enum { + VAR_ASSIGN, VAR_CONDITIONAL_ASSIGN, VAR_APPEND, VAR_SHELL + } opt = VAR_ASSIGN; enum variable_flavor flavor = VAR_FLAVOR_RECURSIVELY_EXPANDED; diff --git a/xmake.c b/xmake.c index 4294af6..47c1378 100644 --- a/xmake.c +++ b/xmake.c @@ -63,18 +63,18 @@ static void pipe_command (const char *filename, unsigned long line_no, char *inp HANDLE hStdInPipeRead, hStdInPipeWrite; HANDLE hStdOutPipeRead, hStdOutPipeWrite; - char *lpApplicationName = getenv ("COMSPEC"), *lpCommandLine = 0, *p; + char *lpApplicationName = getenv ("COMSPEC"), *lpCommandLine = 0; struct linebuf lbuf; input = skip_whitespace (input); - for (p = input; !isspace ((int) *p); p++) { + /*for (p = input; !isspace ((int) *p); p++) { if (*p == '/') { *p = '\\'; } - } + }*/ lpCommandLine = xmalloc (4 + strlen (input) + 1); sprintf (lpCommandLine, "/C %s", input); @@ -155,7 +155,7 @@ static void pipe_command (const char *filename, unsigned long line_no, char *inp while (ReadFile (hStdOutPipeRead, p, end - p, &dwRead, 0)) { size_t offset; - p += strlen (p); + p += dwRead; offset = p - lbuf.start; @@ -198,6 +198,8 @@ static void pipe_command (const char *filename, unsigned long line_no, char *inp } + } else { + printf ("%s", lbuf.start); } } @@ -307,6 +309,35 @@ static void pipe_command (const char *filename, unsigned long line_no, char *inp } + } else { + + char *p = lbuf.start, ch; + + while ((ch = *p)) { + + if (ch == '"') { + + memmove (p, p + 1, strlen (p + 1)); + *(p + strlen (p) - 1) = '\0'; + + continue; + + } + + if (ch == '\n' || ch == '\r') { + *p = ' '; + } + + p++; + + } + + if (isspace ((int) *(p - 1))) { + *(p - 1) = '\0'; + } + + printf ("%s\n", lbuf.start); + } }