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