]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/window/ttgeneric.c
This commit was generated by cvs2svn to compensate for changes in r74784,
[FreeBSD/FreeBSD.git] / usr.bin / window / ttgeneric.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)ttgeneric.c 8.1 (Berkeley) 6/6/93";
39 static char rcsid[] = "@(#)$FreeBSD$";
40 #endif /* not lint */
41
42 #include "ww.h"
43 #include "tt.h"
44
45 char PC, *BC, *UP;
46
47         /* normal frame */
48 short gen_frame[16] = {
49         ' ', '|', '-', '+',
50         '|', '|', '+', '+',
51         '-', '+', '-', '+',
52         '+', '+', '+', '+'
53 };
54
55         /* ANSI graphics frame */
56 #define G (WWM_GRP << WWC_MSHIFT)
57 short ansi_frame[16] = {
58         ' ',    'x'|G,  'Q'|G,  'm'|G,
59         'x'|G,  'x'|G,  'l'|G,  't'|G,
60         'q'|G,  'j'|G,  'q'|G,  'v'|G,
61         'k'|G,  'u'|G,  'w'|G,  'n'|G
62 };
63 struct tt_str ansi_AS = {
64         "\033(0", 3
65 };
66
67 struct tt_str *gen_PC;
68 struct tt_str *gen_CM;
69 struct tt_str *gen_IM;
70 struct tt_str *gen_IC;
71 struct tt_str *gen_ICn;
72 struct tt_str *gen_IP;
73 struct tt_str *gen_EI;
74 struct tt_str *gen_DC;
75 struct tt_str *gen_DCn;
76 struct tt_str *gen_AL;
77 struct tt_str *gen_ALn;
78 struct tt_str *gen_DL;
79 struct tt_str *gen_DLn;
80 struct tt_str *gen_CE;
81 struct tt_str *gen_CD;
82 struct tt_str *gen_CL;
83 struct tt_str *gen_VS;
84 struct tt_str *gen_VE;
85 struct tt_str *gen_TI;
86 struct tt_str *gen_TE;
87 struct tt_str *gen_SO;
88 struct tt_str *gen_SE;
89 struct tt_str *gen_US;
90 struct tt_str *gen_UE;
91 struct tt_str *gen_LE;
92 struct tt_str *gen_ND;
93 struct tt_str *gen_UP;
94 struct tt_str *gen_DO;
95 struct tt_str *gen_BC;
96 struct tt_str *gen_NL;
97 struct tt_str *gen_CR;
98 struct tt_str *gen_HO;
99 struct tt_str *gen_AS;
100 struct tt_str *gen_AE;
101 struct tt_str *gen_XS;
102 struct tt_str *gen_XE;
103 struct tt_str *gen_SF;
104 struct tt_str *gen_SFn;
105 struct tt_str *gen_SR;
106 struct tt_str *gen_SRn;
107 struct tt_str *gen_CS;
108 char gen_MI;
109 char gen_MS;
110 char gen_AM;
111 char gen_OS;
112 char gen_BS;
113 char gen_DA;
114 char gen_DB;
115 char gen_NS;
116 char gen_XN;
117 int gen_CO;
118 int gen_LI;
119 int gen_UG;
120 int gen_SG;
121
122 gen_setinsert(new)
123 char new;
124 {
125         if (new) {
126                 if (gen_IM)
127                         ttxputs(gen_IM);
128         } else
129                 if (gen_EI)
130                         ttxputs(gen_EI);
131         tt.tt_insert = new;
132 }
133
134 gen_setmodes(new)
135 register new;
136 {
137         register diff;
138
139         diff = new ^ tt.tt_modes;
140         if (diff & WWM_REV) {
141                 if (new & WWM_REV) {
142                         if (gen_SO)
143                                 ttxputs(gen_SO);
144                 } else
145                         if (gen_SE)
146                                 ttxputs(gen_SE);
147         }
148         if (diff & WWM_UL) {
149                 if (new & WWM_UL) {
150                         if (gen_US)
151                                 ttxputs(gen_US);
152                 } else
153                         if (gen_UE)
154                                 ttxputs(gen_UE);
155         }
156         if (diff & WWM_GRP) {
157                 if (new & WWM_GRP) {
158                         if (gen_AS)
159                                 ttxputs(gen_AS);
160                 } else
161                         if (gen_AE)
162                                 ttxputs(gen_AE);
163         }
164         if (diff & WWM_USR) {
165                 if (new & WWM_USR) {
166                         if (gen_XS)
167                                 ttxputs(gen_XS);
168                 } else
169                         if (gen_XE)
170                                 ttxputs(gen_XE);
171         }
172         tt.tt_modes = new;
173 }
174
175 gen_insline(n)
176 {
177         if (tt.tt_modes)                        /* for concept 100 */
178                 gen_setmodes(0);
179         if (gen_ALn)
180                 ttpgoto(gen_ALn, 0, n, gen_LI - tt.tt_row);
181         else
182                 while (--n >= 0)
183                         tttputs(gen_AL, gen_LI - tt.tt_row);
184 }
185
186 gen_delline(n)
187 {
188         if (tt.tt_modes)                        /* for concept 100 */
189                 gen_setmodes(0);
190         if (gen_DLn)
191                 ttpgoto(gen_DLn, 0, n, gen_LI - tt.tt_row);
192         else
193                 while (--n >= 0)
194                         tttputs(gen_DL, gen_LI - tt.tt_row);
195 }
196
197 gen_putc(c)
198 register char c;
199 {
200         if (tt.tt_insert)
201                 gen_setinsert(0);
202         if (tt.tt_nmodes != tt.tt_modes)
203                 gen_setmodes(tt.tt_nmodes);
204         ttputc(c);
205         if (++tt.tt_col == gen_CO)
206                 if (gen_XN)
207                         tt.tt_col = tt.tt_row = -10;
208                 else if (gen_AM)
209                         tt.tt_col = 0, tt.tt_row++;
210                 else
211                         tt.tt_col--;
212 }
213
214 gen_write(p, n)
215         register char *p;
216         register n;
217 {
218         if (tt.tt_insert)
219                 gen_setinsert(0);
220         if (tt.tt_nmodes != tt.tt_modes)
221                 gen_setmodes(tt.tt_nmodes);
222         ttwrite(p, n);
223         tt.tt_col += n;
224         if (tt.tt_col == gen_CO)
225                 if (gen_XN)
226                         tt.tt_col = tt.tt_row = -10;
227                 else if (gen_AM)
228                         tt.tt_col = 0, tt.tt_row++;
229                 else
230                         tt.tt_col--;
231 }
232
233 gen_move(row, col)
234 register int row, col;
235 {
236         if (tt.tt_row == row && tt.tt_col == col)
237                 return;
238         if (!gen_MI && tt.tt_insert)
239                 gen_setinsert(0);
240         if (!gen_MS && tt.tt_modes)
241                 gen_setmodes(0);
242         if (row < tt.tt_scroll_top || row > tt.tt_scroll_bot)
243                 gen_setscroll(0, tt.tt_nrow - 1);
244         if (tt.tt_row == row) {
245                 if (col == 0) {
246                         ttxputs(gen_CR);
247                         goto out;
248                 }
249                 if (tt.tt_col == col - 1) {
250                         if (gen_ND) {
251                                 ttxputs(gen_ND);
252                                 goto out;
253                         }
254                 } else if (tt.tt_col == col + 1) {
255                         if (gen_LE) {
256                                 ttxputs(gen_LE);
257                                 goto out;
258                         }
259                 }
260         }
261         if (tt.tt_col == col) {
262                 if (tt.tt_row == row + 1) {
263                         if (gen_UP) {
264                                 ttxputs(gen_UP);
265                                 goto out;
266                         }
267                 } else if (tt.tt_row == row - 1) {
268                         ttxputs(gen_DO);
269                         goto out;
270                 }
271         }
272         if (gen_HO && col == 0 && row == 0) {
273                 ttxputs(gen_HO);
274                 goto out;
275         }
276         tttgoto(gen_CM, col, row);
277 out:
278         tt.tt_col = col;
279         tt.tt_row = row;
280 }
281
282 gen_start()
283 {
284         if (gen_VS)
285                 ttxputs(gen_VS);
286         if (gen_TI)
287                 ttxputs(gen_TI);
288         ttxputs(gen_CL);
289         tt.tt_col = tt.tt_row = 0;
290         tt.tt_insert = 0;
291         tt.tt_nmodes = tt.tt_modes = 0;
292 }
293
294 gen_end()
295 {
296         if (tt.tt_insert)
297                 gen_setinsert(0);
298         if (gen_TE)
299                 ttxputs(gen_TE);
300         if (gen_VE)
301                 ttxputs(gen_VE);
302 }
303
304 gen_clreol()
305 {
306         if (tt.tt_modes)                        /* for concept 100 */
307                 gen_setmodes(0);
308         tttputs(gen_CE, gen_CO - tt.tt_col);
309 }
310
311 gen_clreos()
312 {
313         if (tt.tt_modes)                        /* for concept 100 */
314                 gen_setmodes(0);
315         tttputs(gen_CD, gen_LI - tt.tt_row);
316 }
317
318 gen_clear()
319 {
320         if (tt.tt_modes)                        /* for concept 100 */
321                 gen_setmodes(0);
322         ttxputs(gen_CL);
323 }
324
325 gen_inschar(c)
326 register char c;
327 {
328         if (!tt.tt_insert)
329                 gen_setinsert(1);
330         if (tt.tt_nmodes != tt.tt_modes)
331                 gen_setmodes(tt.tt_nmodes);
332         if (gen_IC)
333                 tttputs(gen_IC, gen_CO - tt.tt_col);
334         ttputc(c);
335         if (gen_IP)
336                 tttputs(gen_IP, gen_CO - tt.tt_col);
337         if (++tt.tt_col == gen_CO)
338                 if (gen_XN)
339                         tt.tt_col = tt.tt_row = -10;
340                 else if (gen_AM)
341                         tt.tt_col = 0, tt.tt_row++;
342                 else
343                         tt.tt_col--;
344 }
345
346 gen_insspace(n)
347 {
348         if (gen_ICn)
349                 ttpgoto(gen_ICn, 0, n, gen_CO - tt.tt_col);
350         else
351                 while (--n >= 0)
352                         tttputs(gen_IC, gen_CO - tt.tt_col);
353 }
354
355 gen_delchar(n)
356 {
357         if (gen_DCn)
358                 ttpgoto(gen_DCn, 0, n, gen_CO - tt.tt_col);
359         else
360                 while (--n >= 0)
361                         tttputs(gen_DC, gen_CO - tt.tt_col);
362 }
363
364 gen_scroll_down(n)
365 {
366         gen_move(tt.tt_scroll_bot, 0);
367         if (gen_SFn)
368                 ttpgoto(gen_SFn, 0, n, n);
369         else
370                 while (--n >= 0)
371                         ttxputs(gen_SF);
372 }
373
374 gen_scroll_up(n)
375 {
376         gen_move(tt.tt_scroll_top, 0);
377         if (gen_SRn)
378                 ttpgoto(gen_SRn, 0, n, n);
379         else
380                 while (--n >= 0)
381                         ttxputs(gen_SR);
382 }
383
384 gen_setscroll(top, bot)
385 {
386         tttgoto(gen_CS, bot, top);
387         tt.tt_scroll_top = top;
388         tt.tt_scroll_bot = bot;
389         tt.tt_row = tt.tt_col = -10;
390 }
391
392 tt_generic()
393 {
394         gen_PC = tttgetstr("pc");
395         PC = gen_PC ? *gen_PC->ts_str : 0;
396
397         gen_CM = ttxgetstr("cm");               /* may not work */
398         gen_IM = ttxgetstr("im");
399         gen_IC = tttgetstr("ic");
400         gen_ICn = tttgetstr("IC");
401         gen_IP = tttgetstr("ip");
402         gen_EI = ttxgetstr("ei");
403         gen_DC = tttgetstr("dc");
404         gen_DCn = tttgetstr("DC");
405         gen_AL = tttgetstr("al");
406         gen_ALn = tttgetstr("AL");
407         gen_DL = tttgetstr("dl");
408         gen_DLn = tttgetstr("DL");
409         gen_CE = tttgetstr("ce");
410         gen_CD = tttgetstr("cd");
411         gen_CL = ttxgetstr("cl");
412         gen_VS = ttxgetstr("vs");
413         gen_VE = ttxgetstr("ve");
414         gen_TI = ttxgetstr("ti");
415         gen_TE = ttxgetstr("te");
416         gen_SO = ttxgetstr("so");
417         gen_SE = ttxgetstr("se");
418         gen_US = ttxgetstr("us");
419         gen_UE = ttxgetstr("ue");
420         gen_LE = ttxgetstr("le");
421         gen_ND = ttxgetstr("nd");
422         gen_UP = ttxgetstr("up");
423         gen_DO = ttxgetstr("do");
424         gen_BC = ttxgetstr("bc");
425         gen_NL = ttxgetstr("nl");
426         gen_CR = ttxgetstr("cr");
427         gen_HO = ttxgetstr("ho");
428         gen_AS = ttxgetstr("as");
429         gen_AE = ttxgetstr("ae");
430         gen_XS = ttxgetstr("XS");
431         gen_XE = ttxgetstr("XE");
432         gen_SF = ttxgetstr("sf");
433         gen_SFn = ttxgetstr("SF");
434         gen_SR = ttxgetstr("sr");
435         gen_SRn = ttxgetstr("SR");
436         gen_CS = ttxgetstr("cs");
437         gen_MI = tgetflag("mi");
438         gen_MS = tgetflag("ms");
439         gen_AM = tgetflag("am");
440         gen_OS = tgetflag("os");
441         gen_BS = tgetflag("bs");
442         gen_DA = tgetflag("da");
443         gen_DB = tgetflag("db");
444         gen_NS = tgetflag("ns");
445         gen_XN = tgetflag("xn");
446         gen_CO = tgetnum("co");
447         gen_LI = tgetnum("li");
448         gen_UG = tgetnum("ug");
449         gen_SG = tgetnum("sg");
450         if (gen_CL == 0 || gen_OS || gen_CM == 0)
451                 return -1;
452
453         /*
454          * Deal with obsolete termcap fields.
455          */
456         if (gen_LE == 0)
457                 if (gen_BC)
458                         gen_LE = gen_BC;
459                 else if (gen_BS) {
460                         static struct tt_str bc = { "\b", 1 };
461                         gen_BC = &bc;
462                 }
463         if (gen_NL == 0) {
464                 static struct tt_str nl = { "\n", 1 };
465                 gen_NL = &nl;
466         }
467         if (gen_DO == 0)
468                 gen_DO = gen_NL;
469         if (gen_CR == 0) {
470                 static struct tt_str cr = { "\r", 1 };
471                 gen_CR = &cr;
472         }
473         /*
474          * Most terminal will scroll with "nl", but very few specify "sf".
475          * We shouldn't use "do" here.
476          */
477         if (gen_SF == 0 && !gen_NS)
478                 gen_SF = gen_NL;
479         BC = gen_LE ? gen_LE->ts_str : 0;
480         UP = gen_UP ? gen_UP->ts_str : 0;
481         /*
482          * Fix up display attributes that we can't handle, or don't
483          * really exist.
484          */
485         if (gen_SG > 0)
486                 gen_SO = 0;
487         if (gen_UG > 0 || gen_US && gen_SO && ttstrcmp(gen_US, gen_SO) == 0)
488                 gen_US = 0;
489
490         if (gen_IM && gen_IM->ts_n == 0) {
491                 free((char *) gen_IM);
492                 gen_IM = 0;
493         }
494         if (gen_EI && gen_EI->ts_n == 0) {
495                 free((char *) gen_EI);
496                 gen_EI = 0;
497         }
498         if (gen_IC && gen_IC->ts_n == 0) {
499                 free((char *) gen_IC);
500                 gen_IC = 0;
501         }
502         if (gen_IM)
503                 tt.tt_inschar = gen_inschar;
504         else if (gen_IC)
505                 tt.tt_insspace = gen_insspace;
506         if (gen_DC)
507                 tt.tt_delchar = gen_delchar;
508         if (gen_AL)
509                 tt.tt_insline = gen_insline;
510         if (gen_DL)
511                 tt.tt_delline = gen_delline;
512         if (gen_CE)
513                 tt.tt_clreol = gen_clreol;
514         if (gen_CD)
515                 tt.tt_clreos = gen_clreos;
516         if (gen_SF)
517                 tt.tt_scroll_down = gen_scroll_down;
518         /*
519          * Don't allow scroll_up if da or db but not cs.
520          * See comment in wwscroll.c.
521          */
522         if (gen_SR && (gen_CS || !gen_DA && !gen_DB))
523                 tt.tt_scroll_up = gen_scroll_up;
524         if (gen_CS)
525                 tt.tt_setscroll = gen_setscroll;
526         if (gen_SO)
527                 tt.tt_availmodes |= WWM_REV;
528         if (gen_US)
529                 tt.tt_availmodes |= WWM_UL;
530         if (gen_AS)
531                 tt.tt_availmodes |= WWM_GRP;
532         if (gen_XS)
533                 tt.tt_availmodes |= WWM_USR;
534         tt.tt_wrap = gen_AM;
535         tt.tt_retain = gen_DB;
536         tt.tt_ncol = gen_CO;
537         tt.tt_nrow = gen_LI;
538         tt.tt_start = gen_start;
539         tt.tt_end = gen_end;
540         tt.tt_write = gen_write;
541         tt.tt_putc = gen_putc;
542         tt.tt_move = gen_move;
543         tt.tt_clear = gen_clear;
544         tt.tt_setmodes = gen_setmodes;
545         tt.tt_frame = gen_AS && ttstrcmp(gen_AS, &ansi_AS) == 0 ?
546                 ansi_frame : gen_frame;
547         return 0;
548 }