]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/captoinfo.c
ncurses 5.9 - patch 20120622
[ncurses.git] / ncurses / tinfo / captoinfo.c
1 /****************************************************************************
2  * Copyright (c) 1998-2011,2012 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 --- conversion between termcap and terminfo formats
37  *
38  *      The captoinfo() code was swiped from Ross Ridge's mytinfo package,
39  *      adapted to fit ncurses by Eric S. Raymond <esr@snark.thyrsus.com>.
40  *
41  *      There is just one entry point:
42  *
43  *      char *_nc_captoinfo(n, s, parameterized)
44  *
45  *      Convert value s for termcap string capability named n into terminfo
46  *      format.
47  *
48  *      This code recognizes all the standard 4.4BSD %-escapes:
49  *
50  *      %%       output `%'
51  *      %d       output value as in printf %d
52  *      %2       output value as in printf %2d
53  *      %3       output value as in printf %3d
54  *      %.       output value as in printf %c
55  *      %+x      add x to value, then do %.
56  *      %>xy     if value > x then add y, no output
57  *      %r       reverse order of two parameters, no output
58  *      %i       increment by one, no output
59  *      %n       exclusive-or all parameters with 0140 (Datamedia 2500)
60  *      %B       BCD (16*(value/10)) + (value%10), no output
61  *      %D       Reverse coding (value - 2*(value%16)), no output (Delta Data).
62  *
63  *      Also, %02 and %03 are accepted as synonyms for %2 and %3.
64  *
65  *      Besides all the standard termcap escapes, this translator understands
66  *      the following extended escapes:
67  *
68  *      used by GNU Emacs termcap libraries
69  *              %a[+*-/=][cp]x  GNU arithmetic.
70  *              %m              xor the first two parameters by 0177
71  *              %b              backup to previous parameter
72  *              %f              skip this parameter
73  *
74  *      used by the University of Waterloo (MFCF) termcap libraries
75  *              %-x      subtract parameter FROM char x and output it as a char
76  *              %ax      add the character x to parameter
77  *
78  *      If #define WATERLOO is on, also enable these translations:
79  *
80  *              %sx      subtract parameter FROM the character x
81  *
82  *      By default, this Waterloo translations are not compiled in, because
83  *      the Waterloo %s conflicts with the way terminfo uses %s in strings for
84  *      function programming.
85  *
86  *      Note the two definitions of %a: the GNU definition is translated if the
87  *      characters after the 'a' are valid for it, otherwise the UW definition
88  *      is translated.
89  */
90
91 #include <curses.priv.h>
92
93 #include <ctype.h>
94 #include <tic.h>
95
96 MODULE_ID("$Id: captoinfo.c,v 1.72 2012/02/22 22:40:24 tom Exp $")
97
98 #define MAX_PUSHED      16      /* max # args we can push onto the stack */
99
100 static int stack[MAX_PUSHED];   /* the stack */
101 static int stackptr;            /* the next empty place on the stack */
102 static int onstack;             /* the top of stack */
103 static int seenm;               /* seen a %m */
104 static int seenn;               /* seen a %n */
105 static int seenr;               /* seen a %r */
106 static int param;               /* current parameter */
107 static char *dp;                /* pointer to end of the converted string */
108
109 static char *my_string;
110 static size_t my_length;
111
112 static char *
113 init_string(void)
114 /* initialize 'my_string', 'my_length' */
115 {
116     if (my_string == 0)
117         my_string = typeMalloc(char, my_length = 256);
118     if (my_string == 0)
119         _nc_err_abort(MSG_NO_MEMORY);
120
121     *my_string = '\0';
122     return my_string;
123 }
124
125 static char *
126 save_string(char *d, const char *const s)
127 {
128     size_t have = (size_t) (d - my_string);
129     size_t need = have + strlen(s) + 2;
130     if (need > my_length) {
131         my_string = (char *) _nc_doalloc(my_string, my_length = (need + need));
132         if (my_string == 0)
133             _nc_err_abort(MSG_NO_MEMORY);
134         d = my_string + have;
135     }
136     _nc_STRCPY(d, s, my_length - have);
137     return d + strlen(d);
138 }
139
140 static NCURSES_INLINE char *
141 save_char(char *s, int c)
142 {
143     static char temp[2];
144     temp[0] = (char) c;
145     return save_string(s, temp);
146 }
147
148 static void
149 push(void)
150 /* push onstack on to the stack */
151 {
152     if (stackptr >= MAX_PUSHED)
153         _nc_warning("string too complex to convert");
154     else
155         stack[stackptr++] = onstack;
156 }
157
158 static void
159 pop(void)
160 /* pop the top of the stack into onstack */
161 {
162     if (stackptr == 0) {
163         if (onstack == 0)
164             _nc_warning("I'm confused");
165         else
166             onstack = 0;
167     } else
168         onstack = stack[--stackptr];
169     param++;
170 }
171
172 static int
173 cvtchar(register const char *sp)
174 /* convert a character to a terminfo push */
175 {
176     unsigned char c = 0;
177     int len;
178
179     switch (*sp) {
180     case '\\':
181         switch (*++sp) {
182         case '\'':
183         case '$':
184         case '\\':
185         case '%':
186             c = (unsigned char) (*sp);
187             len = 2;
188             break;
189         case '\0':
190             c = '\\';
191             len = 1;
192             break;
193         case '0':
194         case '1':
195         case '2':
196         case '3':
197             len = 1;
198             while (isdigit(UChar(*sp))) {
199                 c = (unsigned char) (8 * c + (*sp++ - '0'));
200                 len++;
201             }
202             break;
203         default:
204             c = (unsigned char) (*sp);
205             len = 2;
206             break;
207         }
208         break;
209     case '^':
210         c = (unsigned char) (*++sp & 0x1f);
211         len = 2;
212         break;
213     default:
214         c = (unsigned char) (*sp);
215         len = 1;
216     }
217     if (isgraph(c) && c != ',' && c != '\'' && c != '\\' && c != ':') {
218         dp = save_string(dp, "%\'");
219         dp = save_char(dp, c);
220         dp = save_char(dp, '\'');
221     } else {
222         dp = save_string(dp, "%{");
223         if (c > 99)
224             dp = save_char(dp, c / 100 + '0');
225         if (c > 9)
226             dp = save_char(dp, ((int) (c / 10)) % 10 + '0');
227         dp = save_char(dp, c % 10 + '0');
228         dp = save_char(dp, '}');
229     }
230     return len;
231 }
232
233 static void
234 getparm(int parm, int n)
235 /* push n copies of param on the terminfo stack if not already there */
236 {
237     if (seenr) {
238         if (parm == 1)
239             parm = 2;
240         else if (parm == 2)
241             parm = 1;
242     }
243
244     while (n--) {
245         dp = save_string(dp, "%p");
246         dp = save_char(dp, '0' + parm);
247     }
248
249     if (onstack == parm) {
250         if (n > 1) {
251             _nc_warning("string may not be optimal");
252             dp = save_string(dp, "%Pa");
253             while (n--) {
254                 dp = save_string(dp, "%ga");
255             }
256         }
257         return;
258     }
259     if (onstack != 0)
260         push();
261
262     onstack = parm;
263
264     if (seenn && parm < 3) {
265         dp = save_string(dp, "%{96}%^");
266     }
267
268     if (seenm && parm < 3) {
269         dp = save_string(dp, "%{127}%^");
270     }
271 }
272
273 /*
274  * Convert a termcap string to terminfo format.
275  * 'cap' is the relevant terminfo capability index.
276  * 's' is the string value of the capability.
277  * 'parameterized' tells what type of translations to do:
278  *      % translations if 1
279  *      pad translations if >=0
280  */
281 NCURSES_EXPORT(char *)
282 _nc_captoinfo(const char *cap, const char *s, int const parameterized)
283 {
284     const char *capstart;
285
286     stackptr = 0;
287     onstack = 0;
288     seenm = 0;
289     seenn = 0;
290     seenr = 0;
291     param = 1;
292
293     dp = init_string();
294
295     /* skip the initial padding (if we haven't been told not to) */
296     capstart = 0;
297     if (s == 0)
298         s = "";
299     if (parameterized >= 0 && isdigit(UChar(*s)))
300         for (capstart = s;; s++)
301             if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
302                 break;
303
304     while (*s != '\0') {
305         switch (*s) {
306         case '%':
307             s++;
308             if (parameterized < 1) {
309                 dp = save_char(dp, '%');
310                 break;
311             }
312             switch (*s++) {
313             case '%':
314                 dp = save_char(dp, '%');
315                 break;
316             case 'r':
317                 if (seenr++ == 1) {
318                     _nc_warning("saw %%r twice in %s", cap);
319                 }
320                 break;
321             case 'm':
322                 if (seenm++ == 1) {
323                     _nc_warning("saw %%m twice in %s", cap);
324                 }
325                 break;
326             case 'n':
327                 if (seenn++ == 1) {
328                     _nc_warning("saw %%n twice in %s", cap);
329                 }
330                 break;
331             case 'i':
332                 dp = save_string(dp, "%i");
333                 break;
334             case '6':
335             case 'B':
336                 getparm(param, 1);
337                 dp = save_string(dp, "%{10}%/%{16}%*");
338                 getparm(param, 1);
339                 dp = save_string(dp, "%{10}%m%+");
340                 break;
341             case '8':
342             case 'D':
343                 getparm(param, 2);
344                 dp = save_string(dp, "%{2}%*%-");
345                 break;
346             case '>':
347                 getparm(param, 2);
348                 /* %?%{x}%>%t%{y}%+%; */
349                 dp = save_string(dp, "%?");
350                 s += cvtchar(s);
351                 dp = save_string(dp, "%>%t");
352                 s += cvtchar(s);
353                 dp = save_string(dp, "%+%;");
354                 break;
355             case 'a':
356                 if ((*s == '=' || *s == '+' || *s == '-'
357                      || *s == '*' || *s == '/')
358                     && (s[1] == 'p' || s[1] == 'c')
359                     && s[2] != '\0') {
360                     int l;
361                     l = 2;
362                     if (*s != '=')
363                         getparm(param, 1);
364                     if (s[1] == 'p') {
365                         getparm(param + s[2] - '@', 1);
366                         if (param != onstack) {
367                             pop();
368                             param--;
369                         }
370                         l++;
371                     } else
372                         l += cvtchar(s + 2);
373                     switch (*s) {
374                     case '+':
375                         dp = save_string(dp, "%+");
376                         break;
377                     case '-':
378                         dp = save_string(dp, "%-");
379                         break;
380                     case '*':
381                         dp = save_string(dp, "%*");
382                         break;
383                     case '/':
384                         dp = save_string(dp, "%/");
385                         break;
386                     case '=':
387                         if (seenr) {
388                             if (param == 1)
389                                 onstack = 2;
390                             else if (param == 2)
391                                 onstack = 1;
392                             else
393                                 onstack = param;
394                         } else
395                             onstack = param;
396                         break;
397                     }
398                     s += l;
399                     break;
400                 }
401                 getparm(param, 1);
402                 s += cvtchar(s);
403                 dp = save_string(dp, "%+");
404                 break;
405             case '+':
406                 getparm(param, 1);
407                 s += cvtchar(s);
408                 dp = save_string(dp, "%+%c");
409                 pop();
410                 break;
411             case 's':
412 #ifdef WATERLOO
413                 s += cvtchar(s);
414                 getparm(param, 1);
415                 dp = save_string(dp, "%-");
416 #else
417                 getparm(param, 1);
418                 dp = save_string(dp, "%s");
419                 pop();
420 #endif /* WATERLOO */
421                 break;
422             case '-':
423                 s += cvtchar(s);
424                 getparm(param, 1);
425                 dp = save_string(dp, "%-%c");
426                 pop();
427                 break;
428             case '.':
429                 getparm(param, 1);
430                 dp = save_string(dp, "%c");
431                 pop();
432                 break;
433             case '0':           /* not clear any of the historical termcaps did this */
434                 if (*s == '3')
435                     goto see03;
436                 else if (*s != '2')
437                     goto invalid;
438                 /* FALLTHRU */
439             case '2':
440                 getparm(param, 1);
441                 dp = save_string(dp, "%2d");
442                 pop();
443                 break;
444             case '3':
445               see03:
446                 getparm(param, 1);
447                 dp = save_string(dp, "%3d");
448                 pop();
449                 break;
450             case 'd':
451                 getparm(param, 1);
452                 dp = save_string(dp, "%d");
453                 pop();
454                 break;
455             case 'f':
456                 param++;
457                 break;
458             case 'b':
459                 param--;
460                 break;
461             case '\\':
462                 dp = save_string(dp, "%\\");
463                 break;
464             default:
465               invalid:
466                 dp = save_char(dp, '%');
467                 s--;
468                 _nc_warning("unknown %% code %s (%#x) in %s",
469                             unctrl((chtype) *s), UChar(*s), cap);
470                 break;
471             }
472             break;
473         default:
474             dp = save_char(dp, *s++);
475             break;
476         }
477     }
478
479     /*
480      * Now, if we stripped off some leading padding, add it at the end
481      * of the string as mandatory padding.
482      */
483     if (capstart) {
484         dp = save_string(dp, "$<");
485         for (s = capstart;; s++)
486             if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
487                 dp = save_char(dp, *s);
488             else
489                 break;
490         dp = save_string(dp, "/>");
491     }
492
493     (void) save_char(dp, '\0');
494     return (my_string);
495 }
496
497 /*
498  * Check for an expression that corresponds to "%B" (BCD):
499  *      (parameter / 10) * 16 + (parameter % 10)
500  */
501 static int
502 bcd_expression(const char *str)
503 {
504     /* leave this non-const for HPUX */
505     static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
506     int len = 0;
507     char ch1, ch2;
508
509     if (sscanf(str, fmt, &ch1, &ch2) == 2
510         && isdigit(UChar(ch1))
511         && isdigit(UChar(ch2))
512         && (ch1 == ch2)) {
513         len = 28;
514 #ifndef NDEBUG
515         {
516             char buffer[80];
517             int tst;
518             _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) fmt, ch1, ch2);
519             tst = strlen(buffer) - 1;
520             assert(len == tst);
521         }
522 #endif
523     }
524     return len;
525 }
526
527 static char *
528 save_tc_char(char *bufptr, int c1)
529 {
530     char temp[80];
531
532     if (is7bits(c1) && isprint(c1)) {
533         if (c1 == ':' || c1 == '\\')
534             bufptr = save_char(bufptr, '\\');
535         bufptr = save_char(bufptr, c1);
536     } else {
537         if (c1 == (c1 & 0x1f))  /* iscntrl() returns T on 255 */
538             _nc_STRCPY(temp, unctrl((chtype) c1), sizeof(temp));
539         else
540             _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", c1);
541         bufptr = save_string(bufptr, temp);
542     }
543     return bufptr;
544 }
545
546 static char *
547 save_tc_inequality(char *bufptr, int c1, int c2)
548 {
549     bufptr = save_string(bufptr, "%>");
550     bufptr = save_tc_char(bufptr, c1);
551     bufptr = save_tc_char(bufptr, c2);
552     return bufptr;
553 }
554
555 /*
556  * Here are the capabilities infotocap assumes it can translate to:
557  *
558  *     %%       output `%'
559  *     %d       output value as in printf %d
560  *     %2       output value as in printf %2d
561  *     %3       output value as in printf %3d
562  *     %.       output value as in printf %c
563  *     %+c      add character c to value, then do %.
564  *     %>xy     if value > x then add y, no output
565  *     %r       reverse order of two parameters, no output
566  *     %i       increment by one, no output
567  *     %n       exclusive-or all parameters with 0140 (Datamedia 2500)
568  *     %B       BCD (16*(value/10)) + (value%10), no output
569  *     %D       Reverse coding (value - 2*(value%16)), no output (Delta Data).
570  *     %m       exclusive-or all parameters with 0177 (not in 4.4BSD)
571  */
572
573 /*
574  * Convert a terminfo string to termcap format.  Parameters are as in
575  * _nc_captoinfo().
576  */
577 NCURSES_EXPORT(char *)
578 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
579 {
580     int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
581     const char *padding;
582     const char *trimmed = 0;
583     int in0, in1, in2;
584     char ch1 = 0, ch2 = 0;
585     char *bufptr = init_string();
586     char octal[4];
587     int len;
588     bool syntax_error = FALSE;
589
590     /* we may have to move some trailing mandatory padding up front */
591     padding = str + strlen(str) - 1;
592     if (padding > str && *padding == '>') {
593         if (*--padding == '/')
594             --padding;
595         while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
596             padding--;
597         if (padding > str && *padding == '<' && *--padding == '$')
598             trimmed = padding;
599         padding += 2;
600
601         while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
602             bufptr = save_char(bufptr, *padding++);
603     }
604
605     for (; *str && ((trimmed == 0) || (str < trimmed)); str++) {
606         int c1, c2;
607         char *cp = 0;
608
609         if (str[0] == '^') {
610             if (str[1] == '\0' || (str + 1) == trimmed) {
611                 bufptr = save_string(bufptr, "\\136");
612                 ++str;
613             } else {
614                 bufptr = save_char(bufptr, *str++);
615                 bufptr = save_char(bufptr, *str);
616             }
617         } else if (str[0] == '\\') {
618             if (str[1] == '\0' || (str + 1) == trimmed) {
619                 bufptr = save_string(bufptr, "\\134");
620                 ++str;
621             } else if (str[1] == '^') {
622                 bufptr = save_string(bufptr, "\\136");
623                 ++str;
624             } else if (str[1] == ',') {
625                 bufptr = save_char(bufptr, *++str);
626             } else {
627                 int xx1, xx2;
628
629                 bufptr = save_char(bufptr, *str++);
630                 xx1 = *str;
631                 if (_nc_strict_bsd) {
632                     if (isdigit(UChar(xx1))) {
633                         int pad = 0;
634
635                         if (!isdigit(UChar(str[1])))
636                             pad = 2;
637                         else if (str[1] && !isdigit(UChar(str[2])))
638                             pad = 1;
639
640                         /*
641                          * Test for "\0", "\00" or "\000" and transform those
642                          * into "\200".
643                          */
644                         if (xx1 == '0'
645                             && ((pad == 2) || (str[1] == '0'))
646                             && ((pad >= 1) || (str[2] == '0'))) {
647                             xx2 = '2';
648                         } else {
649                             xx2 = '0';
650                             pad = 0;    /* FIXME - optionally pad to 3 digits */
651                         }
652                         while (pad-- > 0) {
653                             bufptr = save_char(bufptr, xx2);
654                             xx2 = '0';
655                         }
656                     } else if (strchr("E\\nrtbf", xx1) == 0) {
657                         switch (xx1) {
658                         case 'e':
659                             xx1 = 'E';
660                             break;
661                         case 'l':
662                             xx1 = 'n';
663                             break;
664                         case 's':
665                             bufptr = save_char(bufptr, '0');
666                             bufptr = save_char(bufptr, '4');
667                             xx1 = '0';
668                             break;
669                         case ':':
670                             /*
671                              * Note: termcap documentation claims that ":"
672                              * must be escaped as "\072", however the
673                              * documentation is incorrect - read the code.
674                              * The replacement does not work reliably,
675                              * so the advice is not helpful.
676                              */
677                             bufptr = save_char(bufptr, '0');
678                             bufptr = save_char(bufptr, '7');
679                             xx1 = '2';
680                             break;
681                         default:
682                             /* should not happen, but handle this anyway */
683                             _nc_SPRINTF(octal, _nc_SLIMIT(sizeof(octal))
684                                         "%03o", UChar(xx1));
685                             bufptr = save_char(bufptr, octal[0]);
686                             bufptr = save_char(bufptr, octal[1]);
687                             xx1 = octal[2];
688                             break;
689                         }
690                     }
691                 }
692                 bufptr = save_char(bufptr, xx1);
693             }
694         } else if (str[0] == '$' && str[1] == '<') {    /* discard padding */
695             str += 2;
696             while (isdigit(UChar(*str))
697                    || *str == '.'
698                    || *str == '*'
699                    || *str == '/'
700                    || *str == '>')
701                 str++;
702             --str;
703         } else if (sscanf(str,
704                           "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
705                           &in0, &in1, &in2) == 3
706                    && ((in0 == 4 && in1 == 10 && in2 == 48)
707                        || (in0 == 3 && in1 == 9 && in2 == 38))) {
708             /* dumb-down an optimized case from xterm-256color for termcap */
709             str = strstr(str, ";m");
710             ++str;
711             if (in2 == 48) {
712                 bufptr = save_string(bufptr, "[48;5;%dm");
713             } else {
714                 bufptr = save_string(bufptr, "[38;5;%dm");
715             }
716         } else if (str[0] == '%' && str[1] == '%') {    /* escaped '%' */
717             bufptr = save_string(bufptr, "%%");
718             ++str;
719         } else if (*str != '%' || (parameterized < 1)) {
720             bufptr = save_char(bufptr, *str);
721         } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
722             str = strchr(str, ';');
723             bufptr = save_tc_inequality(bufptr, c1, c2);
724         } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
725             str = strchr(str, ';');
726             bufptr = save_tc_inequality(bufptr, c1, ch2);
727         } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
728             str = strchr(str, ';');
729             bufptr = save_tc_inequality(bufptr, ch1, c2);
730         } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
731             str = strchr(str, ';');
732             bufptr = save_tc_inequality(bufptr, ch1, ch2);
733         } else if ((len = bcd_expression(str)) != 0) {
734             str += len;
735             bufptr = save_string(bufptr, "%B");
736         } else if ((sscanf(str, "%%{%d}%%+%%c", &c1) == 1
737                     || sscanf(str, "%%'%c'%%+%%c", &ch1) == 1)
738                    && (cp = strchr(str, '+'))) {
739             str = cp + 2;
740             bufptr = save_string(bufptr, "%+");
741
742             if (ch1)
743                 c1 = ch1;
744             bufptr = save_tc_char(bufptr, c1);
745         }
746         /* FIXME: this "works" for 'delta' */
747         else if (strncmp(str, "%{2}%*%-", (size_t) 8) == 0) {
748             str += 7;
749             bufptr = save_string(bufptr, "%D");
750         } else if (strncmp(str, "%{96}%^", (size_t) 7) == 0) {
751             str += 6;
752             if (saw_m++ == 0) {
753                 bufptr = save_string(bufptr, "%n");
754             }
755         } else if (strncmp(str, "%{127}%^", (size_t) 8) == 0) {
756             str += 7;
757             if (saw_n++ == 0) {
758                 bufptr = save_string(bufptr, "%m");
759             }
760         } else {                /* cm-style format element */
761             str++;
762             switch (*str) {
763             case '%':
764                 bufptr = save_char(bufptr, '%');
765                 break;
766
767             case '0':
768             case '1':
769             case '2':
770             case '3':
771             case '4':
772             case '5':
773             case '6':
774             case '7':
775             case '8':
776             case '9':
777                 bufptr = save_char(bufptr, '%');
778                 ch1 = 0;
779                 ch2 = 0;
780                 while (isdigit(UChar(*str))) {
781                     ch2 = ch1;
782                     ch1 = *str++;
783                     if (_nc_strict_bsd) {
784                         if (ch1 > '3')
785                             return 0;
786                     } else {
787                         bufptr = save_char(bufptr, ch1);
788                     }
789                 }
790                 if (_nc_strict_bsd) {
791                     if (ch2 != 0 && ch2 != '0')
792                         return 0;
793                     if (ch1 < '2')
794                         ch1 = 'd';
795                     bufptr = save_char(bufptr, ch1);
796                 }
797                 if (strchr("doxX.", *str)) {
798                     if (*str != 'd')    /* termcap doesn't have octal, hex */
799                         return 0;
800                 }
801                 break;
802
803             case 'd':
804                 bufptr = save_string(bufptr, "%d");
805                 break;
806
807             case 'c':
808                 bufptr = save_string(bufptr, "%.");
809                 break;
810
811                 /*
812                  * %s isn't in termcap, but it's convenient to pass it through
813                  * so we can represent things like terminfo pfkey strings in
814                  * termcap notation.
815                  */
816             case 's':
817                 if (_nc_strict_bsd)
818                     return 0;
819                 bufptr = save_string(bufptr, "%s");
820                 break;
821
822             case 'p':
823                 str++;
824                 if (*str == '1')
825                     seenone = 1;
826                 else if (*str == '2') {
827                     if (!seenone && !seentwo) {
828                         bufptr = save_string(bufptr, "%r");
829                         seentwo++;
830                     }
831                 } else if (*str >= '3')
832                     return (0);
833                 break;
834
835             case 'i':
836                 bufptr = save_string(bufptr, "%i");
837                 break;
838
839             default:
840                 bufptr = save_char(bufptr, *str);
841                 syntax_error = TRUE;
842                 break;
843             }                   /* endswitch (*str) */
844         }                       /* endelse (*str == '%') */
845
846         /*
847          * 'str' always points to the end of what was scanned in this step,
848          * but that may not be the end of the string.
849          */
850         assert(str != 0);
851         if (*str == '\0')
852             break;
853
854     }                           /* endwhile (*str) */
855
856     return (syntax_error ? NULL : my_string);
857 }
858
859 #ifdef MAIN
860
861 int curr_line;
862
863 int
864 main(int argc, char *argv[])
865 {
866     int c, tc = FALSE;
867
868     while ((c = getopt(argc, argv, "c")) != EOF)
869         switch (c) {
870         case 'c':
871             tc = TRUE;
872             break;
873         }
874
875     curr_line = 0;
876     for (;;) {
877         char buf[BUFSIZ];
878
879         ++curr_line;
880         if (fgets(buf, sizeof(buf), stdin) == 0)
881             break;
882         buf[strlen(buf) - 1] = '\0';
883         _nc_set_source(buf);
884
885         if (tc) {
886             char *cp = _nc_infotocap("to termcap", buf, 1);
887
888             if (cp)
889                 (void) fputs(cp, stdout);
890         } else
891             (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
892         (void) putchar('\n');
893     }
894     return (0);
895 }
896 #endif /* MAIN */
897
898 #if NO_LEAKS
899 NCURSES_EXPORT(void)
900 _nc_captoinfo_leaks(void)
901 {
902     if (my_string != 0) {
903         FreeAndNull(my_string);
904     }
905     my_length = 0;
906 }
907 #endif