1 /* $NetBSD: ttgeneric.c,v 1.10 2009/04/14 08:50:06 lukem Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)ttgeneric.c 8.1 (Berkeley) 6/6/93";
40 __RCSID("$NetBSD: ttgeneric.c,v 1.10 2009/04/14 08:50:06 lukem Exp $");
54 short gen_frame
[16] = {
61 /* ANSI graphics frame */
62 #define G (WWM_GRP << WWC_MSHIFT)
63 short ansi_frame
[16] = {
64 ' ', 'x'|G
, 'Q'|G
, 'm'|G
,
65 'x'|G
, 'x'|G
, 'l'|G
, 't'|G
,
66 'q'|G
, 'j'|G
, 'q'|G
, 'v'|G
,
67 'k'|G
, 'u'|G
, 'w'|G
, 'n'|G
69 struct tt_str ansi_AS
= {
73 struct tt_str
*gen_PC
;
74 struct tt_str
*gen_CM
;
75 struct tt_str
*gen_IM
;
76 struct tt_str
*gen_IC
;
77 struct tt_str
*gen_ICn
;
78 struct tt_str
*gen_IP
;
79 struct tt_str
*gen_EI
;
80 struct tt_str
*gen_DC
;
81 struct tt_str
*gen_DCn
;
82 struct tt_str
*gen_AL
;
83 struct tt_str
*gen_ALn
;
84 struct tt_str
*gen_DL
;
85 struct tt_str
*gen_DLn
;
86 struct tt_str
*gen_CE
;
87 struct tt_str
*gen_CD
;
88 struct tt_str
*gen_CL
;
89 struct tt_str
*gen_VS
;
90 struct tt_str
*gen_VE
;
91 struct tt_str
*gen_TI
;
92 struct tt_str
*gen_TE
;
93 struct tt_str
*gen_SO
;
94 struct tt_str
*gen_SE
;
95 struct tt_str
*gen_US
;
96 struct tt_str
*gen_UE
;
97 struct tt_str
*gen_LE
;
98 struct tt_str
*gen_ND
;
99 struct tt_str
*gen_UP
;
100 struct tt_str
*gen_DO
;
101 struct tt_str
*gen_BC
;
102 struct tt_str
*gen_NL
;
103 struct tt_str
*gen_CR
;
104 struct tt_str
*gen_HO
;
105 struct tt_str
*gen_AS
;
106 struct tt_str
*gen_AE
;
107 struct tt_str
*gen_XS
;
108 struct tt_str
*gen_XE
;
109 struct tt_str
*gen_SF
;
110 struct tt_str
*gen_SFn
;
111 struct tt_str
*gen_SR
;
112 struct tt_str
*gen_SRn
;
113 struct tt_str
*gen_CS
;
128 void gen_clear(void);
129 void gen_clreol(void);
130 void gen_clreos(void);
131 void gen_delchar(int);
132 void gen_delline(int);
134 void gen_inschar(char);
135 void gen_insline(int);
136 void gen_insspace(int);
137 void gen_move(int, int);
139 void gen_scroll_down(int);
140 void gen_scroll_up(int);
141 void gen_setinsert(char);
142 void gen_setmodes(int);
143 void gen_setscroll(int, int);
144 void gen_start(void);
145 void gen_write(const char *, int);
148 gen_setinsert(char new)
160 gen_setmodes(int new)
164 diff
= new ^ tt
.tt_modes
;
165 if (diff
& WWM_REV
) {
172 if (gen_UE
&& gen_US
&& new & WWM_UL
&&
173 !strcmp(gen_SE
->ts_str
, gen_UE
->ts_str
))
184 if (gen_SE
&& gen_SO
&& new & WWM_REV
&&
185 !strcmp(gen_UE
->ts_str
, gen_SE
->ts_str
))
189 if (diff
& WWM_GRP
) {
197 if (diff
& WWM_USR
) {
211 if (tt
.tt_modes
) /* for concept 100 */
214 ttpgoto(gen_ALn
, 0, n
, gen_LI
- tt
.tt_row
);
217 tttputs(gen_AL
, gen_LI
- tt
.tt_row
);
223 if (tt
.tt_modes
) /* for concept 100 */
226 ttpgoto(gen_DLn
, 0, n
, gen_LI
- tt
.tt_row
);
229 tttputs(gen_DL
, gen_LI
- tt
.tt_row
);
237 if (tt
.tt_nmodes
!= tt
.tt_modes
)
238 gen_setmodes(tt
.tt_nmodes
);
240 if (++tt
.tt_col
== gen_CO
) {
242 tt
.tt_col
= tt
.tt_row
= -10;
244 tt
.tt_col
= 0, tt
.tt_row
++;
251 gen_write(const char *p
, int n
)
255 if (tt
.tt_nmodes
!= tt
.tt_modes
)
256 gen_setmodes(tt
.tt_nmodes
);
259 if (tt
.tt_col
== gen_CO
) {
261 tt
.tt_col
= tt
.tt_row
= -10;
263 tt
.tt_col
= 0, tt
.tt_row
++;
270 gen_move(int row
, int col
)
272 if (tt
.tt_row
== row
&& tt
.tt_col
== col
)
274 if (!gen_MI
&& tt
.tt_insert
)
276 if (!gen_MS
&& tt
.tt_modes
)
278 if (row
< tt
.tt_scroll_top
|| row
> tt
.tt_scroll_bot
)
279 gen_setscroll(0, tt
.tt_nrow
- 1);
280 if (tt
.tt_row
== row
) {
285 if (tt
.tt_col
== col
- 1) {
290 } else if (tt
.tt_col
== col
+ 1) {
297 if (tt
.tt_col
== col
) {
298 if (tt
.tt_row
== row
+ 1) {
303 } else if (tt
.tt_row
== row
- 1) {
308 if (gen_HO
&& col
== 0 && row
== 0) {
312 tttgoto(gen_CM
, col
, row
);
326 tt
.tt_col
= tt
.tt_row
= 0;
328 tt
.tt_nmodes
= tt
.tt_modes
= 0;
345 if (tt
.tt_modes
) /* for concept 100 */
347 tttputs(gen_CE
, gen_CO
- tt
.tt_col
);
353 if (tt
.tt_modes
) /* for concept 100 */
355 tttputs(gen_CD
, gen_LI
- tt
.tt_row
);
361 if (tt
.tt_modes
) /* for concept 100 */
371 if (tt
.tt_nmodes
!= tt
.tt_modes
)
372 gen_setmodes(tt
.tt_nmodes
);
374 tttputs(gen_IC
, gen_CO
- tt
.tt_col
);
377 tttputs(gen_IP
, gen_CO
- tt
.tt_col
);
378 if (++tt
.tt_col
== gen_CO
) {
380 tt
.tt_col
= tt
.tt_row
= -10;
382 tt
.tt_col
= 0, tt
.tt_row
++;
392 ttpgoto(gen_ICn
, 0, n
, gen_CO
- tt
.tt_col
);
395 tttputs(gen_IC
, gen_CO
- tt
.tt_col
);
402 ttpgoto(gen_DCn
, 0, n
, gen_CO
- tt
.tt_col
);
405 tttputs(gen_DC
, gen_CO
- tt
.tt_col
);
409 gen_scroll_down(int n
)
411 gen_move(tt
.tt_scroll_bot
, 0);
413 ttpgoto(gen_SFn
, 0, n
, n
);
422 gen_move(tt
.tt_scroll_top
, 0);
424 ttpgoto(gen_SRn
, 0, n
, n
);
431 gen_setscroll(int top
, int bot
)
433 tttgoto(gen_CS
, bot
, top
);
434 tt
.tt_scroll_top
= top
;
435 tt
.tt_scroll_bot
= bot
;
436 tt
.tt_row
= tt
.tt_col
= -10;
442 gen_PC
= tttgetstr("pc");
443 PC
= gen_PC
? *gen_PC
->ts_str
: 0;
446 gen_CM
= ttxgetstr("cm"); /* may not work */
447 gen_IM
= ttxgetstr("im");
448 gen_IC
= tttgetstr("ic");
449 gen_ICn
= tttgetstr("IC");
450 gen_IP
= tttgetstr("ip");
451 gen_EI
= ttxgetstr("ei");
452 gen_DC
= tttgetstr("dc");
453 gen_DCn
= tttgetstr("DC");
454 gen_AL
= tttgetstr("al");
455 gen_ALn
= tttgetstr("AL");
456 gen_DL
= tttgetstr("dl");
457 gen_DLn
= tttgetstr("DL");
458 gen_CE
= tttgetstr("ce");
459 gen_CD
= tttgetstr("cd");
460 gen_CL
= ttxgetstr("cl");
461 gen_VS
= ttxgetstr("vs");
462 gen_VE
= ttxgetstr("ve");
463 gen_TI
= ttxgetstr("ti");
464 gen_TE
= ttxgetstr("te");
465 gen_SO
= ttxgetstr("so");
466 gen_SE
= ttxgetstr("se");
467 gen_US
= ttxgetstr("us");
468 gen_UE
= ttxgetstr("ue");
469 gen_LE
= ttxgetstr("le");
470 gen_ND
= ttxgetstr("nd");
471 gen_UP
= ttxgetstr("up");
472 gen_DO
= ttxgetstr("do");
473 gen_BC
= ttxgetstr("bc");
474 gen_NL
= ttxgetstr("nl");
475 gen_CR
= ttxgetstr("cr");
476 gen_HO
= ttxgetstr("ho");
477 gen_AS
= ttxgetstr("as");
478 gen_AE
= ttxgetstr("ae");
479 gen_XS
= ttxgetstr("XS");
480 gen_XE
= ttxgetstr("XE");
481 gen_SF
= ttxgetstr("sf");
482 gen_SFn
= ttxgetstr("SF");
483 gen_SR
= ttxgetstr("sr");
484 gen_SRn
= ttxgetstr("SR");
485 gen_CS
= ttxgetstr("cs");
486 gen_MI
= tgetflag("mi");
487 gen_MS
= tgetflag("ms");
488 gen_AM
= tgetflag("am");
489 gen_OS
= tgetflag("os");
490 gen_BS
= tgetflag("bs");
491 gen_DA
= tgetflag("da");
492 gen_DB
= tgetflag("db");
493 gen_NS
= tgetflag("ns");
494 gen_XN
= tgetflag("xn");
495 gen_CO
= tgetnum("co");
496 gen_LI
= tgetnum("li");
497 gen_UG
= tgetnum("ug");
498 gen_SG
= tgetnum("sg");
499 if (gen_CL
== 0 || gen_OS
|| gen_CM
== 0)
503 * Deal with obsolete termcap fields.
509 static struct tt_str bc
= { "\b", 1 };
514 static struct tt_str nl
= { "\n", 1 };
520 static struct tt_str cr
= { "\r", 1 };
524 * Most terminal will scroll with "nl", but very few specify "sf".
525 * We shouldn't use "do" here.
527 if (gen_SF
== 0 && !gen_NS
)
529 BC
= gen_LE
? __UNCONST(gen_LE
->ts_str
) : 0;
530 UP
= gen_UP
? __UNCONST(gen_UP
->ts_str
) : 0;
532 * Fix up display attributes that we can't handle, or don't
537 if (gen_UG
> 0 || (gen_US
&& gen_SO
&& ttstrcmp(gen_US
, gen_SO
) == 0))
540 if (gen_IM
&& gen_IM
->ts_n
== 0) {
541 free((char *) gen_IM
);
544 if (gen_EI
&& gen_EI
->ts_n
== 0) {
545 free((char *) gen_EI
);
548 if (gen_IC
&& gen_IC
->ts_n
== 0) {
549 free((char *) gen_IC
);
553 tt
.tt_inschar
= gen_inschar
;
555 tt
.tt_insspace
= gen_insspace
;
557 tt
.tt_delchar
= gen_delchar
;
559 tt
.tt_insline
= gen_insline
;
561 tt
.tt_delline
= gen_delline
;
563 tt
.tt_clreol
= gen_clreol
;
565 tt
.tt_clreos
= gen_clreos
;
567 tt
.tt_scroll_down
= gen_scroll_down
;
569 * Don't allow scroll_up if da or db but not cs.
570 * See comment in wwscroll.c.
572 if (gen_SR
&& (gen_CS
|| (!gen_DA
&& !gen_DB
)))
573 tt
.tt_scroll_up
= gen_scroll_up
;
575 tt
.tt_setscroll
= gen_setscroll
;
577 tt
.tt_availmodes
|= WWM_REV
;
579 tt
.tt_availmodes
|= WWM_UL
;
581 tt
.tt_availmodes
|= WWM_GRP
;
583 tt
.tt_availmodes
|= WWM_USR
;
585 tt
.tt_retain
= gen_DB
;
588 tt
.tt_start
= gen_start
;
590 tt
.tt_write
= gen_write
;
591 tt
.tt_putc
= gen_putc
;
592 tt
.tt_move
= gen_move
;
593 tt
.tt_clear
= gen_clear
;
594 tt
.tt_setmodes
= gen_setmodes
;
595 tt
.tt_frame
= gen_AS
&& ttstrcmp(gen_AS
, &ansi_AS
) == 0 ?
596 ansi_frame
: gen_frame
;