]>
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 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 * Return the specified capability as a string that has already been
154 * processed with tputs().
158 get_term_stringcap(char *capability
)
162 initialize_terminfo();
164 if (termstatus
== -1)
167 termcbufp
= termcbuf
;
169 parm
= tigetstr(capability
);
171 if (parm
== (char *) -1 || parm
== NULL
) {
175 tputs(parm
, 1, termbytes
);
183 * Return a parameterized terminfo capability
187 get_term_stringparm(char *capability
, long arg1
, long arg2
)
191 initialize_terminfo();
193 if (termstatus
== -1)
196 termcbufp
= termcbuf
;
198 parm
= tigetstr(capability
);
200 if (parm
== (char *) -1 || parm
== NULL
) {
204 parm
= tparm(parm
, arg1
, arg2
, 0, 0, 0, 0, 0, 0, 0);
206 tputs(parm
, 1, termbytes
);
214 * Return the value of the specified numeric capability
218 get_term_numcap(char *capability
)
220 initialize_terminfo();
222 if (termstatus
== -1)
225 return tigetnum(capability
);
229 * Store a sequence of characters in our local buffer
233 termbytes(TPUTS_PUTC_ARG c
)
238 * Bump up the buffer size if we've reached the end (leave room for
242 if ((offset
= termcbufp
- termcbuf
) - 1 >= termcbufsz
) {
244 termcbuf
= mh_xrealloc(termcbuf
, termcbufsz
);
245 termcbufp
= termcbuf
+ offset
;