ruby-termios: Patch to make it work under both 1.8 and 1.9

Discussion in 'Ruby' started by Lloyd Zusman, May 27, 2005.

  1. Lloyd Zusman

    Lloyd Zusman Guest

    --=-=-=

    I recently had problems getting ruby-termios to compile properly under
    ruby 1.9. It turns out that "rubyio.h" has changed between these two
    ruby versions, and therefore, I offer the following patch, which should
    enable ruby-termios to compile and run under all 1.8 and 1.9 ruby
    versions.


    --=-=-=
    Content-Type: text/x-patch
    Content-Disposition: attachment; filename=ruby-termios.patch
    Content-Description: Allows ruby-termios to work under 1.8.x and 1.9.x

    --- termios.c.orig 2005-05-27 02:37:18.000000000 -0400
    +++ termios.c 2005-05-27 02:39:20.000000000 -0400
    @@ -8,6 +8,14 @@

    #include "ruby.h"
    #include "rubyio.h"
    +#include "version.h"
    +
    +#if RUBY_VERSION_CODE >= 190
    +# define FPTR_FILE fptr->stdio_file
    +#else
    +# define FPTR_FILE fptr->f
    +#endif
    +
    #include <termios.h>
    #include <unistd.h>
    #include <string.h>
    @@ -201,7 +209,7 @@

    Check_Type(io, T_FILE);
    GetOpenFile(io, fptr);
    - if (tcgetattr(fileno(fptr->f), &t) < 0) {
    + if (tcgetattr(fileno(FPTR_FILE), &t) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't get terminal parameters (%s)", strerror(errno));
    }
    @@ -243,7 +251,7 @@
    old = termios_tcgetattr(io);
    GetOpenFile(io, fptr);
    Termios_to_termios(param, &t);
    - if (tcsetattr(fileno(fptr->f), tcsetattr_option, &t) < 0) {
    + if (tcsetattr(fileno(FPTR_FILE), tcsetattr_option, &t) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't set terminal parameters (%s)", strerror(errno));
    }
    @@ -268,7 +276,7 @@
    Check_Type(duration, T_FIXNUM);

    GetOpenFile(io, fptr);
    - if (tcsendbreak(fileno(fptr->f), FIX2INT(duration)) < 0) {
    + if (tcsendbreak(fileno(FPTR_FILE), FIX2INT(duration)) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't transmits break (%s)", strerror(errno));
    }
    @@ -292,7 +300,7 @@
    Check_Type(io, T_FILE);

    GetOpenFile(io, fptr);
    - if (tcdrain(fileno(fptr->f)) < 0) {
    + if (tcdrain(fileno(FPTR_FILE)) < 0) {
    rb_raise(rb_eRuntimeError, "can't drain (%s)", strerror(errno));
    }

    @@ -322,7 +330,7 @@
    }

    GetOpenFile(io, fptr);
    - if (tcflush(fileno(fptr->f), queue_selector) < 0) {
    + if (tcflush(fileno(FPTR_FILE), queue_selector) < 0) {
    rb_raise(rb_eRuntimeError, "can't flush (%s)", strerror(errno));
    }

    @@ -352,7 +360,7 @@
    }

    GetOpenFile(io, fptr);
    - if (tcflow(fileno(fptr->f), action) < 0) {
    + if (tcflow(fileno(FPTR_FILE), action) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't control transmitting data flow (%s)", strerror(errno));
    }
    @@ -376,7 +384,7 @@

    Check_Type(io, T_FILE);
    GetOpenFile(io, fptr);
    - if ((pid = tcgetpgrp(fileno(fptr->f))) < 0) {
    + if ((pid = tcgetpgrp(fileno(FPTR_FILE))) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't get process group id (%s)", strerror(errno));
    }
    @@ -401,7 +409,7 @@
    Check_Type(pgrpid, T_FIXNUM);

    GetOpenFile(io, fptr);
    - if (tcsetpgrp(fileno(fptr->f), FIX2INT(pgrpid)) < 0) {
    + if (tcsetpgrp(fileno(FPTR_FILE), FIX2INT(pgrpid)) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't set process group id (%s)", strerror(errno));
    }

    --=-=-=



    --
    Lloyd Zusman

    God bless you.

    --=-=-=--
    Lloyd Zusman, May 27, 2005
    #1
    1. Advertising

  2. Hi,

    At Fri, 27 May 2005 15:56:27 +0900,
    Lloyd Zusman wrote in [ruby-talk:143778]:
    > I recently had problems getting ruby-termios to compile properly under
    > ruby 1.9. It turns out that "rubyio.h" has changed between these two
    > ruby versions, and therefore, I offer the following patch, which should
    > enable ruby-termios to compile and run under all 1.8 and 1.9 ruby
    > versions.


    > +#include "version.h"
    > +
    > +#if RUBY_VERSION_CODE >= 190
    > +# define FPTR_FILE fptr->stdio_file
    > +#else
    > +# define FPTR_FILE fptr->f
    > +#endif


    stdio_file isn't non-NULL always. Use rb_io_stdio_file() instead.

    #ifdef GetReadFile
    #define FPTR_FILE GetReadFile(fptr)
    #else
    #define FPTR_FILE rb_io_stdio_file(fptr)
    #endif

    --
    Nobu Nakada
    nobuyoshi nakada, May 27, 2005
    #2
    1. Advertising

  3. Lloyd Zusman

    Tanaka Akira Guest

    In article <2k.ad.ge.com>,
    nobuyoshi nakada <> writes:

    > stdio_file isn't non-NULL always. Use rb_io_stdio_file() instead.


    Don't use rb_io_stdio_file if possible,

    rb_io_stdio_file doesn't work when fd > 255 on Solaris.
    (Its FILE struct's fd member is unsigned char.)

    rb_io_stdio_file is not required in this case since termios needs only
    fd.

    --- ruby-termios-0.9.4-/termios.c 2002-10-13 00:15:03.000000000 +0900
    +++ ruby-termios-0.9.4/termios.c 2005-05-27 16:53:32.000000000 +0900
    @@ -12,6 +12,12 @@
    #include <unistd.h>
    #include <string.h>

    +#ifdef GetReadFile
    +#define GetFD(fptr) fileno(GetReadFile(fptr))
    +#else
    +#define GetFD(fptr) (fptr->fd)
    +#endif
    +
    static VALUE mTermios;
    static VALUE cTermios;
    static VALUE tcsetattr_opt, tcflush_qs, tcflow_act;
    @@ -201,7 +207,7 @@

    Check_Type(io, T_FILE);
    GetOpenFile(io, fptr);
    - if (tcgetattr(fileno(fptr->f), &t) < 0) {
    + if (tcgetattr(GetFD(fptr), &t) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't get terminal parameters (%s)", strerror(errno));
    }
    @@ -243,7 +249,7 @@
    old = termios_tcgetattr(io);
    GetOpenFile(io, fptr);
    Termios_to_termios(param, &t);
    - if (tcsetattr(fileno(fptr->f), tcsetattr_option, &t) < 0) {
    + if (tcsetattr(GetFD(fptr), tcsetattr_option, &t) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't set terminal parameters (%s)", strerror(errno));
    }
    @@ -268,7 +274,7 @@
    Check_Type(duration, T_FIXNUM);

    GetOpenFile(io, fptr);
    - if (tcsendbreak(fileno(fptr->f), FIX2INT(duration)) < 0) {
    + if (tcsendbreak(GetFD(fptr), FIX2INT(duration)) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't transmits break (%s)", strerror(errno));
    }
    @@ -292,7 +298,7 @@
    Check_Type(io, T_FILE);

    GetOpenFile(io, fptr);
    - if (tcdrain(fileno(fptr->f)) < 0) {
    + if (tcdrain(GetFD(fptr)) < 0) {
    rb_raise(rb_eRuntimeError, "can't drain (%s)", strerror(errno));
    }

    @@ -322,7 +328,7 @@
    }

    GetOpenFile(io, fptr);
    - if (tcflush(fileno(fptr->f), queue_selector) < 0) {
    + if (tcflush(GetFD(fptr), queue_selector) < 0) {
    rb_raise(rb_eRuntimeError, "can't flush (%s)", strerror(errno));
    }

    @@ -352,7 +358,7 @@
    }

    GetOpenFile(io, fptr);
    - if (tcflow(fileno(fptr->f), action) < 0) {
    + if (tcflow(GetFD(fptr), action) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't control transmitting data flow (%s)", strerror(errno));
    }
    @@ -376,7 +382,7 @@

    Check_Type(io, T_FILE);
    GetOpenFile(io, fptr);
    - if ((pid = tcgetpgrp(fileno(fptr->f))) < 0) {
    + if ((pid = tcgetpgrp(GetFD(fptr))) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't get process group id (%s)", strerror(errno));
    }
    @@ -401,7 +407,7 @@
    Check_Type(pgrpid, T_FIXNUM);

    GetOpenFile(io, fptr);
    - if (tcsetpgrp(fileno(fptr->f), FIX2INT(pgrpid)) < 0) {
    + if (tcsetpgrp(GetFD(fptr), FIX2INT(pgrpid)) < 0) {
    rb_raise(rb_eRuntimeError,
    "can't set process group id (%s)", strerror(errno));
    }
    --
    Tanaka Akira
    Tanaka Akira, May 27, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brice Rebsamen

    termios and keyboard medium raw mode

    Brice Rebsamen, Mar 13, 2008, in forum: C Programming
    Replies:
    2
    Views:
    2,827
    Mark Bluemel
    Mar 13, 2008
  2. ABCL
    Replies:
    0
    Views:
    545
  3. George Sakkis
    Replies:
    3
    Views:
    352
  4. Jonathan Wallace

    Difficulty installing ruby gem termios

    Jonathan Wallace, Oct 24, 2006, in forum: Ruby
    Replies:
    2
    Views:
    227
    Ken Bloom
    Oct 24, 2006
  5. Robin Stephens

    ruby-termios

    Robin Stephens, Jan 24, 2007, in forum: Ruby
    Replies:
    0
    Views:
    133
    Robin Stephens
    Jan 24, 2007
Loading...

Share This Page