Bug fixes and added envronment overrides
authorRobert Pengelly <robertapengelly@hotmail.com>
Sat, 5 Apr 2025 15:57:07 +0000 (16:57 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Sat, 5 Apr 2025 15:57:07 +0000 (16:57 +0100)
include/xmake/xmake.h
lib.c
variable.c
xmake.c

index 07e9bcff7c8c5fa39ee3be2431861cffd99430f6..5d2f73544e1bd4b53b0d0fa87646ce973cbe43f7 100644 (file)
@@ -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 cb7e4a888cef5ebddda1e22f9f12a243da8b51d4..94b11043518b0ed356889cbf616ec848510d58cf 100644 (file)
--- 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             }
 
 };
 
index c555bc54da47905afef0ca6db80c6ca68e8c68ad..df0fe70edbe2c674755e5195c18166a6250ee200 100644 (file)
@@ -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 4294af6dab911f08a28d2ed6e2d4d7bf4e136c29..47c1378c2bcbab1a63a67033e3dcca0dee558fbc 100644 (file)
--- 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);
+    
     }
 
 }