candle 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.md +4 -0
  3. data/VERSION +1 -0
  4. data/bin/candle +21 -0
  5. data/lib/candle.rb +68 -0
  6. data/lib/candle/command.rb +27 -0
  7. data/lib/candle/generators/actions.rb +183 -0
  8. data/lib/candle/generators/blank.rb +127 -0
  9. data/lib/candle/generators/cli.rb +55 -0
  10. data/lib/candle/generators/help.rb +47 -0
  11. data/lib/candle/generators/jam.rb +80 -0
  12. data/lib/candle/generators/lua/scripts/AppDelegate.lua +36 -0
  13. data/lib/candle/generators/lua/scripts/tests/init.lua +6 -0
  14. data/lib/candle/generators/lua/scripts/tests/someTest.lua +12 -0
  15. data/lib/candle/generators/lua/wax/ProtocolLoader.h +12 -0
  16. data/lib/candle/generators/lua/wax/bin/hammer +157 -0
  17. data/lib/candle/generators/lua/wax/bin/update-xibs +131 -0
  18. data/lib/candle/generators/lua/wax/bin/waxsim +0 -0
  19. data/lib/candle/generators/lua/wax/lib/build-scripts/compile-stdlib.sh +14 -0
  20. data/lib/candle/generators/lua/wax/lib/build-scripts/copy-scripts.sh +58 -0
  21. data/lib/candle/generators/lua/wax/lib/build-scripts/luac.lua +80 -0
  22. data/lib/candle/generators/lua/wax/lib/extensions/CGAffine/wax_CGTransform.h +12 -0
  23. data/lib/candle/generators/lua/wax/lib/extensions/CGAffine/wax_CGTransform.m +85 -0
  24. data/lib/candle/generators/lua/wax/lib/extensions/CGContext/wax_CGContext.h +12 -0
  25. data/lib/candle/generators/lua/wax/lib/extensions/CGContext/wax_CGContext.m +251 -0
  26. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http.h +14 -0
  27. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http.m +240 -0
  28. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http_connection.h +54 -0
  29. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http_connection.m +304 -0
  30. data/lib/candle/generators/lua/wax/lib/extensions/filesystem/wax_filesystem.h +9 -0
  31. data/lib/candle/generators/lua/wax/lib/extensions/filesystem/wax_filesystem.m +273 -0
  32. data/lib/candle/generators/lua/wax/lib/extensions/json/Rakefile +10 -0
  33. data/lib/candle/generators/lua/wax/lib/extensions/json/wax_json.c +304 -0
  34. data/lib/candle/generators/lua/wax/lib/extensions/json/wax_json.h +11 -0
  35. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl-1.0.9.tar.gz +0 -0
  36. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_common.h +85 -0
  37. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_gen.h +159 -0
  38. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_parse.h +193 -0
  39. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl.c +159 -0
  40. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_alloc.c +65 -0
  41. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_alloc.h +50 -0
  42. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_buf.c +119 -0
  43. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_buf.h +73 -0
  44. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_bytestack.h +85 -0
  45. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_common.h +85 -0
  46. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_encode.c +188 -0
  47. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_encode.h +50 -0
  48. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_gen.c +322 -0
  49. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_gen.h +159 -0
  50. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_lex.c +737 -0
  51. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_lex.h +133 -0
  52. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parse.h +193 -0
  53. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parser.c +448 -0
  54. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parser.h +82 -0
  55. data/lib/candle/generators/lua/wax/lib/lua/lapi.c +1087 -0
  56. data/lib/candle/generators/lua/wax/lib/lua/lapi.h +16 -0
  57. data/lib/candle/generators/lua/wax/lib/lua/lauxlib.c +652 -0
  58. data/lib/candle/generators/lua/wax/lib/lua/lauxlib.h +174 -0
  59. data/lib/candle/generators/lua/wax/lib/lua/lbaselib.c +653 -0
  60. data/lib/candle/generators/lua/wax/lib/lua/lcode.c +839 -0
  61. data/lib/candle/generators/lua/wax/lib/lua/lcode.h +76 -0
  62. data/lib/candle/generators/lua/wax/lib/lua/ldblib.c +397 -0
  63. data/lib/candle/generators/lua/wax/lib/lua/ldebug.c +638 -0
  64. data/lib/candle/generators/lua/wax/lib/lua/ldebug.h +33 -0
  65. data/lib/candle/generators/lua/wax/lib/lua/ldo.c +518 -0
  66. data/lib/candle/generators/lua/wax/lib/lua/ldo.h +57 -0
  67. data/lib/candle/generators/lua/wax/lib/lua/ldump.c +164 -0
  68. data/lib/candle/generators/lua/wax/lib/lua/lfunc.c +174 -0
  69. data/lib/candle/generators/lua/wax/lib/lua/lfunc.h +34 -0
  70. data/lib/candle/generators/lua/wax/lib/lua/lgc.c +711 -0
  71. data/lib/candle/generators/lua/wax/lib/lua/lgc.h +110 -0
  72. data/lib/candle/generators/lua/wax/lib/lua/linit.c +38 -0
  73. data/lib/candle/generators/lua/wax/lib/lua/liolib.c +553 -0
  74. data/lib/candle/generators/lua/wax/lib/lua/llex.c +461 -0
  75. data/lib/candle/generators/lua/wax/lib/lua/llex.h +81 -0
  76. data/lib/candle/generators/lua/wax/lib/lua/llimits.h +128 -0
  77. data/lib/candle/generators/lua/wax/lib/lua/lmathlib.c +263 -0
  78. data/lib/candle/generators/lua/wax/lib/lua/lmem.c +86 -0
  79. data/lib/candle/generators/lua/wax/lib/lua/lmem.h +49 -0
  80. data/lib/candle/generators/lua/wax/lib/lua/loadlib.c +666 -0
  81. data/lib/candle/generators/lua/wax/lib/lua/lobject.c +214 -0
  82. data/lib/candle/generators/lua/wax/lib/lua/lobject.h +381 -0
  83. data/lib/candle/generators/lua/wax/lib/lua/lopcodes.c +102 -0
  84. data/lib/candle/generators/lua/wax/lib/lua/lopcodes.h +268 -0
  85. data/lib/candle/generators/lua/wax/lib/lua/loslib.c +243 -0
  86. data/lib/candle/generators/lua/wax/lib/lua/lparser.c +1339 -0
  87. data/lib/candle/generators/lua/wax/lib/lua/lparser.h +82 -0
  88. data/lib/candle/generators/lua/wax/lib/lua/lstate.c +214 -0
  89. data/lib/candle/generators/lua/wax/lib/lua/lstate.h +169 -0
  90. data/lib/candle/generators/lua/wax/lib/lua/lstring.c +111 -0
  91. data/lib/candle/generators/lua/wax/lib/lua/lstring.h +31 -0
  92. data/lib/candle/generators/lua/wax/lib/lua/lstrlib.c +869 -0
  93. data/lib/candle/generators/lua/wax/lib/lua/ltable.c +588 -0
  94. data/lib/candle/generators/lua/wax/lib/lua/ltable.h +40 -0
  95. data/lib/candle/generators/lua/wax/lib/lua/ltablib.c +287 -0
  96. data/lib/candle/generators/lua/wax/lib/lua/ltm.c +75 -0
  97. data/lib/candle/generators/lua/wax/lib/lua/ltm.h +54 -0
  98. data/lib/candle/generators/lua/wax/lib/lua/lua.h +388 -0
  99. data/lib/candle/generators/lua/wax/lib/lua/luaconf.h +753 -0
  100. data/lib/candle/generators/lua/wax/lib/lua/lualib.h +53 -0
  101. data/lib/candle/generators/lua/wax/lib/lua/lundump.c +227 -0
  102. data/lib/candle/generators/lua/wax/lib/lua/lundump.h +36 -0
  103. data/lib/candle/generators/lua/wax/lib/lua/lvm.c +763 -0
  104. data/lib/candle/generators/lua/wax/lib/lua/lvm.h +36 -0
  105. data/lib/candle/generators/lua/wax/lib/lua/lzio.c +82 -0
  106. data/lib/candle/generators/lua/wax/lib/lua/lzio.h +67 -0
  107. data/lib/candle/generators/lua/wax/lib/lua/print.c +227 -0
  108. data/lib/candle/generators/lua/wax/lib/project.rake +159 -0
  109. data/lib/candle/generators/lua/wax/lib/stdlib/enums.lua +396 -0
  110. data/lib/candle/generators/lua/wax/lib/stdlib/ext/http.lua +43 -0
  111. data/lib/candle/generators/lua/wax/lib/stdlib/ext/init.lua +4 -0
  112. data/lib/candle/generators/lua/wax/lib/stdlib/ext/number.lua +21 -0
  113. data/lib/candle/generators/lua/wax/lib/stdlib/ext/string.lua +97 -0
  114. data/lib/candle/generators/lua/wax/lib/stdlib/ext/table.lua +165 -0
  115. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/WaxServer.lua +49 -0
  116. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/autoload.lua +10 -0
  117. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/base64.lua +64 -0
  118. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/bit.lua +274 -0
  119. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/cache.lua +73 -0
  120. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/callback.lua +22 -0
  121. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/frame.lua +76 -0
  122. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/init.lua +78 -0
  123. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/pickView.lua +54 -0
  124. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/time.lua +102 -0
  125. data/lib/candle/generators/lua/wax/lib/stdlib/init.lua +18 -0
  126. data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/init.lua +2 -0
  127. data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/luamock.lua +84 -0
  128. data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/luaspec.lua +377 -0
  129. data/lib/candle/generators/lua/wax/lib/stdlib/repl.lua +9 -0
  130. data/lib/candle/generators/lua/wax/lib/stdlib/structs.lua +11 -0
  131. data/lib/candle/generators/lua/wax/lib/stdlib/waxClass.lua +42 -0
  132. data/lib/candle/generators/lua/wax/lib/wax.h +16 -0
  133. data/lib/candle/generators/lua/wax/lib/wax.m +260 -0
  134. data/lib/candle/generators/lua/wax/lib/wax_class.h +18 -0
  135. data/lib/candle/generators/lua/wax/lib/wax_class.m +190 -0
  136. data/lib/candle/generators/lua/wax/lib/wax_gc.h +20 -0
  137. data/lib/candle/generators/lua/wax/lib/wax_gc.m +56 -0
  138. data/lib/candle/generators/lua/wax/lib/wax_helpers.h +102 -0
  139. data/lib/candle/generators/lua/wax/lib/wax_helpers.m +870 -0
  140. data/lib/candle/generators/lua/wax/lib/wax_instance.h +34 -0
  141. data/lib/candle/generators/lua/wax/lib/wax_instance.m +810 -0
  142. data/lib/candle/generators/lua/wax/lib/wax_server.h +47 -0
  143. data/lib/candle/generators/lua/wax/lib/wax_server.m +252 -0
  144. data/lib/candle/generators/lua/wax/lib/wax_stdlib.h +3 -0
  145. data/lib/candle/generators/lua/wax/lib/wax_struct.h +26 -0
  146. data/lib/candle/generators/lua/wax/lib/wax_struct.m +335 -0
  147. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.pbxproj +836 -0
  148. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  149. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.xcworkspace/xcuserdata/eiffel.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  150. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/WaxApplication.xcscheme +86 -0
  151. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/xcschememanagement.plist +22 -0
  152. data/lib/candle/generators/templates/blank/WaxApplication/Default-568h@2x.png +0 -0
  153. data/lib/candle/generators/templates/blank/WaxApplication/Default.png +0 -0
  154. data/lib/candle/generators/templates/blank/WaxApplication/Default@2x.png +0 -0
  155. data/lib/candle/generators/templates/blank/WaxApplication/ProtocolLoader.h +12 -0
  156. data/lib/candle/generators/templates/blank/WaxApplication/WaxApplication-Info.plist.tt +38 -0
  157. data/lib/candle/generators/templates/blank/WaxApplication/WaxApplication-Prefix.pch.tt +14 -0
  158. data/lib/candle/generators/templates/blank/WaxApplication/en.lproj/InfoPlist.strings +2 -0
  159. data/lib/candle/generators/templates/blank/WaxApplication/main.m.tt +20 -0
  160. data/lib/candle/tasks.rb +22 -0
  161. data/lib/candle/utility.rb +30 -0
  162. data/lib/candle/version.rb +9 -0
  163. data/lib/candle/view.rb +48 -0
  164. metadata +582 -0
@@ -0,0 +1,110 @@
1
+ /*
2
+ ** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $
3
+ ** Garbage Collector
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+ #ifndef lgc_h
8
+ #define lgc_h
9
+
10
+
11
+ #include "lobject.h"
12
+
13
+
14
+ /*
15
+ ** Possible states of the Garbage Collector
16
+ */
17
+ #define GCSpause 0
18
+ #define GCSpropagate 1
19
+ #define GCSsweepstring 2
20
+ #define GCSsweep 3
21
+ #define GCSfinalize 4
22
+
23
+
24
+ /*
25
+ ** some userful bit tricks
26
+ */
27
+ #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
28
+ #define setbits(x,m) ((x) |= (m))
29
+ #define testbits(x,m) ((x) & (m))
30
+ #define bitmask(b) (1<<(b))
31
+ #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
32
+ #define l_setbit(x,b) setbits(x, bitmask(b))
33
+ #define resetbit(x,b) resetbits(x, bitmask(b))
34
+ #define testbit(x,b) testbits(x, bitmask(b))
35
+ #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))
36
+ #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2)))
37
+ #define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2)))
38
+
39
+
40
+
41
+ /*
42
+ ** Layout for bit use in `marked' field:
43
+ ** bit 0 - object is white (type 0)
44
+ ** bit 1 - object is white (type 1)
45
+ ** bit 2 - object is black
46
+ ** bit 3 - for userdata: has been finalized
47
+ ** bit 3 - for tables: has weak keys
48
+ ** bit 4 - for tables: has weak values
49
+ ** bit 5 - object is fixed (should not be collected)
50
+ ** bit 6 - object is "super" fixed (only the main thread)
51
+ */
52
+
53
+
54
+ #define WHITE0BIT 0
55
+ #define WHITE1BIT 1
56
+ #define BLACKBIT 2
57
+ #define FINALIZEDBIT 3
58
+ #define KEYWEAKBIT 3
59
+ #define VALUEWEAKBIT 4
60
+ #define FIXEDBIT 5
61
+ #define SFIXEDBIT 6
62
+ #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
63
+
64
+
65
+ #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
66
+ #define isblack(x) testbit((x)->gch.marked, BLACKBIT)
67
+ #define isgray(x) (!isblack(x) && !iswhite(x))
68
+
69
+ #define otherwhite(g) (g->currentwhite ^ WHITEBITS)
70
+ #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS)
71
+
72
+ #define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
73
+ #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
74
+
75
+ #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
76
+
77
+ #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
78
+
79
+
80
+ #define luaC_checkGC(L) { \
81
+ condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \
82
+ if (G(L)->totalbytes >= G(L)->GCthreshold) \
83
+ luaC_step(L); }
84
+
85
+
86
+ #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
87
+ luaC_barrierf(L,obj2gco(p),gcvalue(v)); }
88
+
89
+ #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \
90
+ luaC_barrierback(L,t); }
91
+
92
+ #define luaC_objbarrier(L,p,o) \
93
+ { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
94
+ luaC_barrierf(L,obj2gco(p),obj2gco(o)); }
95
+
96
+ #define luaC_objbarriert(L,t,o) \
97
+ { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); }
98
+
99
+ LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all);
100
+ LUAI_FUNC void luaC_callGCTM (lua_State *L);
101
+ LUAI_FUNC void luaC_freeall (lua_State *L);
102
+ LUAI_FUNC void luaC_step (lua_State *L);
103
+ LUAI_FUNC void luaC_fullgc (lua_State *L);
104
+ LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
105
+ LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv);
106
+ LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v);
107
+ LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t);
108
+
109
+
110
+ #endif
@@ -0,0 +1,38 @@
1
+ /*
2
+ ** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $
3
+ ** Initialization of libraries for lua.c
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+
8
+ #define linit_c
9
+ #define LUA_LIB
10
+
11
+ #include "lua.h"
12
+
13
+ #include "lualib.h"
14
+ #include "lauxlib.h"
15
+
16
+
17
+ static const luaL_Reg lualibs[] = {
18
+ {"", luaopen_base},
19
+ {LUA_LOADLIBNAME, luaopen_package},
20
+ {LUA_TABLIBNAME, luaopen_table},
21
+ {LUA_IOLIBNAME, luaopen_io},
22
+ {LUA_OSLIBNAME, luaopen_os},
23
+ {LUA_STRLIBNAME, luaopen_string},
24
+ {LUA_MATHLIBNAME, luaopen_math},
25
+ {LUA_DBLIBNAME, luaopen_debug},
26
+ {NULL, NULL}
27
+ };
28
+
29
+
30
+ LUALIB_API void luaL_openlibs (lua_State *L) {
31
+ const luaL_Reg *lib = lualibs;
32
+ for (; lib->func; lib++) {
33
+ lua_pushcfunction(L, lib->func);
34
+ lua_pushstring(L, lib->name);
35
+ lua_call(L, 1, 0);
36
+ }
37
+ }
38
+
@@ -0,0 +1,553 @@
1
+ /*
2
+ ** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $
3
+ ** Standard I/O (and system) library
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+
8
+ #include <errno.h>
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <string.h>
12
+
13
+ #define liolib_c
14
+ #define LUA_LIB
15
+
16
+ #include "lua.h"
17
+
18
+ #include "lauxlib.h"
19
+ #include "lualib.h"
20
+
21
+
22
+
23
+ #define IO_INPUT 1
24
+ #define IO_OUTPUT 2
25
+
26
+
27
+ static const char *const fnames[] = {"input", "output"};
28
+
29
+
30
+ static int pushresult (lua_State *L, int i, const char *filename) {
31
+ int en = errno; /* calls to Lua API may change this value */
32
+ if (i) {
33
+ lua_pushboolean(L, 1);
34
+ return 1;
35
+ }
36
+ else {
37
+ lua_pushnil(L);
38
+ if (filename)
39
+ lua_pushfstring(L, "%s: %s", filename, strerror(en));
40
+ else
41
+ lua_pushfstring(L, "%s", strerror(en));
42
+ lua_pushinteger(L, en);
43
+ return 3;
44
+ }
45
+ }
46
+
47
+
48
+ static void fileerror (lua_State *L, int arg, const char *filename) {
49
+ lua_pushfstring(L, "%s: %s", filename, strerror(errno));
50
+ luaL_argerror(L, arg, lua_tostring(L, -1));
51
+ }
52
+
53
+
54
+ #define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
55
+
56
+
57
+ static int io_type (lua_State *L) {
58
+ void *ud;
59
+ luaL_checkany(L, 1);
60
+ ud = lua_touserdata(L, 1);
61
+ lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
62
+ if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
63
+ lua_pushnil(L); /* not a file */
64
+ else if (*((FILE **)ud) == NULL)
65
+ lua_pushliteral(L, "closed file");
66
+ else
67
+ lua_pushliteral(L, "file");
68
+ return 1;
69
+ }
70
+
71
+
72
+ static FILE *tofile (lua_State *L) {
73
+ FILE **f = tofilep(L);
74
+ if (*f == NULL)
75
+ luaL_error(L, "attempt to use a closed file");
76
+ return *f;
77
+ }
78
+
79
+
80
+
81
+ /*
82
+ ** When creating file handles, always creates a `closed' file handle
83
+ ** before opening the actual file; so, if there is a memory error, the
84
+ ** file is not left opened.
85
+ */
86
+ static FILE **newfile (lua_State *L) {
87
+ FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
88
+ *pf = NULL; /* file handle is currently `closed' */
89
+ luaL_getmetatable(L, LUA_FILEHANDLE);
90
+ lua_setmetatable(L, -2);
91
+ return pf;
92
+ }
93
+
94
+
95
+ /*
96
+ ** function to (not) close the standard files stdin, stdout, and stderr
97
+ */
98
+ static int io_noclose (lua_State *L) {
99
+ lua_pushnil(L);
100
+ lua_pushliteral(L, "cannot close standard file");
101
+ return 2;
102
+ }
103
+
104
+
105
+ /*
106
+ ** function to close 'popen' files
107
+ */
108
+ static int io_pclose (lua_State *L) {
109
+ FILE **p = tofilep(L);
110
+ int ok = lua_pclose(L, *p);
111
+ *p = NULL;
112
+ return pushresult(L, ok, NULL);
113
+ }
114
+
115
+
116
+ /*
117
+ ** function to close regular files
118
+ */
119
+ static int io_fclose (lua_State *L) {
120
+ FILE **p = tofilep(L);
121
+ int ok = (fclose(*p) == 0);
122
+ *p = NULL;
123
+ return pushresult(L, ok, NULL);
124
+ }
125
+
126
+
127
+ static int aux_close (lua_State *L) {
128
+ lua_getfenv(L, 1);
129
+ lua_getfield(L, -1, "__close");
130
+ return (lua_tocfunction(L, -1))(L);
131
+ }
132
+
133
+
134
+ static int io_close (lua_State *L) {
135
+ if (lua_isnone(L, 1))
136
+ lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
137
+ tofile(L); /* make sure argument is a file */
138
+ return aux_close(L);
139
+ }
140
+
141
+
142
+ static int io_gc (lua_State *L) {
143
+ FILE *f = *tofilep(L);
144
+ /* ignore closed files */
145
+ if (f != NULL)
146
+ aux_close(L);
147
+ return 0;
148
+ }
149
+
150
+
151
+ static int io_tostring (lua_State *L) {
152
+ FILE *f = *tofilep(L);
153
+ if (f == NULL)
154
+ lua_pushliteral(L, "file (closed)");
155
+ else
156
+ lua_pushfstring(L, "file (%p)", f);
157
+ return 1;
158
+ }
159
+
160
+
161
+ static int io_open (lua_State *L) {
162
+ const char *filename = luaL_checkstring(L, 1);
163
+ const char *mode = luaL_optstring(L, 2, "r");
164
+ FILE **pf = newfile(L);
165
+ *pf = fopen(filename, mode);
166
+ return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
167
+ }
168
+
169
+
170
+ /*
171
+ ** this function has a separated environment, which defines the
172
+ ** correct __close for 'popen' files
173
+ */
174
+ static int io_popen (lua_State *L) {
175
+ const char *filename = luaL_checkstring(L, 1);
176
+ const char *mode = luaL_optstring(L, 2, "r");
177
+ FILE **pf = newfile(L);
178
+ *pf = lua_popen(L, filename, mode);
179
+ return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
180
+ }
181
+
182
+
183
+ static int io_tmpfile (lua_State *L) {
184
+ FILE **pf = newfile(L);
185
+ *pf = tmpfile();
186
+ return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
187
+ }
188
+
189
+
190
+ static FILE *getiofile (lua_State *L, int findex) {
191
+ FILE *f;
192
+ lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
193
+ f = *(FILE **)lua_touserdata(L, -1);
194
+ if (f == NULL)
195
+ luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
196
+ return f;
197
+ }
198
+
199
+
200
+ static int g_iofile (lua_State *L, int f, const char *mode) {
201
+ if (!lua_isnoneornil(L, 1)) {
202
+ const char *filename = lua_tostring(L, 1);
203
+ if (filename) {
204
+ FILE **pf = newfile(L);
205
+ *pf = fopen(filename, mode);
206
+ if (*pf == NULL)
207
+ fileerror(L, 1, filename);
208
+ }
209
+ else {
210
+ tofile(L); /* check that it's a valid file handle */
211
+ lua_pushvalue(L, 1);
212
+ }
213
+ lua_rawseti(L, LUA_ENVIRONINDEX, f);
214
+ }
215
+ /* return current value */
216
+ lua_rawgeti(L, LUA_ENVIRONINDEX, f);
217
+ return 1;
218
+ }
219
+
220
+
221
+ static int io_input (lua_State *L) {
222
+ return g_iofile(L, IO_INPUT, "r");
223
+ }
224
+
225
+
226
+ static int io_output (lua_State *L) {
227
+ return g_iofile(L, IO_OUTPUT, "w");
228
+ }
229
+
230
+
231
+ static int io_readline (lua_State *L);
232
+
233
+
234
+ static void aux_lines (lua_State *L, int idx, int toclose) {
235
+ lua_pushvalue(L, idx);
236
+ lua_pushboolean(L, toclose); /* close/not close file when finished */
237
+ lua_pushcclosure(L, io_readline, 2);
238
+ }
239
+
240
+
241
+ static int f_lines (lua_State *L) {
242
+ tofile(L); /* check that it's a valid file handle */
243
+ aux_lines(L, 1, 0);
244
+ return 1;
245
+ }
246
+
247
+
248
+ static int io_lines (lua_State *L) {
249
+ if (lua_isnoneornil(L, 1)) { /* no arguments? */
250
+ /* will iterate over default input */
251
+ lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);
252
+ return f_lines(L);
253
+ }
254
+ else {
255
+ const char *filename = luaL_checkstring(L, 1);
256
+ FILE **pf = newfile(L);
257
+ *pf = fopen(filename, "r");
258
+ if (*pf == NULL)
259
+ fileerror(L, 1, filename);
260
+ aux_lines(L, lua_gettop(L), 1);
261
+ return 1;
262
+ }
263
+ }
264
+
265
+
266
+ /*
267
+ ** {======================================================
268
+ ** READ
269
+ ** =======================================================
270
+ */
271
+
272
+
273
+ static int read_number (lua_State *L, FILE *f) {
274
+ lua_Number d;
275
+ if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
276
+ lua_pushnumber(L, d);
277
+ return 1;
278
+ }
279
+ else return 0; /* read fails */
280
+ }
281
+
282
+
283
+ static int test_eof (lua_State *L, FILE *f) {
284
+ int c = getc(f);
285
+ ungetc(c, f);
286
+ lua_pushlstring(L, NULL, 0);
287
+ return (c != EOF);
288
+ }
289
+
290
+
291
+ static int read_line (lua_State *L, FILE *f) {
292
+ luaL_Buffer b;
293
+ luaL_buffinit(L, &b);
294
+ for (;;) {
295
+ size_t l;
296
+ char *p = luaL_prepbuffer(&b);
297
+ if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
298
+ luaL_pushresult(&b); /* close buffer */
299
+ return (lua_objlen(L, -1) > 0); /* check whether read something */
300
+ }
301
+ l = strlen(p);
302
+ if (l == 0 || p[l-1] != '\n')
303
+ luaL_addsize(&b, l);
304
+ else {
305
+ luaL_addsize(&b, l - 1); /* do not include `eol' */
306
+ luaL_pushresult(&b); /* close buffer */
307
+ return 1; /* read at least an `eol' */
308
+ }
309
+ }
310
+ }
311
+
312
+
313
+ static int read_chars (lua_State *L, FILE *f, size_t n) {
314
+ size_t rlen; /* how much to read */
315
+ size_t nr; /* number of chars actually read */
316
+ luaL_Buffer b;
317
+ luaL_buffinit(L, &b);
318
+ rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
319
+ do {
320
+ char *p = luaL_prepbuffer(&b);
321
+ if (rlen > n) rlen = n; /* cannot read more than asked */
322
+ nr = fread(p, sizeof(char), rlen, f);
323
+ luaL_addsize(&b, nr);
324
+ n -= nr; /* still have to read `n' chars */
325
+ } while (n > 0 && nr == rlen); /* until end of count or eof */
326
+ luaL_pushresult(&b); /* close buffer */
327
+ return (n == 0 || lua_objlen(L, -1) > 0);
328
+ }
329
+
330
+
331
+ static int g_read (lua_State *L, FILE *f, int first) {
332
+ int nargs = lua_gettop(L) - 1;
333
+ int success;
334
+ int n;
335
+ clearerr(f);
336
+ if (nargs == 0) { /* no arguments? */
337
+ success = read_line(L, f);
338
+ n = first+1; /* to return 1 result */
339
+ }
340
+ else { /* ensure stack space for all results and for auxlib's buffer */
341
+ luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
342
+ success = 1;
343
+ for (n = first; nargs-- && success; n++) {
344
+ if (lua_type(L, n) == LUA_TNUMBER) {
345
+ size_t l = (size_t)lua_tointeger(L, n);
346
+ success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
347
+ }
348
+ else {
349
+ const char *p = lua_tostring(L, n);
350
+ luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
351
+ switch (p[1]) {
352
+ case 'n': /* number */
353
+ success = read_number(L, f);
354
+ break;
355
+ case 'l': /* line */
356
+ success = read_line(L, f);
357
+ break;
358
+ case 'a': /* file */
359
+ read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
360
+ success = 1; /* always success */
361
+ break;
362
+ default:
363
+ return luaL_argerror(L, n, "invalid format");
364
+ }
365
+ }
366
+ }
367
+ }
368
+ if (ferror(f))
369
+ return pushresult(L, 0, NULL);
370
+ if (!success) {
371
+ lua_pop(L, 1); /* remove last result */
372
+ lua_pushnil(L); /* push nil instead */
373
+ }
374
+ return n - first;
375
+ }
376
+
377
+
378
+ static int io_read (lua_State *L) {
379
+ return g_read(L, getiofile(L, IO_INPUT), 1);
380
+ }
381
+
382
+
383
+ static int f_read (lua_State *L) {
384
+ return g_read(L, tofile(L), 2);
385
+ }
386
+
387
+
388
+ static int io_readline (lua_State *L) {
389
+ FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
390
+ int sucess;
391
+ if (f == NULL) /* file is already closed? */
392
+ luaL_error(L, "file is already closed");
393
+ sucess = read_line(L, f);
394
+ if (ferror(f))
395
+ return luaL_error(L, "%s", strerror(errno));
396
+ if (sucess) return 1;
397
+ else { /* EOF */
398
+ if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
399
+ lua_settop(L, 0);
400
+ lua_pushvalue(L, lua_upvalueindex(1));
401
+ aux_close(L); /* close it */
402
+ }
403
+ return 0;
404
+ }
405
+ }
406
+
407
+ /* }====================================================== */
408
+
409
+
410
+ static int g_write (lua_State *L, FILE *f, int arg) {
411
+ int nargs = lua_gettop(L) - 1;
412
+ int status = 1;
413
+ for (; nargs--; arg++) {
414
+ if (lua_type(L, arg) == LUA_TNUMBER) {
415
+ /* optimization: could be done exactly as for strings */
416
+ status = status &&
417
+ fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
418
+ }
419
+ else {
420
+ size_t l;
421
+ const char *s = luaL_checklstring(L, arg, &l);
422
+ status = status && (fwrite(s, sizeof(char), l, f) == l);
423
+ }
424
+ }
425
+ return pushresult(L, status, NULL);
426
+ }
427
+
428
+
429
+ static int io_write (lua_State *L) {
430
+ return g_write(L, getiofile(L, IO_OUTPUT), 1);
431
+ }
432
+
433
+
434
+ static int f_write (lua_State *L) {
435
+ return g_write(L, tofile(L), 2);
436
+ }
437
+
438
+
439
+ static int f_seek (lua_State *L) {
440
+ static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
441
+ static const char *const modenames[] = {"set", "cur", "end", NULL};
442
+ FILE *f = tofile(L);
443
+ int op = luaL_checkoption(L, 2, "cur", modenames);
444
+ long offset = luaL_optlong(L, 3, 0);
445
+ op = fseek(f, offset, mode[op]);
446
+ if (op)
447
+ return pushresult(L, 0, NULL); /* error */
448
+ else {
449
+ lua_pushinteger(L, ftell(f));
450
+ return 1;
451
+ }
452
+ }
453
+
454
+
455
+ static int f_setvbuf (lua_State *L) {
456
+ static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
457
+ static const char *const modenames[] = {"no", "full", "line", NULL};
458
+ FILE *f = tofile(L);
459
+ int op = luaL_checkoption(L, 2, NULL, modenames);
460
+ lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
461
+ int res = setvbuf(f, NULL, mode[op], sz);
462
+ return pushresult(L, res == 0, NULL);
463
+ }
464
+
465
+
466
+
467
+ static int io_flush (lua_State *L) {
468
+ return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
469
+ }
470
+
471
+
472
+ static int f_flush (lua_State *L) {
473
+ return pushresult(L, fflush(tofile(L)) == 0, NULL);
474
+ }
475
+
476
+
477
+ static const luaL_Reg iolib[] = {
478
+ {"close", io_close},
479
+ {"flush", io_flush},
480
+ {"input", io_input},
481
+ {"lines", io_lines},
482
+ {"open", io_open},
483
+ {"output", io_output},
484
+ {"popen", io_popen},
485
+ {"read", io_read},
486
+ {"tmpfile", io_tmpfile},
487
+ {"type", io_type},
488
+ {"write", io_write},
489
+ {NULL, NULL}
490
+ };
491
+
492
+
493
+ static const luaL_Reg flib[] = {
494
+ {"close", io_close},
495
+ {"flush", f_flush},
496
+ {"lines", f_lines},
497
+ {"read", f_read},
498
+ {"seek", f_seek},
499
+ {"setvbuf", f_setvbuf},
500
+ {"write", f_write},
501
+ {"__gc", io_gc},
502
+ {"__tostring", io_tostring},
503
+ {NULL, NULL}
504
+ };
505
+
506
+
507
+ static void createmeta (lua_State *L) {
508
+ luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
509
+ lua_pushvalue(L, -1); /* push metatable */
510
+ lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
511
+ luaL_register(L, NULL, flib); /* file methods */
512
+ }
513
+
514
+
515
+ static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
516
+ *newfile(L) = f;
517
+ if (k > 0) {
518
+ lua_pushvalue(L, -1);
519
+ lua_rawseti(L, LUA_ENVIRONINDEX, k);
520
+ }
521
+ lua_pushvalue(L, -2); /* copy environment */
522
+ lua_setfenv(L, -2); /* set it */
523
+ lua_setfield(L, -3, fname);
524
+ }
525
+
526
+
527
+ static void newfenv (lua_State *L, lua_CFunction cls) {
528
+ lua_createtable(L, 0, 1);
529
+ lua_pushcfunction(L, cls);
530
+ lua_setfield(L, -2, "__close");
531
+ }
532
+
533
+
534
+ LUALIB_API int luaopen_io (lua_State *L) {
535
+ createmeta(L);
536
+ /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
537
+ newfenv(L, io_fclose);
538
+ lua_replace(L, LUA_ENVIRONINDEX);
539
+ /* open library */
540
+ luaL_register(L, LUA_IOLIBNAME, iolib);
541
+ /* create (and set) default files */
542
+ newfenv(L, io_noclose); /* close function for default files */
543
+ createstdfile(L, stdin, IO_INPUT, "stdin");
544
+ createstdfile(L, stdout, IO_OUTPUT, "stdout");
545
+ createstdfile(L, stderr, 0, "stderr");
546
+ lua_pop(L, 1); /* pop environment for default files */
547
+ lua_getfield(L, -1, "popen");
548
+ newfenv(L, io_pclose); /* create environment for 'popen' */
549
+ lua_setfenv(L, -2); /* set fenv for 'popen' */
550
+ lua_pop(L, 1); /* pop 'popen' */
551
+ return 1;
552
+ }
553
+