--- /SourceCache/flex/flex-19/flex/main.c 2006-02-14 10:55:42.000000000 -0800
+++ ./main.c 2007-07-06 10:23:00.000000000 -0700
@@ -36,7 +36,7 @@
#include "version.h"
#include "options.h"
#include "tables.h"
-
+#include <sys/stat.h>
static char flex_version[] = FLEX_VERSION;
/* declare functions that have forward references */
@@ -359,8 +359,46 @@ void check_options ()
/* 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;
+ }
+
+ strlcpy(m4_path, path, sizeof(m4_path));
+ m4_path[endOfDir-path] = '/';
+ m4_path[endOfDir-path+1] = '\0';
+ strlcat(m4_path, m4, sizeof(m4_path));
+ if (stat(m4_path, &sbuf) == 0 &&
+ (S_ISREG(sbuf.st_mode))) { /* check executable? */
+ 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);