]>
diplodocus.org Git - nmh/blob - sbr/terminal.c
3 * termsbr.c -- termcap support
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
13 #include <sys/ioctl.h>
19 #ifdef WINSIZE_IN_PTEM
20 # include <sys/stream.h>
21 # 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 colums */
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) {
61 if (!initCO
&& (CO
= tigetnum ("cols")) <= 0)
63 if (!initLI
&& (LI
= tigetnum ("lines")) <= 0)
66 ti_clear
= tigetstr ("clear");
67 ti_standbegin
= tigetstr ("smso");
68 ti_standend
= tigetstr ("rmso");
79 if (ioctl (fileno (stderr
), TIOCGWINSZ
, &win
) != NOTOK
80 && (width
= win
.ws_col
) > 0) {
84 #endif /* TIOCGWINSZ */
85 initialize_terminfo();
97 if (ioctl (fileno (stderr
), TIOCGWINSZ
, &win
) != NOTOK
98 && (LI
= win
.ws_row
) > 0)
101 #endif /* TIOCGWINSZ */
102 initialize_terminfo();
109 outc (TPUTS_PUTC_ARG c
)
116 nmh_clear_screen (void)
118 initialize_terminfo ();
121 tputs (ti_clear
, LI
, outc
);
131 * print in standout mode
134 SOprintf (char *fmt
, ...)
138 initialize_terminfo ();
139 if (!(ti_standbegin
&& ti_standend
))
142 tputs (ti_standbegin
, 1, outc
);
148 tputs (ti_standend
, 1, outc
);
154 * Return the specified capability as a string that has already been
155 * processed with tputs().
159 get_term_stringcap(char *capability
)
163 initialize_terminfo();
165 if (termstatus
== -1)
168 termcbufp
= termcbuf
;
170 parm
= tigetstr(capability
);
172 if (parm
== (char *) -1 || parm
== NULL
) {
176 tputs(parm
, 1, termbytes
);
184 * Return a parameterized terminfo capability
188 get_term_stringparm(char *capability
, long arg1
, long arg2
)
192 initialize_terminfo();
194 if (termstatus
== -1)
197 termcbufp
= termcbuf
;
199 parm
= tigetstr(capability
);
201 if (parm
== (char *) -1 || parm
== NULL
) {
205 parm
= tparm(parm
, arg1
, arg2
, 0, 0, 0, 0, 0, 0, 0);
207 tputs(parm
, 1, termbytes
);
215 * Return the value of the specified numeric capability
219 get_term_numcap(char *capability
)
221 initialize_terminfo();
223 if (termstatus
== -1)
226 return tigetnum(capability
);
230 * Store a sequence of characters in our local buffer
234 termbytes(TPUTS_PUTC_ARG c
)
239 * Bump up the buffer size if we've reached the end (leave room for
243 if ((offset
= termcbufp
- termcbuf
) - 1 >= termcbufsz
) {
245 termcbuf
= mh_xrealloc(termcbuf
, termcbufsz
);
246 termcbufp
= termcbuf
+ offset
;