-
Notifications
You must be signed in to change notification settings - Fork 9
/
terminfo.c
419 lines (358 loc) · 9.99 KB
/
terminfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* terminfo.c - Ruby binding for terminfo library.
Copyright (C) 2007, 2008 Tanaka Akira. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ruby.h"
#ifdef HAVE_RUBY_IO_H
# include "ruby/io.h"
#else
# include "rubyio.h"
#endif
#include "extconf.h"
#if defined(HAVE_NCURSES_H)
#include <ncurses.h>
#elif defined(HAVE_CURSES_H)
#include <curses.h>
#endif
#ifdef HAVE_TERM_H
#include <term.h>
#endif
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif
static VALUE cTermInfo;
static VALUE eTermInfoError;
#ifndef HAVE_TYPE_RB_IO_T
typedef OpenFile rb_io_t;
#endif
#if defined(HAVE_RB_IO_T_FD) || defined(HAVE_ST_FD)
# define FILENO(fptr) (fptr->fd)
#else
# define FILENO(fptr) fileno(fptr->f)
#endif
#if (defined(__FreeBSD__) && __FreeBSD_cc_version <= 602001) || \
defined(__OpenBSD__)
/*
* warning on FreeBSD
http://www.FreeBSD.org/cgi/query-pr.cgi?pr=108117&cat=
* core dump on OpenBSD
http://cvs.openbsd.org/cgi-bin/query-pr-wrapper?full=yes&textonly=yes&numbers=5447
*/
#define del_curterm(oterm) do {} while(0)
#endif
static void
rt_free(void *ptr)
{
if(ptr != NULL)
del_curterm(ptr);
}
static VALUE
rt_alloc(VALUE klass)
{
return Data_Wrap_Struct(klass, NULL, rt_free, 0);
}
static TERMINAL *
check_rt(VALUE self)
{
Check_Type(self, T_DATA);
if (RDATA(self)->dfree != rt_free) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected TermInfo)",
rb_class2name(CLASS_OF(self)));
}
return DATA_PTR(self);
}
static void
setup(VALUE self)
{
TERMINAL *term = check_rt(self);
if (term == NULL) { rb_raise(eTermInfoError, "terminfo object not initialized"); }
if (cur_term == term)
return;
}
/*
* TermInfo#setupterm(term, fd) => int
*
* TermInfo#setupterm initializes TermInfo object.
*
* term is a string of nil.
* If nil is given, the environment variable $TERM is used.
*
* fd is a file descriptor for target terminal.
*/
static VALUE
rt_setupterm(VALUE self, VALUE v_term, VALUE v_fd)
{
char *term;
int fd;
int err;
int ret;
if (check_rt(self) != NULL) { rb_raise(eTermInfoError, "terminfo object already initialized"); }
if (v_term == Qnil)
term = NULL;
else
term = StringValueCStr(v_term);
fd = NUM2INT(v_fd);
ret = setupterm(term, fd, &err);
if (ret == ERR) {
if (err == 1) rb_raise(eTermInfoError, "hardcopy terminal");
else if (err == 0) rb_raise(eTermInfoError, "terminal could not be found");
else if (err == -1) rb_raise(eTermInfoError, "terminfo database could not be found");
else rb_raise(eTermInfoError, "unexpected setupterm error");
}
DATA_PTR(self) = cur_term;
return INT2NUM(err);
}
/*
* TermInfo#tigetflag(capname) => int
*
* TermInfo#tigetflag returns a boolean capability specified by capname.
*/
static VALUE
rt_tigetflag(VALUE self, VALUE v_capname)
{
int ret;
setup(self);
ret = tigetflag(StringValueCStr(v_capname));
if (ret == -1) { rb_raise(eTermInfoError, "not a boolean capability"); }
return RTEST(ret) ? Qtrue : Qfalse;
}
/*
* TermInfo#tigetnum(capname) => int
*
* TermInfo#tigetnum returns a numeric capability specified by capname.
*/
static VALUE
rt_tigetnum(VALUE self, VALUE v_capname)
{
int ret;
setup(self);
ret = tigetnum(StringValueCStr(v_capname));
if (ret == -2) { rb_raise(eTermInfoError, "not a numeric capability"); }
if (ret == -1) { rb_raise(eTermInfoError, "canceled or absent numeric capability"); }
return INT2NUM(ret);
}
/*
* TermInfo#tigetstr(capname) => str
*
* TermInfo#tigetstr returns a string capability specified by capname.
*
* The return value should be printed after tputs is applied.
* Also tparm should be applied if it has parameters.
*
* io.print ti.tputs(ti.tparm(ti.tigetstr("cuf"), 2))
*
* Note that "cuf" means "cursor forward".
*/
static VALUE
rt_tigetstr(VALUE self, VALUE v_capname)
{
char *ret;
setup(self);
ret = tigetstr(StringValueCStr(v_capname));
if (ret == (char*)-1) {
rb_raise(eTermInfoError, "not a string capability");
}
if (ret == 0) {
rb_raise(eTermInfoError, "canceled or absent string capability");
}
return rb_str_new2(ret);
}
/*
* TermInfo#tparm(str, ...) => str
*
* TermInfo#tparm expands parameters in str returned by tigetstr.
*/
static VALUE
rt_tparm(int argc, VALUE *argv, VALUE self)
{
char *capname, *ret;
setup(self);
VALUE v_capname, v1, v2, v3, v4, v5, v6, v7, v8, v9;
long p1, p2, p3, p4, p5, p6, p7, p8, p9;
setup(self);
if (rb_scan_args(argc, argv, "19", &v_capname, &v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9) == 0) {
rb_raise(rb_eArgError, "capname required");
}
capname = StringValueCStr(v_capname);
#define conv(p, v) do { if (v == Qnil) p = 0; else p = NUM2LONG(v); } while(0)
conv(p1, v1);
conv(p2, v2);
conv(p3, v3);
conv(p4, v4);
conv(p5, v5);
conv(p6, v6);
conv(p7, v7);
conv(p8, v8);
conv(p9, v9);
ret = tparm(capname, p1, p2, p3, p4, p5, p6, p7, p8, p9);
if (ret == NULL) { rb_raise(eTermInfoError, "tparm failed"); }
return rb_str_new2(ret);
}
static VALUE putfunc_output; /* xxx: not thread safe */
static int
putfunc(int arg)
{
char ch = arg;
rb_str_cat(putfunc_output, &ch, 1);
return arg;
}
/*
* TermInfo#tputs(str, affcnt) => str
*
* TermInfo#tputs expands padding informaiton using padding characters.
* affcnt is a number of lines affected by the str.
*/
static VALUE
rt_tputs(VALUE self, VALUE v_str, VALUE v_affcnt)
{
int ret;
char *str;
int affcnt;
VALUE output;
setup(self);
str = StringValueCStr(v_str);
affcnt = NUM2INT(v_affcnt);
putfunc_output = output = rb_str_new2("");
ret = tputs(str, affcnt, putfunc);
putfunc_output = Qnil;
if (ret == ERR) { rb_raise(eTermInfoError, "tputs failed"); }
return output;
}
/*
* TermInfo.tiocgwinsz(io) => [row, col]
*
* TermInfo.tiocgwinsz returns the screen size of the terminal refered by io,
* using TIOCGWINSZ ioctl.
*/
static VALUE
rt_tiocgwinsz(VALUE self, VALUE io)
{
#ifdef TIOCGWINSZ
struct winsize sz;
int ret;
ret = ioctl(rb_io_descriptor(io), TIOCGWINSZ, &sz);
if (ret == -1) rb_raise(rb_eIOError, "TIOCGWINSZ failed");
return rb_ary_new3(2, INT2NUM(sz.ws_row), INT2NUM(sz.ws_col));
#else
rb_notimplement();
#endif
}
/*
* TermInfo.tiocswinsz(io, row, col)
*
* TermInfo.tiocgwinsz update the screen size information of the terminal refered by io,
* using TIOCSWINSZ ioctl.
*
* It returns nil.
*/
static VALUE
rt_tiocswinsz(VALUE self, VALUE io, VALUE row, VALUE col)
{
#ifdef TIOCSWINSZ
struct winsize sz;
int ret;
sz.ws_row = NUM2INT(row);
sz.ws_col = NUM2INT(col);
ret = ioctl(rb_io_descriptor(io), TIOCSWINSZ, &sz);
if (ret == -1) rb_raise(rb_eIOError, "TIOCSWINSZ failed");
return Qnil;
#else
rb_notimplement();
#endif
}
/*
* TermInfo.ctermid
*
* TermInfo.ctermid returns a pathname for the current controling terminal,
* such as "/dev/tty".
*/
static VALUE
rt_ctermid(VALUE self)
{
#ifdef HAVE_CTERMID
char buf[L_ctermid];
return rb_str_new2(ctermid(buf));
#else
return rb_str_new2("/dev/tty");
#endif
}
/*
* TermInfo.wcswidth(str)
*
* TermInfo.wcswidth returns a the number of columns of str,
* according to current locale.
*/
static VALUE
rt_wcswidth(VALUE self, VALUE str)
{
char *s;
size_t l, r;
mbstate_t mbs;
wchar_t wc;
long cols;
int width;
#ifdef HAVE_RUBY_ENCODING_H
/* The encoding of str is assumed to be the locale encoding on Ruby 1.8. */
str = rb_str_encode(str, rb_enc_from_encoding(rb_locale_encoding()), 0, Qnil);
#endif
memset(&mbs,0,sizeof(mbstate_t));
s = StringValueCStr(str);
l = RSTRING_LEN(str);
cols = 0;
while (0 < l) {
r = mbrtowc(&wc, s, l, &mbs);
if (r == 0)
rb_raise(rb_eArgError, "NUL found");
width = wcwidth(wc);
if (width == -1)
rb_raise(rb_eArgError, "non-printable charactor found");
cols += width;
l -= r;
s += r;
}
return LONG2NUM(cols);
}
void
Init_terminfo(void)
{
putfunc_output = Qnil;
rb_global_variable(&putfunc_output);
cTermInfo = rb_define_class("TermInfo", rb_cObject);
eTermInfoError = rb_define_class_under(cTermInfo, "TermInfoError", rb_eRuntimeError);
rb_define_alloc_func(cTermInfo, rt_alloc);
rb_define_method(cTermInfo, "setupterm", rt_setupterm, 2);
rb_define_method(cTermInfo, "tigetflag", rt_tigetflag, 1);
rb_define_method(cTermInfo, "tigetnum", rt_tigetnum, 1);
rb_define_method(cTermInfo, "tigetstr", rt_tigetstr, 1);
rb_define_method(cTermInfo, "tparm", rt_tparm, -1);
rb_define_method(cTermInfo, "tputs", rt_tputs, 2);
rb_define_module_function(cTermInfo, "tiocgwinsz", rt_tiocgwinsz, 1);
rb_define_module_function(cTermInfo, "tiocswinsz", rt_tiocswinsz, 3);
rb_define_module_function(cTermInfo, "ctermid", rt_ctermid, 0);
rb_define_module_function(cTermInfo, "wcswidth", rt_wcswidth, 1);
}