--- rxvt-unicode/src/rxvtperl.xs 2006/01/07 04:19:43 1.35 +++ rxvt-unicode/src/rxvtperl.xs 2006/01/09 23:00:07 1.51 @@ -28,15 +28,23 @@ #include "../config.h" +#include #include -#include "rxvt.h" #include "iom.h" +#include "rxvt.h" +#include "keyboard.h" #include "rxvtutil.h" #include "rxvtperl.h" #include "perlxsi.c" +#if defined(HAVE_SCROLLBARS) || defined(MENUBAR) +# define GRAB_CURSOR THIS->leftptr_cursor +#else +# define GRAB_CURSOR None +#endif + #undef LINENO #define LINENO(n) MOD (THIS->term_start + int(n), THIS->total_rows) #undef ROW @@ -44,6 +52,22 @@ ///////////////////////////////////////////////////////////////////////////// +static SV * +taint (SV *sv) +{ + SvTAINT (sv); + return sv; +} + +static SV * +taint_if (SV *sv, SV *src) +{ + if (SvTAINTED (src)) + SvTAINT (sv); + + return sv; +} + static wchar_t * sv2wcs (SV *sv) { @@ -103,7 +127,7 @@ return (long)mg->mg_ptr; } -#define newSVterm(term) SvREFCNT_inc ((SV *)term->self) +#define newSVterm(term) SvREFCNT_inc ((SV *)term->perl.self) #define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term") ///////////////////////////////////////////////////////////////////////////// @@ -292,14 +316,14 @@ { char key[33]; sprintf (key, "%32lx", (long)this); - HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->self), "_overlay", 8, 0)); + HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0)); hv_store (hv, key, 32, newSViv ((long)this), 0); } void overlay::hide () { - SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->self), "_overlay", 8, 0); + SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0); if (ovs) { @@ -391,13 +415,14 @@ { char *argv[] = { "", + "-T", "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1", }; perl = perl_alloc (); perl_construct (perl); - if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) + if (perl_parse (perl, xs_init, 3, argv, (char **)NULL) || perl_run (perl)) { rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n"); @@ -409,6 +434,17 @@ } } +static void +ungrab (rxvt_term *THIS) +{ + if (THIS->perl.grabtime) + { + XUngrabKeyboard (THIS->display->display, THIS->perl.grabtime); + XUngrabPointer (THIS->display->display, THIS->perl.grabtime); + THIS->perl.grabtime = 0; + } +} + bool rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...) { @@ -417,29 +453,34 @@ if (htype == HOOK_INIT) // first hook ever called { - term->self = (void *)newSVptr ((void *)term, "urxvt::term"); - hv_store ((HV *)SvRV ((SV *)term->self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0); + term->perl.self = (void *)newSVptr ((void *)term, "urxvt::term"); + hv_store ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0); } - else if (!term->self) + else if (!term->perl.self) return false; // perl not initialized for this instance else if (htype == HOOK_DESTROY) { // handled later } - else if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END) + else { - HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->self), "_overlay", 8, 0)); - - if (HvKEYS (hv)) + if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END) { - hv_iterinit (hv); + HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0)); + + if (HvKEYS (hv)) + { + hv_iterinit (hv); + + while (HE *he = hv_iternext (hv)) + ((overlay *)SvIV (hv_iterval (hv, he)))->swap (); + } - while (HE *he = hv_iternext (hv)) - ((overlay *)SvIV (hv_iterval (hv, he)))->swap (); } + + if (!should_invoke [htype]) + return false; } - else if (!should_invoke [htype]) - return false; dSP; va_list ap; @@ -468,7 +509,7 @@ break; case DT_STR: - XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0))); + XPUSHs (taint (sv_2mortal (newSVpv (va_arg (ap, char *), 0)))); break; case DT_STR_LEN: @@ -476,7 +517,7 @@ char *str = va_arg (ap, char *); int len = va_arg (ap, int); - XPUSHs (sv_2mortal (newSVpvn (str, len))); + XPUSHs (taint (sv_2mortal (newSVpvn (str, len)))); } break; @@ -485,7 +526,7 @@ wchar_t *wstr = va_arg (ap, wchar_t *); int wlen = va_arg (ap, int); - XPUSHs (sv_2mortal (wcs2sv (wstr, wlen))); + XPUSHs (taint (sv_2mortal (wcs2sv (wstr, wlen)))); } break; @@ -560,12 +601,15 @@ LEAVE; if (SvTRUE (ERRSV)) - rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV)); + { + rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV)); + ungrab (term); // better lose the grab than the session + } if (htype == HOOK_DESTROY) { - clearSVptr ((SV *)term->self); - SvREFCNT_dec ((SV *)term->self); + clearSVptr ((SV *)term->perl.self); + SvREFCNT_dec ((SV *)term->perl.self); } return count; @@ -585,21 +629,49 @@ BOOT: { -# define export_const(name) newCONSTSUB (gv_stashpv ("urxvt", 1), # name, newSViv (name)); + sv_setsv (get_sv ("urxvt::LIBDIR", 1), newSVpvn (LIBDIR, sizeof (LIBDIR) - 1)); + AV *hookname = get_av ("urxvt::HOOKNAME", 1); # define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0)); # include "hookinc.h" # undef def - export_const (DEFAULT_RSTYLE); - export_const (OVERLAY_RSTYLE); - export_const (RS_Bold); - export_const (RS_Italic); - export_const (RS_Blink); - export_const (RS_RVid); - export_const (RS_Uline); + HV *option = get_hv ("urxvt::OPTION", 1); +# define def(name,val) hv_store (option, # name, sizeof (# name) - 1, newSVuv (Opt_ ## name), 0); +# define nodef(name) +# include "optinc.h" +# undef nodef +# undef def - sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR); + HV *stash = gv_stashpv ("urxvt", 1); +# define export_const_iv(name) newCONSTSUB (stash, # name, newSViv (name)); + export_const_iv (DEFAULT_RSTYLE); + export_const_iv (OVERLAY_RSTYLE); + export_const_iv (RS_Bold); + export_const_iv (RS_Italic); + export_const_iv (RS_Blink); + export_const_iv (RS_RVid); + export_const_iv (RS_Uline); + + export_const_iv (CurrentTime); + export_const_iv (ShiftMask); + export_const_iv (LockMask); + export_const_iv (ControlMask); + export_const_iv (Mod1Mask); + export_const_iv (Mod2Mask); + export_const_iv (Mod3Mask); + export_const_iv (Mod4Mask); + export_const_iv (Mod5Mask); + export_const_iv (Button1Mask); + export_const_iv (Button2Mask); + export_const_iv (Button3Mask); + export_const_iv (Button4Mask); + export_const_iv (Button5Mask); + export_const_iv (AnyModifier); + + export_const_iv (EVENT_NONE); + export_const_iv (EVENT_READ); + export_const_iv (EVENT_WRITE); } SV * @@ -627,7 +699,7 @@ croak ("exception caught while initializing new terminal instance"); } - RETVAL = term && term->self ? newSVterm (term) : &PL_sv_undef; + RETVAL = term && term->perl.self ? newSVterm (term) : &PL_sv_undef; } OUTPUT: RETVAL @@ -647,6 +719,21 @@ CODE: rxvt_fatal ("%s", msg); +SV * +untaint (SV *sv) + CODE: + RETVAL = newSVsv (sv); + SvTAINTED_off (RETVAL); + OUTPUT: + RETVAL + +bool +safe () + CODE: + RETVAL = !rxvt_tainted (); + OUTPUT: + RETVAL + NV NOW () CODE: @@ -708,15 +795,53 @@ rxvt_term::destroy () void -rxvt_term::grab (int eventtime) +rxvt_term::grab_button (int button, U32 modifiers) + CODE: + XGrabButton (THIS->display->display, button, modifiers, THIS->vt, 1, + ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask, + GrabModeSync, GrabModeSync, None, GRAB_CURSOR); + +bool +rxvt_term::grab (U32 eventtime, int sync = 0) CODE: { -return; - XGrabPointer (THIS->display->display, THIS->vt, 0, - ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask, - GrabModeAsync, GrabModeAsync, None, None, eventtime); - XGrabKeyboard (THIS->display->display, THIS->vt, 0, GrabModeAsync, GrabModeAsync, eventtime); + int mode = sync ? GrabModeSync : GrabModeAsync; + + THIS->perl.grabtime = 0; + + if (!XGrabPointer (THIS->display->display, THIS->vt, 0, + ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask, + mode, mode, None, GRAB_CURSOR, eventtime)) + if (!XGrabKeyboard (THIS->display->display, THIS->vt, 0, mode, mode, eventtime)) + THIS->perl.grabtime = eventtime; + else + XUngrabPointer (THIS->display->display, eventtime); + + RETVAL = !!THIS->perl.grabtime; } + OUTPUT: + RETVAL + +void +rxvt_term::allow_events_async () + CODE: + XAllowEvents (THIS->display->display, AsyncBoth, THIS->perl.grabtime); + +void +rxvt_term::allow_events_sync () + CODE: + XAllowEvents (THIS->display->display, SyncBoth, THIS->perl.grabtime); + +void +rxvt_term::allow_events_replay () + CODE: + XAllowEvents (THIS->display->display, ReplayPointer, THIS->perl.grabtime); + XAllowEvents (THIS->display->display, ReplayKeyboard, THIS->perl.grabtime); + +void +rxvt_term::ungrab () + CODE: + ungrab (THIS); int rxvt_term::strwidth (SV *str) @@ -745,7 +870,7 @@ free (wstr); - RETVAL = newSVpv (mbstr, 0); + RETVAL = taint_if (newSVpv (mbstr, 0), str); free (mbstr); } OUTPUT: @@ -762,14 +887,13 @@ wchar_t *wstr = rxvt_mbstowcs (data, len); rxvt_pop_locale (); - RETVAL = wcs2sv (wstr); + RETVAL = taint_if (wcs2sv (wstr), octets); free (wstr); } OUTPUT: RETVAL -# very portable, especially on objects as opposed to pods -#define TERM_OFFSET(sym) (((char *)&((TermWin_t *)0)->sym) - (char *)(TermWin_t *)0) +#define TERM_OFFSET(sym) offsetof (TermWin_t, sym) #define TERM_OFFSET_width TERM_OFFSET(width) #define TERM_OFFSET_height TERM_OFFSET(height) @@ -804,6 +928,45 @@ OUTPUT: RETVAL +unsigned int +rxvt_term::ModLevel3Mask () + ALIAS: + ModLevel3Mask = 0 + ModMetaMask = 1 + ModNumLockMask = 2 + CODE: + switch (ix) + { + case 0: RETVAL = THIS->ModLevel3Mask; break; + case 1: RETVAL = THIS->ModMetaMask; break; + case 2: RETVAL = THIS->ModNumLockMask; break; + } + OUTPUT: + RETVAL + +char * +rxvt_term::display_id () + ALIAS: + display_id = 0 + locale = 1 + CODE: + switch (ix) + { + case 0: RETVAL = THIS->display->id; break; + case 1: RETVAL = THIS->locale; break; + } + OUTPUT: + RETVAL + +int +rxvt_term::pty_ev_events (int events = EVENT_UNDEF) + CODE: + RETVAL = THIS->pty_ev.events; + if (events != EVENT_UNDEF) + THIS->pty_ev.set (events); + OUTPUT: + RETVAL + U32 rxvt_term::parent () CODE: @@ -864,7 +1027,7 @@ for (int col = 0; col < THIS->ncol; col++) wstr [col] = l.t [col]; - XPUSHs (sv_2mortal (wcs2sv (wstr, THIS->ncol))); + XPUSHs (taint (sv_2mortal (wcs2sv (wstr, THIS->ncol)))); delete [] wstr; } @@ -995,7 +1158,7 @@ rxvt_pop_locale (); - RETVAL = wcs2sv (rstr, r - rstr); + RETVAL = taint_if (wcs2sv (rstr, r - rstr), string); delete [] rstr; } @@ -1031,7 +1194,7 @@ else *r++ = *s; - RETVAL = wcs2sv (rstr, r - rstr); + RETVAL = taint_if (wcs2sv (rstr, r - rstr), text); delete [] rstr; } @@ -1063,7 +1226,7 @@ croak ("requested out-of-bound resource %s+%d,", name, index - rs->value); if (GIMME_V != G_VOID) - XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef); + XPUSHs (THIS->rs [index] ? sv_2mortal (taint (newSVpv (THIS->rs [index], 0))) : &PL_sv_undef); if (newval) { @@ -1078,8 +1241,52 @@ } } +bool +rxvt_term::option (U32 optval, int set = -1) + CODE: +{ + RETVAL = THIS->options & optval; + + if (set >= 0) + { + if (set) + THIS->options |= optval; + else + THIS->options &= ~optval; + + switch (optval) + { + case Opt_skipBuiltinGlyphs: + THIS->set_fonts (); + THIS->scr_remap_chars (); + THIS->scr_touch (true); + THIS->want_refresh = 1; + break; + + case Opt_cursorUnderline: + THIS->want_refresh = 1; + break; + +# case Opt_scrollBar_floating: +# case Opt_scrollBar_right: +# THIS->resize_all_windows (THIS->width, THIS->height, 1); +# break; + } + } +} + OUTPUT: + RETVAL + +bool +rxvt_term::parse_keysym (char *keysym, char *str) + CODE: + RETVAL = 0 < THIS->parse_keysym (keysym, str); + THIS->keyboard->register_done (); + OUTPUT: + RETVAL + void -rxvt_term::cur (...) +rxvt_term::screen_cur (...) PROTOTYPE: $;$$ ALIAS: screen_cur = 0 @@ -1111,14 +1318,16 @@ } int -rxvt_term::selection_grab (int eventtime = CurrentTime) +rxvt_term::selection_grab (U32 eventtime) void rxvt_term::selection (SV *newtext = 0) PPCODE: { if (GIMME_V != G_VOID) - XPUSHs (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len))); + XPUSHs (THIS->selection.text + ? taint (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len))) + : &PL_sv_undef); if (newtext) { @@ -1130,6 +1339,15 @@ } void +rxvt_term::scr_xor_rect (int beg_row, int beg_col, int end_row, int end_col, U32 rstyle1 = RS_RVid, U32 rstyle2 = RS_RVid | RS_Uline) + +void +rxvt_term::scr_xor_span (int beg_row, int beg_col, int end_row, int end_col, U32 rstyle = RS_RVid) + +void +rxvt_term::scr_bell () + +void rxvt_term::scr_add_lines (SV *string) CODE: {