forked from itszero/bbs-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbsruby.c
1249 lines (1080 loc) · 35.9 KB
/
bbsruby.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
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
//------------------------------------------------------------------------
// bbsruby environment settings
//------------------------------------------------------------------------
/* Default settings */
#define BBSRUBY_HAVE_VTUIKIT // The BBS has vtuikit
//#define BBSRUBY_NATIVE_B_LINES // `b_lines` is a global variable
//#define BBSRUBY_NATIVE_B_COLS // `b_cols` is a global variable
//#define BBSRUBY_USE_RH_COOR // Use right-handed coordinate; `x` => row, `y` => col
#define BBSRUBY_HAVE_GETYX // The BBS has `getyx()`
//#define BBSRUBY_HAVE_STR_RANSI // The BBS has `str_ransi` variable
//#define BBSRUBY_VER_INFO_FILE // The file with version information for BBS-Ruby
#define BBSRUBY_HAVE_BMIN_BMAX // The BBS has macros `BMIN` and `BMAX`
//#define BBSRUBY_USE_MRUBY // Use mruby instead of CRuby
//#define BBSRUBY_MRUBY_HAS_FULLCORE_GEMBOX // The mruby library is compiled with the `full-core` gembox
#include "bbs.h"
#include <sys/time.h>
#ifdef M3_USE_BBSRUBY // For compiling on Maple3
#undef BBSRUBY_HAVE_VTUIKIT
#define BBSRUBY_NATIVE_B_LINES
#define BBSRUBY_NATIVE_B_COLS
#undef BBSRUBY_USE_RH_COOR
#define BBSRUBY_HAVE_GETYX
#define BBSRUBY_HAVE_STR_RANSI
#define BBSRUBY_VER_INFO_FILE "bbs_script.h"
//#define BBSRUBY_USE_MRUBY
//#define BBSRUBY_MRUBY_HAS_FULLCORE_GEMBOX
#endif //M3_USE_BBSRUBY
#ifdef PTT_USE_BBSRUBY // For compiling on PttBBS
#define BBSRUBY_HAVE_VTUIKIT
#undef BBSRUBY_NATIVE_B_LINES
#undef BBSRUBY_NATIVE_B_COLS
#undef BBSRUBY_USE_RH_COOR
#define BBSRUBY_HAVE_GETYX
#undef BBSRUBY_HAVE_STR_RANSI
#undef BBSRUBY_VER_INFO_FILE
//#define BBSRUBY_USE_MRUBY
//#define BBSRUBY_MRUBY_HAS_FULLCORE_GEMBOX
#endif //PTT_USE_BBSRUBY
/* Inferred settings */
#ifdef USE_PFTERM
# define BBSRUBY_HAVE_GETYX
#endif
#ifdef BBSRUBY_VER_INFO_FILE
# include BBSRUBY_VER_INFO_FILE
#endif
//------------------------------------------------------------------------
// Redirect macros and functions
//------------------------------------------------------------------------
#if !defined(BBSRUBY_NATIVE_B_LINES) && !defined(b_lines)
#define b_lines ((void)0, t_lines - 1)
#endif
#if !defined(BBSRUBY_NATIVE_B_COLS) && !defined(b_cols)
#define b_cols ((void)0, t_columns - 1)
#endif
#ifndef BBSRUBY_HAVE_STR_RANSI
static const char str_ransi[] = "\x1b[m";
#endif
#ifndef COLOR1
#define COLOR1 "\x1b[34;46m" /* footer/feeter 的前段顏色 */
#endif
#ifndef COLOR2
#define COLOR2 "\x1b[31;47m" /* footer/feeter 的後段顏色 */
#endif
#ifdef BBSRUBY_HAVE_VTUIKIT
#define vs_bar(title) vs_hdr(title)
#define vget(y, x, msg, buf, size, mode) getdata(y, x, msg, buf, size, mode)
#endif
#ifndef BBSRUBY_HAVE_GETYX
static inline void getyx(int *y, int *x)
{
*y = cur_row;
*x = cur_col;
}
#endif
#ifdef BBSRUBY_USE_RH_COOR
#define BRB_COOR_ROW "x"
#define BRB_COOR_COL "y"
#else
#define BRB_COOR_ROW "y"
#define BRB_COOR_COL "x"
#endif
#ifndef BBSRUBY_HAVE_BMIN_BMAX // The BBS has macros `MIN` and `MAX` instead
#define BMIN(a, b) MIN(a, b)
#define BMAX(a, b) MAX(a, b)
#endif
#ifndef DL_RELEASE_VOID
#define DL_RELEASE_VOID() (void) ((void)0)
#endif
//-------------------------------------------------------
// BBSRuby.c
//-------------------------------------------------------
// target : Ruby script support for BBS
// author : Zero <itszero at gmail.com>
// Version: 0.2
// create : 2007/01/08
// update : 2007/01/08
// This program is licensed under MIT License.
//-------------------------------------------------------
#include "bbs.h"
#ifdef BBSRUBY_USE_MRUBY
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/compile.h>
#include <mruby/error.h>
#include <mruby/hash.h>
#include <mruby/numeric.h>
#include <mruby/proc.h>
#include <mruby/string.h>
#include <mruby/variable.h>
#include <mruby/version.h>
#define BRBCPP_EVAL(...) __VA_ARGS__
#define RBF_P(paras) (mrb_state *mrb, mrb_value self) // `rb_func_t` parameter
#define RB_P(paras) (mrb_state *mrb, BRBCPP_EVAL paras) // parameter
#define RB_PV(paras) (mrb_state *mrb) // parameter void
#define RBF_ARG(...) (mrb, self) // `rb_func_t` arguments
#define RB_ARG(...) (mrb, __VA_ARGS__) // arguments
#define RB_VARG() (mrb) // void argument
#define RBF_C(func) ((void)0, func)RBF_ARG // `rb_func_t` call
#define RB_C(func) ((void)0, m##func)RB_ARG // Ruby API call
#define RB_CV(func) ((void)0, m##func)RB_VARG // Ruby API call void
#define BRB_C(func) ((void)0, func)RB_ARG // normal ruby call
#define BRB_CV(func) ((void)0, func)RB_VARG // normal ruby call void
#define CMRB_C(crbfunc, mrbfunc) ((void)0, mrbfunc)RB_ARG // (CRuby, mruby) API call
#define CMRB_CV(crbfunc, mrbfunc) ((void)0, mrbfunc)RB_VARG // (CRuby, mruby) API call void
typedef mrb_value VALUE;
typedef struct RClass *rb_class_t;
typedef mrb_func_t rb_func_t;
#define RB_ARGS_REQ(n) MRB_ARGS_REQ(n)
#define RB_ARGS_REST() MRB_ARGS_REST()
#define rb_cObject mrb->object_class
#define Qnil mrb_nil_value()
#define Qundef mrb_undef_value()
#define INT2FIX(i) mrb_fixnum_value(i)
#define INT2NUM(i) mrb_fixnum_value(i)
#define NUM2INT(o) mrb_int(mrb, o)
#define NUM2DBL(o) mrb_to_flo(mrb, o)
#define StringValueCStr(v) mrb_string_value_cstr(mrb, &(v))
#define RTEST(o) mrb_test(o)
#define BRB_HOOK_COUNT_FACTOR 100
#else
#include <ruby.h>
#ifndef RUBY_VM
#include <rubysig.h>
#include <node.h>
typedef rb_event_t rb_event_flag_t;
#endif
#define RBF_P(paras) paras // `rb_func_t` parameter
#define RB_P(paras) paras // parameter
#define RB_PV(paras) (void) // parameter void
#define RBF_C(func) ((void)0, func) // `rb_func_t` call
#define RB_C(func) ((void)0, func) // Ruby API call
#define RB_CV(func) ((void)0, func) // Ruby API call void
#define BRB_C(func) ((void)0, func) // normal ruby call
#define BRB_CV(func) ((void)0, func) // normal ruby call void
#define CMRB_C(crbfunc, mrbfunc) ((void)0, crbfunc) // (CRuby, mruby) API call
#define CMRB_CV(crbfunc, mrbfunc) ((void)0, crbfunc) // (CRuby, mruby) API call void
typedef VALUE (*rb_func_t)(ANYARGS);
typedef VALUE rb_class_t;
#define RB_ARGS_REQ(n) (n)
#define RB_ARGS_REST() (-1)
#define BRB_HOOK_COUNT_FACTOR 1
#endif
#define BBSRUBY_MAJOR_VERSION 0
#define BBSRUBY_MINOR_VERSION 3
#ifndef BBSRUBY_PATCH_VERSION
#define BBSRUBY_PATCH_VERSION 0
#endif
#ifndef BBSRUBY_DL_PATCH_VERSION
#define BBSRUBY_DL_PATCH_VERSION 0
#endif
#ifndef BBSRUBY_VERSION_STR
#define BBSRUBY_VERSION_STR "v0.3"
#endif
#define BBSRUBY_VERSION_VALUE (1000000 * BBSRUBY_MAJOR_VERSION + 10000 * BBSRUBY_MINOR_VERSION + 100 * BBSRUBY_PATCH_VERSION + BBSRUBY_DL_PATCH_VERSION)
#define BBSRUBY_SIGNATURE "###BBSRuby"
#define BBSRUBY_INTERFACE_VER 0.111
static int ABORT_BBSRUBY = 0;
#define BBSRUBY_TOC_HEADERS 6
static const char* const TOCs_HEADER[BBSRUBY_TOC_HEADERS] = {"interface", "title", "notes", "author", "version", "date"};
static VALUE TOCs_rubyhash;
static double KBHIT_TMIN = 0.001;
static double KBHIT_TMAX = 60*10;
static VALUE KB_QUEUE;
static int badxy_compat = 0; // Whether to replicate the behavior of badly confused x/y coordination in v0.3 and before
static int pause_msg(const char *msg, const char *reason, const char *prompt);
static int getkey(double wait);
/* BBS helper class : following BBSLua SDK */
VALUE brb_keyToString RB_P((int key))
{
if (key == Ctrl('C')) // System-wide Abort
ABORT_BBSRUBY = 1;
if (key == KEY_UP)
return RB_C(rb_str_new_cstr)("UP");
else if (key == KEY_DOWN)
return RB_C(rb_str_new_cstr)("DOWN");
else if (key == KEY_LEFT)
return RB_C(rb_str_new_cstr)("LEFT");
else if (key == KEY_RIGHT)
return RB_C(rb_str_new_cstr)("RIGHT");
else if (key == KEY_HOME)
return RB_C(rb_str_new_cstr)("HOME");
else if (key == KEY_INS)
return RB_C(rb_str_new_cstr)("INS");
else if (key == KEY_DEL)
return RB_C(rb_str_new_cstr)("DEL");
else if (key == KEY_END)
return RB_C(rb_str_new_cstr)("END");
else if (key == KEY_PGUP)
return RB_C(rb_str_new_cstr)("PGUP");
else if (key == KEY_PGDN)
return RB_C(rb_str_new_cstr)("PGDN");
#ifdef KEY_BKSP
else if (key == KEY_BKSP)
#else
else if (key == '\b' || key == 0x7F)
#endif
return RB_C(rb_str_new_cstr)("BKSP");
else if (key == KEY_TAB)
return RB_C(rb_str_new_cstr)("TAB");
else if (key == KEY_ENTER)
return RB_C(rb_str_new_cstr)("ENTER");
else if (key > 0x00 && key < 0x1b) // Ctrl()
{
char buf[3];
sprintf(buf, "^%c", key + 'A' - 1);
return RB_C(rb_str_new_cstr)(buf);
}
else // Normal characters
{
char buf[2];
sprintf(buf, "%c", key);
return RB_C(rb_str_new_cstr)(buf);
}
}
VALUE brb_clock RBF_P((VALUE self))
{
struct timeval tv;
struct timezone tz;
double d = 0;
memset(&tz, 0, sizeof(tz));
gettimeofday(&tv, &tz);
d = tv.tv_sec + tv.tv_usec / (double)1000000;
return CMRB_C(rb_float_new, mrb_float_value)(d);
}
VALUE brb_getch RBF_P((VALUE self))
{
//if (RARRAY_LEN(KB_QUEUE) == 0)
int c = vkey();
return BRB_C(brb_keyToString)(c);
//else
//return RB_C(rb_ary_pop)(KB_QUEUE);
}
#define MACRO_NONZERO(macro) ((macro-0) != 0)
VALUE brb_getdata RBF_P((int argc, VALUE *argv, VALUE self))
{
#ifdef BBSRUBY_USE_MRUBY
mrb_int argc;
VALUE *argv;
mrb_get_args(mrb, "*", &argv, &argc);
#endif
int echo = DOECHO;
if (argc > 1 && NUM2INT(argv[1]) == 0)
#if MACRO_NONZERO(VGET_STEALTH_NOECHO)
echo = NOECHO | VGET_STEALTH_NOECHO;
#else
echo = NOECHO;
#endif
#if MACRO_NONZERO(VGET_STRICT_DOECHO)
if (echo == NOECHO)
echo |= VGET_STRICT_DOECHO;
#endif
#if defined(VGET_STRICT_DOECHO) && MACRO_NONZERO(VGET_BREAKABLE)
echo |= VGET_BREAKABLE;
#endif
int maxsize = NUM2INT(argv[0]);
maxsize = BMIN(maxsize, 511);
char data[512] = "";
int cur_row, cur_col;
getyx(&cur_row, &cur_col);
int res = vget(cur_row, cur_col, "", data, maxsize, echo);
if (res <= 0)
{
#ifdef VGET_EXIT_BREAK
if (res == VGET_EXIT_BREAK)
#else
if (buf[1] == Ctrl('C'))
#endif
{
ABORT_BBSRUBY = 1;
}
return RB_C(rb_str_new_cstr)("");
}
else
{
return RB_C(rb_str_new_cstr)(data);
}
}
VALUE brb_kbhit RBF_P((VALUE self, VALUE wait))
{
#ifdef BBSRUBY_USE_MRUBY
VALUE wait;
mrb_get_args(mrb, "o", &wait);
#endif
double data = NUM2DBL(wait);
data = BMIN(BMAX(data, KBHIT_TMIN), KBHIT_TMAX);
if (getkey(data) != 0)
{
//RB_C(rb_ary_push)(KB_QUEUE, BRB_C(brb_keyToString)(data));
return INT2NUM(1);
}
else
return INT2NUM(0);
}
VALUE brb_outs RBF_P((int argc, VALUE *argv, VALUE self))
{
#ifdef BBSRUBY_USE_MRUBY
mrb_int argc;
VALUE *argv;
mrb_get_args(mrb, "*", &argv, &argc);
#endif
for (int i=0; i<argc; i++)
{
outs(StringValueCStr(argv[i]));
}
return Qnil;
}
VALUE brb_title RBF_P((VALUE self, VALUE msg))
{
#ifdef BBSRUBY_USE_MRUBY
VALUE msg;
mrb_get_args(mrb, "o", &msg);
#endif
vs_bar(StringValueCStr(msg));
return Qnil;
}
VALUE brb_print RBF_P((int argc, VALUE *argv, VALUE self))
{
RBF_C(brb_outs)(argc, argv, self);
outs("\n");
return Qnil;
}
VALUE brb_getmaxyx RBF_P((VALUE self))
{
VALUE rethash = RB_CV(rb_hash_new)();
CMRB_C(rb_hash_aset, mrb_hash_set)(rethash, RB_C(rb_str_new_cstr)(badxy_compat ? "x" : BRB_COOR_ROW), INT2NUM(b_lines + 1));
CMRB_C(rb_hash_aset, mrb_hash_set)(rethash, RB_C(rb_str_new_cstr)(badxy_compat ? "y" : BRB_COOR_COL), INT2NUM(b_cols + 1));
return rethash;
}
VALUE brb_getyx RBF_P((VALUE self))
{
VALUE rethash = RB_CV(rb_hash_new)();
int cur_row, cur_col;
getyx(&cur_row, &cur_col);
CMRB_C(rb_hash_aset, mrb_hash_set)(rethash, RB_C(rb_str_new_cstr)(badxy_compat ? "x" : BRB_COOR_ROW), INT2NUM(badxy_compat ? cur_col : cur_row));
CMRB_C(rb_hash_aset, mrb_hash_set)(rethash, RB_C(rb_str_new_cstr)(badxy_compat ? "y" : BRB_COOR_COL), INT2NUM(badxy_compat ? cur_row : cur_col));
return rethash;
}
VALUE brb_move RBF_P((VALUE self, VALUE y, VALUE x))
{
#ifdef BBSRUBY_USE_MRUBY
VALUE y, x;
mrb_get_args(mrb, "oo", &y, &x);
#endif
move(NUM2INT(badxy_compat ? x : y), NUM2INT(badxy_compat ? y : x));
return Qnil;
}
VALUE brb_moverel RBF_P((VALUE self, VALUE dy, VALUE dx))
{
#ifdef BBSRUBY_USE_MRUBY
VALUE dy, dx;
mrb_get_args(mrb, "oo", &dy, &dx);
#endif
int cur_row, cur_col;
getyx(&cur_row, &cur_col);
move(cur_row + NUM2INT(badxy_compat ? dx : dy), cur_col + NUM2INT(badxy_compat ? dy : dx));
return Qnil;
}
VALUE brb_clear RBF_P((VALUE self)) { clear(); return Qnil; }
VALUE brb_clrtoeol RBF_P((VALUE self)) { clrtoeol(); return Qnil; }
VALUE brb_clrtobot RBF_P((VALUE self)) { clrtobot(); return Qnil; }
VALUE brb_refresh RBF_P((VALUE self)) { refresh(); return Qnil; }
VALUE brb_vmsg RBF_P((VALUE self, VALUE msg))
{
#ifdef BBSRUBY_USE_MRUBY
VALUE msg;
mrb_get_args(mrb, "o", &msg);
#endif
vmsg(StringValueCStr(msg));
return Qnil;
}
VALUE brb_name RBF_P((VALUE self)) { return RB_C(rb_str_new_cstr)(BBSNAME); }
VALUE brb_interface RBF_P((VALUE self)) { return CMRB_C(rb_float_new, mrb_float_value)(BBSRUBY_INTERFACE_VER); }
VALUE brb_ansi_color RBF_P((int argc, VALUE *argv, VALUE self))
{
#ifdef BBSRUBY_USE_MRUBY
mrb_int argc;
VALUE *argv;
mrb_get_args(mrb, "*", &argv, &argc);
#endif
char buf[50] = "\x1b[";
char *p = buf + strlen(buf);
for (int i=0; i<argc; i++)
{
int ansi = NUM2INT(argv[i]);
sprintf(p, "%d%s", ansi, (i == argc - 1) ? "" : ";");
p += strlen(p);
}
*p++ = 'm'; *p = '\0';
return RB_C(rb_str_new_cstr)(buf);
}
VALUE brb_ansi_reset RBF_P((VALUE self))
{
return RB_C(rb_str_new_cstr)("\x1b[m");
}
VALUE brb_esc RBF_P((VALUE self))
{
return RB_C(rb_str_new_cstr)("\x1b");
}
VALUE brb_color RBF_P((int argc, VALUE *argv, VALUE self))
{
#ifdef BBSRUBY_USE_MRUBY
mrb_int argc;
VALUE *argv;
mrb_get_args(mrb, "*", &argv, &argc);
#endif
VALUE str;
if (argc == 0)
str = RBF_C(brb_ansi_reset)(self);
else
str = RBF_C(brb_ansi_color)(argc, argv, self);
#ifdef BBSRUBY_USE_MRUBY
mrb_funcall_argv(mrb, self, mrb_intern_cstr(mrb, "outs"), 1, &str);
#else
brb_outs(1, &str, self);
#endif
return Qnil;
}
VALUE brb_userid RBF_P((VALUE self))
{
return RB_C(rb_str_new_cstr)(cuser.userid);
}
VALUE brb_pause RBF_P((VALUE self, VALUE msg))
{
#ifdef BBSRUBY_USE_MRUBY
VALUE msg;
mrb_get_args(mrb, "o", &msg);
#endif
int k = pause_msg(StringValueCStr(msg), "", "請按任意鍵繼續");
move(b_lines, 0);
clrtoeol();
return BRB_C(brb_keyToString)(k);
}
GCC_PURE VALUE brb_toc RBF_P((VALUE self))
{
return TOCs_rubyhash;
}
/* End of BBS helper class */
static int pause_msg(const char *msg, const char *reason, const char *prompt)
{
char buf[200];
move(b_lines, 0);
sprintf(buf, COLOR1 " ★ %s%s", msg, (reason) ? reason : "");
outs(buf);
char buf2[200];
sprintf(buf2, COLOR2 " [%s] ", prompt);
for (int i = b_cols + (int)(unsigned)(sizeof(COLOR1) + sizeof(COLOR2)) - (int)(unsigned)(strlen(buf) + strlen(buf2)); i > 3; i--)
{
outc(' ');
}
outs(buf2);
outs(str_ransi);
return vkey();
}
static int out_footer(
const char* reason,
const char* msg)
{
return pause_msg("BBSRuby " BBSRUBY_VERSION_STR " (" __DATE__ " " __TIME__ ")", reason, msg);
}
static int getkey(double wait)
{
fd_set fds;
struct timeval tv = {0L, 0L};
FD_ZERO(&fds);
FD_SET(0, &fds);
/* Determine whether to refresh */
if (select(1, &fds, NULL, NULL, &tv) > 0)
return vkey();
if (wait > KBHIT_TMIN)
refresh();
tv.tv_sec = (long)wait;
wait-=(long)wait;
tv.tv_usec = wait * 1000000;
/* 若有按鍵,回傳所按的鍵;若 delay 的時間到了仍沒有按鍵,回傳 0 */
if (select(1, &fds, NULL, NULL, &tv) > 0)
return vkey();
return 0;
}
static void BBSRubyHook(
#ifdef BBSRUBY_USE_MRUBY
mrb_state *mrb,
#if MRUBY_RELEASE_NO >= 30000
const mrb_irep *irep,
#else
mrb_irep *irep,
#endif
#if MRUBY_RELEASE_NO >= 20100
const mrb_code *pc,
#else
mrb_code *pc,
#endif
mrb_value *regs
#else
rb_event_flag_t event,
#ifdef RUBY_VM
VALUE data,
#else
NODE *node,
#endif
VALUE self,
ID mid,
VALUE klass
#endif
)
{
static int hook_count = 0;
hook_count++;
if (hook_count == BRB_HOOK_COUNT_FACTOR * 1000)
{
int key = getkey(KBHIT_TMIN);
if (key == Ctrl('C'))
ABORT_BBSRUBY = 1;
// else
// RB_C(rb_ary_push)(KB_QUEUE, INT2NUM(key));
hook_count = 0;
}
if (ABORT_BBSRUBY)
{
#ifdef BBSRUBY_USE_MRUBY
// Raise an exception to terminate execution
mrb_raise(mrb, mrb_exc_get(mrb, "Exception"), "User Interrupt");
#else
rb_thread_kill(rb_thread_current());
// TODO: Ensure all thread cleanup.
#endif
}
return;
}
static char*
ruby_script_attach(const char *fpath, size_t *plen)
{
struct stat st;
int fd = open(fpath, O_RDONLY, 0600);
char *buf = NULL;
*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;
}
buf = (char *) mmap(NULL, *plen, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (buf == NULL || buf == MAP_FAILED)
{
*plen = 0;
return NULL;
}
return buf;
}
static void ruby_script_detach(char *p, int len)
{
munmap(p, len);
}
static int ruby_script_range_detect(char **pStart, char **pEnd, int *lineshift)
{
int lenSignature = strlen(BBSRUBY_SIGNATURE);
// Search range
char *cStart, *cEnd;
int line = 0;
cStart = *pStart;
cEnd = *pEnd;
// Find head signature first
while (cStart + lenSignature < cEnd)
{
if (strncmp(cStart, BBSRUBY_SIGNATURE, lenSignature) == 0)
break;
// Skip to next line
while (cStart + lenSignature < cEnd && *cStart++ != '\n');
line++;
}
*lineshift = line;
if (cStart + lenSignature >= *pEnd) // Cannot found signature.
return 0;
*pStart = cStart;
// Find the tail signature
cEnd = cStart + 1;
while (cEnd + lenSignature < *pEnd)
{
if (strncmp(cEnd, BBSRUBY_SIGNATURE, lenSignature) == 0)
break;
// Skip to next line
while (cEnd + lenSignature < *pEnd && *cEnd++ != '\n');
}
if (cEnd + lenSignature >= *pEnd)
{
// Not found
*pStart = cStart;
*pEnd = cEnd;
return 0;
}
else
{
cEnd--;
*pEnd = cEnd;
}
cEnd = *pEnd;
while (cEnd > cStart && !*cEnd)
cEnd--;
*pEnd = cEnd;
return 1;
}
static void bbsruby_load_TOC RB_P((const char *cStart, const char *cEnd))
{
// Create TOC class wrapping these information
const char *tStart, *tEnd;
tStart = cStart;
VALUE hashTOC = RB_CV(rb_hash_new)();
GCC_UNUSED int TOCfound = 0;
// In this implement, we only allow TOC to be put BEFORE the actual script code
while (tStart < cEnd)
{
if (tStart[0] == '#' && tStart[1] == '#')
{
tStart += 2;
// The third '#' is optional
if (tStart[0] == '#')
tStart++;
while (*tStart == ' ') tStart++;
tEnd = tStart;
while (*tEnd != '\n') tEnd++;
// Possible TOC item, check patterns
for (int i=0; i<BBSRUBY_TOC_HEADERS; i++)
{
int lenBuf = strlen(TOCs_HEADER[i]);
if (strncasecmp(tStart, TOCs_HEADER[i], lenBuf) == 0)
{
tStart+=lenBuf;
while (*tStart == ' ') tStart++;
if (*tStart != ':') break;
tStart++;
while (*tStart == ' ') tStart++;
CMRB_C(rb_hash_aset, mrb_hash_set)(hashTOC, RB_C(rb_str_new_cstr)(TOCs_HEADER[i]), RB_C(rb_str_new)(tStart, tEnd - tStart));
CMRB_C(rb_hash_aset, mrb_hash_set)(hashTOC, CMRB_C(rb_funcallv, mrb_funcall_argv)(RB_C(rb_str_new_cstr)(TOCs_HEADER[i]), CMRB_C(rb_intern, mrb_intern_cstr)("capitalize!"), 0, NULL), RB_C(rb_str_new)(tStart, tEnd - tStart));
TOCfound = 1;
break;
}
}
tStart = tEnd + 1;
}
else
break;
}
TOCs_rubyhash = hashTOC;
}
static void print_exception RB_PV((void))
{
clear();
#ifdef BBSRUBY_USE_MRUBY
VALUE exception = mrb_obj_value(mrb->exc);
mrb->exc = mrb_obj_ptr(Qnil);
#else
VALUE exception = rb_errinfo();
rb_set_errinfo(Qnil);
#endif
if (!RTEST(exception)) return;
char* buffer = RSTRING_PTR(RB_C(rb_obj_as_string)(exception));
outs("\x1b[m");
clear();
move(0, 0);
outs("程式發生錯誤,無法繼續執行。請通知原作者。\n錯誤資訊:\n");
outs(buffer);
outs("\n");
VALUE ary = CMRB_C(rb_funcallv, mrb_funcall_argv)(exception, CMRB_C(rb_intern, mrb_intern_cstr)("backtrace"), 0, NULL);
#ifdef BBSRUBY_USE_MRUBY
// Some `Exception` objects have theirs `backtrace` unset in mruby
if (mrb_array_p(ary))
#endif
{
for (int c=0; c < RARRAY_LEN(ary); c++)
{
outs(" from: ");
outs(StringValueCStr(RARRAY_PTR(ary)[c]));
outs("\n");
}
}
out_footer(" (發生錯誤)", "按任意鍵返回");
}
#ifdef BBSRUBY_USE_MRUBY
#define BRBCPP_APPENDTO_NOEVAL(x, ...) __VA_ARGS__ ## x
#define BRBCPP_APPENDTO(...) BRBCPP_APPENDTO_NOEVAL(__VA_ARGS__)
#define BRBCPP_EXPAND_LIST(list) \
BRBCPP_APPENDTO(_END, BRBCPP_EXPAND_LIST_OP1 list)
#define BRBCPP_EXPAND_LIST_OP1(elem) \
BRBCPP_LIST_TRANSFORM(elem) BRBCPP_EXPAND_LIST_OP2
#define BRBCPP_EXPAND_LIST_OP2(elem) \
BRBCPP_LIST_TRANSFORM(elem) BRBCPP_EXPAND_LIST_OP1
#define BRBCPP_EXPAND_LIST_OP1_END
#define BRBCPP_EXPAND_LIST_OP2_END
/* mruby gem enabling conditions. Can be combined to achieve the logical AND logic */
#ifdef BBSRUBY_MRUBY_HAS_FULLCORE_GEMBOX
#define BRB_FULLCORE(_gem) _gem
#else
#define BRB_FULLCORE(_gem) /* Empty */
#endif
/* Assume `MRUBY_RELEASE_NO >= 10400` */
#if MRUBY_RELEASE_NO >= 10401
#define BRB_MRB10401(_gem) _gem
#define BRB_BEFORE_MRB10401(_gem) /* Empty */
#else
#define BRB_MRB10401(_gem) /* Empty */
#define BRB_BEFORE_MRB10401(_gem) _gem
#endif
#if MRUBY_RELEASE_NO >= 20000
#define BRB_MRB20000(_gem) _gem
#define BRB_BEFORE_MRB20000(_gem) /* Empty */
#else
#define BRB_MRB20000(_gem) /* Empty */
#define BRB_BEFORE_MRB20000(_gem) _gem
#endif
#if MRUBY_RELEASE_NO >= 20001
#define BRB_MRB20001(_gem) _gem
#define BRB_BEFORE_MRB20001(_gem) /* Empty */
#else
#define BRB_MRB20001(_gem) /* Empty */
#define BRB_BEFORE_MRB20001(_gem) _gem
#endif
#if MRUBY_RELEASE_NO >= 20100
#define BRB_MRB20100(_gem) _gem
#define BRB_BEFORE_MRB20100(_gem) /* Empty */
#else
#define BRB_MRB20100(_gem) /* Empty */
#define BRB_BEFORE_MRB20100(_gem) _gem
#endif
#if MRUBY_RELEASE_NO >= 20102
#define BRB_MRB20102(_gem) _gem
#define BRB_BEFORE_MRB20102(_gem) /* Empty */
#else
#define BRB_MRB20102(_gem) /* Empty */
#define BRB_BEFORE_MRB20102(_gem) _gem
#endif
#if MRUBY_RELEASE_NO >= 30000
#define BRB_MRB30000(_gem) _gem
#define BRB_BEFORE_MRB30000(_gem) /* Empty */
#else
#define BRB_MRB30000(_gem) /* Empty */
#define BRB_BEFORE_MRB30000(_gem) _gem
#endif
#define BRB_MRBGEM_LIST \
(mruby_sprintf) \
/* (mruby_print) */ \
(mruby_math) \
(mruby_time) \
(mruby_struct) \
(mruby_compar_ext) \
(mruby_enum_ext) \
(mruby_string_ext) \
(mruby_numeric_ext) \
(mruby_array_ext) \
(mruby_hash_ext) \
(mruby_range_ext) \
(mruby_proc_ext) \
(mruby_symbol_ext) \
(mruby_random) \
(mruby_object_ext) \
(mruby_objectspace) \
(mruby_fiber) \
(mruby_enumerator) \
(mruby_enum_lazy) \
(mruby_toplevel_ext) \
(mruby_kernel_ext) \
(mruby_class_ext) \
(mruby_error) \
/* BRB_FULLCORE((mruby_exit)) */ \
BRB_FULLCORE(BRB_BEFORE_MRB20102((mruby_eval))) \
/* BRB_FULLCORE(BRB_BEFORE_MRB10401((mruby_io))) */ \
BRB_FULLCORE(BRB_BEFORE_MRB20100((mruby_method))) \
/* BRB_MRB10401((mruby_io)) */ \
BRB_MRB10401((mruby_pack)) \
/* BRB_FULLCORE(BRB_BEFORE_MRB30000(BRB_MRB10401((mruby_socket)))) */ \
BRB_MRB20000((mruby_metaprog)) \
/* BRB_FULLCORE(BRB_MRB20000((mruby_sleep))) */ \
BRB_FULLCORE(BRB_MRB20001((mruby_enum_chain))) \
BRB_MRB20100((mruby_method)) \
BRB_FULLCORE(BRB_BEFORE_MRB20102(BRB_MRB20100((mruby_complex)))) \
BRB_FULLCORE(BRB_BEFORE_MRB20102(BRB_MRB20100((mruby_rational)))) \
BRB_MRB20102((mruby_eval)) \
BRB_MRB20102((mruby_complex)) \
BRB_MRB20102((mruby_rational)) \
/* BRB_MRB30000((mruby_socket)) */ \
BRB_FULLCORE(BRB_MRB30000((mruby_catch))) \
BRB_FULLCORE(BRB_MRB30000((mruby_os_memsize))) \
/* End of list */
#ifdef __cplusplus
extern "C" {
#endif
#undef BRBCPP_LIST_TRANSFORM
#define BRBCPP_LIST_TRANSFORM(elem) \
void GENERATED_TMP_mrb_ ## elem ## _gem_init(mrb_state *mrb); \
void GENERATED_TMP_mrb_ ## elem ## _gem_final(mrb_state *mrb);
BRBCPP_EXPAND_LIST(BRB_MRBGEM_LIST)
#ifdef __cplusplus
} // extern "C"
#endif
static void bbsruby_final_mrbgems(mrb_state *mrb)
{
#undef BRBCPP_LIST_TRANSFORM
#define BRBCPP_LIST_TRANSFORM(elem) \
GENERATED_TMP_mrb_ ## elem ## _gem_final(mrb);
BRBCPP_EXPAND_LIST(BRB_MRBGEM_LIST)
}
static void bbsruby_init_mrbgems(mrb_state *mrb)
{
#undef BRBCPP_LIST_TRANSFORM
#define BRBCPP_LIST_TRANSFORM(elem) \
GENERATED_TMP_mrb_ ## elem ## _gem_init(mrb);
BRBCPP_EXPAND_LIST(BRB_MRBGEM_LIST)
mrb_state_atexit(mrb, bbsruby_final_mrbgems);
}
#endif
static void bbsruby_init_bbs_module RB_PV((void))
{
#ifdef BBSRUBY_USE_MRUBY
// Remove former definition
mrb_const_remove(mrb, mrb_obj_value(mrb->object_class), mrb_intern_cstr(mrb, "BBS"));
#else
// Remove former definition
rb_define_global_const("BBS", Qundef);
#endif
// Prepare BBS wrapper module
rb_class_t brb_mBBS = RB_C(rb_define_module)("BBS");
RB_C(rb_define_module_function)(brb_mBBS, "outs", (rb_func_t) brb_outs, RB_ARGS_REST());
RB_C(rb_define_module_function)(brb_mBBS, "title", (rb_func_t) brb_title, RB_ARGS_REQ(1));
RB_C(rb_define_module_function)(brb_mBBS, "print", (rb_func_t) brb_print, RB_ARGS_REST());
RB_C(rb_define_module_function)(brb_mBBS, "getyx", (rb_func_t) brb_getyx, RB_ARGS_REQ(0));
RB_C(rb_define_module_function)(brb_mBBS, "getmaxyx", (rb_func_t) brb_getmaxyx, RB_ARGS_REQ(0));
RB_C(rb_define_module_function)(brb_mBBS, "move", (rb_func_t) brb_move, RB_ARGS_REQ(2));
RB_C(rb_define_module_function)(brb_mBBS, "moverel", (rb_func_t) brb_moverel, RB_ARGS_REQ(2));
RB_C(rb_define_module_function)(brb_mBBS, "clear", (rb_func_t) brb_clear, RB_ARGS_REQ(0));
RB_C(rb_define_module_function)(brb_mBBS, "clrtoeol", (rb_func_t) brb_clrtoeol, RB_ARGS_REQ(0));
RB_C(rb_define_module_function)(brb_mBBS, "clrtobot", (rb_func_t) brb_clrtobot, RB_ARGS_REQ(0));
RB_C(rb_define_module_function)(brb_mBBS, "refresh", (rb_func_t) brb_refresh, RB_ARGS_REQ(0));
RB_C(rb_define_module_function)(brb_mBBS, "vmsg", (rb_func_t) brb_vmsg, RB_ARGS_REQ(1));