]> diplodocus.org Git - nmh/blob - sbr/terminal.c
cfd883d6c8e15c89ab5eb6335fd96d06e08942ca
[nmh] / sbr / terminal.c
1 /*
2 * termsbr.c -- termcap support
3 *
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11
12 #include <sys/ioctl.h>
13
14 #include <curses.h>
15 #include <term.h>
16 #include <termios.h>
17
18 #ifdef WINSIZE_IN_PTEM
19 # include <sys/stream.h>
20 # include <sys/ptem.h>
21 #endif
22
23 static int initLI = 0;
24 static int initCO = 0;
25
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 */
35
36 static void initialize_terminfo(void);
37 static int termbytes(TPUTS_PUTC_ARG);
38
39 /*
40 * Initialize the terminfo library.
41 */
42
43 static void
44 initialize_terminfo(void)
45 {
46 int errret, rc;
47
48 if (termstatus)
49 return;
50
51 rc = setupterm(NULL, fileno(stdout), &errret);
52
53 if (rc != 0 || errret != 1) {
54 termstatus = -1;
55 return;
56 }
57 termstatus = 1;
58
59 if (!initCO && (CO = tigetnum ("cols")) <= 0)
60 CO = 80;
61 if (!initLI && (LI = tigetnum ("lines")) <= 0)
62 LI = 24;
63
64 ti_clear = tigetstr ("clear");
65 ti_standbegin = tigetstr ("smso");
66 ti_standend = tigetstr ("rmso");
67 }
68
69
70 int
71 sc_width (void)
72 {
73 #ifdef TIOCGWINSZ
74 struct winsize win;
75 int width;
76
77 if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
78 && (width = win.ws_col) > 0) {
79 CO = width;
80 initCO++;
81 } else
82 #endif /* TIOCGWINSZ */
83 initialize_terminfo();
84
85 return CO;
86 }
87
88
89 int
90 sc_length (void)
91 {
92 #ifdef TIOCGWINSZ
93 struct winsize win;
94
95 if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
96 && (LI = win.ws_row) > 0)
97 initLI++;
98 else
99 #endif /* TIOCGWINSZ */
100 initialize_terminfo();
101
102 return LI;
103 }
104
105
106 static int
107 outc (TPUTS_PUTC_ARG c)
108 {
109 return putchar(c);
110 }
111
112
113 void
114 nmh_clear_screen (void)
115 {
116 initialize_terminfo ();
117
118 if (ti_clear)
119 tputs (ti_clear, LI, outc);
120 else {
121 putchar('\f');
122 }
123
124 fflush (stdout);
125 }
126
127
128 /*
129 * print in standout mode
130 */
131 int
132 SOprintf (char *fmt, ...)
133 {
134 va_list ap;
135
136 initialize_terminfo ();
137 if (!(ti_standbegin && ti_standend))
138 return NOTOK;
139
140 tputs (ti_standbegin, 1, outc);
141
142 va_start(ap, fmt);
143 vprintf (fmt, ap);
144 va_end(ap);
145
146 tputs (ti_standend, 1, outc);
147
148 return OK;
149 }
150
151 /*
152 * Return the specified capability as a string that has already been
153 * processed with tputs().
154 */
155
156 char *
157 get_term_stringcap(char *capability)
158 {
159 char *parm;
160
161 initialize_terminfo();
162
163 if (termstatus == -1)
164 return NULL;
165
166 termcbufp = termcbuf;
167
168 parm = tigetstr(capability);
169
170 if (parm == (char *) -1 || parm == NULL) {
171 return NULL;
172 }
173
174 tputs(parm, 1, termbytes);
175
176 *termcbufp = '\0';
177
178 return termcbuf;
179 }
180
181 /*
182 * Return a parameterized terminfo capability
183 */
184
185 char *
186 get_term_stringparm(char *capability, long arg1, long arg2)
187 {
188 char *parm;
189
190 initialize_terminfo();
191
192 if (termstatus == -1)
193 return NULL;
194
195 termcbufp = termcbuf;
196
197 parm = tigetstr(capability);
198
199 if (parm == (char *) -1 || parm == NULL) {
200 return NULL;
201 }
202
203 parm = tparm(parm, arg1, arg2, 0, 0, 0, 0, 0, 0, 0);
204
205 tputs(parm, 1, termbytes);
206
207 *termcbufp = '\0';
208
209 return termcbuf;
210 }
211
212 /*
213 * Return the value of the specified numeric capability
214 */
215
216 int
217 get_term_numcap(char *capability)
218 {
219 initialize_terminfo();
220
221 if (termstatus == -1)
222 return -1;
223
224 return tigetnum(capability);
225 }
226
227 /*
228 * Store a sequence of characters in our local buffer
229 */
230
231 static int
232 termbytes(TPUTS_PUTC_ARG c)
233 {
234 size_t offset;
235
236 /*
237 * Bump up the buffer size if we've reached the end (leave room for
238 * a trailing NUL)
239 */
240
241 if ((offset = termcbufp - termcbuf) - 1 >= termcbufsz) {
242 termcbufsz += 64;
243 termcbuf = mh_xrealloc(termcbuf, termcbufsz);
244 termcbufp = termcbuf + offset;
245 }
246
247 *termcbufp++ = c;
248
249 return 0;
250 }