1 /* $OpenBSD: captoinfo.c,v 1.17 2023/10/17 09:52:09 nicm Exp $ */
2
3 /****************************************************************************
4 * Copyright 2018-2020,2021 Thomas E. Dickey *
5 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
6 * *
7 * Permission is hereby granted, free of charge, to any person obtaining a *
8 * copy of this software and associated documentation files (the *
9 * "Software"), to deal in the Software without restriction, including *
10 * without limitation the rights to use, copy, modify, merge, publish, *
11 * distribute, distribute with modifications, sublicense, and/or sell *
12 * copies of the Software, and to permit persons to whom the Software is *
13 * furnished to do so, subject to the following conditions: *
14 * *
15 * The above copyright notice and this permission notice shall be included *
16 * in all copies or substantial portions of the Software. *
17 * *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25 * *
26 * Except as contained in this notice, the name(s) of the above copyright *
27 * holders shall not be used in advertising or otherwise to promote the *
28 * sale, use or other dealings in this Software without prior written *
29 * authorization. *
30 ****************************************************************************/
31
32 /****************************************************************************
33 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
34 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
35 * and: Thomas E. Dickey 1996-on *
36 ****************************************************************************/
37
38 /*
39 * captoinfo.c
40 *
41 * Provide conversion in both directions between termcap and terminfo.
42 *
43 * cap-to-info --- conversion between termcap and terminfo formats
44 *
45 * The captoinfo() code was swiped from Ross Ridge's mytinfo package,
46 * adapted to fit ncurses by Eric S. Raymond <esr@snark.thyrsus.com>.
47 *
48 * It has just one entry point:
49 *
50 * char *_nc_captoinfo(n, s, parameterized)
51 *
52 * Convert value s for termcap string capability named n into terminfo
53 * format.
54 *
55 * This code recognizes all the standard 4.4BSD %-escapes:
56 *
57 * %% output `%'
58 * %d output value as in printf %d
59 * %2 output value as in printf %2d
60 * %3 output value as in printf %3d
61 * %. output value as in printf %c
62 * %+x add x to value, then do %.
63 * %>xy if value > x then add y, no output
64 * %r reverse order of two parameters, no output
65 * %i increment by one, no output
66 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
67 * %B BCD (16*(value/10)) + (value%10), no output
68 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
69 *
70 * Also, %02 and %03 are accepted as synonyms for %2 and %3.
71 *
72 * Besides all the standard termcap escapes, this translator understands
73 * the following extended escapes:
74 *
75 * used by GNU Emacs termcap libraries
76 * %a[+*-/=][cp]x GNU arithmetic.
77 * %m xor the first two parameters by 0177
78 * %b backup to previous parameter
79 * %f skip this parameter
80 *
81 * used by the University of Waterloo (MFCF) termcap libraries
82 * %-x subtract parameter FROM char x and output it as a char
83 * %ax add the character x to parameter
84 *
85 * If #define WATERLOO is on, also enable these translations:
86 *
87 * %sx subtract parameter FROM the character x
88 *
89 * By default, this Waterloo translations are not compiled in, because
90 * the Waterloo %s conflicts with the way terminfo uses %s in strings for
91 * function programming.
92 *
93 * Note the two definitions of %a: the GNU definition is translated if the
94 * characters after the 'a' are valid for it, otherwise the UW definition
95 * is translated.
96 */
97
98 #include <curses.priv.h>
99
100 #include <ctype.h>
101 #include <tic.h>
102
103 MODULE_ID("$Id: captoinfo.c,v 1.17 2023/10/17 09:52:09 nicm Exp $")
104
105 #if 0
106 #define DEBUG_THIS(p) DEBUG(9, p)
107 #else
108 #define DEBUG_THIS(p) /* nothing */
109 #endif
110
111 #define MAX_PUSHED 16 /* max # args we can push onto the stack */
112
113 static int stack[MAX_PUSHED]; /* the stack */
114 static int stackptr; /* the next empty place on the stack */
115 static int onstack; /* the top of stack */
116 static int seenm; /* seen a %m */
117 static int seenn; /* seen a %n */
118 static int seenr; /* seen a %r */
119 static int param; /* current parameter */
120 static char *dp; /* pointer to end of the converted string */
121
122 static char *my_string;
123 static size_t my_length;
124
125 static char *
init_string(void)126 init_string(void)
127 /* initialize 'my_string', 'my_length' */
128 {
129 if (my_string == 0)
130 TYPE_MALLOC(char, my_length = 256, my_string);
131
132 *my_string = '\0';
133 return my_string;
134 }
135
136 static char *
save_string(char * d,const char * const s)137 save_string(char *d, const char *const s)
138 {
139 size_t have = (size_t) (d - my_string);
140 size_t need = have + strlen(s) + 2;
141 if (need > my_length) {
142 my_string = (char *) _nc_doalloc(my_string, my_length = (need + need));
143 if (my_string == 0)
144 _nc_err_abort(MSG_NO_MEMORY);
145 d = my_string + have;
146 }
147 _nc_STRCPY(d, s, my_length - have);
148 return d + strlen(d);
149 }
150
151 static NCURSES_INLINE char *
save_char(char * s,int c)152 save_char(char *s, int c)
153 {
154 static char temp[2];
155 temp[0] = (char) c;
156 return save_string(s, temp);
157 }
158
159 static void
push(void)160 push(void)
161 /* push onstack on to the stack */
162 {
163 if (stackptr >= MAX_PUSHED)
164 _nc_warning("string too complex to convert");
165 else
166 stack[stackptr++] = onstack;
167 }
168
169 static void
pop(void)170 pop(void)
171 /* pop the top of the stack into onstack */
172 {
173 if (stackptr == 0) {
174 if (onstack == 0)
175 _nc_warning("I'm confused");
176 else
177 onstack = 0;
178 } else
179 onstack = stack[--stackptr];
180 param++;
181 }
182
183 static int
cvtchar(register const char * sp)184 cvtchar(register const char *sp)
185 /* convert a character to a terminfo push */
186 {
187 unsigned char c = 0;
188 int len;
189
190 switch (*sp) {
191 case '\\':
192 switch (*++sp) {
193 case '\'':
194 case '$':
195 case '\\':
196 case '%':
197 c = UChar(*sp);
198 len = 2;
199 break;
200 case '\0':
201 c = '\\';
202 len = 1;
203 break;
204 case '0':
205 case '1':
206 case '2':
207 case '3':
208 len = 1;
209 while (isdigit(UChar(*sp))) {
210 c = UChar(8 * c + (*sp++ - '0'));
211 len++;
212 }
213 break;
214 default:
215 c = UChar(*sp);
216 len = (c != '\0') ? 2 : 1;
217 break;
218 }
219 break;
220 case '^':
221 len = 2;
222 c = UChar(*++sp);
223 if (c == '?') {
224 c = 127;
225 } else if (c == '\0') {
226 len = 1;
227 } else {
228 c &= 0x1f;
229 }
230 break;
231 default:
232 c = UChar(*sp);
233 len = (c != '\0') ? 1 : 0;
234 }
235 if (isgraph(c) && c != ',' && c != '\'' && c != '\\' && c != ':') {
236 dp = save_string(dp, "%\'");
237 dp = save_char(dp, c);
238 dp = save_char(dp, '\'');
239 } else if (c != '\0') {
240 dp = save_string(dp, "%{");
241 if (c > 99)
242 dp = save_char(dp, c / 100 + '0');
243 if (c > 9)
244 dp = save_char(dp, ((int) (c / 10)) % 10 + '0');
245 dp = save_char(dp, c % 10 + '0');
246 dp = save_char(dp, '}');
247 }
248 return len;
249 }
250
251 static void
getparm(int parm,int n)252 getparm(int parm, int n)
253 /* push n copies of param on the terminfo stack if not already there */
254 {
255 int nn;
256
257 if (seenr) {
258 if (parm == 1)
259 parm = 2;
260 else if (parm == 2)
261 parm = 1;
262 }
263
264 for (nn = 0; nn < n; ++nn) {
265 dp = save_string(dp, "%p");
266 dp = save_char(dp, '0' + parm);
267 }
268
269 if (onstack == parm) {
270 if (n > 1) {
271 _nc_warning("string may not be optimal");
272 dp = save_string(dp, "%Pa");
273 while (n-- > 0) {
274 dp = save_string(dp, "%ga");
275 }
276 }
277 return;
278 }
279 if (onstack != 0)
280 push();
281
282 onstack = parm;
283
284 if (seenn && parm < 3) {
285 dp = save_string(dp, "%{96}%^");
286 }
287
288 if (seenm && parm < 3) {
289 dp = save_string(dp, "%{127}%^");
290 }
291 }
292
293 /*
294 * Convert a termcap string to terminfo format.
295 * 'cap' is the relevant terminfo capability index.
296 * 's' is the string value of the capability.
297 * 'parameterized' tells what type of translations to do:
298 * % translations if 1
299 * pad translations if >=0
300 */
301 NCURSES_EXPORT(char *)
_nc_captoinfo(const char * cap,const char * s,int const parameterized)302 _nc_captoinfo(const char *cap, const char *s, int const parameterized)
303 {
304 const char *capstart;
305
306 stackptr = 0;
307 onstack = 0;
308 seenm = 0;
309 seenn = 0;
310 seenr = 0;
311 param = 1;
312
313 DEBUG_THIS(("_nc_captoinfo params %d, %s", parameterized, s));
314
315 dp = init_string();
316
317 /* skip the initial padding (if we haven't been told not to) */
318 capstart = 0;
319 if (s == 0)
320 s = "";
321 if (parameterized >= 0 && isdigit(UChar(*s)))
322 for (capstart = s; *s != '\0'; s++)
323 if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
324 break;
325
326 while (*s != '\0') {
327 switch (*s) {
328 case '%':
329 s++;
330 if (parameterized < 1) {
331 dp = save_char(dp, '%');
332 break;
333 }
334 switch (*s++) {
335 case '%':
336 dp = save_string(dp, "%%");
337 break;
338 case 'r':
339 if (seenr++ == 1) {
340 _nc_warning("saw %%r twice in %s", cap);
341 }
342 break;
343 case 'm':
344 if (seenm++ == 1) {
345 _nc_warning("saw %%m twice in %s", cap);
346 }
347 break;
348 case 'n':
349 if (seenn++ == 1) {
350 _nc_warning("saw %%n twice in %s", cap);
351 }
352 break;
353 case 'i':
354 dp = save_string(dp, "%i");
355 break;
356 case '6':
357 case 'B':
358 getparm(param, 1);
359 dp = save_string(dp, "%{10}%/%{16}%*");
360 getparm(param, 1);
361 dp = save_string(dp, "%{10}%m%+");
362 break;
363 case '8':
364 case 'D':
365 getparm(param, 2);
366 dp = save_string(dp, "%{2}%*%-");
367 break;
368 case '>':
369 /* %?%{x}%>%t%{y}%+%; */
370 if (s[0] && s[1]) {
371 getparm(param, 2);
372 dp = save_string(dp, "%?");
373 s += cvtchar(s);
374 dp = save_string(dp, "%>%t");
375 s += cvtchar(s);
376 dp = save_string(dp, "%+%;");
377 } else {
378 _nc_warning("expected two characters after %%>");
379 dp = save_string(dp, "%>");
380 }
381 break;
382 case 'a':
383 if ((*s == '=' || *s == '+' || *s == '-'
384 || *s == '*' || *s == '/')
385 && (s[1] == 'p' || s[1] == 'c')
386 && s[2] != '\0') {
387 int l;
388 l = 2;
389 if (*s != '=')
390 getparm(param, 1);
391 if (s[1] == 'p') {
392 getparm(param + s[2] - '@', 1);
393 if (param != onstack) {
394 pop();
395 param--;
396 }
397 l++;
398 } else
399 l += cvtchar(s + 2);
400 switch (*s) {
401 case '+':
402 dp = save_string(dp, "%+");
403 break;
404 case '-':
405 dp = save_string(dp, "%-");
406 break;
407 case '*':
408 dp = save_string(dp, "%*");
409 break;
410 case '/':
411 dp = save_string(dp, "%/");
412 break;
413 case '=':
414 if (seenr) {
415 if (param == 1)
416 onstack = 2;
417 else if (param == 2)
418 onstack = 1;
419 else
420 onstack = param;
421 } else
422 onstack = param;
423 break;
424 }
425 s += l;
426 break;
427 }
428 getparm(param, 1);
429 s += cvtchar(s);
430 dp = save_string(dp, "%+");
431 break;
432 case '+':
433 getparm(param, 1);
434 s += cvtchar(s);
435 dp = save_string(dp, "%+%c");
436 pop();
437 break;
438 case 's':
439 #ifdef WATERLOO
440 s += cvtchar(s);
441 getparm(param, 1);
442 dp = save_string(dp, "%-");
443 #else
444 getparm(param, 1);
445 dp = save_string(dp, "%s");
446 pop();
447 #endif /* WATERLOO */
448 break;
449 case '-':
450 s += cvtchar(s);
451 getparm(param, 1);
452 dp = save_string(dp, "%-%c");
453 pop();
454 break;
455 case '.':
456 getparm(param, 1);
457 dp = save_string(dp, "%c");
458 pop();
459 break;
460 case '0': /* not clear any of the historical termcaps did this */
461 if (*s == '3') {
462 ++s;
463 goto see03;
464 }
465 if (*s == '2') {
466 ++s;
467 goto see02;
468 }
469 goto invalid;
470 case '2':
471 see02:
472 getparm(param, 1);
473 dp = save_string(dp, "%2d");
474 pop();
475 break;
476 case '3':
477 see03:
478 getparm(param, 1);
479 dp = save_string(dp, "%3d");
480 pop();
481 break;
482 case 'd':
483 getparm(param, 1);
484 dp = save_string(dp, "%d");
485 pop();
486 break;
487 case 'f':
488 param++;
489 break;
490 case 'b':
491 param--;
492 break;
493 case '\\':
494 dp = save_string(dp, "%\\");
495 break;
496 default:
497 invalid:
498 dp = save_char(dp, '%');
499 s--;
500 _nc_warning("unknown %% code %s (%#x) in %s",
501 unctrl((chtype) *s), UChar(*s), cap);
502 break;
503 }
504 break;
505 default:
506 if (*s != '\0')
507 dp = save_char(dp, *s++);
508 break;
509 }
510 }
511
512 /*
513 * Now, if we stripped off some leading padding, add it at the end
514 * of the string as mandatory padding.
515 */
516 if (capstart) {
517 dp = save_string(dp, "$<");
518 for (s = capstart; *s != '\0'; s++)
519 if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
520 dp = save_char(dp, *s);
521 else
522 break;
523 dp = save_string(dp, "/>");
524 }
525
526 (void) save_char(dp, '\0');
527
528 DEBUG_THIS(("... _nc_captoinfo %s", NonNull(my_string)));
529
530 return (my_string);
531 }
532
533 /*
534 * Check for an expression that corresponds to "%B" (BCD):
535 * (parameter / 10) * 16 + (parameter % 10)
536 */
537 static int
bcd_expression(const char * str)538 bcd_expression(const char *str)
539 {
540 /* leave this non-const for HPUX */
541 static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
542 int len = 0;
543 char ch1, ch2;
544
545 if (sscanf(str, fmt, &ch1, &ch2) == 2
546 && isdigit(UChar(ch1))
547 && isdigit(UChar(ch2))
548 && (ch1 == ch2)) {
549 len = 28;
550 #ifndef NDEBUG
551 {
552 char buffer[80];
553 int tst;
554 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) fmt, ch1, ch2);
555 tst = strlen(buffer) - 1;
556 assert(len == tst);
557 }
558 #endif
559 }
560 return len;
561 }
562
563 static char *
save_tc_char(char * bufptr,int c1)564 save_tc_char(char *bufptr, int c1)
565 {
566 if (is7bits(c1) && isprint(c1)) {
567 if (c1 == ':' || c1 == '\\')
568 bufptr = save_char(bufptr, '\\');
569 bufptr = save_char(bufptr, c1);
570 } else {
571 char temp[80];
572
573 if (c1 == (c1 & 0x1f)) { /* iscntrl() returns T on 255 */
574 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
575 "%.20s", unctrl((chtype) c1));
576 } else {
577 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
578 "\\%03o", c1);
579 }
580 bufptr = save_string(bufptr, temp);
581 }
582 return bufptr;
583 }
584
585 static char *
save_tc_inequality(char * bufptr,int c1,int c2)586 save_tc_inequality(char *bufptr, int c1, int c2)
587 {
588 bufptr = save_string(bufptr, "%>");
589 bufptr = save_tc_char(bufptr, c1);
590 bufptr = save_tc_char(bufptr, c2);
591 return bufptr;
592 }
593
594 /*
595 * info-to-cap --- conversion between terminfo and termcap formats
596 *
597 * Here are the capabilities infotocap assumes it can translate to:
598 *
599 * %% output `%'
600 * %d output value as in printf %d
601 * %2 output value as in printf %2d
602 * %3 output value as in printf %3d
603 * %. output value as in printf %c
604 * %+c add character c to value, then do %.
605 * %>xy if value > x then add y, no output
606 * %r reverse order of two parameters, no output
607 * %i increment by one, no output
608 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
609 * %B BCD (16*(value/10)) + (value%10), no output
610 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
611 * %m exclusive-or all parameters with 0177 (not in 4.4BSD)
612 */
613
614 #define octal_fixup(n, c) fixups[n].ch = ((fixups[n].ch << 3) | ((c) - '0'))
615
616 /*
617 * Convert a terminfo string to termcap format. Parameters are as in
618 * _nc_captoinfo().
619 */
620 NCURSES_EXPORT(char *)
_nc_infotocap(const char * cap GCC_UNUSED,const char * str,int const parameterized)621 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
622 {
623 int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
624 const char *padding;
625 const char *trimmed = 0;
626 int in0, in1, in2;
627 char ch1 = 0, ch2 = 0;
628 char *bufptr = init_string();
629 char octal[4];
630 int len;
631 int digits;
632 bool syntax_error = FALSE;
633 int myfix = 0;
634 struct {
635 int ch;
636 int offset;
637 } fixups[MAX_TC_FIXUPS];
638
639 DEBUG_THIS(("_nc_infotocap %s params %d, %s",
640 _nc_strict_bsd ? "strict" : "loose",
641 parameterized,
642 _nc_visbuf(str)));
643
644 /* we may have to move some trailing mandatory padding up front */
645 padding = str + strlen(str) - 1;
646 if (padding > str && *padding == '>') {
647 if (padding > (str + 1) && *--padding == '/')
648 --padding;
649 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
650 padding--;
651 if (padding > str && *padding == '<' && *--padding == '$')
652 trimmed = padding;
653 padding += 2;
654
655 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
656 bufptr = save_char(bufptr, *padding++);
657 }
658
659 for (; !syntax_error &&
660 *str &&
661 ((trimmed == 0) || (str < trimmed)); str++) {
662 int c1, c2;
663 char *cp = 0;
664
665 if (str[0] == '^') {
666 if (str[1] == '\0' || (str + 1) == trimmed) {
667 bufptr = save_string(bufptr, "\\136");
668 ++str;
669 } else if (str[1] == '?') {
670 /*
671 * Although the 4.3BSD termcap file has an instance of "kb=^?",
672 * that appears to be just cut/paste since neither 4.3BSD nor
673 * 4.4BSD termcap interprets "^?" as DEL.
674 */
675 bufptr = save_string(bufptr, "\\177");
676 ++str;
677 } else {
678 bufptr = save_char(bufptr, *str++);
679 bufptr = save_char(bufptr, *str);
680 }
681 } else if (str[0] == ':') {
682 bufptr = save_char(bufptr, '\\');
683 bufptr = save_char(bufptr, '0');
684 bufptr = save_char(bufptr, '7');
685 bufptr = save_char(bufptr, '2');
686 } else if (str[0] == '\\') {
687 if (str[1] == '\0' || (str + 1) == trimmed) {
688 bufptr = save_string(bufptr, "\\134");
689 ++str;
690 } else if (str[1] == '^') {
691 bufptr = save_string(bufptr, "\\136");
692 ++str;
693 } else if (str[1] == ',') {
694 bufptr = save_char(bufptr, *++str);
695 } else {
696 int xx1;
697
698 bufptr = save_char(bufptr, *str++);
699 xx1 = *str;
700 if (_nc_strict_bsd) {
701
702 if (isoctal(UChar(xx1))) {
703 int pad = 0;
704 int xx2;
705 int fix = 0;
706
707 if (!isoctal(UChar(str[1])))
708 pad = 2;
709 else if (str[1] && !isoctal(UChar(str[2])))
710 pad = 1;
711
712 /*
713 * Test for "\0", "\00" or "\000" and transform those
714 * into "\200".
715 */
716 if (xx1 == '0'
717 && ((pad == 2) || (str[1] == '0'))
718 && ((pad >= 1) || (str[2] == '0'))) {
719 xx2 = '2';
720 } else {
721 xx2 = '0';
722 pad = 0; /* FIXME - optionally pad to 3 digits */
723 }
724 if (myfix < MAX_TC_FIXUPS) {
725 fix = 3 - pad;
726 fixups[myfix].ch = 0;
727 fixups[myfix].offset = (int) (bufptr
728 - my_string
729 - 1);
730 }
731 while (pad-- > 0) {
732 bufptr = save_char(bufptr, xx2);
733 if (myfix < MAX_TC_FIXUPS) {
734 fixups[myfix].ch <<= 3;
735 fixups[myfix].ch |= (xx2 - '0');
736 }
737 xx2 = '0';
738 }
739 if (myfix < MAX_TC_FIXUPS) {
740 int n;
741 for (n = 0; n < fix; ++n) {
742 fixups[myfix].ch <<= 3;
743 fixups[myfix].ch |= (str[n] - '0');
744 }
745 if (fixups[myfix].ch < 32) {
746 ++myfix;
747 }
748 }
749 } else if (strchr("E\\nrtbf", xx1) == 0) {
750 switch (xx1) {
751 case 'e':
752 xx1 = 'E';
753 break;
754 case 'l':
755 xx1 = 'n';
756 break;
757 case 's':
758 bufptr = save_char(bufptr, '0');
759 bufptr = save_char(bufptr, '4');
760 xx1 = '0';
761 break;
762 case ':':
763 /*
764 * Note: termcap documentation claims that ":"
765 * must be escaped as "\072", however the
766 * documentation is incorrect - read the code.
767 * The replacement does not work reliably,
768 * so the advice is not helpful.
769 */
770 bufptr = save_char(bufptr, '0');
771 bufptr = save_char(bufptr, '7');
772 xx1 = '2';
773 break;
774 default:
775 /* should not happen, but handle this anyway */
776 _nc_SPRINTF(octal, _nc_SLIMIT(sizeof(octal))
777 "%03o", UChar(xx1));
778 bufptr = save_char(bufptr, octal[0]);
779 bufptr = save_char(bufptr, octal[1]);
780 xx1 = octal[2];
781 break;
782 }
783 }
784 } else {
785 if (myfix < MAX_TC_FIXUPS && isoctal(UChar(xx1))) {
786 bool will_fix = TRUE;
787 int n;
788
789 fixups[myfix].ch = 0;
790 fixups[myfix].offset = (int) (bufptr - my_string - 1);
791 for (n = 0; n < 3; ++n) {
792 if (isoctal(str[n])) {
793 octal_fixup(myfix, str[n]);
794 } else {
795 will_fix = FALSE;
796 break;
797 }
798 }
799 if (will_fix && (fixups[myfix].ch < 32))
800 ++myfix;
801 }
802 }
803 bufptr = save_char(bufptr, xx1);
804 }
805 } else if (str[0] == '$' && str[1] == '<') { /* discard padding */
806 str += 2;
807 while (isdigit(UChar(*str))
808 || *str == '.'
809 || *str == '*'
810 || *str == '/'
811 || *str == '>')
812 str++;
813 --str;
814 } else if (sscanf(str,
815 "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
816 &in0, &in1, &in2) == 3
817 && ((in0 == 4 && in1 == 10 && in2 == 48)
818 || (in0 == 3 && in1 == 9 && in2 == 38))) {
819 /* dumb-down an optimized case from xterm-256color for termcap */
820 if ((str = strstr(str, ";m")) == 0)
821 break; /* cannot happen */
822 ++str;
823 if (in2 == 48) {
824 bufptr = save_string(bufptr, "[48;5;%dm");
825 } else {
826 bufptr = save_string(bufptr, "[38;5;%dm");
827 }
828 } else if (str[0] == '%' && str[1] == '%') { /* escaped '%' */
829 bufptr = save_string(bufptr, "%%");
830 ++str;
831 } else if (*str != '%' || (parameterized < 1)) {
832 bufptr = save_char(bufptr, *str);
833 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
834 str = strchr(str, ';');
835 bufptr = save_tc_inequality(bufptr, c1, c2);
836 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
837 str = strchr(str, ';');
838 bufptr = save_tc_inequality(bufptr, c1, ch2);
839 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
840 str = strchr(str, ';');
841 bufptr = save_tc_inequality(bufptr, ch1, c2);
842 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
843 str = strchr(str, ';');
844 bufptr = save_tc_inequality(bufptr, ch1, ch2);
845 } else if ((len = bcd_expression(str)) != 0) {
846 str += len;
847 bufptr = save_string(bufptr, "%B");
848 } else if ((sscanf(str, "%%{%d}%%+%%%c", &c1, &ch2) == 2
849 || sscanf(str, "%%'%c'%%+%%%c", &ch1, &ch2) == 2)
850 && ch2 == 'c'
851 && (cp = strchr(str, '+'))) {
852 str = cp + 2;
853 bufptr = save_string(bufptr, "%+");
854
855 if (ch1)
856 c1 = ch1;
857 bufptr = save_tc_char(bufptr, c1);
858 }
859 /* FIXME: this "works" for 'delta' */
860 else if (strncmp(str, "%{2}%*%-", (size_t) 8) == 0) {
861 str += 7;
862 bufptr = save_string(bufptr, "%D");
863 } else if (strncmp(str, "%{96}%^", (size_t) 7) == 0) {
864 str += 6;
865 if (saw_m++ == 0) {
866 bufptr = save_string(bufptr, "%n");
867 }
868 } else if (strncmp(str, "%{127}%^", (size_t) 8) == 0) {
869 str += 7;
870 if (saw_n++ == 0) {
871 bufptr = save_string(bufptr, "%m");
872 }
873 } else { /* cm-style format element */
874 str++;
875 switch (*str) {
876 case '%':
877 bufptr = save_char(bufptr, '%');
878 break;
879
880 case '0':
881 case '1':
882 case '2':
883 case '3':
884 case '4':
885 case '5':
886 case '6':
887 case '7':
888 case '8':
889 case '9':
890 bufptr = save_char(bufptr, '%');
891 ch1 = 0;
892 ch2 = 0;
893 digits = 0;
894 while (isdigit(UChar(*str))) {
895 if (++digits > 2) {
896 syntax_error = TRUE;
897 break;
898 }
899 ch2 = ch1;
900 ch1 = *str++;
901 if (digits == 2 && ch2 != '0') {
902 syntax_error = TRUE;
903 break;
904 } else if (_nc_strict_bsd) {
905 if (ch1 > '3') {
906 syntax_error = TRUE;
907 break;
908 }
909 } else {
910 bufptr = save_char(bufptr, ch1);
911 }
912 }
913 if (syntax_error)
914 break;
915 /*
916 * Convert %02 to %2 and %03 to %3
917 */
918 if (ch2 == '0' && !_nc_strict_bsd) {
919 ch2 = 0;
920 bufptr[-2] = bufptr[-1];
921 *--bufptr = 0;
922 }
923 if (_nc_strict_bsd) {
924 if (ch2 != 0 && ch2 != '0') {
925 syntax_error = TRUE;
926 } else if (ch1 < '2') {
927 ch1 = 'd';
928 }
929 bufptr = save_char(bufptr, ch1);
930 }
931 if (strchr("oxX.", *str)) {
932 syntax_error = TRUE; /* termcap doesn't have octal, hex */
933 }
934 break;
935
936 case 'd':
937 bufptr = save_string(bufptr, "%d");
938 break;
939
940 case 'c':
941 bufptr = save_string(bufptr, "%.");
942 break;
943
944 /*
945 * %s isn't in termcap, but it is convenient to pass it through
946 * so we can represent things like terminfo pfkey strings in
947 * termcap notation.
948 */
949 case 's':
950 if (_nc_strict_bsd) {
951 syntax_error = TRUE;
952 } else {
953 bufptr = save_string(bufptr, "%s");
954 }
955 break;
956
957 case 'p':
958 str++;
959 if (*str == '1')
960 seenone = 1;
961 else if (*str == '2') {
962 if (!seenone && !seentwo) {
963 bufptr = save_string(bufptr, "%r");
964 seentwo++;
965 }
966 } else if (*str >= '3') {
967 syntax_error = TRUE;
968 }
969 break;
970
971 case 'i':
972 bufptr = save_string(bufptr, "%i");
973 break;
974
975 default:
976 bufptr = save_char(bufptr, *str);
977 syntax_error = TRUE;
978 break;
979 } /* endswitch (*str) */
980 } /* endelse (*str == '%') */
981
982 /*
983 * 'str' always points to the end of what was scanned in this step,
984 * but that may not be the end of the string.
985 */
986 assert(str != 0);
987 if (str == 0 || *str == '\0')
988 break;
989
990 } /* endwhile (*str) */
991
992 if (!syntax_error &&
993 myfix > 0 &&
994 ((int) strlen(my_string) - (4 * myfix)) < MIN_TC_FIXUPS) {
995 while (--myfix >= 0) {
996 char *p = fixups[myfix].offset + my_string;
997 *p++ = '^';
998 *p++ = (char) (fixups[myfix].ch | '@');
999 while ((p[0] = p[2]) != '\0') {
1000 ++p;
1001 }
1002 }
1003 }
1004
1005 DEBUG_THIS(("... _nc_infotocap %s",
1006 syntax_error
1007 ? "<ERR>"
1008 : _nc_visbuf(my_string)));
1009
1010 return (syntax_error ? NULL : my_string);
1011 }
1012
1013 #ifdef MAIN
1014
1015 int curr_line;
1016
1017 int
main(int argc,char * argv[])1018 main(int argc, char *argv[])
1019 {
1020 int c, tc = FALSE;
1021
1022 while ((c = getopt(argc, argv, "c")) != EOF)
1023 switch (c) {
1024 case 'c':
1025 tc = TRUE;
1026 break;
1027 }
1028
1029 curr_line = 0;
1030 for (;;) {
1031 char buf[BUFSIZ];
1032
1033 ++curr_line;
1034 if (fgets(buf, sizeof(buf), stdin) == 0)
1035 break;
1036 buf[strlen(buf) - 1] = '\0';
1037 _nc_set_source(buf);
1038
1039 if (tc) {
1040 char *cp = _nc_infotocap("to termcap", buf, 1);
1041
1042 if (cp)
1043 (void) fputs(cp, stdout);
1044 } else
1045 (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
1046 (void) putchar('\n');
1047 }
1048 return (0);
1049 }
1050 #endif /* MAIN */
1051
1052 #if NO_LEAKS
1053 NCURSES_EXPORT(void)
_nc_captoinfo_leaks(void)1054 _nc_captoinfo_leaks(void)
1055 {
1056 if (my_string != 0) {
1057 FreeAndNull(my_string);
1058 }
1059 my_length = 0;
1060 }
1061 #endif
1062