]>
diplodocus.org Git - nmh/blob - sbr/terminal.c
cfd883d6c8e15c89ab5eb6335fd96d06e08942ca
2 * termsbr.c -- termcap support
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
12 #include <sys/ioctl.h>
18 #ifdef WINSIZE_IN_PTEM
19 # include <sys/stream.h>
20 # include <sys/ptem.h>
23 static int initLI
= 0;
24 static int initCO
= 0;
26 static int LI
= 40; /* number of lines */
27 static int CO
= 80; /* number of columns */
28 static char *ti_clear
= NULL
; /* terminfo string to clear screen */
29 static char *ti_standend
= NULL
; /* terminfo string to end standout mode */
30 static char *ti_standbegin
= NULL
; /* terminfo string to begin standout mode */
31 static int termstatus
= 0; /* terminfo initialization status */
32 static char *termcbuf
= NULL
; /* tputs() output buffer */
33 static char *termcbufp
= NULL
; /* tputs() output buffer pointer */
34 static size_t termcbufsz
= 0; /* Size of termcbuf */
36 static void initialize_terminfo(void);
37 static int termbytes(TPUTS_PUTC_ARG
);
40 * Initialize the terminfo library.
44 initialize_terminfo(void)
51 rc
= setupterm(NULL
, fileno(stdout
), &errret
);
53 if (rc
!= 0 || errret
!= 1) {
59 if (!initCO
&& (CO
= tigetnum ("cols")) <= 0)
61 if (!initLI
&& (LI
= tigetnum ("lines")) <= 0)
64 ti_clear
= tigetstr ("clear");
65 ti_standbegin
= tigetstr ("smso");
66 ti_standend
= tigetstr ("rmso");
77 if (ioctl (fileno (stderr
), TIOCGWINSZ
, &win
) != NOTOK
78 && (width
= win
.ws_col
) > 0) {
82 #endif /* TIOCGWINSZ */
83 initialize_terminfo();
95 if (ioctl (fileno (stderr
), TIOCGWINSZ
, &win
) != NOTOK
96 && (LI
= win
.ws_row
) > 0)
99 #endif /* TIOCGWINSZ */
100 initialize_terminfo();
107 outc (TPUTS_PUTC_ARG c
)
114 nmh_clear_screen (void)
116 initialize_terminfo ();
119 tputs (ti_clear
, LI
, outc
);
129 * print in standout mode
132 SOprintf (char *fmt
, ...)
136 initialize_terminfo ();
137 if (!(ti_standbegin
&& ti_standend
))
140 tputs (ti_standbegin
, 1, outc
);
146 tputs (ti_standend
, 1, outc
);
152 * Return the specified capability as a string that has already been
153 * processed with tputs().
157 get_term_stringcap(char *capability
)
161 initialize_terminfo();
163 if (termstatus
== -1)
166 termcbufp
= termcbuf
;
168 parm
= tigetstr(capability
);
170 if (parm
== (char *) -1 || parm
== NULL
) {
174 tputs(parm
, 1, termbytes
);
182 * Return a parameterized terminfo capability
186 get_term_stringparm(char *capability
, long arg1
, long arg2
)
190 initialize_terminfo();
192 if (termstatus
== -1)
195 termcbufp
= termcbuf
;
197 parm
= tigetstr(capability
);
199 if (parm
== (char *) -1 || parm
== NULL
) {
203 parm
= tparm(parm
, arg1
, arg2
, 0, 0, 0, 0, 0, 0, 0);
205 tputs(parm
, 1, termbytes
);
213 * Return the value of the specified numeric capability
217 get_term_numcap(char *capability
)
219 initialize_terminfo();
221 if (termstatus
== -1)
224 return tigetnum(capability
);
228 * Store a sequence of characters in our local buffer
232 termbytes(TPUTS_PUTC_ARG c
)
237 * Bump up the buffer size if we've reached the end (leave room for
241 if ((offset
= termcbufp
- termcbuf
) - 1 >= termcbufsz
) {
243 termcbuf
= mh_xrealloc(termcbuf
, termcbufsz
);
244 termcbufp
= termcbuf
+ offset
;