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