-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathruby.c
More file actions
1430 lines (1274 loc) · 30.2 KB
/
Copy pathruby.c
File metadata and controls
1430 lines (1274 loc) · 30.2 KB
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
/*
Copyright (C) 2018 Mark Alexander
This file is part of MicroEMACS, a small text editor.
MicroEMACS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "def.h"
#include <unistd.h>
#include <sys/ioctl.h>
static char rubyinit_error[100]; /* Buffer containing error message from rubyinit */
#ifndef RUBY_RPC /* Only include the common functions at the end of this file. */
#include <dlfcn.h>
#define RUBY_DONT_SUBST
#include <ruby.h>
static void *ruby_handle; /* Handle to libruby.so, NULL if rubyinit failed */
static int rubyinit_called; /* TRUE if rubyinit has been called */
void *ruby_fptrs[100];
/*
* Structure for passing arguments to rb_funcall dispatcher.
*/
struct callargs
{
ID id; /* ID of command function */
VALUE parm; /* numeric parameter to command */
};
/*
* Do not change the first and last lines in the following table. They
* are used by the makeapi.rb script to generate the jump table
* for the Ruby APIs.
*/
const char *fnames[] = /* Do not change this line */
{
"ruby_init",
"ruby_init_stack",
"ruby_options",
"ruby_executable_node",
"ruby_setup",
"ruby_cleanup",
"rb_eval_string_protect",
"rb_define_global_function",
"rb_string_value_cstr",
"rb_errinfo",
"rb_set_errinfo",
"rb_intern2",
"rb_funcall",
"rb_funcallv",
"rb_str_new",
"rb_str_new_static",
"rb_str_new_cstr",
"rb_str_plus",
"rb_str_cat",
"rb_define_virtual_variable",
"rb_string_value_ptr",
"rb_load_protect",
"ruby_init_loadpath",
"rb_gv_get",
"rb_intern",
"rb_mKernel",
"rb_id2sym",
"rb_cObject",
"rb_protect",
"rb_load",
"rb_ruby_verbose_ptr",
"rb_timespec_now",
"rb_time_timespec_new",
"rb_sym_to_s",
#if __i386__
"rb_num2long",
"rb_int2big",
#else
"rb_fix2int",
"rb_num2int",
#endif
}; /* Do not change this line */
/*
***
*** C functions callable from Ruby.
***
*/
/*
* Look up an underscore-ized symbol in the symbol table.
* Underscores in the symbol name are converted to dashes
* before the lookup is performed.
*/
static SYMBOL *
rubylookup (const char *s)
{
static SYMBOL *sp;
char *p;
char c;
char *s1 = strdup (s);
/* Make a copy of s with '_' replaced with '-'.
*/
if (s1 == NULL)
{
eprintf ("Out of memory in rubylookup!");
return NULL;
}
for (p = s1; (c = *p) != '\0'; ++p)
{
if (c == '_')
*p = '-';
}
/* If the previous symbol we found is the same, just
* return that, instead of searching for it again.
*/
if (sp == NULL || strcmp (s1, sp->s_name) != 0)
sp = symlookup (s1);
free (s1);
return sp;
}
/*
* Check if the string c is is actual MicroEMACS command.
*/
static VALUE
my_iscmd (VALUE self, VALUE c)
{
VALUE ret;
const char *name;
ret = Qfalse;
if (RB_TYPE_P(c, T_STRING))
{
name = StringValueCStr(c);
if (rubylookup (name) != NULL)
ret = Qtrue;
}
return ret;
}
/*
* Helper function for Ruby bind (equivalent to
* built-in command bind-to-key). The first
* parameter is a string containing the command
* name, and the second parameter is the numeric
* keycode.
*/
static VALUE
my_cbind (VALUE self, VALUE c, VALUE k, VALUE mode)
{
VALUE ret;
const char *name = NULL;
SYMBOL *sp = NULL;
int key = KRANDOM;
int cret = TRUE;
if (RB_TYPE_P (c, T_STRING))
{
name = StringValueCStr (c);
if ((sp = rubylookup (name)) == NULL)
{
eprintf ("%s: no such command", name);
cret = FALSE;
}
}
else
{
eprintf ("command name must be a string");
cret = FALSE;
}
if (FIXNUM_P (k))
key = FIX2INT (k);
else
{
eprintf ("key must be a fixnum");
cret = FALSE;
}
if (cret == TRUE)
{
if (mode == Qtrue)
setmodebinding (key, sp);
else
setbinding (key, sp);
}
ret = INT2NUM (cret);
return ret;
}
/*
* Run a MicroEMACS command. Return its result code
* as a FIXNUM (0 = FALSE, 1 = TRUE, 2 = ABORT).
*
* Parameters:
* c = name of command
* f = argument flag (0 if no argument present, 1 if argument present)
* n = argument (integer)
* k = keystroke (only looked at by selfinsert)
* s = array of strings to be added to a queue of replies to eread
*/
static VALUE
my_cmd (VALUE self, VALUE c, VALUE f, VALUE n, VALUE k, VALUE s)
{
VALUE ret;
const char *name = "";
int flag = 0;
int narg = 1;
int key;
int cret = TRUE;
SYMBOL *sp;
if (RB_TYPE_P(c, T_STRING))
name = StringValueCStr (c);
else
{
eprintf ("command name must be a string");
cret = FALSE;
}
if (FIXNUM_P(f))
flag = FIX2INT (f);
else
{
eprintf ("flag must be a fixnum");
cret = FALSE;
}
if (FIXNUM_P(n))
narg = FIX2INT (n);
else
{
eprintf ("numeric argument must be a fixnum");
cret = FALSE;
}
if (FIXNUM_P(k))
key = FIX2INT (k);
else
{
eprintf ("key must be a fixnum");
cret = FALSE;
}
if (!RB_TYPE_P(s, T_ARRAY))
{
eprintf ("strings must be an array");
cret = FALSE;
}
else
{
/* Collect the strings in the array and add
* them to the reply queue for eread.
*/
int ci;
VALUE len = rb_funcall (s, rb_intern("length"), 0);
int clen = FIX2INT (len);
for (ci = 0; ci < clen; ci++)
{
VALUE i = LONG2FIX (ci);
VALUE str = rb_funcall (s, rb_intern ("slice"), 1, i);
char *cstr = StringValueCStr (str);
replyq_put (cstr);
}
}
/* If all parameters look OK, run the command.
*/
if (cret == TRUE)
{
cret = FALSE;
/* fprintf (stdout, "my_cmd: name = %s\n", name); */
if ((sp = rubylookup (name)) != NULL)
{
if (sp->s_macro)
{
if (kbdmip != NULL || kbdmop != NULL)
{
eprintf ("Not now");
cret = FALSE;
}
else
cret = domacro (sp->s_macro, 1);
}
else
{
startsaveundo ();
cret = sp->s_funcp (flag, narg, key);
endsaveundo ();
}
}
else
{
eprintf ("Unknown command %s", name);
cret = FALSE;
}
}
ret = INT2NUM (cret);
return ret;
}
/*
* Get the current line's text.
*/
static VALUE
get_line (ID id, VALUE *var)
{
VALUE ret;
VALUE utf8;
char *str;
/* Make a copy of the line with an extra space for a newline.
*/
str = ruby_getline();
if (str == NULL)
return Qnil;
/* Make the copy of the line into a Ruby string, and free the copy.
*/
ret = rb_str_new (str, strlen (str));
utf8 = rb_str_new_cstr("utf-8");
rb_funcall (ret, rb_intern ("force_encoding"), 1, utf8);
free (str);
return ret;
}
/*
* Replace the current line with the new string;
*/
static void
set_line (VALUE val, ID id, VALUE *var)
{
char *str;
str = StringValueCStr (val);
if (str == NULL)
{
eprintf ("Out of memory in set_line!");
return;
}
ruby_setline (str);
}
/*
* Get the character at the current position. Return it
* as a one-character UTF-8 string.
*/
static VALUE
get_char (VALUE self, VALUE *var)
{
VALUE ret;
VALUE utf8;
char *str;
/* Make a copy of the character at dot as a string.
*/
str = ruby_getchar();
if (str == NULL)
return Qnil;
/* Make the copy of the char into a Ruby string, and free the copy.
*/
ret = rb_str_new (str, strlen (str));
utf8 = rb_str_new_cstr("utf-8");
rb_funcall (ret, rb_intern ("force_encoding"), 1, utf8);
free (str);
return ret;
}
/*
* Replace character at the current position with the new string.
*/
static void
set_char (VALUE val, ID id, VALUE *var)
{
const char *str;
str = StringValueCStr (val);
if (str == NULL)
{
eprintf ("Out of memory in set_line!");
return;
}
ruby_setchar (str);
}
/*
* Get the current buffer's filename.
*/
static VALUE
get_filename (VALUE self, VALUE *var)
{
VALUE ret;
ret = rb_str_new_cstr (curbp->b_fname);
return ret;
}
/*
* Set the current buffer's filename.
*/
static void
set_filename (VALUE val, ID id, VALUE *var)
{
const char *str = StringValueCStr (val);
replyq_put (str);
filename (FALSE, 0, KRANDOM);
}
/*
* Get the current line number, 1 based so that it can
* be used with goto-line, and for compatibility with
* display-position.
*/
static VALUE
get_lineno (ID id, VALUE *var)
{
VALUE ret;
int l;
l = lineno (curwp->w_dot.p) + 1;
ret = INT2NUM (l);
return ret;
}
/*
* Set the current line number, 1 based so that it can
* be used with goto-line.
*/
static void
set_lineno (VALUE val, ID id, VALUE *var)
{
int new_lineno = NUM2INT (val);
gotoline (TRUE, new_lineno, KRANDOM);
}
/*
* Get the current buffer's flags.
*/
static VALUE
get_bflag (VALUE self, VALUE *var)
{
VALUE ret;
ret = INT2NUM (curbp->b_flag);
return ret;
}
/*
* Set the current buffer's flags.
*/
static void
set_bflag (VALUE val, ID id, VALUE *var)
{
int flag = NUM2INT (val);
curbp->b_flag = flag;
curwp->w_flag |= WFMODE;
}
/*
* Get the current local time.
*/
static VALUE
my_now (VALUE self)
{
struct timespec ts;
VALUE now;
rb_timespec_now (&ts);
now = rb_time_timespec_new (&ts, INT_MAX);
return now;
}
/*
* Convert a symbol to a string
*/
static VALUE
my_sym2str (VALUE self, VALUE sym)
{
VALUE ret;
ret = rb_sym_to_s (sym);
return ret;
}
/*
* Update the screen.
*/
static VALUE
my_update (VALUE self)
{
VALUE ret;
update ();
ret = INT2NUM (1);
return ret;
}
/*
* Get the current buffer's name (not filename).
*/
static VALUE
get_bname (VALUE self, VALUE *var)
{
VALUE ret;
ret = rb_str_new_cstr (curbp->b_bname);
return ret;
}
/*
* Set the current buffer's name (not filename).
*/
static void
set_bname (VALUE val, ID id, VALUE *var)
{
const char *str = StringValueCStr (val);
if (strlen (str) >= NBUFN)
{
eprintf ("Buffer name too long!");
return;
}
strcpy (curbp->b_bname, str);
curwp->w_flag |= WFMODE;
}
/*
* Get the current line length in UTF-8 characters, not bytes.
*/
static VALUE
my_linelen (VALUE self)
{
VALUE ret;
ret = INT2NUM (wllength (curwp->w_dot.p));
return ret;
}
/*
* Get the current offset into the current line.
*/
static VALUE
get_offset (ID id, VALUE *var)
{
VALUE ret;
ret = INT2NUM (curwp->w_dot.o);
return ret;
}
/*
* Set the offset into the current line.
*/
static void
set_offset (VALUE val, ID id, VALUE *var)
{
int offset = NUM2INT (val);
if (offset > wllength (curwp->w_dot.p))
eprintf ("Offset too large");
else
curwp->w_dot.o = offset;
}
/*
* Get the current tab size.
*/
static VALUE
get_tabsize (VALUE self, VALUE *var)
{
VALUE ret;
ret = INT2NUM (tabsize);
return ret;
}
/*
* Set the current tab size.
*/
static void
set_tabsize (VALUE val, ID id, VALUE *var)
{
settabsize (TRUE, NUM2INT (val), KRANDOM);
}
/*
* Get the current fill column.
*/
static VALUE
get_fillcol (VALUE self, VALUE *var)
{
VALUE ret;
ret = INT2NUM (fillcol);
return ret;
}
/*
* Set the current fill column.
*/
static void
set_fillcol (VALUE val, ID id, VALUE *var)
{
setfillcol (TRUE, NUM2INT (val), KRANDOM);
}
/*
* Insert a string at the current location.
*/
static VALUE
my_insert (VALUE self, VALUE s)
{
VALUE ret;
int cret;
if (!RB_TYPE_P(s, T_STRING))
{
eprintf ("insert parameter must be a string");
cret = FALSE;
}
else
{
char *cs = StringValuePtr (s);
startsaveundo ();
int len = RSTRING_LEN (s);
cret = insertwithnl (cs, len);
endsaveundo ();
}
ret = INT2NUM (cret);
return ret;
}
/*
* Show the popup buffer with the specified string as the contents.
*/
static VALUE
my_popup (VALUE self, VALUE s)
{
VALUE ret;
int cret;
if (!RB_TYPE_P(s, T_STRING))
{
eprintf ("popup parameter must be a string");
cret = FALSE;
}
else
{
const char *str = StringValueCStr(s);
cret = ruby_popup (str);
}
ret = INT2NUM (cret);
return ret;
}
/*
* Prompt the user and read back a reply, which is returned
* as a string. If the user aborts the reply with Control-G,
* return nil.
*/
static VALUE
my_reply (VALUE self, VALUE s)
{
VALUE ret;
int cret;
char buf[NCOL];
if (!RB_TYPE_P(s, T_STRING))
{
eprintf ("reply parameter must be a string");
ret = Qnil;
}
else
{
char *prompt = StringValueCStr (s);
cret = ereply ("%s", buf, sizeof (buf), prompt);
if (cret == ABORT)
ret = Qnil;
else
ret = rb_str_new_cstr (buf);
}
return ret;
}
/*
* Get an input character from the keyboard or profile.
*/
static VALUE
my_getkey (VALUE self)
{
VALUE ret;
ret = INT2NUM (getkey ());
return ret;
}
/*
* Initialize a mode for the current buffer. The parameter
* is the name of the mode. The key binding table for the mode
* is cleared, and can be filled in with subsequent calls to bind.
* Return 1 if success, or zero otherwise.
*/
static VALUE
my_setmode (VALUE self, VALUE s)
{
VALUE ret;
int cret;
if (!RB_TYPE_P(s, T_STRING))
{
eprintf ("setmode parameter must be a string");
cret = FALSE;
}
else
{
char *cs = StringValuePtr (s);
createmode (cs);
cret = TRUE;
}
ret = INT2NUM (cret);
return ret;
}
/*
* Redirect stderr to a pipe that we can read read later
* at our convenience, without blocking. Use read_stderr
* to read the pipe. Return TRUE if successful, FALSE otherwise.
*/
static int pipefd[2];
static int ignore_stderr;
static int
capture_stderr (void)
{
int status;
if (pipe (pipefd) != 0)
return FALSE;
if ((status = dup2 (pipefd[1], 2)) == -1)
return FALSE;
return TRUE;
}
/*
* If any data is available in the stderr pipe, return it as
* a string. If there's no data, return the empty string.
*/
static const char *
read_stderr (void)
{
static char *buf = NULL;
int nbytes, nread;
if (ioctl (pipefd[0], FIONREAD, &nbytes) != 0)
return "";
if (nbytes == 0)
return "";
buf = (char *) realloc (buf, nbytes + 1);
if (buf == NULL)
return "";
nread = read (pipefd[0], buf, nbytes);
buf[nread] = '\0';
if (ignore_stderr)
return "";
else
return buf;
}
/*
* Check if the last call to Ruby returned an exception.
* If so, display the exception string on the echo line
* and return FALSE. Otherwise return TRUE.
*/
static int
check_exception (int state)
{
const char *err;
if (state)
{
/* Get the exception string and display it in the popup buffer.
*/
VALUE exception = rb_errinfo ();
if (RTEST(exception))
{
VALUE exc;
VALUE msg;
VALUE bt, btarray;
char *msgc;
/* Get the exception string and append a newline.
*/
exc = rb_funcall (exception, rb_intern("to_s"), 0);
rb_str_cat2 (exc, "\n");
/* Join the backtrace string array into a single string.
*/
btarray = rb_funcall (exception, rb_intern("backtrace"), 0);
bt = rb_funcall (btarray, rb_intern("join"), 1, rb_str_new_cstr ("\n"));
/* Combine the exception string and the backtrace string.
*/
msg = rb_str_plus (exc, bt);
/* Clear the exception.
*/
rb_set_errinfo (Qnil);
/* Convert the combined string into a C string and display it
* in the popup window.
*/
msgc = StringValueCStr (msg);
return ruby_popup (msgc);
}
rb_set_errinfo (Qnil); /* clear last exception */
return FALSE;
}
/* If anything was written to stderr, display it in the popup buffer.
*/
err = read_stderr ();
if (strlen (err) > 0)
return ruby_popup (err);
return TRUE;
}
/*
* Run the Ruby code in the passed-in string. Return TRUE
* if successful, or FALSE otherwise.
*/
int
runruby (const char * line)
{
int state;
rb_eval_string_protect(line, &state);
return check_exception (state);
}
/*
* Helper function for loadscript. This is a simple wrapper
* for rb_load, required because rb_protect requires a function
* that takes just one parameter, but rb_load takes two parameters.
*/
static VALUE
loadhelper (VALUE arg)
{
rb_load (arg, 0);
return Qnil;
}
/*
* Load the specified Ruby script. Return TRUE if the file was loaded
* successfully. If unsuccessful, display the exception information
* on the echo line and return FALSE.
*
* Unfortunately, we can't use rb_load_protect here. Despite its name,
* it doesn't seem to protect against exceptions; it aborts
* with a segfault. So instead, we call a wrapper for rb_load
* from rb_protect, which allows us to catch any exceptions
* in the loaded file.
*/
int
rubyloadscript (const char *path)
{
VALUE script;
int state;
script = rb_str_new_cstr (path);
rb_protect(loadhelper, script, &state);
return check_exception (state);
}
/*
* Load the Ruby library, initialize the pointers to the APIs,
* define some C helper functions, and load the Ruby helper code
* in pe.rb. Return TRUE on success, or FALSE on failure.
* If quiet is TRUE, don't display an error message if the
* Ruby shared library can't be loaded.
*/
int
rubyinit (int quiet)
{
int i, status, namecount, fptrcount;
VALUE loadpath;
VALUE dir;
static const char *libruby = STRINGIFY(LIBRUBY);
static const char *global_pe_rb = STRINGIFY(PREFIX) "/share/pe/pe.rb";
const char *dummy_args[] = {"ruby", "-e0"};
void *iseq;
/* If we've been called before, return the status from that call.
*/
if (rubyinit_called)
return ruby_handle != NULL;
rubyinit_called = TRUE;
/* Make sure the API address table is big enough.
*/
namecount = sizeof (fnames) / sizeof (fnames[0]);
fptrcount = sizeof (ruby_fptrs) / sizeof (ruby_fptrs[0]);
if (namecount >= fptrcount)
{
return rubyinit_set_error ("ruby_fptrs has %d entries but needs %d", fptrcount, namecount);
}
/* Open the Ruby runtime library.
*/
ruby_handle = dlopen(libruby, RTLD_LAZY);
if (ruby_handle == NULL)
{
return rubyinit_set_error ("Unable to load %s", libruby);
}
/* Query the addresses of our required Ruby API functions.
*/
for (i = 0; i < namecount; i++)
{
ruby_fptrs[i] = dlsym (ruby_handle, fnames[i]);
if (ruby_fptrs[i] == NULL)
{
ruby_handle = NULL;
return rubyinit_set_error ("Unable to get address of ruby function %s", fnames[i]);
}
else
{
/* printf("Address of %s is %p\n", fnames[i], ruby_fptrs[i]); */
}
}
/* Capture standard error to a pipe, but discard it until
* we're done with the initialization.
*/
capture_stderr ();
ignore_stderr = TRUE;
/* Do the basic initialization. The very first thing is to set up
* the Ruby stack, which depends on main() having stored a pointer
* to its local variable ruby_stack in ruby_stack_ptr.
*/
if (sizeof (*ruby_stack_ptr) != sizeof (VALUE))
{
ruby_handle = NULL;
return rubyinit_set_error ("Size of ruby_stack was %d, should be %d",
sizeof(*ruby_stack_ptr), sizeof(VALUE));
}
ruby_init_stack (ruby_stack_ptr);
ruby_init();
/* Initialize the load path for gems. This causes the later call to
* ruby_options to write this message to stderr:
* ruby: warning: already initialized constant TMP_RUBY_PREFIX
* To keep this message from messing up the screen, we use
* capture_stderr and read_stderr to read and discard the message.
*/
ruby_init_loadpath();
/* Calling ruby_options forces the builtin methods to be loaded. See:
* https://stackoverflow.com/questions/79816264/
*/
iseq = ruby_options(2, (char **)dummy_args);
if (!ruby_executable_node(iseq, &status))
{
ruby_handle = NULL;
return rubyinit_set_error ("ruby_options failed, status %d", status);
}
/* Define global functions that can be called from Ruby.
*/
rb_define_global_function("e_cmd", my_cmd, 5);
rb_define_global_function("e_iscmd", my_iscmd, 1);
rb_define_global_function("e_linelen", my_linelen, 0);
rb_define_global_function("e_insert", my_insert, 1);
rb_define_global_function("e_popup", my_popup, 1);
rb_define_global_function("e_cbind", my_cbind, 3);
rb_define_global_function("e_reply", my_reply, 1);
rb_define_global_function("e_cgetkey", my_getkey, 0);
rb_define_global_function("e_setmode", my_setmode, 1);
rb_define_global_function("e_timenow", my_now, 0);
rb_define_global_function("e_sym2str", my_sym2str, 1);
rb_define_global_function("e_update", my_update, 0);