Vinay Sajip avatar Vinay Sajip committed 99f394b

Made some features conditional.

Comments (0)

Files changed (1)

CLILauncher/launcher.c

 #define BUFSIZE 256
 #define MSGSIZE 1024
 
+/* Build options. */
+#define SKIP_PREFIX
+#define SEARCH_PATH
+
 /* Just for now - static definition */
 
 static FILE * log_fp = NULL;
 static COMMAND commands[MAX_COMMANDS];
 static int num_commands = 0;
 
+#if defined(SKIP_PREFIX)
+
 static wchar_t * builtin_prefixes [] = {
     /* These must be in an order that the longest matches should be found,
      * i.e. if the prefix is "/usr/bin/env ", it should match that entry
     return result;
 }
 
+#endif
+
+#if defined(SEARCH_PATH)
+
+static COMMAND path_command;
+
+static COMMAND * find_on_path(wchar_t * name)
+{
+    wchar_t * pathext;
+    size_t    varsize;
+    wchar_t * context = NULL;
+    wchar_t * extension;
+    COMMAND * result = NULL;
+    DWORD     len;
+    errno_t   rc;
+
+    wcscpy_s(path_command.key, MAX_PATH, name);
+    if (wcschr(name, L'.') != NULL) {
+        /* assume it has an extension. */
+        len = SearchPathW(NULL, name, NULL, MSGSIZE, path_command.value, NULL);
+        if (len) {
+            result = &path_command;
+        }
+    }
+    else {
+        /* No extension - search using registered extensions. */
+        rc = _wdupenv_s(&pathext, &varsize, L"PATHEXT");
+        if (rc == 0) {
+            extension = wcstok_s(pathext, L";", &context);
+            while (extension) {
+                len = SearchPathW(NULL, name, extension, MSGSIZE, path_command.value, NULL);
+                if (len) {
+                    result = &path_command;
+                    break;
+                }
+                extension = wcstok_s(NULL, L";", &context);
+            }
+            free(pathext);
+        }
+    }
+    return result;
+}
+
+#endif
+
 static COMMAND * find_command(wchar_t * name)
 {
     COMMAND * result = NULL;
             break;
         }
     }
+#if defined(SEARCH_PATH)
+    if (result == NULL)
+        result = find_on_path(name);
+#endif
     return result;
 }
 
                  * stick a NUL after the command while searching for it,
                  * then put back the char we zapped.
                  */
+#if defined(SKIP_PREFIX)
                 skipped = skip_prefix(shebang_line);
+#else
+                skipped = shebang_line;
+#endif
                 p = wcspbrk(skipped, L" \t\r\n");
                 if (p != NULL) {
                     zapped = *p;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.