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