lua-users home
lua-l archive

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



Lua authors:

Way to 'fix' the -l path finding could be to:

lua.c could set argv[0] to each library's _full_ filename, during loading of that library. This would suffice. Once the actual filename parameter (or none) is being loaded, argv[0] would carry that, as it already does.

5.1.1 behaviour on -l loading is to have argv as nil:

$ more test.lua
print( argv[0] or "none" )

$ lua -l test
lua: ./test.lua:1: attempt to index global 'argv' (a nil value)

-asko



On Fri, 06 Oct 2006 12:44:55 +0300
 askok@dnainternet.net wrote:

There's basically two ways to this, based on whether your script is launched as a filename, or via the '-l' (library) flag.

The '-l' solution is kind-of ugly, and a better one would be welcomed. This has been mentioned on the list (at least) once, but no changes came about.


1. filename argument
=============

Lumikki is started by such shell script ("lumikki"):

   #!/bin/sh
   #
   lua -lstrict ./scripts/main.lua $@

This allows main.lua to use 'argv[0]' to find other scripts:

-- arg[0] should have our full filename, let's take the path out:
--
local _,_, MY_PATH= string.find( arg[0], "(.+[/\\]).-" )
assert(MY_PATH)

dofile( MY_PATH.."filters.lua" )

dofile( MY_PATH.."support.lua" )
dofile( MY_PATH.."dirtools.lua" )

dofile( MY_PATH.."depcache.lua" )
dofile( MY_PATH.."lumikki2.lua" )


2. '-l' flag
======

--
-- HAMSTER.LUA -- Copyright (c) 2005-06, askok@dnainternet.net
--
-- Hamster initialisation hub.
--
-- This script is the entry point for Lua's "-l" loading mechanism, and can -- handle any Lua engine incompatibility issue prior to loading actual
-- application scripts.
--

local main= "main" -- main script <--- NAMING THIS IS UP TO YOU

-- Finding subscripts:
--
local LUA_PATH= rawget(_G,"package") and package.path
or os.getenv("LUA_PATH")
if not LUA_PATH then
    error "LUA_PATH environment variable not set."
end

rawset( _G, "MY_PATH", false )

for pat in string.gfind( LUA_PATH, "[^;]+" ) do
    local try= string.gsub( pat, "?", "hamster" )
    local fh= io.open(try,"r")
    if fh then
        fh:close()
        MY_PATH= string.gsub(try,"hamster%.lua","")
        break
    end
end
assert(MY_PATH)

--
-- Actual application code:
--
local m= dofile( MY_PATH..main..".lua" )

return m




On Fri, 06 Oct 2006 10:10:47 +0100
 Gavin Wraith <gavin@wra1th.plus.com> wrote:
In message <1079b050610060102g63d45083of3f8a6e0b00786f9@mail.gmail.com> you wrote:

How does eveyone determine the location of scripts relative to their application? I've been racking my brain on this one and haven't found a solution yet. I tried reading the directory "./" and got the hard drive's root. Are there other ways? I'm trying to overcome the problem of my app being run on different computers and not wanting to
ask the user to do any more work than download the app.

I use arg[0].

--
Gavin Wraith (gavin@wra1th.plus.com)
Home page: http://www.wra1th.plus.com/