insert mode bug fix and cleanup
[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
d9375810 19static char sccsid[] = "@(#)ttgeneric.c 3.40 (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;
2d152f42 52struct tt_str *gen_ICn;
e1daf7d6
EW
53struct tt_str *gen_IP;
54struct tt_str *gen_EI;
55struct tt_str *gen_DC;
2d152f42 56struct tt_str *gen_DCn;
e1daf7d6 57struct tt_str *gen_AL;
2d152f42 58struct tt_str *gen_ALn;
e1daf7d6 59struct tt_str *gen_DL;
2d152f42 60struct tt_str *gen_DLn;
e1daf7d6
EW
61struct tt_str *gen_CE;
62struct tt_str *gen_CD;
63struct tt_str *gen_CL;
64struct tt_str *gen_VS;
65struct tt_str *gen_VE;
66struct tt_str *gen_TI;
67struct tt_str *gen_TE;
68struct tt_str *gen_SO;
69struct tt_str *gen_SE;
70struct tt_str *gen_US;
71struct tt_str *gen_UE;
f6af6d5e
EW
72struct tt_str *gen_LE;
73struct tt_str *gen_ND;
e1daf7d6 74struct tt_str *gen_UP;
f6af6d5e 75struct tt_str *gen_DO;
e1daf7d6 76struct tt_str *gen_BC;
e1daf7d6 77struct tt_str *gen_NL;
16ea9636 78struct tt_str *gen_CR;
f6af6d5e 79struct tt_str *gen_HO;
e1daf7d6
EW
80struct tt_str *gen_AS;
81struct tt_str *gen_AE;
a6833679
EW
82struct tt_str *gen_XS;
83struct tt_str *gen_XE;
16ea9636 84struct tt_str *gen_SF;
2d152f42 85struct tt_str *gen_SFn;
16ea9636 86struct tt_str *gen_SR;
2d152f42 87struct tt_str *gen_SRn;
16ea9636 88struct tt_str *gen_CS;
745d1a08
EW
89char gen_MI;
90char gen_MS;
91char gen_AM;
92char gen_OS;
93char gen_BS;
16ea9636 94char gen_DA;
377a2410 95char gen_DB;
df7b254b 96char gen_NS;
b16d80c6 97char gen_XN;
745d1a08
EW
98int gen_CO;
99int gen_LI;
3babdf29
EW
100int gen_UG;
101int gen_SG;
745d1a08 102
73f690a1
EW
103gen_setinsert(new)
104char new;
105{
745d1a08
EW
106 if (new) {
107 if (gen_IM)
e1daf7d6 108 ttxputs(gen_IM);
745d1a08
EW
109 } else
110 if (gen_EI)
e1daf7d6 111 ttxputs(gen_EI);
3130283e 112 tt.tt_insert = new;
73f690a1
EW
113}
114
115gen_setmodes(new)
745d1a08 116register new;
73f690a1 117{
745d1a08
EW
118 register diff;
119
3130283e 120 diff = new ^ tt.tt_modes;
745d1a08
EW
121 if (diff & WWM_REV) {
122 if (new & WWM_REV) {
123 if (gen_SO)
e1daf7d6 124 ttxputs(gen_SO);
745d1a08
EW
125 } else
126 if (gen_SE)
e1daf7d6 127 ttxputs(gen_SE);
745d1a08
EW
128 }
129 if (diff & WWM_UL) {
130 if (new & WWM_UL) {
131 if (gen_US)
e1daf7d6 132 ttxputs(gen_US);
745d1a08
EW
133 } else
134 if (gen_UE)
e1daf7d6 135 ttxputs(gen_UE);
745d1a08 136 }
a830e8bb
EW
137 if (diff & WWM_GRP) {
138 if (new & WWM_GRP) {
a64e9887 139 if (gen_AS)
e1daf7d6 140 ttxputs(gen_AS);
a830e8bb 141 } else
a64e9887 142 if (gen_AE)
e1daf7d6 143 ttxputs(gen_AE);
a830e8bb 144 }
a6833679
EW
145 if (diff & WWM_USR) {
146 if (new & WWM_USR) {
147 if (gen_XS)
148 ttxputs(gen_XS);
149 } else
150 if (gen_XE)
151 ttxputs(gen_XE);
152 }
3130283e 153 tt.tt_modes = new;
73f690a1
EW
154}
155
2d152f42 156gen_insline(n)
73f690a1 157{
3130283e
EW
158 if (tt.tt_modes) /* for concept 100 */
159 gen_setmodes(0);
2d152f42
EW
160 if (gen_ALn)
161 ttpgoto(gen_ALn, 0, n, gen_LI - tt.tt_row);
162 else
163 while (--n >= 0)
164 tttputs(gen_AL, gen_LI - tt.tt_row);
73f690a1
EW
165}
166
2d152f42 167gen_delline(n)
73f690a1 168{
3130283e
EW
169 if (tt.tt_modes) /* for concept 100 */
170 gen_setmodes(0);
2d152f42
EW
171 if (gen_DLn)
172 ttpgoto(gen_DLn, 0, n, gen_LI - tt.tt_row);
173 else
174 while (--n >= 0)
175 tttputs(gen_DL, gen_LI - tt.tt_row);
73f690a1
EW
176}
177
178gen_putc(c)
179register char c;
180{
d9375810
EW
181 if (tt.tt_insert)
182 gen_setinsert(0);
3130283e
EW
183 if (tt.tt_nmodes != tt.tt_modes)
184 gen_setmodes(tt.tt_nmodes);
d9375810 185 ttputc(c);
3130283e 186 if (++tt.tt_col == gen_CO)
b16d80c6
EW
187 if (gen_XN)
188 tt.tt_col = tt.tt_row = -10;
189 else if (gen_AM)
3130283e 190 tt.tt_col = 0, tt.tt_row++;
5e785082 191 else
3130283e 192 tt.tt_col--;
73f690a1
EW
193}
194
3130283e 195gen_write(p, n)
e1daf7d6
EW
196 register char *p;
197 register n;
73f690a1 198{
d9375810
EW
199 if (tt.tt_insert)
200 gen_setinsert(0);
3130283e
EW
201 if (tt.tt_nmodes != tt.tt_modes)
202 gen_setmodes(tt.tt_nmodes);
d9375810
EW
203 ttwrite(p, n);
204 tt.tt_col += n;
3130283e 205 if (tt.tt_col == gen_CO)
b16d80c6
EW
206 if (gen_XN)
207 tt.tt_col = tt.tt_row = -10;
208 else if (gen_AM)
3130283e 209 tt.tt_col = 0, tt.tt_row++;
5e785082 210 else
3130283e 211 tt.tt_col--;
73f690a1
EW
212}
213
214gen_move(row, col)
1306887c 215register int row, col;
73f690a1 216{
3130283e 217 if (tt.tt_row == row && tt.tt_col == col)
745d1a08 218 return;
3130283e
EW
219 if (!gen_MI && tt.tt_insert)
220 gen_setinsert(0);
221 if (!gen_MS && tt.tt_modes)
222 gen_setmodes(0);
16ea9636
EW
223 if (row < tt.tt_scroll_top || row > tt.tt_scroll_bot)
224 gen_setscroll(tt.tt_nrow - 1, 0);
3130283e 225 if (tt.tt_row == row) {
16ea9636
EW
226 if (col == 0) {
227 ttxputs(gen_CR);
228 goto out;
229 }
3130283e 230 if (tt.tt_col == col - 1) {
745d1a08 231 if (gen_ND) {
e1daf7d6 232 ttxputs(gen_ND);
745d1a08
EW
233 goto out;
234 }
3130283e 235 } else if (tt.tt_col == col + 1) {
f6af6d5e
EW
236 if (gen_LE) {
237 ttxputs(gen_LE);
745d1a08
EW
238 goto out;
239 }
73f690a1
EW
240 }
241 }
3130283e
EW
242 if (tt.tt_col == col) {
243 if (tt.tt_row == row + 1) {
745d1a08 244 if (gen_UP) {
e1daf7d6 245 ttxputs(gen_UP);
745d1a08
EW
246 goto out;
247 }
f6af6d5e
EW
248 } else if (tt.tt_row == row - 1) {
249 ttxputs(gen_DO);
e1daf7d6 250 goto out;
73f690a1
EW
251 }
252 }
745d1a08 253 if (gen_HO && col == 0 && row == 0) {
e1daf7d6 254 ttxputs(gen_HO);
73f690a1
EW
255 goto out;
256 }
e1daf7d6 257 tttgoto(gen_CM, col, row);
73f690a1 258out:
3130283e
EW
259 tt.tt_col = col;
260 tt.tt_row = row;
73f690a1
EW
261}
262
ab8b3b31 263gen_start()
73f690a1 264{
745d1a08 265 if (gen_VS)
e1daf7d6 266 ttxputs(gen_VS);
7d9e29c8 267 if (gen_TI)
e1daf7d6
EW
268 ttxputs(gen_TI);
269 ttxputs(gen_CL);
3130283e 270 tt.tt_col = tt.tt_row = 0;
d9375810 271 tt.tt_insert = 0;
3130283e 272 tt.tt_nmodes = tt.tt_modes = 0;
73f690a1
EW
273}
274
fff1d01b 275gen_end()
73f690a1 276{
d9375810
EW
277 if (tt.tt_insert)
278 gen_setinsert(0);
7d9e29c8 279 if (gen_TE)
e1daf7d6 280 ttxputs(gen_TE);
745d1a08 281 if (gen_VE)
e1daf7d6 282 ttxputs(gen_VE);
73f690a1
EW
283}
284
285gen_clreol()
286{
3130283e
EW
287 if (tt.tt_modes) /* for concept 100 */
288 gen_setmodes(0);
e1daf7d6 289 tttputs(gen_CE, gen_CO - tt.tt_col);
73f690a1
EW
290}
291
292gen_clreos()
293{
3130283e
EW
294 if (tt.tt_modes) /* for concept 100 */
295 gen_setmodes(0);
e1daf7d6 296 tttputs(gen_CD, gen_LI - tt.tt_row);
73f690a1
EW
297}
298
299gen_clear()
300{
3130283e
EW
301 if (tt.tt_modes) /* for concept 100 */
302 gen_setmodes(0);
e1daf7d6 303 ttxputs(gen_CL);
73f690a1
EW
304}
305
d9375810
EW
306gen_inschar(c)
307register char c;
308{
309 if (!tt.tt_insert)
310 gen_setinsert(1);
311 if (tt.tt_nmodes != tt.tt_modes)
312 gen_setmodes(tt.tt_nmodes);
313 if (gen_IC)
314 tttputs(gen_IC, gen_CO - tt.tt_col);
315 ttputc(c);
316 if (gen_IP)
317 tttputs(gen_IP, gen_CO - tt.tt_col);
318 if (++tt.tt_col == gen_CO)
319 if (gen_XN)
320 tt.tt_col = tt.tt_row = -10;
321 else if (gen_AM)
322 tt.tt_col = 0, tt.tt_row++;
323 else
324 tt.tt_col--;
325}
326
327gen_insspace(n)
2d152f42
EW
328{
329 if (gen_ICn)
330 ttpgoto(gen_ICn, 0, n, gen_CO - tt.tt_col);
331 else
332 while (--n >= 0)
333 tttputs(gen_IC, gen_CO - tt.tt_col);
334}
335
336gen_delchar(n)
73f690a1 337{
2d152f42
EW
338 if (gen_DCn)
339 ttpgoto(gen_DCn, 0, n, gen_CO - tt.tt_col);
340 else
341 while (--n >= 0)
342 tttputs(gen_DC, gen_CO - tt.tt_col);
73f690a1
EW
343}
344
2d152f42 345gen_scroll_down(n)
16ea9636
EW
346{
347 gen_move(tt.tt_scroll_bot, 0);
2d152f42
EW
348 if (gen_SFn)
349 ttpgoto(gen_SFn, 0, n, n);
350 else
351 while (--n >= 0)
352 ttxputs(gen_SF);
16ea9636
EW
353}
354
2d152f42 355gen_scroll_up(n)
16ea9636
EW
356{
357 gen_move(tt.tt_scroll_top, 0);
2d152f42
EW
358 if (gen_SRn)
359 ttpgoto(gen_SRn, 0, n, n);
360 else
361 while (--n >= 0)
362 ttxputs(gen_SR);
16ea9636
EW
363}
364
365gen_setscroll(top, bot)
366{
367 tttgoto(gen_CS, bot, top);
368 tt.tt_scroll_top = top;
369 tt.tt_scroll_bot = bot;
370 tt.tt_row = tt.tt_col = -10;
371}
372
73f690a1
EW
373tt_generic()
374{
e1daf7d6
EW
375 gen_PC = tttgetstr("pc");
376 PC = gen_PC ? *gen_PC->ts_str : 0;
377 ospeed = wwoldtty.ww_sgttyb.sg_ospeed;
378
b1189050
EW
379 gen_CM = ttxgetstr("cm"); /* may not work */
380 gen_IM = ttxgetstr("im");
381 gen_IC = tttgetstr("ic");
2d152f42 382 gen_ICn = tttgetstr("IC");
b1189050
EW
383 gen_IP = tttgetstr("ip");
384 gen_EI = ttxgetstr("ei");
385 gen_DC = tttgetstr("dc");
2d152f42 386 gen_DCn = tttgetstr("DC");
b1189050 387 gen_AL = tttgetstr("al");
2d152f42 388 gen_ALn = tttgetstr("AL");
b1189050 389 gen_DL = tttgetstr("dl");
2d152f42 390 gen_DLn = tttgetstr("DL");
b1189050
EW
391 gen_CE = tttgetstr("ce");
392 gen_CD = tttgetstr("cd");
393 gen_CL = ttxgetstr("cl");
394 gen_VS = ttxgetstr("vs");
395 gen_VE = ttxgetstr("ve");
396 gen_TI = ttxgetstr("ti");
397 gen_TE = ttxgetstr("te");
398 gen_SO = ttxgetstr("so");
399 gen_SE = ttxgetstr("se");
400 gen_US = ttxgetstr("us");
401 gen_UE = ttxgetstr("ue");
f6af6d5e
EW
402 gen_LE = ttxgetstr("le");
403 gen_ND = ttxgetstr("nd");
b1189050 404 gen_UP = ttxgetstr("up");
f6af6d5e 405 gen_DO = ttxgetstr("do");
b1189050 406 gen_BC = ttxgetstr("bc");
b1189050 407 gen_NL = ttxgetstr("nl");
16ea9636 408 gen_CR = ttxgetstr("cr");
f6af6d5e 409 gen_HO = ttxgetstr("ho");
b1189050
EW
410 gen_AS = ttxgetstr("as");
411 gen_AE = ttxgetstr("ae");
a6833679
EW
412 gen_XS = ttxgetstr("XS");
413 gen_XE = ttxgetstr("XE");
16ea9636 414 gen_SF = ttxgetstr("sf");
2d152f42 415 gen_SFn = ttxgetstr("SF");
16ea9636 416 gen_SR = ttxgetstr("sr");
2d152f42 417 gen_SRn = ttxgetstr("SR");
16ea9636 418 gen_CS = ttxgetstr("cs");
745d1a08
EW
419 gen_MI = tgetflag("mi");
420 gen_MS = tgetflag("ms");
421 gen_AM = tgetflag("am");
422 gen_OS = tgetflag("os");
423 gen_BS = tgetflag("bs");
16ea9636 424 gen_DA = tgetflag("da");
377a2410 425 gen_DB = tgetflag("db");
df7b254b 426 gen_NS = tgetflag("ns");
b16d80c6 427 gen_XN = tgetflag("xn");
745d1a08
EW
428 gen_CO = tgetnum("co");
429 gen_LI = tgetnum("li");
3babdf29
EW
430 gen_UG = tgetnum("ug");
431 gen_SG = tgetnum("sg");
16ea9636
EW
432 if (gen_CL == 0 || gen_OS || gen_CM == 0)
433 return -1;
745d1a08 434
f6af6d5e
EW
435 /*
436 * Deal with obsolete termcap fields.
437 */
438 if (gen_LE == 0)
439 if (gen_BC)
440 gen_LE = gen_BC;
441 else if (gen_BS) {
442 static struct tt_str bc = { "\b", 1 };
443 gen_BC = &bc;
444 }
e1daf7d6
EW
445 if (gen_NL == 0) {
446 static struct tt_str nl = { "\n", 1 };
447 gen_NL = &nl;
448 }
f6af6d5e
EW
449 if (gen_DO == 0)
450 gen_DO = gen_NL;
16ea9636
EW
451 if (gen_CR == 0) {
452 static struct tt_str cr = { "\r", 1 };
453 gen_CR = &cr;
454 }
f6af6d5e
EW
455 /*
456 * Most terminal will scroll with "nl", but very few specify "sf".
457 * We shouldn't use "do" here.
458 */
16ea9636
EW
459 if (gen_SF == 0 && !gen_NS)
460 gen_SF = gen_NL;
f6af6d5e 461 BC = gen_LE ? gen_LE->ts_str : 0;
e1daf7d6 462 UP = gen_UP ? gen_UP->ts_str : 0;
f6af6d5e
EW
463 /*
464 * Fix up display attributes that we can't handle, or don't
465 * really exist.
466 */
467 if (gen_SG > 0)
468 gen_SO = 0;
469 if (gen_UG > 0 || gen_US && gen_SO && ttstrcmp(gen_US, gen_SO) == 0)
470 gen_US = 0;
745d1a08 471
2d152f42 472 if (gen_IM)
2d152f42 473 tt.tt_inschar = gen_inschar;
d9375810
EW
474 else if (gen_IC)
475 tt.tt_insspace = gen_insspace;
745d1a08
EW
476 if (gen_DC)
477 tt.tt_delchar = gen_delchar;
478 if (gen_AL)
479 tt.tt_insline = gen_insline;
480 if (gen_DL)
481 tt.tt_delline = gen_delline;
482 if (gen_CE)
483 tt.tt_clreol = gen_clreol;
484 if (gen_CD)
485 tt.tt_clreos = gen_clreos;
16ea9636
EW
486 if (gen_SF)
487 tt.tt_scroll_down = gen_scroll_down;
488 /*
489 * Don't allow scroll_up if da or db but not cs.
490 * See comment in wwscroll.c.
491 */
492 if (gen_SR && (gen_CS || !gen_DA && !gen_DB))
493 tt.tt_scroll_up = gen_scroll_up;
494 if (gen_CS)
495 tt.tt_setscroll = gen_setscroll;
5e785082
EW
496 if (gen_SO)
497 tt.tt_availmodes |= WWM_REV;
498 if (gen_US)
499 tt.tt_availmodes |= WWM_UL;
a64e9887
EW
500 if (gen_AS)
501 tt.tt_availmodes |= WWM_GRP;
a6833679
EW
502 if (gen_XS)
503 tt.tt_availmodes |= WWM_USR;
5e785082 504 tt.tt_wrap = gen_AM;
377a2410 505 tt.tt_retain = gen_DB;
745d1a08 506 tt.tt_ncol = gen_CO;
745d1a08 507 tt.tt_nrow = gen_LI;
ab8b3b31 508 tt.tt_start = gen_start;
fff1d01b 509 tt.tt_end = gen_end;
73f690a1
EW
510 tt.tt_write = gen_write;
511 tt.tt_putc = gen_putc;
745d1a08 512 tt.tt_move = gen_move;
e1daf7d6 513 tt.tt_clear = gen_clear;
c1a57462 514 tt.tt_setmodes = gen_setmodes;
b16d80c6
EW
515 tt.tt_frame = gen_AS && ttstrcmp(gen_AS, &ansi_AS) == 0 ?
516 ansi_frame : gen_frame;
73f690a1
EW
517 return 0;
518}