Debian Codegraph @

src/ANSITerminal_unix_stubs.c

raw symbols SCIP
1 /* File: ANSITerminal_unix_stubs.c
2
3 Copyright (C) 2010
4
5 Christophe Troestler <[email protected]>
6 WWW: http://math.umons.ac.be/an/software/
7
8 This library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License version 3 or
10 later as published by the Free Software Foundation. See the file
11 LICENCE for more details.
12
13 This library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
16 LICENSE for more details. */
17
18 #include <caml/mlvalues.h>
19 #include <caml/alloc.h>
20 #include <caml/memory.h>
21 #include <caml/fail.h>
22
23 #include <sys/ioctl.h>
24 #include <termios.h>
25
26 /* Based on http://www.ohse.de/uwe/software/resize.c.html */
27 /* Inquire actual terminal size (this it what the kernel thinks - not
28 * was the user on the over end of the phone line has really). */
29 CAMLexport
30 value ANSITerminal_term_size(value vfd)
31 {
32 CAMLparam1(vfd);
33 CAMLlocal1(vsize);
34 int fd = Int_val(vfd);
35 int x, y;
36
37 #ifdef TIOCGSIZE
38 struct ttysize win;
39 #elif defined(TIOCGWINSZ)
40 struct winsize win;
41 #endif
42
43 #ifdef TIOCGSIZE
44 if (ioctl(fd, TIOCGSIZE, &win))
45 caml_failwith("ANSITerminal.size");
46 x = win.ts_cols;
47 y = win.ts_lines;
48 #elif defined TIOCGWINSZ
49 if (ioctl(fd, TIOCGWINSZ, &win))
50 caml_failwith("ANSITerminal.size");
51 x = win.ws_col;
52 y = win.ws_row;
53 #else
54 {
55 const char *s;
56 s = getenv("LINES");
57 if (s)
58 y = strtol(s,NULL,10);
59 else
60 y = 25;
61 s = getenv("COLUMNS");
62 if (s)
63 x = strtol(s,NULL,10);
64 else
65 x = 80;
66 }
67 #endif
68
69 vsize = caml_alloc_tuple(2);
70 Store_field(vsize, 0, Val_int(x));
71 Store_field(vsize, 1, Val_int(y));
72 CAMLreturn(vsize);
73 }
74