/* 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);