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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
|
//////////////////////////////////////////////////////////////////////////
// BBS-Lua Project
//
// Author: Hung-Te Lin(piaip), Jan. 2008.
// <piaip@csie.ntu.edu.tw>
// Create: 2008-01-04 22:02:58
// $Id$
//
// This source is released in MIT License, same as Lua 5.0
// http://www.lua.org/license.html
//
// Copyright 2008 Hung-Te Lin <piaip@csie.ntu.edu.tw>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// TODO:
// BBSLUA 1.0
// 1. add quick key/val conversion [deprecated]
// 2. add key values (UP/DOWN/...) [done]
// 3. remove i/o libraries [done]
// 4. add system break key (Ctrl-C) [done]
// 5. add version string and script tags [done]
// 6. standalone w32 sdk [done]
// 7. syntax highlight in editor [done]
// 8. prevent loadfile, dofile [done]
// 9. provide local storage
// ?. modify bbs user data (eg, money)
// ?. os.date(), os.exit(), abort(), os.time()
// ?. memory free issue in C library level?
// ?. add digital signature
//
// BBSLUA 2.0
// 1. 2 people communication
//
// BBSLUA 3.0
// 1. n people communication
//////////////////////////////////////////////////////////////////////////
#include "bbs.h"
#include "fnv_hash.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
//////////////////////////////////////////////////////////////////////////
// CONST DEFINITION
//////////////////////////////////////////////////////////////////////////
#define BBSLUA_INTERFACE_VER 0.119 // (0.201)
#define BBSLUA_SIGNATURE "--#BBSLUA"
// BBS-Lua script format:
// $BBSLUA_SIGNATURE
// -- Interface: $interface
// -- Title: $title
// -- Notes: $notes
// -- Author: $author <email@domain>
// -- Version: $version
// -- Date: $date
// -- LatestRef: #AID board
// [... script ...]
// $BBSLUA_SIGNATURE
//////////////////////////////////////////////////////////////////////////
// CONFIGURATION VARIABLES
//////////////////////////////////////////////////////////////////////////
#define BLAPI_PROTO int
#define BLCONF_BREAK_KEY Ctrl('C')
#define BLCONF_EXEC_COUNT (5000)
#define BLCONF_PEEK_TIME (0.01)
#define BLCONF_KBHIT_TMIN (BLCONF_PEEK_TIME)
#define BLCONF_KBHIT_TMAX (60*10)
#define BLCONF_SLEEP_TMIN (BLCONF_PEEK_TIME)
#define BLCONF_SLEEP_TMAX (BLCONF_KBHIT_TMAX)
#define BLCONF_U_SECOND (1000000L)
#define BLCONF_PRINT_TOC_INDEX (2)
#define BLCONF_MMAP_ATTACH
#define BLCONF_CURRENT_USERID cuser.userid
#define BLCONF_CURRENT_USERNICK cuser.nickname
// BBS-Lua Storage
enum {
BLS_INVALID= 0,
BLS_GLOBAL = 1,
BLS_USER,
};
// #define BLSCONF_ENABLED
#define BLSCONF_GLOBAL_VAL "global"
#define BLSCONF_USER_VAL "user"
#define BLSCONF_GMAXSIZE (16*1024) // should be aligned to block size
#define BLSCONF_UMAXSIZE (16*1024) // should be aligned to block size
#define BLSCONF_GPATH BBSHOME "/luastore"
#define BLSCONF_UPATH ".luastore"
#define BLSCONF_PREFIX "v1_"
#define BLSCONF_MAXIO 32 // prevent bursting system
// #define BBSLUA_USAGE
#ifdef _WIN32
# undef BLCONF_MMAP_ATTACH
# undef BLCONF_CURRENT_USERID
# define BLCONF_CURRENT_USERID "guest"
# undef BLCONF_CURRENT_USERNICK
# define BLCONF_CURRENT_USERNICK "測試帳號"
#endif
//////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
//////////////////////////////////////////////////////////////////////////
typedef struct {
char running; // prevent re-entrant
char abort; // system break key hit
char iocounter; // prevent bursting i/o
Fnv32_t storename; // storage filename
} BBSLuaRT;
// runtime information
// static
BBSLuaRT blrt = {0};
#define BL_INIT_RUNTIME() { \
memset(&blrt, 0, sizeof(blrt)); \
blrt.storename = FNV1_32_INIT; \
}
#define BL_END_RUNTIME() { \
memset(&blrt, 0, sizeof(blrt)); \
}
#ifdef BBSLUA_USAGE
static int bbslua_count;
#endif
//////////////////////////////////////////////////////////////////////////
// UTILITIES
//////////////////////////////////////////////////////////////////////////
static void
bl_double2tv(double d, struct timeval *tv)
{
tv->tv_sec = d;
tv->tv_usec = (d - tv->tv_sec) * BLCONF_U_SECOND;
}
static double
bl_tv2double(const struct timeval *tv)
{
double d = tv->tv_sec;
d += tv->tv_usec / (double)BLCONF_U_SECOND;
return d;
}
static int
bl_peekbreak(float f)
{
if (input_isfull())
drop_input();
if (peek_input(f, BLCONF_BREAK_KEY))
{
drop_input();
blrt.abort = 1;
return 1;
}
return 0;
}
static void
bl_k2s(lua_State* L, int v)
{
if (v <= 0)
lua_pushnil(L);
else if (v == KEY_TAB)
lua_pushstring(L, "TAB");
else if (v == '\b' || v == 0x7F)
lua_pushstring(L, "BS");
else if (v == '\n' || v == '\r' || v == Ctrl('M'))
lua_pushstring(L, "ENTER");
else if (v < ' ')
lua_pushfstring(L, "^%c", v-1+'A');
else if (v < 0x100)
lua_pushfstring(L, "%c", v);
else if (v >= KEY_F1 && v <= KEY_F12)
lua_pushfstring(L, "F%d", v - KEY_F1 +1);
else switch(v)
{
case KEY_UP: lua_pushstring(L, "UP"); break;
case KEY_DOWN: lua_pushstring(L, "DOWN"); break;
case KEY_RIGHT: lua_pushstring(L, "RIGHT"); break;
case KEY_LEFT: lua_pushstring(L, "LEFT"); break;
case KEY_HOME: lua_pushstring(L, "HOME"); break;
case KEY_END: lua_pushstring(L, "END"); break;
case KEY_INS: lua_pushstring(L, "INS"); break;
case KEY_DEL: lua_pushstring(L, "DEL"); break;
case KEY_PGUP: lua_pushstring(L, "PGUP"); break;
case KEY_PGDN: lua_pushstring(L, "PGDN"); break;
default: lua_pushnil(L); break;
}
}
BLAPI_PROTO
bl_newwin(int rows, int cols, const char *title)
{
// input: (rows, cols, title)
int y = 0, x = 0, n = 0;
int oy = 0, ox = 0;
if (rows <= 0 || cols <= 0)
return 0;
getyx(&oy, &ox);
// now, draw the window
newwin(rows, cols, oy, ox);
if (!title || !*title)
return 0;
// draw center-ed title
n = strlen_noansi(title);
x = ox + (cols - n)/2;
y = oy + (rows)/2;
move(y, x);
outs(title);
move(oy, ox);
return 0;
}
//////////////////////////////////////////////////////////////////////////
// BBSLUA API IMPLEMENTATION
//////////////////////////////////////////////////////////////////////////
BLAPI_PROTO
bl_getyx(lua_State* L)
{
int y, x;
getyx(&y, &x);
lua_pushinteger(L, y);
lua_pushinteger(L, x);
return 2;
}
BLAPI_PROTO
bl_getmaxyx(lua_State* L)
{
lua_pushinteger(L, t_lines);
lua_pushinteger(L, t_columns);
return 2;
}
BLAPI_PROTO
bl_move(lua_State* L)
{
int n = lua_gettop(L);
int y = 0, x = 0;
if (n > 0)
y = lua_tointeger(L, 1);
if (n > 1)
x = lua_tointeger(L, 2);
move_ansi(y, x);
return 0;
}
BLAPI_PROTO
bl_moverel(lua_State* L)
{
int n = lua_gettop(L);
int y = 0, x = 0;
getyx(&y, &x);
if (n > 0)
y += lua_tointeger(L, 1);
if (n > 1)
x += lua_tointeger(L, 2);
move(y, x);
getyx(&y, &x);
lua_pushinteger(L, y);
lua_pushinteger(L, x);
return 2;
}
BLAPI_PROTO
bl_clear(lua_State* L)
{
(void)L; /* to avoid warnings */
clear();
return 0;
}
BLAPI_PROTO
bl_clrtoeol(lua_State* L)
{
(void)L; /* to avoid warnings */
clrtoeol();
return 0;
}
BLAPI_PROTO
bl_clrtobot(lua_State* L)
{
(void)L; /* to avoid warnings */
clrtobot();
return 0;
}
BLAPI_PROTO
bl_refresh(lua_State* L)
{
(void)L; /* to avoid warnings */
// refresh();
// Seems like that most people don't understand the relationship
// between refresh() and input queue, so let's force update here.
doupdate();
return 0;
}
BLAPI_PROTO
bl_addstr(lua_State* L)
{
int n = lua_gettop(L);
int i = 1;
for (i = 1; i <= n; i++)
{
const char *s = lua_tostring(L, i);
if(s)
outs(s);
}
return 0;
}
BLAPI_PROTO
bl_rect(lua_State *L)
{
// input: (rows, cols, title)
int rows = 1, cols = 1;
int n = lua_gettop(L);
const char *title = NULL;
if (n > 0)
rows = lua_tointeger(L, 1);
if (n > 1)
cols = lua_tointeger(L, 2);
if (n > 2)
title = lua_tostring(L, 3);
if (rows <= 0 || cols <= 0)
return 0;
// now, draw the rectangle
bl_newwin(rows, cols, title);
return 0;
}
BLAPI_PROTO
bl_print(lua_State* L)
{
bl_addstr(L);
outc('\n');
return 0;
}
BLAPI_PROTO
bl_getch(lua_State* L)
{
int c = igetch();
if (c == BLCONF_BREAK_KEY)
{
drop_input();
blrt.abort = 1;
return lua_yield(L, 0);
}
bl_k2s(L, c);
return 1;
}
BLAPI_PROTO
bl_getstr(lua_State* L)
{
int y, x;
// TODO not using fixed length here?
char buf[PATHLEN] = "";
int len = 2, echo = 1;
int n = lua_gettop(L);
const char *pmsg = NULL;
if (n > 0)
len = lua_tointeger(L, 1);
if (n > 1)
echo = lua_tointeger(L, 2);
if (n > 2)
pmsg = lua_tostring(L, 3);
if (len < 2)
len = 2;
if (len >= sizeof(buf))
len = sizeof(buf)-1;
/*
* this part now done in getdata_str
if (pmsg && *pmsg)
{
strlcpy(buf, pmsg, sizeof(buf));
}
*/
// TODO process Ctrl-C here
getyx(&y, &x);
if (!pmsg) pmsg = "";
len = getdata_str(y, x, NULL, buf, len, echo, pmsg);
if (len <= 0)
{
len = 0;
// check if we got Ctrl-C? (workaround in getdata)
// TODO someday write 'ungetch()' in io.c to prevent
// such workaround.
if (buf[1] == Ctrl('C'))
{
drop_input();
blrt.abort = 1;
return lua_yield(L, 0);
}
lua_pushstring(L, "");
}
else
{
lua_pushstring(L, buf);
}
// return len ? 1 : 0;
return 1;
}
BLAPI_PROTO
bl_kbhit(lua_State *L)
{
int n = lua_gettop(L);
double f = 0.1f;
if (n > 0)
f = (double)lua_tonumber(L, 1);
if (f < BLCONF_KBHIT_TMIN) f = BLCONF_KBHIT_TMIN;
if (f > BLCONF_KBHIT_TMAX) f = BLCONF_KBHIT_TMAX;
refresh();
if (num_in_buf() || wait_input(f, 0))
lua_pushboolean(L, 1);
else
lua_pushboolean(L, 0);
return 1;
}
BLAPI_PROTO
bl_kbreset(lua_State *L)
{
// peek input queue first!
if (bl_peekbreak(BLCONF_PEEK_TIME))
return lua_yield(L, 0);
drop_input();
return 0;
}
BLAPI_PROTO
bl_sleep(lua_State *L)
{
int n = lua_gettop(L);
double us = 0, nus = 0;
// update screen first.
bl_refresh(L);
if (n > 0)
us = lua_tonumber(L, 1);
if (us < BLCONF_SLEEP_TMIN) us = BLCONF_SLEEP_TMIN;
if (us > BLCONF_SLEEP_TMAX) us = BLCONF_SLEEP_TMAX;
nus = us;
#ifdef _WIN32
Sleep(us * 1000);
#else // !_WIN32
{
struct timeval tp, tdest;
gettimeofday(&tp, NULL);
// nus is the destination time
nus = bl_tv2double(&tp) + us;
bl_double2tv(nus, &tdest);
while ( (tp.tv_sec < tdest.tv_sec) ||
((tp.tv_sec == tdest.tv_sec) && (tp.tv_usec < tdest.tv_usec)))
{
// calculate new peek time
us = nus - bl_tv2double(&tp);
// check if input key is system break key.
if (bl_peekbreak(us))
return lua_yield(L, 0);
// check time
gettimeofday(&tp, NULL);
}
}
#endif // !_WIN32
return 0;
}
BLAPI_PROTO
bl_kball(lua_State *L)
{
// first, sleep by given seconds
int r = 0, oldr = 0, i = 0;
r = bl_sleep(L);
if (blrt.abort)
return r;
// pop all arguments
lua_pop(L, lua_gettop(L));
#ifdef _WIN32
while (peekch(0))
{
bl_k2s(L, igetch());
i++;
}
#else
// next, collect all input and return.
if (num_in_buf() < 1)
return 0;
oldr = num_in_buf() +1;
i = 0;
while ( i < LUA_MINSTACK &&
(r = num_in_buf()) > 0 && oldr > r)
{
oldr = r;
bl_k2s(L, igetch());
i++;
}
#endif
return i;
}
BLAPI_PROTO
bl_pause(lua_State* L)
{
int n = lua_gettop(L);
const char *s = NULL;
if (n > 0)
s = lua_tostring(L, 1);
n = vmsg(s);
if (n == BLCONF_BREAK_KEY)
{
drop_input();
blrt.abort = 1;
return lua_yield(L, 0);
}
bl_k2s(L, n);
return 1;
}
BLAPI_PROTO
bl_title(lua_State* L)
{
int n = lua_gettop(L);
const char *s = NULL;
if (n > 0)
s = lua_tostring(L, 1);
if (s == NULL)
return 0;
stand_title(s);
return 0;
}
BLAPI_PROTO
bl_ansi_color(lua_State *L)
{
char buf[PATHLEN] = ESC_STR "[";
char *p = buf + strlen(buf);
int i = 1;
int n = lua_gettop(L);
if (n >= 10) n = 10;
for (i = 1; i <= n; i++)
{
if (i > 1) *p++ = ';';
sprintf(p, "%d", (int)lua_tointeger(L, i));
p += strlen(p);
}
*p++ = 'm';
*p = 0;
lua_pushstring(L, buf);
return 1;
}
BLAPI_PROTO
bl_strip_ansi(lua_State *L)
{
int n = lua_gettop(L);
const char *s = NULL;
char *s2 = NULL;
size_t os2 = 0;
if (n < 1 || (s = lua_tostring(L, 1)) == NULL ||
*s == 0)
{
lua_pushstring(L, "");
return 1;
}
os2 = strlen(s)+1;
s2 = (char*) lua_newuserdata(L, os2);
strip_ansi(s2, s, STRIP_ALL);
lua_pushstring(L, s2);
lua_remove(L, -2);
return 1;
}
BLAPI_PROTO
bl_attrset(lua_State *L)
{
char buf[PATHLEN] = ESC_STR "[";
char *p = buf + strlen(buf);
int i = 1;
int n = lua_gettop(L);
if (n == 0)
outs(ANSI_RESET);
for (i = 1; i <= n; i++)
{
sprintf(p, "%dm",(int)lua_tointeger(L, i));
outs(buf);
}
return 0;
}
BLAPI_PROTO
bl_time(lua_State *L)
{
syncnow();
lua_pushinteger(L, now);
return 1;
}
BLAPI_PROTO
bl_ctime(lua_State *L)
{
syncnow();
lua_pushstring(L, ctime4(&now));
return 1;
}
BLAPI_PROTO
bl_clock(lua_State *L)
{
double d = 0;
#ifdef _WIN32
// XXX this is a fast hack because we don't want to do 64bit calculation.
SYSTEMTIME st;
GetSystemTime(&st);
syncnow();
// XXX the may be some latency between our GetSystemTime and syncnow.
// So build again the "second" part.
d = (int)((now / 60) * 60);
d += st.wSecond;
d += (st.wMilliseconds / 1000.0f);
#else // !_WIN32
struct timeval tp;
gettimeofday(&tp, NULL);
d = bl_tv2double(&tp);
#endif // !_WIN32
lua_pushnumber(L, d);
return 1;
}
//////////////////////////////////////////////////////////////////////////
// BBS-Lua Storage System
//////////////////////////////////////////////////////////////////////////
static int
bls_getcat(const char *s)
{
if (!s || !*s)
return BLS_INVALID;
if (strcmp(s, BLSCONF_GLOBAL_VAL) == 0)
return BLS_GLOBAL;
else if (strcmp(s, BLSCONF_USER_VAL) == 0)
return BLS_USER;
return BLS_INVALID;
}
static int
bls_getlimit(const char *p)
{
switch(bls_getcat(p))
{
case BLS_GLOBAL:
return BLSCONF_GMAXSIZE;
case BLS_USER:
return BLSCONF_UMAXSIZE;
}
return 0;
}
static int
bls_setfn(char *fn, size_t sz, const char *p)
{
*fn = 0;
switch(bls_getcat(p))
{
case BLS_GLOBAL:
snprintf(fn, sz, "%s/" BLSCONF_PREFIX "U%08x",
BLSCONF_GPATH, blrt.storename);
return 1;
case BLS_USER:
setuserfile(fn, BLSCONF_UPATH);
mkdir(fn, 0755);
assert(strlen(fn) +8 <= sz);
snprintf(fn + strlen(fn),
sz - strlen(fn),
"/" BLSCONF_PREFIX "G%08x", blrt.storename);
return 1;
}
return 0;
}
BLAPI_PROTO
bls_iolimit(lua_State *L)
{
lua_pushinteger(L, BLSCONF_MAXIO);
return 1;
}
BLAPI_PROTO
bls_limit(lua_State *L)
{
int n = lua_gettop(L);
const char *s = NULL;
if (n > 0)
s = lua_tostring(L, 1);
lua_pushinteger(L, bls_getlimit(s));
return 1;
}
BLAPI_PROTO
bls_load(lua_State *L)
{
int n = lua_gettop(L);
const char *cat = NULL;
char fn[PATHLEN];
if (blrt.iocounter >= BLSCONF_MAXIO)
{
lua_pushnil(L);
return 1;
}
if (n != 1)
{
lua_pushnil(L);
return 1;
}
cat = lua_tostring(L, 1); // category
if (!cat)
{
lua_pushnil(L);
return 1;
}
blrt.iocounter++;
// read file!
if (bls_setfn(fn, sizeof(fn), cat))
{
int fd = open(fn, O_RDONLY);
if (fd >= 0)
{
char buf[2048];
luaL_Buffer b;
luaL_buffinit(L, &b);
while ((n = read(fd, buf, sizeof(buf))) > 0)
luaL_addlstring(&b, buf, n);
close(fd);
luaL_pushresult(&b);
} else {
lua_pushnil(L);
}
} else {
lua_pushnil(L);
}
return 1;
}
BLAPI_PROTO
bls_save(lua_State *L)
{
int n = lua_gettop(L), fd = -1;
int limit = 0, slen = 0;
const char *s = NULL, *cat = NULL;
char fn[PATHLEN] = "", ret = 0;
if (blrt.iocounter >= BLSCONF_MAXIO)
{
lua_pushboolean(L, 0);
return 1;
}
if (n != 2) { lua_pushboolean(L, 0); return 1; }
cat = lua_tostring(L, 1); // category
s = lua_tostring(L, 2); // data
limit = bls_getlimit(cat);
if (!cat || !s || limit < 1)
{
lua_pushboolean(L, 0);
return 1;
}
slen = lua_objlen(L, 2);
if (slen >= limit) slen = limit;
blrt.iocounter++;
// write file!
if (bls_setfn(fn, sizeof(fn), cat))
{
fd = open(fn, O_WRONLY|O_CREAT, 0644);
if (fd >= 0)
{
write(fd, s, slen);
close(fd);
ret = 1;
}
}
lua_pushboolean(L, ret);
return 1;
}
//////////////////////////////////////////////////////////////////////////
// BBSLUA LIBRARY
//////////////////////////////////////////////////////////////////////////
static const struct luaL_reg lib_bbslua [] = {
/* curses output */
{ "getyx", bl_getyx },
{ "getmaxyx", bl_getmaxyx },
{ "move", bl_move },
{ "moverel", bl_moverel },
{ "clear", bl_clear },
{ "clrtoeol", bl_clrtoeol },
{ "clrtobot", bl_clrtobot },
{ "refresh", bl_refresh },
{ "addstr", bl_addstr },
{ "outs", bl_addstr },
{ "print", bl_print },
/* input */
{ "getch", bl_getch },
{ "getdata", bl_getstr },
{ "getstr", bl_getstr },
{ "kbhit", bl_kbhit },
{ "kbreset", bl_kbreset },
{ "kball", bl_kball },
/* advanced output */
{ "rect", bl_rect },
/* BBS utilities */
{ "pause", bl_pause },
{ "title", bl_title },
/* time */
{ "time", bl_time },
{ "now", bl_time },
{ "clock", bl_clock },
{ "ctime", bl_ctime },
{ "sleep", bl_sleep },
/* ANSI helpers */
{ "ANSI_COLOR", bl_ansi_color },
{ "color", bl_attrset },
{ "attrset", bl_attrset },
{ "strip_ansi", bl_strip_ansi },
{ NULL, NULL},
};
static const struct luaL_reg lib_store [] = {
{ "load", bls_load },
{ "save", bls_save },
{ "limit", bls_limit },
{ "iolimit", bls_iolimit },
{ NULL, NULL},
};
// non-standard modules in bbsluaext.c
LUALIB_API int luaopen_bit (lua_State *L);
static const luaL_Reg bbslualibs[] = {
// standard modules
{"", luaopen_base},
// {LUA_LOADLIBNAME, luaopen_package},
{LUA_TABLIBNAME, luaopen_table},
// {LUA_IOLIBNAME, luaopen_io},
// {LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
// {LUA_DBLIBNAME, luaopen_debug},
// bbslua-ext modules
{"bit", luaopen_bit},
{NULL, NULL}
};
LUALIB_API void bbsluaL_openlibs (lua_State *L) {
const luaL_Reg *lib = bbslualibs;
for (; lib->func; lib++) {
lua_pushcfunction(L, lib->func);
lua_pushstring(L, lib->name);
lua_call(L, 1, 0);
}
}
// Constant registration
typedef struct bbsluaL_RegStr {
const char *name;
const char *val;
} bbsluaL_RegStr;
typedef struct bbsluaL_RegNum {
const char *name;
lua_Number val;
} bbsluaL_RegNum;
static const bbsluaL_RegStr bbsluaStrs[] = {
{"ESC", ESC_STR},
{"ANSI_RESET", ANSI_RESET},
{"sitename", BBSNAME},
{NULL, NULL},
};
static const bbsluaL_RegNum bbsluaNums[] = {
{"interface", BBSLUA_INTERFACE_VER},
{NULL, 0},
};
static void
bbsluaRegConst(lua_State *L)
{
int i = 0;
// unbind unsafe API
lua_pushnil(L); lua_setglobal(L, "dofile");
lua_pushnil(L); lua_setglobal(L, "loadfile");
// global
lua_pushcfunction(L, bl_print);
lua_setglobal(L, "print");
// bbs.*
lua_getglobal(L, "bbs");
for (i = 0; bbsluaStrs[i].name; i++)
{
lua_pushstring(L, bbsluaStrs[i].val);
lua_setfield(L, -2, bbsluaStrs[i].name);
}
for (i = 0; bbsluaNums[i].name; i++)
{
lua_pushnumber(L, bbsluaNums[i].val);
lua_setfield(L, -2, bbsluaNums[i].name);
}
// dynamic info
lua_pushstring(L, BLCONF_CURRENT_USERID);
lua_setfield(L, -2, "userid");
lua_pushstring(L, BLCONF_CURRENT_USERNICK);
lua_setfield(L, -2, "usernick");
lua_pop(L, 1);
#ifdef BLSCONF_ENABLED
// store.*
lua_getglobal(L, "store");
lua_pushstring(L, BLSCONF_USER_VAL);
lua_setfield(L, -2, "USER");
lua_pushstring(L, BLSCONF_GLOBAL_VAL);
lua_setfield(L, -2, "GLOBAL");
lua_pop(L, 1);
#endif // BLSCONF_ENABLED
}
//////////////////////////////////////////////////////////////////////////
// BBSLUA ENGINE UTILITIES
//////////////////////////////////////////////////////////////////////////
static void
bbsluaHook(lua_State *L, lua_Debug* ar)
{
// vmsg("bbslua HOOK!");
if (blrt.abort)
{
drop_input();
lua_yield(L, 0);
return;
}
if (ar->event != LUA_HOOKCOUNT)
return;
#ifdef BBSLUA_USAGE
bbslua_count += BLCONF_EXEC_COUNT;
#endif
// now, peek and check
if (input_isfull())
drop_input();
// refresh();
// check if input key is system break key.
if (bl_peekbreak(BLCONF_PEEK_TIME))
{
lua_yield(L, 0);
return;
}
}
static char *
bbslua_attach(const char *fpath, size_t *plen)
{
char *buf = NULL;
#ifdef BLCONF_MMAP_ATTACH
struct stat st;
int fd = open(fpath, O_RDONLY);
*plen = 0;
if (fd < 0) return buf;
if (fstat(fd, &st) || ((*plen = st.st_size) < 1) || S_ISDIR(st.st_mode))
{
close(fd);
return buf;
}
*plen = *plen +1;
buf = mmap(NULL, *plen, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (buf == NULL || buf == MAP_FAILED)
{
*plen = 0;
return NULL;
}
madvise(buf, *plen, MADV_SEQUENTIAL);
#else // !BLCONF_MMAP_ATTACH
FILE *fp = fopen(fpath, "rt");
*plen = 0;
if (!fp)
return NULL;
fseek(fp, 0, SEEK_END);
*plen = ftell(fp);
buf = (char*) malloc (*plen);
rewind(fp);
fread(buf, *plen, 1, fp);
#endif // !BLCONF_MMAP_ATTACH
return buf;
}
static void
bbslua_detach(char *p, int len)
{
#ifdef BLCONF_MMAP_ATTACH
munmap(p, len);
#else // !BLCONF_MMAP_ATTACH
free(p);
#endif // !BLCONF_MMAP_ATTACH
}
int
bbslua_isHeader(const char *ps, const char *pe)
{
int szsig = strlen(BBSLUA_SIGNATURE);
if (ps + szsig > pe)
return 0;
if (strncmp(ps, BBSLUA_SIGNATURE, szsig) == 0)
return 1;
return 0;
}
static int
bbslua_detect_range(char **pbs, char **pbe, int *lineshift)
{
int szsig = strlen(BBSLUA_SIGNATURE);
char *bs, *be, *ps, *pe;
int line = 0;
bs = ps = *pbs;
be = pe = *pbe;
// find start
while (ps + szsig < pe)
{
if (strncmp(ps, BBSLUA_SIGNATURE, szsig) == 0)
break;
// else, skip to next line
while (ps + szsig < pe && *ps++ != '\n');
line++;
}
*lineshift = line;
if (!(ps + szsig < pe))
return 0;
*pbs = ps;
*pbe = be;
// find tail by SIGNATURE
pe = ps + 1;
while (pe + szsig < be)
{
if (strncmp(pe, BBSLUA_SIGNATURE, szsig) == 0)
break;
// else, skip to next line
while (pe + szsig < be && *pe++ != '\n');
}
if (pe + szsig < be)
{
// found sig, end at such line.
pe--;
*pbe = pe;
} else {
// abort.
*pbe = NULL;
*pbs = NULL;
return 0;
}
// prevent trailing zeros
pe = *pbe;
while (pe > ps && !*pe)
pe--;
*pbe = pe;
return 1;
}
//////////////////////////////////////////////////////////////////////////
// BBSLUA TOC Processing
//////////////////////////////////////////////////////////////////////////
static const char *bbsluaTocTags[] =
{
"interface",
"latestref",
// BLCONF_PRINT_TOC_INDEX
"title",
"notes",
"author",
"version",
"date",
NULL
};
static const char *bbsluaTocPrompts[] =
{
"界面版本",
"最新版本",
// BLCONF_PRINT_TOC_INDEX
"名稱",
"說明",
"作者",
"版本",
"日期",
NULL
};
int
bbslua_load_TOC(lua_State *L, const char *bs, const char *be, char **ppc)
{
unsigned char *ps = NULL, *pe = NULL;
int i = 0;
lua_newtable(L);
*ppc = NULL;
while (bs < be)
{
// find stripped line start, end
ps = pe = (unsigned char *) bs;
while (pe < (unsigned char*)be && *pe != '\n' && *pe != '\r')
pe ++;
bs = (char*)pe+1;
while (ps < pe && *ps <= ' ') ps++;
while (pe > ps && *(pe-1) <= ' ') pe--;
// at least "--"
if (pe < ps+2)
break;
if (*ps++ != '-' || *ps++ != '-')
break;
while (ps < pe && *ps <= ' ') ps++;
// empty entry?
if (ps >= pe)
continue;
// find pattern
for (i = 0; bbsluaTocTags[i]; i++)
{
int l = strlen(bbsluaTocTags[i]);
if (ps + l > pe)
continue;
if (strncasecmp((char*)ps, bbsluaTocTags[i], l) != 0)
continue;
ps += l;
// found matching pattern, now find value
while (ps < pe && *ps <= ' ') ps++;
if (ps >= pe || *ps++ != ':')
break;
while (ps < pe && *ps <= ' ') ps++;
// finally, (ps, pe) is the value!
if (ps >= pe)
break;
lua_pushlstring(L, (char*)ps, pe-ps);
// accept only real floats for interface ([0])
if (i == 0 && lua_tonumber(L, -1) <= 0)
{
lua_pop(L, 1);
}
else
{
lua_setfield(L, -2, bbsluaTocTags[i]);
}
break;
}
}
if (ps >= (unsigned char*)bs && ps < (unsigned char*)be)
*ppc = (char*)ps;
lua_setglobal(L, "toc");
return 0;
}
static void
fullmsg(const char *s)
{
clrtoeol();
prints("%-*.*s\n", t_columns-1, t_columns-1, s);
}
void
bbslua_logo(lua_State *L)
{
int y, by = b_lines -1; // print - back from bottom
int i = 0;
double tocinterface = 0;
int tocs = 0;
char msg[STRLEN];
// get toc information
lua_getglobal(L, "toc");
lua_getfield(L, -1, bbsluaTocTags[0]);
tocinterface = lua_tonumber(L, -1); lua_pop(L, 1);
// query print-able toc tags
for (i = BLCONF_PRINT_TOC_INDEX; bbsluaTocTags[i]; i++)
{
lua_getfield(L, -1, bbsluaTocTags[i]);
if (lua_tostring(L, -1))
tocs++;
lua_pop(L, 1);
}
// prepare logo window
grayout(0, b_lines, GRAYOUT_DARK);
// print compatibility test
// now (by) is the base of new information
if (tocinterface == 0)
{
by -= 4; y = by+2;
move(y-1, 0);
outs(ANSI_COLOR(0;31;47));
fullmsg("");
fullmsg(" ▲ 此程式缺少相容性資訊,您可能無法正常執行");
fullmsg(" 若執行出現錯誤,請向原作者取得新版");
fullmsg("");
}
else if (tocinterface > BBSLUA_INTERFACE_VER)
{
by -= 4; y = by+2;
move(y-1, 0);
outs(ANSI_COLOR(0;1;37;41));
fullmsg("");
snprintf(msg, sizeof(msg),
" ▲ 此程式使用新版的 BBS-Lua 規格 (%0.3f),您可能無法正常執行",
tocinterface);
fullmsg(msg);
fullmsg(" 若執行出現錯誤,建議您重新登入 BBS 後再重試");
fullmsg("");
}
else if (tocinterface == BBSLUA_INTERFACE_VER)
{
// do nothing!
}
else
{
// should be comtaible
// prints("相容 (%.03f)", tocinterface);
}
// print toc, if any.
if (tocs)
{
y = by - 1 - tocs;
by = y-1;
move(y, 0); outs(ANSI_COLOR(0;1;30;47));
fullmsg("");
// now try to print all TOC infos
for (i = BLCONF_PRINT_TOC_INDEX; bbsluaTocTags[i]; i++)
{
lua_getfield(L, -1, bbsluaTocTags[i]);
if (!lua_isstring(L, -1))
{
lua_pop(L, 1);
continue;
}
move(++y, 0);
snprintf(msg, sizeof(msg), " %s: %-.*s",
bbsluaTocPrompts[i], STRLEN-12, lua_tostring(L, -1));
msg[sizeof(msg)-1] = 0;
fullmsg(msg);
lua_pop(L, 1);
}
fullmsg("");
}
// print caption
move(by-2, 0); outc('\n');
outs(ANSI_COLOR(0;1;37;44));
snprintf(msg, sizeof(msg),
" ■ BBS-Lua %.03f (Build " __DATE__ " " __TIME__") ",
(double)BBSLUA_INTERFACE_VER);
fullmsg(msg);
// system break key prompt
{
int sz = t_columns -1;
const char
*prompt1 = " 提醒您執行中隨時可按 ",
*prompt2 = "[Ctrl-C]",
*prompt3 = " 強制中斷 BBS-Lua 程式";
sz -= strlen(prompt1);
sz -= strlen(prompt2);
sz -= strlen(prompt3);
outs(ANSI_COLOR(22;37)); outs(prompt1);
outs(ANSI_COLOR(1;31)); outs(prompt2);
outs(ANSI_COLOR(0;37;44));outs(prompt3);
prints("%*s", sz, "");
outs(ANSI_RESET);
}
lua_pop(L, 1);
}
//////////////////////////////////////////////////////////////////////////
// BBSLUA Script Loader
//////////////////////////////////////////////////////////////////////////
typedef struct LoadS {
const char *s;
size_t size;
int lineshift;
} LoadS;
static const char* bbslua_reader(lua_State *L, void *ud, size_t *size)
{
LoadS *ls = (LoadS *)ud;
(void)L;
if (ls->size == 0) return NULL;
if (ls->lineshift > 0) {
const char *linefeed = "\n";
*size = 1;
ls->lineshift--;
return linefeed;
}
*size = ls->size;
ls->size = 0;
return ls->s;
}
static int bbslua_loadbuffer (lua_State *L, const char *buff, size_t size,
const char *name, int lineshift) {
LoadS ls;
ls.s = buff;
ls.size = size;
ls.lineshift = lineshift;
return lua_load(L, bbslua_reader, &ls, name);
}
typedef struct AllocData {
size_t alloc_size;
size_t max_alloc_size;
size_t alloc_limit;
} AllocData;
static void alloc_init(AllocData *ad)
{
memset(ad, 0, sizeof(*ad));
// real limit is not determined yet, just assign a big value
ad->alloc_limit = 20*1048576;
}
static void *allocf (void *ud, void *ptr, size_t osize, size_t nsize) {
/* TODO use our own allocator, for better memory control, avoid fragment and leak */
AllocData *ad = (AllocData*)ud;
if (ad->alloc_size + nsize - osize > ad->alloc_limit) {
return NULL;
}
ad->alloc_size += nsize - osize;
if (ad->alloc_size > ad->max_alloc_size) {
ad->max_alloc_size = ad->alloc_size;
}
if (nsize == 0) {
free(ptr);
return NULL;
}
else
return realloc(ptr, nsize);
}
static int panic (lua_State *L) {
(void)L; /* to avoid warnings */
fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
lua_tostring(L, -1));
return 0;
}
void bbslua_loadLatest(lua_State *L,
char **pbs, char **pps, char **ppe, char **ppc, int *psz,
int *plineshift, char *bfpath, const char **pfpath)
{
int r = 1; // max redirect for one article only.
do {
char *xbs = NULL, *xps = NULL, *xpe = NULL, *xpc = NULL;
int xlineshift = 0;
size_t xsz;
const char *lastref = NULL;
char loadnext = 0, isnewver = 0;
// detect file
xbs = bbslua_attach(bfpath, &xsz);
if (!xbs)
break;
xps = xbs;
xpe = xps + xsz;
if(!bbslua_detect_range(&xps, &xpe, &xlineshift))
{
// not detected
bbslua_detach(xbs, xsz);
break;
}
// detect and verify TOC meta data
lua_getglobal(L, "toc"); // stack 1
if (lua_istable(L, -1))
{
const char *oref, *otitle, *nref, *ntitle;
// load and verify tocinfo
lua_getfield(L, -1, "latestref"); // stack 2
oref = lua_tostring(L, -1);
lua_getfield(L, -2, "title"); // stack 3
otitle = lua_tostring(L, -1);
bbslua_load_TOC(L, xps, xpe, &xpc);
lua_getglobal(L, "toc"); // stack 4
lua_getfield(L, -1, "latestref"); // stack 5
nref = lua_tostring(L, -1);
lua_getfield(L, -2, "title"); // stack 6
ntitle = lua_tostring(L, -1);
if (oref && nref && otitle && ntitle &&
strcmp(oref, nref) == 0 &&
strcmp(otitle, ntitle) == 0)
isnewver = 1;
// pop all
lua_pop(L, 5); // stack = 1 (old toc)
if (!isnewver)
lua_setglobal(L, "toc");
else
lua_pop(L, 1);
} else {
lua_pop(L, 1);
bbslua_load_TOC(L, xps, xpe, &xpc);
}
if (*pbs && !isnewver)
{
// different.
bbslua_detach(xbs, xsz);
break;
}
// now, apply current buffer
if (*pbs)
{
bbslua_detach(*pbs, *psz);
// XXX fpath=bfpath, supporting only one level redirection now.
*pfpath = bfpath;
}
*pbs = xbs; *pps = xps; *ppe = xpe; *psz = xsz;
*ppc = xpc;
*plineshift = xlineshift;
#ifdef AID_DISPLAYNAME
// quick exit
if (r <= 0)
break;
// LatestRef only works if system supports AID.
// try to load next reference.
lua_getglobal(L, "toc"); // stack 1
lua_getfield(L, -1, "latestref"); // stack 2
lastref = lua_tostring(L, -1);
while (lastref && *lastref)
{
// try to load by AID
char bn[IDLEN+1] = "";
aidu_t aidu = 0;
unsigned char *p = (unsigned char*)bn;
if (*lastref == '#') lastref++;
aidu = aidc2aidu((char*)lastref);
if (aidu <= 0) break;
while (*lastref > ' ') lastref ++; // lastref points to zero of space
while (*lastref && *lastref <= ' ') lastref++; // lastref points to zero or board name
if (*lastref == '(') lastref ++;
if (!*lastref) break;
strlcpy(bn, lastref, sizeof(bn));
// truncate board name
// (not_alnum(ch) && ch != '_' && ch != '-' && ch != '.')
while (*p &&
(isalnum(*p) || *p == '_' || *p == '-' || *p == '.')) p++;
*p = 0;
if (bn[0])
{
bfpath[0] = 0;
setaidfile(bfpath, bn, aidu);
}
if (bfpath[0])
loadnext = 1;
break;
}
lua_pop(L, 2);
if (loadnext) continue;
#endif // AID_DISPLAYNAME
break;
} while (r-- > 0);
}
//////////////////////////////////////////////////////////////////////////
// BBSLUA Hash
//////////////////////////////////////////////////////////////////////////
#if 0
static int
bbslua_hashWriter(lua_State *L, const void *p, size_t sz, void *ud)
{
Fnv32_t *phash = (Fnv32_t*) ud;
*phash = fnv_32_buf(p, sz, *phash);
return 0;
}
static Fnv32_t
bbslua_f2hash(lua_State *L)
{
Fnv32_t fnvseed = FNV1_32_INIT;
lua_dump(L, bbslua_hashWriter, &fnvseed);
return fnvseed;
}
static Fnv32_t
bbslua_str2hash(const char *str)
{
if (!str)
return 0;
return fnv_32_str(str, FNV1_32_INIT);
}
#endif
static Fnv32_t
bbslua_path2hash(const char *path)
{
Fnv32_t seed = FNV1_32_INIT;
if (!path)
return 0;
if (path[0] != '/') // relative, append BBSHOME
seed = fnv_32_str(BBSHOME "/", seed);
return fnv_32_str(path, seed);
}
//////////////////////////////////////////////////////////////////////////
// BBSLUA Main
//////////////////////////////////////////////////////////////////////////
int
bbslua(const char *fpath)
{
int r = 0;
lua_State *L;
char *bs = NULL, *ps = NULL, *pe = NULL, *pc = NULL;
char bfpath[PATHLEN] = "";
int sz = 0;
int lineshift = 0;
AllocData ad;
#ifdef BBSLUA_USAGE
struct rusage rusage_begin, rusage_end;
struct timeval lua_begintime, lua_endtime;
gettimeofday(&lua_begintime, NULL);
getrusage(0, &rusage_begin);
#endif
#ifdef UMODE_BBSLUA
unsigned int prevmode = getutmpmode();
#endif
// re-entrant not supported!
if (blrt.running)
return 0;
// initialize runtime
BL_INIT_RUNTIME();
#ifdef BBSLUA_USAGE
bbslua_count = 0;
#endif
// init lua
alloc_init(&ad);
L = lua_newstate(allocf, &ad);
if (!L)
return 0;
lua_atpanic(L, &panic);
strlcpy(bfpath, fpath, sizeof(bfpath));
// load file
bbslua_loadLatest(L, &ps, &ps, &pe, &pc, &sz, &lineshift, bfpath, &fpath);
if (!ps || !pe || ps >= pe)
{
if (bs)
bbslua_detach(bs, sz);
lua_close(L);
vmsg("BBS-Lua 載入錯誤: 未含 BBS-Lua 程式");
return 0;
}
// init library
bbsluaL_openlibs(L);
luaL_openlib(L, "bbs", lib_bbslua, 0);
#ifdef BLSCONF_ENABLED
luaL_openlib(L, "store", lib_store, 0);
#endif // BLSCONF_ENABLED
bbsluaRegConst(L);
// load script
r = bbslua_loadbuffer(L, ps, pe-ps, "BBS-Lua", lineshift);
// build hash or store name
blrt.storename = bbslua_path2hash(fpath);
// vmsgf("BBS-Lua Hash: %08X", blrt.storename);
// unmap
bbslua_detach(bs, sz);
if (r != 0)
{
const char *errmsg = lua_tostring(L, -1);
lua_close(L);
outs(ANSI_RESET);
move(b_lines-3, 0); clrtobot();
outs("\n");
outs(errmsg);
vmsg("BBS-Lua 載入錯誤: 請通知作者修正程式碼。");
return 0;
}
#ifdef UMODE_BBSLUA
setutmpmode(UMODE_BBSLUA);
#endif
bbslua_logo(L);
vmsgf("提醒您執行中隨時可按 [Ctrl-C] 強制中斷 BBS-Lua 程式");
// ready for running
clear();
blrt.running =1;
lua_sethook(L, bbsluaHook, LUA_MASKCOUNT, BLCONF_EXEC_COUNT );
refresh();
// check is now done inside hook
r = lua_resume(L, 0);
// even if r == yield, let's abort - you cannot yield main thread.
if (r != 0)
{
const char *errmsg = lua_tostring(L, -1);
move(b_lines-3, 0); clrtobot();
outs("\n");
if (errmsg) outs(errmsg);
}
lua_close(L);
blrt.running =0;
drop_input();
#ifdef BBSLUA_USAGE
{
double cputime;
double load;
double walltime;
getrusage(0, &rusage_end);
gettimeofday(&lua_endtime, NULL);
cputime = bl_tv2double(&rusage_end.ru_utime) - bl_tv2double(&rusage_begin.ru_utime);
walltime = bl_tv2double(&lua_endtime) - bl_tv2double(&lua_begintime);
load = cputime / walltime;
log_filef("log/bbslua.log", LOG_CREAT,
"maxalloc=%d leak=%d op=%d cpu=%.3f Mop/s=%.1f load=%f file=%s\n",
(int)ad.max_alloc_size, (int)ad.alloc_size,
bbslua_count, cputime, bbslua_count / cputime / 1000000.0, load * 100,
fpath);
}
#endif
// grayout(0, b_lines, GRAYOUT_DARK);
move(b_lines, 0); clrtoeol();
vmsgf("BBS-Lua 執行結束%s。",
blrt.abort ? " (使用者中斷)" : r ? " (程式錯誤)" : "");
BL_END_RUNTIME();
clear();
#ifdef UMODE_BBSLUA
setutmpmode(prevmode);
#endif
return 0;
}
// vim:ts=4:sw=4
|