aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Germain <bnt.germain@gmail.com>2012-07-10 21:43:32 +0200
committerBenoit Germain <bnt.germain@gmail.com>2012-07-10 21:43:32 +0200
commit0373f863a9dbabbbe3e9aa74bea3e4b778f3a0cd (patch)
tree52dfb987d5ee12ef4668e875d19a70a6c77cebe1
parent4468ec94e354a9f6d8d7ea2859afa757bb934617 (diff)
downloadlanes-0373f863a9dbabbbe3e9aa74bea3e4b778f3a0cd.tar.gz
lanes-0373f863a9dbabbbe3e9aa74bea3e4b778f3a0cd.tar.bz2
lanes-0373f863a9dbabbbe3e9aa74bea3e4b778f3a0cd.zip
one more step toward 5.2 compatibilityv3.1.5
* lua51-lanes renamed lanes.core * keeper state microcode is no longer embedded inside lanes.core, but located and loaded with package.loaders[2] * changed rockspec build type from "make" to "builtin"
-rw-r--r--CHANGES5
-rw-r--r--Makefile36
-rw-r--r--docs/index.html4
-rw-r--r--lanes-3.1.4.rockspec91
-rw-r--r--src/Makefile37
-rw-r--r--src/keeper.c27
-rw-r--r--src/lanes-keeper.lua (renamed from src/keeper.lua)0
-rw-r--r--src/lanes.c10
-rw-r--r--src/lanes.lua4
-rw-r--r--tools/bin2c.lua131
10 files changed, 58 insertions, 287 deletions
diff --git a/CHANGES b/CHANGES
index 95e0e70..6bcc061 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,11 @@
1 1
2CHANGES: 2CHANGES:
3 3
4CHANGE 41: BGe 07-Jul-2012
5 * lua51-lanes renamed lanes/core
6 * keeper state microcode is no longer embedded inside lanes.core, but located and loaded with package.loaders[2]
7 * changed rockspec build type from "make" to "builtin"
8
4CHANGE 40: BGe 26-Jun-2012 9CHANGE 40: BGe 26-Jun-2012
5 * when a transfered function is not found in source, guess its name to help the user find out what's wrong 10 * when a transfered function is not found in source, guess its name to help the user find out what's wrong
6 * new function lanes.nameof() 11 * new function lanes.nameof()
diff --git a/Makefile b/Makefile
index 3f1f0e1..c5c41b1 100644
--- a/Makefile
+++ b/Makefile
@@ -17,39 +17,40 @@ MODULE = lanes
17 17
18N=1000 18N=1000
19 19
20_SO=.so 20_TARGET_DIR=src/lanes
21_TARGET_SO=src/lua51-lanes.so
22TIME=time 21TIME=time
23 22
24ifeq "$(findstring MINGW32,$(shell uname -s))" "MINGW32" 23ifeq "$(findstring MINGW32,$(shell uname -s))" "MINGW32"
25 # MinGW MSYS on XP 24 # MinGW MSYS on XP
26 # 25 #
27 LUA=lua.exe 26 _SO=dll
28 LUAC=luac.exe 27 _LUAEXT=.exe
29 _SO=.dll
30 _TARGET_SO=./lua51-lanes.dll
31 TIME=timeit.exe 28 TIME=timeit.exe
32else 29else
33 # Autodetect LUA & LUAC 30 _SO=so
34 # 31 _LUAEXT=
35 LUA=$(word 1,$(shell which lua5.1) $(shell which lua51) lua)
36 LUAC=$(word 1,$(shell which luac5.1) $(shell which luac51) luac)
37endif 32endif
38 33
39_PREFIX=LUA_CPATH="./src/?$(_SO)" LUA_PATH="./src/?.lua;./tests/?.lua" 34# Autodetect LUA
35#
36LUA=$(word 1,$(shell which lua5.1$(_LUAEXT)) $(shell which lua51$(_LUAEXT)) lua$(_LUAEXT))
37
38_TARGET_SO=$(_TARGET_DIR)/core.$(_SO)
39
40_PREFIX=LUA_CPATH="./src/?.$(_SO)" LUA_PATH="./src/?.lua;./tests/?.lua"
40 41
41#--- 42#---
42all: $(_TARGET_SO) 43all: $(_TARGET_SO)
43 44
44$(_TARGET_SO): src/*.lua src/*.c src/*.h 45$(_TARGET_SO): src/*.lua src/*.c src/*.h
45 cd src && $(MAKE) LUA=$(LUA) LUAC=$(LUAC) 46 cd src && $(MAKE) LUA=$(LUA)
46 47
47clean: 48clean:
48 cd src && $(MAKE) clean 49 cd src && $(MAKE) clean
49 50
50debug: 51debug:
51 $(MAKE) clean 52 $(MAKE) clean
52 cd src && $(MAKE) LUA=$(LUA) LUAC=$(LUAC) OPT_FLAGS="-O0 -g" 53 cd src && $(MAKE) LUA=$(LUA) OPT_FLAGS="-O0 -g"
53 @echo "" 54 @echo ""
54 @echo "** Now, try 'make repetitive' or something and if it crashes, 'gdb $(LUA) ...core file...'" 55 @echo "** Now, try 'make repetitive' or something and if it crashes, 'gdb $(LUA) ...core file...'"
55 @echo " Then 'bt' for a backtrace." 56 @echo " Then 'bt' for a backtrace."
@@ -65,7 +66,7 @@ gdb:
65#--- LuaRocks automated build --- 66#--- LuaRocks automated build ---
66# 67#
67rock: 68rock:
68 cd src && $(MAKE) LUAROCKS=1 CFLAGS="$(CFLAGS)" LIBFLAG="$(LIBFLAG)" LUA=$(LUA) LUAC=$(LUAC) 69 cd src && $(MAKE) LUAROCKS=1 CFLAGS="$(CFLAGS)" LIBFLAG="$(LIBFLAG)" LUA=$(LUA)
69 70
70 71
71#--- Testing --- 72#--- Testing ---
@@ -196,10 +197,11 @@ LUA_SHAREDIR=$(DESTDIR)/share/lua/5.1
196# 197#
197# AKa 17-Oct: changed to use 'install -m 644' and 'cp -p' 198# AKa 17-Oct: changed to use 'install -m 644' and 'cp -p'
198# 199#
199install: $(_TARGET_SO) src/lanes.lua 200install: $(_TARGET_SO) src/lanes.lua src/lanes-keeper.lua
200 mkdir -p $(LUA_LIBDIR) $(LUA_SHAREDIR) 201 mkdir -p $(LUA_LIBDIR) $(LUA_LIBDIR)/lanes $(LUA_SHAREDIR)
201 install -m 644 $(_TARGET_SO) $(LUA_LIBDIR) 202 install -m 644 $(_TARGET_SO) $(LUA_LIBDIR)/lanes
202 cp -p src/lanes.lua $(LUA_SHAREDIR) 203 cp -p src/lanes.lua $(LUA_SHAREDIR)
204 cp -p src/lanes-keeper.lua $(LUA_SHAREDIR)
203 205
204 206
205#--- Packaging --- 207#--- Packaging ---
diff --git a/docs/index.html b/docs/index.html
index 2d1c78e..7cb4f40 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -56,7 +56,7 @@
56 56
57<p><br/><font size="-1"><i>Copyright &copy; 2007-12 Asko Kauppi, Benoit Germain. All rights reserved.</i> 57<p><br/><font size="-1"><i>Copyright &copy; 2007-12 Asko Kauppi, Benoit Germain. All rights reserved.</i>
58 <br>Lua Lanes is published under the same <A HREF="http://en.wikipedia.org/wiki/MIT_License">MIT license</A> as Lua 5.1. 58 <br>Lua Lanes is published under the same <A HREF="http://en.wikipedia.org/wiki/MIT_License">MIT license</A> as Lua 5.1.
59 </p><p>This document was revised on 26-Jun-12, and applies to version 3.1.4 59 </p><p>This document was revised on 09-Jul-12, and applies to version 3.1.5
60</font></p> 60</font></p>
61 61
62</center> 62</center>
@@ -145,7 +145,7 @@
145details and limitations. 145details and limitations.
146</p> 146</p>
147 147
148<p>To install Lanes, all you need are the <tt>lanes.lua</tt> and <tt>lua51-lanes.so|dll</tt> 148<p>To install Lanes, all you need are the <tt>lanes.lua</tt> <tt>lanes-keeper.lua</tt> and <tt>lanes/core.so|dll</tt>
149files to be reachable by Lua (see LUA_PATH, LUA_CPATH). 149files to be reachable by Lua (see LUA_PATH, LUA_CPATH).
150 150
151Or use <A HREF="http://www.luarocks.org" TARGET="_blank">Lua Rocks</A> package management. 151Or use <A HREF="http://www.luarocks.org" TARGET="_blank">Lua Rocks</A> package management.
diff --git a/lanes-3.1.4.rockspec b/lanes-3.1.4.rockspec
deleted file mode 100644
index 64206fa..0000000
--- a/lanes-3.1.4.rockspec
+++ /dev/null
@@ -1,91 +0,0 @@
1--
2-- Lanes rockspec
3--
4-- Ref:
5-- <http://luarocks.org/en/Rockspec_format>
6--
7
8package = "Lanes"
9
10version = "3.1.4-1"
11
12source= {
13 url= "git://github.com/LuaLanes/lanes.git",
14 branch= "v3.1.4"
15}
16
17description = {
18 summary= "Multithreading support for Lua",
19 detailed= [[
20 Lua Lanes is a portable, message passing multithreading library
21 providing the possibility to run multiple Lua states in parallel.
22 ]],
23 license= "MIT/X11",
24 homepage="http://kotisivu.dnainternet.net/askok/lanes/",
25 maintainer="Benoit Germain <bnt.germain@gmail.com>"
26}
27
28-- Q: What is the difference of "windows" and "win32"? Seems there is none;
29-- so should we list either one or both?
30--
31supported_platforms= { "win32",
32 "macosx",
33 "linux",
34 "freebsd", -- TBD: not tested
35 "msys", -- TBD: not supported by LuaRocks 1.0 (or is it?)
36}
37
38dependencies= {
39 "lua = 5.1",
40}
41
42--
43-- Non-Win32: build using the Makefile
44-- Win32: build using 'make-vc.cmd' and "manual" copy of products
45--
46-- TBD: How is MSYS treated? We'd like (really) it to use the Makefile.
47-- It should be a target like "cygwin", not defining "windows".
48-- "windows" should actually guarantee Visual C++ as the compiler.
49--
50-- Q: Does "win32" guarantee we have Visual C++ 2005/2008 command line tools?
51--
52-- Note: Cannot use the simple "module" build type, because we need to precompile
53-- 'src/keeper.lua' -> keeper.lch and bake it into lanes.c.
54--
55build = {
56
57 -- Win32 (Visual C++) uses 'make-vc.cmd' for building
58 --
59 platforms= {
60 windows= {
61 type= "command",
62 build_command= "make-vc.cmd",
63 install= {
64 lua = { "src/lanes.lua" },
65 lib = { "lua51-lanes.dll" }
66 }
67 }
68 },
69
70 -- Other platforms use the Makefile
71 --
72 -- LuaRocks defines CFLAGS, LIBFLAG and LUA_INCDIR for 'make rock',
73 -- defines LIBDIR, LUADIR for 'make install'
74 --
75 -- Ref: <http://www.luarocks.org/en/Paths_and_external_dependencies>
76 --
77 type = "make",
78
79 build_target = "rock",
80 build_variables= {
81 CFLAGS= "$(CFLAGS) -I$(LUA_INCDIR)",
82 LIBFLAG= "$(LIBFLAG)",
83 },
84
85 install_target = "install",
86 install_variables= {
87 LUA_LIBDIR= "$(LIBDIR)",
88 LUA_SHAREDIR= "$(LUADIR)",
89 }
90}
91
diff --git a/src/Makefile b/src/Makefile
index 03f5558..a16acce 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,8 +1,8 @@
1# 1#
2# Lanes/src/Makefile 2# Lanes/src/Makefile
3# 3#
4# make [LUA=... LUAC=...] Manual build 4# make [LUA=...] Manual build
5# make LUAROCKS=1 CFLAGS=... LIBFLAG=... LUA=... LUAC=... LuaRocks automated build 5# make LUAROCKS=1 CFLAGS=... LIBFLAG=... LUA=... LuaRocks automated build
6# 6#
7 7
8MODULE=lanes 8MODULE=lanes
@@ -19,11 +19,10 @@ OPT_FLAGS=-O2
19 # -O0 -g 19 # -O0 -g
20 20
21LUA=lua 21LUA=lua
22LUAC=luac
23 22
24_SO=.so 23_SO=so
25ifeq "$(findstring MINGW32,$(shell uname -s))" "MINGW32" 24ifeq "$(findstring MINGW32,$(shell uname -s))" "MINGW32"
26 _SO=.dll 25 _SO=dll
27endif 26endif
28 27
29ifeq "$(LUAROCKS)" "" 28ifeq "$(LUAROCKS)" ""
@@ -116,32 +115,21 @@ ifeq "$(shell uname -s)" "BSD"
116 LIBS += -lpthread 115 LIBS += -lpthread
117endif 116endif
118 117
118MODULE_DIR=$(MODULE)
119
119#--- 120#---
120all: lua51-$(MODULE)$(_SO) 121all: $(MODULE)/core.$(_SO)
121 122
122%.o: %.c *.h Makefile 123%.o: %.c *.h Makefile
123 124
124# Note: Don't put $(LUA_LIBS) ahead of $^; MSYS will not like that (I think) 125# Note: Don't put $(LUA_LIBS) ahead of $^; MSYS will not like that (I think)
125# 126#
126lua51-$(MODULE)$(_SO): $(OBJ) 127$(MODULE_DIR)/core.$(_SO): $(OBJ)
128 mkdir -p $(MODULE_DIR)
127 $(CC) $(LIBFLAG) $^ $(LIBS) $(LUA_LIBS) -o $@ 129 $(CC) $(LIBFLAG) $^ $(LIBS) $(LUA_LIBS) -o $@
128 130
129clean: 131clean:
130 -rm -rf lua51-$(MODULE)$(_SO) *.lch *.o *.tmp *.map 132 -rm -rf $(MODULE)/core.$(_SO) *.o *.map
131
132lanes.o: keeper.lch
133
134# Note: 'luac -o -' could be used on systems other than Windows (where pipes
135# are binary). We need to support MinGW as well, so a temporary file.
136#
137# mode 1: embed bytecode version
138#%.lch: %.lua
139# $(LUAC) -o $@.tmp $<
140# $(LUA) ../tools/bin2c.lua $@.tmp -o $@
141# -rm $@.tmp
142# mode 2: embed text version (LuaJIT2-compatible)
143%.lch: %.lua
144 $(LUA) ../tools/bin2c.lua $< -o $@
145 133
146#--- 134#---
147# NSLU2 "slug" Linux ARM 135# NSLU2 "slug" Linux ARM
@@ -166,10 +154,9 @@ win32: $(WIN32_LUA51)/include/lua.h
166 $(MAKE) build CC=$(MINGW_GCC) \ 154 $(MAKE) build CC=$(MINGW_GCC) \
167 LUA_FLAGS=-I$(WIN32_LUA51)/include \ 155 LUA_FLAGS=-I$(WIN32_LUA51)/include \
168 LUA_LIBS="-L$(WIN32_LUA51) -llua51" \ 156 LUA_LIBS="-L$(WIN32_LUA51) -llua51" \
169 _SO=.dll \ 157 _SO=dll \
170 SO_FLAGS=-shared \ 158 SO_FLAGS=-shared \
171 LUA=lua51 \ 159 LUA=lua51
172 LUAC=luac51
173 160
174$(WIN32_LUA51)/include/lua.h: 161$(WIN32_LUA51)/include/lua.h:
175 @echo "Usage: make win32 WIN32_LUA51=<path of extracted LuaBinaries dll8 and dev packages>" 162 @echo "Usage: make win32 WIN32_LUA51=<path of extracted LuaBinaries dll8 and dev packages>"
diff --git a/src/keeper.c b/src/keeper.c
index 6f5bd95..2404520 100644
--- a/src/keeper.c
+++ b/src/keeper.c
@@ -64,12 +64,6 @@ static struct s_Keeper *GKeepers = NULL;
64static int GNbKeepers = 0; 64static int GNbKeepers = 0;
65 65
66/* 66/*
67* Lua code for the keeper states (baked in)
68*/
69static char const keeper_chunk[]=
70#include "keeper.lch"
71
72/*
73* Initialize keeper states 67* Initialize keeper states
74* 68*
75* If there is a problem, return an error message (NULL for okay). 69* If there is a problem, return an error message (NULL for okay).
@@ -95,26 +89,31 @@ char const* init_keepers( int const _nbKeepers, lua_CFunction _on_state_create)
95 if (!K) 89 if (!K)
96 return "out of memory"; 90 return "out of memory";
97 91
92 STACK_CHECK( K)
98 // to see VM name in Decoda debugger 93 // to see VM name in Decoda debugger
99 lua_pushliteral( K, "Keeper #"); 94 lua_pushliteral( K, "Keeper #");
100 lua_pushinteger( K, i + 1); 95 lua_pushinteger( K, i + 1);
101 lua_concat( K, 2); 96 lua_concat( K, 2);
102 lua_setglobal( K, "decoda_name"); 97 lua_setglobal( K, "decoda_name");
103 98
104 // Read in the preloaded chunk (and run it) 99 // use package.loaders[2] to find keeper microcode
105 // 100 lua_getfield( K, LUA_GLOBALSINDEX, "package"); // package
106 if( luaL_loadbuffer( K, keeper_chunk, sizeof(keeper_chunk), "@keeper.lua")) 101 lua_getfield( K, -1, "loaders"); // package package.loaders
107 return "luaL_loadbuffer() failed"; // LUA_ERRMEM 102 lua_rawgeti( K, -1, 2); // package package.loaders package.loaders[2]
108 103 lua_pushliteral( K, "lanes-keeper"); // package package.loaders package.loaders[2] "lanes-keeper"
109 if( lua_pcall( K, 0 /*args*/, 0 /*results*/, 0 /*errfunc*/)) 104 STACK_MID( K, 4);
105 // first pcall loads lanes-keeper.lua, second one runs the chunk
106 if( lua_pcall( K, 1 /*args*/, 1 /*results*/, 0 /*errfunc*/) || lua_pcall( K, 0 /*args*/, 0 /*results*/, 0 /*errfunc*/))
110 { 107 {
111 // LUA_ERRRUN / LUA_ERRMEM / LUA_ERRERR 108 // LUA_ERRRUN / LUA_ERRMEM / LUA_ERRERR
112 // 109 //
113 char const* err = lua_tostring( K, -1); 110 char const* err = lua_tostring( K, -1);
114 assert( err); 111 assert( err);
115 return err; 112 return err;
116 } 113 } // package package.loaders
117 114 STACK_MID( K, 2);
115 lua_pop( K, 2);
116 STACK_END( K, 0)
118 MUTEX_INIT( &GKeepers[i].lock_); 117 MUTEX_INIT( &GKeepers[i].lock_);
119 GKeepers[i].L = K; 118 GKeepers[i].L = K;
120 //GKeepers[i].count = 0; 119 //GKeepers[i].count = 0;
diff --git a/src/keeper.lua b/src/lanes-keeper.lua
index 1f17599..1f17599 100644
--- a/src/keeper.lua
+++ b/src/lanes-keeper.lua
diff --git a/src/lanes.c b/src/lanes.c
index 3e906c0..300c924 100644
--- a/src/lanes.c
+++ b/src/lanes.c
@@ -51,7 +51,7 @@
51 * ... 51 * ...
52 */ 52 */
53 53
54char const* VERSION = "3.1.4"; 54char const* VERSION = "3.1.5";
55 55
56/* 56/*
57=============================================================================== 57===============================================================================
@@ -868,7 +868,7 @@ static void linda_id( lua_State *L, char const * const which)
868 // in other words, forever. 868 // in other words, forever.
869 lua_pushnil( L); 869 lua_pushnil( L);
870 // other idfuncs must push a string naming the module they come from 870 // other idfuncs must push a string naming the module they come from
871 //lua_pushliteral( L, "lua51-lanes"); 871 //lua_pushliteral( L, "lanes.core");
872 } 872 }
873} 873}
874 874
@@ -1697,11 +1697,11 @@ LUAG_FUNC( thread_new )
1697 1697
1698 // modules to require in the target lane *before* the function is transfered! 1698 // modules to require in the target lane *before* the function is transfered!
1699 1699
1700 //start by requiring lua51-lanes, since it is a bit special 1700 //start by requiring lanes.core, since it is a bit special
1701 // it is not fatal if 'require' isn't loaded, just ignore (may cause function transfer errors later on if the lane pulls the lanes module itself) 1701 // it is not fatal if 'require' isn't loaded, just ignore (may cause function transfer errors later on if the lane pulls the lanes module itself)
1702 STACK_CHECK(L) 1702 STACK_CHECK(L)
1703 STACK_CHECK(L2) 1703 STACK_CHECK(L2)
1704 lua_pushliteral( L, "lua51-lanes"); 1704 lua_pushliteral( L, "lanes.core");
1705 require_one_module( L, L2, FALSE); 1705 require_one_module( L, L2, FALSE);
1706 lua_pop( L, 1); 1706 lua_pop( L, 1);
1707 STACK_END(L2,0) 1707 STACK_END(L2,0)
@@ -2510,7 +2510,7 @@ int
2510#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) 2510#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
2511__declspec(dllexport) 2511__declspec(dllexport)
2512#endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) 2512#endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
2513luaopen_lanes( lua_State *L ) 2513luaopen_lanes_core( lua_State *L )
2514{ 2514{
2515 // Create main module interface table 2515 // Create main module interface table
2516 // we only have 1 closure, which must be called to configure Lanes 2516 // we only have 1 closure, which must be called to configure Lanes
diff --git a/src/lanes.lua b/src/lanes.lua
index bd66e6c..8cd3737 100644
--- a/src/lanes.lua
+++ b/src/lanes.lua
@@ -55,10 +55,10 @@ if _params.on_state_create and (type( _params.on_state_create) ~= "function") th
55 error( "Bad on_state_create: " .. tostring( _params.on_state_create), 2) 55 error( "Bad on_state_create: " .. tostring( _params.on_state_create), 2)
56end 56end
57 57
58local mm = require "lua51-lanes" 58local mm = require "lanes.core"
59assert( type(mm)=="table" ) 59assert( type(mm)=="table" )
60 60
61-- configure() is available only the first time lua51-lanes is required process-wide, and we *must* call it to have the other functions in the interface 61-- configure() is available only the first time lanes.core is required process-wide, and we *must* call it to have the other functions in the interface
62if mm.configure then mm.configure( _params.nb_keepers, _params.on_state_create) end 62if mm.configure then mm.configure( _params.nb_keepers, _params.on_state_create) end
63 63
64local thread_new = assert(mm.thread_new) 64local thread_new = assert(mm.thread_new)
diff --git a/tools/bin2c.lua b/tools/bin2c.lua
deleted file mode 100644
index 352d18e..0000000
--- a/tools/bin2c.lua
+++ /dev/null
@@ -1,131 +0,0 @@
1--
2-- BIN2C.LUA [filename] [-o output.lch]
3--
4-- Convert files to byte arrays for automatic loading with 'lua_dobuffer'.
5--
6-- Based on 'etc/bin2c.c' of Lua 5.0.1 sources by:
7-- Luiz Henrique de Figueiredo (lhf@tecgraf.puc-rio.br)
8--
9-- Changes:
10--
11-- 12-Dec-07/AKa: changed the output to have just the "{ ... }" part; others
12-- (variable name) can be explicitly given before the '#include'
13-- 16-Nov-07/AKa: taken into luaSub
14-- 16-Mar-04/AKa: added 'glua_wrap()' support
15-- xx-Jan-04/AKa: subdirectory names are not included in debug info
16--
17
18local function USAGE()
19 io.stderr:write "lua bin2c.lua [filename] [-o output.lch]"
20 os.exit(-1)
21end
22
23local out_f -- file to output to (stdout if nil)
24
25local function OUT( ... )
26 (out_f or io.stdout): write( ... ); -- ; actually needed by Lua
27 (out_f or io.stdout): write "\n"
28end
29
30local HEAD= "{ "
31local START= ' '
32local FILL= '%3d,'
33local STOP= ""
34local TAIL= "};\n"
35
36--
37local function dump( f )
38 --
39 OUT [[
40/* bin2c.lua generated code -- DO NOT EDIT
41 *
42 * To use from C source:
43 * char my_chunk[]=
44 * #include "my.lch"
45 */]]
46
47 local str= HEAD..'\n'..START
48 local len= 0
49
50 while true do
51 for n=1,20 do
52 local c= f:read(1)
53 if c then
54 str= str..string.format( FILL, string.byte(c) )
55 len= len+1
56 else
57 OUT( str..STOP.. string.format( TAIL, len ) )
58 return -- the end
59 end
60 end
61 OUT(str..STOP)
62 str= START
63 end
64end
65
66--
67local function fdump( fn )
68 --
69 local f= io.open( fn, "rb" ) -- must open as binary
70
71 if not f then
72 error( "bin2c: cannot open "..fn )
73 else
74 dump( f )
75 f:close()
76 end
77end
78
79--
80local function main( argv )
81 --
82 local fn= argv.o
83 if fn then
84 local f,err= io.open( fn, "w" )
85 assert( f, "Unable to write '"..fn.."': "..(err or "") )
86
87 out_f= f
88 end
89
90 if argv[2] then
91 USAGE()
92 elseif argv[1] then
93 fdump( argv[1] )
94 else -- use stdin (no params)
95 if os.getenv("WINDIR") then
96 error "using stdin not allowed on Win32!" -- it wouldn't be binary
97 end
98 dump(io.stdin)
99 end
100
101 if out_f then
102 out_f:close()
103 end
104end
105
106--
107local argv= {}
108local valid_flags= { o=1 } -- lookup: 0=no value, 1=value
109
110-- Need to use while since '-o' advances 'i' by 2
111--
112local args= select('#',...)
113local i=1
114
115while i<=args do
116 local v= select(i,...)
117 local flag= string.match( v, "^%-(.+)" )
118
119 if flag then
120 if not valid_flags[flag] then
121 error( "Unknown flag: -"..flag )
122 end
123 argv[flag]= (i+1<=args) and select(i+1,...) or true
124 i= i+1
125 else
126 table.insert( argv, v ) -- [1..N]
127 end
128 i= i+1
129end
130
131return main(argv)