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