|
root / lang / dune / patches / winsize.c
winsize.c C 56 lines 1.3 KB
 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
/*
 * winsize.c - illumos/Solaris compatible version for notty (used by dune)
 *
 * This file replaces vendor/notty/src-unix/native/winsize.c
 * in dune to fix compilation on illumos/Solaris where TIOCGWINSZ
 * is defined in sys/termios.h rather than sys/ioctl.h.
 *
 * Apply by copying this file to:
 *   dune-X.Y.Z/vendor/notty/src-unix/native/winsize.c
 *
 * For opam's bundled dune-local:
 *   opam-X.Y.Z/src_ext/dune-local/vendor/notty/src-unix/native/winsize.c
 */

#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/fail.h>

#ifdef __sun
/* illumos/Solaris: struct winsize and TIOCGWINSZ in termios */
#include <sys/termios.h>
#include <sys/ioctl.h>
#else
#include <sys/ioctl.h>
#endif

#include <unistd.h>
#include <signal.h>

CAMLprim value caml_notty_winsize (value fd) {
#ifdef TIOCGWINSZ
  struct winsize w;
  if (ioctl(Int_val(fd), TIOCGWINSZ, &w) == -1)
    caml_failwith("ioctl");
  value res = caml_alloc_small(2, 0);
  Field(res, 0) = Val_int(w.ws_col);
  Field(res, 1) = Val_int(w.ws_row);
  return res;
#else
  /* Fallback: return default 80x24 */
  value res = caml_alloc_small(2, 0);
  Field(res, 0) = Val_int(80);
  Field(res, 1) = Val_int(24);
  return res;
#endif
}

/* Return SIGWINCH signal number */
CAMLprim value caml_notty_winch_number (value unit) {
#ifdef SIGWINCH
  return Val_int(SIGWINCH);
#else
  return Val_int(0);
#endif
}