]> git.sesse.net Git - vlc/blob - share/lua/intf/luac.lua
Add error handling to luac intf.
[vlc] / share / lua / intf / luac.lua
1 --[==========================================================================[
2  luac.lua: lua compilation module for VLC (duplicates luac)
3 --[==========================================================================[
4  Copyright (C) 2010 Antoine Cellerier
5  $Id$
6
7  Authors: Antoine Cellerier <dionoea at videolan dot org>
8
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 --]==========================================================================]
23
24 usage = 
25 [[
26 To compile a lua script to bytecode (luac) run:
27   vlc -I lua --lua-intf --lua-config 'luac={input="file.lua",output="file.luac"}'
28 Output will be similar to that of the luac command line tool provided with lua with the following arguments:
29   luac -o file.luac file.lua
30 ]]
31
32 require "string"
33 require "io"
34
35 function compile()
36     vlc.msg.info("About to compile lua file")
37     vlc.msg.info("  Input is '"..tostring(config.input).."'")
38     vlc.msg.info("  Output is '"..tostring(config.output).."'")
39     if not config.input or not config.output then
40         vlc.msg.err("Input and output config options must be set")
41         return false
42     end
43
44     local bytecode, msg = loadfile(config.input)
45     if not bytecode then
46         vlc.msg.err("Error while loading file '"..config.input.."': "..msg)
47         return false
48     end
49
50     local output, msg = io.open(config.output, "wb")
51     if not output then
52         vlc.msg.err("Error while opening file '"..config.output.."' for output: "..msg)
53         return false
54     else
55         output:write(string.dump(bytecode))
56         return true
57     end
58 end
59
60
61 if not compile() then
62     for line in string.gmatch(usage,"([^\n]+)\n*") do
63         vlc.msg.err(line)
64     end
65 end
66 vlc.misc.quit()
67