diff options
author | Leon Bottou <leon@bottou.org> | 2013-09-02 19:24:55 +0400 |
---|---|---|
committer | Leon Bottou <leon@bottou.org> | 2013-09-02 19:24:55 +0400 |
commit | 5272c2d361660a3de733033b08905fa311857633 (patch) | |
tree | ae15b87b5b99b818f07f62f67138d2c1667ad63a | |
parent | fdbba0b8c08d55b79ebf9d954424fb14d675db56 (diff) |
paths.findprogram takes multiple args
-rw-r--r-- | dok/index.dok | 6 | ||||
-rw-r--r-- | init.lua.in | 55 |
2 files changed, 33 insertions, 28 deletions
diff --git a/dok/index.dok b/dok/index.dok index 6cde4e5..dfa0c50 100644 --- a/dok/index.dok +++ b/dok/index.dok @@ -269,8 +269,10 @@ Returns true if the operating system is Mac OS X. Query a value in the Windows registry value. Causes an error on other systems. -==== paths.findprogram(progname) ==== +==== paths.findprogram(progname,...) ==== -Finds an executable program and returns its full path. +Finds an executable program named "progname" and returns its full path. +If none is found, continue searching programs named after the following arguments +and return the full path of the first match. All the directories specified by the PATH variable are searched. Under windows, this also searches the "App Path" registry entries. diff --git a/init.lua.in b/init.lua.in index d841027..4a5bcd0 100644 --- a/init.lua.in +++ b/init.lua.in @@ -6,6 +6,7 @@ local assert = assert local debug = debug local pcall = pcall local type = type +local ipairs = ipairs local os = os local g = _G @@ -118,32 +119,34 @@ function rmall(d, more) end end -function findprogram(exe) - if is_win() then - if not exe:match('[.]exe$') then - exe = exe .. '.exe' - end - local path, k, x = os.getenv("PATH") or "." - for dir in path:gmatch('[^;]+') do - x = concat(dir, exe) - if filep(x) then return x end - end - local function clean(s) - if s:match('^"') then return s:match('[^"]+') else return s end - end - k = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' .. exe - x = getregistryvalue('HKEY_CURRENT_USER', k, '') - if type(x) == 'string' then return clean(x) end - x = getregistryvalue('HKEY_LOCAL_MACHINE', k, '') - if type(x) == 'string' then return clean(x) end - k = 'Applications\\' .. exe .. '\\shell\\open\\command' - x = getregistryvalue('HKEY_CLASSES_ROOT', k, '') - if type(x) == 'string' then return clean(x) end - else - local path = os.getenv("PATH") or "." - for dir in path:gmatch('[^:]+') do - local x = concat(dir, exe) - if filep(x) then return x end +function findprogram(...) + for _,exe in ipairs{...} do + if is_win() then + if not exe:match('[.]exe$') then + exe = exe .. '.exe' + end + local path, k, x = os.getenv("PATH") or "." + for dir in path:gmatch('[^;]+') do + x = concat(dir, exe) + if filep(x) then return x end + end + local function clean(s) + if s:match('^"') then return s:match('[^"]+') else return s end + end + k = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' .. exe + x = getregistryvalue('HKEY_CURRENT_USER', k, '') + if type(x) == 'string' then return clean(x) end + x = getregistryvalue('HKEY_LOCAL_MACHINE', k, '') + if type(x) == 'string' then return clean(x) end + k = 'Applications\\' .. exe .. '\\shell\\open\\command' + x = getregistryvalue('HKEY_CLASSES_ROOT', k, '') + if type(x) == 'string' then return clean(x) end + else + local path = os.getenv("PATH") or "." + for dir in path:gmatch('[^:]+') do + local x = concat(dir, exe) + if filep(x) then return x end + end end end return nil |