1 /****************************************************************************
2 * Copyright 2020,2023 Thomas E. Dickey *
3 * Copyright 2001-2008,2012 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas E. Dickey 2001 *
32 ****************************************************************************/
34 #include <curses.priv.h>
38 MODULE_ID("$Id: varargs.c,v 1.13 2023/06/24 13:41:46 tom Exp $")
45 atUnknown = 0, atInteger, atFloat, atPoint, atString
48 #define VA_INT(type) ival = (int) va_arg(ap, type)
49 #define VA_FLT(type) fval = va_arg(ap, type)
50 #define VA_PTR(type) pval = (char *)va_arg(ap, type)
51 #define VA_STR(type) sval = va_arg(ap, type)
53 #define MyBuffer _nc_globals.tracearg_buf
54 #define MyLength _nc_globals.tracearg_used
57 * Returns a string that represents the parameter list of a printf-style call.
59 NCURSES_EXPORT(char *)
60 _nc_varargs(const char *fmt, va_list ap)
66 if (fmt == 0 || *fmt == '\0')
69 MyBuffer = typeMalloc(char, MyLength = BUFSIZ);
74 while (*fmt != '\0') {
76 char *pval = 0; /* avoid const-cast */
77 const char *sval = "";
82 ARGTYPE parm[MAX_PARMS];
84 ARGTYPE used = atUnknown;
86 while (*++fmt != '\0' && !done) {
90 if (parms < MAX_PARMS)
91 parm[parms++] = atInteger;
92 } else if (isalpha(UChar(*fmt))) {
95 case 'Z': /* FALLTHRU */
96 case 'h': /* FALLTHRU */
97 case 'l': /* FALLTHRU */
101 case 'i': /* FALLTHRU */
102 case 'd': /* FALLTHRU */
103 case 'u': /* FALLTHRU */
104 case 'x': /* FALLTHRU */
105 case 'X': /* FALLTHRU */
108 else if (type == 'Z')
114 case 'f': /* FALLTHRU */
115 case 'e': /* FALLTHRU */
116 case 'E': /* FALLTHRU */
117 case 'g': /* FALLTHRU */
118 case 'G': /* FALLTHRU */
127 VA_STR(const char *);
141 } else if (*fmt == '%') {
144 if (used != atUnknown && parms < MAX_PARMS) {
145 parm[parms++] = used;
146 for (n = 0; n < parms; ++n) {
151 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
155 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
159 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
163 param = _nc_visbuf2(1, sval);
167 _nc_STRCPY(buffer, "?", sizeof(buffer));
170 MyLength += strlen(param) + 2;
171 MyBuffer = typeRealloc(char, MyLength, MyBuffer);
173 _nc_SPRINTF(MyBuffer + strlen(MyBuffer),
174 _nc_SLIMIT(MyLength - strlen(MyBuffer))
186 return (MyBuffer ? MyBuffer : NULL);
189 EMPTY_MODULE(_nc_varargs)