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