free ww_fmap if any
[unix-history] / usr / src / usr.bin / window / ttgeneric.c
CommitLineData
73f690a1 1#ifndef lint
3babdf29 2static char *sccsid = "@(#)ttgeneric.c 3.12 83/08/23";
73f690a1
EW
3#endif
4
5#include "ww.h"
fff1d01b 6#include "tt.h"
73f690a1 7
94c16993
EW
8char *tgoto();
9
73f690a1
EW
10char gen_frame[16] = {
11 ' ', '|', '-', '+',
12 '|', '|', '+', '+',
fff1d01b 13 '-', '+', '-', '+',
73f690a1
EW
14 '+', '+', '+', '+'
15};
16
745d1a08
EW
17int gen_row, gen_col;
18char gen_modes;
745d1a08
EW
19char gen_insert;
20
745d1a08
EW
21char *gen_CM;
22char *gen_IM;
23char *gen_IC;
24char *gen_IP;
25char *gen_EI;
26char *gen_DC;
27char *gen_AL;
28char *gen_DL;
29char *gen_CE;
30char *gen_CD;
31char *gen_CL;
32char *gen_VS;
33char *gen_VE;
34char *gen_SO;
35char *gen_SE;
36char *gen_US;
37char *gen_UE;
38char *gen_UP;
39char *gen_PC;
40char *gen_BC;
41char *gen_ND;
42char *gen_HO;
43char *gen_NL;
44char gen_MI;
45char gen_MS;
46char gen_AM;
47char gen_OS;
48char gen_BS;
49int gen_CO;
50int gen_LI;
3babdf29
EW
51int gen_UG;
52int gen_SG;
745d1a08
EW
53
54#define pc(c) putchar('c')
55#define ps(s) fputs((s), stdout)
56
73f690a1
EW
57gen_setinsert(new)
58char new;
59{
745d1a08
EW
60 if (gen_insert == new)
61 return;
62 if (new) {
63 if (gen_IM)
64 ps(gen_IM);
65 } else
66 if (gen_EI)
67 ps(gen_EI);
68 gen_insert = new;
73f690a1
EW
69}
70
71gen_setmodes(new)
745d1a08 72register new;
73f690a1 73{
745d1a08
EW
74 register diff;
75
7d77e730 76 new &= tt.tt_availmodes;
745d1a08
EW
77 if ((diff = new ^ gen_modes) == 0)
78 return;
79 if (diff & WWM_REV) {
80 if (new & WWM_REV) {
81 if (gen_SO)
82 ps(gen_SO);
83 } else
84 if (gen_SE)
85 ps(gen_SE);
86 }
87 if (diff & WWM_UL) {
88 if (new & WWM_UL) {
89 if (gen_US)
90 ps(gen_US);
91 } else
92 if (gen_UE)
93 ps(gen_UE);
94 }
95 gen_modes = new;
73f690a1
EW
96}
97
98gen_insline()
99{
745d1a08 100 if (gen_AL)
c3f80cb5 101 tt_tputs(gen_AL, gen_LI - gen_row);
73f690a1
EW
102}
103
104gen_delline()
105{
745d1a08 106 if (gen_DL)
c3f80cb5 107 tt_tputs(gen_DL, gen_LI - gen_row);
73f690a1
EW
108}
109
110gen_putc(c)
111register char c;
112{
745d1a08
EW
113 if (gen_insert) {
114 if (gen_IC)
c3f80cb5 115 tt_tputs(gen_IC, gen_CO - gen_col);
745d1a08
EW
116 putchar(c);
117 if (gen_IP)
c3f80cb5 118 tt_tputs(gen_IP, gen_CO - gen_col);
745d1a08 119 } else
73f690a1 120 putchar(c);
5e785082
EW
121 if (++gen_col == gen_CO)
122 if (gen_AM)
123 gen_col = 0, gen_row++;
124 else
9574abf2 125 gen_col--;
73f690a1
EW
126}
127
128gen_write(start, end)
129register char *start, *end;
130{
73f690a1
EW
131 if (gen_insert) {
132 while (start <= end) {
745d1a08 133 if (gen_IC)
c3f80cb5 134 tt_tputs(gen_IC, gen_CO - gen_col);
745d1a08
EW
135 putchar(*start++);
136 if (gen_IP)
c3f80cb5 137 tt_tputs(gen_IP, gen_CO - gen_col);
73f690a1
EW
138 gen_col++;
139 }
140 } else {
141 gen_col += end - start + 1;
142 while (start <= end)
745d1a08 143 putchar(*start++);
73f690a1 144 }
5e785082
EW
145 if (gen_col == gen_CO)
146 if (gen_AM)
147 gen_col = 0, gen_row++;
148 else
9574abf2 149 gen_col--;
73f690a1
EW
150}
151
152gen_blank(n)
153register n;
154{
155 if (n <= 0)
156 return;
73f690a1
EW
157 if (gen_insert) {
158 while (--n >= 0) {
745d1a08 159 if (gen_IC)
c3f80cb5 160 tt_tputs(gen_IC, gen_CO - gen_col);
73f690a1 161 putchar(' ');
745d1a08 162 if (gen_IP)
c3f80cb5 163 tt_tputs(gen_IP, gen_CO - gen_col);
73f690a1
EW
164 gen_col++;
165 }
166 } else {
167 gen_col += n;
168 while (--n >= 0)
169 putchar(' ');
170 }
5e785082
EW
171 if (gen_col == gen_CO)
172 if (gen_AM)
173 gen_col = 0, gen_row++;
174 else
9574abf2 175 gen_col--;
73f690a1
EW
176}
177
178gen_move(row, col)
179register char row, col;
180{
745d1a08
EW
181 if (gen_row == row && gen_col == col)
182 return;
183 if (!gen_MI && gen_insert)
184 if (gen_EI)
185 ps(gen_EI);
186 if (!gen_MS && gen_modes & WWM_REV)
187 if (gen_SE)
188 ps(gen_SE);
73f690a1
EW
189 if (gen_row == row) {
190 if (gen_col == col)
191 return;
192 if (gen_col == col - 1) {
745d1a08
EW
193 if (gen_ND) {
194 ps(gen_ND);
195 goto out;
196 }
73f690a1 197 } else if (gen_col == col + 1) {
745d1a08
EW
198 if (gen_BC) {
199 ps(gen_BC);
200 goto out;
201 }
73f690a1
EW
202 }
203 }
204 if (gen_col == col) {
205 if (gen_row == row + 1) {
745d1a08
EW
206 if (gen_UP) {
207 ps(gen_UP);
208 goto out;
209 }
73f690a1 210 } else if (gen_row == row + 1) {
745d1a08
EW
211 if (gen_NL) {
212 ps(gen_NL);
213 goto out;
214 }
73f690a1
EW
215 }
216 }
745d1a08
EW
217 if (gen_HO && col == 0 && row == 0) {
218 ps(gen_HO);
73f690a1
EW
219 goto out;
220 }
c3f80cb5 221 ps(tgoto(gen_CM, col, row));
73f690a1
EW
222out:
223 gen_col = col;
224 gen_row = row;
745d1a08
EW
225 if (!gen_MI && gen_insert)
226 if (gen_IM)
227 ps(gen_IM);
228 if (!gen_MS && gen_modes & WWM_REV)
229 if (gen_SO)
230 ps(gen_SO);
73f690a1
EW
231}
232
233gen_init()
73f690a1 234{
745d1a08
EW
235 if (gen_VS)
236 ps(gen_VS);
fff1d01b
EW
237 if (gen_CL)
238 ps(gen_CL);
73f690a1
EW
239 gen_col = gen_row = 0;
240 gen_insert = 0;
73f690a1
EW
241 gen_modes = 0;
242}
243
fff1d01b 244gen_end()
73f690a1 245{
745d1a08
EW
246 gen_setmodes(0);
247 gen_setinsert(0);
248 if (gen_VE)
249 ps(gen_VE);
73f690a1
EW
250}
251
252gen_clreol()
253{
745d1a08 254 if (gen_CE)
c3f80cb5 255 tt_tputs(gen_CE, gen_CO - gen_col);
73f690a1
EW
256}
257
258gen_clreos()
259{
745d1a08 260 if (gen_CD)
c3f80cb5 261 tt_tputs(gen_CD, gen_LI - gen_row);
73f690a1
EW
262}
263
264gen_clear()
265{
745d1a08
EW
266 if (gen_CL)
267 ps(gen_CL);
73f690a1
EW
268}
269
270gen_delchar()
271{
745d1a08 272 if (gen_DC)
c3f80cb5 273 tt_tputs(gen_DC, gen_CO - gen_col);
73f690a1
EW
274}
275
276tt_generic()
277{
c3f80cb5
EW
278 gen_CM = tt_xgetstr("cm"); /* may not work */
279 gen_IM = tt_xgetstr("im");
280 gen_IC = tt_tgetstr("ic");
281 gen_IP = tt_tgetstr("ip");
282 gen_EI = tt_xgetstr("ei");
283 gen_DC = tt_tgetstr("dc");
284 gen_AL = tt_tgetstr("al");
285 gen_DL = tt_tgetstr("dl");
286 gen_CE = tt_tgetstr("ce");
287 gen_CD = tt_tgetstr("cd");
288 gen_CL = tt_xgetstr("cl");
289 gen_VS = tt_xgetstr("vs");
290 gen_VE = tt_xgetstr("ve");
291 gen_SO = tt_xgetstr("so");
292 gen_SE = tt_xgetstr("se");
293 gen_US = tt_xgetstr("us");
294 gen_UE = tt_xgetstr("ue");
295 gen_UP = tt_xgetstr("up");
296 gen_PC = tt_tgetstr("pc");
297 gen_BC = tt_xgetstr("bc");
298 gen_ND = tt_xgetstr("nd");
299 gen_HO = tt_xgetstr("ho");
300 gen_NL = tt_xgetstr("nl");
745d1a08
EW
301 gen_MI = tgetflag("mi");
302 gen_MS = tgetflag("ms");
303 gen_AM = tgetflag("am");
304 gen_OS = tgetflag("os");
305 gen_BS = tgetflag("bs");
306 gen_CO = tgetnum("co");
307 gen_LI = tgetnum("li");
3babdf29
EW
308 gen_UG = tgetnum("ug");
309 gen_SG = tgetnum("sg");
745d1a08
EW
310
311 if (gen_CL == 0 || gen_CM == 0 || gen_OS)
312 return -1;
313
314 if (gen_NL == 0)
315 gen_NL = "\n";
316 if (gen_BC == 0 && gen_BS)
317 gen_BC == "\b";
318
319 {
320 extern char PC, *BC, *UP;
321 extern short ospeed;
322
323 PC = gen_PC ? *gen_PC : 0;
324 BC = gen_BC;
325 UP = gen_UP;
326 ospeed = wwoldtty.ww_sgttyb.sg_ospeed;
327 }
328
745d1a08
EW
329 if (gen_IM)
330 tt.tt_setinsert = gen_setinsert;
331 if (gen_DC)
332 tt.tt_delchar = gen_delchar;
333 if (gen_AL)
334 tt.tt_insline = gen_insline;
335 if (gen_DL)
336 tt.tt_delline = gen_delline;
337 if (gen_CE)
338 tt.tt_clreol = gen_clreol;
339 if (gen_CD)
340 tt.tt_clreos = gen_clreos;
341 if (gen_CL)
342 tt.tt_clear = gen_clear;
3babdf29
EW
343 if (gen_SG > 0)
344 gen_SO = 0;
345 if (gen_UG > 0)
346 gen_US = 0;
5e785082
EW
347 if (gen_SO)
348 tt.tt_availmodes |= WWM_REV;
349 if (gen_US)
350 tt.tt_availmodes |= WWM_UL;
351 tt.tt_wrap = gen_AM;
745d1a08 352 tt.tt_ncol = gen_CO;
745d1a08 353 tt.tt_nrow = gen_LI;
73f690a1 354 tt.tt_init = gen_init;
fff1d01b 355 tt.tt_end = gen_end;
745d1a08
EW
356 tt.tt_setmodes = gen_setmodes;
357 tt.tt_blank = gen_blank;
73f690a1
EW
358 tt.tt_write = gen_write;
359 tt.tt_putc = gen_putc;
745d1a08 360 tt.tt_move = gen_move;
73f690a1
EW
361 tt.tt_frame = gen_frame;
362 return 0;
363}