-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby.c
3154 lines (2829 loc) · 91.2 KB
/
ruby.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
/**********************************************************************
ruby.c -
$Author$
created at: Tue Aug 10 12:47:31 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby/internal/config.h"
#include <ctype.h>
#include <stdio.h>
#include <sys/types.h>
#ifdef __CYGWIN__
# include <windows.h>
# include <sys/cygwin.h>
#endif
#if defined(LOAD_RELATIVE) && defined(HAVE_DLADDR)
# include <dlfcn.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#if defined(HAVE_FCNTL_H)
# include <fcntl.h>
#elif defined(HAVE_SYS_FCNTL_H)
# include <sys/fcntl.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#include "dln.h"
#include "eval_intern.h"
#include "internal.h"
#include "internal/cmdlineopt.h"
#include "internal/cont.h"
#include "internal/error.h"
#include "internal/file.h"
#include "internal/inits.h"
#include "internal/io.h"
#include "internal/load.h"
#include "internal/loadpath.h"
#include "internal/missing.h"
#include "internal/object.h"
#include "internal/thread.h"
#include "internal/ruby_parser.h"
#include "internal/variable.h"
#include "ruby/encoding.h"
#include "ruby/thread.h"
#include "ruby/util.h"
#include "ruby/version.h"
#include "ruby/internal/error.h"
#define singlebit_only_p(x) !((x) & ((x)-1))
STATIC_ASSERT(Qnil_1bit_from_Qfalse, singlebit_only_p(Qnil^Qfalse));
STATIC_ASSERT(Qundef_1bit_from_Qnil, singlebit_only_p(Qundef^Qnil));
#ifndef MAXPATHLEN
# define MAXPATHLEN 1024
#endif
#ifndef O_ACCMODE
# define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
#endif
void Init_ruby_description(ruby_cmdline_options_t *opt);
#ifndef HAVE_STDLIB_H
char *getenv();
#endif
#ifndef DISABLE_RUBYGEMS
# define DISABLE_RUBYGEMS 0
#endif
#if DISABLE_RUBYGEMS
#define DEFAULT_RUBYGEMS_ENABLED "disabled"
#else
#define DEFAULT_RUBYGEMS_ENABLED "enabled"
#endif
void rb_warning_category_update(unsigned int mask, unsigned int bits);
#define COMMA ,
#define FEATURE_BIT(bit) (1U << feature_##bit)
#define EACH_FEATURES(X, SEP) \
X(gems) \
SEP \
X(error_highlight) \
SEP \
X(did_you_mean) \
SEP \
X(syntax_suggest) \
SEP \
X(rubyopt) \
SEP \
X(frozen_string_literal) \
SEP \
X(rjit) \
SEP \
X(yjit) \
/* END OF FEATURES */
#define EACH_DEBUG_FEATURES(X, SEP) \
X(frozen_string_literal) \
/* END OF DEBUG FEATURES */
#define AMBIGUOUS_FEATURE_NAMES 0 /* no ambiguous feature names now */
#define DEFINE_FEATURE(bit) feature_##bit
#define DEFINE_DEBUG_FEATURE(bit) feature_debug_##bit
enum feature_flag_bits {
EACH_FEATURES(DEFINE_FEATURE, COMMA),
DEFINE_FEATURE(frozen_string_literal_set),
feature_debug_flag_first,
#if defined(RJIT_FORCE_ENABLE) || !USE_YJIT
DEFINE_FEATURE(jit) = feature_rjit,
#else
DEFINE_FEATURE(jit) = feature_yjit,
#endif
feature_jit_mask = FEATURE_BIT(rjit) | FEATURE_BIT(yjit),
feature_debug_flag_begin = feature_debug_flag_first - 1,
EACH_DEBUG_FEATURES(DEFINE_DEBUG_FEATURE, COMMA),
feature_flag_count
};
#define MULTI_BITS_P(bits) ((bits) & ((bits) - 1))
#define DEBUG_BIT(bit) (1U << feature_debug_##bit)
#define DUMP_BIT(bit) (1U << dump_##bit)
#define DEFINE_DUMP(bit) dump_##bit
#define EACH_DUMPS(X, SEP) \
X(version) \
SEP \
X(copyright) \
SEP \
X(usage) \
SEP \
X(help) \
SEP \
X(yydebug) \
SEP \
X(syntax) \
SEP \
X(parsetree) \
SEP \
X(insns) \
/* END OF DUMPS */
enum dump_flag_bits {
dump_version_v,
dump_opt_error_tolerant,
dump_opt_comment,
dump_opt_optimize,
EACH_DUMPS(DEFINE_DUMP, COMMA),
dump_exit_bits = (DUMP_BIT(yydebug) | DUMP_BIT(syntax) |
DUMP_BIT(parsetree) | DUMP_BIT(insns)),
dump_optional_bits = (DUMP_BIT(opt_error_tolerant) |
DUMP_BIT(opt_comment) |
DUMP_BIT(opt_optimize))
};
static inline void
rb_feature_set_to(ruby_features_t *feat, unsigned int bit_mask, unsigned int bit_set)
{
feat->mask |= bit_mask;
feat->set = (feat->set & ~bit_mask) | bit_set;
}
#define FEATURE_SET_TO(feat, bit_mask, bit_set) \
rb_feature_set_to(&(feat), bit_mask, bit_set)
#define FEATURE_SET(feat, bits) FEATURE_SET_TO(feat, bits, bits)
#define FEATURE_SET_RESTORE(feat, save) FEATURE_SET_TO(feat, (save).mask, (save).set & (save).mask)
#define FEATURE_SET_P(feat, bits) ((feat).set & FEATURE_BIT(bits))
#define FEATURE_USED_P(feat, bits) ((feat).mask & FEATURE_BIT(bits))
#define FEATURE_SET_BITS(feat) ((feat).set & (feat).mask)
static void init_ids(ruby_cmdline_options_t *);
#define src_encoding_index GET_VM()->src_encoding_index
enum {
COMPILATION_FEATURES = (
0
| FEATURE_BIT(frozen_string_literal)
| FEATURE_BIT(frozen_string_literal_set)
| FEATURE_BIT(debug_frozen_string_literal)
),
DEFAULT_FEATURES = (
(FEATURE_BIT(debug_flag_first)-1)
#if DISABLE_RUBYGEMS
& ~FEATURE_BIT(gems)
#endif
& ~FEATURE_BIT(frozen_string_literal)
& ~FEATURE_BIT(frozen_string_literal_set)
& ~feature_jit_mask
)
};
#define BACKTRACE_LENGTH_LIMIT_VALID_P(n) ((n) >= -1)
#define OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt) \
BACKTRACE_LENGTH_LIMIT_VALID_P((opt)->backtrace_length_limit)
static ruby_cmdline_options_t *
cmdline_options_init(ruby_cmdline_options_t *opt)
{
MEMZERO(opt, *opt, 1);
init_ids(opt);
opt->src.enc.index = src_encoding_index;
opt->ext.enc.index = -1;
opt->intern.enc.index = -1;
opt->features.set = DEFAULT_FEATURES;
#ifdef RJIT_FORCE_ENABLE /* to use with: ./configure cppflags="-DRJIT_FORCE_ENABLE" */
opt->features.set |= FEATURE_BIT(rjit);
#elif defined(YJIT_FORCE_ENABLE)
opt->features.set |= FEATURE_BIT(yjit);
#endif
opt->dump |= DUMP_BIT(opt_optimize);
opt->backtrace_length_limit = LONG_MIN;
return opt;
}
static VALUE load_file(VALUE parser, VALUE fname, VALUE f, int script,
ruby_cmdline_options_t *opt);
static VALUE open_load_file(VALUE fname_v, int *xflag);
static void forbid_setid(const char *, const ruby_cmdline_options_t *);
#define forbid_setid(s) forbid_setid((s), opt)
static struct {
int argc;
char **argv;
} origarg;
static const char esc_standout[] = "\n\033[1;7m";
static const char esc_bold[] = "\033[1m";
static const char esc_reset[] = "\033[0m";
static const char esc_none[] = "";
#define USAGE_INDENT " " /* macro for concatenation */
static void
show_usage_part(const char *str, const unsigned int namelen,
const char *str2, const unsigned int secondlen,
const char *desc,
int help, int highlight, unsigned int w, int columns)
{
static const int indent_width = (int)rb_strlen_lit(USAGE_INDENT);
const char *sb = highlight ? esc_bold : esc_none;
const char *se = highlight ? esc_reset : esc_none;
unsigned int desclen = (unsigned int)strcspn(desc, "\n");
if (!help && desclen > 0 && strchr(".;:", desc[desclen-1])) --desclen;
if (help && (namelen + 1 > w) && /* a padding space */
(int)(namelen + secondlen + indent_width) >= columns) {
printf(USAGE_INDENT "%s" "%.*s" "%s\n", sb, namelen, str, se);
if (secondlen > 0) {
const int second_end = secondlen;
int n = 0;
if (str2[n] == ',') n++;
if (str2[n] == ' ') n++;
printf(USAGE_INDENT "%s" "%.*s" "%s\n", sb, second_end-n, str2+n, se);
}
printf("%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
}
else {
const int wrap = help && namelen + secondlen >= w;
printf(USAGE_INDENT "%s%.*s%-*.*s%s%-*s%.*s\n", sb, namelen, str,
(wrap ? 0 : w - namelen),
(help ? secondlen : 0), str2, se,
(wrap ? (int)(w + rb_strlen_lit("\n" USAGE_INDENT)) : 0),
(wrap ? "\n" USAGE_INDENT : ""),
desclen, desc);
}
if (help) {
while (desc[desclen]) {
desc += desclen + rb_strlen_lit("\n");
desclen = (unsigned int)strcspn(desc, "\n");
printf("%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
}
}
}
static void
show_usage_line(const struct ruby_opt_message *m,
int help, int highlight, unsigned int w, int columns)
{
const char *str = m->str;
const unsigned int namelen = m->namelen, secondlen = m->secondlen;
const char *desc = str + namelen + secondlen;
show_usage_part(str, namelen - 1, str + namelen, secondlen - 1, desc,
help, highlight, w, columns);
}
void
ruby_show_usage_line(const char *name, const char *secondary, const char *description,
int help, int highlight, unsigned int width, int columns)
{
unsigned int namelen = (unsigned int)strlen(name);
unsigned int secondlen = (secondary ? (unsigned int)strlen(secondary) : 0);
show_usage_part(name, namelen, secondary, secondlen,
description, help, highlight, width, columns);
}
static void
usage(const char *name, int help, int highlight, int columns)
{
#define M(shortopt, longopt, desc) RUBY_OPT_MESSAGE(shortopt, longopt, desc)
#if USE_YJIT
# define PLATFORM_JIT_OPTION "--yjit"
#else
# define PLATFORM_JIT_OPTION "--rjit (experimental)"
#endif
/* This message really ought to be max 23 lines.
* Removed -h because the user already knows that option. Others? */
static const struct ruby_opt_message usage_msg[] = {
M("-0[octal]", "", "Set input record separator ($/):\n"
"-0 for \\0; -00 for paragraph mode; -0777 for slurp mode."),
M("-a", "", "Split each input line ($_) into fields ($F)."),
M("-c", "", "Check syntax (no execution)."),
M("-Cdirpath", "", "Execute program in specified directory."),
M("-d", ", --debug", "Set debugging flag ($DEBUG) to true."),
M("-e 'code'", "", "Execute given Ruby code; multiple -e allowed."),
M("-Eex[:in]", ", --encoding=ex[:in]", "Set default external and internal encodings."),
M("-Fpattern", "", "Set input field separator ($;); used with -a."),
M("-i[extension]", "", "Set ARGF in-place mode;\n"
"create backup files with given extension."),
M("-Idirpath", "", "Add specified directory to load paths ($LOAD_PATH);\n"
"multiple -I allowed."),
M("-l", "", "Set output record separator ($\\) to $/;\n"
"used for line-oriented output."),
M("-n", "", "Run program in gets loop."),
M("-p", "", "Like -n, with printing added."),
M("-rlibrary", "", "Require the given library."),
M("-s", "", "Define global variables using switches following program path."),
M("-S", "", "Search directories found in the PATH environment variable."),
M("-v", "", "Print version; set $VERBOSE to true."),
M("-w", "", "Synonym for -W1."),
M("-W[level=2|:category]", "", "Set warning flag ($-W):\n"
"0 for silent; 1 for moderate; 2 for verbose."),
M("-x[dirpath]", "", "Execute Ruby code starting from a #!ruby line."),
M("--jit", "", "Enable JIT for the platform; same as " PLATFORM_JIT_OPTION "."),
#if USE_YJIT
M("--yjit", "", "Enable in-process JIT compiler."),
#endif
#if USE_RJIT
M("--rjit", "", "Enable pure-Ruby JIT compiler (experimental)."),
#endif
M("-h", "", "Print this help message; use --help for longer message."),
};
STATIC_ASSERT(usage_msg_size, numberof(usage_msg) < 25);
static const struct ruby_opt_message help_msg[] = {
M("--backtrace-limit=num", "", "Set backtrace limit."),
M("--copyright", "", "Print Ruby copyright."),
M("--crash-report=template", "", "Set template for crash report file."),
M("--disable=features", "", "Disable features; see list below."),
M("--dump=items", "", "Dump items; see list below."),
M("--enable=features", "", "Enable features; see list below."),
M("--external-encoding=encoding", "", "Set default external encoding."),
M("--help", "", "Print long help message; use -h for short message."),
M("--internal-encoding=encoding", "", "Set default internal encoding."),
M("--parser=parser", "", "Set Ruby parser: parse.y or prism."),
M("--verbose", "", "Set $VERBOSE to true; ignore input from $stdin."),
M("--version", "", "Print Ruby version."),
M("-y", ", --yydebug", "Print parser log; backward compatibility not guaranteed."),
};
static const struct ruby_opt_message dumps[] = {
M("insns", "", "Instruction sequences."),
M("yydebug", "", "yydebug of yacc parser generator."),
M("parsetree", "", "Abstract syntax tree (AST)."),
M("-optimize", "", "Disable optimization (affects insns)."),
M("+error-tolerant", "", "Error-tolerant parsing (affects yydebug, parsetree)."),
M("+comment", "", "Add comments to AST (affects parsetree)."),
};
static const struct ruby_opt_message features[] = {
M("gems", "", "Rubygems (only for debugging, default: "DEFAULT_RUBYGEMS_ENABLED")."),
M("error_highlight", "", "error_highlight (default: "DEFAULT_RUBYGEMS_ENABLED")."),
M("did_you_mean", "", "did_you_mean (default: "DEFAULT_RUBYGEMS_ENABLED")."),
M("syntax_suggest", "", "syntax_suggest (default: "DEFAULT_RUBYGEMS_ENABLED")."),
M("rubyopt", "", "RUBYOPT environment variable (default: enabled)."),
M("frozen-string-literal", "", "Freeze all string literals (default: disabled)."),
#if USE_YJIT
M("yjit", "", "In-process JIT compiler (default: disabled)."),
#endif
#if USE_RJIT
M("rjit", "", "Pure-Ruby JIT compiler (experimental, default: disabled)."),
#endif
};
static const struct ruby_opt_message warn_categories[] = {
M("deprecated", "", "Deprecated features."),
M("experimental", "", "Experimental features."),
M("performance", "", "Performance issues."),
};
#if USE_RJIT
extern const struct ruby_opt_message rb_rjit_option_messages[];
#endif
int i;
const char *sb = highlight ? esc_standout+1 : esc_none;
const char *se = highlight ? esc_reset : esc_none;
const int num = numberof(usage_msg) - (help ? 1 : 0);
unsigned int w = (columns > 80 ? (columns - 79) / 2 : 0) + 16;
#define SHOW(m) show_usage_line(&(m), help, highlight, w, columns)
printf("%sUsage:%s %s [options] [--] [filepath] [arguments]\n", sb, se, name);
for (i = 0; i < num; ++i)
SHOW(usage_msg[i]);
if (!help) return;
if (highlight) sb = esc_standout;
for (i = 0; i < numberof(help_msg); ++i)
SHOW(help_msg[i]);
printf("%s""Dump List:%s\n", sb, se);
for (i = 0; i < numberof(dumps); ++i)
SHOW(dumps[i]);
printf("%s""Features:%s\n", sb, se);
for (i = 0; i < numberof(features); ++i)
SHOW(features[i]);
printf("%s""Warning categories:%s\n", sb, se);
for (i = 0; i < numberof(warn_categories); ++i)
SHOW(warn_categories[i]);
#if USE_YJIT
printf("%s""YJIT options:%s\n", sb, se);
rb_yjit_show_usage(help, highlight, w, columns);
#endif
#if USE_RJIT
printf("%s""RJIT options (experimental):%s\n", sb, se);
for (i = 0; rb_rjit_option_messages[i].str; ++i)
SHOW(rb_rjit_option_messages[i]);
#endif
}
#define rubylib_path_new rb_str_new
static void
ruby_push_include(const char *path, VALUE (*filter)(VALUE))
{
const char sep = PATH_SEP_CHAR;
const char *p, *s;
VALUE load_path = GET_VM()->load_path;
#ifdef __CYGWIN__
char rubylib[FILENAME_MAX];
VALUE buf = 0;
# define is_path_sep(c) ((c) == sep || (c) == ';')
#else
# define is_path_sep(c) ((c) == sep)
#endif
if (path == 0) return;
p = path;
while (*p) {
long len;
while (is_path_sep(*p))
p++;
if (!*p) break;
for (s = p; *s && !is_path_sep(*s); s = CharNext(s));
len = s - p;
#undef is_path_sep
#ifdef __CYGWIN__
if (*s) {
if (!buf) {
buf = rb_str_new(p, len);
p = RSTRING_PTR(buf);
}
else {
rb_str_resize(buf, len);
p = strncpy(RSTRING_PTR(buf), p, len);
}
}
#ifdef HAVE_CYGWIN_CONV_PATH
#define CONV_TO_POSIX_PATH(p, lib) \
cygwin_conv_path(CCP_WIN_A_TO_POSIX|CCP_RELATIVE, (p), (lib), sizeof(lib))
#else
# error no cygwin_conv_path
#endif
if (CONV_TO_POSIX_PATH(p, rubylib) == 0) {
p = rubylib;
len = strlen(p);
}
#endif
rb_ary_push(load_path, (*filter)(rubylib_path_new(p, len)));
p = s;
}
}
static VALUE
identical_path(VALUE path)
{
return path;
}
static VALUE
locale_path(VALUE path)
{
rb_enc_associate(path, rb_locale_encoding());
return path;
}
void
ruby_incpush(const char *path)
{
ruby_push_include(path, locale_path);
}
static VALUE
expand_include_path(VALUE path)
{
char *p = RSTRING_PTR(path);
if (!p)
return path;
if (*p == '.' && p[1] == '/')
return path;
return rb_file_expand_path(path, Qnil);
}
void
ruby_incpush_expand(const char *path)
{
ruby_push_include(path, expand_include_path);
}
#undef UTF8_PATH
#if defined _WIN32 || defined __CYGWIN__
static HMODULE libruby;
BOOL WINAPI
DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
{
if (reason == DLL_PROCESS_ATTACH)
libruby = dll;
return TRUE;
}
HANDLE
rb_libruby_handle(void)
{
return libruby;
}
static inline void
translit_char_bin(char *p, int from, int to)
{
while (*p) {
if ((unsigned char)*p == from)
*p = to;
p++;
}
}
#endif
#ifdef _WIN32
# define UTF8_PATH 1
#endif
#ifndef UTF8_PATH
# define UTF8_PATH 0
#endif
#if UTF8_PATH
# define IF_UTF8_PATH(t, f) t
#else
# define IF_UTF8_PATH(t, f) f
#endif
#if UTF8_PATH
static VALUE
str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
{
return rb_str_conv_enc_opts(str, from, to,
ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE,
Qnil);
}
#else
# define str_conv_enc(str, from, to) (str)
#endif
void ruby_init_loadpath(void);
#if defined(LOAD_RELATIVE)
static VALUE
runtime_libruby_path(void)
{
#if defined _WIN32 || defined __CYGWIN__
DWORD ret;
DWORD len = 32;
VALUE path;
VALUE wsopath = rb_str_new(0, len*sizeof(WCHAR));
WCHAR *wlibpath;
char *libpath;
while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath),
ret = GetModuleFileNameW(libruby, wlibpath, len),
(ret == len))
{
rb_str_modify_expand(wsopath, len*sizeof(WCHAR));
rb_str_set_len(wsopath, (len += len)*sizeof(WCHAR));
}
if (!ret || ret > len) rb_fatal("failed to get module file name");
#if defined __CYGWIN__
{
const int win_to_posix = CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
size_t newsize = cygwin_conv_path(win_to_posix, wlibpath, 0, 0);
if (!newsize) rb_fatal("failed to convert module path to cygwin");
path = rb_str_new(0, newsize);
libpath = RSTRING_PTR(path);
if (cygwin_conv_path(win_to_posix, wlibpath, libpath, newsize)) {
rb_str_resize(path, 0);
}
}
#else
{
DWORD i;
for (len = ret, i = 0; i < len; ++i) {
if (wlibpath[i] == L'\\') {
wlibpath[i] = L'/';
ret = i+1; /* chop after the last separator */
}
}
}
len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL);
path = rb_utf8_str_new(0, len);
libpath = RSTRING_PTR(path);
WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath, len, NULL, NULL);
#endif
rb_str_resize(wsopath, 0);
return path;
#elif defined(HAVE_DLADDR)
Dl_info dli;
VALUE fname, path;
const void* addr = (void *)(VALUE)expand_include_path;
if (!dladdr((void *)addr, &dli)) {
return rb_str_new(0, 0);
}
#ifdef __linux__
else if (origarg.argc > 0 && origarg.argv && dli.dli_fname == origarg.argv[0]) {
fname = rb_str_new_cstr("/proc/self/exe");
path = rb_readlink(fname, NULL);
}
#endif
else {
fname = rb_str_new_cstr(dli.dli_fname);
path = rb_realpath_internal(Qnil, fname, 1);
}
rb_str_resize(fname, 0);
return path;
#else
# error relative load path is not supported on this platform.
#endif
}
#endif
#define INITIAL_LOAD_PATH_MARK rb_intern_const("@gem_prelude_index")
VALUE ruby_archlibdir_path, ruby_prefix_path;
void
ruby_init_loadpath(void)
{
VALUE load_path, archlibdir = 0;
ID id_initial_load_path_mark;
const char *paths = ruby_initial_load_paths;
#if defined LOAD_RELATIVE
#if !defined ENABLE_MULTIARCH
# define RUBY_ARCH_PATH ""
#elif defined RUBY_ARCH
# define RUBY_ARCH_PATH "/"RUBY_ARCH
#else
# define RUBY_ARCH_PATH "/"RUBY_PLATFORM
#endif
char *libpath;
VALUE sopath;
size_t baselen;
const char *p;
sopath = runtime_libruby_path();
libpath = RSTRING_PTR(sopath);
p = strrchr(libpath, '/');
if (p) {
static const char libdir[] = "/"
#ifdef LIBDIR_BASENAME
LIBDIR_BASENAME
#else
"lib"
#endif
RUBY_ARCH_PATH;
const ptrdiff_t libdir_len = (ptrdiff_t)sizeof(libdir)
- rb_strlen_lit(RUBY_ARCH_PATH) - 1;
static const char bindir[] = "/bin";
const ptrdiff_t bindir_len = (ptrdiff_t)sizeof(bindir) - 1;
const char *p2 = NULL;
#ifdef ENABLE_MULTIARCH
multiarch:
#endif
if (p - libpath >= bindir_len && !STRNCASECMP(p - bindir_len, bindir, bindir_len)) {
p -= bindir_len;
archlibdir = rb_str_subseq(sopath, 0, p - libpath);
rb_str_cat_cstr(archlibdir, libdir);
OBJ_FREEZE(archlibdir);
}
else if (p - libpath >= libdir_len && !strncmp(p - libdir_len, libdir, libdir_len)) {
archlibdir = rb_str_subseq(sopath, 0, (p2 ? p2 : p) - libpath);
OBJ_FREEZE(archlibdir);
p -= libdir_len;
}
#ifdef ENABLE_MULTIARCH
else if (p2) {
p = p2;
}
else {
p2 = p;
p = rb_enc_path_last_separator(libpath, p, rb_ascii8bit_encoding());
if (p) goto multiarch;
p = p2;
}
#endif
baselen = p - libpath;
}
else {
baselen = 0;
}
rb_str_resize(sopath, baselen);
libpath = RSTRING_PTR(sopath);
#define PREFIX_PATH() sopath
#define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen)
#define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len))
#else
const size_t exec_prefix_len = strlen(ruby_exec_prefix);
#define RUBY_RELATIVE(path, len) rubylib_path_new((path), (len))
#define PREFIX_PATH() RUBY_RELATIVE(ruby_exec_prefix, exec_prefix_len)
#endif
rb_gc_register_address(&ruby_prefix_path);
ruby_prefix_path = PREFIX_PATH();
OBJ_FREEZE(ruby_prefix_path);
if (!archlibdir) archlibdir = ruby_prefix_path;
rb_gc_register_address(&ruby_archlibdir_path);
ruby_archlibdir_path = archlibdir;
load_path = GET_VM()->load_path;
ruby_push_include(getenv("RUBYLIB"), identical_path);
id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
while (*paths) {
size_t len = strlen(paths);
VALUE path = RUBY_RELATIVE(paths, len);
rb_ivar_set(path, id_initial_load_path_mark, path);
rb_ary_push(load_path, path);
paths += len + 1;
}
rb_const_set(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX"), ruby_prefix_path);
}
static void
add_modules(VALUE *req_list, const char *mod)
{
VALUE list = *req_list;
VALUE feature;
if (!list) {
*req_list = list = rb_ary_hidden_new(0);
}
feature = rb_str_cat_cstr(rb_str_tmp_new(0), mod);
rb_ary_push(list, feature);
}
static void
require_libraries(VALUE *req_list)
{
VALUE list = *req_list;
VALUE self = rb_vm_top_self();
ID require;
rb_encoding *extenc = rb_default_external_encoding();
CONST_ID(require, "require");
while (list && RARRAY_LEN(list) > 0) {
VALUE feature = rb_ary_shift(list);
rb_enc_associate(feature, extenc);
RBASIC_SET_CLASS_RAW(feature, rb_cString);
OBJ_FREEZE(feature);
rb_funcallv(self, require, 1, &feature);
}
*req_list = 0;
}
static const struct rb_block*
toplevel_context(rb_binding_t *bind)
{
return &bind->block;
}
static int
process_sflag(int sflag)
{
if (sflag > 0) {
long n;
const VALUE *args;
VALUE argv = rb_argv;
n = RARRAY_LEN(argv);
args = RARRAY_CONST_PTR(argv);
while (n > 0) {
VALUE v = *args++;
char *s = StringValuePtr(v);
char *p;
int hyphen = FALSE;
if (s[0] != '-')
break;
n--;
if (s[1] == '-' && s[2] == '\0')
break;
v = Qtrue;
/* check if valid name before replacing - with _ */
for (p = s + 1; *p; p++) {
if (*p == '=') {
*p++ = '\0';
v = rb_str_new2(p);
break;
}
if (*p == '-') {
hyphen = TRUE;
}
else if (*p != '_' && !ISALNUM(*p)) {
VALUE name_error[2];
name_error[0] =
rb_str_new2("invalid name for global variable - ");
if (!(p = strchr(p, '='))) {
rb_str_cat2(name_error[0], s);
}
else {
rb_str_cat(name_error[0], s, p - s);
}
name_error[1] = args[-1];
rb_exc_raise(rb_class_new_instance(2, name_error, rb_eNameError));
}
}
s[0] = '$';
if (hyphen) {
for (p = s + 1; *p; ++p) {
if (*p == '-')
*p = '_';
}
}
rb_gv_set(s, v);
}
n = RARRAY_LEN(argv) - n;
while (n--) {
rb_ary_shift(argv);
}
return -1;
}
return sflag;
}
static long proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt);
static void
moreswitches(const char *s, ruby_cmdline_options_t *opt, int envopt)
{
long argc, i, len;
char **argv, *p;
const char *ap = 0;
VALUE argstr, argary;
void *ptr;
VALUE src_enc_name = opt->src.enc.name;
VALUE ext_enc_name = opt->ext.enc.name;
VALUE int_enc_name = opt->intern.enc.name;
ruby_features_t feat = opt->features;
ruby_features_t warn = opt->warn;
long backtrace_length_limit = opt->backtrace_length_limit;
const char *crash_report = opt->crash_report;
while (ISSPACE(*s)) s++;
if (!*s) return;
opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0;
const int hyphen = *s != '-';
argstr = rb_str_tmp_new((len = strlen(s)) + hyphen);
argary = rb_str_tmp_new(0);
p = RSTRING_PTR(argstr);
if (hyphen) *p = '-';
memcpy(p + hyphen, s, len + 1);
ap = 0;
rb_str_cat(argary, (char *)&ap, sizeof(ap));
while (*p) {
ap = p;
rb_str_cat(argary, (char *)&ap, sizeof(ap));
while (*p && !ISSPACE(*p)) ++p;
if (!*p) break;
*p++ = '\0';
while (ISSPACE(*p)) ++p;
}
argc = RSTRING_LEN(argary) / sizeof(ap);
ap = 0;
rb_str_cat(argary, (char *)&ap, sizeof(ap));
argv = ptr = ALLOC_N(char *, argc);
MEMMOVE(argv, RSTRING_PTR(argary), char *, argc);
while ((i = proc_options(argc, argv, opt, envopt)) > 1 && envopt && (argc -= i) > 0) {
argv += i;
if (**argv != '-') {
*--*argv = '-';
}
if ((*argv)[1]) {
++argc;
--argv;
}
}
if (src_enc_name) {
opt->src.enc.name = src_enc_name;
}
if (ext_enc_name) {
opt->ext.enc.name = ext_enc_name;
}
if (int_enc_name) {
opt->intern.enc.name = int_enc_name;
}
FEATURE_SET_RESTORE(opt->features, feat);
FEATURE_SET_RESTORE(opt->warn, warn);
if (BACKTRACE_LENGTH_LIMIT_VALID_P(backtrace_length_limit)) {
opt->backtrace_length_limit = backtrace_length_limit;
}
if (crash_report) {
opt->crash_report = crash_report;
}
ruby_xfree(ptr);
/* get rid of GC */
rb_str_resize(argary, 0);
rb_str_resize(argstr, 0);
}
static int
name_match_p(const char *name, const char *str, size_t len)
{
if (len == 0) return 0;
while (1) {
while (TOLOWER(*str) == *name) {
if (!--len) return 1;
++name;
++str;
}
if (*str != '-' && *str != '_') return 0;
while (ISALNUM(*name)) name++;
if (*name != '-' && *name != '_') return 0;
if (!*++name) return 1;
++str;
if (--len == 0) return 1;
}
}
#define NAME_MATCH_P(name, str, len) \
((len) < (int)sizeof(name) && name_match_p((name), (str), (len)))
#define UNSET_WHEN(name, bit, str, len) \
if (NAME_MATCH_P((name), (str), (len))) { \
*(unsigned int *)arg &= ~(bit); \
return; \
}
#define SET_WHEN(name, bit, str, len) \
if (NAME_MATCH_P((name), (str), (len))) { \
*(unsigned int *)arg |= (bit); \
return; \
}
#define LITERAL_NAME_ELEMENT(name) #name
static void
feature_option(const char *str, int len, void *arg, const unsigned int enable)
{
static const char list[] = EACH_FEATURES(LITERAL_NAME_ELEMENT, ", ");
ruby_features_t *argp = arg;
unsigned int mask = ~0U;
unsigned int set = 0U;
#if AMBIGUOUS_FEATURE_NAMES
int matched = 0;
# define FEATURE_FOUND ++matched
#else