less verbose...
[unix-history] / usr / src / usr.bin / window / ttgeneric.c
CommitLineData
60de5df9 1/*
46e9ea25
KB
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
5e8b0e60
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
60de5df9
EW
16 */
17
46e9ea25 18#ifndef lint
7bb29073 19static char sccsid[] = "@(#)ttgeneric.c 3.37 (Berkeley) %G%";
46e9ea25
KB
20#endif /* not lint */
21
73f690a1 22#include "ww.h"
fff1d01b 23#include "tt.h"
73f690a1 24
769cbf09
EW
25char PC, *BC, *UP;
26short ospeed;
94c16993 27
a6a7cd35 28 /* normal frame */
a830e8bb 29short gen_frame[16] = {
73f690a1
EW
30 ' ', '|', '-', '+',
31 '|', '|', '+', '+',
fff1d01b 32 '-', '+', '-', '+',
73f690a1
EW
33 '+', '+', '+', '+'
34};
35
a6a7cd35
EW
36 /* ANSI graphics frame */
37#define G (WWM_GRP << WWC_MSHIFT)
38short ansi_frame[16] = {
39 ' ', 'x'|G, 'Q'|G, 'm'|G,
40 'x'|G, 'x'|G, 'l'|G, 't'|G,
41 'q'|G, 'j'|G, 'q'|G, 'v'|G,
42 'k'|G, 'u'|G, 'w'|G, 'n'|G
43};
b16d80c6
EW
44struct tt_str ansi_AS = {
45 "\033(0", 3
46};
a6a7cd35 47
f6af6d5e 48struct tt_str *gen_PC;
e1daf7d6
EW
49struct tt_str *gen_CM;
50struct tt_str *gen_IM;
51struct tt_str *gen_IC;
52struct tt_str *gen_IP;
53struct tt_str *gen_EI;
54struct tt_str *gen_DC;
55struct tt_str *gen_AL;
56struct tt_str *gen_DL;
57struct tt_str *gen_CE;
58struct tt_str *gen_CD;
59struct tt_str *gen_CL;
60struct tt_str *gen_VS;
61struct tt_str *gen_VE;
62struct tt_str *gen_TI;
63struct tt_str *gen_TE;
64struct tt_str *gen_SO;
65struct tt_str *gen_SE;
66struct tt_str *gen_US;
67struct tt_str *gen_UE;
f6af6d5e
EW
68struct tt_str *gen_LE;
69struct tt_str *gen_ND;
e1daf7d6 70struct tt_str *gen_UP;
f6af6d5e 71struct tt_str *gen_DO;
e1daf7d6 72struct tt_str *gen_BC;
e1daf7d6 73struct tt_str *gen_NL;
16ea9636 74struct tt_str *gen_CR;
f6af6d5e 75struct tt_str *gen_HO;
e1daf7d6
EW
76struct tt_str *gen_AS;
77struct tt_str *gen_AE;
a6833679
EW
78struct tt_str *gen_XS;
79struct tt_str *gen_XE;
16ea9636
EW
80struct tt_str *gen_SF;
81struct tt_str *gen_SR;
82struct tt_str *gen_CS;
745d1a08
EW
83char gen_MI;
84char gen_MS;
85char gen_AM;
86char gen_OS;
87char gen_BS;
16ea9636 88char gen_DA;
377a2410 89char gen_DB;
df7b254b 90char gen_NS;
b16d80c6 91char gen_XN;
745d1a08
EW
92int gen_CO;
93int gen_LI;
3babdf29
EW
94int gen_UG;
95int gen_SG;
745d1a08 96
73f690a1
EW
97gen_setinsert(new)
98char new;
99{
745d1a08
EW
100 if (new) {
101 if (gen_IM)
e1daf7d6 102 ttxputs(gen_IM);
745d1a08
EW
103 } else
104 if (gen_EI)
e1daf7d6 105 ttxputs(gen_EI);
3130283e 106 tt.tt_insert = new;
73f690a1
EW
107}
108
109gen_setmodes(new)
745d1a08 110register new;
73f690a1 111{
745d1a08
EW
112 register diff;
113
3130283e 114 diff = new ^ tt.tt_modes;
745d1a08
EW
115 if (diff & WWM_REV) {
116 if (new & WWM_REV) {
117 if (gen_SO)
e1daf7d6 118 ttxputs(gen_SO);
745d1a08
EW
119 } else
120 if (gen_SE)
e1daf7d6 121 ttxputs(gen_SE);
745d1a08
EW
122 }
123 if (diff & WWM_UL) {
124 if (new & WWM_UL) {
125 if (gen_US)
e1daf7d6 126 ttxputs(gen_US);
745d1a08
EW
127 } else
128 if (gen_UE)
e1daf7d6 129 ttxputs(gen_UE);
745d1a08 130 }
a830e8bb
EW
131 if (diff & WWM_GRP) {
132 if (new & WWM_GRP) {
a64e9887 133 if (gen_AS)
e1daf7d6 134 ttxputs(gen_AS);
a830e8bb 135 } else
a64e9887 136 if (gen_AE)
e1daf7d6 137 ttxputs(gen_AE);
a830e8bb 138 }
a6833679
EW
139 if (diff & WWM_USR) {
140 if (new & WWM_USR) {
141 if (gen_XS)
142 ttxputs(gen_XS);
143 } else
144 if (gen_XE)
145 ttxputs(gen_XE);
146 }
3130283e 147 tt.tt_modes = new;
73f690a1
EW
148}
149
150gen_insline()
151{
3130283e
EW
152 if (tt.tt_modes) /* for concept 100 */
153 gen_setmodes(0);
745d1a08 154 if (gen_AL)
b1189050 155 tttputs(gen_AL, gen_LI - tt.tt_row);
73f690a1
EW
156}
157
158gen_delline()
159{
3130283e
EW
160 if (tt.tt_modes) /* for concept 100 */
161 gen_setmodes(0);
745d1a08 162 if (gen_DL)
b1189050 163 tttputs(gen_DL, gen_LI - tt.tt_row);
73f690a1
EW
164}
165
166gen_putc(c)
167register char c;
168{
3130283e
EW
169 if (tt.tt_ninsert != tt.tt_insert)
170 gen_setinsert(tt.tt_ninsert);
171 if (tt.tt_nmodes != tt.tt_modes)
172 gen_setmodes(tt.tt_nmodes);
173 if (tt.tt_insert) {
745d1a08 174 if (gen_IC)
b1189050
EW
175 tttputs(gen_IC, gen_CO - tt.tt_col);
176 ttputc(c);
745d1a08 177 if (gen_IP)
b1189050 178 tttputs(gen_IP, gen_CO - tt.tt_col);
745d1a08 179 } else
b1189050 180 ttputc(c);
3130283e 181 if (++tt.tt_col == gen_CO)
b16d80c6
EW
182 if (gen_XN)
183 tt.tt_col = tt.tt_row = -10;
184 else if (gen_AM)
3130283e 185 tt.tt_col = 0, tt.tt_row++;
5e785082 186 else
3130283e 187 tt.tt_col--;
73f690a1
EW
188}
189
3130283e 190gen_write(p, n)
e1daf7d6
EW
191 register char *p;
192 register n;
73f690a1 193{
3130283e
EW
194 if (tt.tt_ninsert != tt.tt_insert)
195 gen_setinsert(tt.tt_ninsert);
196 if (tt.tt_nmodes != tt.tt_modes)
197 gen_setmodes(tt.tt_nmodes);
e1daf7d6 198 if (tt.tt_insert && (gen_IC || gen_IP)) {
73f690a1 199 while (--n >= 0) {
745d1a08 200 if (gen_IC)
b1189050
EW
201 tttputs(gen_IC, gen_CO - tt.tt_col);
202 ttputc(*p++);
745d1a08 203 if (gen_IP)
b1189050 204 tttputs(gen_IP, gen_CO - tt.tt_col);
3130283e 205 tt.tt_col++;
73f690a1
EW
206 }
207 } else {
3130283e 208 tt.tt_col += n;
e1daf7d6 209 ttwrite(p, n);
73f690a1 210 }
3130283e 211 if (tt.tt_col == gen_CO)
b16d80c6
EW
212 if (gen_XN)
213 tt.tt_col = tt.tt_row = -10;
214 else if (gen_AM)
3130283e 215 tt.tt_col = 0, tt.tt_row++;
5e785082 216 else
3130283e 217 tt.tt_col--;
73f690a1
EW
218}
219
220gen_move(row, col)
1306887c 221register int row, col;
73f690a1 222{
3130283e 223 if (tt.tt_row == row && tt.tt_col == col)
745d1a08 224 return;
3130283e
EW
225 if (!gen_MI && tt.tt_insert)
226 gen_setinsert(0);
227 if (!gen_MS && tt.tt_modes)
228 gen_setmodes(0);
16ea9636
EW
229 if (row < tt.tt_scroll_top || row > tt.tt_scroll_bot)
230 gen_setscroll(tt.tt_nrow - 1, 0);
3130283e 231 if (tt.tt_row == row) {
16ea9636
EW
232 if (col == 0) {
233 ttxputs(gen_CR);
234 goto out;
235 }
3130283e 236 if (tt.tt_col == col - 1) {
745d1a08 237 if (gen_ND) {
e1daf7d6 238 ttxputs(gen_ND);
745d1a08
EW
239 goto out;
240 }
3130283e 241 } else if (tt.tt_col == col + 1) {
f6af6d5e
EW
242 if (gen_LE) {
243 ttxputs(gen_LE);
745d1a08
EW
244 goto out;
245 }
73f690a1
EW
246 }
247 }
3130283e
EW
248 if (tt.tt_col == col) {
249 if (tt.tt_row == row + 1) {
745d1a08 250 if (gen_UP) {
e1daf7d6 251 ttxputs(gen_UP);
745d1a08
EW
252 goto out;
253 }
f6af6d5e
EW
254 } else if (tt.tt_row == row - 1) {
255 ttxputs(gen_DO);
e1daf7d6 256 goto out;
73f690a1
EW
257 }
258 }
745d1a08 259 if (gen_HO && col == 0 && row == 0) {
e1daf7d6 260 ttxputs(gen_HO);
73f690a1
EW
261 goto out;
262 }
e1daf7d6 263 tttgoto(gen_CM, col, row);
73f690a1 264out:
3130283e
EW
265 tt.tt_col = col;
266 tt.tt_row = row;
73f690a1
EW
267}
268
269gen_init()
73f690a1 270{
745d1a08 271 if (gen_VS)
e1daf7d6 272 ttxputs(gen_VS);
7d9e29c8 273 if (gen_TI)
e1daf7d6
EW
274 ttxputs(gen_TI);
275 ttxputs(gen_CL);
3130283e
EW
276 tt.tt_col = tt.tt_row = 0;
277 tt.tt_ninsert = tt.tt_insert = 0;
278 tt.tt_nmodes = tt.tt_modes = 0;
73f690a1
EW
279}
280
fff1d01b 281gen_end()
73f690a1 282{
7d9e29c8 283 if (gen_TE)
e1daf7d6 284 ttxputs(gen_TE);
745d1a08 285 if (gen_VE)
e1daf7d6 286 ttxputs(gen_VE);
73f690a1
EW
287}
288
289gen_clreol()
290{
3130283e
EW
291 if (tt.tt_modes) /* for concept 100 */
292 gen_setmodes(0);
e1daf7d6 293 tttputs(gen_CE, gen_CO - tt.tt_col);
73f690a1
EW
294}
295
296gen_clreos()
297{
3130283e
EW
298 if (tt.tt_modes) /* for concept 100 */
299 gen_setmodes(0);
e1daf7d6 300 tttputs(gen_CD, gen_LI - tt.tt_row);
73f690a1
EW
301}
302
303gen_clear()
304{
3130283e
EW
305 if (tt.tt_modes) /* for concept 100 */
306 gen_setmodes(0);
e1daf7d6 307 ttxputs(gen_CL);
73f690a1
EW
308}
309
310gen_delchar()
311{
e1daf7d6 312 tttputs(gen_DC, gen_CO - tt.tt_col);
73f690a1
EW
313}
314
16ea9636
EW
315gen_scroll_down()
316{
317 gen_move(tt.tt_scroll_bot, 0);
318 ttxputs(gen_SF);
319}
320
321gen_scroll_up()
322{
323 gen_move(tt.tt_scroll_top, 0);
324 ttxputs(gen_SR);
325}
326
327gen_setscroll(top, bot)
328{
329 tttgoto(gen_CS, bot, top);
330 tt.tt_scroll_top = top;
331 tt.tt_scroll_bot = bot;
332 tt.tt_row = tt.tt_col = -10;
333}
334
73f690a1
EW
335tt_generic()
336{
e1daf7d6
EW
337 gen_PC = tttgetstr("pc");
338 PC = gen_PC ? *gen_PC->ts_str : 0;
339 ospeed = wwoldtty.ww_sgttyb.sg_ospeed;
340
b1189050
EW
341 gen_CM = ttxgetstr("cm"); /* may not work */
342 gen_IM = ttxgetstr("im");
343 gen_IC = tttgetstr("ic");
344 gen_IP = tttgetstr("ip");
345 gen_EI = ttxgetstr("ei");
346 gen_DC = tttgetstr("dc");
347 gen_AL = tttgetstr("al");
348 gen_DL = tttgetstr("dl");
349 gen_CE = tttgetstr("ce");
350 gen_CD = tttgetstr("cd");
351 gen_CL = ttxgetstr("cl");
352 gen_VS = ttxgetstr("vs");
353 gen_VE = ttxgetstr("ve");
354 gen_TI = ttxgetstr("ti");
355 gen_TE = ttxgetstr("te");
356 gen_SO = ttxgetstr("so");
357 gen_SE = ttxgetstr("se");
358 gen_US = ttxgetstr("us");
359 gen_UE = ttxgetstr("ue");
f6af6d5e
EW
360 gen_LE = ttxgetstr("le");
361 gen_ND = ttxgetstr("nd");
b1189050 362 gen_UP = ttxgetstr("up");
f6af6d5e 363 gen_DO = ttxgetstr("do");
b1189050 364 gen_BC = ttxgetstr("bc");
b1189050 365 gen_NL = ttxgetstr("nl");
16ea9636 366 gen_CR = ttxgetstr("cr");
f6af6d5e 367 gen_HO = ttxgetstr("ho");
b1189050
EW
368 gen_AS = ttxgetstr("as");
369 gen_AE = ttxgetstr("ae");
a6833679
EW
370 gen_XS = ttxgetstr("XS");
371 gen_XE = ttxgetstr("XE");
16ea9636
EW
372 gen_SF = ttxgetstr("sf");
373 gen_SR = ttxgetstr("sr");
374 gen_CS = ttxgetstr("cs");
745d1a08
EW
375 gen_MI = tgetflag("mi");
376 gen_MS = tgetflag("ms");
377 gen_AM = tgetflag("am");
378 gen_OS = tgetflag("os");
379 gen_BS = tgetflag("bs");
16ea9636 380 gen_DA = tgetflag("da");
377a2410 381 gen_DB = tgetflag("db");
df7b254b 382 gen_NS = tgetflag("ns");
b16d80c6 383 gen_XN = tgetflag("xn");
745d1a08
EW
384 gen_CO = tgetnum("co");
385 gen_LI = tgetnum("li");
3babdf29
EW
386 gen_UG = tgetnum("ug");
387 gen_SG = tgetnum("sg");
16ea9636
EW
388 if (gen_CL == 0 || gen_OS || gen_CM == 0)
389 return -1;
745d1a08 390
f6af6d5e
EW
391 /*
392 * Deal with obsolete termcap fields.
393 */
394 if (gen_LE == 0)
395 if (gen_BC)
396 gen_LE = gen_BC;
397 else if (gen_BS) {
398 static struct tt_str bc = { "\b", 1 };
399 gen_BC = &bc;
400 }
e1daf7d6
EW
401 if (gen_NL == 0) {
402 static struct tt_str nl = { "\n", 1 };
403 gen_NL = &nl;
404 }
f6af6d5e
EW
405 if (gen_DO == 0)
406 gen_DO = gen_NL;
16ea9636
EW
407 if (gen_CR == 0) {
408 static struct tt_str cr = { "\r", 1 };
409 gen_CR = &cr;
410 }
f6af6d5e
EW
411 /*
412 * Most terminal will scroll with "nl", but very few specify "sf".
413 * We shouldn't use "do" here.
414 */
16ea9636
EW
415 if (gen_SF == 0 && !gen_NS)
416 gen_SF = gen_NL;
f6af6d5e 417 BC = gen_LE ? gen_LE->ts_str : 0;
e1daf7d6 418 UP = gen_UP ? gen_UP->ts_str : 0;
f6af6d5e
EW
419 /*
420 * Fix up display attributes that we can't handle, or don't
421 * really exist.
422 */
423 if (gen_SG > 0)
424 gen_SO = 0;
425 if (gen_UG > 0 || gen_US && gen_SO && ttstrcmp(gen_US, gen_SO) == 0)
426 gen_US = 0;
745d1a08 427
745d1a08
EW
428 if (gen_DC)
429 tt.tt_delchar = gen_delchar;
430 if (gen_AL)
431 tt.tt_insline = gen_insline;
432 if (gen_DL)
433 tt.tt_delline = gen_delline;
434 if (gen_CE)
435 tt.tt_clreol = gen_clreol;
436 if (gen_CD)
437 tt.tt_clreos = gen_clreos;
16ea9636
EW
438 if (gen_SF)
439 tt.tt_scroll_down = gen_scroll_down;
440 /*
441 * Don't allow scroll_up if da or db but not cs.
442 * See comment in wwscroll.c.
443 */
444 if (gen_SR && (gen_CS || !gen_DA && !gen_DB))
445 tt.tt_scroll_up = gen_scroll_up;
446 if (gen_CS)
447 tt.tt_setscroll = gen_setscroll;
5e785082
EW
448 if (gen_SO)
449 tt.tt_availmodes |= WWM_REV;
450 if (gen_US)
451 tt.tt_availmodes |= WWM_UL;
a64e9887
EW
452 if (gen_AS)
453 tt.tt_availmodes |= WWM_GRP;
a6833679
EW
454 if (gen_XS)
455 tt.tt_availmodes |= WWM_USR;
7bb29073 456 tt.tt_hasinsert = gen_IM || gen_IC;
5e785082 457 tt.tt_wrap = gen_AM;
377a2410 458 tt.tt_retain = gen_DB;
745d1a08 459 tt.tt_ncol = gen_CO;
745d1a08 460 tt.tt_nrow = gen_LI;
73f690a1 461 tt.tt_init = gen_init;
fff1d01b 462 tt.tt_end = gen_end;
73f690a1
EW
463 tt.tt_write = gen_write;
464 tt.tt_putc = gen_putc;
745d1a08 465 tt.tt_move = gen_move;
e1daf7d6 466 tt.tt_clear = gen_clear;
c1a57462
EW
467 tt.tt_setinsert = gen_setinsert;
468 tt.tt_setmodes = gen_setmodes;
b16d80c6
EW
469 tt.tt_frame = gen_AS && ttstrcmp(gen_AS, &ansi_AS) == 0 ?
470 ansi_frame : gen_frame;
73f690a1
EW
471 return 0;
472}