]> granicus.if.org Git - flex/commitdiff
Made search for m4 more explicit.
authorWill Estes <westes575@gmail.com>
Sat, 12 Dec 2015 16:03:05 +0000 (11:03 -0500)
committerWill Estes <westes575@gmail.com>
Sat, 12 Dec 2015 16:03:05 +0000 (11:03 -0500)
src/flexdef.h
src/main.c

index 9c6baecea2944496095582dc79f2e960a523b202..3aa63df5f43dcc672761aa1aec674f8d78594c1e 100644 (file)
@@ -66,6 +66,9 @@
 #ifdef HAVE_SYS_PARAMS_H
 #include <sys/params.h>
 #endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
 #ifdef HAVE_SYS_WAIT_H
 #include <sys/wait.h>
 #endif
index 5fadfc4cbe99d5a69dbd6d3b7eff8915cd754de6..c276bc4683e146a15615e520ccb5c35444ceeb0f 100644 (file)
@@ -359,8 +359,46 @@ void check_options (void)
 
     /* Setup the filter chain. */
     output_chain = filter_create_int(NULL, filter_tee_header, headerfilename);
-    if ( !(m4 = getenv("M4")))
-        m4 = M4;
+    if ( !(m4 = getenv("M4"))) {
+           char *slash;
+               m4 = M4;
+               if (slash = strrchr(M4, '/')) {
+                       m4 = slash+1;
+                       /* break up $PATH */
+                       const char *path = getenv("PATH");
+                       if (!path) {
+                               m4 = M4;
+                       } else {
+                               do {
+                                       char m4_path[PATH_MAX];
+                                       int length = strlen(path);
+                                       struct stat sbuf;
+
+                                       const char *endOfDir = strchr(path, ':');
+                                       if (!endOfDir)
+                                               endOfDir = path+length;
+
+                                       if ((endOfDir-path+2) >= sizeof(m4_path)) {
+                                           path = endOfDir+1;
+                                               continue;
+                                       }
+
+                                       strncpy(m4_path, path, sizeof(m4_path));
+                                       m4_path[endOfDir-path] = '/';
+                                       m4_path[endOfDir-path+1] = '\0';
+                                       strncat(m4_path, m4, sizeof(m4_path));
+                                       if (stat(m4_path, &sbuf) == 0 &&
+                                               (S_ISREG(sbuf.st_mode)) && sbuf.st_mode & S_IXUSR) {
+                                               m4 = strdup(m4_path);
+                                               break;
+                                       }
+                                       path = endOfDir+1;
+                               } while (path[0]);
+                               if (!path[0])
+                                   m4 = M4;
+                       }
+               }
+       }
     filter_create_ext(output_chain, m4, "-P", 0);
     filter_create_int(output_chain, filter_fix_linedirs, NULL);