| 1 |
/* |
| 2 |
* Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved. |
| 3 |
* |
| 4 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 5 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 6 |
* |
| 7 |
* Permission is hereby granted to use or copy this program |
| 8 |
* for any purpose, provided the above notices are retained on all copies. |
| 9 |
* Permission to modify the code and to distribute modified code is granted, |
| 10 |
* provided the above notices are retained, and a notice that the code was |
| 11 |
* modified is included with the above copyright notice. |
| 12 |
*/ |
| 13 |
|
| 14 |
/* An sprintf implementation that understands cords. This is probably */ |
| 15 |
/* not terribly portable. It assumes an ANSI stdarg.h. It further */ |
| 16 |
/* assumes that I can make copies of va_list variables, and read */ |
| 17 |
/* arguments repeatedly by applying va_arg to the copies. This */ |
| 18 |
/* could be avoided at some performance cost. */ |
| 19 |
/* We also assume that unsigned and signed integers of various kinds */ |
| 20 |
/* have the same sizes, and can be cast back and forth. */ |
| 21 |
/* We assume that void * and char * have the same size. */ |
| 22 |
/* All this cruft is needed because we want to rely on the underlying */ |
| 23 |
/* sprintf implementation whenever possible. */ |
| 24 |
|
| 25 |
#ifdef HAVE_CONFIG_H |
| 26 |
# include "config.h" |
| 27 |
#endif |
| 28 |
#ifndef CORD_BUILD |
| 29 |
# define CORD_BUILD |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "cord.h" |
| 33 |
#include "ec.h" |
| 34 |
|
| 35 |
#include <stdarg.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#include "gc.h" |
| 41 |
|
| 42 |
#define CONV_SPEC_LEN 50 /* Maximum length of a single */ |
| 43 |
/* conversion specification. */ |
| 44 |
#define CONV_RESULT_LEN 50 /* Maximum length of any */ |
| 45 |
/* conversion with default */ |
| 46 |
/* width and prec. */ |
| 47 |
#if defined(CPPCHECK) |
| 48 |
# define MACRO_BLKSTMT_BEGIN { |
| 49 |
# define MACRO_BLKSTMT_END } |
| 50 |
#else |
| 51 |
# define MACRO_BLKSTMT_BEGIN do { |
| 52 |
# define MACRO_BLKSTMT_END } while (0) |
| 53 |
#endif |
| 54 |
|
| 55 |
#define OUT_OF_MEMORY MACRO_BLKSTMT_BEGIN \ |
| 56 |
if (CORD_oom_fn != 0) (*CORD_oom_fn)(); \ |
| 57 |
fprintf(stderr, "Out of memory\n"); \ |
| 58 |
abort(); \ |
| 59 |
MACRO_BLKSTMT_END |
| 60 |
|
| 61 |
static int ec_len(CORD_ec x) |
| 62 |
{ |
| 63 |
return (int)(CORD_len(x[0].ec_cord) + (x[0].ec_bufptr - x[0].ec_buf)); |
| 64 |
} |
| 65 |
|
| 66 |
/* Possible non-numeric precision values. */ |
| 67 |
# define NONE -1 |
| 68 |
# define VARIABLE -2 |
| 69 |
/* Copy the conversion specification from CORD_pos into the buffer buf */ |
| 70 |
/* Return negative on error. */ |
| 71 |
/* Source initially points one past the leading %. */ |
| 72 |
/* It is left pointing at the conversion type. */ |
| 73 |
/* Assign field width and precision to *width and *prec. */ |
| 74 |
/* If width or prec is *, VARIABLE is assigned. */ |
| 75 |
/* Set *left to 1 if left adjustment flag is present. */ |
| 76 |
/* Set *long_arg to 1 if long flag ('l' or 'L') is present, or to */ |
| 77 |
/* -1 if 'h' is present, or to 2 if 'z' is present. */ |
| 78 |
static int extract_conv_spec(CORD_pos source, char *buf, |
| 79 |
int * width, int *prec, int *left, int * long_arg) |
| 80 |
{ |
| 81 |
int result = 0; |
| 82 |
int current_number = 0; |
| 83 |
int saw_period = 0; |
| 84 |
int saw_number = 0; |
| 85 |
int chars_so_far = 0; |
| 86 |
char current; |
| 87 |
|
| 88 |
*width = NONE; |
| 89 |
buf[chars_so_far++] = '%'; |
| 90 |
while(CORD_pos_valid(source)) { |
| 91 |
if (chars_so_far >= CONV_SPEC_LEN) return(-1); |
| 92 |
current = CORD_pos_fetch(source); |
| 93 |
buf[chars_so_far++] = current; |
| 94 |
switch(current) { |
| 95 |
case '*': |
| 96 |
saw_number = 1; |
| 97 |
current_number = VARIABLE; |
| 98 |
break; |
| 99 |
case '0': |
| 100 |
if (!saw_number) { |
| 101 |
/* Zero fill flag; ignore */ |
| 102 |
break; |
| 103 |
} |
| 104 |
current_number *= 10; |
| 105 |
break; |
| 106 |
case '1': |
| 107 |
case '2': |
| 108 |
case '3': |
| 109 |
case '4': |
| 110 |
case '5': |
| 111 |
case '6': |
| 112 |
case '7': |
| 113 |
case '8': |
| 114 |
case '9': |
| 115 |
saw_number = 1; |
| 116 |
current_number *= 10; |
| 117 |
current_number += current - '0'; |
| 118 |
break; |
| 119 |
case '.': |
| 120 |
saw_period = 1; |
| 121 |
if(saw_number) { |
| 122 |
*width = current_number; |
| 123 |
saw_number = 0; |
| 124 |
} |
| 125 |
current_number = 0; |
| 126 |
break; |
| 127 |
case 'l': |
| 128 |
case 'L': |
| 129 |
*long_arg = 1; |
| 130 |
current_number = 0; |
| 131 |
break; |
| 132 |
case 'z': |
| 133 |
*long_arg = 2; |
| 134 |
current_number = 0; |
| 135 |
break; |
| 136 |
case 'h': |
| 137 |
*long_arg = -1; |
| 138 |
current_number = 0; |
| 139 |
break; |
| 140 |
case ' ': |
| 141 |
case '+': |
| 142 |
case '#': |
| 143 |
current_number = 0; |
| 144 |
break; |
| 145 |
case '-': |
| 146 |
*left = 1; |
| 147 |
current_number = 0; |
| 148 |
break; |
| 149 |
case 'd': |
| 150 |
case 'i': |
| 151 |
case 'o': |
| 152 |
case 'u': |
| 153 |
case 'x': |
| 154 |
case 'X': |
| 155 |
case 'f': |
| 156 |
case 'e': |
| 157 |
case 'E': |
| 158 |
case 'g': |
| 159 |
case 'G': |
| 160 |
case 'c': |
| 161 |
case 'C': |
| 162 |
case 's': |
| 163 |
case 'S': |
| 164 |
case 'p': |
| 165 |
case 'n': |
| 166 |
case 'r': |
| 167 |
goto done; |
| 168 |
default: |
| 169 |
return(-1); |
| 170 |
} |
| 171 |
CORD_next(source); |
| 172 |
} |
| 173 |
return(-1); |
| 174 |
done: |
| 175 |
if (saw_number) { |
| 176 |
if (saw_period) { |
| 177 |
*prec = current_number; |
| 178 |
} else { |
| 179 |
*prec = NONE; |
| 180 |
*width = current_number; |
| 181 |
} |
| 182 |
} else { |
| 183 |
*prec = NONE; |
| 184 |
} |
| 185 |
buf[chars_so_far] = '\0'; |
| 186 |
return(result); |
| 187 |
} |
| 188 |
|
| 189 |
#if defined(__DJGPP__) || defined(__STRICT_ANSI__) |
| 190 |
/* vsnprintf is missing in DJGPP (v2.0.3) */ |
| 191 |
# define GC_VSNPRINTF(buf, bufsz, format, args) vsprintf(buf, format, args) |
| 192 |
#elif defined(_MSC_VER) |
| 193 |
# if defined(_WIN32_WCE) |
| 194 |
/* _vsnprintf is deprecated in WinCE */ |
| 195 |
# define GC_VSNPRINTF StringCchVPrintfA |
| 196 |
# else |
| 197 |
# define GC_VSNPRINTF _vsnprintf |
| 198 |
# endif |
| 199 |
#else |
| 200 |
# define GC_VSNPRINTF vsnprintf |
| 201 |
#endif |
| 202 |
|
| 203 |
int CORD_vsprintf(CORD * out, CORD format, va_list args) |
| 204 |
{ |
| 205 |
CORD_ec result; |
| 206 |
int count; |
| 207 |
char current; |
| 208 |
CORD_pos pos; |
| 209 |
char conv_spec[CONV_SPEC_LEN + 1]; |
| 210 |
|
| 211 |
CORD_ec_init(result); |
| 212 |
for (CORD_set_pos(pos, format, 0); CORD_pos_valid(pos); CORD_next(pos)) { |
| 213 |
current = CORD_pos_fetch(pos); |
| 214 |
if (current == '%') { |
| 215 |
CORD_next(pos); |
| 216 |
if (!CORD_pos_valid(pos)) return(-1); |
| 217 |
current = CORD_pos_fetch(pos); |
| 218 |
if (current == '%') { |
| 219 |
CORD_ec_append(result, current); |
| 220 |
} else { |
| 221 |
int width, prec; |
| 222 |
int left_adj = 0; |
| 223 |
int long_arg = 0; |
| 224 |
CORD arg; |
| 225 |
size_t len; |
| 226 |
|
| 227 |
if (extract_conv_spec(pos, conv_spec, |
| 228 |
&width, &prec, |
| 229 |
&left_adj, &long_arg) < 0) { |
| 230 |
return(-1); |
| 231 |
} |
| 232 |
current = CORD_pos_fetch(pos); |
| 233 |
switch(current) { |
| 234 |
case 'n': |
| 235 |
/* Assign length to next arg */ |
| 236 |
if (long_arg == 0) { |
| 237 |
int * pos_ptr; |
| 238 |
pos_ptr = va_arg(args, int *); |
| 239 |
*pos_ptr = ec_len(result); |
| 240 |
} else if (long_arg == 2) { |
| 241 |
size_t * pos_ptr = va_arg(args, size_t *); |
| 242 |
*pos_ptr = (size_t)(unsigned)ec_len(result); |
| 243 |
} else if (long_arg > 0) { |
| 244 |
long * pos_ptr; |
| 245 |
pos_ptr = va_arg(args, long *); |
| 246 |
*pos_ptr = ec_len(result); |
| 247 |
} else { |
| 248 |
short * pos_ptr; |
| 249 |
pos_ptr = va_arg(args, short *); |
| 250 |
*pos_ptr = (short)ec_len(result); |
| 251 |
} |
| 252 |
goto done; |
| 253 |
case 'r': |
| 254 |
/* Append cord and any padding */ |
| 255 |
if (width == VARIABLE) width = va_arg(args, int); |
| 256 |
if (prec == VARIABLE) prec = va_arg(args, int); |
| 257 |
arg = va_arg(args, CORD); |
| 258 |
len = CORD_len(arg); |
| 259 |
if (prec != NONE && len > (unsigned)prec) { |
| 260 |
if (prec < 0) return(-1); |
| 261 |
arg = CORD_substr(arg, 0, (unsigned)prec); |
| 262 |
len = (unsigned)prec; |
| 263 |
} |
| 264 |
if (width != NONE && len < (unsigned)width) { |
| 265 |
char * blanks = (char *)GC_MALLOC_ATOMIC( |
| 266 |
(unsigned)width - len + 1); |
| 267 |
|
| 268 |
if (NULL == blanks) OUT_OF_MEMORY; |
| 269 |
memset(blanks, ' ', (unsigned)width - len); |
| 270 |
blanks[(unsigned)width - len] = '\0'; |
| 271 |
if (left_adj) { |
| 272 |
arg = CORD_cat(arg, blanks); |
| 273 |
} else { |
| 274 |
arg = CORD_cat(blanks, arg); |
| 275 |
} |
| 276 |
} |
| 277 |
CORD_ec_append_cord(result, arg); |
| 278 |
goto done; |
| 279 |
case 'c': |
| 280 |
if (width == NONE && prec == NONE) { |
| 281 |
char c; |
| 282 |
|
| 283 |
c = (char)va_arg(args, int); |
| 284 |
CORD_ec_append(result, c); |
| 285 |
goto done; |
| 286 |
} |
| 287 |
break; |
| 288 |
case 's': |
| 289 |
if (width == NONE && prec == NONE) { |
| 290 |
char * str = va_arg(args, char *); |
| 291 |
char c; |
| 292 |
|
| 293 |
while ((c = *str++) != '\0') { |
| 294 |
CORD_ec_append(result, c); |
| 295 |
} |
| 296 |
goto done; |
| 297 |
} |
| 298 |
break; |
| 299 |
default: |
| 300 |
break; |
| 301 |
} |
| 302 |
/* Use standard sprintf to perform conversion */ |
| 303 |
{ |
| 304 |
char * buf; |
| 305 |
va_list vsprintf_args; |
| 306 |
int max_size = 0; |
| 307 |
int res = 0; |
| 308 |
|
| 309 |
# if defined(CPPCHECK) |
| 310 |
va_copy(vsprintf_args, args); |
| 311 |
# elif defined(__va_copy) |
| 312 |
__va_copy(vsprintf_args, args); |
| 313 |
# elif defined(__GNUC__) && !defined(__DJGPP__) \ |
| 314 |
&& !defined(__EMX__) /* and probably in other cases */ |
| 315 |
va_copy(vsprintf_args, args); |
| 316 |
# else |
| 317 |
vsprintf_args = args; |
| 318 |
# endif |
| 319 |
if (width == VARIABLE) width = va_arg(args, int); |
| 320 |
if (prec == VARIABLE) prec = va_arg(args, int); |
| 321 |
if (width != NONE) max_size = width; |
| 322 |
if (prec != NONE && prec > max_size) max_size = prec; |
| 323 |
max_size += CONV_RESULT_LEN; |
| 324 |
if (max_size >= CORD_BUFSZ) { |
| 325 |
buf = (char *)GC_MALLOC_ATOMIC((unsigned)max_size + 1); |
| 326 |
if (NULL == buf) OUT_OF_MEMORY; |
| 327 |
} else { |
| 328 |
if (CORD_BUFSZ - (result[0].ec_bufptr-result[0].ec_buf) |
| 329 |
< max_size) { |
| 330 |
CORD_ec_flush_buf(result); |
| 331 |
} |
| 332 |
buf = result[0].ec_bufptr; |
| 333 |
} |
| 334 |
switch(current) { |
| 335 |
case 'd': |
| 336 |
case 'i': |
| 337 |
case 'o': |
| 338 |
case 'u': |
| 339 |
case 'x': |
| 340 |
case 'X': |
| 341 |
case 'c': |
| 342 |
if (long_arg <= 0) { |
| 343 |
(void) va_arg(args, int); |
| 344 |
} else if (long_arg == 2) { |
| 345 |
(void) va_arg(args, size_t); |
| 346 |
} else /* long_arg == 1 */ { |
| 347 |
(void) va_arg(args, long); |
| 348 |
} |
| 349 |
break; |
| 350 |
case 's': |
| 351 |
case 'p': |
| 352 |
(void) va_arg(args, char *); |
| 353 |
break; |
| 354 |
case 'f': |
| 355 |
case 'e': |
| 356 |
case 'E': |
| 357 |
case 'g': |
| 358 |
case 'G': |
| 359 |
(void) va_arg(args, double); |
| 360 |
break; |
| 361 |
default: |
| 362 |
res = -1; |
| 363 |
} |
| 364 |
if (0 == res) |
| 365 |
res = GC_VSNPRINTF(buf, max_size + 1, conv_spec, |
| 366 |
vsprintf_args); |
| 367 |
# if defined(CPPCHECK) || defined(__va_copy) \ |
| 368 |
|| (defined(__GNUC__) && !defined(__DJGPP__) \ |
| 369 |
&& !defined(__EMX__)) |
| 370 |
va_end(vsprintf_args); |
| 371 |
# endif |
| 372 |
len = (unsigned)res; |
| 373 |
if ((char *)(GC_word)res == buf) { |
| 374 |
/* old style vsprintf */ |
| 375 |
len = strlen(buf); |
| 376 |
} else if (res < 0) { |
| 377 |
return(-1); |
| 378 |
} |
| 379 |
if (buf != result[0].ec_bufptr) { |
| 380 |
char c; |
| 381 |
|
| 382 |
while ((c = *buf++) != '\0') { |
| 383 |
CORD_ec_append(result, c); |
| 384 |
} |
| 385 |
} else { |
| 386 |
result[0].ec_bufptr = buf + len; |
| 387 |
} |
| 388 |
} |
| 389 |
done:; |
| 390 |
} |
| 391 |
} else { |
| 392 |
CORD_ec_append(result, current); |
| 393 |
} |
| 394 |
} |
| 395 |
count = ec_len(result); |
| 396 |
*out = CORD_balance(CORD_ec_to_cord(result)); |
| 397 |
return(count); |
| 398 |
} |
| 399 |
|
| 400 |
int CORD_sprintf(CORD * out, CORD format, ...) |
| 401 |
{ |
| 402 |
va_list args; |
| 403 |
int result; |
| 404 |
|
| 405 |
va_start(args, format); |
| 406 |
result = CORD_vsprintf(out, format, args); |
| 407 |
va_end(args); |
| 408 |
return(result); |
| 409 |
} |
| 410 |
|
| 411 |
int CORD_fprintf(FILE * f, CORD format, ...) |
| 412 |
{ |
| 413 |
va_list args; |
| 414 |
int result; |
| 415 |
CORD out = CORD_EMPTY; /* initialized to prevent compiler warning */ |
| 416 |
|
| 417 |
va_start(args, format); |
| 418 |
result = CORD_vsprintf(&out, format, args); |
| 419 |
va_end(args); |
| 420 |
if (result > 0) CORD_put(out, f); |
| 421 |
return(result); |
| 422 |
} |
| 423 |
|
| 424 |
int CORD_vfprintf(FILE * f, CORD format, va_list args) |
| 425 |
{ |
| 426 |
int result; |
| 427 |
CORD out = CORD_EMPTY; |
| 428 |
|
| 429 |
result = CORD_vsprintf(&out, format, args); |
| 430 |
if (result > 0) CORD_put(out, f); |
| 431 |
return(result); |
| 432 |
} |
| 433 |
|
| 434 |
int CORD_printf(CORD format, ...) |
| 435 |
{ |
| 436 |
va_list args; |
| 437 |
int result; |
| 438 |
CORD out = CORD_EMPTY; |
| 439 |
|
| 440 |
va_start(args, format); |
| 441 |
result = CORD_vsprintf(&out, format, args); |
| 442 |
va_end(args); |
| 443 |
if (result > 0) CORD_put(out, stdout); |
| 444 |
return(result); |
| 445 |
} |
| 446 |
|
| 447 |
int CORD_vprintf(CORD format, va_list args) |
| 448 |
{ |
| 449 |
int result; |
| 450 |
CORD out = CORD_EMPTY; |
| 451 |
|
| 452 |
result = CORD_vsprintf(&out, format, args); |
| 453 |
if (result > 0) CORD_put(out, stdout); |
| 454 |
return(result); |
| 455 |
} |