あ伊藤です.

先日 curses で変なことを口走ったから,というわけでもないの
ですが,curses 拡張モジュールに,低レベルの terminfo ライブ
ラリへのインタフェースを追加してみました.

もし可能なら,curses の配布パッケージに追加していただきたい
のですが.標準配布するのがいかん,という場合は,別な拡張
モジュールにする予定です.

# pty と terminfo.さあ次は何だ?
--
aito

*** curses.c.org	Wed Oct 21 12:19:58 1998
--- curses.c	Wed Oct 21 14:27:30 1998
***************
*** 28,36 ****
--- 28,38 ----
  #endif
  
  #include "ruby.h"
+ #include "rubyio.h"    /* by A. Ito */
  
  static VALUE mCurses;
  static VALUE cWindow;
+ static VALUE mTerminfo;  /* by A. Ito */
  
  VALUE rb_stdscr;
  
***************
*** 750,755 ****
--- 752,917 ----
      return Qnil;
  }
  
+ /*------------------------- module Terminfo-------------------------*/
+ /* terminfo module is added by A. Ito, 21 October, 1998             */
+ 
+ #include <term.h>
+ 
+ /* def setupterm(term,file) */
+ static VALUE
+ ti_setupterm(obj,term,file)
+ VALUE obj;
+ VALUE file;
+ VALUE term;
+ {
+     int err_return;
+     char *c_term;
+     if (NIL_P(term))
+         c_term = NULL;
+     else {
+ 	Check_Type(term,T_STRING);
+         c_term = RSTRING(term)->ptr;
+     }
+     Check_Type(file,T_FILE);
+     setupterm(c_term,fileno(RFILE(file)->fptr->f),&err_return);
+     if (err_return == 0)
+         Fail("Can't find terminal %s",c_term?c_term:"(NULL)");
+     else if (err_return == -1)
+         Fail("Can't find terminfo database");
+ 
+     return Qnil;
+ }
+ 
+ /* def def_prog_mode */
+ static VALUE
+ ti_def_prog_mode(obj)
+ VALUE obj;
+ {
+     def_prog_mode();
+     return Qnil;
+ }
+ 
+ /* def def_shell_mode */
+ static VALUE
+ ti_def_shell_mode(obj)
+ VALUE obj;
+ {
+     def_shell_mode();
+     return Qnil;
+ }
+ 
+ /* def reset_prog_mode */
+ static VALUE
+ ti_reset_prog_mode(obj)
+ VALUE obj;
+ {
+     reset_prog_mode();
+     return Qnil;
+ }
+ 
+ /* def reset_shell_mode */
+ static VALUE
+ ti_reset_shell_mode(obj)
+ VALUE obj;
+ {
+     reset_shell_mode();
+     return Qnil;
+ }
+ 
+ /* def tigetstr(capname) */
+ static VALUE
+ ti_tigetstr(obj,capname)
+ VALUE obj;
+ VALUE capname;
+ {
+     char* ret;
+     Check_Type(capname,T_STRING);
+     ret = tigetstr(RSTRING(capname)->ptr);
+     if (ret == (char*)-1)
+         Fail("unknown terminfo string entry :%s",RSTRING(capname)->ptr);
+     if (ret == NULL)
+        return Qnil;
+     return str_new2(ret);
+ }
+ 
+ /* def tigetnum(capname) */
+ static VALUE
+ ti_tigetnum(obj,capname)
+ VALUE obj;
+ VALUE capname;
+ {
+     int ret;
+     Check_Type(capname,T_STRING);
+     ret = tigetnum(RSTRING(capname)->ptr);
+     if (ret == -2)
+         Fail("unknown terminfo number entry: %s",RSTRING(capname)->ptr);
+     return INT2FIX(ret);
+ }
+ 
+ /* def tigetflag(capname) */
+ static VALUE
+ ti_tigetflag(obj,capname)
+ VALUE obj;
+ VALUE capname;
+ {
+     int ret;
+     Check_Type(capname,T_STRING);
+     ret = tigetflag(RSTRING(capname)->ptr);
+     if (ret == -1)
+         Fail("unknown terminfo boolean entry: %s",RSTRING(capname)->ptr);
+     return ret==0?FALSE:TRUE;
+ }
+ 
+ /* def tparm(str,...) */
+ static VALUE
+ ti_tparm(argc,argv,obj)
+ int argc;
+ VALUE *argv;
+ VALUE obj;
+ {
+     long int p[9];
+     char *str;
+     int i;
+ 
+     if (argc < 1)
+       Fail("too few argument");
+     if (argc >= 10)
+       Fail("too many argument");
+     for (i = 0; i < 9; i++)
+         p[i] = 0;
+     Check_Type(argv[0],T_STRING);
+     str = RSTRING(argv[0])->ptr;
+     for (i = 1; i < argc; i++) {
+         Check_Type(argv[i],T_FIXNUM);
+         p[i-1] = FIX2INT(argv[i]);
+     }
+     return str_new2(tparm(str,p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8],p[9]));
+ }
+ 
+ static FILE *output_stream_for_putc;
+ 
+ static int
+ ti_pseudo_putc(char c)
+ {
+     fputc(c,output_stream_for_putc);
+     return c;
+ }
+ 
+ /* def tputs(str,io) */
+ static VALUE
+ ti_tputs(obj,str,io)
+ VALUE obj;
+ VALUE str;
+ VALUE io;
+ {
+     Check_Type(str,T_STRING);
+     Check_Type(io,T_FILE);
+     output_stream_for_putc = RFILE(io)->fptr->f;
+     tputs(RSTRING(str)->ptr,1,ti_pseudo_putc);
+     return Qnil;
+ }
+ 
+ 
  /*------------------------- Initialization -------------------------*/
  void
  Init_curses()
***************
*** 817,820 ****
--- 979,994 ----
      rb_define_method(cWindow, "deleteln", window_deleteln, 0);
  
      rb_set_end_proc(curses_finalize, 0);
+ 
+     mTerminfo = rb_define_module("Terminfo");
+     rb_define_module_function(mTerminfo, "setupterm", ti_setupterm, 2);
+     rb_define_module_function(mTerminfo, "def_prog_mode", ti_def_prog_mode, 0);
+     rb_define_module_function(mTerminfo, "def_shell_mode", ti_def_shell_mode, 0);
+     rb_define_module_function(mTerminfo, "reset_prog_mode", ti_reset_prog_mode, 0);
+     rb_define_module_function(mTerminfo, "reset_shell_mode", ti_reset_shell_mode, 0);
+     rb_define_module_function(mTerminfo, "getstr", ti_tigetstr, 1);
+     rb_define_module_function(mTerminfo, "getnum", ti_tigetnum, 1);
+     rb_define_module_function(mTerminfo, "getflag", ti_tigetflag, 1);
+     rb_define_module_function(mTerminfo, "tparm", ti_tparm, -1);
+     rb_define_module_function(mTerminfo, "tputs", ti_tputs, 2);
  }