]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/tcsh/ed.screen.c
This commit was generated by cvs2svn to compensate for changes in r165254,
[FreeBSD/FreeBSD.git] / contrib / tcsh / ed.screen.c
1 /* $Header: /src/pub/tcsh/ed.screen.c,v 3.63 2005/01/18 20:43:30 christos Exp $ */
2 /*
3  * ed.screen.c: Editor/termcap-curses interface
4  */
5 /*-
6  * Copyright (c) 1980, 1991 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 #include "sh.h"
34
35 RCSID("$Id: ed.screen.c,v 3.63 2005/01/18 20:43:30 christos Exp $")
36
37 #include "ed.h"
38 #include "tc.h"
39 #include "ed.defns.h"
40
41 /* #define DEBUG_LITERAL */
42
43 /*
44  * IMPORTANT NOTE: these routines are allowed to look at the current screen
45  * and the current possition assuming that it is correct.  If this is not
46  * true, then the update will be WRONG!  This is (should be) a valid
47  * assumption...
48  */
49
50 #define TC_BUFSIZE 2048
51
52 #define GoodStr(a) (tstr[a].str != NULL && tstr[a].str[0] != '\0')
53 #define Str(a) tstr[a].str
54 #define Val(a) tval[a].val
55
56 static struct {
57     const char   *b_name;
58     speed_t b_rate;
59 }       baud_rate[] = {
60
61 #ifdef B0
62     { "0", B0 },
63 #endif
64 #ifdef B50
65     { "50", B50 },
66 #endif
67 #ifdef B75
68     { "75", B75 },
69 #endif
70 #ifdef B110
71     { "110", B110 },
72 #endif
73 #ifdef B134
74     { "134", B134 },
75 #endif
76 #ifdef B150
77     { "150", B150 },
78 #endif
79 #ifdef B200
80     { "200", B200 },
81 #endif
82 #ifdef B300
83     { "300", B300 },
84 #endif
85 #ifdef B600
86     { "600", B600 },
87 #endif
88 #ifdef B900
89     { "900", B900 },
90 #endif
91 #ifdef B1200
92     { "1200", B1200 },
93 #endif
94 #ifdef B1800
95     { "1800", B1800 },
96 #endif
97 #ifdef B2400
98     { "2400", B2400 },
99 #endif
100 #ifdef B3600
101     { "3600", B3600 },
102 #endif
103 #ifdef B4800
104     { "4800", B4800 },
105 #endif
106 #ifdef B7200
107     { "7200", B7200 },
108 #endif
109 #ifdef B9600
110     { "9600", B9600 },
111 #endif
112 #ifdef EXTA
113     { "19200", EXTA },
114 #endif
115 #ifdef B19200
116     { "19200", B19200 },
117 #endif
118 #ifdef EXTB
119     { "38400", EXTB },
120 #endif
121 #ifdef B38400
122     { "38400", B38400 },
123 #endif
124     { NULL, 0 }
125 };
126
127 #define T_al    0
128 #define T_bl    1
129 #define T_cd    2
130 #define T_ce    3
131 #define T_ch    4
132 #define T_cl    5
133 #define T_dc    6
134 #define T_dl    7
135 #define T_dm    8
136 #define T_ed    9
137 #define T_ei    10
138 #define T_fs    11
139 #define T_ho    12
140 #define T_ic    13
141 #define T_im    14 
142 #define T_ip    15
143 #define T_kd    16
144 #define T_kl    17
145 #define T_kr    18
146 #define T_ku    19
147 #define T_md    20
148 #define T_me    21
149 #define T_nd    22
150 #define T_se    23
151 #define T_so    24
152 #define T_ts    25
153 #define T_up    26
154 #define T_us    27
155 #define T_ue    28
156 #define T_vb    29
157 #define T_DC    30
158 #define T_DO    31
159 #define T_IC    32
160 #define T_LE    33
161 #define T_RI    34
162 #define T_UP    35
163 #define T_kh    36
164 #define T_at7   37
165 #define T_str   38
166 static struct termcapstr {
167     const char   *name;
168     const char   *long_name;
169     char   *str;
170 } tstr[T_str + 1];
171
172
173 #define T_am    0
174 #define T_pt    1
175 #define T_li    2
176 #define T_co    3
177 #define T_km    4
178 #define T_xn    5
179 #define T_val   6
180 static struct termcapval {
181     const char   *name;
182     const char   *long_name;
183     int     val;
184 } tval[T_val + 1];
185
186 void
187 terminit()
188 {
189 #ifdef NLS_CATALOGS
190     int i;
191
192     for (i = 0; i < T_str + 1; i++)
193         xfree((ptr_t) tstr[i].long_name);
194
195     for (i = 0; i < T_val + 1; i++)
196         xfree((ptr_t) tval[i].long_name);
197 #endif
198
199     tstr[T_al].name = "al";
200     tstr[T_al].long_name = CSAVS(4, 1, "add new blank line");
201
202     tstr[T_bl].name = "bl";
203     tstr[T_bl].long_name = CSAVS(4, 2, "audible bell");
204
205     tstr[T_cd].name = "cd";
206     tstr[T_cd].long_name = CSAVS(4, 3, "clear to bottom");
207
208     tstr[T_ce].name = "ce";
209     tstr[T_ce].long_name = CSAVS(4, 4, "clear to end of line");
210
211     tstr[T_ch].name = "ch";
212     tstr[T_ch].long_name = CSAVS(4, 5, "cursor to horiz pos");
213
214     tstr[T_cl].name = "cl";
215     tstr[T_cl].long_name = CSAVS(4, 6, "clear screen");
216
217     tstr[T_dc].name = "dc";
218     tstr[T_dc].long_name = CSAVS(4, 7, "delete a character");
219
220     tstr[T_dl].name = "dl";
221     tstr[T_dl].long_name = CSAVS(4, 8, "delete a line");
222
223     tstr[T_dm].name = "dm";
224     tstr[T_dm].long_name = CSAVS(4, 9, "start delete mode");
225
226     tstr[T_ed].name = "ed";
227     tstr[T_ed].long_name = CSAVS(4, 10, "end delete mode");
228
229     tstr[T_ei].name = "ei";
230     tstr[T_ei].long_name = CSAVS(4, 11, "end insert mode");
231
232     tstr[T_fs].name = "fs";
233     tstr[T_fs].long_name = CSAVS(4, 12, "cursor from status line");
234
235     tstr[T_ho].name = "ho";
236     tstr[T_ho].long_name = CSAVS(4, 13, "home cursor");
237
238     tstr[T_ic].name = "ic";
239     tstr[T_ic].long_name = CSAVS(4, 14, "insert character");
240
241     tstr[T_im].name = "im";
242     tstr[T_im].long_name = CSAVS(4, 15, "start insert mode");
243
244     tstr[T_ip].name = "ip";
245     tstr[T_ip].long_name = CSAVS(4, 16, "insert padding");
246
247     tstr[T_kd].name = "kd";
248     tstr[T_kd].long_name = CSAVS(4, 17, "sends cursor down");
249
250     tstr[T_kl].name = "kl";
251     tstr[T_kl].long_name = CSAVS(4, 18, "sends cursor left");
252
253     tstr[T_kr].name = "kr";
254     tstr[T_kr].long_name = CSAVS(4, 19, "sends cursor right");
255
256     tstr[T_ku].name = "ku";
257     tstr[T_ku].long_name = CSAVS(4, 20, "sends cursor up");
258
259     tstr[T_md].name = "md";
260     tstr[T_md].long_name = CSAVS(4, 21, "begin bold");
261
262     tstr[T_me].name = "me";
263     tstr[T_me].long_name = CSAVS(4, 22, "end attributes");
264
265     tstr[T_nd].name = "nd";
266     tstr[T_nd].long_name = CSAVS(4, 23, "non destructive space");
267
268     tstr[T_se].name = "se";
269     tstr[T_se].long_name = CSAVS(4, 24, "end standout");
270
271     tstr[T_so].name = "so";
272     tstr[T_so].long_name = CSAVS(4, 25, "begin standout");
273
274     tstr[T_ts].name = "ts";
275     tstr[T_ts].long_name = CSAVS(4, 26, "cursor to status line");
276
277     tstr[T_up].name = "up";
278     tstr[T_up].long_name = CSAVS(4, 27, "cursor up one");
279
280     tstr[T_us].name = "us";
281     tstr[T_us].long_name = CSAVS(4, 28, "begin underline");
282
283     tstr[T_ue].name = "ue";
284     tstr[T_ue].long_name = CSAVS(4, 29, "end underline");
285
286     tstr[T_vb].name = "vb";
287     tstr[T_vb].long_name = CSAVS(4, 30, "visible bell");
288
289     tstr[T_DC].name = "DC";
290     tstr[T_DC].long_name = CSAVS(4, 31, "delete multiple chars");
291
292     tstr[T_DO].name = "DO";
293     tstr[T_DO].long_name = CSAVS(4, 32, "cursor down multiple");
294
295     tstr[T_IC].name = "IC";
296     tstr[T_IC].long_name = CSAVS(4, 33, "insert multiple chars");
297
298     tstr[T_LE].name = "LE";
299     tstr[T_LE].long_name = CSAVS(4, 34, "cursor left multiple");
300
301     tstr[T_RI].name = "RI";
302     tstr[T_RI].long_name = CSAVS(4, 35, "cursor right multiple");
303
304     tstr[T_UP].name = "UP";
305     tstr[T_UP].long_name = CSAVS(4, 36, "cursor up multiple");
306
307     tstr[T_kh].name = "kh";
308     tstr[T_kh].long_name = CSAVS(4, 37, "send cursor home");
309
310     tstr[T_at7].name = "@7";
311     tstr[T_at7].long_name = CSAVS(4, 38, "send cursor end");
312
313     tstr[T_str].name = NULL;
314     tstr[T_str].long_name = NULL;
315
316
317     tval[T_am].name = "am";
318     tval[T_am].long_name = CSAVS(4, 37, "Has automatic margins");
319
320     tval[T_pt].name = "pt";
321     tval[T_pt].long_name = CSAVS(4, 38, "Can use physical tabs");
322
323     tval[T_li].name = "li";
324     tval[T_li].long_name = CSAVS(4, 39, "Number of lines");
325
326     tval[T_co].name = "co";
327     tval[T_co].long_name = CSAVS(4, 40, "Number of columns");
328
329     tval[T_km].name = "km";
330     tval[T_km].long_name = CSAVS(4, 41, "Has meta key");
331
332     tval[T_xn].name = "xn";
333     tval[T_xn].long_name = CSAVS(4, 42, "Newline ignored at right margin");
334
335     tval[T_val].name = NULL;
336     tval[T_val].long_name = NULL;
337 }
338
339 /*
340  * A very useful table from justin@crim.ca (Justin Bur) :-)
341  * (Modified by per@erix.ericsson.se (Per Hedeland)
342  *  - first (and second:-) case fixed)
343  *
344  * Description     Termcap variables       tcsh behavior
345  *                 am      xn              UseRightmost    SendCRLF
346  * --------------  ------- -------         ------------    ------------
347  * Automargins     yes     no              yes             no
348  * Magic Margins   yes     yes             yes             no
349  * No Wrap         no      --              yes             yes
350  */
351
352 static int me_all = 0;          /* does two or more of the attributes use me */
353
354 static  void    ReBufferDisplay __P((void));
355 static  void    TCalloc         __P((struct termcapstr *, char *)); 
356
357
358 static void
359 TCalloc(t, cap)
360     struct termcapstr *t;
361     char   *cap;
362 {
363     static char termcap_alloc[TC_BUFSIZE];
364     char    termbuf[TC_BUFSIZE];
365     struct termcapstr *ts;
366     static int tloc = 0;
367     int     tlen, clen;
368
369     if (cap == NULL || *cap == '\0') {
370         t->str = NULL;
371         return;
372     }
373     else
374         clen = strlen(cap);
375
376     if (t->str == NULL)
377         tlen = 0;
378     else
379         tlen = strlen(t->str);
380
381     /*
382      * New string is shorter; no need to allocate space
383      */
384     if (clen <= tlen) {
385         (void) strcpy(t->str, cap);
386         return;
387     }
388
389     /*
390      * New string is longer; see if we have enough space to append
391      */
392     if (tloc + 3 < TC_BUFSIZE) {
393         (void) strcpy(t->str = &termcap_alloc[tloc], cap);
394         tloc += clen + 1;       /* one for \0 */
395         return;
396     }
397
398     /*
399      * Compact our buffer; no need to check compaction, cause we know it
400      * fits...
401      */
402     tlen = 0;
403     for (ts = tstr; ts->name != NULL; ts++)
404         if (t != ts && ts->str != NULL && ts->str[0] != '\0') {
405             char   *ptr;
406
407             for (ptr = ts->str; *ptr != '\0'; termbuf[tlen++] = *ptr++)
408                 continue;
409             termbuf[tlen++] = '\0';
410         }
411     (void) memmove((ptr_t) termcap_alloc, (ptr_t) termbuf, (size_t) TC_BUFSIZE);
412     tloc = tlen;
413     if (tloc + 3 >= TC_BUFSIZE) {
414         stderror(ERR_NAME | ERR_TCNOSTR);
415         return;
416     }
417     (void) strcpy(t->str = &termcap_alloc[tloc], cap);
418     tloc += clen + 1;           /* one for \0 */
419     return;
420 }
421
422
423 /*ARGSUSED*/
424 void
425 TellTC()
426 {
427     struct termcapstr *t;
428     char *s;
429
430     xprintf(CGETS(7, 1, "\n\tTcsh thinks your terminal has the\n"));
431     xprintf(CGETS(7, 2, "\tfollowing characteristics:\n\n"));
432     xprintf(CGETS(7, 3, "\tIt has %d columns and %d lines\n"),
433             Val(T_co), Val(T_li));
434     s = strsave(T_HasMeta ? CGETS(7, 5, "a") : CGETS(7, 6, "no"));
435     xprintf(CGETS(7, 4, "\tIt has %s meta key\n"), s);
436     xfree(s);
437     s = strsave(T_Tabs ? "" : CGETS(7, 8, " not"));
438     xprintf(CGETS(7, 7, "\tIt can%s use tabs\n"), s);
439     xfree(s);
440     s = strsave((T_Margin&MARGIN_AUTO) ?
441                 CGETS(7, 10, "has") : CGETS(7, 11, "does not have"));
442     xprintf(CGETS(7, 9, "\tIt %s automatic margins\n"), s);
443     xfree(s);
444     if (T_Margin & MARGIN_AUTO) {
445         s = strsave((T_Margin & MARGIN_MAGIC) ?
446                         CGETS(7, 10, "has") : CGETS(7, 11, "does not have"));
447         xprintf(CGETS(7, 12, "\tIt %s magic margins\n"), s);
448         xfree(s);
449     }
450     for (t = tstr; t->name != NULL; t++) {
451         s = strsave(t->str && *t->str ? t->str : CGETS(7, 13, "(empty)"));
452         xprintf("\t%36s (%s) == %s\n", t->long_name, t->name, s);
453         xfree(s);
454     }
455     xputchar('\n');
456 }
457
458
459 static void
460 ReBufferDisplay()
461 {
462     int i;
463     Char **b;
464     Char **bufp;
465
466     b = Display;
467     Display = NULL;
468     if (b != NULL) {
469         for (bufp = b; *bufp != NULL; bufp++)
470             xfree((ptr_t) * bufp);
471         xfree((ptr_t) b);
472     }
473     b = Vdisplay;
474     Vdisplay = NULL;
475     if (b != NULL) {
476         for (bufp = b; *bufp != NULL; bufp++)
477             xfree((ptr_t) * bufp);
478         xfree((ptr_t) b);
479     }
480     TermH = Val(T_co);
481     TermV = (INBUFSIZE * 4) / TermH + 1;
482     b = (Char **) xmalloc((size_t) (sizeof(*b) * (TermV + 1)));
483     for (i = 0; i < TermV; i++)
484         b[i] = (Char *) xmalloc((size_t) (sizeof(*b[i]) * (TermH + 1)));
485     b[TermV] = NULL;
486     Display = b;
487     b = (Char **) xmalloc((size_t) (sizeof(*b) * (TermV + 1)));
488     for (i = 0; i < TermV; i++)
489         b[i] = (Char *) xmalloc((size_t) (sizeof(*b[i]) * (TermH + 1)));
490     b[TermV] = NULL;
491     Vdisplay = b;
492 }
493
494 void
495 SetTC(what, how)
496     char   *what, *how;
497 {
498     struct termcapstr *ts;
499     struct termcapval *tv;
500
501     /*
502      * Do the strings first
503      */
504     setname("settc");
505     for (ts = tstr; ts->name != NULL; ts++)
506         if (strcmp(ts->name, what) == 0)
507             break;
508     if (ts->name != NULL) {
509         TCalloc(ts, how);
510         /*
511          * Reset variables
512          */
513         if (GoodStr(T_me) && GoodStr(T_ue))
514             me_all = (strcmp(Str(T_me), Str(T_ue)) == 0);
515         else
516             me_all = 0;
517         if (GoodStr(T_me) && GoodStr(T_se))
518             me_all |= (strcmp(Str(T_me), Str(T_se)) == 0);
519
520         T_CanCEOL = GoodStr(T_ce);
521         T_CanDel = GoodStr(T_dc) || GoodStr(T_DC);
522         T_CanIns = GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC);
523         T_CanUP = GoodStr(T_up) || GoodStr(T_UP);
524         return;
525     }
526
527     /*
528      * Do the numeric ones second
529      */
530     for (tv = tval; tv->name != NULL; tv++)
531         if (strcmp(tv->name, what) == 0)
532             break;
533
534     if (tv->name != NULL) {
535         if (tv == &tval[T_pt] || tv == &tval[T_km] || 
536             tv == &tval[T_am] || tv == &tval[T_xn]) {
537             if (strcmp(how, "yes") == 0)
538                 tv->val = 1;
539             else if (strcmp(how, "no") == 0)
540                 tv->val = 0;
541             else {
542                 stderror(ERR_SETTCUS, tv->name);
543                 return;
544             }
545             T_Tabs = (Char) Val(T_pt);
546             T_HasMeta = (Char) Val(T_km);
547             T_Margin = (Char) Val(T_am) ? MARGIN_AUTO : 0;
548             T_Margin |= (Char) Val(T_xn) ? MARGIN_MAGIC : 0;
549             if (tv == &tval[T_am] || tv == &tval[T_xn]) 
550                 ChangeSize(Val(T_li), Val(T_co));
551             return;
552         }
553         else {
554             tv->val = atoi(how);
555             T_Cols = (Char) Val(T_co);
556             T_Lines = (Char) Val(T_li);
557             if (tv == &tval[T_co] || tv == &tval[T_li])
558                 ChangeSize(Val(T_li), Val(T_co));
559             return;
560         }
561     }
562     stderror(ERR_NAME | ERR_TCCAP, what);
563     return;
564 }
565
566
567 /*
568  * Print the termcap string out with variable substitution
569  */
570 void
571 EchoTC(v)
572     Char  **v;
573 {
574     char   *cap, *scap, cv[BUFSIZE];
575     int     arg_need, arg_cols, arg_rows;
576     int     verbose = 0, silent = 0;
577     char   *area;
578     static const char *fmts = "%s\n", *fmtd = "%d\n";
579     struct termcapstr *t;
580     char    buf[TC_BUFSIZE];
581
582     area = buf;
583
584     setname("echotc");
585
586     tglob(v);
587     if (gflag) {
588         v = globall(v);
589         if (v == 0)
590             stderror(ERR_NAME | ERR_NOMATCH);
591     }
592     else
593         v = gargv = saveblk(v);
594     trim(v);
595
596     if (!*v || *v[0] == '\0')
597         return;
598     if (v[0][0] == '-') {
599         switch (v[0][1]) {
600         case 'v':
601             verbose = 1;
602             break;
603         case 's':
604             silent = 1;
605             break;
606         default:
607             stderror(ERR_NAME | ERR_TCUSAGE);
608             break;
609         }
610         v++;
611     }
612     if (!*v || *v[0] == '\0')
613         return;
614     (void) strcpy(cv, short2str(*v));
615     if (strcmp(cv, "tabs") == 0) {
616         xprintf(fmts, T_Tabs ? CGETS(7, 14, "yes") :
617                 CGETS(7, 15, "no"));
618         flush();
619         return;
620     }
621     else if (strcmp(cv, "meta") == 0) {
622         xprintf(fmts, Val(T_km) ? CGETS(7, 14, "yes") :
623                 CGETS(7, 15, "no"));
624         flush();
625         return;
626     }
627     else if (strcmp(cv, "xn") == 0) {
628         xprintf(fmts, T_Margin & MARGIN_MAGIC ? CGETS(7, 14, "yes") :
629                 CGETS(7, 15,  "no"));
630         flush();
631         return;
632     }
633     else if (strcmp(cv, "am") == 0) {
634         xprintf(fmts, T_Margin & MARGIN_AUTO ? CGETS(7, 14, "yes") :
635                 CGETS(7, 15, "no"));
636         flush();
637         return;
638     }
639     else if (strcmp(cv, "baud") == 0) {
640         int     i;
641
642         for (i = 0; baud_rate[i].b_name != NULL; i++)
643             if (T_Speed == baud_rate[i].b_rate) {
644                 xprintf(fmts, baud_rate[i].b_name);
645                 flush();
646                 return;
647             }
648         xprintf(fmtd, 0);
649         flush();
650         return;
651     }
652     else if (strcmp(cv, "rows") == 0 || strcmp(cv, "lines") == 0) {
653         xprintf(fmtd, Val(T_li));
654         flush();
655         return;
656     }
657     else if (strcmp(cv, "cols") == 0) {
658         xprintf(fmtd, Val(T_co));
659         flush();
660         return;
661     }
662
663     /* 
664      * Try to use our local definition first
665      */
666     scap = NULL;
667     for (t = tstr; t->name != NULL; t++)
668         if (strcmp(t->name, cv) == 0) {
669             scap = t->str;
670             break;
671         }
672     if (t->name == NULL)
673         scap = tgetstr(cv, &area);
674     if (!scap || scap[0] == '\0') {
675         if (tgetflag(cv)) {
676             xprintf(CGETS(7, 14, "yes\n"));
677             return;
678         }
679         if (silent)
680             return;
681         else
682             stderror(ERR_NAME | ERR_TCCAP, cv);
683     }
684
685     /*
686      * Count home many values we need for this capability.
687      */
688     for (cap = scap, arg_need = 0; *cap; cap++)
689         if (*cap == '%')
690             switch (*++cap) {
691             case 'd':
692             case '2':
693             case '3':
694             case '.':
695             case '+':
696                 arg_need++;
697                 break;
698             case '%':
699             case '>':
700             case 'i':
701             case 'r':
702             case 'n':
703             case 'B':
704             case 'D':
705                 break;
706             default:
707                 /*
708                  * hpux has lot's of them...
709                  */
710                 if (verbose)
711                     stderror(ERR_NAME | ERR_TCPARM, *cap);
712                 /* This is bad, but I won't complain */
713                 break;
714             }
715
716     switch (arg_need) {
717     case 0:
718         v++;
719         if (*v && *v[0]) {
720             if (silent)
721                 return;
722             else
723                 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
724         }
725         (void) tputs(scap, 1, PUTRAW);
726         break;
727     case 1:
728         v++;
729         if (!*v || *v[0] == '\0')
730             stderror(ERR_NAME | ERR_TCNARGS, cv, 1);
731         arg_cols = 0;
732         arg_rows = atoi(short2str(*v));
733         v++;
734         if (*v && *v[0]) {
735             if (silent)
736                 return;
737             else
738                 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
739         }
740         (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, PUTRAW);
741         break;
742     default:
743         /* This is wrong, but I will ignore it... */
744         if (verbose)
745             stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
746         /*FALLTHROUGH*/
747     case 2:
748         v++;
749         if (!*v || *v[0] == '\0') {
750             if (silent)
751                 return;
752             else
753                 stderror(ERR_NAME | ERR_TCNARGS, cv, 2);
754         }
755         arg_cols = atoi(short2str(*v));
756         v++;
757         if (!*v || *v[0] == '\0') {
758             if (silent)
759                 return;
760             else
761                 stderror(ERR_NAME | ERR_TCNARGS, cv, 2);
762         }
763         arg_rows = atoi(short2str(*v));
764         v++;
765         if (*v && *v[0]) {
766             if (silent)
767                 return;
768             else
769                 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
770         }
771         (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows, PUTRAW);
772         break;
773     }
774     flush();
775     if (gargv) {
776         blkfree(gargv);
777         gargv = 0;
778     }
779 }
780
781 int    GotTermCaps = 0;
782
783 static struct {
784     Char   *name;
785     int     key;
786     XmapVal fun;
787     int     type;
788 } arrow[] = {
789 #define A_K_DN  0
790     { STRdown,  T_kd, { 0 }, 0 },
791 #define A_K_UP  1
792     { STRup,    T_ku, { 0 }, 0 },
793 #define A_K_LT  2
794     { STRleft,  T_kl, { 0 }, 0 },
795 #define A_K_RT  3
796     { STRright, T_kr, { 0 }, 0 },
797 #define A_K_HO  4
798     { STRhome,  T_kh, { 0 }, 0 },
799 #define A_K_EN  5
800     { STRend,   T_at7, { 0 }, 0}
801 };
802 #define A_K_NKEYS 6
803
804 void
805 ResetArrowKeys()
806 {
807     arrow[A_K_DN].fun.cmd = F_DOWN_HIST;
808     arrow[A_K_DN].type    = XK_CMD;
809
810     arrow[A_K_UP].fun.cmd = F_UP_HIST;
811     arrow[A_K_UP].type    = XK_CMD;
812
813     arrow[A_K_LT].fun.cmd = F_CHARBACK;
814     arrow[A_K_LT].type    = XK_CMD;
815
816     arrow[A_K_RT].fun.cmd = F_CHARFWD;
817     arrow[A_K_RT].type    = XK_CMD;
818
819     arrow[A_K_HO].fun.cmd = F_TOBEG;
820     arrow[A_K_HO].type    = XK_CMD;
821
822     arrow[A_K_EN].fun.cmd = F_TOEND;
823     arrow[A_K_EN].type    = XK_CMD;
824 }
825
826 void
827 DefaultArrowKeys() 
828 {
829     static Char strA[] = {033, '[', 'A', '\0'};
830     static Char strB[] = {033, '[', 'B', '\0'};
831     static Char strC[] = {033, '[', 'C', '\0'};
832     static Char strD[] = {033, '[', 'D', '\0'};
833     static Char strH[] = {033, '[', 'H', '\0'};
834     static Char strF[] = {033, '[', 'F', '\0'};
835     static Char stOA[] = {033, 'O', 'A', '\0'};
836     static Char stOB[] = {033, 'O', 'B', '\0'};
837     static Char stOC[] = {033, 'O', 'C', '\0'};
838     static Char stOD[] = {033, 'O', 'D', '\0'};
839     static Char stOH[] = {033, 'O', 'H', '\0'};
840     static Char stOF[] = {033, 'O', 'F', '\0'};
841
842     CStr cs;
843 #ifndef IS_ASCII
844     if (strA[0] == 033)
845     {
846         strA[0] = CTL_ESC('\033');
847         strB[0] = CTL_ESC('\033');
848         strC[0] = CTL_ESC('\033');
849         strD[0] = CTL_ESC('\033');
850         strH[0] = CTL_ESC('\033');
851         strF[0] = CTL_ESC('\033');
852         stOA[0] = CTL_ESC('\033');
853         stOB[0] = CTL_ESC('\033');
854         stOC[0] = CTL_ESC('\033');
855         stOD[0] = CTL_ESC('\033');
856         stOH[0] = CTL_ESC('\033');
857         stOF[0] = CTL_ESC('\033');
858     }
859 #endif
860
861     cs.len = 3;
862
863     cs.buf = strA; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
864     cs.buf = strB; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
865     cs.buf = strC; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
866     cs.buf = strD; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
867     cs.buf = strH; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
868     cs.buf = strF; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
869     cs.buf = stOA; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
870     cs.buf = stOB; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
871     cs.buf = stOC; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
872     cs.buf = stOD; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
873     cs.buf = stOH; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
874     cs.buf = stOF; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
875
876     if (VImode) {
877         cs.len = 2;
878         cs.buf = &strA[1]; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
879         cs.buf = &strB[1]; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
880         cs.buf = &strC[1]; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
881         cs.buf = &strD[1]; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
882         cs.buf = &strH[1]; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
883         cs.buf = &strF[1]; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
884         cs.buf = &stOA[1]; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
885         cs.buf = &stOB[1]; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
886         cs.buf = &stOC[1]; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
887         cs.buf = &stOD[1]; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
888         cs.buf = &stOH[1]; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
889         cs.buf = &stOF[1]; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
890     }
891 }
892
893
894 int
895 SetArrowKeys(name, fun, type)
896     CStr *name;
897     XmapVal *fun;
898     int type;
899 {
900     int i;
901     for (i = 0; i < A_K_NKEYS; i++)
902         if (Strcmp(name->buf, arrow[i].name) == 0) {
903             arrow[i].fun  = *fun;
904             arrow[i].type = type;
905             return 0;
906         }
907     return -1;
908 }
909
910 int
911 IsArrowKey(name)
912     Char *name;
913 {
914     int i;
915     for (i = 0; i < A_K_NKEYS; i++)
916         if (Strcmp(name, arrow[i].name) == 0)
917             return 1;
918     return 0;
919 }
920
921 int
922 ClearArrowKeys(name)
923     CStr *name;
924 {
925     int i;
926     for (i = 0; i < A_K_NKEYS; i++)
927         if (Strcmp(name->buf, arrow[i].name) == 0) {
928             arrow[i].type = XK_NOD;
929             return 0;
930         }
931     return -1;
932 }
933
934 void
935 PrintArrowKeys(name)
936     CStr *name;
937 {
938     int i;
939
940     for (i = 0; i < A_K_NKEYS; i++)
941         if (name->len == 0 || Strcmp(name->buf, arrow[i].name) == 0)
942             if (arrow[i].type != XK_NOD) {
943                 CStr cs;
944                 cs.buf = arrow[i].name;
945                 cs.len = Strlen(cs.buf);
946                 (void) printOne(&cs, &arrow[i].fun, arrow[i].type);
947             }
948 }
949
950
951 void
952 BindArrowKeys()
953 {
954     KEYCMD *map, *dmap;
955     int     i, j;
956     char   *p;
957     CStr    cs;
958
959     if (!GotTermCaps)
960         return;
961     map = VImode ? CcAltMap : CcKeyMap;
962     dmap = VImode ? CcViCmdMap : CcEmacsMap;
963
964     DefaultArrowKeys();
965
966     for (i = 0; i < A_K_NKEYS; i++) {
967         p = tstr[arrow[i].key].str;
968         if (p && *p) {
969             j = (unsigned char) *p;
970             cs.buf = str2short(p);
971             cs.len = Strlen(cs.buf);
972             /*
973              * Assign the arrow keys only if:
974              *
975              * 1. They are multi-character arrow keys and the user 
976              *    has not re-assigned the leading character, or 
977              *    has re-assigned the leading character to be F_XKEY
978              * 2. They are single arrow keys pointing to an unassigned key.
979              */
980             if (arrow[i].type == XK_NOD) {
981                 ClearXkey(map, &cs);
982             }
983             else {
984                 if (p[1] && (dmap[j] == map[j] || map[j] == F_XKEY)) {
985                     AddXkey(&cs, &arrow[i].fun, arrow[i].type);
986                     map[j] = F_XKEY;
987                 }
988                 else if (map[j] == F_UNASSIGNED) {
989                     ClearXkey(map, &cs);
990                     if (arrow[i].type == XK_CMD)
991                         map[j] = arrow[i].fun.cmd;
992                     else
993                         AddXkey(&cs, &arrow[i].fun, arrow[i].type);
994                 }
995             }
996         }
997     }
998 }
999
1000 static Char cur_atr = 0;        /* current attributes */
1001
1002 void
1003 SetAttributes(atr)
1004     Char     atr;
1005 {
1006     atr &= ATTRIBUTES;
1007     if (atr != cur_atr) {
1008         if (me_all && GoodStr(T_me)) {
1009             if (((cur_atr & BOLD) && !(atr & BOLD)) ||
1010                 ((cur_atr & UNDER) && !(atr & UNDER)) ||
1011                 ((cur_atr & STANDOUT) && !(atr & STANDOUT))) {
1012                 (void) tputs(Str(T_me), 1, PUTPURE);
1013                 cur_atr = 0;
1014             }
1015         }
1016         if ((atr & BOLD) != (cur_atr & BOLD)) {
1017             if (atr & BOLD) {
1018                 if (GoodStr(T_md) && GoodStr(T_me)) {
1019                     (void) tputs(Str(T_md), 1, PUTPURE);
1020                     cur_atr |= BOLD;
1021                 }
1022             }
1023             else {
1024                 if (GoodStr(T_md) && GoodStr(T_me)) {
1025                     (void) tputs(Str(T_me), 1, PUTPURE);
1026                     if ((cur_atr & STANDOUT) && GoodStr(T_se)) {
1027                         (void) tputs(Str(T_se), 1, PUTPURE);
1028                         cur_atr &= ~STANDOUT;
1029                     }
1030                     if ((cur_atr & UNDER) && GoodStr(T_ue)) {
1031                         (void) tputs(Str(T_ue), 1, PUTPURE);
1032                         cur_atr &= ~UNDER;
1033                     }
1034                     cur_atr &= ~BOLD;
1035                 }
1036             }
1037         }
1038         if ((atr & STANDOUT) != (cur_atr & STANDOUT)) {
1039             if (atr & STANDOUT) {
1040                 if (GoodStr(T_so) && GoodStr(T_se)) {
1041                     (void) tputs(Str(T_so), 1, PUTPURE);
1042                     cur_atr |= STANDOUT;
1043                 }
1044             }
1045             else {
1046                 if (GoodStr(T_se)) {
1047                     (void) tputs(Str(T_se), 1, PUTPURE);
1048                     cur_atr &= ~STANDOUT;
1049                 }
1050             }
1051         }
1052         if ((atr & UNDER) != (cur_atr & UNDER)) {
1053             if (atr & UNDER) {
1054                 if (GoodStr(T_us) && GoodStr(T_ue)) {
1055                     (void) tputs(Str(T_us), 1, PUTPURE);
1056                     cur_atr |= UNDER;
1057                 }
1058             }
1059             else {
1060                 if (GoodStr(T_ue)) {
1061                     (void) tputs(Str(T_ue), 1, PUTPURE);
1062                     cur_atr &= ~UNDER;
1063                 }
1064             }
1065         }
1066     }
1067 }
1068
1069 /* PWP 6-27-88 -- if the tty driver thinks that we can tab, we ask termcap */
1070 int
1071 CanWeTab()
1072 {
1073     return (Val(T_pt));
1074 }
1075
1076 void
1077 MoveToLine(where)               /* move to line <where> (first line == 0) */
1078     int     where;              /* as efficiently as possible; */
1079 {
1080     int     del;
1081
1082     if (where == CursorV)
1083         return;
1084
1085     if (where > TermV) {
1086 #ifdef DEBUG_SCREEN
1087         xprintf("MoveToLine: where is ridiculous: %d\r\n", where);
1088         flush();
1089 #endif /* DEBUG_SCREEN */
1090         return;
1091     }
1092
1093     del = where - CursorV;
1094
1095     if (del > 0) {
1096         while (del > 0) {
1097             if ((T_Margin & MARGIN_AUTO) && Display[CursorV][0] != '\0') {
1098                 size_t h;
1099
1100                 for (h = TermH - 1; h > 0 && Display[CursorV][h] == CHAR_DBWIDTH;
1101                      h--)
1102                     ;
1103                 /* move without newline */
1104                 MoveToChar(h);
1105                 so_write(&Display[CursorV][CursorH], TermH - CursorH); /* updates CursorH/V*/
1106                 del--;
1107             }
1108             else {
1109                 if ((del > 1) && GoodStr(T_DO)) {
1110                     (void) tputs(tgoto(Str(T_DO), del, del), del, PUTPURE);
1111                     del = 0;
1112                 }
1113                 else {
1114                     for ( ; del > 0; del--) 
1115                         (void) putraw('\n');
1116                     CursorH = 0;        /* because the \n will become \r\n */
1117                 }
1118             }
1119         }
1120     }
1121     else {                      /* del < 0 */
1122         if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
1123             (void) tputs(tgoto(Str(T_UP), -del, -del), -del, PUTPURE);
1124         else {
1125             int i;
1126             if (GoodStr(T_up))
1127                 for (i = 0; i < -del; i++)
1128                     (void) tputs(Str(T_up), 1, PUTPURE);
1129         }
1130     }
1131     CursorV = where;            /* now where is here */
1132 }
1133
1134 void
1135 MoveToChar(where)               /* move to character position (where) */
1136     int     where;
1137 {                               /* as efficiently as possible */
1138     int     del;
1139
1140 mc_again:
1141     if (where == CursorH)
1142         return;
1143
1144     if (where >= TermH) {
1145 #ifdef DEBUG_SCREEN
1146         xprintf("MoveToChar: where is riduculous: %d\r\n", where);
1147         flush();
1148 #endif /* DEBUG_SCREEN */
1149         return;
1150     }
1151
1152     if (!where) {               /* if where is first column */
1153         (void) putraw('\r');    /* do a CR */
1154         CursorH = 0;
1155         return;
1156     }
1157
1158     del = where - CursorH;
1159
1160     if ((del < -4 || del > 4) && GoodStr(T_ch))
1161         /* go there directly */
1162         (void) tputs(tgoto(Str(T_ch), where, where), where, PUTPURE);
1163     else {
1164         int i;
1165         if (del > 0) {          /* moving forward */
1166             if ((del > 4) && GoodStr(T_RI))
1167                 (void) tputs(tgoto(Str(T_RI), del, del), del, PUTPURE);
1168             else {
1169                 /* if I can do tabs, use them */
1170                 if (T_Tabs) {
1171                     if ((CursorH & 0370) != (where & ~0x7)
1172                         && Display[CursorV][where & ~0x7] != CHAR_DBWIDTH) {
1173                         /* if not within tab stop */
1174                         for (i = (CursorH & 0370); i < (where & ~0x7); i += 8)
1175                             (void) putraw('\t');        /* then tab over */
1176                         CursorH = where & ~0x7;
1177                         /* Note: considering that we often want to go to
1178                            TermH - 1 for the wrapping, it would be nice to
1179                            optimize this case by tabbing to the last column
1180                            - but this doesn't work for all terminals! */
1181                     }
1182                 }
1183                 /* it's usually cheaper to just write the chars, so we do. */
1184
1185                 /* NOTE THAT so_write() WILL CHANGE CursorH!!! */
1186                 so_write(&Display[CursorV][CursorH], where - CursorH);
1187
1188             }
1189         }
1190         else {                  /* del < 0 := moving backward */
1191             if ((-del > 4) && GoodStr(T_LE))
1192                 (void) tputs(tgoto(Str(T_LE), -del, -del), -del, PUTPURE);
1193             else {              /* can't go directly there */
1194                 /* if the "cost" is greater than the "cost" from col 0 */
1195                 if (T_Tabs ? (-del > ((where >> 3) + (where & 07)))
1196                     : (-del > where)) {
1197                     (void) putraw('\r');        /* do a CR */
1198                     CursorH = 0;
1199                     goto mc_again;      /* and try again */
1200                 }
1201                 for (i = 0; i < -del; i++)
1202                     (void) putraw('\b');
1203             }
1204         }
1205     }
1206     CursorH = where;            /* now where is here */
1207 }
1208
1209 void
1210 so_write(cp, n)
1211     Char *cp;
1212     int n;
1213 {
1214     if (n <= 0)
1215         return;                 /* catch bugs */
1216
1217     if (n > TermH) {
1218 #ifdef DEBUG_SCREEN
1219         xprintf("so_write: n is riduculous: %d\r\n", n);
1220         flush();
1221 #endif /* DEBUG_SCREEN */
1222         return;
1223     }
1224
1225     do {
1226         if (*cp != CHAR_DBWIDTH) {
1227             if (*cp & LITERAL) {
1228                 Char   *d;
1229 #ifdef DEBUG_LITERAL
1230                 xprintf("so: litnum %d\r\n", (int)(*cp & ~LITERAL));
1231 #endif /* DEBUG_LITERAL */
1232                 for (d = litptr + (*cp & ~LITERAL) * LIT_FACTOR; *d; d++)
1233                     (void) putwraw(*d);
1234             }
1235             else
1236                 (void) putwraw(*cp);
1237         }
1238         cp++;
1239         CursorH++;
1240     } while (--n);
1241
1242     if (CursorH >= TermH) { /* wrap? */
1243         if (T_Margin & MARGIN_AUTO) { /* yes */
1244             CursorH = 0;
1245             CursorV++;
1246             if (T_Margin & MARGIN_MAGIC) {
1247                 /* force the wrap to avoid the "magic" situation */
1248                 Char c;
1249                 if ((c = Display[CursorV][CursorH]) != '\0') {
1250                     so_write(&c, 1);
1251                     while(Display[CursorV][CursorH] == CHAR_DBWIDTH)
1252                         CursorH++;
1253                 }
1254                 else {
1255                     (void) putraw(' ');
1256                     CursorH = 1;
1257                 }
1258             }
1259         }
1260         else                    /* no wrap, but cursor stays on screen */
1261             CursorH = TermH - 1;
1262     }
1263 }
1264
1265
1266 void
1267 DeleteChars(num)                /* deletes <num> characters */
1268     int     num;
1269 {
1270     if (num <= 0)
1271         return;
1272
1273     if (!T_CanDel) {
1274 #ifdef DEBUG_EDIT
1275         xprintf(CGETS(7, 16, "ERROR: cannot delete\r\n"));
1276 #endif /* DEBUG_EDIT */
1277         flush();
1278         return;
1279     }
1280
1281     if (num > TermH) {
1282 #ifdef DEBUG_SCREEN
1283         xprintf(CGETS(7, 17, "DeleteChars: num is riduculous: %d\r\n"), num);
1284         flush();
1285 #endif /* DEBUG_SCREEN */
1286         return;
1287     }
1288
1289     if (GoodStr(T_DC))          /* if I have multiple delete */
1290         if ((num > 1) || !GoodStr(T_dc)) {      /* if dc would be more expen. */
1291             (void) tputs(tgoto(Str(T_DC), num, num), num, PUTPURE);
1292             return;
1293         }
1294
1295     if (GoodStr(T_dm))          /* if I have delete mode */
1296         (void) tputs(Str(T_dm), 1, PUTPURE);
1297
1298     if (GoodStr(T_dc))          /* else do one at a time */
1299         while (num--)
1300             (void) tputs(Str(T_dc), 1, PUTPURE);
1301
1302     if (GoodStr(T_ed))          /* if I have delete mode */
1303         (void) tputs(Str(T_ed), 1, PUTPURE);
1304 }
1305
1306 void
1307 Insert_write(cp, num)           /* Puts terminal in insert character mode, */
1308     Char *cp;
1309     int num;            /* or inserts num characters in the line */
1310 {
1311     if (num <= 0)
1312         return;
1313     if (!T_CanIns) {
1314 #ifdef DEBUG_EDIT
1315         xprintf(CGETS(7, 18, "ERROR: cannot insert\r\n"));
1316 #endif /* DEBUG_EDIT */
1317         flush();
1318         return;
1319     }
1320
1321     if (num > TermH) {
1322 #ifdef DEBUG_SCREEN
1323         xprintf(CGETS(7, 19, "StartInsert: num is riduculous: %d\r\n"), num);
1324         flush();
1325 #endif /* DEBUG_SCREEN */
1326         return;
1327     }
1328
1329     if (GoodStr(T_IC))          /* if I have multiple insert */
1330         if ((num > 1) || !GoodStr(T_ic)) {      /* if ic would be more expen. */
1331             (void) tputs(tgoto(Str(T_IC), num, num), num, PUTPURE);
1332             so_write(cp, num);  /* this updates CursorH/V */
1333             return;
1334         }
1335
1336     if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
1337         (void) tputs(Str(T_im), 1, PUTPURE);
1338
1339         so_write(cp, num);      /* this updates CursorH/V */
1340
1341         if (GoodStr(T_ip))      /* have to make num chars insert */
1342             (void) tputs(Str(T_ip), 1, PUTPURE);
1343
1344         (void) tputs(Str(T_ei), 1, PUTPURE);
1345         return;
1346     }
1347
1348     do {
1349         if (GoodStr(T_ic))      /* have to make num chars insert */
1350             (void) tputs(Str(T_ic), 1, PUTPURE);        /* insert a char */
1351
1352         so_write(cp++, 1);      /* this updates CursorH/V */
1353
1354         if (GoodStr(T_ip))      /* have to make num chars insert */
1355             (void) tputs(Str(T_ip), 1, PUTPURE);/* pad the inserted char */
1356
1357     } while (--num);
1358
1359 }
1360
1361 void
1362 ClearEOL(num)                   /* clear to end of line.  There are num */
1363     int     num;                /* characters to clear */
1364 {
1365     int i;
1366
1367     if (num <= 0)
1368         return;
1369
1370     if (T_CanCEOL && GoodStr(T_ce))
1371         (void) tputs(Str(T_ce), 1, PUTPURE);
1372     else {
1373         for (i = 0; i < num; i++)
1374             (void) putraw(' ');
1375         CursorH += num;         /* have written num spaces */
1376     }
1377 }
1378
1379 void
1380 ClearScreen()
1381 {                               /* clear the whole screen and home */
1382     if (GoodStr(T_cl))
1383         /* send the clear screen code */
1384         (void) tputs(Str(T_cl), Val(T_li), PUTPURE);
1385     else if (GoodStr(T_ho) && GoodStr(T_cd)) {
1386         (void) tputs(Str(T_ho), Val(T_li), PUTPURE);    /* home */
1387         /* clear to bottom of screen */
1388         (void) tputs(Str(T_cd), Val(T_li), PUTPURE);
1389     }
1390     else {
1391         (void) putraw('\r');
1392         (void) putraw('\n');
1393     }
1394 }
1395
1396 void
1397 SoundBeep()
1398 {                               /* produce a sound */
1399     beep_cmd ();
1400     if (adrof(STRnobeep))
1401         return;
1402
1403     if (GoodStr(T_vb) && adrof(STRvisiblebell))
1404         (void) tputs(Str(T_vb), 1, PUTPURE);    /* visible bell */
1405     else if (GoodStr(T_bl))
1406         /* what termcap says we should use */
1407         (void) tputs(Str(T_bl), 1, PUTPURE);
1408     else
1409         (void) putraw(CTL_ESC('\007')); /* an ASCII bell; ^G */
1410 }
1411
1412 void
1413 ClearToBottom()
1414 {                               /* clear to the bottom of the screen */
1415     if (GoodStr(T_cd))
1416         (void) tputs(Str(T_cd), Val(T_li), PUTPURE);
1417     else if (GoodStr(T_ce))
1418         (void) tputs(Str(T_ce), Val(T_li), PUTPURE);
1419 }
1420
1421 void
1422 GetTermCaps()
1423 {                               /* read in the needed terminal capabilites */
1424     int i;
1425     const char   *ptr;
1426     char    buf[TC_BUFSIZE];
1427     static char bp[TC_BUFSIZE];
1428     char   *area;
1429     struct termcapstr *t;
1430
1431
1432 #ifdef SIG_WINDOW
1433 # ifdef BSDSIGS
1434     sigmask_t omask;
1435 # endif /* BSDSIGS */
1436     int     lins, cols;
1437
1438     /* don't want to confuse things here */
1439 # ifdef BSDSIGS
1440     omask = sigblock(sigmask(SIG_WINDOW)) & ~sigmask(SIG_WINDOW);
1441 # else /* BSDSIGS */
1442     (void) sighold(SIG_WINDOW);
1443 # endif /* BSDSIGS */
1444 #endif /* SIG_WINDOW */
1445     area = buf;
1446
1447     GotTermCaps = 1;
1448
1449     setname("gettermcaps");
1450     ptr = getenv("TERM");
1451
1452 #ifdef apollo
1453     /* 
1454      * If we are on a pad, we pretend that we are dumb. Otherwise the termcap
1455      * library will put us in a weird screen mode, thinking that we are going
1456      * to use curses
1457      */
1458     if (isapad())
1459         ptr = "dumb";
1460 #endif /* apollo */
1461
1462     if (!ptr || !ptr[0] || !strcmp(ptr, "wm") || !strcmp(ptr,"dmx"))
1463         ptr = "dumb";
1464
1465     setzero(bp, TC_BUFSIZE);
1466
1467     i = tgetent(bp, ptr);
1468     if (i <= 0) {
1469         if (i == -1) {
1470 #if (SYSVREL == 0) || defined(IRIS3D)
1471             xprintf(CGETS(7, 20, "%s: Cannot open /etc/termcap.\n"), progname);
1472         }
1473         else if (i == 0) {
1474 #endif /* SYSVREL */
1475             xprintf(CGETS(7, 21,
1476                           "%s: No entry for terminal type \"%s\"\n"), progname,
1477                     getenv("TERM"));
1478         }
1479         xprintf(CGETS(7, 22, "%s: using dumb terminal settings.\n"), progname);
1480         Val(T_co) = 80;         /* do a dumb terminal */
1481         Val(T_pt) = Val(T_km) = Val(T_li) = 0;
1482         for (t = tstr; t->name != NULL; t++)
1483             TCalloc(t, NULL);
1484     }
1485     else {
1486         /* Can we tab */
1487         Val(T_pt) = tgetflag("pt") && !tgetflag("xt");
1488         /* do we have a meta? */
1489         Val(T_km) = (tgetflag("km") || tgetflag("MT"));
1490         Val(T_am) = tgetflag("am");
1491         Val(T_xn) = tgetflag("xn");
1492         Val(T_co) = tgetnum("co");
1493         Val(T_li) = tgetnum("li");
1494         for (t = tstr; t->name != NULL; t++)
1495             TCalloc(t, tgetstr(t->name, &area));
1496     }
1497     if (Val(T_co) < 2)
1498         Val(T_co) = 80;         /* just in case */
1499     if (Val(T_li) < 1)
1500         Val(T_li) = 24;
1501
1502     T_Cols = (Char) Val(T_co);
1503     T_Lines = (Char) Val(T_li);
1504     if (T_Tabs)
1505         T_Tabs = (Char) Val(T_pt);
1506     T_HasMeta = (Char) Val(T_km);
1507     T_Margin = (Char) Val(T_am) ? MARGIN_AUTO : 0;
1508     T_Margin |= (Char) Val(T_xn) ? MARGIN_MAGIC : 0;
1509     T_CanCEOL = GoodStr(T_ce);
1510     T_CanDel = GoodStr(T_dc) || GoodStr(T_DC);
1511     T_CanIns = GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC);
1512     T_CanUP = GoodStr(T_up) || GoodStr(T_UP);
1513     if (GoodStr(T_me) && GoodStr(T_ue))
1514         me_all = (strcmp(Str(T_me), Str(T_ue)) == 0);
1515     else
1516         me_all = 0;
1517     if (GoodStr(T_me) && GoodStr(T_se))
1518         me_all |= (strcmp(Str(T_me), Str(T_se)) == 0);
1519
1520
1521 #ifdef DEBUG_SCREEN
1522     if (!T_CanUP) {
1523         xprintf(CGETS(7, 23, "%s: WARNING: Your terminal cannot move up.\n",
1524                 progname));
1525         xprintf(CGETS(7, 24, "Editing may be odd for long lines.\n"));
1526     }
1527     if (!T_CanCEOL)
1528         xprintf(CGETS(7, 25, "no clear EOL capability.\n"));
1529     if (!T_CanDel)
1530         xprintf(CGETS(7, 26, "no delete char capability.\n"));
1531     if (!T_CanIns)
1532         xprintf(CGETS(7, 27, "no insert char capability.\n"));
1533 #endif /* DEBUG_SCREEN */
1534
1535
1536
1537 #ifdef SIG_WINDOW
1538     (void) GetSize(&lins, &cols);       /* get the correct window size */
1539     ChangeSize(lins, cols);
1540
1541 # ifdef BSDSIGS
1542     (void) sigsetmask(omask);   /* can change it again */
1543 # else /* BSDSIGS */
1544     (void) sigrelse(SIG_WINDOW);
1545 # endif /* BSDSIGS */
1546 #else /* SIG_WINDOW */
1547     ChangeSize(Val(T_li), Val(T_co));
1548 #endif /* SIG_WINDOW */
1549
1550     BindArrowKeys();
1551 }
1552
1553 #ifdef SIG_WINDOW
1554 /* GetSize():
1555  *      Return the new window size in lines and cols, and
1556  *      true if the size was changed. This can fail if SHIN
1557  *      is not a tty, but it will work in most cases.
1558  */
1559 int
1560 GetSize(lins, cols)
1561     int    *lins, *cols;
1562 {
1563     *cols = Val(T_co);
1564     *lins = Val(T_li);
1565
1566 #ifdef TIOCGWINSZ
1567 # define KNOWsize
1568 # ifndef lint
1569     {
1570         struct winsize ws;      /* from 4.3 */
1571
1572         if (ioctl(SHIN, TIOCGWINSZ, (ioctl_t) &ws) != -1) {
1573             if (ws.ws_col)
1574                 *cols = ws.ws_col;
1575             if (ws.ws_row)
1576                 *lins = ws.ws_row;
1577         }
1578     }
1579 # endif /* !lint */
1580 #else /* TIOCGWINSZ */
1581 # ifdef TIOCGSIZE
1582 #  define KNOWsize
1583     {
1584         struct ttysize ts;      /* from Sun */
1585
1586         if (ioctl(SHIN, TIOCGSIZE, (ioctl_t) &ts) != -1) {
1587             if (ts.ts_cols)
1588                 *cols = ts.ts_cols;
1589             if (ts.ts_lines)
1590                 *lins = ts.ts_lines;
1591         }
1592     }
1593 # endif /* TIOCGSIZE */
1594 #endif /* TIOCGWINSZ */
1595
1596     return (Val(T_co) != *cols || Val(T_li) != *lins);
1597 }
1598
1599 #endif /* SIGWINDOW */
1600
1601 void
1602 ChangeSize(lins, cols)
1603     int     lins, cols;
1604 {
1605     /*
1606      * Just in case
1607      */
1608     Val(T_co) = (cols < 2) ? 80 : cols;
1609     Val(T_li) = (lins < 1) ? 24 : lins;
1610
1611 #ifdef KNOWsize
1612     /*
1613      * We want to affect the environment only when we have a valid
1614      * setup, not when we get bad settings. Consider the following scenario:
1615      * We just logged in, and we have not initialized the editor yet.
1616      * We reset termcap with tset, and not $TERMCAP has the right
1617      * terminal size. But since the editor is not initialized yet, and
1618      * the kernel's notion of the terminal size might be wrong we arrive
1619      * here with lines = columns = 0. If we reset the environment we lose
1620      * our only chance to get the window size right.
1621      */
1622     if (Val(T_co) == cols && Val(T_li) == lins) {
1623         Char    buf[10];
1624         char   *tptr;
1625
1626         if (getenv("COLUMNS")) {
1627             (void) Itoa(Val(T_co), buf, 0, 0);
1628             tsetenv(STRCOLUMNS, buf);
1629         }
1630
1631         if (getenv("LINES")) {
1632             (void) Itoa(Val(T_li), buf, 0, 0);
1633             tsetenv(STRLINES, buf);
1634         }
1635
1636         if ((tptr = getenv("TERMCAP")) != NULL) {
1637             /* Leave 64 characters slop in case we enlarge the termcap string */
1638             Char    termcap[1024+64], backup[1024+64], *ptr;
1639
1640             ptr = str2short(tptr);
1641             (void) Strncpy(termcap, ptr, 1024);
1642             termcap[1023] = '\0';
1643
1644             /* update termcap string; first do columns */
1645             buf[0] = 'c';
1646             buf[1] = 'o';
1647             buf[2] = '#';
1648             buf[3] = '\0';
1649             if ((ptr = Strstr(termcap, buf)) == NULL) {
1650                 (void) Strcpy(backup, termcap);
1651             }
1652             else {
1653                 size_t len = (ptr - termcap) + Strlen(buf);
1654                 (void) Strncpy(backup, termcap, len);
1655                 backup[len] = '\0';
1656                 (void) Itoa(Val(T_co), buf, 0, 0);
1657                 (void) Strcat(backup + len, buf);
1658                 ptr = Strchr(ptr, ':');
1659                 (void) Strcat(backup, ptr);
1660             }
1661
1662             /* now do lines */
1663             buf[0] = 'l';
1664             buf[1] = 'i';
1665             buf[2] = '#';
1666             buf[3] = '\0';
1667             if ((ptr = Strstr(backup, buf)) == NULL) {
1668                 (void) Strcpy(termcap, backup);
1669             }
1670             else {
1671                 size_t len = (ptr - backup) + Strlen(buf);
1672                 (void) Strncpy(termcap, backup, len);
1673                 termcap[len] = '\0';
1674                 (void) Itoa(Val(T_li), buf, 0, 0);
1675                 (void) Strcat(termcap, buf);
1676                 ptr = Strchr(ptr, ':');
1677                 (void) Strcat(termcap, ptr);
1678             }
1679             /*
1680              * Chop the termcap string at 1024 characters to avoid core-dumps
1681              * in the termcap routines
1682              */
1683             termcap[1023] = '\0';
1684             tsetenv(STRTERMCAP, termcap);
1685         }
1686     }
1687 #endif /* KNOWsize */
1688
1689     ReBufferDisplay();          /* re-make display buffers */
1690     ClearDisp();
1691 }