diff --git a/ChangeLog b/ChangeLog index 6029e13952..f7969ae0f2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +Tue Feb 3 12:24:07 1998 Yukihiro Matsumoto + + * re.c (match_to_a): non matching element should be nil. + + * ruby.c (ruby_load_script): load script after all initialization. + + * bignum.c (str2inum): need to interpret prefix `0' of `0x'. + +Tue Feb 3 10:00:18 1998 WATANABE Hirofumi + + * numeric.c (fix_rshift): use `sizeof(INT)*8' instead of 32. + +Mon Feb 2 14:09:24 1998 Yukihiro Matsumoto + + * ruby.c (set_arg0): grab environment region too. + Thu Jan 29 18:36:25 1998 WATANABE Hirofumi * process.c (rb_proc_exec): check `sh' to be exist. diff --git a/bignum.c b/bignum.c index 357a17dba4..481a35efcf 100644 --- a/bignum.c +++ b/bignum.c @@ -195,12 +195,16 @@ str2inum(str, base) base = 10; } } - len = strlen(str); if (base == 8) { - len = 3*len*sizeof(char); + while (str[0] == '0') str++; + len = 3*strlen(str)*sizeof(char); } else { /* base == 10 or 16 */ - len = 4*len*sizeof(char); + if (base == 16 && str[0] == '0' && (str[1] == 'x'||str[1] == 'X')) { + str += 2; + } + while (str[0] == '0') str++; + len = 4*strlen(str)*sizeof(char); } if (len <= (sizeof(VALUE)*CHAR_BIT)) { diff --git a/eval.c b/eval.c index 7ec0e0821b..5d66e25d7d 100644 --- a/eval.c +++ b/eval.c @@ -733,15 +733,13 @@ ruby_options(argc, argv) PUSH_TAG(PROT_NONE) if ((state = EXEC_TAG()) == 0) { - NODE *save; ruby_process_options(argc, argv); - save = eval_tree; - eval_tree = 0; Init_ext(); ext_init = 1; - rb_require_modules(); - eval_tree = save; + ruby_require_modules(); + eval_tree = 0; + ruby_load_script(); } POP_TAG(); if (state) { diff --git a/intern.h b/intern.h index 192da014d5..d8531bdda4 100644 --- a/intern.h +++ b/intern.h @@ -216,12 +216,13 @@ VALUE reg_match _((VALUE, VALUE)); VALUE reg_match2 _((VALUE)); void rb_set_kcode _((char *)); /* ruby.c */ -void rb_require_modules _((void)); void rb_load_file _((char *)); void ruby_script _((char *)); void ruby_prog_init _((void)); void ruby_set_argv _((int, char **)); void ruby_process_options _((int, char **)); +void ruby_require_modules _((void)); +void ruby_load_script _((void)); /* signal.c */ VALUE f_kill _((int, VALUE *)); void gc_mark_trap_list _((void)); diff --git a/lib/date.rb b/lib/date.rb index 0f14d59ff5..2d5090b62b 100644 --- a/lib/date.rb +++ b/lib/date.rb @@ -1,8 +1,8 @@ # # Date.rb - # $Release Version: $ -# $Revision: 1.1.1.1.4.1 $ -# $Date: 1998/01/16 12:36:04 $ +# $Revision: 1.1.1.1.4.2 $ +# $Date: 1998/02/02 04:49:13 $ # by Yasuo OHBA(SHL Japan Inc. Technology Dept.) # # -- @@ -32,10 +32,16 @@ class Date } def initialize(y = 1, m = 1, d = 1) - if y.kind_of?(String) && y.size == 8 - @year = y[0,4].to_i - @month = y[4,2].to_i - @day = y[6,2].to_i + if y.kind_of?(String) + case y + when /(\d\d\d\d)-?(?:(\d\d)-?(\d\d)?)?/ + @year = $1.to_i + @month = if $2 then $2.to_i else 1 end + @day = if $3 then $3.to_i else 1 end + else + require 'parsedate' + @year, @month, @day = ParseDate.parsedate(y) + end else if m.kind_of?(String) m = Monthtab[m.downcase] @@ -66,18 +72,25 @@ class Date def period return Date.period!(@year, @month, @day) end - + + def jd + return period + 1721423 + end + + def mjd + return jd - 2400000.5 + end + + def to_s + format("%.3s, %.3s %2d %4d", name_of_week, name_of_month, @day, @year) + end + + def inspect + to_s + end + def day_of_week - dl = Date.daylist(@year) - d = Date.jan1!(@year) - for m in 1..(@month - 1) - d += dl[m] - end - d += @day - 1 - if @year == 1752 && @month == 9 && @day >= 14 - d -= (14 - 3) - end - return (d % 7) + return (period + 5) % 7 end def name_of_week @@ -141,6 +154,9 @@ class Date end def _check_date + if @year == nil or @month == nil or @day == nil + raise ArgumentError, "argument contains nil" + end m = Date.daylist(@year) if @month < 1 || @month > 12 raise ArgumentError, "argument(month) out of range." diff --git a/lib/parsedate.rb b/lib/parsedate.rb index 2ec54b0125..69b7ae98a9 100644 --- a/lib/parsedate.rb +++ b/lib/parsedate.rb @@ -4,39 +4,66 @@ module ParseDate 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12 } MONTHPAT = MONTHS.keys.join('|') - DAYPAT = 'mon|tue|wed|thu|fri|sat|sun' + DAYS = { + 'sun' => 0, 'mon' => 1, 'tue' => 2, 'wed' => 3, + 'thu' => 4, 'fri' => 5, 'sat' => 6 } + DAYPAT = DAYS.keys.join('|') def parsedate(date) - if date.sub!(/(#{DAYPAT})/i, ' ') - dayofweek = $1 + # ISO 8601? + if date =~ /(\d\d\d\d)-?(?:(\d\d)-?(\d\d)?)? *(?:(\d\d):(\d\d)(?::(\d\d))?)?/ + return $1.to_i, + if $2 then $2.to_i else 1 end, + if $3 then $3.to_i else 1 end, + nil, + if $4 then $4.to_i end, + if $5 then $5.to_i end, + if $6 then $6.to_i end, + nil end - if date.sub!(/\s+(\d+:\d+(:\d+)?)/, ' ') - time = $1 + date = date.dup + if date.sub!(/(#{DAYPAT})[a-z]*,?/i, ' ') + wday = DAYS[$1.downcase] end - if date =~ /(19|20)(\d\d)/ - year = Integer($2) + if date.sub!(/(\d+):(\d+)(?::(\d+))?\s*(am|pm)?\s*(?:\s+([a-z]{1,4}(?:\s+[a-z]{1,4})|[-+]\d{4}))?/i, ' ') + hour = $1.to_i + min = $2.to_i + if $3 + sec = $3.to_i + end + if $4 == 'pm' + hour += 12 + end + if $5 + zone = $5 + end end - if date.sub!(/\s*(\d+)\s+(#{MONTHPAT})\S*\s+/i, ' ') - dayofmonth = $1.to_i - monthname = $2 - elsif date.sub!(/\s*(#{MONTHPAT})\S*\s+(\d+)\s+/i, ' ') - monthname = $1 - dayofmonth = $2.to_i - elsif date.sub!(/\s*(#{MONTHPAT})\S*\s+(\d+)\D+/i, ' ') - monthname = $1 - dayofmonth = $2.to_i - elsif date.sub!(/\s*(\d\d?)\/(\d\d?)/, ' ') - month = $1 - dayofmonth = $2.to_i + if date.sub!(/(\d+)\S*\s+(#{MONTHPAT})\S*(?:\s+(\d+))?/i, ' ') + mday = $1.to_i + mon = MONTHS[$2.downcase] + if $3 + year = $3.to_i + end + elsif date.sub!(/(#{MONTHPAT})\S*\s+(\d+)\S*\s*,?(?:\s+(\d+))?/i, ' ') + mon = MONTHS[$1.downcase] + mday = $2.to_i + if $3 + year = $3.to_i + end + elsif date.sub!(/(\d+)\/(\d+)(?:\/(\d+))/, ' ') + mon = $1.to_i + mday = $2.to_i + if $3 + year = $3.to_i + end end - if monthname - month = MONTHS[monthname.downcase] - end - if ! year && date =~ /\d\d/ - year = Integer($&) - end - return year, month, dayofmonth + return year, mon, mday, wday, hour, min, sec, zone end module_function :parsedate end + +if __FILE__ == $0 + p Time.now.asctime + p ParseDate.parsedate(Time.now.asctime) +end diff --git a/lib/tracer.rb b/lib/tracer.rb index d37339fd62..ef03fe09c9 100644 --- a/lib/tracer.rb +++ b/lib/tracer.rb @@ -1,7 +1,23 @@ +#!/usr/local/bin/ruby +# +# tracer.rb - +# $Release Version: 0.2$ +# $Revision: 1.6 $ +# $Date: 1998/02/02 08:12:02 $ +# by Keiju ISHITSUKA(Nippon Rational Inc.) +# +# -- +# +# +# + +# +# tracer main class +# class Tracer - MY_FILE_NAME_PATTERN = /^tracer\.(rb)?/ - Threads = Hash.new - Sources = Hash.new + RCS_ID='-$Id: tracer.rb,v 1.6 1998/02/02 08:12:02 keiju Exp keiju $-' + + MY_FILE_NAME = caller(0)[0].scan(/^(.*):[0-9]+$/)[0] EVENT_SYMBOL = { "line" => "-", @@ -10,11 +26,31 @@ class Tracer "class" => "C", "end" => "E"} + def initialize + @threads = Hash.new + if defined? Thread.main + @threads[Thread.main.id] = 0 + else + @threads[Thread.current.id] = 0 + end + + @sources = Hash.new + end + def on - set_trace_func proc{|event, file, line, id, binding| - trace_func event, file, line, id, binding - } - print "Trace on\n" + if iterator? + on + begin + yield + ensure + off + end + else + set_trace_func proc{|event, file, line, id, binding| + trace_func event, file, line, id, binding + } + print "Trace on\n" + end end def off @@ -22,27 +58,38 @@ class Tracer print "Trace off\n" end - def get_thread_no - unless no = Threads[Thread.current.id] - Threads[Thread.current.id] = no = Threads.size - end - no - end - def get_line(file, line) - unless list = Sources[file] - f =open(file) - begin - Sources[file] = list = f.readlines - ensure - f.close + unless list = @sources[file] +# print file if $DEBUG + begin + f = open(file) + begin + @sources[file] = list = f.readlines + ensure + f.close + end + rescue + @sources[file] = list = [] end end - list[line - 1] + if l = list[line - 1] + l + else + "-\n" + end + end + + def get_thread_no + if no = @threads[Thread.current.id] + no + else + @threads[Thread.current.id] = @threads.size + end end def trace_func(event, file, line, id, binding) - return if File.basename(file) =~ MY_FILE_NAME_PATTERN + return if file == MY_FILE_NAME + #printf "Th: %s\n", Thread.current.inspect Thread.critical = TRUE printf("#%d:%s:%d:%s: %s", @@ -65,11 +112,15 @@ class Tracer end -if File.basename($0) =~ Tracer::MY_FILE_NAME_PATTERN - $0 = ARGV.shift - - Tracer.on - load $0 -else - Tracer.on +if caller(0).size == 1 + if $0 == Tracer::MY_FILE_NAME + # direct call + + $0 = ARGV[0] + ARGV.shift + Tracer.on + require $0 + else + Tracer.on + end end diff --git a/numeric.c b/numeric.c index 4bc9a604ec..28adaa6671 100644 --- a/numeric.c +++ b/numeric.c @@ -824,7 +824,7 @@ fix_rshift(x, y) long i, val; i = NUM2INT(y); - if (y < 32) { + if (i < sizeof(INT) * 8) { val = RSHIFT(FIX2INT(x), i); return INT2FIX(val); } diff --git a/object.c b/object.c index 1ba155cbfa..834055e996 100644 --- a/object.c +++ b/object.c @@ -100,8 +100,8 @@ obj_clone(obj) CLONESETUP(clone,obj); if (ROBJECT(obj)->iv_tbl) { ROBJECT(clone)->iv_tbl = st_copy(ROBJECT(obj)->iv_tbl); - RBASIC(clone)->class = singleton_class_clone(RBASIC(obj)->class); - RBASIC(clone)->flags = RBASIC(obj)->flags; + RBASIC(clone)->class = singleton_class_clone(RBASIC(obj)->class); + RBASIC(clone)->flags = RBASIC(obj)->flags; } return clone; diff --git a/re.c b/re.c index 83fb94afb1..41f5ff887b 100644 --- a/re.c +++ b/re.c @@ -570,7 +570,7 @@ match_to_a(match) int i; for (i=0; inum_regs; i++) { - if (regs->beg[0] == -1) ary_push(ary, Qnil); + if (regs->beg[i] == -1) ary_push(ary, Qnil); else ary_push(ary, str_new(ptr+regs->beg[i], regs->end[i]-regs->beg[i])); } diff --git a/ruby.c b/ruby.c index 03b29ff7b8..ed4e653190 100644 --- a/ruby.c +++ b/ruby.c @@ -56,12 +56,25 @@ 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 int do_search = FALSE; static char *script; +static char *e_body; static int origargc; static char **origargv; +#if defined(NeXT) && defined(__DYNAMIC__) + +#include +extern char *** environ_pointer; +#define environ (*environ_pointer) +#else +#ifndef NT +extern char **environ; +#endif +#endif +static char **origenviron; + extern int sourceline; extern char *sourcefile; @@ -124,7 +137,7 @@ add_modules(mod) } void -rb_require_modules() +ruby_require_modules() { struct req_list *list = req_list; struct req_list *tmp; @@ -145,7 +158,7 @@ proc_options(argcp, argvp) { int argc = *argcp; char **argv = *argvp; - int script_given, do_search; + int script_given; char *s; if (argc == 0) return; @@ -219,11 +232,11 @@ proc_options(argcp, argvp) script_given++; if (script == 0) script = "-e"; if (argv[1]) { - compile_string("-e", argv[1], strlen(argv[1])); + e_body = argv[1]; argc--,argv++; } else { - compile_string("-e", "", 0); + e_body = ""; } break; @@ -365,35 +378,20 @@ proc_options(argcp, argvp) if (argc == 0) { /* no more args */ if (verbose == 3) exit(0); script = "-"; - load_stdin(); } else { script = argv[0]; if (script[0] == '\0') { script = "-"; - load_stdin(); } else { - if (do_search) { - char *path = getenv("RUBYPATH"); - - script = 0; - if (path) { - script = dln_find_file(argv[0], path); - } - if (!script) { - script = dln_find_file(argv[0], getenv("PATH")); - } - if (!script) script = argv[0]; - } - load_file(script, 1); + script = argv[0]; } argc--; argv++; } } if (verbose) verbose = TRUE; - xflag = FALSE; *argvp = argv; *argcp = argc; @@ -420,6 +418,41 @@ proc_options(argcp, argvp) } +void +ruby_load_script() +{ + if (script[0] == '-') { + if (script[1] == '\0') { + load_stdin(); + } + else if (script[1] == 'e') { + compile_string("-e", e_body, strlen(e_body)); + } + } + else { + if (do_search) { + char *path = getenv("RUBYPATH"); + char *s = 0; + + if (path) { + s = dln_find_file(script, path); + } + if (!s) { + s = dln_find_file(script, getenv("PATH")); + } + if (s) script = s; + } + load_file(script, 1); + } + xflag = FALSE; + if (do_print) { + yyappend_print(); + } + if (do_loop) { + yywhile_loop(do_line, do_split); + } +} + static void load_file(fname, script) char *fname; @@ -514,6 +547,9 @@ load_file(fname, script) RSTRING(line)->ptr[RSTRING(line)->len-2] = '\0'; argc = 2; argv[0] = 0; argv[1] = p + 5; proc_options(&argc, &argvp); +#if 0 + proc_sflag(&argc, &argvp); +#endif } } } @@ -553,7 +589,7 @@ set_arg0(val, id) int i; static int len; - if (origargv == 0) Fail("$0 not initialized"); + if (origargv == 0) ArgError("$0 not initialized"); Check_Type(val, T_STRING); if (len == 0) { s = origargv[0]; @@ -563,6 +599,14 @@ set_arg0(val, id) if (origargv[i] == s + 1) s += strlen(++s); /* this one is ok too */ } + /* can grab env area too? */ + if (origenviron && origenviron[0] == s + 1) { + setenv("NoNe SuCh", "Ruby Compiler :-)", 1); + /* force copy of environment */ + for (i = 0; origenviron[i]; i++) + if (origenviron[i] == s + 1) + s += strlen(++s); + } len = s - origargv[0]; } s = RSTRING(val)->ptr; @@ -729,6 +773,11 @@ ruby_process_options(argc, argv) int i; origargc = argc; origargv = argv; +#if defined(NeXT) && defined(__DYNAMIC__) + _dyld_lookup_and_bind("__environ", (unsigned long*)&environ_pointer, NULL); +#endif /* environ */ + origenviron = environ; + ruby_script(argv[0]); /* for the time being */ rb_argv0 = str_taint(str_new2(argv[0])); #if defined(USE_DLN_A_OUT) @@ -742,10 +791,4 @@ ruby_process_options(argc, argv) printf("Syntax OK\n"); exit(0); } - if (do_print) { - yyappend_print(); - } - if (do_loop) { - yywhile_loop(do_line, do_split); - } } diff --git a/time.c b/time.c index 7dd2bb6286..c16a11641f 100644 --- a/time.c +++ b/time.c @@ -417,6 +417,23 @@ time_asctime(time) char buf[64]; int len; + GetTimeval(time, tobj); + if (tobj->tm_got == 0) { + time_localtime(time); + } + len = strftime(buf, 64, "%c", &(tobj->tm)); + + return str_new(buf, len); +} + +static VALUE +time_to_s(time) + VALUE time; +{ + struct time_object *tobj; + char buf[64]; + int len; + GetTimeval(time, tobj); if (tobj->tm_got == 0) { time_localtime(time); @@ -774,8 +791,8 @@ Init_Time() rb_define_method(cTime, "gmtime", time_gmtime, 0); rb_define_method(cTime, "ctime", time_asctime, 0); rb_define_method(cTime, "asctime", time_asctime, 0); - rb_define_method(cTime, "to_s", time_asctime, 0); - rb_define_method(cTime, "inspect", time_asctime, 0); + rb_define_method(cTime, "to_s", time_to_s, 0); + rb_define_method(cTime, "inspect", time_to_s, 0); rb_define_method(cTime, "to_a", time_to_a, 0); rb_define_method(cTime, "+", time_plus, 1);