1 /* $Header: /src/pub/tcsh/ed.screen.c,v 3.48 2000/11/11 23:03:34 christos Exp $ */
3 * ed.screen.c: Editor/termcap-curses interface
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 RCSID("$Id: ed.screen.c,v 3.48 2000/11/11 23:03:34 christos Exp $")
47 * We don't prototype these, cause some systems have them wrong!
49 extern int tgetent __P(());
50 extern char *tgetstr __P(());
51 extern int tgetflag __P(());
52 extern int tgetnum __P(());
53 extern char *tgoto __P(());
54 # define PUTPURE putpure
55 # define PUTRAW putraw
57 extern int tgetent __P((char *, char *));
58 extern char *tgetstr __P((char *, char **));
59 extern int tgetflag __P((char *));
60 extern int tgetnum __P((char *));
61 extern char *tgoto __P((char *, int, int));
62 extern void tputs __P((char *, int, void (*)(int)));
63 # define PUTPURE ((void (*)__P((int))) putpure)
64 # define PUTRAW ((void (*)__P((int))) putraw)
68 /* #define DEBUG_LITERAL */
71 * IMPORTANT NOTE: these routines are allowed to look at the current screen
72 * and the current possition assuming that it is correct. If this is not
73 * true, then the update will be WRONG! This is (should be) a valid
77 #define TC_BUFSIZE 2048
79 #define GoodStr(a) (tstr[a].str != NULL && tstr[a].str[0] != '\0')
80 #define Str(a) tstr[a].str
81 #define Val(a) tval[a].val
193 static struct termcapstr {
207 static struct termcapval {
219 for (i = 0; i < T_str + 1; i++)
220 xfree((ptr_t) tstr[i].long_name);
222 for (i = 0; i < T_val + 1; i++)
223 xfree((ptr_t) tval[i].long_name);
226 tstr[T_al].name = "al";
227 tstr[T_al].long_name = CSAVS(4, 1, "add new blank line");
229 tstr[T_bl].name = "bl";
230 tstr[T_bl].long_name = CSAVS(4, 2, "audible bell");
232 tstr[T_cd].name = "cd";
233 tstr[T_cd].long_name = CSAVS(4, 3, "clear to bottom");
235 tstr[T_ce].name = "ce";
236 tstr[T_ce].long_name = CSAVS(4, 4, "clear to end of line");
238 tstr[T_ch].name = "ch";
239 tstr[T_ch].long_name = CSAVS(4, 5, "cursor to horiz pos");
241 tstr[T_cl].name = "cl";
242 tstr[T_cl].long_name = CSAVS(4, 6, "clear screen");
244 tstr[T_dc].name = "dc";
245 tstr[T_dc].long_name = CSAVS(4, 7, "delete a character");
247 tstr[T_dl].name = "dl";
248 tstr[T_dl].long_name = CSAVS(4, 8, "delete a line");
250 tstr[T_dm].name = "dm";
251 tstr[T_dm].long_name = CSAVS(4, 9, "start delete mode");
253 tstr[T_ed].name = "ed";
254 tstr[T_ed].long_name = CSAVS(4, 10, "end delete mode");
256 tstr[T_ei].name = "ei";
257 tstr[T_ei].long_name = CSAVS(4, 11, "end insert mode");
259 tstr[T_fs].name = "fs";
260 tstr[T_fs].long_name = CSAVS(4, 12, "cursor from status line");
262 tstr[T_ho].name = "ho";
263 tstr[T_ho].long_name = CSAVS(4, 13, "home cursor");
265 tstr[T_ic].name = "ic";
266 tstr[T_ic].long_name = CSAVS(4, 14, "insert character");
268 tstr[T_im].name = "im";
269 tstr[T_im].long_name = CSAVS(4, 15, "start insert mode");
271 tstr[T_ip].name = "ip";
272 tstr[T_ip].long_name = CSAVS(4, 16, "insert padding");
274 tstr[T_kd].name = "kd";
275 tstr[T_kd].long_name = CSAVS(4, 17, "sends cursor down");
277 tstr[T_kl].name = "kl";
278 tstr[T_kl].long_name = CSAVS(4, 18, "sends cursor left");
280 tstr[T_kr].name = "kr";
281 tstr[T_kr].long_name = CSAVS(4, 19, "sends cursor right");
283 tstr[T_ku].name = "ku";
284 tstr[T_ku].long_name = CSAVS(4, 20, "sends cursor up");
286 tstr[T_md].name = "md";
287 tstr[T_md].long_name = CSAVS(4, 21, "begin bold");
289 tstr[T_me].name = "me";
290 tstr[T_me].long_name = CSAVS(4, 22, "end attributes");
292 tstr[T_nd].name = "nd";
293 tstr[T_nd].long_name = CSAVS(4, 23, "non destructive space");
295 tstr[T_se].name = "se";
296 tstr[T_se].long_name = CSAVS(4, 24, "end standout");
298 tstr[T_so].name = "so";
299 tstr[T_so].long_name = CSAVS(4, 25, "begin standout");
301 tstr[T_ts].name = "ts";
302 tstr[T_ts].long_name = CSAVS(4, 26, "cursor to status line");
304 tstr[T_up].name = "up";
305 tstr[T_up].long_name = CSAVS(4, 27, "cursor up one");
307 tstr[T_us].name = "us";
308 tstr[T_us].long_name = CSAVS(4, 28, "begin underline");
310 tstr[T_ue].name = "ue";
311 tstr[T_ue].long_name = CSAVS(4, 29, "end underline");
313 tstr[T_vb].name = "vb";
314 tstr[T_vb].long_name = CSAVS(4, 30, "visible bell");
316 tstr[T_DC].name = "DC";
317 tstr[T_DC].long_name = CSAVS(4, 31, "delete multiple chars");
319 tstr[T_DO].name = "DO";
320 tstr[T_DO].long_name = CSAVS(4, 32, "cursor down multiple");
322 tstr[T_IC].name = "IC";
323 tstr[T_IC].long_name = CSAVS(4, 33, "insert multiple chars");
325 tstr[T_LE].name = "LE";
326 tstr[T_LE].long_name = CSAVS(4, 34, "cursor left multiple");
328 tstr[T_RI].name = "RI";
329 tstr[T_RI].long_name = CSAVS(4, 35, "cursor right multiple");
331 tstr[T_UP].name = "UP";
332 tstr[T_UP].long_name = CSAVS(4, 36, "cursor up multiple");
334 tstr[T_kh].name = "kh";
335 tstr[T_kh].long_name = CSAVS(4, 37, "send cursor home");
337 tstr[T_at7].name = "@7";
338 tstr[T_at7].long_name = CSAVS(4, 38, "send cursor end");
340 tstr[T_str].name = NULL;
341 tstr[T_str].long_name = NULL;
344 tval[T_am].name = "am";
345 tval[T_am].long_name = CSAVS(4, 37, "Has automatic margins");
347 tval[T_pt].name = "pt";
348 tval[T_pt].long_name = CSAVS(4, 38, "Can use physical tabs");
350 tval[T_li].name = "li";
351 tval[T_li].long_name = CSAVS(4, 39, "Number of lines");
353 tval[T_co].name = "co";
354 tval[T_co].long_name = CSAVS(4, 40, "Number of columns");
356 tval[T_km].name = "km";
357 tval[T_km].long_name = CSAVS(4, 41, "Has meta key");
359 tval[T_xn].name = "xn";
360 tval[T_xn].long_name = CSAVS(4, 42, "Newline ignored at right margin");
362 tval[T_val].name = NULL;
363 tval[T_val].long_name = NULL;
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)
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
379 static bool me_all = 0; /* does two or more of the attributes use me */
381 static void ReBufferDisplay __P((void));
382 static void TCalloc __P((struct termcapstr *, char *));
387 struct termcapstr *t;
390 static char termcap_alloc[TC_BUFSIZE];
391 char termbuf[TC_BUFSIZE];
392 struct termcapstr *ts;
396 if (cap == NULL || *cap == '\0') {
406 tlen = strlen(t->str);
409 * New string is shorter; no need to allocate space
412 (void) strcpy(t->str, cap);
417 * New string is longer; see if we have enough space to append
419 if (tloc + 3 < TC_BUFSIZE) {
420 (void) strcpy(t->str = &termcap_alloc[tloc], cap);
421 tloc += clen + 1; /* one for \0 */
426 * Compact our buffer; no need to check compaction, cause we know it
430 for (ts = tstr; ts->name != NULL; ts++)
431 if (t != ts && ts->str != NULL && ts->str[0] != '\0') {
434 for (ptr = ts->str; *ptr != '\0'; termbuf[tlen++] = *ptr++)
436 termbuf[tlen++] = '\0';
438 (void) memmove((ptr_t) termcap_alloc, (ptr_t) termbuf, (size_t) TC_BUFSIZE);
440 if (tloc + 3 >= TC_BUFSIZE) {
441 stderror(ERR_NAME | ERR_TCNOSTR);
444 (void) strcpy(t->str = &termcap_alloc[tloc], cap);
445 tloc += clen + 1; /* one for \0 */
455 struct termcapstr *t;
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)?
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) ?
474 CGETS(7, 11, "does not have"));
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)"));
493 for (bufp = b; *bufp != NULL; bufp++)
494 xfree((ptr_t) * bufp);
500 for (bufp = b; *bufp != NULL; bufp++)
501 xfree((ptr_t) * bufp);
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)));
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)));
522 struct termcapstr *ts;
523 struct termcapval *tv;
526 * Do the strings first
529 for (ts = tstr; ts->name != NULL; ts++)
530 if (strcmp(ts->name, what) == 0)
532 if (ts->name != NULL) {
537 if (GoodStr(T_me) && GoodStr(T_ue))
538 me_all = (strcmp(Str(T_me), Str(T_ue)) == 0);
541 if (GoodStr(T_me) && GoodStr(T_se))
542 me_all |= (strcmp(Str(T_me), Str(T_se)) == 0);
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);
552 * Do the numeric ones second
554 for (tv = tval; tv->name != NULL; tv++)
555 if (strcmp(tv->name, what) == 0)
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)
563 else if (strcmp(how, "no") == 0)
566 stderror(ERR_SETTCUS, tv->name);
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));
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));
586 stderror(ERR_NAME | ERR_TCCAP, what);
592 * Print the termcap string out with variable substitution
598 char *cap, *scap, cv[BUFSIZE];
599 int arg_need, arg_cols, arg_rows;
600 int verbose = 0, silent = 0;
602 static char *fmts = "%s\n", *fmtd = "%d\n";
603 struct termcapstr *t;
604 char buf[TC_BUFSIZE];
614 stderror(ERR_NAME | ERR_NOMATCH);
617 v = gargv = saveblk(v);
620 if (!*v || *v[0] == '\0')
622 if (v[0][0] == '-') {
631 stderror(ERR_NAME | ERR_TCUSAGE);
636 if (!*v || *v[0] == '\0')
638 (void) strcpy(cv, short2str(*v));
639 if (strcmp(cv, "tabs") == 0) {
640 xprintf(fmts, T_Tabs ? CGETS(7, 14, "yes") :
645 else if (strcmp(cv, "meta") == 0) {
646 xprintf(fmts, Val(T_km) ? CGETS(7, 14, "yes") :
651 else if (strcmp(cv, "xn") == 0) {
652 xprintf(fmts, T_Margin & MARGIN_MAGIC ? CGETS(7, 14, "yes") :
657 else if (strcmp(cv, "am") == 0) {
658 xprintf(fmts, T_Margin & MARGIN_AUTO ? CGETS(7, 14, "yes") :
663 else if (strcmp(cv, "baud") == 0) {
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);
676 else if (strcmp(cv, "rows") == 0 || strcmp(cv, "lines") == 0) {
677 xprintf(fmtd, Val(T_li));
681 else if (strcmp(cv, "cols") == 0) {
682 xprintf(fmtd, Val(T_co));
688 * Try to use our local definition first
691 for (t = tstr; t->name != NULL; t++)
692 if (strcmp(t->name, cv) == 0) {
697 scap = tgetstr(cv, &area);
698 if (!scap || scap[0] == '\0') {
700 xprintf(CGETS(7, 14, "yes\n"));
706 stderror(ERR_NAME | ERR_TCCAP, cv);
710 * Count home many values we need for this capability.
712 for (cap = scap, arg_need = 0; *cap; cap++)
732 * hpux has lot's of them...
735 stderror(ERR_NAME | ERR_TCPARM, *cap);
736 /* This is bad, but I won't complain */
747 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
749 (void) tputs(scap, 1, PUTRAW);
753 if (!*v || *v[0] == '\0')
754 stderror(ERR_NAME | ERR_TCNARGS, cv, 1);
756 arg_rows = atoi(short2str(*v));
762 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
764 (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, PUTRAW);
767 /* This is wrong, but I will ignore it... */
769 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
773 if (!*v || *v[0] == '\0') {
777 stderror(ERR_NAME | ERR_TCNARGS, cv, 2);
779 arg_cols = atoi(short2str(*v));
781 if (!*v || *v[0] == '\0') {
785 stderror(ERR_NAME | ERR_TCNARGS, cv, 2);
787 arg_rows = atoi(short2str(*v));
793 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
795 (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows, PUTRAW);
805 bool GotTermCaps = 0;
831 arrow[A_K_DN].fun.cmd = F_DOWN_HIST;
832 arrow[A_K_DN].type = XK_CMD;
834 arrow[A_K_UP].fun.cmd = F_UP_HIST;
835 arrow[A_K_UP].type = XK_CMD;
837 arrow[A_K_LT].fun.cmd = F_CHARBACK;
838 arrow[A_K_LT].type = XK_CMD;
840 arrow[A_K_RT].fun.cmd = F_CHARFWD;
841 arrow[A_K_RT].type = XK_CMD;
843 arrow[A_K_HO].fun.cmd = F_TOBEG;
844 arrow[A_K_HO].type = XK_CMD;
846 arrow[A_K_EN].fun.cmd = F_TOEND;
847 arrow[A_K_EN].type = XK_CMD;
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'};
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');
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);
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);
919 SetArrowKeys(name, fun, type)
925 for (i = 0; i < A_K_NKEYS; i++)
926 if (Strcmp(name->buf, arrow[i].name) == 0) {
928 arrow[i].type = type;
939 for (i = 0; i < A_K_NKEYS; i++)
940 if (Strcmp(name, arrow[i].name) == 0)
950 for (i = 0; i < A_K_NKEYS; i++)
951 if (Strcmp(name->buf, arrow[i].name) == 0) {
952 arrow[i].type = XK_NOD;
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) {
968 cs.buf = arrow[i].name;
969 cs.len = Strlen(cs.buf);
970 (void) printOne(&cs, &arrow[i].fun, arrow[i].type);
985 map = VImode ? CcAltMap : CcKeyMap;
986 dmap = VImode ? CcViCmdMap : CcEmacsMap;
990 for (i = 0; i < A_K_NKEYS; i++) {
991 p = tstr[arrow[i].key].str;
993 j = (unsigned char) *p;
994 cs.buf = str2short(p);
995 cs.len = Strlen(cs.buf);
997 * Assign the arrow keys only if:
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.
1004 if (arrow[i].type == XK_NOD) {
1005 ClearXkey(map, &cs);
1008 if (p[1] && (dmap[j] == map[j] || map[j] == F_XKEY)) {
1009 AddXkey(&cs, &arrow[i].fun, arrow[i].type);
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;
1017 AddXkey(&cs, &arrow[i].fun, arrow[i].type);
1024 static Char cur_atr = 0; /* current 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);
1040 if ((atr & BOLD) != (cur_atr & BOLD)) {
1042 if (GoodStr(T_md) && GoodStr(T_me)) {
1043 (void) tputs(Str(T_md), 1, PUTPURE);
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;
1054 if ((cur_atr & UNDER) && GoodStr(T_ue)) {
1055 (void) tputs(Str(T_ue), 1, PUTPURE);
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;
1070 if (GoodStr(T_se)) {
1071 (void) tputs(Str(T_se), 1, PUTPURE);
1072 cur_atr &= ~STANDOUT;
1076 if ((atr & UNDER) != (cur_atr & UNDER)) {
1078 if (GoodStr(T_us) && GoodStr(T_ue)) {
1079 (void) tputs(Str(T_us), 1, PUTPURE);
1084 if (GoodStr(T_ue)) {
1085 (void) tputs(Str(T_ue), 1, PUTPURE);
1093 /* PWP 6-27-88 -- if the tty driver thinks that we can tab, we ask termcap */
1101 MoveToLine(where) /* move to line <where> (first line == 0) */
1102 int where; /* as efficiently as possible; */
1106 if (where == CursorV)
1109 if (where > TermV) {
1111 xprintf("MoveToLine: where is ridiculous: %d\r\n", where);
1113 #endif /* DEBUG_SCREEN */
1117 del = where - CursorV;
1119 #ifndef WINNT_NATIVE
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*/
1129 if ((del > 1) && GoodStr(T_DO)) {
1130 (void) tputs(tgoto(Str(T_DO), del, del), del, PUTPURE);
1134 for ( ; del > 0; del--)
1135 (void) putraw('\n');
1136 CursorH = 0; /* because the \n will become \r\n */
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);
1147 for (i = 0; i < -del; i++)
1148 (void) tputs(Str(T_up), 1, PUTPURE);
1151 #else /* WINNT_NATIVE */
1152 NT_MoveToLineOrChar(del, 1);
1153 #endif /* !WINNT_NATIVE */
1154 CursorV = where; /* now where is here */
1158 MoveToChar(where) /* move to character position (where) */
1160 { /* as efficiently as possible */
1161 #ifndef WINNT_NATIVE
1165 #endif /* WINNT_NATIVE */
1166 if (where == CursorH)
1169 if (where >= TermH) {
1171 xprintf("MoveToChar: where is riduculous: %d\r\n", where);
1173 #endif /* DEBUG_SCREEN */
1177 if (!where) { /* if where is first column */
1178 (void) putraw('\r'); /* do a CR */
1183 #ifndef WINNT_NATIVE
1184 del = where - CursorH;
1186 if ((del < -4 || del > 4) && GoodStr(T_ch))
1187 /* go there directly */
1188 (void) tputs(tgoto(Str(T_ch), where, where), where, PUTPURE);
1191 if (del > 0) { /* moving forward */
1192 if ((del > 4) && GoodStr(T_RI))
1193 (void) tputs(tgoto(Str(T_RI), del, del), del, PUTPURE);
1195 /* if I can do tabs, use them */
1199 #endif /* DSPMBYTE */
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! */
1212 /* it's usually cheaper to just write the chars, so we do. */
1214 /* NOTE THAT so_write() WILL CHANGE CursorH!!! */
1215 so_write(&Display[CursorV][CursorH], where - CursorH);
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)))
1226 (void) putraw('\r'); /* do a CR */
1228 goto mc_again; /* and try again */
1230 for (i = 0; i < -del; i++)
1231 (void) putraw('\b');
1235 #else /* WINNT_NATIVE */
1236 NT_MoveToLineOrChar(where, 0);
1237 #endif /* !WINNT_NATIVE */
1238 CursorH = where; /* now where is here */
1247 return; /* catch bugs */
1251 xprintf("so_write: n is riduculous: %d\r\n", n);
1253 #endif /* DEBUG_SCREEN */
1258 if (*cp & LITERAL) {
1259 extern Char *litptr[];
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)
1268 char buf[256], *ptr = &buf[0];
1269 for (d = litptr[*cp++ & CHAR]; *d & LITERAL; d++)
1270 *ptr++ = (*d & CHAR);
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 */
1282 (void) putraw(*cp++);
1286 if (CursorH >= TermH) { /* wrap? */
1287 if (T_Margin & MARGIN_AUTO) { /* yes */
1290 if (T_Margin & MARGIN_MAGIC) {
1291 /* force the wrap to avoid the "magic" situation */
1293 if ((c = Display[CursorV][CursorH]) != '\0')
1300 else /* no wrap, but cursor stays on screen */
1301 CursorH = TermH - 1;
1307 DeleteChars(num) /* deletes <num> characters */
1315 xprintf(CGETS(7, 16, "ERROR: cannot delete\r\n"));
1316 #endif /* DEBUG_EDIT */
1323 xprintf(CGETS(7, 17, "DeleteChars: num is riduculous: %d\r\n"), num);
1325 #endif /* DEBUG_SCREEN */
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);
1335 if (GoodStr(T_dm)) /* if I have delete mode */
1336 (void) tputs(Str(T_dm), 1, PUTPURE);
1338 if (GoodStr(T_dc)) /* else do one at a time */
1340 (void) tputs(Str(T_dc), 1, PUTPURE);
1342 if (GoodStr(T_ed)) /* if I have delete mode */
1343 (void) tputs(Str(T_ed), 1, PUTPURE);
1347 Insert_write(cp, num) /* Puts terminal in insert character mode, */
1349 register int num; /* or inserts num characters in the line */
1355 xprintf(CGETS(7, 18, "ERROR: cannot insert\r\n"));
1356 #endif /* DEBUG_EDIT */
1363 xprintf(CGETS(7, 19, "StartInsert: num is riduculous: %d\r\n"), num);
1365 #endif /* DEBUG_SCREEN */
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 */
1376 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
1377 (void) tputs(Str(T_im), 1, PUTPURE);
1381 (void) putraw(*cp++);
1384 if (GoodStr(T_ip)) /* have to make num chars insert */
1385 (void) tputs(Str(T_ip), 1, PUTPURE);
1387 (void) tputs(Str(T_ei), 1, PUTPURE);
1392 if (GoodStr(T_ic)) /* have to make num chars insert */
1393 (void) tputs(Str(T_ic), 1, PUTPURE); /* insert a char */
1395 (void) putraw(*cp++);
1399 if (GoodStr(T_ip)) /* have to make num chars insert */
1400 (void) tputs(Str(T_ip), 1, PUTPURE);/* pad the inserted char */
1407 ClearEOL(num) /* clear to end of line. There are num */
1408 int num; /* characters to clear */
1415 if (T_CanCEOL && GoodStr(T_ce))
1416 (void) tputs(Str(T_ce), 1, PUTPURE);
1418 for (i = 0; i < num; i++)
1420 CursorH += num; /* have written num spaces */
1426 { /* clear the whole screen and home */
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);
1436 (void) putraw('\r');
1437 (void) putraw('\n');
1443 { /* produce a sound */
1445 if (adrof(STRnobeep))
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);
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 */
1463 { /* clear to the bottom of the screen */
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);
1472 { /* read in the needed terminal capabilites */
1475 char buf[TC_BUFSIZE];
1476 static char bp[TC_BUFSIZE];
1478 struct termcapstr *t;
1484 # endif /* BSDSIGS */
1487 /* don't want to confuse things here */
1489 omask = sigblock(sigmask(SIG_WINDOW)) & ~sigmask(SIG_WINDOW);
1490 # else /* BSDSIGS */
1491 (void) sighold(SIG_WINDOW);
1492 # endif /* BSDSIGS */
1493 #endif /* SIG_WINDOW */
1498 setname("gettermcaps");
1499 ptr = getenv("TERM");
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
1511 if (!ptr || !ptr[0] || !strcmp(ptr, "wm") || !strcmp(ptr,"dmx"))
1514 setzero(bp, TC_BUFSIZE);
1516 i = tgetent(bp, ptr);
1519 #if (SYSVREL == 0) || defined(IRIS3D)
1520 xprintf(CGETS(7, 20, "%s: Cannot open /etc/termcap.\n"), progname);
1523 #endif /* SYSVREL */
1524 xprintf(CGETS(7, 21,
1525 "%s: No entry for terminal type \"%s\"\n"), progname,
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++)
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));
1547 Val(T_co) = 80; /* just in case */
1551 T_Cols = (Char) Val(T_co);
1552 T_Lines = (Char) Val(T_li);
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);
1566 if (GoodStr(T_me) && GoodStr(T_se))
1567 me_all |= (strcmp(Str(T_me), Str(T_se)) == 0);
1572 xprintf(CGETS(7, 23, "%s: WARNING: Your terminal cannot move up.\n",
1574 xprintf(CGETS(7, 24, "Editing may be odd for long lines.\n"));
1577 xprintf(CGETS(7, 25, "no clear EOL capability.\n"));
1579 xprintf(CGETS(7, 26, "no delete char capability.\n"));
1581 xprintf(CGETS(7, 27, "no insert char capability.\n"));
1582 #endif /* DEBUG_SCREEN */
1587 (void) GetSize(&lins, &cols); /* get the correct window size */
1588 ChangeSize(lins, cols);
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 */
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.
1619 struct winsize ws; /* from 4.3 */
1621 if (ioctl(SHIN, TIOCGWINSZ, (ioctl_t) &ws) != -1) {
1629 #else /* TIOCGWINSZ */
1633 struct ttysize ts; /* from Sun */
1635 if (ioctl(SHIN, TIOCGSIZE, (ioctl_t) &ts) != -1) {
1639 *lins = ts.ts_lines;
1642 # endif /* TIOCGSIZE */
1643 #endif /* TIOCGWINSZ */
1645 return (Val(T_co) != *cols || Val(T_li) != *lins);
1648 #endif /* SIGWINDOW */
1651 ChangeSize(lins, cols)
1657 Val(T_co) = (cols < 2) ? 80 : cols;
1658 Val(T_li) = (lins < 1) ? 24 : lins;
1661 nt_set_size(lins,cols);
1662 #endif /* WINNT_NATIVE */
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.
1674 if (Val(T_co) == cols && Val(T_li) == lins) {
1678 if (getenv("COLUMNS")) {
1679 (void) Itoa(Val(T_co), buf, 0, 0);
1680 tsetenv(STRCOLUMNS, buf);
1683 if (getenv("LINES")) {
1684 (void) Itoa(Val(T_li), buf, 0, 0);
1685 tsetenv(STRLINES, buf);
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;
1693 ptr = str2short(tptr);
1694 (void) Strncpy(termcap, ptr, 1024);
1695 termcap[1023] = '\0';
1697 /* update termcap string; first do columns */
1702 if ((ptr = Strstr(termcap, buf)) == NULL) {
1703 (void) Strcpy(backup, termcap);
1706 i = (int) (ptr - termcap + Strlen(buf));
1707 (void) Strncpy(backup, termcap, (size_t) i);
1709 (void) Itoa(Val(T_co), buf, 0, 0);
1710 (void) Strcat(backup + i, buf);
1711 ptr = Strchr(ptr, ':');
1712 (void) Strcat(backup, ptr);
1720 if ((ptr = Strstr(backup, buf)) == NULL) {
1721 (void) Strcpy(termcap, backup);
1724 i = (int) (ptr - backup + Strlen(buf));
1725 (void) Strncpy(termcap, backup, (size_t) i);
1727 (void) Itoa(Val(T_li), buf, 0, 0);
1728 (void) Strcat(termcap, buf);
1729 ptr = Strchr(ptr, ':');
1730 (void) Strcat(termcap, ptr);
1733 * Chop the termcap string at 1024 characters to avoid core-dumps
1734 * in the termcap routines
1736 termcap[1023] = '\0';
1737 tsetenv(STRTERMCAP, termcap);
1740 #endif /* KNOWsize */
1742 ReBufferDisplay(); /* re-make display buffers */