]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ncurses/ncurses/trace/varargs.c
This commit was generated by cvs2svn to compensate for changes in r159952,
[FreeBSD/FreeBSD.git] / contrib / ncurses / ncurses / trace / varargs.c
1 /****************************************************************************
2  * Copyright (c) 2001 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: Thomas E. Dickey 2001                                           *
31  ****************************************************************************/
32
33 #include <curses.priv.h>
34
35 #include <ctype.h>
36
37 MODULE_ID("$Id: varargs.c,v 1.2 2002/06/01 16:16:00 tom Exp $")
38
39 #ifdef TRACE
40
41 typedef enum {
42     atUnknown = 0, atInteger, atFloat, atPoint, atString
43 } ARGTYPE;
44
45 #define VA_INT(type) ival = va_arg(ap, type)
46 #define VA_FLT(type) fval = va_arg(ap, type)
47 #define VA_PTR(type) pval = (void *)va_arg(ap, type)
48 #define VA_STR(type) sval = va_arg(ap, type)
49
50 /*
51  * Returns a string that represents the parameter list of a printf-style call.
52  */
53 NCURSES_EXPORT(char *)
54 _nc_varargs(const char *fmt, va_list ap)
55 {
56     static char dummy[] = "";
57     static char *result_buf;
58     static size_t result_len;
59
60     char buffer[BUFSIZ];
61
62     if (fmt == 0 || *fmt == '\0')
63         return dummy;
64     if (result_len == 0)
65         result_buf = typeMalloc(char, result_len = BUFSIZ);
66     if (result_buf == 0)
67         return dummy;
68     *result_buf = '\0';
69
70     while (*fmt != '\0') {
71         if (*fmt == '%') {
72             char *pval = 0;     /* avoid const-cast */
73             const char *sval = "";
74             double fval = 0.0;
75             int done = FALSE;
76             int ival = 0;
77             int type = 0;
78             ARGTYPE used = atUnknown;
79
80             while (*++fmt != '\0' && !done) {
81
82                 if (*fmt == '*') {
83                     VA_INT(int);
84                     used = atInteger;
85                     break;
86                 } else if (isalpha(UChar(*fmt))) {
87                     done = TRUE;
88                     switch (*fmt) {
89                     case 'Z':   /* FALLTHRU */
90                     case 'h':   /* FALLTHRU */
91                     case 'l':   /* FALLTHRU */
92                         done = FALSE;
93                         type = *fmt;
94                         break;
95                     case 'i':   /* FALLTHRU */
96                     case 'd':   /* FALLTHRU */
97                     case 'u':   /* FALLTHRU */
98                     case 'x':   /* FALLTHRU */
99                     case 'X':   /* FALLTHRU */
100                         if (type == 'l')
101                             VA_INT(long);
102                         else if (type == 'Z')
103                             VA_INT(size_t);
104                         else
105                             VA_INT(int);
106                         used = atInteger;
107                         break;
108                     case 'f':   /* FALLTHRU */
109                     case 'e':   /* FALLTHRU */
110                     case 'E':   /* FALLTHRU */
111                     case 'g':   /* FALLTHRU */
112                     case 'G':   /* FALLTHRU */
113                         VA_FLT(double);
114                         used = atFloat;
115                         break;
116                     case 'c':
117                         VA_INT(int);
118                         used = atInteger;
119                         break;
120                     case 's':
121                         VA_STR(const char *);
122                         used = atString;
123                         break;
124                     case 'p':
125                         VA_PTR(void *);
126                         used = atPoint;
127                         break;
128                     case 'n':
129                         VA_PTR(int *);
130                         used = atPoint;
131                         break;
132                     default:
133                         break;
134                     }
135                 } else if (*fmt == '%') {
136                     done = TRUE;
137                 }
138                 if (used != atUnknown) {
139                     const char *param = buffer;
140                     switch (used) {
141                     case atInteger:
142                         sprintf(buffer, "%d", ival);
143                         break;
144                     case atFloat:
145                         sprintf(buffer, "%f", fval);
146                         break;
147                     case atPoint:
148                         sprintf(buffer, "%p", pval);
149                         break;
150                     case atString:
151                         param = _nc_visbuf2(1, sval);
152                         break;
153                     default:
154                         strcpy(buffer, "?");
155                         break;
156                     }
157                     result_len += strlen(param) + 2;
158                     result_buf = typeRealloc(char, result_len, result_buf);
159                     sprintf(result_buf + strlen(result_buf), ",%s", param);
160                     used = atUnknown;
161                 }
162             }
163         } else {
164             fmt++;
165         }
166     }
167
168     return (result_buf);
169 }
170 #else
171 empty_module(_nc_varargs)
172 #endif