0
|
1 -- functions to initialize and search through paths.
|
|
2 -- path strings specify a set of directory patterns separated by ';'.
|
|
3 -- the searchpath function
|
|
4
|
|
5 --- Initialize lua-style paths.
|
|
6 -- Looks up environment variable envname and substitute all ';;' by syspath
|
|
7 -- @param envname name of environment variable
|
|
8 -- @param syspath default value if envname is not set and substitution for all ';;'.
|
|
9 -- @return value from env variable or syspath. ';;' in env will be substituted by syspath.
|
|
10
|
|
11 function pathinit(envname, syspath)
|
|
12 local envvar = os.getenv(envname)
|
|
13 local path
|
|
14 if envvar then
|
|
15 path = envvar:gsub(";;",syspath)
|
|
16 else
|
|
17 path = syspath
|
|
18 end
|
|
19 return path
|
|
20 end
|
|
21
|
|
22
|
|
23 --- find object by searching through the path
|
|
24 -- @param openfun function(expanded)
|
|
25 -- @return found, expanded
|
|
26
|
|
27 function pathfind(path,name,openfun)
|
|
28 local replaced = path:gsub("?", name)
|
|
29 local found = nil
|
|
30 local fails = {}
|
|
31 for expanded in replaced:gmatch("([^;]+)") do
|
|
32 found = openfun(expanded)
|
|
33 if found then return found, expanded end
|
|
34 table.insert(fails, expanded)
|
|
35 end
|
|
36 return nil, fails
|
|
37 end
|
|
38
|