lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org]
> On Behalf Of Seppl Vorderhörer
> Sent: vrijdag 31 augustus 2012 17:50
> To: lua-l@lists.lua.org
> Subject: Require / lua-searchers / findfile: Why is the dot replaced by
> the dir separator?
> 
> Hello,
> 
> I'm using Lua 5.2 directly embedded in another program (compiled as C++
> code).
> 
> When using "require(SomePath)" inside scripts I saw, that require
> (beside a lot of other things) also does a replacement inside SomePath
> of "." with the directory separator ("\" in my use case, as I'm using
> windows).
> 
> After some digging in the manuals and the code I found out, that this
> substitution is not configurable for the standard lua Searcher used.
> Both the standard Lua and C searchers (functions searcher_C and
> searcher_Lua) use the internal function findfile which itself uses
> searchpath() but unfortunately with a hardcoded "." as separator.
> 
> For this I have two questions:
> 1.) Why is this behaviour hardcoded? Shouldn't there be some
> configuration possibility like for the other special characters used
> e.g. in package.config?
> 
> 2.) Is it a good idea to use a special character that has another
> meaning on some (or basically most) file systems in existence? "." and
> ".." are used for "current" or "parent directory" under Windows.
> 
> 3.) What is the point of this replacement when looking for a filename
> as done in findfile() anyway? Actually I don't see the reason, why this
> is done at all and I have the feeling, that I miss something here.
> 
> Background:
> What I try to achieve is to make require work with relative pathnames,
> especially with being able to reach parent directories of certain
> scripts.
> I checked the mailing lists and found already some discussions about
> that but nothing describing the issue above.
> 
> What I see is that if I try something like
> 
>      require("..\\OtherDir\\OtherFile.lua")
> 
> this fails due to the fact that the internally used function findfile()
> replaces the dots by directory separators.
> A simple patch not replacing the "." and voi'la I can use relative
> paths containing "." or ".." without any problem.
> 
> Is there a reason behind this behaviour and is there maybe a
> possibility to achive the same without patching the original Lua source
> files?
> 
> Greetings,
> Seppl

Two reasons I can think of;
1) ansi C has no notion of directories/paths
2) I think the main reason would be not to allow client scripts (in
applications with Lua embedded) to run any arbitrary code. If it would
search the file system (relative or absolute) then any file could be loaded.
Now the path set, for require to explore, simply restricts it to those
search locations. If you want to load files from elsewhere, just modify the
search path.