yarv-diff:314
From: ko1 atdot.net
Date: 27 Feb 2006 04:55:06 -0000
Subject: [yarv-diff:314] r479 - in trunk: . ext ext/racc ext/racc/cparse ext/syslog
Author: aamine Date: 2006-02-27 13:55:05 +0900 (Mon, 27 Feb 2006) New Revision: 479 Added: trunk/ext/racc/ trunk/ext/racc/cparse/ trunk/ext/racc/cparse/.cvsignore trunk/ext/racc/cparse/Makefile trunk/ext/racc/cparse/README trunk/ext/racc/cparse/cparse.c trunk/ext/racc/cparse/depend trunk/ext/racc/cparse/extconf.rb trunk/ext/syslog/ trunk/ext/syslog/.cvsignore trunk/ext/syslog/depend trunk/ext/syslog/extconf.rb trunk/ext/syslog/syslog.c trunk/ext/syslog/syslog.txt trunk/ext/syslog/test.rb Modified: trunk/ChangeLog Log: * ext/syslog: imported from Ruby CVS trunk HEAD. * ext/racc: ditto. Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ChangeLog 2006-02-27 04:55:05 UTC (rev 479) @@ -4,6 +4,13 @@ # from Mon, 03 May 2004 01:24:19 +0900 # +2006-02-27(Mon) 13:54:47 +0900 Minero Aoki <aamine loveruby.net> + + * ext/syslog: imported from Ruby CVS trunk HEAD. + + * ext/racc: ditto. + + 2006-02-27(Mon) 12:47:10 +0900 Minero Aoki <aamine loveruby.net> * parse.y: follow coding style change. Added: trunk/ext/racc/cparse/.cvsignore =================================================================== --- trunk/ext/racc/cparse/.cvsignore 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/racc/cparse/.cvsignore 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,3 @@ +Makefile +mkmf.log +*.def Added: trunk/ext/racc/cparse/Makefile =================================================================== --- trunk/ext/racc/cparse/Makefile 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/racc/cparse/Makefile 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,143 @@ + +SHELL = /bin/sh + +#### Start of system configuration section. #### + +srcdir = $(hdrdir)/ext/racc/cparse +topdir = ../../.. +hdrdir = $(topdir)/. +VPATH = $(srcdir):$(topdir):$(hdrdir) +prefix = $(DESTDIR)/home/aamine/tmp/yarv +exec_prefix = $(prefix) +sitedir = $(prefix)/lib/ruby/site_ruby +rubylibdir = $(libdir)/ruby/$(ruby_version) +archdir = $(rubylibdir)/$(arch) +sbindir = $(exec_prefix)/sbin +datadir = $(prefix)/share +includedir = $(prefix)/include +infodir = $(prefix)/info +sysconfdir = $(prefix)/etc +mandir = $(prefix)/man +libdir = $(exec_prefix)/lib +sharedstatedir = $(prefix)/com +oldincludedir = $(DESTDIR)/usr/include +sitearchdir = $(sitelibdir)/$(sitearch) +bindir = $(exec_prefix)/bin +localstatedir = $(prefix)/var +sitelibdir = $(sitedir)/$(ruby_version) +libexecdir = $(exec_prefix)/libexec + +CC = gcc +CXX = g++ +LIBRUBY = $(LIBRUBY_A) +LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a +LIBRUBYARG_SHARED = -Wl,-R -Wl,$(libdir) -L$(libdir) -L. +LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static + +CFLAGS = -fPIC -g -O2 -Wall +CPPFLAGS = -I. -I$(topdir) -I$(hdrdir) -I$(srcdir) +CXXFLAGS = $(CFLAGS) -g -O2 +DLDFLAGS = +LDSHARED = $(CC) -shared +LDSHAREDXX = $(CXX) -shared +AR = ar +EXEEXT = + +RUBY_INSTALL_NAME = ruby +RUBY_SO_NAME = ruby +arch = x86_64-linux +sitearch = x86_64-linux +ruby_version = 2.0 +ruby = $(topdir)/miniruby -I'$(topdir)' -I'$(hdrdir)/lib' +RUBY = $(ruby) +RM = rm -f +MAKEDIRS = mkdir -p +INSTALL = /usr/bin/install -c +INSTALL_PROG = $(INSTALL) -m 0755 +INSTALL_DATA = $(INSTALL) -m 644 +COPY = cp + +#### End of system configuration section. #### + +preload = + +libpath = $(topdir) +LIBPATH = -L'$(topdir)' +DEFFILE = + +CLEANFILES = +DISTCLEANFILES = + +extout = ./.ext +extout_prefix = $(extout)$(target_prefix)/ +target_prefix = /racc +LOCAL_LIBS = +LIBS = -lpthread -ldl -lcrypt -lm -lc +SRCS = cparse.c +OBJS = cparse.o +TARGET = cparse +DLLIB = $(TARGET).so +STATIC_LIB = $(TARGET).a + +RUBYCOMMONDIR = $(extout) +RUBYLIBDIR = $(extout)$(target_prefix) +RUBYARCHDIR = $(extout)/$(arch)$(target_prefix) + +TARGET_SO = $(RUBYARCHDIR)/$(DLLIB) +CLEANLIBS = $(RUBYARCHDIR)/$(TARGET).so $(RUBYARCHDIR)/$(TARGET).il? $(RUBYARCHDIR)/$(TARGET).tds $(RUBYARCHDIR)/$(TARGET).map +CLEANOBJS = *.o *.a *.s[ol] *.pdb *.exp *.bak + +all: install +static: $(STATIC_LIB) install-rb + +clean: + @-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) + +distclean: clean + @-$(RM) Makefile extconf.h conftest.* mkmf.log + @-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES) + +realclean: distclean +install: install-so install-rb + +install-so: $(RUBYARCHDIR) +install-so: $(RUBYARCHDIR)/$(DLLIB) +install-rb: pre-install-rb install-rb-default +install-rb-default: pre-install-rb-default +pre-install-rb: Makefile +pre-install-rb-default: Makefile +$(RUBYARCHDIR): + $(MAKEDIRS) $@ + +site-install: site-install-so site-install-rb +site-install-so: install-so +site-install-rb: install-rb + +.SUFFIXES: .c .m .cc .cxx .cpp .C .o + +.cc.o: + $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< + +.cxx.o: + $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< + +.cpp.o: + $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< + +.C.o: + $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< + +.c.o: + $(CC) $(CFLAGS) $(CPPFLAGS) -c $< + +$(RUBYARCHDIR)/$(DLLIB): $(OBJS) + @-$(RM) $@ + @-$(MAKEDIRS) $(@D) + $(LDSHARED) $(DLDFLAGS) $(LIBPATH) -o $@ $(OBJS) $(LOCAL_LIBS) $(LIBS) + +$(STATIC_LIB): $(OBJS) + $(AR) cru $@ $(OBJS) + @-ranlib $(DLLIB) 2> /dev/null || true + +### +cparse.o: cparse.c $(hdrdir)/ruby.h $(topdir)/config.h $(hdrdir)/defines.h Added: trunk/ext/racc/cparse/README =================================================================== --- trunk/ext/racc/cparse/README 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/racc/cparse/README 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,10 @@ +Racc Runtime README +=================== + +This directory contains a runtime library of +Racc parser generator. If you want to generate +your own parser, you must get Racc full package. +Get it from: + + http://raa.ruby-lang.org/list.rhtml?name=racc + Added: trunk/ext/racc/cparse/cparse.c =================================================================== --- trunk/ext/racc/cparse/cparse.c 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/racc/cparse/cparse.c 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,824 @@ +/* + + cparse.c -- racc runtime core + + Copyright (c) 1999-2003 Minero Aoki <aamine loveruby.net> + + This library is free software. + You can distribute/modify this program under the same terms of ruby. + + $raccId: cparse.c,v 1.3 2003/11/03 12:20:54 aamine Exp $ + +*/ + +#include "ruby.h" +#include <stdio.h> + + +/* ----------------------------------------------------------------------- + Important Constants +----------------------------------------------------------------------- */ + +#define RACC_VERSION "1.4.4" + +#define DEFAULT_TOKEN -1 +#define ERROR_TOKEN 1 +#define FINAL_TOKEN 0 + +#define vDEFAULT_TOKEN INT2FIX(DEFAULT_TOKEN) +#define vERROR_TOKEN INT2FIX(ERROR_TOKEN) +#define vFINAL_TOKEN INT2FIX(FINAL_TOKEN) + + +/* ----------------------------------------------------------------------- + File Local Variables +----------------------------------------------------------------------- */ + +static VALUE RaccBug; +static VALUE CparseParams; + +static ID id_yydebug; +static ID id_nexttoken; +static ID id_onerror; +static ID id_noreduce; +static ID id_catch; +static VALUE sym_raccjump; +static ID id_errstatus; + +static ID id_d_shift; +static ID id_d_reduce; +static ID id_d_accept; +static ID id_d_read_token; +static ID id_d_next_state; +static ID id_d_e_pop; + + +/* ----------------------------------------------------------------------- + Utils +----------------------------------------------------------------------- */ + +static ID value_to_id _((VALUE v)); +static inline long num_to_long _((VALUE n)); + +#ifdef ID2SYM +# define id_to_value(i) ID2SYM(i) +#else +# define id_to_value(i) ULONG2NUM(i) +#endif + +static ID +value_to_id(v) + VALUE v; +{ +#ifndef SYMBOL_P +# define SYMBOL_P(v) FIXNUM_P(v) +#endif + if (! SYMBOL_P(v)) { + rb_raise(rb_eTypeError, "not symbol"); + } +#ifdef SYM2ID + return SYM2ID(v); +#else + return (ID)NUM2ULONG(v); +#endif +} + +#ifndef LONG2NUM +# define LONG2NUM(i) INT2NUM(i) +#endif + +static inline long +num_to_long(n) + VALUE n; +{ + return NUM2LONG(n); +} + +#define AREF(s, idx) \ + ((0 <= idx && idx < RARRAY(s)->len) ? RARRAY(s)->ptr[idx] : Qnil) + + +/* ----------------------------------------------------------------------- + Parser Stack Interfaces +----------------------------------------------------------------------- */ + +static VALUE get_stack_tail _((VALUE stack, long len)); +static void cut_stack_tail _((VALUE stack, long len)); + +static VALUE +get_stack_tail(stack, len) + VALUE stack; + long len; +{ + if (len < 0) return Qnil; /* system error */ + if (len > RARRAY(stack)->len) len = RARRAY(stack)->len; + return rb_ary_new4(len, RARRAY(stack)->ptr + RARRAY(stack)->len - len); +} + +static void +cut_stack_tail(stack, len) + VALUE stack; + long len; +{ + while (len > 0) { + rb_ary_pop(stack); + len--; + } +} + +#define STACK_INIT_LEN 64 +#define NEW_STACK() rb_ary_new2(STACK_INIT_LEN) +#define PUSH(s, i) rb_ary_store(s, RARRAY(s)->len, i) +#define POP(s) rb_ary_pop(s) +#define LAST_I(s) \ + ((RARRAY(s)->len > 0) ? RARRAY(s)->ptr[RARRAY(s)->len - 1] : Qnil) +#define GET_TAIL(s, len) get_stack_tail(s, len) +#define CUT_TAIL(s, len) cut_stack_tail(s, len) + + +/* ----------------------------------------------------------------------- + struct cparse_params +----------------------------------------------------------------------- */ + +struct cparse_params { + VALUE value_v; /* VALUE version of this struct */ + + VALUE parser; /* parser object */ + + int lex_is_iterator; + VALUE lexer; /* scanner object */ + ID lexmid; /* name of scanner method (must be an iterator) */ + + /* State transition tables (immutable) + Data structure is from Dragon Book 4.9 */ + /* action table */ + VALUE action_table; + VALUE action_check; + VALUE action_default; + VALUE action_pointer; + /* goto table */ + VALUE goto_table; + VALUE goto_check; + VALUE goto_default; + VALUE goto_pointer; + + long nt_base; /* NonTerminal BASE index */ + VALUE reduce_table; /* reduce data table */ + VALUE token_table; /* token conversion table */ + + /* parser stacks and parameters */ + VALUE state; + long curstate; + VALUE vstack; + VALUE tstack; + VALUE t; + long shift_n; + long reduce_n; + long ruleno; + + long errstatus; /* nonzero in error recovering mode */ + long nerr; /* number of error */ + + int use_result_var; + + VALUE retval; /* return value of parser routine */ + long fin; /* parse result status */ +#define CP_FIN_ACCEPT 1 +#define CP_FIN_EOT 2 +#define CP_FIN_CANTPOP 3 + + int debug; /* user level debug */ + int sys_debug; /* system level debug */ + + long i; /* table index */ +}; + + +/* ----------------------------------------------------------------------- + Parser Main Routines +----------------------------------------------------------------------- */ + +static VALUE racc_cparse _((VALUE parser, VALUE arg, VALUE sysdebug)); +static VALUE racc_yyparse _((VALUE parser, VALUE lexer, VALUE lexmid, + VALUE arg, VALUE sysdebug)); + +static void call_lexer _((struct cparse_params *v)); +static VALUE lexer_iter _((VALUE data)); +static VALUE lexer_i _((VALUE block_args, VALUE data, VALUE self)); + +static VALUE assert_array _((VALUE a)); +static long assert_integer _((VALUE n)); +static VALUE assert_hash _((VALUE h)); +static void initialize_params _((struct cparse_params *v, + VALUE parser, VALUE arg, + VALUE lexer, VALUE lexmid)); + +static void parse_main _((struct cparse_params *v, + VALUE tok, VALUE val, int resume)); +static void extract_user_token _((struct cparse_params *v, + VALUE block_args, VALUE *tok, VALUE *val)); +static void shift _((struct cparse_params* v, long act, VALUE tok, VALUE val)); +static int reduce _((struct cparse_params* v, long act)); +static VALUE catch_iter _((VALUE dummy)); +static VALUE reduce0 _((VALUE block_args, VALUE data, VALUE self)); + +#ifdef DEBUG +# define D_puts(msg) if (v->sys_debug) puts(msg) +# define D_printf(fmt,arg) if (v->sys_debug) printf(fmt,arg) +#else +# define D_puts(msg) +# define D_printf(fmt,arg) +#endif + +static VALUE +racc_cparse(parser, arg, sysdebug) + VALUE parser, arg, sysdebug; +{ + struct cparse_params params; + struct cparse_params *v = ¶ms; + + D_puts("starting cparse"); + v->sys_debug = RTEST(sysdebug); + initialize_params(v, parser, arg, Qnil, Qnil); + v->lex_is_iterator = Qfalse; + parse_main(v, Qnil, Qnil, 0); + + return v->retval; +} + +static VALUE +racc_yyparse(parser, lexer, lexmid, arg, sysdebug) + VALUE parser, lexer, lexmid, arg, sysdebug; +{ + struct cparse_params params; + struct cparse_params *v = ¶ms; + + v->sys_debug = RTEST(sysdebug); + D_puts("start C yyparse"); + initialize_params(v, parser, arg, lexer, lexmid); + v->lex_is_iterator = Qtrue; + D_puts("params initialized"); + parse_main(v, Qnil, Qnil, 0); + call_lexer(v); + if (!v->fin) { + rb_raise(rb_eArgError, "%s() is finished before EndOfToken", + rb_id2name(v->lexmid)); + } + + return v->retval; +} + +static void +call_lexer(v) + struct cparse_params *v; +{ + rb_iterate(lexer_iter, v->value_v, lexer_i, v->value_v); +} + +static VALUE +lexer_iter(data) + VALUE data; +{ + struct cparse_params *v; + + Data_Get_Struct(data, struct cparse_params, v); + rb_funcall(v->lexer, v->lexmid, 0); + return Qnil; +} + +static VALUE +lexer_i(block_args, data, self) + VALUE block_args, data, self; +{ + struct cparse_params *v; + VALUE tok, val; + + Data_Get_Struct(data, struct cparse_params, v); + if (v->fin) + rb_raise(rb_eArgError, "extra token after EndOfToken"); + extract_user_token(v, block_args, &tok, &val); + parse_main(v, tok, val, 1); + if (v->fin && v->fin != CP_FIN_ACCEPT) + rb_iter_break(); + return Qnil; +} + +static VALUE +assert_array(a) + VALUE a; +{ + Check_Type(a, T_ARRAY); + return a; +} + +static VALUE +assert_hash(h) + VALUE h; +{ + Check_Type(h, T_HASH); + return h; +} + +static long +assert_integer(n) + VALUE n; +{ + return NUM2LONG(n); +} + +static void +initialize_params(v, parser, arg, lexer, lexmid) + struct cparse_params *v; + VALUE parser, arg, lexer, lexmid; +{ + v->value_v = Data_Wrap_Struct(CparseParams, 0, 0, v); + + v->parser = parser; + v->lexer = lexer; + if (! NIL_P(lexmid)) + v->lexmid = value_to_id(lexmid); + + v->debug = RTEST(rb_ivar_get(parser, id_yydebug)); + + Check_Type(arg, T_ARRAY); + if (!(13 <= RARRAY(arg)->len && RARRAY(arg)->len <= 14)) + rb_raise(RaccBug, "[Racc Bug] wrong arg.size %ld", RARRAY(arg)->len); + v->action_table = assert_array (RARRAY(arg)->ptr[ 0]); + v->action_check = assert_array (RARRAY(arg)->ptr[ 1]); + v->action_default = assert_array (RARRAY(arg)->ptr[ 2]); + v->action_pointer = assert_array (RARRAY(arg)->ptr[ 3]); + v->goto_table = assert_array (RARRAY(arg)->ptr[ 4]); + v->goto_check = assert_array (RARRAY(arg)->ptr[ 5]); + v->goto_default = assert_array (RARRAY(arg)->ptr[ 6]); + v->goto_pointer = assert_array (RARRAY(arg)->ptr[ 7]); + v->nt_base = assert_integer(RARRAY(arg)->ptr[ 8]); + v->reduce_table = assert_array (RARRAY(arg)->ptr[ 9]); + v->token_table = assert_hash (RARRAY(arg)->ptr[10]); + v->shift_n = assert_integer(RARRAY(arg)->ptr[11]); + v->reduce_n = assert_integer(RARRAY(arg)->ptr[12]); + if (RARRAY(arg)->len > 13) { + v->use_result_var = RTEST(RARRAY(arg)->ptr[13]); + } + else { + v->use_result_var = Qtrue; + } + + v->tstack = v->debug ? NEW_STACK() : Qnil; + v->vstack = NEW_STACK(); + v->state = NEW_STACK(); + v->curstate = 0; + PUSH(v->state, INT2FIX(0)); + v->t = INT2FIX(FINAL_TOKEN + 1); /* must not init to FINAL_TOKEN */ + v->nerr = 0; + v->errstatus = 0; + rb_ivar_set(parser, id_errstatus, LONG2NUM(v->errstatus)); + + v->retval = Qnil; + v->fin = 0; + + v->lex_is_iterator = Qfalse; +} + +static void +extract_user_token(v, block_args, tok, val) + struct cparse_params *v; + VALUE block_args; + VALUE *tok, *val; +{ + if (NIL_P(block_args)) { + /* EOF */ + *tok = Qfalse; + *val = rb_str_new("$", 1); + return; + } + + if (TYPE(block_args) != T_ARRAY) { + rb_raise(rb_eTypeError, + "%s() %s %s (must be Array[2])", + v->lex_is_iterator ? rb_id2name(v->lexmid) : "next_token", + v->lex_is_iterator ? "yielded" : "returned", + rb_class2name(CLASS_OF(block_args))); + } + if (RARRAY(block_args)->len != 2) { + rb_raise(rb_eArgError, + "%s() %s wrong size of array (%ld for 2)", + v->lex_is_iterator ? rb_id2name(v->lexmid) : "next_token", + v->lex_is_iterator ? "yielded" : "returned", + RARRAY(block_args)->len); + } + *tok = AREF(block_args, 0); + *val = AREF(block_args, 1); +} + +#define SHIFT(v,act,tok,val) shift(v,act,tok,val) +#define REDUCE(v,act) do {\ + switch (reduce(v,act)) { \ + case 0: /* normal */ \ + break; \ + case 1: /* yyerror */ \ + goto user_yyerror; \ + case 2: /* yyaccept */ \ + D_puts("u accept"); \ + goto accept; \ + default: \ + break; \ + } \ +} while (0) + +static void +parse_main(v, tok, val, resume) + struct cparse_params *v; + VALUE tok, val; + int resume; +{ + long i; /* table index */ + long act; /* action type */ + VALUE act_value; /* action type, VALUE version */ + int read_next = 1; /* true if we need to read next token */ + VALUE tmp; + + if (resume) + goto resume; + + while (1) { + D_puts(""); + D_puts("---- enter new loop ----"); + D_puts(""); + + D_printf("(act) k1=%ld\n", v->curstate); + tmp = AREF(v->action_pointer, v->curstate); + if (NIL_P(tmp)) goto notfound; + D_puts("(act) pointer[k1] ok"); + i = NUM2LONG(tmp); + + D_printf("read_next=%d\n", read_next); + if (read_next && (v->t != vFINAL_TOKEN)) { + if (v->lex_is_iterator) { + D_puts("resuming..."); + if (v->fin) rb_raise(rb_eArgError, "token given after EOF"); + v->i = i; /* save i */ + return; + resume: + D_puts("resumed"); + i = v->i; /* load i */ + } + else { + D_puts("next_token"); + tmp = rb_funcall(v->parser, id_nexttoken, 0); + extract_user_token(v, tmp, &tok, &val); + } + /* convert token */ + tmp = rb_hash_aref(v->token_table, tok); + v->t = NIL_P(tmp) ? vERROR_TOKEN : tmp; + D_printf("(act) t(k2)=%ld\n", NUM2LONG(v->t)); + if (v->debug) { + rb_funcall(v->parser, id_d_read_token, + 3, v->t, tok, val); + } + } + read_next = 0; + + i += NUM2LONG(v->t); + D_printf("(act) i=%ld\n", i); + if (i < 0) goto notfound; + + act_value = AREF(v->action_table, i); + if (NIL_P(act_value)) goto notfound; + act = NUM2LONG(act_value); + D_printf("(act) table[i]=%ld\n", act); + + tmp = AREF(v->action_check, i); + if (NIL_P(tmp)) goto notfound; + if (NUM2LONG(tmp) != v->curstate) goto notfound; + D_printf("(act) check[i]=%ld\n", NUM2LONG(tmp)); + + D_puts("(act) found"); + act_fixed: + D_printf("act=%ld\n", act); + goto handle_act; + + notfound: + D_puts("(act) not found: use default"); + act_value = AREF(v->action_default, v->curstate); + act = NUM2LONG(act_value); + goto act_fixed; + + + handle_act: + if (act > 0 && act < v->shift_n) { + D_puts("shift"); + if (v->errstatus > 0) { + v->errstatus--; + rb_ivar_set(v->parser, id_errstatus, LONG2NUM(v->errstatus)); + } + SHIFT(v, act, v->t, val); + read_next = 1; + } + else if (act < 0 && act > -(v->reduce_n)) { + D_puts("reduce"); + REDUCE(v, act); + } + else if (act == -(v->reduce_n)) { + goto error; + error_recovered: + ; /* goto label requires stmt */ + } + else if (act == v->shift_n) { + D_puts("accept"); + goto accept; + } + else { + rb_raise(RaccBug, "[Racc Bug] unknown act value %ld", act); + } + + if (v->debug) { + rb_funcall(v->parser, id_d_next_state, + 2, LONG2NUM(v->curstate), v->state); + } + } + /* not reach */ + + + accept: + if (v->debug) rb_funcall(v->parser, id_d_accept, 0); + v->retval = RARRAY(v->vstack)->ptr[0]; + v->fin = CP_FIN_ACCEPT; + return; + + + error: + D_printf("error detected, status=%ld\n", v->errstatus); + if (v->errstatus == 0) { + v->nerr++; + rb_funcall(v->parser, id_onerror, + 3, v->t, val, v->vstack); + } + user_yyerror: + if (v->errstatus == 3) { + if (v->t == vFINAL_TOKEN) { + v->retval = Qfalse; + v->fin = CP_FIN_EOT; + return; + } + read_next = 1; + } + v->errstatus = 3; + rb_ivar_set(v->parser, id_errstatus, LONG2NUM(v->errstatus)); + + /* check if we can shift/reduce error token */ + D_printf("(err) k1=%ld\n", v->curstate); + D_printf("(err) k2=%d (error)\n", ERROR_TOKEN); + while (1) { + tmp = AREF(v->action_pointer, v->curstate); + if (NIL_P(tmp)) goto error_pop; + D_puts("(err) pointer[k1] ok"); + + i = NUM2LONG(tmp) + ERROR_TOKEN; + D_printf("(err) i=%ld\n", i); + if (i < 0) goto error_pop; + + act_value = AREF(v->action_table, i); + if (NIL_P(act_value)) { + D_puts("(err) table[i] == nil"); + goto error_pop; + } + act = NUM2LONG(act_value); + D_printf("(err) table[i]=%ld\n", act); + + tmp = AREF(v->action_check, i); + if (NIL_P(tmp)) { + D_puts("(err) check[i] == nil"); + goto error_pop; + } + if (NUM2LONG(tmp) != v->curstate) { + D_puts("(err) check[i] != k1"); + goto error_pop; + } + + D_puts("(err) found: can handle error token"); + break; + + error_pop: + D_puts("(err) act not found: can't handle error token; pop"); + + if (RARRAY(v->state)->len <= 1) { + v->retval = Qnil; + v->fin = CP_FIN_CANTPOP; + return; + } + POP(v->state); + POP(v->vstack); + v->curstate = num_to_long(LAST_I(v->state)); + if (v->debug) { + POP(v->tstack); + rb_funcall(v->parser, id_d_e_pop, + 3, v->state, v->tstack, v->vstack); + } + } + + /* shift/reduce error token */ + if (act > 0 && act < v->shift_n) { + D_puts("e shift"); + SHIFT(v, act, ERROR_TOKEN, val); + } + else if (act < 0 && act > -(v->reduce_n)) { + D_puts("e reduce"); + REDUCE(v, act); + } + else if (act == v->shift_n) { + D_puts("e accept"); + goto accept; + } + else { + rb_raise(RaccBug, "[Racc Bug] unknown act value %ld", act); + } + goto error_recovered; +} + +static void +shift(v, act, tok, val) + struct cparse_params *v; + long act; + VALUE tok, val; +{ + PUSH(v->vstack, val); + if (v->debug) { + PUSH(v->tstack, tok); + rb_funcall(v->parser, id_d_shift, + 3, tok, v->tstack, v->vstack); + } + v->curstate = act; + PUSH(v->state, LONG2NUM(v->curstate)); +} + +static int +reduce(v, act) + struct cparse_params *v; + long act; +{ + VALUE code; + v->ruleno = -act * 3; + code = rb_iterate(catch_iter, Qnil, reduce0, v->value_v); + v->errstatus = num_to_long(rb_ivar_get(v->parser, id_errstatus)); + return NUM2INT(code); +} + +static VALUE +catch_iter(dummy) + VALUE dummy; +{ + return rb_funcall(rb_mKernel, id_catch, 1, sym_raccjump); +} + +static VALUE +reduce0(val, data, self) + VALUE val, data, self; +{ + struct cparse_params *v; + VALUE reduce_to, reduce_len, method_id; + long len; + ID mid; + VALUE tmp, tmp_t, tmp_v; + long i, k1, k2; + VALUE goto_state; + + Data_Get_Struct(data, struct cparse_params, v); + reduce_len = RARRAY(v->reduce_table)->ptr[v->ruleno]; + reduce_to = RARRAY(v->reduce_table)->ptr[v->ruleno+1]; + method_id = RARRAY(v->reduce_table)->ptr[v->ruleno+2]; + len = NUM2LONG(reduce_len); + mid = value_to_id(method_id); + + /* call action */ + if (len == 0) { + tmp = Qnil; + if (mid != id_noreduce) + tmp_v = rb_ary_new(); + if (v->debug) + tmp_t = rb_ary_new(); + } + else { + if (mid != id_noreduce) { + tmp_v = GET_TAIL(v->vstack, len); + tmp = RARRAY(tmp_v)->ptr[0]; + } + else { + tmp = RARRAY(v->vstack)->ptr[ RARRAY(v->vstack)->len - len ]; + } + CUT_TAIL(v->vstack, len); + if (v->debug) { + tmp_t = GET_TAIL(v->tstack, len); + CUT_TAIL(v->tstack, len); + } + CUT_TAIL(v->state, len); + } + if (mid != id_noreduce) { + if (v->use_result_var) { + tmp = rb_funcall(v->parser, mid, + 3, tmp_v, v->vstack, tmp); + } + else { + tmp = rb_funcall(v->parser, mid, + 2, tmp_v, v->vstack); + } + } + + /* then push result */ + PUSH(v->vstack, tmp); + if (v->debug) { + PUSH(v->tstack, reduce_to); + rb_funcall(v->parser, id_d_reduce, + 4, tmp_t, reduce_to, v->tstack, v->vstack); + } + + /* calculate transition state */ + if (RARRAY(v->state)->len == 0) + rb_raise(RaccBug, "state stack unexpectedly empty"); + k2 = num_to_long(LAST_I(v->state)); + k1 = num_to_long(reduce_to) - v->nt_base; + D_printf("(goto) k1=%ld\n", k1); + D_printf("(goto) k2=%ld\n", k2); + + tmp = AREF(v->goto_pointer, k1); + if (NIL_P(tmp)) goto notfound; + + i = NUM2LONG(tmp) + k2; + D_printf("(goto) i=%ld\n", i); + if (i < 0) goto notfound; + + goto_state = AREF(v->goto_table, i); + if (NIL_P(goto_state)) { + D_puts("(goto) table[i] == nil"); + goto notfound; + } + D_printf("(goto) table[i]=%ld (goto_state)\n", NUM2LONG(goto_state)); + + tmp = AREF(v->goto_check, i); + if (NIL_P(tmp)) { + D_puts("(goto) check[i] == nil"); + goto notfound; + } + if (tmp != LONG2NUM(k1)) { + D_puts("(goto) check[i] != table[i]"); + goto notfound; + } + D_printf("(goto) check[i]=%ld\n", NUM2LONG(tmp)); + + D_puts("(goto) found"); + transit: + PUSH(v->state, goto_state); + v->curstate = NUM2LONG(goto_state); + return INT2FIX(0); + + notfound: + D_puts("(goto) not found: use default"); + /* overwrite `goto-state' by default value */ + goto_state = AREF(v->goto_default, k1); + goto transit; +} + + +/* ----------------------------------------------------------------------- + Ruby Interface +----------------------------------------------------------------------- */ + +void +Init_cparse() +{ + VALUE Racc, Parser; + ID id_racc = rb_intern("Racc"); + + if (rb_const_defined(rb_cObject, id_racc)) { + Racc = rb_const_get(rb_cObject, id_racc); + Parser = rb_const_get_at(Racc, rb_intern("Parser")); + } + else { + Racc = rb_define_module("Racc"); + Parser = rb_define_class_under(Racc, "Parser", rb_cObject); + } + rb_define_private_method(Parser, "_racc_do_parse_c", racc_cparse, 2); + rb_define_private_method(Parser, "_racc_yyparse_c", racc_yyparse, 4); + rb_define_const(Parser, "Racc_Runtime_Core_Version_C", + rb_str_new2(RACC_VERSION)); + rb_define_const(Parser, "Racc_Runtime_Core_Id_C", + rb_str_new2("$raccId: cparse.c,v 1.3 2003/11/03 12:20:54 aamine Exp $")); + + CparseParams = rb_define_class_under(Racc, "CparseParams", rb_cObject); + + RaccBug = rb_eRuntimeError; + + id_yydebug = rb_intern("@yydebug"); + id_nexttoken = rb_intern("next_token"); + id_onerror = rb_intern("on_error"); + id_noreduce = rb_intern("_reduce_none"); + id_catch = rb_intern("catch"); + id_errstatus = rb_intern("@racc_error_status"); + sym_raccjump = id_to_value(rb_intern("racc_jump")); + + id_d_shift = rb_intern("racc_shift"); + id_d_reduce = rb_intern("racc_reduce"); + id_d_accept = rb_intern("racc_accept"); + id_d_read_token = rb_intern("racc_read_token"); + id_d_next_state = rb_intern("racc_next_state"); + id_d_e_pop = rb_intern("racc_e_pop"); +} Added: trunk/ext/racc/cparse/depend =================================================================== --- trunk/ext/racc/cparse/depend 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/racc/cparse/depend 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1 @@ +cparse.o: cparse.c $(hdrdir)/ruby.h $(topdir)/config.h $(hdrdir)/defines.h Added: trunk/ext/racc/cparse/extconf.rb =================================================================== --- trunk/ext/racc/cparse/extconf.rb 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/racc/cparse/extconf.rb 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,4 @@ +# $Id: extconf.rb,v 1.1 2002/03/22 07:20:31 aamine Exp $ + +require 'mkmf' +create_makefile 'racc/cparse' Added: trunk/ext/syslog/.cvsignore =================================================================== --- trunk/ext/syslog/.cvsignore 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/syslog/.cvsignore 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,3 @@ +Makefile +mkmf.log +*.def Added: trunk/ext/syslog/depend =================================================================== --- trunk/ext/syslog/depend 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/syslog/depend 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,2 @@ +syslog.o: syslog.c $(hdrdir)/ruby.h $(topdir)/config.h $(hdrdir)/defines.h \ + $(hdrdir)/intern.h Added: trunk/ext/syslog/extconf.rb =================================================================== --- trunk/ext/syslog/extconf.rb 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/syslog/extconf.rb 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,10 @@ +# $RoughId: extconf.rb,v 1.3 2001/11/24 17:49:26 knu Exp $ +# $Id: extconf.rb,v 1.1 2001/11/26 12:00:40 knu Exp $ + +require 'mkmf' + +have_header("syslog.h") && + have_func("openlog") && + have_func("setlogmask") && + create_makefile("syslog") + Added: trunk/ext/syslog/syslog.c =================================================================== --- trunk/ext/syslog/syslog.c 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/syslog/syslog.c 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,394 @@ +/* + * UNIX Syslog extension for Ruby + * Amos Gouaux, University of Texas at Dallas + * <amos+ruby utdallas.edu> + * + * $RoughId: syslog.c,v 1.21 2002/02/25 12:21:17 knu Exp $ + * $Id: syslog.c,v 1.9 2004/03/29 07:54:25 matz Exp $ + */ + +#include "ruby.h" +#include "util.h" +#include <syslog.h> + +/* Syslog class */ +static VALUE mSyslog, mSyslogConstants; +static const char *syslog_ident = NULL; +static int syslog_options = -1, syslog_facility = -1, syslog_mask = -1; +static int syslog_opened = 0; + +/* Package helper routines */ +static void syslog_write(int pri, int argc, VALUE *argv) +{ + VALUE str; + + if (argc < 1) { + rb_raise(rb_eArgError, "no log message supplied"); + } + + if (!syslog_opened) { + rb_raise(rb_eRuntimeError, "must open syslog before write"); + } + + str = rb_f_sprintf(argc, argv); + + syslog(pri, "%s", RSTRING(str)->ptr); +} + +/* Syslog module methods */ +static VALUE mSyslog_close(VALUE self) +{ + if (!syslog_opened) { + rb_raise(rb_eRuntimeError, "syslog not opened"); + } + + closelog(); + + free((void *)syslog_ident); + syslog_ident = NULL; + syslog_options = syslog_facility = syslog_mask = -1; + syslog_opened = 0; + + return Qnil; +} + +static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self) +{ + VALUE ident, opt, fac; + + if (syslog_opened) { + rb_raise(rb_eRuntimeError, "syslog already open"); + } + + rb_scan_args(argc, argv, "03", &ident, &opt, &fac); + + if (NIL_P(ident)) { + ident = rb_gv_get("$0"); + } +#ifdef SafeStringValue + SafeStringValue(ident); +#else + Check_SafeStr(ident); +#endif + syslog_ident = strdup(RSTRING(ident)->ptr); + + if (NIL_P(opt)) { + syslog_options = LOG_PID | LOG_CONS; + } else { + syslog_options = NUM2INT(opt); + } + + if (NIL_P(fac)) { + syslog_facility = LOG_USER; + } else { + syslog_facility = NUM2INT(fac); + } + + openlog(syslog_ident, syslog_options, syslog_facility); + + syslog_opened = 1; + + setlogmask(syslog_mask = setlogmask(0)); + + /* be like File.new.open {...} */ + if (rb_block_given_p()) { + rb_ensure(rb_yield, self, mSyslog_close, self); + } + + return self; +} + +static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self) +{ + mSyslog_close(self); + + return mSyslog_open(argc, argv, self); +} + +static VALUE mSyslog_isopen(VALUE self) +{ + return syslog_opened ? Qtrue : Qfalse; +} + +static VALUE mSyslog_ident(VALUE self) +{ + return syslog_opened ? rb_str_new2(syslog_ident) : Qnil; +} + +static VALUE mSyslog_options(VALUE self) +{ + return syslog_opened ? INT2NUM(syslog_options) : Qnil; +} + +static VALUE mSyslog_facility(VALUE self) +{ + return syslog_opened ? INT2NUM(syslog_facility) : Qnil; +} + +static VALUE mSyslog_get_mask(VALUE self) +{ + return syslog_opened ? INT2NUM(syslog_mask) : Qnil; +} + +static VALUE mSyslog_set_mask(VALUE self, VALUE mask) +{ + if (!syslog_opened) { + rb_raise(rb_eRuntimeError, "must open syslog before setting log mask"); + } + + setlogmask(syslog_mask = NUM2INT(mask)); + + return mask; +} + +static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self) +{ + VALUE pri; + + if (argc < 2) { + rb_raise(rb_eArgError, "wrong number of arguments (%d for 2+)", argc); + } + + argc--; + pri = *argv++; + + if (!FIXNUM_P(pri)) { + rb_raise(rb_eTypeError, "type mismatch: %s given", rb_class2name(CLASS_OF(pri))); + } + + syslog_write(FIX2INT(pri), argc, argv); + + return self; +} + +static VALUE mSyslog_inspect(VALUE self) +{ + char buf[1024]; + + if (syslog_opened) { + snprintf(buf, sizeof(buf), + "<#%s: opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>", + rb_class2name(self), + syslog_ident, + syslog_options, + syslog_facility, + syslog_mask); + } else { + snprintf(buf, sizeof(buf), + "<#%s: opened=false>", rb_class2name(self)); + } + + return rb_str_new2(buf); +} + +static VALUE mSyslog_instance(VALUE self) +{ + return self; +} + +#define define_syslog_shortcut_method(pri, name) \ +static VALUE mSyslog_##name(int argc, VALUE *argv, VALUE self) \ +{ \ + syslog_write(pri, argc, argv); \ +\ + return self; \ +} + +#ifdef LOG_EMERG +define_syslog_shortcut_method(LOG_EMERG, emerg) +#endif +#ifdef LOG_ALERT +define_syslog_shortcut_method(LOG_ALERT, alert) +#endif +#ifdef LOG_CRIT +define_syslog_shortcut_method(LOG_CRIT, crit) +#endif +#ifdef LOG_ERR +define_syslog_shortcut_method(LOG_ERR, err) +#endif +#ifdef LOG_WARNING +define_syslog_shortcut_method(LOG_WARNING, warning) +#endif +#ifdef LOG_NOTICE +define_syslog_shortcut_method(LOG_NOTICE, notice) +#endif +#ifdef LOG_INFO +define_syslog_shortcut_method(LOG_INFO, info) +#endif +#ifdef LOG_DEBUG +define_syslog_shortcut_method(LOG_DEBUG, debug) +#endif + +static VALUE mSyslogConstants_LOG_MASK(VALUE klass, VALUE pri) +{ + return INT2FIX(LOG_MASK(FIX2INT(pri))); +} + +static VALUE mSyslogConstants_LOG_UPTO(VALUE klass, VALUE pri) +{ + return INT2FIX(LOG_UPTO(FIX2INT(pri))); +} + +/* Init for package syslog */ +void Init_syslog() +{ + mSyslog = rb_define_module("Syslog"); + + mSyslogConstants = rb_define_module_under(mSyslog, "Constants"); + + rb_include_module(mSyslog, mSyslogConstants); + + rb_define_module_function(mSyslog, "open", mSyslog_open, -1); + rb_define_module_function(mSyslog, "reopen", mSyslog_reopen, -1); + rb_define_module_function(mSyslog, "open!", mSyslog_reopen, -1); + rb_define_module_function(mSyslog, "opened?", mSyslog_isopen, 0); + + rb_define_module_function(mSyslog, "ident", mSyslog_ident, 0); + rb_define_module_function(mSyslog, "options", mSyslog_options, 0); + rb_define_module_function(mSyslog, "facility", mSyslog_facility, 0); + + rb_define_module_function(mSyslog, "log", mSyslog_log, -1); + rb_define_module_function(mSyslog, "close", mSyslog_close, 0); + rb_define_module_function(mSyslog, "mask", mSyslog_get_mask, 0); + rb_define_module_function(mSyslog, "mask=", mSyslog_set_mask, 1); + + rb_define_module_function(mSyslog, "LOG_MASK", mSyslogConstants_LOG_MASK, 1); + rb_define_module_function(mSyslog, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1); + + rb_define_module_function(mSyslog, "inspect", mSyslog_inspect, 0); + rb_define_module_function(mSyslog, "instance", mSyslog_instance, 0); + + rb_define_module_function(mSyslogConstants, "LOG_MASK", mSyslogConstants_LOG_MASK, 1); + rb_define_module_function(mSyslogConstants, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1); + +#define rb_define_syslog_const(id) \ + rb_define_const(mSyslogConstants, #id, INT2NUM(id)) + + /* Various options when opening log */ +#ifdef LOG_PID + rb_define_syslog_const(LOG_PID); +#endif +#ifdef LOG_CONS + rb_define_syslog_const(LOG_CONS); +#endif +#ifdef LOG_ODELAY + rb_define_syslog_const(LOG_ODELAY); /* deprecated */ +#endif +#ifdef LOG_NDELAY + rb_define_syslog_const(LOG_NDELAY); +#endif +#ifdef LOG_NOWAIT + rb_define_syslog_const(LOG_NOWAIT); /* deprecated */ +#endif +#ifdef LOG_PERROR + rb_define_syslog_const(LOG_PERROR); +#endif + + /* Various syslog facilities */ +#ifdef LOG_AUTH + rb_define_syslog_const(LOG_AUTH); +#endif +#ifdef LOG_AUTHPRIV + rb_define_syslog_const(LOG_AUTHPRIV); +#endif +#ifdef LOG_CONSOLE + rb_define_syslog_const(LOG_CONSOLE); +#endif +#ifdef LOG_CRON + rb_define_syslog_const(LOG_CRON); +#endif +#ifdef LOG_DAEMON + rb_define_syslog_const(LOG_DAEMON); +#endif +#ifdef LOG_FTP + rb_define_syslog_const(LOG_FTP); +#endif +#ifdef LOG_KERN + rb_define_syslog_const(LOG_KERN); +#endif +#ifdef LOG_LPR + rb_define_syslog_const(LOG_LPR); +#endif +#ifdef LOG_MAIL + rb_define_syslog_const(LOG_MAIL); +#endif +#ifdef LOG_NEWS + rb_define_syslog_const(LOG_NEWS); +#endif +#ifdef LOG_NTP + rb_define_syslog_const(LOG_NTP); +#endif +#ifdef LOG_SECURITY + rb_define_syslog_const(LOG_SECURITY); +#endif +#ifdef LOG_SYSLOG + rb_define_syslog_const(LOG_SYSLOG); +#endif +#ifdef LOG_USER + rb_define_syslog_const(LOG_USER); +#endif +#ifdef LOG_UUCP + rb_define_syslog_const(LOG_UUCP); +#endif +#ifdef LOG_LOCAL0 + rb_define_syslog_const(LOG_LOCAL0); +#endif +#ifdef LOG_LOCAL1 + rb_define_syslog_const(LOG_LOCAL1); +#endif +#ifdef LOG_LOCAL2 + rb_define_syslog_const(LOG_LOCAL2); +#endif +#ifdef LOG_LOCAL3 + rb_define_syslog_const(LOG_LOCAL3); +#endif +#ifdef LOG_LOCAL4 + rb_define_syslog_const(LOG_LOCAL4); +#endif +#ifdef LOG_LOCAL5 + rb_define_syslog_const(LOG_LOCAL5); +#endif +#ifdef LOG_LOCAL6 + rb_define_syslog_const(LOG_LOCAL6); +#endif +#ifdef LOG_LOCAL7 + rb_define_syslog_const(LOG_LOCAL7); +#endif + +#define rb_define_syslog_shortcut(name) \ + rb_define_module_function(mSyslog, #name, mSyslog_##name, -1) + + /* Various syslog priorities and the shortcut methods */ +#ifdef LOG_EMERG + rb_define_syslog_const(LOG_EMERG); + rb_define_syslog_shortcut(emerg); +#endif +#ifdef LOG_ALERT + rb_define_syslog_const(LOG_ALERT); + rb_define_syslog_shortcut(alert); +#endif +#ifdef LOG_CRIT + rb_define_syslog_const(LOG_CRIT); + rb_define_syslog_shortcut(crit); +#endif +#ifdef LOG_ERR + rb_define_syslog_const(LOG_ERR); + rb_define_syslog_shortcut(err); +#endif +#ifdef LOG_WARNING + rb_define_syslog_const(LOG_WARNING); + rb_define_syslog_shortcut(warning); +#endif +#ifdef LOG_NOTICE + rb_define_syslog_const(LOG_NOTICE); + rb_define_syslog_shortcut(notice); +#endif +#ifdef LOG_INFO + rb_define_syslog_const(LOG_INFO); + rb_define_syslog_shortcut(info); +#endif +#ifdef LOG_DEBUG + rb_define_syslog_const(LOG_DEBUG); + rb_define_syslog_shortcut(debug); +#endif +} Added: trunk/ext/syslog/syslog.txt =================================================================== --- trunk/ext/syslog/syslog.txt 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/syslog/syslog.txt 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,121 @@ +.\" syslog.txt - -*- Indented-Text -*- +$RoughId: syslog.txt,v 1.18 2002/02/25 08:20:14 knu Exp $ +$Id: syslog.txt,v 1.2 2002/02/25 12:13:30 knu Exp $ + +UNIX Syslog extension for Ruby +Amos Gouaux, University of Texas at Dallas +<amos+ruby utdallas.edu> +& +Akinori MUSHA +<knu ruby-lang.org> + +** Syslog(Module) + +Included Modules: Syslog::Constants + +require 'syslog' + +A Simple wrapper for the UNIX syslog system calls that might be handy +if you're writing a server in Ruby. For the details of the syslog(8) +architecture and constants, see the syslog(3) manual page of your +platform. + +Module Methods: + + open(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, + facility = Syslog::LOG_USER) [{ |syslog| ... }] + + Opens syslog with the given options and returns the module + itself. If a block is given, calls it with an argument of + itself. If syslog is already opened, raises RuntimeError. + + Example: + Syslog.open('ftpd', Syslog::LOG_PID | Syslog::LOG_NDELAY, + Syslog::LOG_FTP) + + open!(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, + facility = Syslog::LOG_USER) + reopen(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, + facility = Syslog::LOG_USER) + + Same as open, but does a close first. + + opened? + + Returns true if syslog opened, otherwise false. + + ident + options + facility + + Returns the parameters given in the last open, respectively. + Every call of Syslog::open resets these values. + + log(pri, message, ...) + + Writes message to syslog. + + Example: + Syslog.log(Syslog::LOG_CRIT, "the sky is falling in %d seconds!", 10) + + crit(message, ...) + emerg(message, ...) + alert(message, ...) + err(message, ...) + warning(message, ...) + notice(message, ...) + info(message, ...) + debug(message, ...) + + These are shortcut methods of Syslog::log(). The lineup may + vary depending on what priorities are defined on your system. + + Example: + Syslog.crit("the sky is falling in %d seconds!", 5) + + mask + mask=(mask) + + Returns or sets the log priority mask. The value of the mask + is persistent and will not be reset by Syslog::open or + Syslog::close. + + Example: + Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR) + + close + + Closes syslog. + + inspect + + Returns the "inspect" string of the Syslog module. + + instance + + Returns the module itself. (Just for backward compatibility) + + LOG_MASK(pri) + + Creates a mask for one priority. + + LOG_UPTO(pri) + + Creates a mask for all priorities up to pri. + +** Syslog::Constants(Module) + +require 'syslog' +include Syslog::Constants + +This module includes the LOG_* constants available on the system. + +Module Methods: + + LOG_MASK(pri) + + Creates a mask for one priority. + + LOG_UPTO(pri) + + Creates a mask for all priorities up to pri. Added: trunk/ext/syslog/test.rb =================================================================== --- trunk/ext/syslog/test.rb 2006-02-27 03:47:20 UTC (rev 478) +++ trunk/ext/syslog/test.rb 2006-02-27 04:55:05 UTC (rev 479) @@ -0,0 +1,164 @@ +#!/usr/bin/env ruby +# $RoughId: test.rb,v 1.9 2002/02/25 08:20:14 knu Exp $ +# $Id: test.rb,v 1.4 2002/11/27 08:36:22 knu Exp $ + +# Please only run this test on machines reasonable for testing. +# If in doubt, ask your admin. + +require 'test/unit' + +# Prepend current directory to load path for testing. +$:.unshift('.') + +require 'syslog' + +class TestSyslog < Test::Unit::TestCase + def test_new + assert_raises(NoMethodError) { + Syslog.new + } + end + + def test_instance + sl1 = Syslog.instance + sl2 = Syslog.open + sl3 = Syslog.instance + + assert_equal(Syslog, sl1) + assert_equal(Syslog, sl2) + assert_equal(Syslog, sl3) + ensure + Syslog.close if Syslog.opened? + end + + def test_open + # default parameters + Syslog.open + + assert_equal($0, Syslog.ident) + assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, Syslog.options) + assert_equal(Syslog::LOG_USER, Syslog.facility) + + # open without close + assert_raises(RuntimeError) { + Syslog.open + } + + Syslog.close + + # given parameters + Syslog.open("foo", Syslog::LOG_NDELAY | Syslog::LOG_PERROR, Syslog::LOG_DAEMON) + + assert_equal('foo', Syslog.ident) + assert_equal(Syslog::LOG_NDELAY | Syslog::LOG_PERROR, Syslog.options) + assert_equal(Syslog::LOG_DAEMON, Syslog.facility) + + Syslog.close + + # default parameters again (after close) + Syslog.open + Syslog.close + + assert_equal(nil, Syslog.ident) + assert_equal(nil, Syslog.options) + assert_equal(nil, Syslog.facility) + + # block + param = nil + Syslog.open { |param| } + assert_equal(Syslog, param) + ensure + Syslog.close if Syslog.opened? + end + + def test_opened? + assert_equal(false, Syslog.opened?) + + Syslog.open + assert_equal(true, Syslog.opened?) + + Syslog.close + assert_equal(false, Syslog.opened?) + + Syslog.open { + assert_equal(true, Syslog.opened?) + } + + assert_equal(false, Syslog.opened?) + end + + def test_close + assert_raises(RuntimeError) { + Syslog.close + } + end + + def test_mask + assert_equal(nil, Syslog.mask) + + Syslog.open + + orig = Syslog.mask + + Syslog.mask = Syslog.LOG_UPTO(Syslog::LOG_ERR) + assert_equal(Syslog.LOG_UPTO(Syslog::LOG_ERR), Syslog.mask) + + Syslog.mask = Syslog.LOG_MASK(Syslog::LOG_CRIT) + assert_equal(Syslog.LOG_MASK(Syslog::LOG_CRIT), Syslog.mask) + + Syslog.mask = orig + ensure + Syslog.close if Syslog.opened? + end + + def test_log + stderr = IO::pipe + + pid = fork { + stderr[0].close + STDERR.reopen(stderr[1]) + stderr[1].close + + options = Syslog::LOG_PERROR | Syslog::LOG_NDELAY + + Syslog.open("syslog_test", options) { |sl| + sl.log(Syslog::LOG_NOTICE, "test1 - hello, %s!", "world") + sl.notice("test1 - hello, %s!", "world") + } + + Syslog.open("syslog_test", options | Syslog::LOG_PID) { |sl| + sl.log(Syslog::LOG_CRIT, "test2 - pid") + sl.crit("test2 - pid") + } + exit! + } + + stderr[1].close + Process.waitpid(pid) + + # LOG_PERROR is not yet implemented on Cygwin. + return if RUBY_PLATFORM =~ /cygwin/ + + 2.times { + assert_equal("syslog_test: test1 - hello, world!\n", stderr[0].gets) + } + + 2.times { + assert_equal(format("syslog_test[%d]: test2 - pid\n", pid), stderr[0].gets) + } + end + + def test_inspect + Syslog.open { |sl| + assert_equal(format('<#%s: opened=true, ident="%s", options=%d, facility=%d, mask=%d>', + Syslog, + sl.ident, + sl.options, + sl.facility, + sl.mask), + sl.inspect) + } + + assert_equal(format('<#%s: opened=false>', Syslog), Syslog.inspect) + end +end -- ML: yarv-diff quickml.atdot.net Info: http://www.atdot.net/~ko1/quickml