ncurses 4.2
[ncurses.git] / ncurses / safe_sprintf.c
1 /****************************************************************************
2  * Copyright (c) 1998 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 <dickey@clark.net> 1997                        *
31  ****************************************************************************/
32
33 #include <curses.priv.h>
34 #include <ctype.h>
35
36 MODULE_ID("$Id: safe_sprintf.c,v 1.5 1998/02/11 12:13:57 tom Exp $")
37
38 #if USE_SAFE_SPRINTF
39
40 typedef enum { Flags, Width, Prec, Type, Format } PRINTF;
41
42 #define VA_INTGR(type) ival = va_arg(ap, type)
43 #define VA_FLOAT(type) fval = va_arg(ap, type)
44 #define VA_POINT(type) pval = (void *)va_arg(ap, type)
45
46 /*
47  * Scan a variable-argument list for printf to determine the number of
48  * characters that would be emitted.
49  */
50 static int
51 _nc_printf_length(const char *fmt, va_list ap)
52 {
53         size_t length = BUFSIZ;
54         char *buffer;
55         char *format;
56         int len = 0;
57
58         if (fmt == 0 || *fmt == '\0')
59                 return -1;
60         if ((format = malloc(strlen(fmt)+1)) == 0)
61                 return -1;
62         if ((buffer = malloc(length)) == 0) {
63                 free(format);
64                 return -1;
65         }
66
67         while (*fmt != '\0') {
68                 if (*fmt == '%') {
69                         PRINTF state = Flags;
70                         char *pval   = "";
71                         double fval  = 0.0;
72                         int done     = FALSE;
73                         int ival     = 0;
74                         int prec     = -1;
75                         int type     = 0;
76                         int used     = 0;
77                         int width    = -1;
78                         size_t f     = 0;
79
80                         format[f++] = *fmt;
81                         while (*++fmt != '\0' && len >= 0 && !done) {
82                                 format[f++] = *fmt;
83
84                                 if (isdigit(*fmt)) {
85                                         int num = *fmt - '0';
86                                         if (state == Flags && num != 0)
87                                                 state = Width;
88                                         if (state == Width) {
89                                                 if (width < 0)
90                                                         width = 0;
91                                                 width = (width * 10) + num;
92                                         } else if (state == Prec) {
93                                                 if (prec < 0)
94                                                         prec = 0;
95                                                 prec = (prec * 10) + num;
96                                         }
97                                 } else if (*fmt == '*') {
98                                         VA_INTGR(int);
99                                         if (state == Flags)
100                                                 state = Width;
101                                         if (state == Width) {
102                                                 width = ival;
103                                         } else if (state == Prec) {
104                                                 prec = ival;
105                                         }
106                                         sprintf(&format[--f], "%d", ival);
107                                         f = strlen(format);
108                                 } else if (isalpha(*fmt)) {
109                                         done = TRUE;
110                                         switch (*fmt) {
111                                         case 'Z': /* FALLTHRU */
112                                         case 'h': /* FALLTHRU */
113                                         case 'l': /* FALLTHRU */
114                                         case 'L': /* FALLTHRU */
115                                                 done = FALSE;
116                                                 type = *fmt;
117                                                 break;
118                                         case 'i': /* FALLTHRU */
119                                         case 'd': /* FALLTHRU */
120                                         case 'u': /* FALLTHRU */
121                                         case 'x': /* FALLTHRU */
122                                         case 'X': /* FALLTHRU */
123                                                 if (type == 'l')
124                                                         VA_INTGR(long);
125                                                 else if (type == 'Z')
126                                                         VA_INTGR(size_t);
127                                                 else
128                                                         VA_INTGR(int);
129                                                 used = 'i';
130                                                 break;
131                                         case 'f': /* FALLTHRU */
132                                         case 'e': /* FALLTHRU */
133                                         case 'E': /* FALLTHRU */
134                                         case 'g': /* FALLTHRU */
135                                         case 'G': /* FALLTHRU */
136                                                 if (type == 'L')
137                                                         VA_FLOAT(long double);
138                                                 else
139                                                         VA_FLOAT(double);
140                                                 used = 'f';
141                                                 break;
142                                         case 'c':
143                                                 VA_INTGR(int);
144                                                 used = 'i';
145                                                 break;
146                                         case 's':
147                                                 VA_POINT(char *);
148                                                 if (prec < 0)
149                                                         prec = strlen(pval);
150                                                 if (prec > (int)length) {
151                                                         length = length + prec;
152                                                         buffer = realloc(buffer, length);
153                                                         if (buffer == 0) {
154                                                                 free(format);
155                                                                 return -1;
156                                                         }
157                                                 }
158                                                 used = 'p';
159                                                 break;
160                                         case 'p':
161                                                 VA_POINT(void *);
162                                                 used = 'p';
163                                                 break;
164                                         case 'n':
165                                                 VA_POINT(int *);
166                                                 used = 0;
167                                                 break;
168                                         default:
169                                                 break;
170                                         }
171                                 } else if (*fmt == '.') {
172                                         state = Prec;
173                                 } else if (*fmt == '%') {
174                                         done = TRUE;
175                                         used = 'p';
176                                 }
177                         }
178                         format[f] = '\0';
179                         switch (used) {
180                         case 'i':
181                                 sprintf(buffer, format, ival);
182                                 break;
183                         case 'f':
184                                 sprintf(buffer, format, fval);
185                                 break;
186                         default:
187                                 sprintf(buffer, format, pval);
188                                 break;
189                         }
190                         len += (int)strlen(buffer);
191                 } else {
192                         fmt++;
193                         len++;
194                 }
195         }
196
197         free(buffer);
198         free(format);
199         return len;
200 }
201 #endif
202
203 /*
204  * Wrapper for vsprintf that allocates a buffer big enough to hold the result.
205  */
206 char *
207 _nc_printf_string(const char *fmt, va_list ap)
208 {
209 #if USE_SAFE_SPRINTF
210         char *buf = 0;
211         int len = _nc_printf_length(fmt, ap);
212
213         if (len > 0) {
214                 buf = malloc(len+1);
215                 vsprintf(buf, fmt, ap);
216         }
217 #else
218         static int rows, cols;
219         static char *buf;
220         static size_t len;
221
222         if (screen_lines > rows || screen_columns > cols) {
223                 if (screen_lines   > rows) rows = screen_lines;
224                 if (screen_columns > cols) cols = screen_columns;
225                 len = (rows * (cols + 1)) + 1;
226                 if (buf == 0)
227                         buf = malloc(len);
228                 else
229                         buf = realloc(buf, len);
230         }
231
232         if (buf != 0) {
233 # if HAVE_VSNPRINTF
234                 vsnprintf(buf, len, fmt, ap);   /* GNU extension */
235 # else
236                 vsprintf(buf, fmt, ap);         /* ANSI */
237 # endif
238 #endif
239         }
240         return buf;
241 }