io-console 0.8.0.beta1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e3275d6ea38b3acd5026d152568527ea25251d09abbf16893113814dd23318b
4
- data.tar.gz: 609613402c097734e446fe8e98ca544448bba8d6b473ec17825f551dd7332c0c
3
+ metadata.gz: a8485f98825214ba70388cfaf023a5380201fc1de70b87ffddb36a9a2603115c
4
+ data.tar.gz: db5deeb27f6bc90121cdb8ad6005aca2615f5a81976f97f09f25e63caed7070e
5
5
  SHA512:
6
- metadata.gz: 1f60ce0eb29811ef5b1927245318d6f7b689ba7bd14b14e06dc203c25f5588e3f30eac964f9334c0fd8c0837608bcd0b31e87fb6e2e0a18b1bd6ef082951a502
7
- data.tar.gz: d4857834e30e5cfa20950d50c67e047417c9a838ac4e7d80d2d3c889391734e7b5790135fe2bc1074056893f9399bfd480779ce439da59901947308d0c50e796
6
+ metadata.gz: fcafaead8dd6a72e82e852ad8cf57bff49fb8fd8959520b8522c3fbf79bd97f1b218dd341f1e66656500c2a5ccd34045260655af948d86031e8f01ce19887df7
7
+ data.tar.gz: f8ebf55a9fef2383adda797b62824cc7b9cc9458c98fd9025bab2945111851651df9a44469ec10549d4ea063469870ba2cc32a4a25d3e68dc57206531bce6c54
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  static const char *const
7
- IO_CONSOLE_VERSION = "0.8.0.beta1";
7
+ IO_CONSOLE_VERSION = "0.8.0";
8
8
 
9
9
  #include "ruby.h"
10
10
  #include "ruby/io.h"
@@ -84,6 +84,11 @@ getattr(int fd, conmode *t)
84
84
  static ID id_getc, id_close;
85
85
  static ID id_gets, id_flush, id_chomp_bang;
86
86
 
87
+ #ifndef HAVE_RB_INTERNED_STR_CSTR
88
+ # define rb_str_to_interned_str(str) rb_str_freeze(str)
89
+ # define rb_interned_str_cstr(str) rb_str_freeze(rb_usascii_str_new_cstr(str))
90
+ #endif
91
+
87
92
  #if defined HAVE_RUBY_FIBER_SCHEDULER_H
88
93
  # include "ruby/fiber/scheduler.h"
89
94
  #elif defined HAVE_RB_SCHEDULER_TIMEOUT
@@ -125,7 +130,14 @@ io_get_write_io_fallback(VALUE io)
125
130
  #define rb_io_get_write_io io_get_write_io_fallback
126
131
  #endif
127
132
 
128
- #define sys_fail(io) rb_sys_fail_str(rb_io_path(io))
133
+ #ifndef DHAVE_RB_SYSERR_FAIL_STR
134
+ # define rb_syserr_fail_str(e, mesg) rb_exc_raise(rb_syserr_new_str(e, mesg))
135
+ #endif
136
+
137
+ #define sys_fail(io) do { \
138
+ int err = errno; \
139
+ rb_syserr_fail_str(err, rb_io_path(io)); \
140
+ } while (0)
129
141
 
130
142
  #ifndef HAVE_RB_F_SEND
131
143
  #ifndef RB_PASS_CALLED_KEYWORDS
@@ -1811,6 +1823,61 @@ io_getpass(int argc, VALUE *argv, VALUE io)
1811
1823
  return str_chomp(str);
1812
1824
  }
1813
1825
 
1826
+ #if defined(_WIN32) || defined(HAVE_TTYNAME_R) || defined(HAVE_TTYNAME)
1827
+ /*
1828
+ * call-seq:
1829
+ * io.ttyname -> string or nil
1830
+ *
1831
+ * Returns name of associated terminal (tty) if +io+ is not a tty.
1832
+ * Returns +nil+ otherwise.
1833
+ */
1834
+ static VALUE
1835
+ console_ttyname(VALUE io)
1836
+ {
1837
+ int fd = rb_io_descriptor(io);
1838
+ if (!isatty(fd)) return Qnil;
1839
+ # if defined _WIN32
1840
+ return rb_usascii_str_new_lit("con");
1841
+ # elif defined HAVE_TTYNAME_R
1842
+ {
1843
+ char termname[1024], *tn = termname;
1844
+ size_t size = sizeof(termname);
1845
+ int e;
1846
+ if (ttyname_r(fd, tn, size) == 0)
1847
+ return rb_interned_str_cstr(tn);
1848
+ if ((e = errno) == ERANGE) {
1849
+ VALUE s = rb_str_new(0, size);
1850
+ while (1) {
1851
+ tn = RSTRING_PTR(s);
1852
+ size = rb_str_capacity(s);
1853
+ if (ttyname_r(fd, tn, size) == 0) {
1854
+ return rb_str_to_interned_str(rb_str_resize(s, strlen(tn)));
1855
+ }
1856
+ if ((e = errno) != ERANGE) break;
1857
+ if ((size *= 2) >= INT_MAX/2) break;
1858
+ rb_str_resize(s, size);
1859
+ }
1860
+ }
1861
+ rb_syserr_fail_str(e, rb_sprintf("ttyname_r(%d)", fd));
1862
+ UNREACHABLE_RETURN(Qnil);
1863
+ }
1864
+ # elif defined HAVE_TTYNAME
1865
+ {
1866
+ const char *tn = ttyname(fd);
1867
+ if (!tn) {
1868
+ int e = errno;
1869
+ rb_syserr_fail_str(e, rb_sprintf("ttyname(%d)", fd));
1870
+ }
1871
+ return rb_interned_str_cstr(tn);
1872
+ }
1873
+ # else
1874
+ # error No ttyname function
1875
+ # endif
1876
+ }
1877
+ #else
1878
+ # define console_ttyname rb_f_notimplement
1879
+ #endif
1880
+
1814
1881
  /*
1815
1882
  * IO console methods
1816
1883
  */
@@ -1878,6 +1945,7 @@ InitVM_console(void)
1878
1945
  rb_define_method(rb_cIO, "pressed?", console_key_pressed_p, 1);
1879
1946
  rb_define_method(rb_cIO, "check_winsize_changed", console_check_winsize_changed, 0);
1880
1947
  rb_define_method(rb_cIO, "getpass", console_getpass, -1);
1948
+ rb_define_method(rb_cIO, "ttyname", console_ttyname, 0);
1881
1949
  rb_define_singleton_method(rb_cIO, "console", console_dev, -1);
1882
1950
  {
1883
1951
  /* :stopdoc: */
@@ -1889,7 +1957,7 @@ InitVM_console(void)
1889
1957
  {
1890
1958
  /* :stopdoc: */
1891
1959
  cConmode = rb_define_class_under(rb_cIO, "ConsoleMode", rb_cObject);
1892
- rb_define_const(cConmode, "VERSION", rb_str_new_cstr(IO_CONSOLE_VERSION));
1960
+ rb_define_const(cConmode, "VERSION", rb_obj_freeze(rb_str_new_cstr(IO_CONSOLE_VERSION)));
1893
1961
  rb_define_alloc_func(cConmode, conmode_alloc);
1894
1962
  rb_undef_method(cConmode, "initialize");
1895
1963
  rb_define_method(cConmode, "initialize_copy", conmode_init_copy, 1);
@@ -5,6 +5,11 @@ require 'mkmf'
5
5
  # See https://bugs.ruby-lang.org/issues/20345
6
6
  MakeMakefile::RbConfig ||= ::RbConfig
7
7
 
8
+ have_func("rb_syserr_fail_str(0, Qnil)") or
9
+ have_func("rb_syserr_new_str(0, Qnil)") or
10
+ abort
11
+
12
+ have_func("rb_interned_str_cstr")
8
13
  have_func("rb_io_path")
9
14
  have_func("rb_io_descriptor")
10
15
  have_func("rb_io_get_write_io")
@@ -47,6 +52,7 @@ when true
47
52
  elsif have_func("rb_scheduler_timeout") # 3.0
48
53
  have_func("rb_io_wait")
49
54
  end
55
+ have_func("ttyname_r") or have_func("ttyname")
50
56
  create_makefile("io/console") {|conf|
51
57
  conf << "\n""VK_HEADER = #{vk_header}\n"
52
58
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io-console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0.beta1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nobu Nakada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-12 00:00:00.000000000 Z
11
+ date: 2024-12-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: add console capabilities to IO instances.
14
14
  email: nobu@ruby-lang.org