]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/captoinfo.c
ncurses 6.1 - patch 20190803
[ncurses.git] / ncurses / tinfo / captoinfo.c
1 /****************************************************************************
2  * Copyright (c) 1998-2017,2018 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.96 2018/05/12 16:46:55 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 = 2;
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 = 1;
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 {
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++)
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                 getparm(param, 2);
364                 /* %?%{x}%>%t%{y}%+%; */
365                 dp = save_string(dp, "%?");
366                 s += cvtchar(s);
367                 dp = save_string(dp, "%>%t");
368                 s += cvtchar(s);
369                 dp = save_string(dp, "%+%;");
370                 break;
371             case 'a':
372                 if ((*s == '=' || *s == '+' || *s == '-'
373                      || *s == '*' || *s == '/')
374                     && (s[1] == 'p' || s[1] == 'c')
375                     && s[2] != '\0') {
376                     int l;
377                     l = 2;
378                     if (*s != '=')
379                         getparm(param, 1);
380                     if (s[1] == 'p') {
381                         getparm(param + s[2] - '@', 1);
382                         if (param != onstack) {
383                             pop();
384                             param--;
385                         }
386                         l++;
387                     } else
388                         l += cvtchar(s + 2);
389                     switch (*s) {
390                     case '+':
391                         dp = save_string(dp, "%+");
392                         break;
393                     case '-':
394                         dp = save_string(dp, "%-");
395                         break;
396                     case '*':
397                         dp = save_string(dp, "%*");
398                         break;
399                     case '/':
400                         dp = save_string(dp, "%/");
401                         break;
402                     case '=':
403                         if (seenr) {
404                             if (param == 1)
405                                 onstack = 2;
406                             else if (param == 2)
407                                 onstack = 1;
408                             else
409                                 onstack = param;
410                         } else
411                             onstack = param;
412                         break;
413                     }
414                     s += l;
415                     break;
416                 }
417                 getparm(param, 1);
418                 s += cvtchar(s);
419                 dp = save_string(dp, "%+");
420                 break;
421             case '+':
422                 getparm(param, 1);
423                 s += cvtchar(s);
424                 dp = save_string(dp, "%+%c");
425                 pop();
426                 break;
427             case 's':
428 #ifdef WATERLOO
429                 s += cvtchar(s);
430                 getparm(param, 1);
431                 dp = save_string(dp, "%-");
432 #else
433                 getparm(param, 1);
434                 dp = save_string(dp, "%s");
435                 pop();
436 #endif /* WATERLOO */
437                 break;
438             case '-':
439                 s += cvtchar(s);
440                 getparm(param, 1);
441                 dp = save_string(dp, "%-%c");
442                 pop();
443                 break;
444             case '.':
445                 getparm(param, 1);
446                 dp = save_string(dp, "%c");
447                 pop();
448                 break;
449             case '0':           /* not clear any of the historical termcaps did this */
450                 if (*s == '3') {
451                     ++s;
452                     goto see03;
453                 }
454                 if (*s == '2') {
455                     ++s;
456                     goto see02;
457                 }
458                 goto invalid;
459             case '2':
460               see02:
461                 getparm(param, 1);
462                 dp = save_string(dp, "%2d");
463                 pop();
464                 break;
465             case '3':
466               see03:
467                 getparm(param, 1);
468                 dp = save_string(dp, "%3d");
469                 pop();
470                 break;
471             case 'd':
472                 getparm(param, 1);
473                 dp = save_string(dp, "%d");
474                 pop();
475                 break;
476             case 'f':
477                 param++;
478                 break;
479             case 'b':
480                 param--;
481                 break;
482             case '\\':
483                 dp = save_string(dp, "%\\");
484                 break;
485             default:
486               invalid:
487                 dp = save_char(dp, '%');
488                 s--;
489                 _nc_warning("unknown %% code %s (%#x) in %s",
490                             unctrl((chtype) *s), UChar(*s), cap);
491                 break;
492             }
493             break;
494         default:
495             dp = save_char(dp, *s++);
496             break;
497         }
498     }
499
500     /*
501      * Now, if we stripped off some leading padding, add it at the end
502      * of the string as mandatory padding.
503      */
504     if (capstart) {
505         dp = save_string(dp, "$<");
506         for (s = capstart;; s++)
507             if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
508                 dp = save_char(dp, *s);
509             else
510                 break;
511         dp = save_string(dp, "/>");
512     }
513
514     (void) save_char(dp, '\0');
515
516     DEBUG_THIS(("... _nc_captoinfo %s", NonNull(my_string)));
517
518     return (my_string);
519 }
520
521 /*
522  * Check for an expression that corresponds to "%B" (BCD):
523  *      (parameter / 10) * 16 + (parameter % 10)
524  */
525 static int
526 bcd_expression(const char *str)
527 {
528     /* leave this non-const for HPUX */
529     static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
530     int len = 0;
531     char ch1, ch2;
532
533     if (sscanf(str, fmt, &ch1, &ch2) == 2
534         && isdigit(UChar(ch1))
535         && isdigit(UChar(ch2))
536         && (ch1 == ch2)) {
537         len = 28;
538 #ifndef NDEBUG
539         {
540             char buffer[80];
541             int tst;
542             _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) fmt, ch1, ch2);
543             tst = strlen(buffer) - 1;
544             assert(len == tst);
545         }
546 #endif
547     }
548     return len;
549 }
550
551 static char *
552 save_tc_char(char *bufptr, int c1)
553 {
554     if (is7bits(c1) && isprint(c1)) {
555         if (c1 == ':' || c1 == '\\')
556             bufptr = save_char(bufptr, '\\');
557         bufptr = save_char(bufptr, c1);
558     } else {
559         char temp[80];
560
561         if (c1 == (c1 & 0x1f)) {        /* iscntrl() returns T on 255 */
562             _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
563                         "%.20s", unctrl((chtype) c1));
564         } else {
565             _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
566                         "\\%03o", c1);
567         }
568         bufptr = save_string(bufptr, temp);
569     }
570     return bufptr;
571 }
572
573 static char *
574 save_tc_inequality(char *bufptr, int c1, int c2)
575 {
576     bufptr = save_string(bufptr, "%>");
577     bufptr = save_tc_char(bufptr, c1);
578     bufptr = save_tc_char(bufptr, c2);
579     return bufptr;
580 }
581
582 /*
583  * info-to-cap --- conversion between terminfo and termcap formats
584  *
585  * Here are the capabilities infotocap assumes it can translate to:
586  *
587  *     %%       output `%'
588  *     %d       output value as in printf %d
589  *     %2       output value as in printf %2d
590  *     %3       output value as in printf %3d
591  *     %.       output value as in printf %c
592  *     %+c      add character c to value, then do %.
593  *     %>xy     if value > x then add y, no output
594  *     %r       reverse order of two parameters, no output
595  *     %i       increment by one, no output
596  *     %n       exclusive-or all parameters with 0140 (Datamedia 2500)
597  *     %B       BCD (16*(value/10)) + (value%10), no output
598  *     %D       Reverse coding (value - 2*(value%16)), no output (Delta Data).
599  *     %m       exclusive-or all parameters with 0177 (not in 4.4BSD)
600  */
601
602 #define octal_fixup(n, c) fixups[n].ch = ((fixups[n].ch << 3) | ((c) - '0'))
603
604 /*
605  * Convert a terminfo string to termcap format.  Parameters are as in
606  * _nc_captoinfo().
607  */
608 NCURSES_EXPORT(char *)
609 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
610 {
611     int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
612     const char *padding;
613     const char *trimmed = 0;
614     int in0, in1, in2;
615     char ch1 = 0, ch2 = 0;
616     char *bufptr = init_string();
617     char octal[4];
618     int len;
619     int digits;
620     bool syntax_error = FALSE;
621     int myfix = 0;
622     struct {
623         int ch;
624         int offset;
625     } fixups[MAX_TC_FIXUPS];
626
627     DEBUG_THIS(("_nc_infotocap params %d, %s", parameterized, str));
628
629     /* we may have to move some trailing mandatory padding up front */
630     padding = str + strlen(str) - 1;
631     if (padding > str && *padding == '>') {
632         if (*--padding == '/')
633             --padding;
634         while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
635             padding--;
636         if (padding > str && *padding == '<' && *--padding == '$')
637             trimmed = padding;
638         padding += 2;
639
640         while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
641             bufptr = save_char(bufptr, *padding++);
642     }
643
644     for (; !syntax_error &&
645          *str &&
646          ((trimmed == 0) || (str < trimmed)); str++) {
647         int c1, c2;
648         char *cp = 0;
649
650         if (str[0] == '^') {
651             if (str[1] == '\0' || (str + 1) == trimmed) {
652                 bufptr = save_string(bufptr, "\\136");
653                 ++str;
654             } else if (str[1] == '?') {
655                 /*
656                  * Although the 4.3BSD termcap file has an instance of "kb=^?",
657                  * that appears to be just cut/paste since neither 4.3BSD nor
658                  * 4.4BSD termcap interprets "^?" as DEL.
659                  */
660                 bufptr = save_string(bufptr, "\\177");
661                 ++str;
662             } else {
663                 bufptr = save_char(bufptr, *str++);
664                 bufptr = save_char(bufptr, *str);
665             }
666         } else if (str[0] == '\\') {
667             if (str[1] == '\0' || (str + 1) == trimmed) {
668                 bufptr = save_string(bufptr, "\\134");
669                 ++str;
670             } else if (str[1] == '^') {
671                 bufptr = save_string(bufptr, "\\136");
672                 ++str;
673             } else if (str[1] == ',') {
674                 bufptr = save_char(bufptr, *++str);
675             } else {
676                 int xx1;
677
678                 bufptr = save_char(bufptr, *str++);
679                 xx1 = *str;
680                 if (_nc_strict_bsd) {
681
682                     if (isoctal(UChar(xx1))) {
683                         int pad = 0;
684                         int xx2;
685                         int fix = 0;
686
687                         if (!isoctal(UChar(str[1])))
688                             pad = 2;
689                         else if (str[1] && !isoctal(UChar(str[2])))
690                             pad = 1;
691
692                         /*
693                          * Test for "\0", "\00" or "\000" and transform those
694                          * into "\200".
695                          */
696                         if (xx1 == '0'
697                             && ((pad == 2) || (str[1] == '0'))
698                             && ((pad >= 1) || (str[2] == '0'))) {
699                             xx2 = '2';
700                         } else {
701                             xx2 = '0';
702                             pad = 0;    /* FIXME - optionally pad to 3 digits */
703                         }
704                         if (myfix < MAX_TC_FIXUPS) {
705                             fix = 3 - pad;
706                             fixups[myfix].ch = 0;
707                             fixups[myfix].offset = (int) (bufptr
708                                                           - my_string
709                                                           - 1);
710                         }
711                         while (pad-- > 0) {
712                             bufptr = save_char(bufptr, xx2);
713                             if (myfix < MAX_TC_FIXUPS) {
714                                 fixups[myfix].ch <<= 3;
715                                 fixups[myfix].ch |= (xx2 - '0');
716                             }
717                             xx2 = '0';
718                         }
719                         if (myfix < MAX_TC_FIXUPS) {
720                             int n;
721                             for (n = 0; n < fix; ++n) {
722                                 fixups[myfix].ch <<= 3;
723                                 fixups[myfix].ch |= (str[n] - '0');
724                             }
725                             if (fixups[myfix].ch < 32) {
726                                 ++myfix;
727                             }
728                         }
729                     } else if (strchr("E\\nrtbf", xx1) == 0) {
730                         switch (xx1) {
731                         case 'e':
732                             xx1 = 'E';
733                             break;
734                         case 'l':
735                             xx1 = 'n';
736                             break;
737                         case 's':
738                             bufptr = save_char(bufptr, '0');
739                             bufptr = save_char(bufptr, '4');
740                             xx1 = '0';
741                             break;
742                         case ':':
743                             /*
744                              * Note: termcap documentation claims that ":"
745                              * must be escaped as "\072", however the
746                              * documentation is incorrect - read the code.
747                              * The replacement does not work reliably,
748                              * so the advice is not helpful.
749                              */
750                             bufptr = save_char(bufptr, '0');
751                             bufptr = save_char(bufptr, '7');
752                             xx1 = '2';
753                             break;
754                         default:
755                             /* should not happen, but handle this anyway */
756                             _nc_SPRINTF(octal, _nc_SLIMIT(sizeof(octal))
757                                         "%03o", UChar(xx1));
758                             bufptr = save_char(bufptr, octal[0]);
759                             bufptr = save_char(bufptr, octal[1]);
760                             xx1 = octal[2];
761                             break;
762                         }
763                     }
764                 } else {
765                     if (myfix < MAX_TC_FIXUPS && isoctal(UChar(xx1))) {
766                         bool will_fix = TRUE;
767                         int n;
768
769                         fixups[myfix].ch = 0;
770                         fixups[myfix].offset = (int) (bufptr - my_string - 1);
771                         for (n = 0; n < 3; ++n) {
772                             if (isoctal(str[n])) {
773                                 octal_fixup(myfix, str[n]);
774                             } else {
775                                 will_fix = FALSE;
776                                 break;
777                             }
778                         }
779                         if (will_fix && (fixups[myfix].ch < 32))
780                             ++myfix;
781                     }
782                 }
783                 bufptr = save_char(bufptr, xx1);
784             }
785         } else if (str[0] == '$' && str[1] == '<') {    /* discard padding */
786             str += 2;
787             while (isdigit(UChar(*str))
788                    || *str == '.'
789                    || *str == '*'
790                    || *str == '/'
791                    || *str == '>')
792                 str++;
793             --str;
794         } else if (sscanf(str,
795                           "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
796                           &in0, &in1, &in2) == 3
797                    && ((in0 == 4 && in1 == 10 && in2 == 48)
798                        || (in0 == 3 && in1 == 9 && in2 == 38))) {
799             /* dumb-down an optimized case from xterm-256color for termcap */
800             if ((str = strstr(str, ";m")) == 0)
801                 break;          /* cannot happen */
802             ++str;
803             if (in2 == 48) {
804                 bufptr = save_string(bufptr, "[48;5;%dm");
805             } else {
806                 bufptr = save_string(bufptr, "[38;5;%dm");
807             }
808         } else if (str[0] == '%' && str[1] == '%') {    /* escaped '%' */
809             bufptr = save_string(bufptr, "%%");
810             ++str;
811         } else if (*str != '%' || (parameterized < 1)) {
812             bufptr = save_char(bufptr, *str);
813         } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
814             str = strchr(str, ';');
815             bufptr = save_tc_inequality(bufptr, c1, c2);
816         } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
817             str = strchr(str, ';');
818             bufptr = save_tc_inequality(bufptr, c1, ch2);
819         } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
820             str = strchr(str, ';');
821             bufptr = save_tc_inequality(bufptr, ch1, c2);
822         } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
823             str = strchr(str, ';');
824             bufptr = save_tc_inequality(bufptr, ch1, ch2);
825         } else if ((len = bcd_expression(str)) != 0) {
826             str += len;
827             bufptr = save_string(bufptr, "%B");
828         } else if ((sscanf(str, "%%{%d}%%+%%%c", &c1, &ch2) == 2
829                     || sscanf(str, "%%'%c'%%+%%%c", &ch1, &ch2) == 2)
830                    && ch2 == 'c'
831                    && (cp = strchr(str, '+'))) {
832             str = cp + 2;
833             bufptr = save_string(bufptr, "%+");
834
835             if (ch1)
836                 c1 = ch1;
837             bufptr = save_tc_char(bufptr, c1);
838         }
839         /* FIXME: this "works" for 'delta' */
840         else if (strncmp(str, "%{2}%*%-", (size_t) 8) == 0) {
841             str += 7;
842             bufptr = save_string(bufptr, "%D");
843         } else if (strncmp(str, "%{96}%^", (size_t) 7) == 0) {
844             str += 6;
845             if (saw_m++ == 0) {
846                 bufptr = save_string(bufptr, "%n");
847             }
848         } else if (strncmp(str, "%{127}%^", (size_t) 8) == 0) {
849             str += 7;
850             if (saw_n++ == 0) {
851                 bufptr = save_string(bufptr, "%m");
852             }
853         } else {                /* cm-style format element */
854             str++;
855             switch (*str) {
856             case '%':
857                 bufptr = save_char(bufptr, '%');
858                 break;
859
860             case '0':
861             case '1':
862             case '2':
863             case '3':
864             case '4':
865             case '5':
866             case '6':
867             case '7':
868             case '8':
869             case '9':
870                 bufptr = save_char(bufptr, '%');
871                 ch1 = 0;
872                 ch2 = 0;
873                 digits = 0;
874                 while (isdigit(UChar(*str))) {
875                     if (++digits > 2) {
876                         syntax_error = TRUE;
877                         break;
878                     }
879                     ch2 = ch1;
880                     ch1 = *str++;
881                     if (digits == 2 && ch2 != '0') {
882                         syntax_error = TRUE;
883                         break;
884                     } else if (_nc_strict_bsd) {
885                         if (ch1 > '3') {
886                             syntax_error = TRUE;
887                             break;
888                         }
889                     } else {
890                         bufptr = save_char(bufptr, ch1);
891                     }
892                 }
893                 if (syntax_error)
894                     break;
895                 /*
896                  * Convert %02 to %2 and %03 to %3
897                  */
898                 if (ch2 == '0' && !_nc_strict_bsd) {
899                     ch2 = 0;
900                     bufptr[-2] = bufptr[-1];
901                     *--bufptr = 0;
902                 }
903                 if (_nc_strict_bsd) {
904                     if (ch2 != 0 && ch2 != '0') {
905                         syntax_error = TRUE;
906                     } else if (ch1 < '2') {
907                         ch1 = 'd';
908                     }
909                     bufptr = save_char(bufptr, ch1);
910                 }
911                 if (strchr("oxX.", *str)) {
912                     syntax_error = TRUE;        /* termcap doesn't have octal, hex */
913                 }
914                 break;
915
916             case 'd':
917                 bufptr = save_string(bufptr, "%d");
918                 break;
919
920             case 'c':
921                 bufptr = save_string(bufptr, "%.");
922                 break;
923
924                 /*
925                  * %s isn't in termcap, but it's convenient to pass it through
926                  * so we can represent things like terminfo pfkey strings in
927                  * termcap notation.
928                  */
929             case 's':
930                 if (_nc_strict_bsd) {
931                     syntax_error = TRUE;
932                 } else {
933                     bufptr = save_string(bufptr, "%s");
934                 }
935                 break;
936
937             case 'p':
938                 str++;
939                 if (*str == '1')
940                     seenone = 1;
941                 else if (*str == '2') {
942                     if (!seenone && !seentwo) {
943                         bufptr = save_string(bufptr, "%r");
944                         seentwo++;
945                     }
946                 } else if (*str >= '3') {
947                     syntax_error = TRUE;
948                 }
949                 break;
950
951             case 'i':
952                 bufptr = save_string(bufptr, "%i");
953                 break;
954
955             default:
956                 bufptr = save_char(bufptr, *str);
957                 syntax_error = TRUE;
958                 break;
959             }                   /* endswitch (*str) */
960         }                       /* endelse (*str == '%') */
961
962         /*
963          * 'str' always points to the end of what was scanned in this step,
964          * but that may not be the end of the string.
965          */
966         assert(str != 0);
967         if (str == 0 || *str == '\0')
968             break;
969
970     }                           /* endwhile (*str) */
971
972     if (!syntax_error &&
973         myfix > 0 &&
974         ((int) strlen(my_string) - (4 * myfix)) < MIN_TC_FIXUPS) {
975         while (--myfix >= 0) {
976             char *p = fixups[myfix].offset + my_string;
977             *p++ = '^';
978             *p++ = (char) (fixups[myfix].ch | '@');
979             while ((p[0] = p[2]) != '\0') {
980                 ++p;
981             }
982         }
983     }
984
985     DEBUG_THIS(("... _nc_infotocap %s",
986                 syntax_error
987                 ? "<ERR>"
988                 : _nc_visbuf(my_string)));
989
990     return (syntax_error ? NULL : my_string);
991 }
992
993 #ifdef MAIN
994
995 int curr_line;
996
997 int
998 main(int argc, char *argv[])
999 {
1000     int c, tc = FALSE;
1001
1002     while ((c = getopt(argc, argv, "c")) != EOF)
1003         switch (c) {
1004         case 'c':
1005             tc = TRUE;
1006             break;
1007         }
1008
1009     curr_line = 0;
1010     for (;;) {
1011         char buf[BUFSIZ];
1012
1013         ++curr_line;
1014         if (fgets(buf, sizeof(buf), stdin) == 0)
1015             break;
1016         buf[strlen(buf) - 1] = '\0';
1017         _nc_set_source(buf);
1018
1019         if (tc) {
1020             char *cp = _nc_infotocap("to termcap", buf, 1);
1021
1022             if (cp)
1023                 (void) fputs(cp, stdout);
1024         } else
1025             (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
1026         (void) putchar('\n');
1027     }
1028     return (0);
1029 }
1030 #endif /* MAIN */
1031
1032 #if NO_LEAKS
1033 NCURSES_EXPORT(void)
1034 _nc_captoinfo_leaks(void)
1035 {
1036     if (my_string != 0) {
1037         FreeAndNull(my_string);
1038     }
1039     my_length = 0;
1040 }
1041 #endif