]>
diplodocus.org Git - nmh/blob - sbr/terminal.c
1 /* terminal.c -- termcap support
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
11 #include <sys/ioctl.h>
17 #ifdef WINSIZE_IN_PTEM
18 # include <sys/stream.h>
19 # include <sys/ptem.h>
24 static int initLI
= 0;
25 static int initCO
= 0;
27 static int LI
= 40; /* number of lines */
28 static int CO
= 80; /* number of columns */
29 static char *ti_clear
= NULL
; /* terminfo string to clear screen */
30 static char *ti_standend
= NULL
; /* terminfo string to end standout mode */
31 static char *ti_standbegin
= NULL
; /* terminfo string to begin standout mode */
32 static int termstatus
= 0; /* terminfo initialization status */
33 static char *termcbuf
= NULL
; /* tputs() output buffer */
34 static char *termcbufp
= NULL
; /* tputs() output buffer pointer */
35 static size_t termcbufsz
= 0; /* Size of termcbuf */
37 static void initialize_terminfo(void);
38 static int termbytes(TPUTS_PUTC_ARG
);
41 * Initialize the terminfo library.
45 initialize_terminfo(void)
52 rc
= setupterm(NULL
, fileno(stdout
), &errret
);
54 if (rc
!= 0 || errret
!= 1) {
60 if (!initCO
&& (CO
= tigetnum ("cols")) <= 0)
62 if (!initLI
&& (LI
= tigetnum ("lines")) <= 0)
65 ti_clear
= tigetstr ("clear");
66 ti_standbegin
= tigetstr ("smso");
67 ti_standend
= tigetstr ("rmso");
78 if (ioctl (fileno (stderr
), TIOCGWINSZ
, &win
) != NOTOK
79 && (width
= win
.ws_col
) > 0) {
83 #endif /* TIOCGWINSZ */
84 initialize_terminfo();
96 if (ioctl (fileno (stderr
), TIOCGWINSZ
, &win
) != NOTOK
97 && (LI
= win
.ws_row
) > 0)
100 #endif /* TIOCGWINSZ */
101 initialize_terminfo();
108 outc (TPUTS_PUTC_ARG c
)
115 nmh_clear_screen (void)
117 initialize_terminfo ();
120 tputs (ti_clear
, LI
, outc
);
130 * print in standout mode
133 SOprintf (char *fmt
, ...)
137 initialize_terminfo ();
138 if (!(ti_standbegin
&& ti_standend
))
141 tputs (ti_standbegin
, 1, outc
);
147 tputs (ti_standend
, 1, outc
);
153 * Get a string from the terminfo database for the current terminal.
155 * Retrieve the specified terminfo capability and return a string that
156 * can be output to the terminal. The string returned has already been
157 * processed by tputs(), so it is safe to output directly. The return
158 * value of this function is valid until the next call.
162 * capability - The name of the terminfo capability (see terminfo(5)).
164 * Returns a tputs-processed string, or NULL if terminal initialization failed
165 * or the capability wasn't found.
169 get_term_stringcap(char *capability
)
173 initialize_terminfo();
175 if (termstatus
== -1)
178 termcbufp
= termcbuf
;
180 parm
= tigetstr(capability
);
182 if (parm
== (char *) -1 || parm
== NULL
) {
186 tputs(parm
, 1, termbytes
);
194 * Get a parameterized string from the terminfo database for the current
197 * We don't yet have a standardized tparm() that will take a stdarg
198 * argument. Right now we don't want many parameters, so we only
199 * take two. Everything gets passed to tparm() as-is. If we need
200 * a capability with more arguments, we'll just add more later.
204 * capability - The name of the terminfo capability (see terminfo(5)).
205 * arg1..argN - Arguments 1-N.
207 * Returns a tparm and tputs-processed string, or NULL if there was a problem
208 * initialising the terminal or retrieving the capability.
211 get_term_stringparm(char *capability
, long arg1
, long arg2
)
215 initialize_terminfo();
217 if (termstatus
== -1)
220 termcbufp
= termcbuf
;
222 parm
= tigetstr(capability
);
224 if (parm
== (char *) -1 || parm
== NULL
) {
228 parm
= tparm(parm
, arg1
, arg2
, 0, 0, 0, 0, 0, 0, 0);
230 tputs(parm
, 1, termbytes
);
238 * Get a number from the terminfo database for the current terminal.
240 * Retrieve the specified terminfo capability and return the numeric
241 * value of that capability from the terminfo database.
245 * capability - The name of the terminfo capability (see terminfo(5)).
247 * Returns the output of tigetnum() for that capability, or -1 if it was
248 * unable to initialize the terminfo database.
251 get_term_numcap(char *capability
)
253 initialize_terminfo();
255 if (termstatus
== -1)
258 return tigetnum(capability
);
262 * Store a sequence of characters in our local buffer
266 termbytes(TPUTS_PUTC_ARG c
)
271 * Bump up the buffer size if we've reached the end (leave room for
275 if ((offset
= termcbufp
- termcbuf
) - 1 >= termcbufsz
) {
277 termcbuf
= mh_xrealloc(termcbuf
, termcbufsz
);
278 termcbufp
= termcbuf
+ offset
;