1 /***********************************************************************/
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../LICENSE. */
12 /***********************************************************************/
14 /* $Id: terminfo.c 6045 2004-01-01 16:42:43Z doligez $ */
16 /* Read and output terminal commands */
24 #define Uninitialised (Val_int(0))
25 #define Bad_term (Val_int(1))
26 #define Good_term_tag 0
28 #if defined (HAS_TERMCAP) && !defined (NATIVE_CODE)
30 extern int tgetent (char * buffer, char * name);
31 extern char * tgetstr (char * id, char ** area);
32 extern int tgetnum (char * id);
33 extern int tputs (char * str, int count, int (*outchar)(int c));
35 static struct channel *chan;
36 static char area [1024];
37 static char *area_p = area;
39 static char *up = NULL;
40 static char *down = NULL;
41 static char *standout = NULL;
42 static char *standend = NULL;
44 CAMLprim value caml_terminfo_setup (value vchan)
47 static char buffer[1024];
50 chan = Channel (vchan);
52 term = getenv ("TERM");
53 if (term == NULL) return Bad_term;
54 if (tgetent(buffer, term) != 1) return Bad_term;
56 num_lines = tgetnum ("li");
57 up = tgetstr ("up", &area_p);
58 down = tgetstr ("do", &area_p);
59 standout = tgetstr ("us", &area_p);
60 standend = tgetstr ("ue", &area_p);
61 if (standout == NULL || standend == NULL){
62 standout = tgetstr ("so", &area_p);
63 standend = tgetstr ("se", &area_p);
65 Assert (area_p <= area + 1024);
66 if (num_lines == -1 || up == NULL || down == NULL
67 || standout == NULL || standend == NULL){
70 result = caml_alloc_small (1, Good_term_tag);
71 Field (result, 0) = Val_int (num_lines);
75 static int terminfo_putc (int c)
81 CAMLprim value caml_terminfo_backup (value lines)
85 for (i = 0; i < Int_val (lines); i++){
86 tputs (up, 1, terminfo_putc);
91 CAMLprim value caml_terminfo_standout (value start)
93 tputs (Bool_val (start) ? standout : standend, 1, terminfo_putc);
97 CAMLprim value caml_terminfo_resume (value lines)
101 for (i = 0; i < Int_val (lines); i++){
102 tputs (down, 1, terminfo_putc);
107 #else /* defined (HAS_TERMCAP) && !defined (NATIVE_CODE) */
109 CAMLexport value caml_terminfo_setup (value vchan)
114 CAMLexport value caml_terminfo_backup (value lines)
116 caml_invalid_argument("Terminfo.backup");
120 CAMLexport value caml_terminfo_standout (value start)
122 caml_invalid_argument("Terminfo.standout");
126 CAMLexport value caml_terminfo_resume (value lines)
128 caml_invalid_argument("Terminfo.resume");
132 #endif /* defined (HAS_TERMCAP) && !defined (NATIVE_CODE) */