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