Merge branch 'master' into lua-scripting
[screen-lua.git] / src / termcap.c
blob01082cb999c4a544dd937734299111c86e2be5f2
1 /* Copyright (c) 1993-2002
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Copyright (c) 1987 Oliver Laumann
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (see the file COPYING); if not, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 ****************************************************************
24 #include <sys/types.h>
25 #include "config.h"
26 #include "screen.h"
27 #include "extern.h"
29 #undef DEBUGG
31 extern struct display *display, *displays;
32 extern int real_uid, real_gid, eff_uid, eff_gid;
33 extern struct term term[]; /* terminal capabilities */
34 extern struct NewWindow nwin_undef, nwin_default, nwin_options;
35 extern int force_vt;
36 extern int Z0width, Z1width;
37 extern int hardstatusemu;
38 #ifdef MAPKEYS
39 extern struct action umtab[];
40 extern struct action mmtab[];
41 extern struct action dmtab[];
42 extern struct action ktab[];
43 extern struct kmap_ext *kmap_exts;
44 extern int kmap_extn;
45 extern int DefaultEsc;
46 #endif
49 static void AddCap __P((char *));
50 static void MakeString __P((char *, char *, int, char *));
51 static char *findcap __P((char *, char **, int));
52 static int copyarg __P((char **, char *));
53 static int e_tgetent __P((char *, char *));
54 static char *e_tgetstr __P((char *, char **));
55 static int e_tgetflag __P((char *));
56 static int e_tgetnum __P((char *));
57 #ifdef MAPKEYS
58 static int findseq_ge __P((char *, int, unsigned char **));
59 static void setseqoff __P((unsigned char *, int, int));
60 static int addmapseq __P((char *, int, int));
61 static int remmapseq __P((char *, int));
62 #ifdef DEBUGG
63 static void dumpmap __P((void));
64 #endif
65 #endif
68 char Termcap[TERMCAP_BUFSIZE + 8]; /* new termcap +8:"TERMCAP=" */
69 static int Termcaplen;
70 static int tcLineLen;
71 char Term[MAXSTR+5]; /* +5: "TERM=" */
72 char screenterm[20]; /* new $TERM, usually "screen" */
74 char *extra_incap, *extra_outcap;
76 static const char TermcapConst[] = "\\\n\
77 \t:DO=\\E[%dB:LE=\\E[%dD:RI=\\E[%dC:UP=\\E[%dA:bs:bt=\\E[Z:\\\n\
78 \t:cd=\\E[J:ce=\\E[K:cl=\\E[H\\E[J:cm=\\E[%i%d;%dH:ct=\\E[3g:\\\n\
79 \t:do=^J:nd=\\E[C:pt:rc=\\E8:rs=\\Ec:sc=\\E7:st=\\EH:up=\\EM:\\\n\
80 \t:le=^H:bl=^G:cr=^M:it#8:ho=\\E[H:nw=\\EE:ta=^I:is=\\E)0:";
82 char *
83 gettermcapstring(s)
84 char *s;
86 int i;
88 if (display == 0 || s == 0)
89 return 0;
90 for (i = 0; i < T_N; i++)
92 if (term[i].type != T_STR)
93 continue;
94 if (strcmp(term[i].tcname, s) == 0)
95 return D_tcs[i].str;
97 return 0;
101 * Compile the terminal capabilities for a display.
102 * Input: tgetent(, D_termname) extra_incap, extra_outcap.
103 * Effect: display initialisation.
106 InitTermcap(wi, he)
107 int wi;
108 int he;
110 register char *s;
111 int i;
112 char tbuf[TERMCAP_BUFSIZE], *tp;
113 int t, xue, xse, xme;
115 ASSERT(display);
116 bzero(tbuf, sizeof(tbuf));
117 debug1("InitTermcap: looking for tgetent('%s')\n", D_termname);
118 if (*D_termname == 0 || e_tgetent(tbuf, D_termname) != 1)
120 #ifdef TERMINFO
121 Msg(0, "Cannot find terminfo entry for '%s'.", D_termname);
122 #else
123 Msg(0, "Cannot find termcap entry for '%s'.", D_termname);
124 #endif
125 return -1;
127 debug1("got it:\n%s\n", tbuf);
128 #ifdef DEBUG
129 if (extra_incap)
130 debug1("Extra incap: %s\n", extra_incap);
131 if (extra_outcap)
132 debug1("Extra outcap: %s\n", extra_outcap);
133 #endif
135 if ((D_tentry = (char *)malloc(TERMCAP_BUFSIZE + (extra_incap ? strlen(extra_incap) + 1 : 0))) == 0)
137 Msg(0, strnomem);
138 return -1;
142 * loop through all needed capabilities, record their values in the display
144 tp = D_tentry;
145 for (i = 0; i < T_N; i++)
147 switch(term[i].type)
149 case T_FLG:
150 D_tcs[i].flg = e_tgetflag(term[i].tcname);
151 break;
152 case T_NUM:
153 D_tcs[i].num = e_tgetnum(term[i].tcname);
154 break;
155 case T_STR:
156 D_tcs[i].str = e_tgetstr(term[i].tcname, &tp);
157 /* no empty strings, please */
158 if (D_tcs[i].str && *D_tcs[i].str == 0)
159 D_tcs[i].str = 0;
160 break;
161 default:
162 Panic(0, "Illegal tc type in entry #%d", i);
163 /*NOTREACHED*/
168 * Now a good deal of sanity checks on the retrieved capabilities.
170 if (D_HC)
172 Msg(0, "You can't run screen on a hardcopy terminal.");
173 return -1;
175 if (D_OS)
177 Msg(0, "You can't run screen on a terminal that overstrikes.");
178 return -1;
180 if (!D_CL)
182 Msg(0, "Clear screen capability required.");
183 return -1;
185 if (!D_CM)
187 Msg(0, "Addressable cursor capability required.");
188 return -1;
190 if ((s = getenv("COLUMNS")) && (i = atoi(s)) > 0)
191 D_CO = i;
192 if ((s = getenv("LINES")) && (i = atoi(s)) > 0)
193 D_LI = i;
194 if (wi)
195 D_CO = wi;
196 if (he)
197 D_LI = he;
198 if (D_CO <= 0)
199 D_CO = 80;
200 if (D_LI <= 0)
201 D_LI = 24;
203 if (D_CTF)
205 /* standard fixes for xterms etc */
206 /* assume color for everything that looks ansi-compatible */
207 if (!D_CAF && D_ME && (InStr(D_ME, "\033[m") || InStr(D_ME, "\033[0m")))
209 #ifdef TERMINFO
210 D_CAF = "\033[3%p1%dm";
211 D_CAB = "\033[4%p1%dm";
212 #else
213 D_CAF = "\033[3%dm";
214 D_CAB = "\033[4%dm";
215 #endif
217 if (D_OP && InStr(D_OP, "\033[39;49m"))
218 D_CAX = 1;
219 if (D_OP && (InStr(D_OP, "\033[m") || InStr(D_OP, "\033[0m")))
220 D_OP = 0;
221 /* ISO2022 */
222 if ((D_EA && InStr(D_EA, "\033(B")) || (D_AS && InStr(D_AS, "\033(0")))
223 D_CG0 = 1;
224 if (InStr(D_termname, "xterm") || InStr(D_termname, "rxvt"))
225 D_CXT = 1;
226 /* "be" seems to be standard for xterms... */
227 if (D_CXT)
228 D_BE = 1;
230 if (nwin_options.flowflag == nwin_undef.flowflag)
231 nwin_default.flowflag = D_CNF ? FLOW_NOW * 0 :
232 D_NX ? FLOW_NOW * 1 :
233 FLOW_AUTOFLAG;
234 D_CLP |= (!D_AM || D_XV || D_XN);
235 if (!D_BL)
236 D_BL = "\007";
237 if (!D_BC)
239 if (D_BS)
240 D_BC = "\b";
241 else
242 D_BC = D_LE;
244 if (!D_CR)
245 D_CR = "\r";
246 if (!D_NL)
247 D_NL = "\n";
250 * Set up attribute handling.
251 * This is rather complicated because termcap has different
252 * attribute groups.
255 if (D_UG > 0)
256 D_US = D_UE = 0;
257 if (D_SG > 0)
258 D_SO = D_SE = 0;
259 /* Unfortunatelly there is no 'mg' capability.
260 * For now we think that mg > 0 if sg and ug > 0.
262 if (D_UG > 0 && D_SG > 0)
263 D_MH = D_MD = D_MR = D_MB = D_ME = 0;
265 xue = ATYP_U;
266 xse = ATYP_S;
267 xme = ATYP_M;
269 if (D_SO && D_SE == 0)
271 Msg(0, "Warning: 'so' but no 'se' capability.");
272 if (D_ME)
273 xse = xme;
274 else
275 D_SO = 0;
277 if (D_US && D_UE == 0)
279 Msg(0, "Warning: 'us' but no 'ue' capability.");
280 if (D_ME)
281 xue = xme;
282 else
283 D_US = 0;
285 if ((D_MH || D_MD || D_MR || D_MB) && D_ME == 0)
287 Msg(0, "Warning: 'm?' but no 'me' capability.");
288 D_MH = D_MD = D_MR = D_MB = 0;
291 * Does ME also reverse the effect of SO and/or US? This is not
292 * clearly specified by the termcap manual. Anyway, we should at
293 * least look whether ME and SE/UE are equal:
295 if (D_UE && D_SE && strcmp(D_SE, D_UE) == 0)
296 xse = xue;
297 if (D_SE && D_ME && strcmp(D_ME, D_SE) == 0)
298 xse = xme;
299 if (D_UE && D_ME && strcmp(D_ME, D_UE) == 0)
300 xue = xme;
302 for (i = 0; i < NATTR; i++)
304 D_attrtab[i] = D_tcs[T_ATTR + i].str;
305 D_attrtyp[i] = i == ATTR_SO ? xse : (i == ATTR_US ? xue : xme);
308 /* Set up missing entries (attributes are priority ordered) */
309 s = 0;
310 t = 0;
311 for (i = 0; i < NATTR; i++)
312 if ((s = D_attrtab[i]))
314 t = D_attrtyp[i];
315 break;
317 for (i = 0; i < NATTR; i++)
319 if (D_attrtab[i] == 0)
321 D_attrtab[i] = s;
322 D_attrtyp[i] = t;
324 else
326 s = D_attrtab[i];
327 t = D_attrtyp[i];
330 if (D_CAF || D_CAB || D_CSF || D_CSB)
331 D_hascolor = 1;
332 if (D_UT)
333 D_BE = 1; /* screen erased with background color */
335 if (!D_DO)
336 D_DO = D_NL;
337 if (!D_SF)
338 D_SF = D_NL;
339 if (D_IN)
340 D_IC = D_IM = 0;
341 if (D_EI == 0)
342 D_IM = 0;
343 /* some strange termcap entries have IC == IM */
344 if (D_IC && D_IM && strcmp(D_IC, D_IM) == 0)
345 D_IC = 0;
346 if (D_KE == 0)
347 D_KS = 0;
348 if (D_CVN == 0)
349 D_CVR = 0;
350 if (D_VE == 0)
351 D_VI = D_VS = 0;
352 if (D_CCE == 0)
353 D_CCS = 0;
355 #ifdef FONT
356 if (D_CG0)
358 if (D_CS0 == 0)
359 #ifdef TERMINFO
360 D_CS0 = "\033(%p1%c";
361 #else
362 D_CS0 = "\033(%.";
363 #endif
364 if (D_CE0 == 0)
365 D_CE0 = "\033(B";
366 D_AC = 0;
367 D_EA = 0;
369 else if (D_AC || (D_AS && D_AE)) /* some kind of graphics */
371 D_CS0 = (D_AS && D_AE) ? D_AS : "";
372 D_CE0 = (D_AS && D_AE) ? D_AE : "";
373 D_CC0 = D_AC;
375 else
377 D_CS0 = D_CE0 = "";
378 D_CC0 = 0;
379 D_AC = ""; /* enable default string */
382 for (i = 0; i < 256; i++)
383 D_c0_tab[i] = i;
384 if (D_AC)
386 /* init with default string first */
387 s = "l+m+k+j+u+t+v+w+q-x|n+o~s_p\"r#`+a:f'g#~o.v-^+<,>h#I#0#y<z>";
388 for (i = strlen(s) & ~1; i >= 0; i -= 2)
389 D_c0_tab[(int)(unsigned char)s[i]] = s[i + 1];
391 if (D_CC0)
392 for (i = strlen(D_CC0) & ~1; i >= 0; i -= 2)
393 D_c0_tab[(int)(unsigned char)D_CC0[i]] = D_CC0[i + 1];
394 debug1("ISO2022 = %d\n", D_CG0);
395 #endif /* FONT */
396 if (D_PF == 0)
397 D_PO = 0;
398 debug2("terminal size is %d, %d (says TERMCAP)\n", D_CO, D_LI);
400 #ifdef FONT
401 if (D_CXC)
402 if (CreateTransTable(D_CXC))
403 return -1;
404 #endif
406 /* Termcap fields Z0 & Z1 contain width-changing sequences. */
407 if (D_CZ1 == 0)
408 D_CZ0 = 0;
409 Z0width = 132;
410 Z1width = 80;
412 CheckScreenSize(0);
414 if (D_TS == 0 || D_FS == 0 || D_DS == 0)
415 D_HS = 0;
416 if (D_HS)
418 debug("oy! we have a hardware status line, says termcap\n");
419 if (D_WS < 0)
420 D_WS = 0;
422 D_has_hstatus = hardstatusemu & ~HSTATUS_ALWAYS;
423 if (D_HS && !(hardstatusemu & HSTATUS_ALWAYS))
424 D_has_hstatus = HSTATUS_HS;
426 #ifdef ENCODINGS
427 if (D_CKJ)
429 int enc = FindEncoding(D_CKJ);
430 if (enc != -1)
431 D_encoding = enc;
433 #endif
434 if (!D_tcs[T_NAVIGATE].str && D_tcs[T_NAVIGATE + 1].str)
435 D_tcs[T_NAVIGATE].str = D_tcs[T_NAVIGATE + 1].str; /* kh = @1 */
436 if (!D_tcs[T_NAVIGATE + 2].str && D_tcs[T_NAVIGATE + 3].str)
437 D_tcs[T_NAVIGATE + 2].str = D_tcs[T_NAVIGATE + 3].str; /* kH = @7 */
439 D_UPcost = CalcCost(D_UP);
440 D_DOcost = CalcCost(D_DO);
441 D_NLcost = CalcCost(D_NL);
442 D_LEcost = CalcCost(D_BC);
443 D_NDcost = CalcCost(D_ND);
444 D_CRcost = CalcCost(D_CR);
445 D_IMcost = CalcCost(D_IM);
446 D_EIcost = CalcCost(D_EI);
448 #ifdef AUTO_NUKE
449 if (D_CAN)
451 debug("termcap has AN, setting autonuke\n");
452 D_auto_nuke = 1;
454 #endif
455 if (D_COL > 0)
457 debug1("termcap has OL (%d), setting limit\n", D_COL);
458 D_obufmax = D_COL;
459 D_obuflenmax = D_obuflen - D_obufmax;
462 /* Some xterm entries set F0 and F10 to the same string. Nuke F0. */
463 if (D_tcs[T_CAPS].str && D_tcs[T_CAPS + 10].str && !strcmp(D_tcs[T_CAPS].str, D_tcs[T_CAPS + 10].str))
464 D_tcs[T_CAPS].str = 0;
465 /* Some xterm entries set kD to ^?. Nuke it. */
466 if (D_tcs[T_NAVIGATE_DELETE].str && !strcmp(D_tcs[T_NAVIGATE_DELETE].str, "\0177"))
467 D_tcs[T_NAVIGATE_DELETE].str = 0;
468 /* wyse52 entries have kcub1 == kb == ^H. Nuke... */
469 if (D_tcs[T_CURSOR + 3].str && !strcmp(D_tcs[T_CURSOR + 3].str, "\008"))
470 D_tcs[T_CURSOR + 3].str = 0;
472 #ifdef MAPKEYS
473 D_nseqs = 0;
474 for (i = 0; i < T_OCAPS - T_CAPS; i++)
475 remap(i, 1);
476 for (i = 0; i < kmap_extn; i++)
477 remap(i + (KMAP_KEYS+KMAP_AKEYS), 1);
478 D_seqp = D_kmaps + 3;
479 D_seql = 0;
480 D_seqh = 0;
481 #endif
483 D_tcinited = 1;
484 MakeTermcap(0);
485 #ifdef MAPKEYS
486 CheckEscape();
487 #endif
488 return 0;
491 #ifdef MAPKEYS
494 remap(n, map)
495 int n;
496 int map;
498 char *s = 0;
499 int fl = 0, domap = 0;
500 struct action *a1, *a2, *tab;
501 int l = 0;
502 struct kmap_ext *kme = 0;
504 a1 = 0;
505 if (n >= KMAP_KEYS+KMAP_AKEYS)
507 kme = kmap_exts + (n - (KMAP_KEYS+KMAP_AKEYS));
508 s = kme->str;
509 l = kme->fl & ~KMAP_NOTIMEOUT;
510 fl = kme->fl & KMAP_NOTIMEOUT;
511 a1 = &kme->um;
513 tab = umtab;
514 for (;;)
516 a2 = 0;
517 if (n < KMAP_KEYS+KMAP_AKEYS)
519 a1 = &tab[n];
520 if (n >= KMAP_KEYS)
521 n -= T_OCAPS-T_CURSOR;
522 s = D_tcs[n + T_CAPS].str;
523 l = s ? strlen(s) : 0;
524 if (n >= T_CURSOR-T_CAPS)
525 a2 = &tab[n + (T_OCAPS-T_CURSOR)];
527 if (s == 0 || l == 0)
528 return 0;
529 if (a1 && a1->nr == RC_ILLEGAL)
530 a1 = 0;
531 if (a2 && a2->nr == RC_ILLEGAL)
532 a2 = 0;
533 if (a1 && a1->nr == RC_STUFF && strcmp(a1->args[0], s) == 0)
534 a1 = 0;
535 if (a2 && a2->nr == RC_STUFF && strcmp(a2->args[0], s) == 0)
536 a2 = 0;
537 domap |= (a1 || a2);
538 if (tab == umtab)
540 tab = dmtab;
541 a1 = kme ? &kme->dm : 0;
543 else if (tab == dmtab)
545 tab = mmtab;
546 a1 = kme ? &kme->mm : 0;
548 else
549 break;
551 if (n < KMAP_KEYS)
552 domap = 1;
553 if (map == 0 && domap)
554 return 0;
555 if (map && !domap)
556 return 0;
557 debug3("%smapping %s %#x\n", map? "" :"un",s,n);
558 if (map)
559 return addmapseq(s, l, n | fl);
560 else
561 return remmapseq(s, l);
564 void
565 CheckEscape()
567 struct display *odisplay;
568 int i, nr;
570 if (DefaultEsc >= 0)
571 return;
573 odisplay = display;
574 for (display = displays; display; display = display->d_next)
576 for (i = 0; i < D_nseqs; i += D_kmaps[i + 2] * 2 + 4)
578 nr = (D_kmaps[i] << 8 | D_kmaps[i + 1]) & ~KMAP_NOTIMEOUT;
579 if (nr < KMAP_KEYS+KMAP_AKEYS)
581 if (umtab[nr].nr == RC_COMMAND)
582 break;
583 if (umtab[nr].nr == RC_ILLEGAL && dmtab[nr].nr == RC_COMMAND)
584 break;
586 else
588 struct kmap_ext *kme = kmap_exts + nr - (KMAP_KEYS+KMAP_AKEYS);
589 if (kme->um.nr == RC_COMMAND)
590 break;
591 if (kme->um.nr == RC_ILLEGAL && kme->dm.nr == RC_COMMAND)
592 break;
596 if (display == 0)
598 display = odisplay;
599 return;
601 SetEscape((struct acluser *)0, Ctrl('a'), 'a');
602 if (odisplay->d_user->u_Esc == -1)
603 odisplay->d_user->u_Esc = DefaultEsc;
604 if (odisplay->d_user->u_MetaEsc == -1)
605 odisplay->d_user->u_MetaEsc = DefaultMetaEsc;
606 display = 0;
607 Msg(0, "Warning: escape char set back to ^A");
608 display = odisplay;
611 static int
612 findseq_ge(seq, k, sp)
613 char *seq;
614 int k;
615 unsigned char **sp;
617 unsigned char *p;
618 int j, l;
620 p = D_kmaps;
621 while (p - D_kmaps < D_nseqs)
623 l = p[2];
624 p += 3;
625 for (j = 0; ; j++)
627 if (j == k || j == l)
628 j = l - k;
629 else if (p[j] != ((unsigned char *)seq)[j])
630 j = p[j] - ((unsigned char *)seq)[j];
631 else
632 continue;
633 break;
635 if (j >= 0)
637 *sp = p - 3;
638 return j;
640 p += 2 * l + 1;
642 *sp = p;
643 return -1;
646 static void
647 setseqoff(p, i, o)
648 unsigned char *p;
649 int i;
650 int o;
652 unsigned char *q;
653 int l, k;
655 k = p[2];
656 if (o < 256)
658 p[k + 4 + i] = o;
659 return;
661 /* go for the biggest offset */
662 for (q = p + k * 2 + 4; ; q += l * 2 + 4)
664 l = q[2];
665 if ((q + l * 2 - p) / 2 >= 256)
667 p[k + 4 + i] = (q - p - 4) / 2;
668 return;
673 static int
674 addmapseq(seq, k, nr)
675 char *seq;
676 int k;
677 int nr;
679 int i, j, l, mo, m;
680 unsigned char *p, *q;
682 if (k >= 254)
683 return -1;
684 j = findseq_ge(seq, k, &p);
685 if (j == 0)
687 p[0] = nr >> 8;
688 p[1] = nr;
689 return 0;
691 i = p - D_kmaps;
692 if (D_nseqs + 2 * k + 4 >= D_aseqs)
694 D_kmaps = (unsigned char *)xrealloc((char *)D_kmaps, D_aseqs + 256);
695 D_aseqs += 256;
696 p = D_kmaps + i;
698 D_seqp = D_kmaps + 3;
699 D_seql = 0;
700 D_seqh = 0;
701 evdeq(&D_mapev);
702 if (j > 0)
703 bcopy((char *)p, (char *)p + 2 * k + 4, D_nseqs - i);
704 p[0] = nr >> 8;
705 p[1] = nr;
706 p[2] = k;
707 bcopy(seq, (char *)p + 3, k);
708 bzero(p + k + 3, k + 1);
709 D_nseqs += 2 * k + 4;
710 if (j > 0)
712 q = p + 2 * k + 4;
713 l = q[2];
714 for (i = 0; i < k; i++)
716 if (p[3 + i] != q[3 + i])
718 p[k + 4 + i] = k;
719 break;
721 setseqoff(p, i, q[l + 4 + i] ? q[l + 4 + i] + k + 2: 0);
724 for (q = D_kmaps; q < p; q += 2 * l + 4)
726 l = q[2];
727 for (m = j = 0; j < l; j++)
729 mo = m;
730 if (!m && q[3 + j] != seq[j])
731 m = 1;
732 if (q[l + 4 + j] == 0)
734 if (!mo && m)
735 setseqoff(q, j, (p - q - 4) / 2);
737 else if (q + q[l + 4 + j] * 2 + 4 > p || (q + q[l + 4 + j] * 2 + 4 == p && !m))
738 setseqoff(q, j, q[l + 4 + j] + k + 2);
741 #ifdef DEBUGG
742 dumpmap();
743 #endif
744 return 0;
747 static int
748 remmapseq(seq, k)
749 char *seq;
750 int k;
752 int j, l;
753 unsigned char *p, *q;
755 if (k >= 254 || (j = findseq_ge(seq, k, &p)) != 0)
756 return -1;
757 for (q = D_kmaps; q < p; q += 2 * l + 4)
759 l = q[2];
760 for (j = 0; j < l; j++)
762 if (q + q[l + 4 + j] * 2 + 4 == p)
763 setseqoff(q, j, p[k + 4 + j] ? q[l + 4 + j] + p[k + 4 + j] - k : 0);
764 else if (q + q[l + 4 + j] * 2 + 4 > p)
765 q[l + 4 + j] -= k + 2;
768 if (D_kmaps + D_nseqs > p + 2 * k + 4)
769 bcopy((char *)p + 2 * k + 4, (char *)p, (D_kmaps + D_nseqs) - (p + 2 * k + 4));
770 D_nseqs -= 2 * k + 4;
771 D_seqp = D_kmaps + 3;
772 D_seql = 0;
773 D_seqh = 0;
774 evdeq(&D_mapev);
775 #ifdef DEBUGG
776 dumpmap();
777 #endif
778 return 0;
781 #ifdef DEBUGG
782 static void
783 dumpmap()
785 unsigned char *p;
786 int j, n, l, o, oo;
787 debug("Mappings:\n");
788 p = D_kmaps;
789 if (!p)
790 return;
791 while (p < D_kmaps + D_nseqs)
793 l = p[2];
794 debug1("%d: ", p - D_kmaps + 3);
795 for (j = 0; j < l; j++)
797 o = oo = p[l + 4 + j];
798 if (o)
799 o = 2 * o + 4 + (p + 3 + j - D_kmaps);
800 if (p[j + 3] > ' ' && p[j + 3] < 0177)
802 debug3("%c[%d:%d] ", p[j + 3], oo, o);
804 else
805 debug3("\\%03o[%d:%d] ", p[j + 3], oo, o);
807 n = p[0] << 8 | p[1];
808 debug2(" ==> %d%s\n", n & ~KMAP_NOTIMEOUT, (n & KMAP_NOTIMEOUT) ? " (no timeout)" : "");
809 p += 2 * l + 4;
812 #endif /* DEBUGG */
814 #endif /* MAPKEYS */
817 * Appends to the static variable Termcap
819 static void
820 AddCap(s)
821 char *s;
823 register int n;
825 if (tcLineLen + (n = strlen(s)) > 55 && Termcaplen < TERMCAP_BUFSIZE - 4 - 1)
827 strcpy(Termcap + Termcaplen, "\\\n\t:");
828 Termcaplen += 4;
829 tcLineLen = 0;
831 if (Termcaplen + n < TERMCAP_BUFSIZE - 1)
833 strcpy(Termcap + Termcaplen, s);
834 Termcaplen += n;
835 tcLineLen += n;
837 else
838 Panic(0, "TERMCAP overflow - sorry.");
842 * Reads a displays capabilities and reconstructs a termcap entry in the
843 * global buffer "Termcap". A pointer to this buffer is returned.
845 char *
846 MakeTermcap(aflag)
847 int aflag;
849 char buf[TERMCAP_BUFSIZE];
850 register char *p, *cp, *s, ch, *tname;
851 int i, wi, he;
852 #if 0
853 int found;
854 #endif
856 if (display)
858 wi = D_width;
859 he = D_height;
860 tname = D_termname;
862 else
864 wi = 80;
865 he = 24;
866 tname = "vt100";
868 debug1("MakeTermcap(%d)\n", aflag);
869 if ((s = getenv("SCREENCAP")) && strlen(s) < TERMCAP_BUFSIZE)
871 sprintf(Termcap, "TERMCAP=%s", s);
872 strcpy(Term, "TERM=screen");
873 debug("getenvSCREENCAP o.k.\n");
874 return Termcap;
876 Termcaplen = 0;
877 debug1("MakeTermcap screenterm='%s'\n", screenterm);
878 debug1("MakeTermcap termname='%s'\n", tname);
879 if (*screenterm == '\0' || strlen(screenterm) > MAXSTR - 3)
881 debug("MakeTermcap sets screenterm=screen\n");
882 strcpy(screenterm, "screen");
884 #if 0
885 found = 1;
886 #endif
889 strcpy(Term, "TERM=");
890 p = Term + 5;
891 if (!aflag && strlen(screenterm) + strlen(tname) < MAXSTR-1)
893 sprintf(p, "%s.%s", screenterm, tname);
894 if (e_tgetent(buf, p) == 1)
895 break;
897 #ifdef COLOR
898 if (nwin_default.bce)
900 sprintf(p, "%s-bce", screenterm);
901 if (e_tgetent(buf, p) == 1)
902 break;
904 #endif
905 #ifdef CHECK_SCREEN_W
906 if (wi >= 132)
908 sprintf(p, "%s-w", screenterm);
909 if (e_tgetent(buf, p) == 1)
910 break;
912 #endif
913 strcpy(p, screenterm);
914 if (e_tgetent(buf, p) == 1)
915 break;
916 strcpy(p, "vt100");
917 #if 0
918 found = 0;
919 #endif
921 while (0); /* Goto free programming... */
923 #if 0
924 #ifndef TERMINFO
925 /* check for compatibility problems, displays == 0 after fork */
926 if (found)
928 char xbuf[TERMCAP_BUFSIZE], *xbp = xbuf;
929 if (tgetstr("im", &xbp) && tgetstr("ic", &xbp) && displays)
931 Msg(0, "Warning: im and ic set in %s termcap entry", p);
934 #endif
935 #endif
937 tcLineLen = 100; /* Force NL */
938 if (strlen(Term) > TERMCAP_BUFSIZE - 40)
939 strcpy(Term, "too_long");
940 sprintf(Termcap, "TERMCAP=SC|%s|VT 100/ANSI X3.64 virtual terminal:", Term + 5);
941 Termcaplen = strlen(Termcap);
942 debug1("MakeTermcap decided '%s'\n", p);
943 if (extra_outcap && *extra_outcap)
945 for (cp = extra_outcap; (p = index(cp, ':')); cp = p)
947 ch = *++p;
948 *p = '\0';
949 AddCap(cp);
950 *p = ch;
952 tcLineLen = 100; /* Force NL */
954 debug1("MakeTermcap after outcap '%s'\n", (char *)TermcapConst);
955 if (Termcaplen + strlen(TermcapConst) < TERMCAP_BUFSIZE)
957 strcpy(Termcap + Termcaplen, (char *)TermcapConst);
958 Termcaplen += strlen(TermcapConst);
960 sprintf(buf, "li#%d:co#%d:", he, wi);
961 AddCap(buf);
962 AddCap("am:");
963 if (aflag || (force_vt && !D_COP) || D_CLP || !D_AM)
965 AddCap("xn:");
966 AddCap("xv:");
967 AddCap("LP:");
969 if (aflag || (D_CS && D_SR) || D_AL || D_CAL)
971 AddCap("sr=\\EM:");
972 AddCap("al=\\E[L:");
973 AddCap("AL=\\E[%dL:");
975 else if (D_SR)
976 AddCap("sr=\\EM:");
977 if (aflag || D_CS)
978 AddCap("cs=\\E[%i%d;%dr:");
979 if (aflag || D_CS || D_DL || D_CDL)
981 AddCap("dl=\\E[M:");
982 AddCap("DL=\\E[%dM:");
984 if (aflag || D_DC || D_CDC)
986 AddCap("dc=\\E[P:");
987 AddCap("DC=\\E[%dP:");
989 if (aflag || D_CIC || D_IC || D_IM)
991 AddCap("im=\\E[4h:");
992 AddCap("ei=\\E[4l:");
993 AddCap("mi:");
994 AddCap("IC=\\E[%d@:");
996 #ifdef MAPKEYS
997 AddCap("ks=\\E[?1h\\E=:");
998 AddCap("ke=\\E[?1l\\E>:");
999 #endif
1000 AddCap("vi=\\E[?25l:");
1001 AddCap("ve=\\E[34h\\E[?25h:");
1002 AddCap("vs=\\E[34l:");
1003 AddCap("ti=\\E[?1049h:");
1004 AddCap("te=\\E[?1049l:");
1005 if (display)
1007 if (D_US)
1009 AddCap("us=\\E[4m:");
1010 AddCap("ue=\\E[24m:");
1012 if (D_SO)
1014 AddCap("so=\\E[3m:");
1015 AddCap("se=\\E[23m:");
1017 if (D_MB)
1018 AddCap("mb=\\E[5m:");
1019 if (D_MD)
1020 AddCap("md=\\E[1m:");
1021 if (D_MH)
1022 AddCap("mh=\\E[2m:");
1023 if (D_MR)
1024 AddCap("mr=\\E[7m:");
1025 if (D_MB || D_MD || D_MH || D_MR)
1026 AddCap("me=\\E[m:ms:");
1027 if (D_hascolor)
1028 AddCap("Co#8:pa#64:AF=\\E[3%dm:AB=\\E[4%dm:op=\\E[39;49m:AX:");
1029 if (D_VB)
1030 AddCap("vb=\\Eg:");
1031 #ifndef MAPKEYS
1032 if (D_KS)
1034 AddCap("ks=\\E=:");
1035 AddCap("ke=\\E>:");
1037 if (D_CCS)
1039 AddCap("CS=\\E[?1h:");
1040 AddCap("CE=\\E[?1l:");
1042 #endif
1043 if (D_CG0)
1044 AddCap("G0:");
1045 if (D_CC0 || (D_CS0 && *D_CS0))
1047 AddCap("as=\\E(0:");
1048 AddCap("ae=\\E(B:");
1049 /* avoid `` because some shells dump core... */
1050 AddCap("ac=\\140\\140aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~..--++,,hhII00:");
1052 if (D_PO)
1054 AddCap("po=\\E[5i:");
1055 AddCap("pf=\\E[4i:");
1057 if (D_CZ0)
1059 AddCap("Z0=\\E[?3h:");
1060 AddCap("Z1=\\E[?3l:");
1062 if (D_CWS)
1063 AddCap("WS=\\E[8;%d;%dt:");
1065 for (i = T_CAPS; i < T_ECAPS; i++)
1067 #ifdef MAPKEYS
1068 struct action *act;
1069 if (i < T_OCAPS)
1071 if (i >= T_KEYPAD) /* don't put keypad codes in TERMCAP */
1072 continue; /* - makes it too big */
1073 if (i >= T_CURSOR && i < T_OCAPS)
1075 act = &umtab[i - (T_CURSOR - T_OCAPS + T_CAPS)];
1076 if (act->nr == RC_ILLEGAL)
1077 act = &dmtab[i - (T_CURSOR - T_OCAPS + T_CAPS)];
1079 else
1081 act = &umtab[i - T_CAPS];
1082 if (act->nr == RC_ILLEGAL)
1083 act = &dmtab[i - T_CAPS];
1085 if (act->nr == RC_ILLEGAL && (i == T_NAVIGATE + 1 || i == T_NAVIGATE + 3))
1087 /* kh -> @1, kH -> @7 */
1088 act = &umtab[i - T_CAPS - 1];
1089 if (act->nr == RC_ILLEGAL)
1090 act = &dmtab[i - T_CAPS - 1];
1092 if (act->nr != RC_ILLEGAL)
1094 if (act->nr == RC_STUFF)
1096 MakeString(term[i].tcname, buf, sizeof(buf), act->args[0]);
1097 AddCap(buf);
1099 continue;
1102 #endif
1103 if (display == 0)
1104 continue;
1105 switch(term[i].type)
1107 case T_STR:
1108 if (D_tcs[i].str == 0)
1109 break;
1110 MakeString(term[i].tcname, buf, sizeof(buf), D_tcs[i].str);
1111 AddCap(buf);
1112 break;
1113 case T_FLG:
1114 if (D_tcs[i].flg == 0)
1115 break;
1116 sprintf(buf, "%s:", term[i].tcname);
1117 AddCap(buf);
1118 break;
1119 default:
1120 break;
1123 debug("MakeTermcap: end\n");
1124 return Termcap;
1127 static void
1128 MakeString(cap, buf, buflen, s)
1129 char *cap, *buf;
1130 int buflen;
1131 char *s;
1133 register char *p, *pmax;
1134 register unsigned int c;
1136 p = buf;
1137 pmax = p + buflen - (3+4+2);
1138 *p++ = *cap++;
1139 *p++ = *cap;
1140 *p++ = '=';
1141 while ((c = *s++) && (p < pmax))
1143 switch (c)
1145 case '\033':
1146 *p++ = '\\';
1147 *p++ = 'E';
1148 break;
1149 case ':':
1150 strcpy(p, "\\072");
1151 p += 4;
1152 break;
1153 case '^':
1154 case '\\':
1155 *p++ = '\\';
1156 *p++ = c;
1157 break;
1158 default:
1159 if (c >= 200)
1161 sprintf(p, "\\%03o", c & 0377);
1162 p += 4;
1164 else if (c < ' ')
1166 *p++ = '^';
1167 *p++ = c + '@';
1169 else
1170 *p++ = c;
1173 *p++ = ':';
1174 *p = '\0';
1178 #undef QUOTES
1179 #define QUOTES(p) \
1180 (*p == '\\' && (p[1] == '\\' || p[1] == ',' || p[1] == '%'))
1182 #ifdef FONT
1184 CreateTransTable(s)
1185 char *s;
1187 int curchar;
1188 char *templ, *arg;
1189 int templlen;
1190 int templnsub;
1191 char *p, *sx;
1192 char **ctable;
1193 int l, c;
1195 if ((D_xtable = (char ***)malloc(256 * sizeof(char **))) == 0)
1197 Msg(0, strnomem);
1198 return -1;
1200 bzero((char *)D_xtable, 256 * sizeof(char **));
1202 while (*s)
1204 if (QUOTES(s))
1205 s++;
1206 curchar = (unsigned char)*s++;
1207 if (curchar == 'B')
1208 curchar = 0; /* ASCII */
1209 templ = s;
1210 templlen = 0;
1211 templnsub = 0;
1212 if (D_xtable[curchar] == 0)
1214 if ((D_xtable[curchar] = (char **)malloc(257 * sizeof(char *))) == 0)
1216 Msg(0, strnomem);
1217 FreeTransTable();
1218 return -1;
1220 bzero((char *)D_xtable[curchar], 257 * sizeof(char *));
1222 ctable = D_xtable[curchar];
1223 for(; *s && *s != ','; s++)
1225 if (QUOTES(s))
1226 s++;
1227 else if (*s == '%')
1229 templnsub++;
1230 continue;
1232 templlen++;
1234 if (*s++ == 0)
1235 break;
1236 while (*s && *s != ',')
1238 c = (unsigned char)*s++;
1239 if (QUOTES((s - 1)))
1240 c = (unsigned char)*s++;
1241 else if (c == '%')
1242 c = 256;
1243 if (ctable[c])
1244 free(ctable[c]);
1245 arg = s;
1246 l = copyarg(&s, (char *)0);
1247 if (c != 256)
1248 l = l * templnsub + templlen;
1249 if ((ctable[c] = (char *)malloc(l + 1)) == 0)
1251 Msg(0, strnomem);
1252 FreeTransTable();
1253 return -1;
1255 sx = ctable[c];
1256 for (p = ((c == 256) ? "%" : templ); *p && *p != ','; p++)
1258 if (QUOTES(p))
1259 p++;
1260 else if (*p == '%')
1262 s = arg;
1263 sx += copyarg(&s, sx);
1264 continue;
1266 *sx++ = *p;
1268 *sx = 0;
1269 ASSERT(ctable[c] + l * templnsub + templlen == sx);
1270 debug3("XC: %c %c->%s\n", curchar, c, ctable[c]);
1272 if (*s == ',')
1273 s++;
1275 return 0;
1278 void
1279 FreeTransTable()
1281 char ***p, **q;
1282 int i, j;
1284 if ((p = D_xtable) == 0)
1285 return;
1286 for (i = 0; i < 256; i++, p++)
1288 if (*p == 0)
1289 continue;
1290 q = *p;
1291 for (j = 0; j < 257; j++, q++)
1292 if (*q)
1293 free(*q);
1294 free(*p);
1296 free(D_xtable);
1298 #endif /* FONT */
1300 static int
1301 copyarg(pp, s)
1302 char **pp, *s;
1304 int l;
1305 char *p;
1307 for (l = 0, p = *pp; *p && *p != ','; p++)
1309 if (QUOTES(p))
1310 p++;
1311 if (s)
1312 *s++ = *p;
1313 l++;
1315 if (*p == ',')
1316 p++;
1317 *pp = p;
1318 return l;
1324 ** Termcap routines that use our extra_incap
1328 static int
1329 e_tgetent(bp, name)
1330 char *bp, *name;
1332 int r;
1334 #ifdef USE_SETEUID
1335 xseteuid(real_uid);
1336 xsetegid(real_gid);
1337 #endif
1338 r = tgetent(bp, name);
1339 #ifdef USE_SETEUID
1340 xseteuid(eff_uid);
1341 xsetegid(eff_gid);
1342 #endif
1343 return r;
1347 /* findcap:
1348 * cap = capability we are looking for
1349 * tepp = pointer to bufferpointer
1350 * n = size of buffer (0 = infinity)
1353 static char *
1354 findcap(cap, tepp, n)
1355 char *cap;
1356 char **tepp;
1357 int n;
1359 char *tep;
1360 char c, *p, *cp;
1361 int mode; /* mode: 0=LIT 1=^ 2=\x 3,4,5=\nnn */
1362 int num = 0, capl;
1364 if (!extra_incap)
1365 return 0;
1366 tep = *tepp;
1367 capl = strlen(cap);
1368 cp = 0;
1369 mode = 0;
1370 for (p = extra_incap; *p; )
1372 if (strncmp(p, cap, capl) == 0)
1374 p += capl;
1375 c = *p;
1376 if (c && c != ':' && c != '@')
1377 p++;
1378 if (c == 0 || c == '@' || c == '=' || c == ':' || c == '#')
1379 cp = tep;
1381 while ((c = *p))
1383 p++;
1384 if (mode == 0)
1386 if (c == ':')
1387 break;
1388 if (c == '^')
1389 mode = 1;
1390 if (c == '\\')
1391 mode = 2;
1393 else if (mode == 1)
1395 mode = 0;
1396 c = c & 0x1f;
1398 else if (mode == 2)
1400 mode = 0;
1401 switch(c)
1403 case '0':
1404 case '1':
1405 case '2':
1406 case '3':
1407 case '4':
1408 case '5':
1409 case '6':
1410 case '7':
1411 case '8':
1412 case '9':
1413 mode = 3;
1414 num = 0;
1415 break;
1416 case 'E':
1417 c = 27;
1418 break;
1419 case 'n':
1420 c = '\n';
1421 break;
1422 case 'r':
1423 c = '\r';
1424 break;
1425 case 't':
1426 c = '\t';
1427 break;
1428 case 'b':
1429 c = '\b';
1430 break;
1431 case 'f':
1432 c = '\f';
1433 break;
1436 if (mode > 2)
1438 num = num * 8 + (c - '0');
1439 if (mode++ == 5 || (*p < '0' || *p > '9'))
1441 c = num;
1442 mode = 0;
1445 if (mode)
1446 continue;
1448 if (cp && n != 1)
1450 *cp++ = c;
1451 n--;
1454 if (cp)
1456 *cp++ = 0;
1457 *tepp = cp;
1458 debug2("'%s' found in extra_incap -> %s\n", cap, tep);
1459 return tep;
1462 return 0;
1465 static char *
1466 e_tgetstr(cap, tepp)
1467 char *cap;
1468 char **tepp;
1470 char *tep;
1471 if ((tep = findcap(cap, tepp, 0)))
1472 return (*tep == '@') ? 0 : tep;
1473 return tgetstr(cap, tepp);
1476 static int
1477 e_tgetflag(cap)
1478 char *cap;
1480 char buf[2], *bufp;
1481 char *tep;
1482 bufp = buf;
1483 if ((tep = findcap(cap, &bufp, 2)))
1484 return (*tep == '@') ? 0 : 1;
1485 return tgetflag(cap) > 0;
1488 static int
1489 e_tgetnum(cap)
1490 char *cap;
1492 char buf[20], *bufp;
1493 char *tep, c;
1494 int res, base = 10;
1496 bufp = buf;
1497 if ((tep = findcap(cap, &bufp, 20)))
1499 c = *tep;
1500 if (c == '@')
1501 return -1;
1502 if (c == '0')
1503 base = 8;
1504 res = 0;
1505 while ((c = *tep++) >= '0' && c <= '9')
1506 res = res * base + (c - '0');
1507 return res;
1509 return tgetnum(cap);