jcs's openbsd hax
openbsd
1/* $OpenBSD: varargs.c,v 1.1 2010/01/12 23:22:07 nicm Exp $ */
2
3/****************************************************************************
4 * Copyright (c) 2001-2007,2008 Free Software Foundation, Inc. *
5 * *
6 * Permission is hereby granted, free of charge, to any person obtaining a *
7 * copy of this software and associated documentation files (the *
8 * "Software"), to deal in the Software without restriction, including *
9 * without limitation the rights to use, copy, modify, merge, publish, *
10 * distribute, distribute with modifications, sublicense, and/or sell *
11 * copies of the Software, and to permit persons to whom the Software is *
12 * furnished to do so, subject to the following conditions: *
13 * *
14 * The above copyright notice and this permission notice shall be included *
15 * in all copies or substantial portions of the Software. *
16 * *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * *
25 * Except as contained in this notice, the name(s) of the above copyright *
26 * holders shall not be used in advertising or otherwise to promote the *
27 * sale, use or other dealings in this Software without prior written *
28 * authorization. *
29 ****************************************************************************/
30
31/****************************************************************************
32 * Author: Thomas E. Dickey 2001 *
33 ****************************************************************************/
34
35#include <curses.priv.h>
36
37#include <ctype.h>
38
39MODULE_ID("$Id: varargs.c,v 1.1 2010/01/12 23:22:07 nicm Exp $")
40
41#ifdef TRACE
42
43#define MAX_PARMS 10
44
45typedef enum {
46 atUnknown = 0, atInteger, atFloat, atPoint, atString
47} ARGTYPE;
48
49#define VA_INT(type) ival = va_arg(ap, type)
50#define VA_FLT(type) fval = va_arg(ap, type)
51#define VA_PTR(type) pval = (char *)va_arg(ap, type)
52#define VA_STR(type) sval = va_arg(ap, type)
53
54#define MyBuffer _nc_globals.tracearg_buf
55#define MyLength _nc_globals.tracearg_used
56
57/*
58 * Returns a string that represents the parameter list of a printf-style call.
59 */
60NCURSES_EXPORT(char *)
61_nc_varargs(const char *fmt, va_list ap)
62{
63 static char dummy[] = "";
64
65 char buffer[BUFSIZ];
66 const char *param;
67 int n;
68
69 if (fmt == 0 || *fmt == '\0')
70 return dummy;
71 if (MyLength == 0)
72 MyBuffer = typeMalloc(char, MyLength = BUFSIZ);
73 if (MyBuffer == 0)
74 return dummy;
75 *MyBuffer = '\0';
76
77 while (*fmt != '\0') {
78 if (*fmt == '%') {
79 char *pval = 0; /* avoid const-cast */
80 const char *sval = "";
81 double fval = 0.0;
82 int done = FALSE;
83 int ival = 0;
84 int type = 0;
85 ARGTYPE parm[MAX_PARMS];
86 int parms = 0;
87 ARGTYPE used = atUnknown;
88
89 while (*++fmt != '\0' && !done) {
90
91 if (*fmt == '*') {
92 VA_INT(int);
93 if (parms < MAX_PARMS)
94 parm[parms++] = atInteger;
95 } else if (isalpha(UChar(*fmt))) {
96 done = TRUE;
97 switch (*fmt) {
98 case 'Z': /* FALLTHRU */
99 case 'h': /* FALLTHRU */
100 case 'l': /* FALLTHRU */
101 done = FALSE;
102 type = *fmt;
103 break;
104 case 'i': /* FALLTHRU */
105 case 'd': /* FALLTHRU */
106 case 'u': /* FALLTHRU */
107 case 'x': /* FALLTHRU */
108 case 'X': /* FALLTHRU */
109 if (type == 'l')
110 VA_INT(long);
111 else if (type == 'Z')
112 VA_INT(size_t);
113 else
114 VA_INT(int);
115 used = atInteger;
116 break;
117 case 'f': /* FALLTHRU */
118 case 'e': /* FALLTHRU */
119 case 'E': /* FALLTHRU */
120 case 'g': /* FALLTHRU */
121 case 'G': /* FALLTHRU */
122 VA_FLT(double);
123 used = atFloat;
124 break;
125 case 'c':
126 VA_INT(int);
127 used = atInteger;
128 break;
129 case 's':
130 VA_STR(const char *);
131 used = atString;
132 break;
133 case 'p':
134 VA_PTR(void *);
135 used = atPoint;
136 break;
137 case 'n':
138 VA_PTR(int *);
139 used = atPoint;
140 break;
141 default:
142 break;
143 }
144 } else if (*fmt == '%') {
145 done = TRUE;
146 }
147 if (used != atUnknown && parms < MAX_PARMS) {
148 parm[parms++] = used;
149 for (n = 0; n < parms; ++n) {
150 used = parm[n];
151 param = buffer;
152 switch (used) {
153 case atInteger:
154 snprintf(buffer, sizeof(buffer), "%d", ival);
155 break;
156 case atFloat:
157 snprintf(buffer, sizeof(buffer), "%f", fval);
158 break;
159 case atPoint:
160 snprintf(buffer, sizeof(buffer), "%p", pval);
161 break;
162 case atString:
163 param = _nc_visbuf2(1, sval);
164 break;
165 case atUnknown:
166 default:
167 strlcpy(buffer, "?", sizeof(buffer));
168 break;
169 }
170 MyLength += strlen(param) + 2;
171 MyBuffer = typeRealloc(char, MyLength, MyBuffer);
172 snprintf(MyBuffer + strlen(MyBuffer), MyLength - strlen(MyBuffer), ", %s", param);
173 }
174 }
175 used = atUnknown;
176 }
177 } else {
178 fmt++;
179 }
180 }
181
182 return (MyBuffer);
183}
184#else
185EMPTY_MODULE(_nc_varargs)
186#endif