io-console 0.5.2 → 0.5.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f87daa288fc605a3bf48b70558316f9427552fcf895e3a40363f71ba095d3e1b
4
- data.tar.gz: bddde3f65430cad405fdd4ef576f9d18b85368001f5ea27dc16fb941e0065a04
3
+ metadata.gz: 11b22a8d7f56c201a15c12dcd7989f79252c4197c46d145a611fc6e7f21fed6b
4
+ data.tar.gz: 4421595bf26a904a74167777f8b456438ec0cbb2f7a46314d35a783c48aef3f5
5
5
  SHA512:
6
- metadata.gz: bd7bb0e4285b7a3de96006386f262cc74fde97fc34a157ad8b834661e52a1aefe210d683c8264e6a98cf8ce80610b7fa8917e79d2b80f87ea4ae052e60c98839
7
- data.tar.gz: 23a0ac10ecd3f77ca899f5c00a9cc0689bc1c593540b116a6656eaef7d50aad887a3cfbcd66e6f954ebcac3e93f7f9eddbcda0f620d7804a196d541359e230bc
6
+ metadata.gz: ed6a2aed986b44ba25be2e04e7867421a5f7f341e5a6c4ef7a06f0d6c5790adfd4d8345005fd755ce75ea3306e73cc809ffe5f85ea3eed7995766b533e90b867
7
+ data.tar.gz: 64802adccfd88e58df35f30a269999cb685bc6b10d2f7aa715a862d8d7cb2bed985c0778c8e0a0575823c91bea369d4ab5d8703be34269ff8d4e9fbce3865019
data/README.md CHANGED
@@ -27,7 +27,7 @@ IO.console -> #<File:/dev/tty>
27
27
  IO.console(sym, *args)
28
28
  ```
29
29
 
30
- Returns an File instance opened console.
30
+ Returns a File instance opened console.
31
31
 
32
32
  If `sym` is given, it will be sent to the opened console with `args` and the result will be returned instead of the console IO itself.
33
33
 
@@ -1,4 +1,4 @@
1
- /* -*- c-file-style: "ruby" -*- */
1
+ /* -*- c-file-style: "ruby"; indent-tabs-mode: t -*- */
2
2
  /*
3
3
  * console IO module
4
4
  */
@@ -23,7 +23,7 @@ typedef struct termios conmode;
23
23
  static int
24
24
  setattr(int fd, conmode *t)
25
25
  {
26
- while (tcsetattr(fd, TCSAFLUSH, t)) {
26
+ while (tcsetattr(fd, TCSANOW, t)) {
27
27
  if (errno != EINTR) return 0;
28
28
  }
29
29
  return 1;
@@ -77,9 +77,15 @@ getattr(int fd, conmode *t)
77
77
 
78
78
  static ID id_getc, id_console, id_close, id_min, id_time, id_intr;
79
79
  #if ENABLE_IO_GETPASS
80
- static ID id_gets;
80
+ static ID id_gets, id_chomp_bang;
81
81
  #endif
82
82
 
83
+ #ifdef HAVE_RB_SCHEDULER_TIMEOUT
84
+ extern VALUE rb_scheduler_timeout(struct timeval *timeout);
85
+ #endif
86
+
87
+ #define sys_fail_fptr(fptr) rb_sys_fail_str((fptr)->pathv)
88
+
83
89
  #ifndef HAVE_RB_F_SEND
84
90
  static ID id___send__;
85
91
 
@@ -111,6 +117,9 @@ rawmode_opt(int *argcp, VALUE *argv, int min_argc, int max_argc, rawmode_arg_t *
111
117
  int argc = *argcp;
112
118
  rawmode_arg_t *optp = NULL;
113
119
  VALUE vopts = Qnil;
120
+ #ifdef RB_SCAN_ARGS_PASS_CALLED_KEYWORDS
121
+ argc = rb_scan_args(argc, argv, "*:", NULL, &vopts);
122
+ #else
114
123
  if (argc > min_argc) {
115
124
  vopts = rb_check_hash_type(argv[argc-1]);
116
125
  if (!NIL_P(vopts)) {
@@ -120,6 +129,7 @@ rawmode_opt(int *argcp, VALUE *argv, int min_argc, int max_argc, rawmode_arg_t *
120
129
  if (!vopts) vopts = Qnil;
121
130
  }
122
131
  }
132
+ #endif
123
133
  rb_check_arity(argc, min_argc, max_argc);
124
134
  if (!NIL_P(vopts)) {
125
135
  VALUE vmin = rb_hash_aref(vopts, ID2SYM(id_min));
@@ -165,11 +175,13 @@ set_rawmode(conmode *t, void *arg)
165
175
  cfmakeraw(t);
166
176
  t->c_lflag &= ~(ECHOE|ECHOK);
167
177
  #elif defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
168
- t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
178
+ t->c_iflag &= ~(IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|IMAXBEL);
169
179
  t->c_oflag &= ~OPOST;
170
- t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
180
+ t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|XCASE);
171
181
  t->c_cflag &= ~(CSIZE|PARENB);
172
182
  t->c_cflag |= CS8;
183
+ t->c_cc[VMIN] = 1;
184
+ t->c_cc[VTIME] = 0;
173
185
  #elif defined HAVE_SGTTY_H
174
186
  t->sg_flags &= ~ECHO;
175
187
  t->sg_flags |= RAW;
@@ -186,8 +198,9 @@ set_rawmode(conmode *t, void *arg)
186
198
  #endif
187
199
  #ifdef ISIG
188
200
  if (r->intr) {
189
- t->c_iflag |= BRKINT|IXON;
201
+ t->c_iflag |= BRKINT;
190
202
  t->c_lflag |= ISIG;
203
+ t->c_oflag |= OPOST;
191
204
  }
192
205
  #endif
193
206
  (void)r;
@@ -353,9 +366,9 @@ ttymode_with_io(VALUE io, VALUE (*func)(VALUE, VALUE), VALUE farg, void (*setter
353
366
 
354
367
  /*
355
368
  * call-seq:
356
- * io.raw(min: nil, time: nil) {|io| }
369
+ * io.raw(min: nil, time: nil, intr: nil) {|io| }
357
370
  *
358
- * Yields +self+ within raw mode.
371
+ * Yields +self+ within raw mode, and returns the result of the block.
359
372
  *
360
373
  * STDIN.raw(&:gets)
361
374
  *
@@ -367,6 +380,9 @@ ttymode_with_io(VALUE io, VALUE (*func)(VALUE, VALUE), VALUE farg, void (*setter
367
380
  * The parameter +time+ specifies the timeout in _seconds_ with a
368
381
  * precision of 1/10 of a second. (default: 0)
369
382
  *
383
+ * If the parameter +intr+ is +true+, enables break, interrupt, quit,
384
+ * and suspend special characters.
385
+ *
370
386
  * Refer to the manual page of termios for further details.
371
387
  *
372
388
  * You must require 'io/console' to use this method.
@@ -380,11 +396,11 @@ console_raw(int argc, VALUE *argv, VALUE io)
380
396
 
381
397
  /*
382
398
  * call-seq:
383
- * io.raw!(min: nil, time: nil)
399
+ * io.raw!(min: nil, time: nil, intr: nil) -> io
384
400
  *
385
- * Enables raw mode.
401
+ * Enables raw mode, and returns +io+.
386
402
  *
387
- * If the terminal mode needs to be back, use io.raw { ... }.
403
+ * If the terminal mode needs to be back, use <code>io.raw { ... }</code>.
388
404
  *
389
405
  * See IO#raw for details on the parameters.
390
406
  *
@@ -400,9 +416,9 @@ console_set_raw(int argc, VALUE *argv, VALUE io)
400
416
 
401
417
  GetOpenFile(io, fptr);
402
418
  fd = GetReadFD(fptr);
403
- if (!getattr(fd, &t)) rb_sys_fail(0);
419
+ if (!getattr(fd, &t)) sys_fail_fptr(fptr);
404
420
  set_rawmode(&t, optp);
405
- if (!setattr(fd, &t)) rb_sys_fail(0);
421
+ if (!setattr(fd, &t)) sys_fail_fptr(fptr);
406
422
  return io;
407
423
  }
408
424
 
@@ -443,9 +459,9 @@ console_set_cooked(VALUE io)
443
459
 
444
460
  GetOpenFile(io, fptr);
445
461
  fd = GetReadFD(fptr);
446
- if (!getattr(fd, &t)) rb_sys_fail(0);
462
+ if (!getattr(fd, &t)) sys_fail_fptr(fptr);
447
463
  set_cookedmode(&t, NULL);
448
- if (!setattr(fd, &t)) rb_sys_fail(0);
464
+ if (!setattr(fd, &t)) sys_fail_fptr(fptr);
449
465
  return io;
450
466
  }
451
467
 
@@ -480,7 +496,7 @@ nogvl_getch(void *p)
480
496
 
481
497
  /*
482
498
  * call-seq:
483
- * io.getch(min: nil, time: nil) -> char
499
+ * io.getch(min: nil, time: nil, intr: nil) -> char
484
500
  *
485
501
  * Reads and returns a character in raw mode.
486
502
  *
@@ -498,28 +514,50 @@ console_getch(int argc, VALUE *argv, VALUE io)
498
514
  rb_io_t *fptr;
499
515
  VALUE str;
500
516
  wint_t c;
501
- int w, len;
517
+ int len;
502
518
  char buf[8];
503
519
  wint_t wbuf[2];
520
+ # ifndef HAVE_RB_IO_WAIT
504
521
  struct timeval *to = NULL, tv;
522
+ # else
523
+ VALUE timeout = Qnil;
524
+ # endif
505
525
 
506
526
  GetOpenFile(io, fptr);
507
527
  if (optp) {
508
528
  if (optp->vtime) {
529
+ # ifndef HAVE_RB_IO_WAIT
509
530
  to = &tv;
531
+ # else
532
+ struct timeval tv;
533
+ # endif
510
534
  tv.tv_sec = optp->vtime / 10;
511
535
  tv.tv_usec = (optp->vtime % 10) * 100000;
536
+ # ifdef HAVE_RB_IO_WAIT
537
+ timeout = rb_scheduler_timeout(&tv);
538
+ # endif
512
539
  }
513
- if (optp->vmin != 1) {
514
- rb_warning("min option ignored");
540
+ switch (optp->vmin) {
541
+ case 1: /* default */
542
+ break;
543
+ case 0: /* return nil when timed out */
544
+ if (optp->vtime) break;
545
+ /* fallthru */
546
+ default:
547
+ rb_warning("min option larger than 1 ignored");
515
548
  }
516
549
  if (optp->intr) {
517
- w = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, to);
550
+ # ifndef HAVE_RB_IO_WAIT
551
+ int w = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, to);
518
552
  if (w < 0) rb_eof_error();
519
553
  if (!(w & RB_WAITFD_IN)) return Qnil;
554
+ # else
555
+ VALUE result = rb_io_wait(io, RUBY_IO_READABLE, timeout);
556
+ if (result == Qfalse) return Qnil;
557
+ # endif
520
558
  }
521
- else {
522
- rb_warning("vtime option ignored if intr flag is unset");
559
+ else if (optp->vtime) {
560
+ rb_warning("Non-zero vtime option ignored if intr flag is unset");
523
561
  }
524
562
  }
525
563
  len = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getch, wbuf, RUBY_UBF_IO, 0);
@@ -580,12 +618,12 @@ console_set_echo(VALUE io, VALUE f)
580
618
 
581
619
  GetOpenFile(io, fptr);
582
620
  fd = GetReadFD(fptr);
583
- if (!getattr(fd, &t)) rb_sys_fail(0);
621
+ if (!getattr(fd, &t)) sys_fail_fptr(fptr);
584
622
  if (RTEST(f))
585
623
  set_echo(&t, NULL);
586
624
  else
587
625
  set_noecho(&t, NULL);
588
- if (!setattr(fd, &t)) rb_sys_fail(0);
626
+ if (!setattr(fd, &t)) sys_fail_fptr(fptr);
589
627
  return io;
590
628
  }
591
629
 
@@ -606,7 +644,7 @@ console_echo_p(VALUE io)
606
644
 
607
645
  GetOpenFile(io, fptr);
608
646
  fd = GetReadFD(fptr);
609
- if (!getattr(fd, &t)) rb_sys_fail(0);
647
+ if (!getattr(fd, &t)) sys_fail_fptr(fptr);
610
648
  return echo_p(&t) ? Qtrue : Qfalse;
611
649
  }
612
650
 
@@ -690,7 +728,7 @@ console_conmode_get(VALUE io)
690
728
 
691
729
  GetOpenFile(io, fptr);
692
730
  fd = GetReadFD(fptr);
693
- if (!getattr(fd, &t)) rb_sys_fail(0);
731
+ if (!getattr(fd, &t)) sys_fail_fptr(fptr);
694
732
 
695
733
  return conmode_new(cConmode, &t);
696
734
  }
@@ -714,7 +752,7 @@ console_conmode_set(VALUE io, VALUE mode)
714
752
  r = *t;
715
753
  GetOpenFile(io, fptr);
716
754
  fd = GetReadFD(fptr);
717
- if (!setattr(fd, &r)) rb_sys_fail(0);
755
+ if (!setattr(fd, &r)) sys_fail_fptr(fptr);
718
756
 
719
757
  return mode;
720
758
  }
@@ -756,7 +794,7 @@ console_winsize(VALUE io)
756
794
 
757
795
  GetOpenFile(io, fptr);
758
796
  fd = GetWriteFD(fptr);
759
- if (!getwinsize(fd, &ws)) rb_sys_fail(0);
797
+ if (!getwinsize(fd, &ws)) sys_fail_fptr(fptr);
760
798
  return rb_assoc_new(INT2NUM(winsize_row(&ws)), INT2NUM(winsize_col(&ws)));
761
799
  }
762
800
 
@@ -803,7 +841,7 @@ console_set_winsize(VALUE io, VALUE size)
803
841
  SET(xpixel);
804
842
  SET(ypixel);
805
843
  #undef SET
806
- if (!setwinsize(fd, &ws)) rb_sys_fail(0);
844
+ if (!setwinsize(fd, &ws)) sys_fail_fptr(fptr);
807
845
  #elif defined _WIN32
808
846
  wh = (HANDLE)rb_w32_get_osfhandle(fd);
809
847
  #define SET(m) new##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m)
@@ -878,7 +916,7 @@ console_iflush(VALUE io)
878
916
  GetOpenFile(io, fptr);
879
917
  fd = GetReadFD(fptr);
880
918
  #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
881
- if (tcflush(fd, TCIFLUSH)) rb_sys_fail(0);
919
+ if (tcflush(fd, TCIFLUSH)) sys_fail_fptr(fptr);
882
920
  #endif
883
921
  (void)fd;
884
922
  return io;
@@ -901,7 +939,7 @@ console_oflush(VALUE io)
901
939
  GetOpenFile(io, fptr);
902
940
  fd = GetWriteFD(fptr);
903
941
  #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
904
- if (tcflush(fd, TCOFLUSH)) rb_sys_fail(0);
942
+ if (tcflush(fd, TCOFLUSH)) sys_fail_fptr(fptr);
905
943
  #endif
906
944
  (void)fd;
907
945
  return io;
@@ -928,11 +966,11 @@ console_ioflush(VALUE io)
928
966
  fd1 = GetReadFD(fptr);
929
967
  fd2 = GetWriteFD(fptr);
930
968
  if (fd2 != -1 && fd1 != fd2) {
931
- if (tcflush(fd1, TCIFLUSH)) rb_sys_fail(0);
932
- if (tcflush(fd2, TCOFLUSH)) rb_sys_fail(0);
969
+ if (tcflush(fd1, TCIFLUSH)) sys_fail_fptr(fptr);
970
+ if (tcflush(fd2, TCOFLUSH)) sys_fail_fptr(fptr);
933
971
  }
934
972
  else {
935
- if (tcflush(fd1, TCIOFLUSH)) rb_sys_fail(0);
973
+ if (tcflush(fd1, TCIOFLUSH)) sys_fail_fptr(fptr);
936
974
  }
937
975
  #endif
938
976
  return io;
@@ -951,7 +989,7 @@ console_beep(VALUE io)
951
989
  MessageBeep(0);
952
990
  #else
953
991
  if (write(fd, "\a", 1) < 0)
954
- rb_sys_fail(0);
992
+ sys_fail_fptr(fptr);
955
993
  #endif
956
994
  return io;
957
995
  }
@@ -1185,8 +1223,8 @@ console_key_pressed_p(VALUE io, VALUE k)
1185
1223
  }
1186
1224
  #else
1187
1225
  struct query_args {
1188
- const char *qstr;
1189
- int opt;
1226
+ char qstr[6];
1227
+ unsigned char opt;
1190
1228
  };
1191
1229
 
1192
1230
  static int
@@ -1487,7 +1525,7 @@ console_dev(int argc, VALUE *argv, VALUE klass)
1487
1525
 
1488
1526
  /*
1489
1527
  * call-seq:
1490
- * io.getch(min: nil, time: nil) -> char
1528
+ * io.getch(min: nil, time: nil, intr: nil) -> char
1491
1529
  *
1492
1530
  * See IO#getch.
1493
1531
  */
@@ -1524,7 +1562,7 @@ static VALUE
1524
1562
  str_chomp(VALUE str)
1525
1563
  {
1526
1564
  if (!NIL_P(str)) {
1527
- str = rb_funcallv(str, rb_intern("chomp!"), 0, 0);
1565
+ rb_funcallv(str, id_chomp_bang, 0, 0);
1528
1566
  }
1529
1567
  return str;
1530
1568
  }
@@ -1536,6 +1574,10 @@ str_chomp(VALUE str)
1536
1574
  * Reads and returns a line without echo back.
1537
1575
  * Prints +prompt+ unless it is +nil+.
1538
1576
  *
1577
+ * The newline character that terminates the
1578
+ * read line is removed from the returned string,
1579
+ * see String#chomp!.
1580
+ *
1539
1581
  * You must require 'io/console' to use this method.
1540
1582
  */
1541
1583
  static VALUE
@@ -1580,6 +1622,7 @@ Init_console(void)
1580
1622
  id_getc = rb_intern("getc");
1581
1623
  #if ENABLE_IO_GETPASS
1582
1624
  id_gets = rb_intern("gets");
1625
+ id_chomp_bang = rb_intern("chomp!");
1583
1626
  #endif
1584
1627
  id_console = rb_intern("console");
1585
1628
  id_close = rb_intern("close");
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: false
2
2
  require 'mkmf'
3
3
 
4
- ok = true
4
+ ok = true if RUBY_ENGINE == "ruby"
5
5
  hdr = nil
6
6
  case
7
7
  when macro_defined?("_WIN32", "")
@@ -14,8 +14,9 @@ when have_header(hdr = "sgtty.h")
14
14
  %w"stty gtty".each {|f| have_func(f, hdr)}
15
15
  else
16
16
  ok = false
17
- end
18
- if ok
17
+ end if ok
18
+ case ok
19
+ when true
19
20
  have_header("sys/ioctl.h") if hdr
20
21
  # rb_check_hash_type: 1.9.3
21
22
  # rb_io_get_write_io: 1.9.1
@@ -23,8 +24,13 @@ if ok
23
24
  # rb_funcallv: 2.1.0
24
25
  # RARRAY_CONST_PTR: 2.1.0
25
26
  # rb_sym2str: 2.2.0
27
+ if have_func("rb_scheduler_timeout")
28
+ have_func("rb_io_wait")
29
+ end
26
30
  $defs << "-D""ENABLE_IO_GETPASS=1"
27
31
  create_makefile("io/console") {|conf|
28
32
  conf << "\n""VK_HEADER = #{vk_header}\n"
29
33
  }
34
+ when nil
35
+ File.write("Makefile", dummy_makefile($srcdir).join(""))
30
36
  end
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.5.2
4
+ version: 0.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nobu Nakada
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-10 00:00:00.000000000 Z
11
+ date: 2021-01-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: add console capabilities to IO instances.
14
14
  email: nobu@ruby-lang.org
@@ -25,10 +25,11 @@ files:
25
25
  - lib/io/console/size.rb
26
26
  homepage: https://github.com/ruby/io-console
27
27
  licenses:
28
+ - Ruby
28
29
  - BSD-2-Clause
29
30
  metadata:
30
31
  source_code_url: https://github.com/ruby/io-console
31
- post_install_message:
32
+ post_install_message:
32
33
  rdoc_options: []
33
34
  require_paths:
34
35
  - lib
@@ -36,15 +37,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
36
37
  requirements:
37
38
  - - ">="
38
39
  - !ruby/object:Gem::Version
39
- version: 2.2.0
40
+ version: 2.4.0
40
41
  required_rubygems_version: !ruby/object:Gem::Requirement
41
42
  requirements:
42
43
  - - ">="
43
44
  - !ruby/object:Gem::Version
44
45
  version: '0'
45
46
  requirements: []
46
- rubygems_version: 3.1.0.pre3
47
- signing_key:
47
+ rubygems_version: 3.2.3
48
+ signing_key:
48
49
  specification_version: 4
49
50
  summary: Console interface
50
51
  test_files: []