]>
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>
22 static int initLI
= 0;
23 static int initCO
= 0;
25 static int LI
= 40; /* number of lines */
26 static int CO
= 80; /* number of columns */
27 static char *ti_clear
= NULL
; /* terminfo string to clear screen */
28 static char *ti_standend
= NULL
; /* terminfo string to end standout mode */
29 static char *ti_standbegin
= NULL
; /* terminfo string to begin standout mode */
30 static int termstatus
= 0; /* terminfo initialization status */
31 static char *termcbuf
= NULL
; /* tputs() output buffer */
32 static char *termcbufp
= NULL
; /* tputs() output buffer pointer */
33 static size_t termcbufsz
= 0; /* Size of termcbuf */
35 static void initialize_terminfo(void);
36 static int termbytes(TPUTS_PUTC_ARG
);
39 * Initialize the terminfo library.
43 initialize_terminfo(void)
50 rc
= setupterm(NULL
, fileno(stdout
), &errret
);
52 if (rc
!= 0 || errret
!= 1) {
58 if (!initCO
&& (CO
= tigetnum ("cols")) <= 0)
60 if (!initLI
&& (LI
= tigetnum ("lines")) <= 0)
63 ti_clear
= tigetstr ("clear");
64 ti_standbegin
= tigetstr ("smso");
65 ti_standend
= tigetstr ("rmso");
76 if (ioctl (fileno (stderr
), TIOCGWINSZ
, &win
) != NOTOK
77 && (width
= win
.ws_col
) > 0) {
81 #endif /* TIOCGWINSZ */
82 initialize_terminfo();
94 if (ioctl (fileno (stderr
), TIOCGWINSZ
, &win
) != NOTOK
95 && (LI
= win
.ws_row
) > 0)
98 #endif /* TIOCGWINSZ */
99 initialize_terminfo();
106 outc (TPUTS_PUTC_ARG c
)
113 nmh_clear_screen (void)
115 initialize_terminfo ();
118 tputs (ti_clear
, LI
, outc
);
128 * print in standout mode
131 SOprintf (char *fmt
, ...)
135 initialize_terminfo ();
136 if (!(ti_standbegin
&& ti_standend
))
139 tputs (ti_standbegin
, 1, outc
);
145 tputs (ti_standend
, 1, outc
);
151 * Return the specified capability as a string that has already been
152 * processed with tputs().
156 get_term_stringcap(char *capability
)
160 initialize_terminfo();
162 if (termstatus
== -1)
165 termcbufp
= termcbuf
;
167 parm
= tigetstr(capability
);
169 if (parm
== (char *) -1 || parm
== NULL
) {
173 tputs(parm
, 1, termbytes
);
181 * Return a parameterized terminfo capability
185 get_term_stringparm(char *capability
, long arg1
, long arg2
)
189 initialize_terminfo();
191 if (termstatus
== -1)
194 termcbufp
= termcbuf
;
196 parm
= tigetstr(capability
);
198 if (parm
== (char *) -1 || parm
== NULL
) {
202 parm
= tparm(parm
, arg1
, arg2
, 0, 0, 0, 0, 0, 0, 0);
204 tputs(parm
, 1, termbytes
);
212 * Return the value of the specified numeric capability
216 get_term_numcap(char *capability
)
218 initialize_terminfo();
220 if (termstatus
== -1)
223 return tigetnum(capability
);
227 * Store a sequence of characters in our local buffer
231 termbytes(TPUTS_PUTC_ARG c
)
236 * Bump up the buffer size if we've reached the end (leave room for
240 if ((offset
= termcbufp
- termcbuf
) - 1 >= termcbufsz
) {
242 termcbuf
= mh_xrealloc(termcbuf
, termcbufsz
);
243 termcbufp
= termcbuf
+ offset
;