summaryrefslogtreecommitdiff
path: root/ruby.c
diff options
context:
space:
mode:
Diffstat (limited to 'ruby.c')
-rw-r--r--ruby.c393
1 files changed, 239 insertions, 154 deletions
diff --git a/ruby.c b/ruby.c
index 6e748f249d..9bf28022e8 100644
--- a/ruby.c
+++ b/ruby.c
@@ -6,7 +6,7 @@
$Date$
created at: Tue Aug 10 12:47:31 JST 1993
- Copyright (C) 1993-1996 Yukihiro Matsumoto
+ Copyright (C) 1993-1998 Yukihiro Matsumoto
************************************************/
@@ -14,102 +14,117 @@
#include <windows.h>
#endif
#include "ruby.h"
-#include "re.h"
#include "dln.h"
+#include "node.h"
#include <stdio.h>
-#include <ctype.h>
#include <sys/types.h>
#include <fcntl.h>
+#include <ctype.h>
+
+#ifdef __hpux
+#include <sys/pstat.h>
+#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
+
+#ifdef USE_CWGUSI
+#include "macruby_missing.h"
+#endif
+
#ifndef HAVE_STRING_H
-char *strchr();
-char *strrchr();
-char *strstr();
+char *strchr _((char*,char));
+char *strrchr _((char*,char));
+char *strstr _((char*,char*));
#endif
+#include "util.h"
+
+#ifndef HAVE_STDLIB_H
char *getenv();
+#endif
static int version, copyright;
-VALUE debug = FALSE;
-VALUE verbose = FALSE;
-int tainting = FALSE;
-static int sflag = FALSE;
+VALUE rb_debug = Qfalse;
+VALUE rb_verbose = Qfalse;
+int rb_tainting = Qfalse;
+static int sflag = Qfalse;
-char *inplace = FALSE;
+char *ruby_inplace_mode = Qfalse;
char *strdup();
-extern char *sourcefile;
extern int yydebug;
-extern int nerrs;
+static int xflag = Qfalse;
-static int xflag = FALSE;
-extern VALUE RS, RS_default, ORS, FS;
-
-static void load_stdin();
+static void load_stdin _((void));
static void load_file _((char *, int));
static void forbid_setid _((char *));
-static VALUE do_loop = FALSE, do_print = FALSE;
-static VALUE do_check = FALSE, do_line = FALSE;
-static VALUE do_split = FALSE;
+static VALUE do_loop = Qfalse, do_print = Qfalse;
+static VALUE do_check = Qfalse, do_line = Qfalse;
+static VALUE do_split = Qfalse;
static char *script;
static int origargc;
static char **origargv;
-extern int sourceline;
-extern char *sourcefile;
-
#ifndef RUBY_LIB
#define RUBY_LIB "/usr/local/lib/ruby"
#endif
-
-#if defined(MSDOS) || defined(NT)
-#define RUBY_LIB_SEP ';'
-#else
-#define RUBY_LIB_SEP ':'
+#ifndef RUBY_SITE_LIB
+#define RUBY_SITE_LIB "/usr/local/lib/site_ruby"
#endif
extern VALUE rb_load_path;
-VALUE Frequire();
+
+static FILE *e_fp;
+static char *e_tmpname;
static void
addpath(path)
char *path;
{
+ const char sep = RUBY_PATH_SEP[0];
+
if (path == 0) return;
- if (strchr(path, RUBY_LIB_SEP)) {
+#if defined(__CYGWIN32__)
+ {
+ char rubylib[FILENAME_MAX];
+ conv_to_posix_path(path, rubylib);
+ path = rubylib;
+ }
+#endif
+ if (strchr(path, sep)) {
char *p, *s;
- VALUE ary = ary_new();
+ VALUE ary = rb_ary_new();
p = path;
while (*p) {
- while (*p == RUBY_LIB_SEP) p++;
- if (s = strchr(p, RUBY_LIB_SEP)) {
- ary_push(ary, str_new(p, (int)(s-p)));
+ while (*p == sep) p++;
+ if (s = strchr(p, sep)) {
+ rb_ary_push(ary, rb_str_new(p, (int)(s-p)));
p = s + 1;
}
else {
- ary_push(ary, str_new2(p));
+ rb_ary_push(ary, rb_str_new2(p));
break;
}
}
- rb_load_path = ary_plus(ary, rb_load_path);
+ rb_load_path = rb_ary_plus(ary, rb_load_path);
}
else {
- ary_unshift(rb_load_path, str_new2(path));
+ rb_ary_unshift(rb_load_path, rb_str_new2(path));
}
}
struct req_list {
char *name;
struct req_list *next;
-} *req_list;
+} req_list_head;
+struct req_list *req_list_last = &req_list_head;
static void
add_modules(mod)
@@ -119,25 +134,28 @@ add_modules(mod)
list = ALLOC(struct req_list);
list->name = mod;
- list->next = req_list;
- req_list = list;
+ list->next = 0;
+ req_list_last->next = list;
+ req_list_last = list;
}
void
-rb_require_modules()
+ruby_require_modules()
{
- struct req_list *list = req_list;
+ struct req_list *list = req_list_head.next;
struct req_list *tmp;
- req_list = 0;
+ req_list_last = 0;
while (list) {
- f_require(Qnil, str_new2(list->name));
+ rb_f_require(Qnil, rb_str_new2(list->name));
tmp = list->next;
free(list);
list = tmp;
}
}
+extern void Init_ext _((void));
+
static void
proc_options(argcp, argvp)
int *argcp;
@@ -150,9 +168,10 @@ proc_options(argcp, argvp)
if (argc == 0) return;
- version = FALSE;
- do_search = FALSE;
+ version = Qfalse;
+ do_search = Qfalse;
script_given = 0;
+ e_tmpname = NULL;
for (argc--,argv++; argc > 0; argc--,argv++) {
if (argv[0][0] != '-' || !argv[0][1]) break;
@@ -161,20 +180,21 @@ proc_options(argcp, argvp)
reswitch:
switch (*s) {
case 'a':
- do_split = TRUE;
+ do_split = Qtrue;
s++;
goto reswitch;
case 'p':
- do_print = TRUE;
+ do_print = Qtrue;
/* through */
case 'n':
- do_loop = TRUE;
+ do_loop = Qtrue;
s++;
goto reswitch;
case 'd':
- debug = TRUE;
+ rb_debug = Qtrue;
+ rb_verbose |= 1;
s++;
goto reswitch;
@@ -184,47 +204,52 @@ proc_options(argcp, argvp)
goto reswitch;
case 'v':
- show_version();
- verbose = 2;
+ ruby_show_version();
+ rb_verbose = 2;
case 'w':
- verbose |= 1;
+ rb_verbose |= 1;
s++;
goto reswitch;
case 'c':
- do_check = TRUE;
+ do_check = Qtrue;
s++;
goto reswitch;
case 's':
forbid_setid("-s");
- sflag = TRUE;
+ sflag = Qtrue;
s++;
goto reswitch;
case 'l':
- do_line = TRUE;
- ORS = RS;
+ do_line = Qtrue;
+ rb_output_rs = rb_rs;
s++;
goto reswitch;
case 'S':
forbid_setid("-S");
- do_search = TRUE;
+ do_search = Qtrue;
s++;
goto reswitch;
case 'e':
forbid_setid("-e");
- script_given++;
- if (script == 0) script = "-e";
- if (argv[1]) {
- compile_string("-e", argv[1], strlen(argv[1]));
- argc--,argv++;
+ if (!e_fp) {
+ e_tmpname = ruby_mktemp();
+ if (!e_tmpname) rb_fatal("Can't mktemp");
+ e_fp = fopen(e_tmpname, "w");
+ if (!e_fp) {
+ rb_fatal("Cannot open temporary file: %s", e_tmpname);
+ }
+ if (script == 0) script = e_tmpname;
}
- else {
- compile_string("-e", "", 0);
+ if (argv[1]) {
+ fputs(argv[1], e_fp);
+ argc--, argv++;
}
+ putc('\n', e_fp);
break;
case 'r':
@@ -240,15 +265,15 @@ proc_options(argcp, argvp)
case 'i':
forbid_setid("-i");
- if (inplace) free(inplace);
- inplace = strdup(s+1);
+ if (ruby_inplace_mode) free(ruby_inplace_mode);
+ ruby_inplace_mode = strdup(s+1);
break;
case 'x':
- xflag = TRUE;
+ xflag = Qtrue;
s++;
if (*s && chdir(s) < 0) {
- Fatal("Can't chdir to %s", s);
+ rb_fatal("Can't chdir to %s", s);
}
break;
@@ -259,12 +284,12 @@ proc_options(argcp, argvp)
argc--,argv++;
}
if (*s && chdir(s) < 0) {
- Fatal("Can't chdir to %s", s);
+ rb_fatal("Can't chdir to %s", s);
}
break;
case 'F':
- FS = str_new2(s+1);
+ rb_fs = rb_str_new2(s+1);
break;
case 'K':
@@ -283,7 +308,7 @@ proc_options(argcp, argvp)
if (numlen == 0) v = 1;
}
rb_set_safe_level(v);
- tainting = TRUE;
+ rb_tainting = Qtrue;
}
break;
@@ -305,13 +330,13 @@ proc_options(argcp, argvp)
v = scan_oct(s, 4, &numlen);
s += numlen;
- if (v > 0377) RS = Qnil;
+ if (v > 0377) rb_rs = Qnil;
else if (v == 0 && numlen >= 2) {
- RS = str_new2("\n\n");
+ rb_rs = rb_str_new2("\n\n");
}
else {
c = v & 0xff;
- RS = str_new(&c, 1);
+ rb_rs = rb_str_new(&c, 1);
}
}
goto reswitch;
@@ -325,15 +350,15 @@ proc_options(argcp, argvp)
if (strcmp("copyright", s) == 0)
copyright = 1;
else if (strcmp("debug", s) == 0)
- debug = 1;
+ rb_debug = 1;
else if (strcmp("version", s) == 0)
version = 1;
else if (strcmp("verbose", s) == 0)
- verbose = 2;
+ rb_verbose = 2;
else if (strcmp("yydebug", s) == 0)
yydebug = 1;
else {
- Fatal("Unrecognized long option: --%s",s);
+ rb_fatal("Unrecognized long option: --%s",s);
}
break;
@@ -343,7 +368,7 @@ proc_options(argcp, argvp)
break;
default:
- Fatal("Unrecognized switch: -%s",s);
+ rb_fatal("Unrecognized switch: -%s",s);
case 0:
break;
@@ -353,17 +378,26 @@ proc_options(argcp, argvp)
switch_end:
if (*argvp[0] == 0) return;
+ if (e_fp) {
+ if (fflush(e_fp) || ferror(e_fp) || fclose(e_fp))
+ rb_fatal("Cannot write to temp file for -e");
+ e_fp = NULL;
+ argc++, argv--;
+ argv[0] = e_tmpname;
+ }
+
if (version) {
- show_version();
+ ruby_show_version();
exit(0);
}
if (copyright) {
- show_copyright();
+ ruby_show_copyright();
}
- if (script_given == FALSE) {
+ Init_ext(); /* should be called here for some reason :-( */
+ if (script_given == Qfalse) {
if (argc == 0) { /* no more args */
- if (verbose == 3) exit(0);
+ if (rb_verbose == 3) exit(0);
script = "-";
load_stdin();
}
@@ -391,9 +425,10 @@ proc_options(argcp, argvp)
argc--; argv++;
}
}
- if (verbose) verbose = TRUE;
+ if (rb_verbose) rb_verbose = Qtrue;
+ if (rb_debug) rb_debug = Qtrue;
- xflag = FALSE;
+ xflag = Qfalse;
*argvp = argv;
*argcp = argc;
@@ -409,17 +444,20 @@ proc_options(argcp, argvp)
argv[0][0] = '$';
if (s = strchr(argv[0], '=')) {
*s++ = '\0';
- rb_gvar_set2(argv[0], str_new2(s));
+ rb_gvar_set2(argv[0], rb_str_new2(s));
}
else {
- rb_gvar_set2(argv[0], TRUE);
+ rb_gvar_set2(argv[0], Qtrue);
}
+ argv[0][0] = '-';
}
*argcp = argc; *argvp = argv;
}
}
+extern int ruby__end__seen;
+
static void
load_file(fname, script)
char *fname;
@@ -433,42 +471,43 @@ load_file(fname, script)
f = rb_stdin;
}
else {
- f = file_open(fname, "r");
+ FILE *fp = fopen(fname, "r");
+
+ if (fp == NULL) {
+ rb_raise(rb_eLoadError, "No such file to load -- %s", fname);
+ }
+ fclose(fp);
+
+ f = rb_file_open(fname, "r");
}
if (script) {
VALUE c;
VALUE line;
- VALUE rs = RS;
+ char *p;
- RS = RS_default;
if (xflag) {
forbid_setid("-x");
- xflag = FALSE;
- while (!NIL_P(line = io_gets(f))) {
+ xflag = Qfalse;
+ while (!NIL_P(line = rb_io_gets(f))) {
line_start++;
if (RSTRING(line)->len > 2
&& RSTRING(line)->ptr[0] == '#'
&& RSTRING(line)->ptr[1] == '!') {
- if (strstr(RSTRING(line)->ptr, "ruby")) {
+ if (p = strstr(RSTRING(line)->ptr, "ruby")) {
goto start_read;
}
}
}
- RS = rs;
- LoadError("No Ruby script found in input");
+ rb_raise(rb_eLoadError, "No Ruby script found in input");
}
- c = io_getc(f);
+ c = rb_io_getc(f);
if (c == INT2FIX('#')) {
- line = io_gets(f);
+ line = rb_io_gets(f);
line_start++;
- if (RSTRING(line)->len > 2
- && RSTRING(line)->ptr[0] == '!') {
-
- char *p;
-
+ if (RSTRING(line)->len > 2 && RSTRING(line)->ptr[0] == '!') {
if ((p = strstr(RSTRING(line)->ptr, "ruby")) == 0) {
/* not ruby script, kick the program */
char **argv;
@@ -479,10 +518,10 @@ load_file(fname, script)
if (pend[-1] == '\n') pend--; /* chomp line */
if (pend[-1] == '\r') pend--;
*pend = '\0';
- while (p < pend && isspace(*p))
+ while (p < pend && ISSPACE(*p))
p++;
path = p; /* interpreter path */
- while (p < pend && !isspace(*p))
+ while (p < pend && !ISSPACE(*p))
p++;
*p++ = '\0';
if (p < pend) {
@@ -494,36 +533,48 @@ load_file(fname, script)
argv = origargv;
}
argv[0] = path;
+#ifndef USE_CWGUSI
execv(path, argv);
- sourcefile = fname;
- sourceline = 1;
- Fatal("Can't exec %s", path);
+#endif
+ ruby_sourcefile = fname;
+ ruby_sourceline = 1;
+ rb_fatal("Can't exec %s", path);
}
start_read:
- if (p = strstr(RSTRING(line)->ptr, "ruby -")) {
+ p += 4;
+ RSTRING(line)->ptr[RSTRING(line)->len-1] = '\0';
+ if (RSTRING(line)->ptr[RSTRING(line)->len-2] == '\r')
+ RSTRING(line)->ptr[RSTRING(line)->len-2] = '\0';
+ if (p = strstr(p, " -")) {
int argc; char *argv[2]; char **argvp = argv;
- UCHAR *s;
-
- s = RSTRING(line)->ptr;
- while (isspace(*s))
- s++;
- *s = '\0';
- RSTRING(line)->ptr[RSTRING(line)->len-1] = '\0';
- if (RSTRING(line)->ptr[RSTRING(line)->len-2] == '\r')
- RSTRING(line)->ptr[RSTRING(line)->len-2] = '\0';
- argc = 2; argv[0] = 0; argv[1] = p + 5;
- proc_options(&argc, &argvp);
+ char *s = ++p;
+
+ argc = 2; argv[0] = 0;
+ while (*p == '-') {
+ while (*s && !ISSPACE(*s))
+ s++;
+ *s = '\0';
+ argv[1] = p;
+ proc_options(&argc, &argvp);
+ p = ++s;
+ while (*p && ISSPACE(*p))
+ p++;
+ }
}
}
}
else if (!NIL_P(c)) {
- io_ungetc(f, c);
+ rb_io_ungetc(f, c);
}
- RS = rs;
}
- compile_file(fname, f, line_start);
- if (f != rb_stdin) io_close(f);
+ rb_compile_file(fname, f, line_start);
+ if (script && ruby__end__seen) {
+ rb_define_global_const("DATA", f);
+ }
+ else if (f != rb_stdin) {
+ rb_io_close(f);
+ }
}
void
@@ -553,8 +604,8 @@ set_arg0(val, id)
int i;
static int len;
- if (origargv == 0) Fail("$0 not initialized");
- Check_Type(val, T_STRING);
+ if (origargv == 0) rb_raise(rb_eRuntimeError, "$0 not initialized");
+#ifndef __hpux
if (len == 0) {
s = origargv[0];
s += strlen(s);
@@ -565,8 +616,9 @@ set_arg0(val, id)
}
len = s - origargv[0];
}
- s = RSTRING(val)->ptr;
- i = RSTRING(val)->len;
+#endif
+ s = rb_str2cstr(val, &i);
+#ifndef __hpux
if (i > len) {
memcpy(origargv[0], s, len);
origargv[0][len] = '\0';
@@ -578,7 +630,22 @@ set_arg0(val, id)
while (++i < len)
*s++ = ' ';
}
- rb_progname = str_taint(str_new2(origargv[0]));
+ rb_progname = rb_tainted_str_new2(origargv[0]);
+#else
+ if (i >= PST_CLEN) {
+ union pstun j;
+ j.pst_command = s;
+ i = PST_CLEN;
+ RSTRING(val)->len = i;
+ *(s + i) = '\0';
+ pstat(PSTAT_SETCMD, j, PST_CLEN, 0, 0);
+ } else {
+ union pstun j;
+ j.pst_command = s;
+ pstat(PSTAT_SETCMD, j, i, 0, 0);
+ }
+ rb_progname = rb_tainted_str_new(s, i);
+#endif
}
void
@@ -586,8 +653,8 @@ ruby_script(name)
char *name;
{
if (name) {
- rb_progname = str_taint(str_new2(name));
- sourcefile = name;
+ rb_progname = rb_tainted_str_new2(name);
+ ruby_sourcefile = name;
}
}
@@ -614,9 +681,11 @@ forbid_setid(s)
char *s;
{
if (euid != uid)
- Fatal("No %s allowed while running setuid", s);
+ rb_raise(rb_eSecurityError, "No %s allowed while running setuid", s);
if (egid != gid)
- Fatal("No %s allowed while running setgid", s);
+ rb_raise(rb_eSecurityError, "No %s allowed while running setgid", s);
+ if (rb_safe_level() > 0)
+ rb_raise(rb_eSecurityError, "No %s allowed in tainted mode", s);
}
#if defined(_WIN32) || defined(DJGPP)
@@ -658,37 +727,48 @@ ruby_prog_init()
{
init_ids();
- sourcefile = "ruby";
- rb_define_variable("$VERBOSE", &verbose);
- rb_define_variable("$-v", &verbose);
- rb_define_variable("$DEBUG", &debug);
- rb_define_variable("$-d", &debug);
+ ruby_sourcefile = "ruby";
+ rb_define_variable("$VERBOSE", &rb_verbose);
+ rb_define_variable("$-v", &rb_verbose);
+ rb_define_variable("$DEBUG", &rb_debug);
+ rb_define_variable("$-d", &rb_debug);
rb_define_readonly_variable("$-p", &do_print);
rb_define_readonly_variable("$-l", &do_line);
+ if (rb_safe_level() == 0) {
+ addpath(".");
+ }
+
+ addpath(RUBY_LIB);
#if defined(_WIN32) || defined(DJGPP)
addpath(ruby_libpath());
#endif
+#ifdef __MACOS__
+ setup_macruby_libpath();
+#endif
- if (rb_safe_level() == 0) {
- addpath(getenv("RUBYLIB"));
- }
-
+#ifdef RUBY_ARCHLIB
+ addpath(RUBY_ARCHLIB);
+#endif
#ifdef RUBY_THIN_ARCHLIB
addpath(RUBY_THIN_ARCHLIB);
#endif
-#ifdef RUBY_ARCHLIB
- addpath(RUBY_ARCHLIB);
+ addpath(RUBY_SITE_LIB);
+#ifdef RUBY_SITE_ARCHLIB
+ addpath(RUBY_SITE_ARCHLIB);
#endif
- addpath(RUBY_LIB);
+#ifdef RUBY_SITE_THIN_ARCHLIB
+ addpath(RUBY_SITE_THIN_ARCHLIB);
+#endif
+
if (rb_safe_level() == 0) {
- addpath(".");
+ addpath(getenv("RUBYLIB"));
}
rb_define_hooked_variable("$0", &rb_progname, 0, set_arg0);
- rb_argv = ary_new();
+ rb_argv = rb_ary_new();
rb_define_readonly_variable("$*", &rb_argv);
rb_define_global_const("ARGV", rb_argv);
rb_define_readonly_variable("$-a", &do_split);
@@ -716,7 +796,7 @@ ruby_set_argv(argc, argv)
else dln_argv0 = argv[0];
#endif
for (i=0; i < argc; i++) {
- ary_push(rb_argv, str_taint(str_new2(argv[i])));
+ rb_ary_push(rb_argv, rb_tainted_str_new2(argv[i]));
}
}
@@ -725,12 +805,9 @@ ruby_process_options(argc, argv)
int argc;
char **argv;
{
- extern VALUE errat;
- int i;
-
origargc = argc; origargv = argv;
ruby_script(argv[0]); /* for the time being */
- rb_argv0 = str_taint(str_new2(argv[0]));
+ rb_argv0 = rb_progname;
#if defined(USE_DLN_A_OUT)
dln_argv0 = argv[0];
#endif
@@ -738,14 +815,22 @@ ruby_process_options(argc, argv)
ruby_script(script);
ruby_set_argv(argc, argv);
- if (do_check && nerrs == 0) {
+ if (do_check && ruby_nerrs == 0) {
printf("Syntax OK\n");
exit(0);
}
if (do_print) {
- yyappend_print();
+ rb_parser_append_print();
}
if (do_loop) {
- yywhile_loop(do_line, do_split);
+ rb_parser_while_loop(do_line, do_split);
+ }
+ if (e_fp) {
+ fclose(e_fp);
+ e_fp = NULL;
+ }
+ if (e_tmpname) {
+ unlink(e_tmpname);
+ e_tmpname = NULL;
}
}