-
Notifications
You must be signed in to change notification settings - Fork 0
/
l_init.c
809 lines (808 loc) · 28.8 KB
/
l_init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// Generated file, see luatoc.lua
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
static char code[] = "\n\n\n\n\n"
"package.preload[\"extensions\"] = function (...)\n"
" -- os\n"
" \n"
" function os.dirname(path)\n"
" return string.match(path, \"(.*)[\\\\/]\") or '.'\n"
" end\n"
" \n"
" function os.basename(path)\n"
" return string.match(path, \"([^\\\\/]*)$\") or ''\n"
" end\n"
" \n"
" if os.platform == 'windows' then\n"
" local socket = require'socket'\n"
" function os.pipe()\n"
" local p1, p2, lsock\n"
" lsock = assert(socket.bind(\"localhost\", 0, 1))\n"
" local laddr, lport = lsock:getsockname()\n"
" p1 = assert(socket.connect(laddr, lport))\n"
" p2 = assert(lsock:accept())\n"
" local addr1, port1 = p1:getsockname()\n"
" local paddr2, pport2 = p2:getpeername()\n"
" assert(addr1 == paddr2 and tonumber(port1) == pport2, \"address mismatch\")\n"
" lsock:close()\n"
" p1:settimeout(0)\n"
" p2:settimeout(0)\n"
" return p1, p2\n"
" end\n"
" end\n"
" \n"
" -- table\n"
" function table.index (t, v)\n"
" for i,x in ipairs (t) do\n"
" if x == v then\n"
" return i\n"
" end\n"
" end\n"
" end\n"
" \n"
" -- string\n"
" do\n"
" local matches =\n"
" {\n"
" [\"^\"] = \"%^\";\n"
" [\"$\"] = \"%$\";\n"
" [\"(\"] = \"%(\";\n"
" [\")\"] = \"%)\";\n"
" [\"%\"] = \"%%\";\n"
" [\".\"] = \"%.\";\n"
" [\"[\"] = \"%[\";\n"
" [\"]\"] = \"%]\";\n"
" [\"*\"] = \"%*\";\n"
" [\"+\"] = \"%+\";\n"
" [\"-\"] = \"%-\";\n"
" [\"?\"] = \"%?\";\n"
" [\"\\0\"]= \"%z\";\n"
" }\n"
" function string.quote_patterns (s)\n"
" return (s:gsub(\".\", matches))\n"
" end\n"
" end\n"
" \n"
" function string.strip (str, chars)\n"
" if not str then return nil end\n"
" if chars then\n"
" chars = \"[\"..string.quote_patterns(chars)..\"]\"\n"
" else\n"
" chars = \"[ \\t\\r\\n]\"\n"
" end\n"
" return string.match(str, \"^\"..chars..\"*(.-)\"..chars..\"*$\")\n"
" end\n"
" \n"
" local ssub = string.sub\n"
" local sfind = string.find\n"
" \n"
" function string.startswith(s, prefix)\n"
" return ssub(s, 1, #prefix) == prefix\n"
" end\n"
" \n"
" function string.endswith(s, suffix)\n"
" return ssub(s, -#suffix) == suffix\n"
" end\n"
" \n"
" function string.split (str, pat, n)\n"
" -- FIXME: transform into a closure based iterator?\n"
" pat = pat or \"[ \\t\\r\\n]+\"\n"
" n = n or #str\n"
" local r = {}\n"
" local s, e = sfind (str, pat, 1)\n"
" if not s then return {str} end\n"
" if s ~= 1 then r[#r+1] = ssub(str, 1, s - 1) end\n"
" while true do\n"
" if e == #str then return r end\n"
" local ne\n"
" s, ne = sfind (str, pat, e + 1)\n"
" if not s or #r >= n then r[#r+1] = ssub(str, e + 1, #str) return r end\n"
" r[#r+1] = ssub(str, e + 1, s - 1)\n"
" e = ne\n"
" end\n"
" end\n"
" \n"
" function string.splitv (str, pat, n)\n"
" return unpack(string.split (str, pat, n))\n"
" end\n"
" \n"
" --[[ tests\n"
" do\n"
" local split = function (str, pat, n) return yd('split', string.split (str, pat, n)) end\n"
" split('foo/bar/baz/test','/')\n"
" --> {'foo','bar','baz','test'}\n"
" split('/foo/bar/baz/test','/')\n"
" --> {'foo','bar','baz','test'}\n"
" split('/foo/bar/baz/test/','/')\n"
" --> {'foo','bar','baz','test'}\n"
" split('/foo/bar//baz/test///','/')\n"
" --> {'foo','bar','','baz','test','',''}\n"
" split('//foo////bar/baz///test///','/+')\n"
" --> {'foo','bar','baz','test'}\n"
" split('foo','/+')\n"
" --> {'foo'}\n"
" split('','/+')\n"
" --> {}\n"
" split('foo','') -- splits a zero-sized string 3 (#str) times\n"
" --> {'','','',''}\n"
" split('a|b|c|d','|',2)\n"
" --> {'a','b','c|d'}\n"
" split('|a|b|c|d|','|',2)\n"
" --> {'a','b','c|d|')\n"
" end\n"
" --]]\n"
" \n"
" function string.splitall (str, pat, n)\n"
" -- FIXME: transform into a closure based iterator?\n"
" pat = pat or \"[ \\t\\r\\n]+\"\n"
" n = n or #str\n"
" local r = {}\n"
" local s = 0\n"
" local e = 0\n"
" while true do\n"
" local ne\n"
" s, ne = sfind (str, pat, e + 1)\n"
" if not s or #r >= n then r[#r+1] = ssub(str, e + 1, #str) return r end\n"
" r[#r+1] = ssub(str, e + 1, s - 1)\n"
" e = ne\n"
" end\n"
" end\n"
" \n"
" function string.splitallv (str, pat, n)\n"
" return unpack(string.splitall (str, pat, n))\n"
" end\n"
" \n"
" --[[ tests\n"
" do\n"
" local split = function (str, pat, n) local t = string.splitall (str, pat, n) yd('splitall', pat, str == table.concat (t, pat), t) return t end\n"
" split('foo/bar/baz/test','/')\n"
" --> {'foo','bar','baz','test'}\n"
" split('/foo/bar/baz/test','/')\n"
" --> {'','foo','bar','baz','test'}\n"
" split('/foo/bar/baz/test/','/')\n"
" --> {'','foo','bar','baz','test',''}\n"
" split('/foo/bar//baz/test///','/')\n"
" --> {'','foo','bar','','baz','test','','',''}\n"
" split('//foo////bar/baz///test///','/+')\n"
" --> {'','foo','bar','baz','test',''}\n"
" split('foo','/+')\n"
" --> {'foo'}\n"
" split('','/+')\n"
" --> {}\n"
" split('foo','') -- splits a zero-sized string 3 (#str) times\n"
" --> {'','','',''}\n"
" split('a|b|c|d','|',2)\n"
" --> {'a','b','c|d'}\n"
" end\n"
" --]]\n"
" \n"
" \n"
" \n"
" \n"
" --[[\n"
" START\n"
" The getopt function by Attractive Chaos <[email protected]>\n"
" --]]\n"
" \n"
" --[[\n"
" The MIT License\n"
" \n"
" Copyright (c) 2011, Attractive Chaos <[email protected]>\n"
" \n"
" Permission is hereby granted, free of charge, to any person obtaining a copy\n"
" of this software and associated documentation files (the \"Software\"), to deal\n"
" in the Software without restriction, including without limitation the rights\n"
" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n"
" copies of the Software, and to permit persons to whom the Software is\n"
" furnished to do so, subject to the following conditions:\n"
" \n"
" The above copyright notice and this permission notice shall be included in\n"
" all copies or substantial portions of the Software.\n"
" \n"
" THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n"
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n"
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n"
" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
" SOFTWARE.\n"
" ]]--\n"
" \n"
" -- Description: getopt() translated from the BSD getopt(); compatible with the default Unix getopt()\n"
" --[[ Example:\n"
" for o, a in os.getopt(arg, 'a:b') do\n"
" print(o, a)\n"
" end\n"
" ]]--\n"
" function os.getopt(args, ostr)\n"
" local arg, place = nil, 0;\n"
" return function ()\n"
" if place == 0 then -- update scanning pointer\n"
" place = 1\n"
" if #args == 0 or args[1]:sub(1, 1) ~= '-' then place = 0; return nil end\n"
" if #args[1] >= 2 then\n"
" place = place + 1\n"
" if args[1]:sub(2, 2) == '-' then -- found \"--\"\n"
" place = 0\n"
" table.remove(args, 1);\n"
" return nil;\n"
" end\n"
" end\n"
" end\n"
" local optopt = args[1]:sub(place, place);\n"
" place = place + 1;\n"
" local oli = ostr:find(optopt);\n"
" if optopt == ':' or oli == nil then -- unknown option\n"
" if optopt == '-' then return nil end\n"
" if place > #args[1] then\n"
" table.remove(args, 1);\n"
" place = 0;\n"
" end\n"
" return '?';\n"
" end\n"
" oli = oli + 1;\n"
" if ostr:sub(oli, oli) ~= ':' then -- do not need argument\n"
" arg = nil;\n"
" if place > #args[1] then\n"
" table.remove(args, 1);\n"
" place = 0;\n"
" end\n"
" else -- need an argument\n"
" if place <= #args[1] then -- no white space\n"
" arg = args[1]:sub(place);\n"
" else\n"
" table.remove(args, 1);\n"
" if #args == 0 then -- an option requiring argument is the last one\n"
" place = 0;\n"
" if ostr:sub(1, 1) == ':' then return ':' end\n"
" return '?';\n"
" else arg = args[1] end\n"
" end\n"
" table.remove(args, 1);\n"
" place = 0;\n"
" end\n"
" return optopt, arg;\n"
" end\n"
" end\n"
" \n"
" --[[\n"
" The getopt function by Attractive Chaos <[email protected]>\n"
" END\n"
" --]]\n"
" \n"
" do\n"
" -- Copyright (c) 2012 Rob Hoelz <[email protected]>, 2020 LoEE – Jakub Piotr Cłapa <[email protected]>\n"
" --\n"
" -- Permission is hereby granted, free of charge, to any person obtaining a copy of\n"
" -- this software and associated documentation files (the \"Software\"), to deal in\n"
" -- the Software without restriction, including without limitation the rights to\n"
" -- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n"
" -- the Software, and to permit persons to whom the Software is furnished to do so,\n"
" -- subject to the following conditions:\n"
" --\n"
" -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n"
" --\n"
" -- THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
" -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\n"
" -- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\n"
" -- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\n"
" -- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n"
" -- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
" \n"
" local sformat = string.format\n"
" local sgmatch = string.gmatch\n"
" local sgsub = string.gsub\n"
" local smatch = string.match\n"
" local tconcat = table.concat\n"
" local tinsert = table.insert\n"
" local setmetatable = setmetatable\n"
" local ploadlib = package.loadlib\n"
" \n"
" local meta = {}\n"
" local _M = setmetatable({}, meta)\n"
" \n"
" _M.VERSION = '0.01'\n"
" \n"
" -- XXX assert(type(package.preload[name]) == 'function')?\n"
" local function preload_loader(name)\n"
" if package.preload[name] then\n"
" return package.preload[name]\n"
" else\n"
" return sformat(\"no field package.preload['%s']\\n\", name)\n"
" end\n"
" end\n"
" \n"
" local function path_loader(name, paths, loader_func)\n"
" local errors = {}\n"
" local loader\n"
" \n"
" name = sgsub(name, '%.', '/')\n"
" \n"
" for path in sgmatch(paths, '[^;]+') do\n"
" path = sgsub(path, '%?', name)\n"
" \n"
" local errmsg\n"
" \n"
" loader, errmsg = loader_func(path)\n"
" \n"
" if loader then\n"
" break\n"
" else\n"
" -- XXX error for when file isn't readable?\n"
" -- XXX error for when file isn't valid Lua (or loadable?)\n"
" tinsert(errors, sformat(\"no file '%s'\", path))\n"
" end\n"
" end\n"
" \n"
" if loader then\n"
" return loader\n"
" else\n"
" return tconcat(errors, '\\n') .. '\\n'\n"
" end\n"
" end\n"
" \n"
" local function lua_loader(name)\n"
" return path_loader(name, package.path, function (path)\n"
" local fd = io.open(path, 'r')\n"
" if not fd then return nil end\n"
" local code = fd:read'*a'\n"
" fd:close()\n"
" local chunks = {string.format(\n"
" \"local __SRC_DIR = %q; local function rrequire(name) return require(%q..name) end;\",\n"
" os.dirname(path), name\n"
" ), code}\n"
" local i = 0\n"
" local fun, err = load(function () i = i + 1 return chunks[i] end, '@'..path)\n"
" if not fun then error(err, 7) end\n"
" return fun\n"
" end)\n"
" end\n"
" \n"
" local function get_init_function_name(name)\n"
" name = sgsub(name, '^.*%-', '', 1)\n"
" name = sgsub(name, '%.', '_')\n"
" \n"
" return 'luaopen_' .. name\n"
" end\n"
" \n"
" local function c_loader(name)\n"
" local init_func_name = get_init_function_name(name)\n"
" \n"
" return path_loader(name, package.cpath, function(path)\n"
" return ploadlib(path, init_func_name)\n"
" end)\n"
" end\n"
" \n"
" local function all_in_one_loader(name)\n"
" local init_func_name = get_init_function_name(name)\n"
" local base_name = smatch(name, '^[^.]+')\n"
" \n"
" return path_loader(base_name, package.cpath, function(path)\n"
" return ploadlib(path, init_func_name)\n"
" end)\n"
" end\n"
" \n"
" local function findchunk(name)\n"
" local errors = { string.format(\"module '%s' not found\\n\", name) }\n"
" local found\n"
" \n"
" for _, loader in ipairs(_M.loaders) do\n"
" local chunk = loader(name)\n"
" \n"
" if type(chunk) == 'function' then\n"
" return chunk\n"
" elseif type(chunk) == 'string' then\n"
" errors[#errors + 1] = chunk\n"
" end\n"
" end\n"
" \n"
" return nil, table.concat(errors, '')\n"
" end\n"
" \n"
" local function require(name)\n"
" if package.loaded[name] == nil then\n"
" local chunk, errors = findchunk(name)\n"
" \n"
" if not chunk then\n"
" error(errors, 2)\n"
" end\n"
" \n"
" local result = chunk(name)\n"
" \n"
" if result ~= nil then\n"
" package.loaded[name] = result\n"
" elseif package.loaded[name] == nil then\n"
" package.loaded[name] = true\n"
" end\n"
" end\n"
" \n"
" return package.loaded[name]\n"
" end\n"
" \n"
" local loadermeta = {}\n"
" \n"
" function loadermeta:__call(...)\n"
" return self.impl(...)\n"
" end\n"
" \n"
" local function makeloader(loader_func, name)\n"
" return setmetatable({ impl = loader_func, name = name }, loadermeta)\n"
" end\n"
" \n"
" -- XXX make sure that any added loaders are preserved (esp. luarocks)\n"
" _M.loaders = {\n"
" makeloader(preload_loader, 'preload'),\n"
" makeloader(lua_loader, 'lua'),\n"
" makeloader(c_loader, 'c'),\n"
" makeloader(all_in_one_loader, 'all_in_one'),\n"
" }\n"
" \n"
" if package.loaded['luarocks.require'] then\n"
" local luarocks_loader = require('luarocks.require').luarocks_loader\n"
" \n"
" table.insert(_M.loaders, 1, makeloader(luarocks_loader, 'luarocks')) \n"
" end\n"
" \n"
" -- XXX sugar for adding/removing loaders\n"
" \n"
" _G.require = require\n"
" \n"
" _M.findchunk = findchunk\n"
" end\n"
"end\n"
"package.preload[\"coxpcall\"] = function (...)\n"
" -------------------------------------------------------------------------------\n"
" -- Coroutine safe xpcall and pcall versions\n"
" --\n"
" -- Encapsulates the protected calls with a coroutine based loop, so errors can\n"
" -- be dealed without the usual Lua 5.x pcall/xpcall issues with coroutines\n"
" -- yielding inside the call to pcall or xpcall.\n"
" --\n"
" -- Authors: Roberto Ierusalimschy and Andre Carregal \n"
" -- Contributors: Thomas Harning Jr., Ignacio Burgueño, Fábio Mascarenhas\n"
" --\n"
" -- Copyright 2005 - Kepler Project (www.keplerproject.org)\n"
" --\n"
" -- $Id: coxpcall.lua,v 1.13 2008/05/19 19:20:02 mascarenhas Exp $\n"
" -------------------------------------------------------------------------------\n"
" \n"
" -------------------------------------------------------------------------------\n"
" -- Implements xpcall with coroutines\n"
" -------------------------------------------------------------------------------\n"
" local M = {}\n"
" local performResume, handleReturnValue\n"
" local oldpcall, oldxpcall = pcall, xpcall\n"
" \n"
" function handleReturnValue(err, co, status, ...)\n"
" if not status then\n"
" return false, err(debug.traceback(co, (...)), ...)\n"
" end\n"
" if coroutine.status(co) == 'suspended' then\n"
" return performResume(err, co, coroutine.yield(...))\n"
" else\n"
" return true, ...\n"
" end\n"
" end\n"
" \n"
" function performResume(err, co, ...)\n"
" return handleReturnValue(err, co, coroutine.resume(co, ...))\n"
" end \n"
" \n"
" function M.coxpcall(f, err, ...)\n"
" local res, co = oldpcall(coroutine.create, f)\n"
" if not res then\n"
" local params = {...}\n"
" local newf = function() return f(unpack(params)) end\n"
" co = coroutine.create(newf)\n"
" end\n"
" return performResume(err, co, ...)\n"
" end\n"
" \n"
" -------------------------------------------------------------------------------\n"
" -- Implements pcall with coroutines\n"
" -------------------------------------------------------------------------------\n"
" \n"
" local function id(trace, ...)\n"
" return ...\n"
" end\n"
" \n"
" function M.copcall(f, ...)\n"
" return M.coxpcall(f, id, ...)\n"
" end\n"
" \n"
" return M\n"
"end\n"
"package.preload[\"md5\"] = function (...)\n"
" ----------------------------------------------------------------------------\n"
" -- $Id: md5.lua,v 1.4 2006/08/21 19:24:21 carregal Exp $\n"
" ----------------------------------------------------------------------------\n"
" \n"
" local core\n"
" local string = string or require\"string\"\n"
" if string.find(_VERSION, \"Lua 5.0\") then\n"
" local cpath = os.getenv\"LUA_CPATH\" or \"/usr/local/lib/lua/5.0/\"\n"
" core = loadlib(cpath..\"md5/core.so\", \"luaopen_md5_core\")()\n"
" else\n"
" core = require\"md5.core\"\n"
" end\n"
" \n"
" \n"
" ----------------------------------------------------------------------------\n"
" -- @param k String with original message.\n"
" -- @return String with the md5 hash value converted to hexadecimal digits\n"
" \n"
" function core.sumhexa (k)\n"
" k = core.sum(k)\n"
" return (string.gsub(k, \".\", function (c)\n"
" return string.format(\"%02x\", string.byte(c))\n"
" end))\n"
" end\n"
" \n"
" return core\n"
"end\n"
"package.preload[\"socket\"] = function (...)\n"
" -----------------------------------------------------------------------------\n"
" -- LuaSocket helper module\n"
" -- Author: Diego Nehab\n"
" -----------------------------------------------------------------------------\n"
" \n"
" -----------------------------------------------------------------------------\n"
" -- Declare module and import dependencies\n"
" -----------------------------------------------------------------------------\n"
" local base = _G\n"
" local string = require(\"string\")\n"
" local math = require(\"math\")\n"
" local socket = require(\"socket.core\")\n"
" \n"
" local _M = socket\n"
" \n"
" -----------------------------------------------------------------------------\n"
" -- Exported auxiliar functions\n"
" -----------------------------------------------------------------------------\n"
" function _M.connect4(address, port, laddress, lport)\n"
" return socket.connect(address, port, laddress, lport, \"inet\")\n"
" end\n"
" \n"
" function _M.connect6(address, port, laddress, lport)\n"
" return socket.connect(address, port, laddress, lport, \"inet6\")\n"
" end\n"
" \n"
" function _M.bind(host, port, backlog)\n"
" if host == \"*\" then host = \"0.0.0.0\" end\n"
" local addrinfo, err = socket.dns.getaddrinfo(host);\n"
" if not addrinfo then return nil, err end\n"
" local sock, res\n"
" err = \"no info on address\"\n"
" for i, alt in base.ipairs(addrinfo) do\n"
" if alt.family == \"inet\" then\n"
" sock, err = socket.tcp4()\n"
" else\n"
" sock, err = socket.tcp6()\n"
" end\n"
" if not sock then return nil, err end\n"
" sock:setoption(\"reuseaddr\", true)\n"
" res, err = sock:bind(alt.addr, port)\n"
" if not res then\n"
" sock:close()\n"
" else\n"
" res, err = sock:listen(backlog)\n"
" if not res then\n"
" sock:close()\n"
" else\n"
" return sock\n"
" end\n"
" end\n"
" end\n"
" return nil, err\n"
" end\n"
" \n"
" _M.try = _M.newtry()\n"
" \n"
" function _M.choose(table)\n"
" return function(name, opt1, opt2)\n"
" if base.type(name) ~= \"string\" then\n"
" name, opt1, opt2 = \"default\", name, opt1\n"
" end\n"
" local f = table[name or \"nil\"]\n"
" if not f then base.error(\"unknown key (\".. base.tostring(name) ..\")\", 3)\n"
" else return f(opt1, opt2) end\n"
" end\n"
" end\n"
" \n"
" -----------------------------------------------------------------------------\n"
" -- Socket sources and sinks, conforming to LTN12\n"
" -----------------------------------------------------------------------------\n"
" -- create namespaces inside LuaSocket namespace\n"
" local sourcet, sinkt = {}, {}\n"
" _M.sourcet = sourcet\n"
" _M.sinkt = sinkt\n"
" \n"
" _M.BLOCKSIZE = 2048\n"
" \n"
" sinkt[\"close-when-done\"] = function(sock)\n"
" return base.setmetatable({\n"
" getfd = function() return sock:getfd() end,\n"
" dirty = function() return sock:dirty() end\n"
" }, {\n"
" __call = function(self, chunk, err)\n"
" if not chunk then\n"
" sock:close()\n"
" return 1\n"
" else return sock:send(chunk) end\n"
" end\n"
" })\n"
" end\n"
" \n"
" sinkt[\"keep-open\"] = function(sock)\n"
" return base.setmetatable({\n"
" getfd = function() return sock:getfd() end,\n"
" dirty = function() return sock:dirty() end\n"
" }, {\n"
" __call = function(self, chunk, err)\n"
" if chunk then return sock:send(chunk)\n"
" else return 1 end\n"
" end\n"
" })\n"
" end\n"
" \n"
" sinkt[\"default\"] = sinkt[\"keep-open\"]\n"
" \n"
" _M.sink = _M.choose(sinkt)\n"
" \n"
" sourcet[\"by-length\"] = function(sock, length)\n"
" return base.setmetatable({\n"
" getfd = function() return sock:getfd() end,\n"
" dirty = function() return sock:dirty() end\n"
" }, {\n"
" __call = function()\n"
" if length <= 0 then return nil end\n"
" local size = math.min(socket.BLOCKSIZE, length)\n"
" local chunk, err = sock:receive(size)\n"
" if err then return nil, err end\n"
" length = length - string.len(chunk)\n"
" return chunk\n"
" end\n"
" })\n"
" end\n"
" \n"
" sourcet[\"until-closed\"] = function(sock)\n"
" local done\n"
" return base.setmetatable({\n"
" getfd = function() return sock:getfd() end,\n"
" dirty = function() return sock:dirty() end\n"
" }, {\n"
" __call = function()\n"
" if done then return nil end\n"
" local chunk, err, partial = sock:receive(socket.BLOCKSIZE)\n"
" if not err then return chunk\n"
" elseif err == \"closed\" then\n"
" sock:close()\n"
" done = 1\n"
" return partial\n"
" else return nil, err end\n"
" end\n"
" })\n"
" end\n"
" \n"
" \n"
" sourcet[\"default\"] = sourcet[\"until-closed\"]\n"
" \n"
" _M.source = _M.choose(sourcet)\n"
" \n"
" return _M\n"
"end\n"
"do\n"
" os.executable_path, os.platform, os.arch = ...\n"
" \n"
" _G.thb = {}\n"
" \n"
" package.path = ''\n"
" package.cpath = ''\n"
" \n"
" setmetatable(_G, {\n"
" __newindex = function (_, n)\n"
" error(\"error: write to a global variable: \"..n, 2)\n"
" end,\n"
" __index = function (_, n)\n"
" if n == 'D' or n == 'log' then rawset(_G, 'D', require'util') return _G[n] end\n"
" error(\"error: read from a non-existing global variable: \"..n, 2)\n"
" end,\n"
" })\n"
" \n"
" require'extensions'\n"
" \n"
" local function addtoPATH(p)\n"
" package.path = p..'/?.luac;'..p..'/?/init.luac;'..p..'/?.lua;'..p..'/?/init.lua;'..package.path\n"
" package.cpath = p..'/?.so;'..package.cpath\n"
" end\n"
" \n"
" os.executable_dir = os.dirname(os.executable_path)\n"
" addtoPATH(os.executable_dir..'/lualib')\n"
" addtoPATH(os.executable_dir..'/lualib/'..os.platform)\n"
" \n"
" local main\n"
" \n"
" local function drop_arguments(n)\n"
" for i=0,#arg do\n"
" arg[i] = arg[i+n]\n"
" end\n"
" end\n"
" \n"
" local function dofile_error(code, msg)\n"
" io.stderr:write(msg..'\\n')\n"
" io.stderr:flush()\n"
" os.exit(code)\n"
" end\n"
" \n"
" local function dofile(fname)\n"
" local fd, openerr = io.open(fname, 'r')\n"
" if not fd then return dofile_error(3, 'Error loading file: '..fname..': '..openerr) end\n"
" local src = assert(fd:read'*a')\n"
" fd:close()\n"
" local chunks = {string.format(\n"
" \"local __SRC_DIR = %q; local __MAIN__ = true; local rrequire = require;\",\n"
" os.dirname(fname)\n"
" )}\n"
" if src:startswith'#!' then chunks[#chunks+1] = '--' end\n"
" chunks[#chunks+1] = src\n"
" local i = 0\n"
" local code, syntax_err = load(function () i = i + 1 return chunks[i] end, '@'..fname)\n"
" if not code then return dofile_error(3, 'Syntax error:\\n\\t'..syntax_err) end\n"
" local ok, err = xpcall(code, debug.traceback) local dofile_tb = debug.traceback():sub(18)\n"
" local tb_prefix = \"\\9[C]: in function 'xpcall'\\n\" .. dofile_tb\n"
" if not ok then\n"
" -- remove pcall, dofile and our callers from the traceback\n"
" if err:sub(#err - #tb_prefix + 1, -1) == tb_prefix then err = err:sub(1, #err - #tb_prefix) end\n"
" dofile_error(4, err)\n"
" end\n"
" end\n"
" thb.dofile = dofile\n"
" \n"
" if not os.basename(arg[0]):startswith\"thb\" then\n"
" -- FIXME: realpath does not work for executables in PATH\n"
" os.program_path = os.dirname(os.realpath(arg[0]))\n"
" addtoPATH(os.program_path)\n"
" function main()\n"
" local name = arg[0]\n"
" if name:endswith\".exe\" then\n"
" name = name:sub(1, -5)\n"
" end\n"
" dofile(name..'.lua')\n"
" end\n"
" elseif arg[1] then\n"
" if string.sub(arg[1], 1, 1) == ':' then\n"
" arg[1] = os.executable_dir..'/'..string.sub(arg[1], 2)..'.lua'\n"
" else\n"
" local rpath = os.realpath(arg[1])\n"
" if not rpath then io.stderr:write('error: file not found: '..arg[1]..'\\n') os.exit(2) end\n"
" os.program_path = os.dirname(os.realpath(arg[1]))\n"
" addtoPATH(os.program_path)\n"
" end\n"
" function main()\n"
" drop_arguments(1)\n"
" dofile(arg[0])\n"
" local loop = require'loop'\n"
" loop.run()\n"
" end\n"
" else\n"
" function main()\n"
" addtoPATH('.')\n"
" local loop = require'loop'\n"
" local repl = require'repl'\n"
" repl.start(0)\n"
" loop.run()\n"
" end\n"
" end\n"
" \n"
" main()\n"
"end\n"
;
int l_init (lua_State *L)
{
int n = lua_gettop(L);
if (luaL_loadbuffer(L, code, sizeof(code) - 1, "@l_init.c")) return lua_error(L);
lua_insert(L, 1);
lua_call(L, n, 0);
return 0;
}