mprintf.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1999 - 2014, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. *
  22. * Purpose:
  23. * A merge of Bjorn Reese's format() function and Daniel's dsprintf()
  24. * 1.0. A full blooded printf() clone with full support for <num>$
  25. * everywhere (parameters, widths and precisions) including variabled
  26. * sized parameters (like doubles, long longs, long doubles and even
  27. * void * in 64-bit architectures).
  28. *
  29. * Current restrictions:
  30. * - Max 128 parameters
  31. * - No 'long double' support.
  32. *
  33. * If you ever want truly portable and good *printf() clones, the project that
  34. * took on from here is named 'Trio' and you find more details on the trio web
  35. * page at http://daniel.haxx.se/trio/
  36. */
  37. #include "curl_setup.h"
  38. #if defined(DJGPP) && (DJGPP_MINOR < 4)
  39. #undef _MPRINTF_REPLACE /* don't use x_was_used() here */
  40. #endif
  41. #include <curl/mprintf.h>
  42. #include "curl_memory.h"
  43. /* The last #include file should be: */
  44. #include "memdebug.h"
  45. /*
  46. * If SIZEOF_SIZE_T has not been defined, default to the size of long.
  47. */
  48. #ifndef SIZEOF_SIZE_T
  49. # define SIZEOF_SIZE_T CURL_SIZEOF_LONG
  50. #endif
  51. #ifdef HAVE_LONGLONG
  52. # define LONG_LONG_TYPE long long
  53. # define HAVE_LONG_LONG_TYPE
  54. #else
  55. # if defined(_MSC_VER) && (_MSC_VER >= 900) && (_INTEGRAL_MAX_BITS >= 64)
  56. # define LONG_LONG_TYPE __int64
  57. # define HAVE_LONG_LONG_TYPE
  58. # else
  59. # undef LONG_LONG_TYPE
  60. # undef HAVE_LONG_LONG_TYPE
  61. # endif
  62. #endif
  63. /*
  64. * Non-ANSI integer extensions
  65. */
  66. #if (defined(__BORLANDC__) && (__BORLANDC__ >= 0x520)) || \
  67. (defined(__WATCOMC__) && defined(__386__)) || \
  68. (defined(__POCC__) && defined(_MSC_VER)) || \
  69. (defined(_WIN32_WCE)) || \
  70. (defined(__MINGW32__)) || \
  71. (defined(_MSC_VER) && (_MSC_VER >= 900) && (_INTEGRAL_MAX_BITS >= 64))
  72. # define MP_HAVE_INT_EXTENSIONS
  73. #endif
  74. /*
  75. * Max integer data types that mprintf.c is capable
  76. */
  77. #ifdef HAVE_LONG_LONG_TYPE
  78. # define mp_intmax_t LONG_LONG_TYPE
  79. # define mp_uintmax_t unsigned LONG_LONG_TYPE
  80. #else
  81. # define mp_intmax_t long
  82. # define mp_uintmax_t unsigned long
  83. #endif
  84. #define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */
  85. #define MAX_PARAMETERS 128 /* lame static limit */
  86. #ifdef __AMIGA__
  87. # undef FORMAT_INT
  88. #endif
  89. /* Lower-case digits. */
  90. static const char lower_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  91. /* Upper-case digits. */
  92. static const char upper_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  93. #define OUTCHAR(x) \
  94. do{ \
  95. if(stream((unsigned char)(x), (FILE *)data) != -1) \
  96. done++; \
  97. else \
  98. return done; /* return immediately on failure */ \
  99. } WHILE_FALSE
  100. /* Data type to read from the arglist */
  101. typedef enum {
  102. FORMAT_UNKNOWN = 0,
  103. FORMAT_STRING,
  104. FORMAT_PTR,
  105. FORMAT_INT,
  106. FORMAT_INTPTR,
  107. FORMAT_LONG,
  108. FORMAT_LONGLONG,
  109. FORMAT_DOUBLE,
  110. FORMAT_LONGDOUBLE,
  111. FORMAT_WIDTH /* For internal use */
  112. } FormatType;
  113. /* conversion and display flags */
  114. enum {
  115. FLAGS_NEW = 0,
  116. FLAGS_SPACE = 1<<0,
  117. FLAGS_SHOWSIGN = 1<<1,
  118. FLAGS_LEFT = 1<<2,
  119. FLAGS_ALT = 1<<3,
  120. FLAGS_SHORT = 1<<4,
  121. FLAGS_LONG = 1<<5,
  122. FLAGS_LONGLONG = 1<<6,
  123. FLAGS_LONGDOUBLE = 1<<7,
  124. FLAGS_PAD_NIL = 1<<8,
  125. FLAGS_UNSIGNED = 1<<9,
  126. FLAGS_OCTAL = 1<<10,
  127. FLAGS_HEX = 1<<11,
  128. FLAGS_UPPER = 1<<12,
  129. FLAGS_WIDTH = 1<<13, /* '*' or '*<num>$' used */
  130. FLAGS_WIDTHPARAM = 1<<14, /* width PARAMETER was specified */
  131. FLAGS_PREC = 1<<15, /* precision was specified */
  132. FLAGS_PRECPARAM = 1<<16, /* precision PARAMETER was specified */
  133. FLAGS_CHAR = 1<<17, /* %c story */
  134. FLAGS_FLOATE = 1<<18, /* %e or %E */
  135. FLAGS_FLOATG = 1<<19 /* %g or %G */
  136. };
  137. typedef struct {
  138. FormatType type;
  139. int flags;
  140. long width; /* width OR width parameter number */
  141. long precision; /* precision OR precision parameter number */
  142. union {
  143. char *str;
  144. void *ptr;
  145. union {
  146. mp_intmax_t as_signed;
  147. mp_uintmax_t as_unsigned;
  148. } num;
  149. double dnum;
  150. } data;
  151. } va_stack_t;
  152. struct nsprintf {
  153. char *buffer;
  154. size_t length;
  155. size_t max;
  156. };
  157. struct asprintf {
  158. char *buffer; /* allocated buffer */
  159. size_t len; /* length of string */
  160. size_t alloc; /* length of alloc */
  161. int fail; /* (!= 0) if an alloc has failed and thus
  162. the output is not the complete data */
  163. };
  164. static long dprintf_DollarString(char *input, char **end)
  165. {
  166. int number=0;
  167. while(ISDIGIT(*input)) {
  168. number *= 10;
  169. number += *input-'0';
  170. input++;
  171. }
  172. if(number && ('$'==*input++)) {
  173. *end = input;
  174. return number;
  175. }
  176. return 0;
  177. }
  178. static bool dprintf_IsQualifierNoDollar(const char *fmt)
  179. {
  180. #if defined(MP_HAVE_INT_EXTENSIONS)
  181. if(!strncmp(fmt, "I32", 3) || !strncmp(fmt, "I64", 3)) {
  182. return TRUE;
  183. }
  184. #endif
  185. switch(*fmt) {
  186. case '-': case '+': case ' ': case '#': case '.':
  187. case '0': case '1': case '2': case '3': case '4':
  188. case '5': case '6': case '7': case '8': case '9':
  189. case 'h': case 'l': case 'L': case 'z': case 'q':
  190. case '*': case 'O':
  191. #if defined(MP_HAVE_INT_EXTENSIONS)
  192. case 'I':
  193. #endif
  194. return TRUE;
  195. default:
  196. return FALSE;
  197. }
  198. }
  199. /******************************************************************
  200. *
  201. * Pass 1:
  202. * Create an index with the type of each parameter entry and its
  203. * value (may vary in size)
  204. *
  205. ******************************************************************/
  206. static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos,
  207. va_list arglist)
  208. {
  209. char *fmt = (char *)format;
  210. int param_num = 0;
  211. long this_param;
  212. long width;
  213. long precision;
  214. int flags;
  215. long max_param=0;
  216. long i;
  217. while(*fmt) {
  218. if(*fmt++ == '%') {
  219. if(*fmt == '%') {
  220. fmt++;
  221. continue; /* while */
  222. }
  223. flags = FLAGS_NEW;
  224. /* Handle the positional case (N$) */
  225. param_num++;
  226. this_param = dprintf_DollarString(fmt, &fmt);
  227. if(0 == this_param)
  228. /* we got no positional, get the next counter */
  229. this_param = param_num;
  230. if(this_param > max_param)
  231. max_param = this_param;
  232. /*
  233. * The parameter with number 'i' should be used. Next, we need
  234. * to get SIZE and TYPE of the parameter. Add the information
  235. * to our array.
  236. */
  237. width = 0;
  238. precision = 0;
  239. /* Handle the flags */
  240. while(dprintf_IsQualifierNoDollar(fmt)) {
  241. #if defined(MP_HAVE_INT_EXTENSIONS)
  242. if(!strncmp(fmt, "I32", 3)) {
  243. flags |= FLAGS_LONG;
  244. fmt += 3;
  245. }
  246. else if(!strncmp(fmt, "I64", 3)) {
  247. flags |= FLAGS_LONGLONG;
  248. fmt += 3;
  249. }
  250. else
  251. #endif
  252. switch(*fmt++) {
  253. case ' ':
  254. flags |= FLAGS_SPACE;
  255. break;
  256. case '+':
  257. flags |= FLAGS_SHOWSIGN;
  258. break;
  259. case '-':
  260. flags |= FLAGS_LEFT;
  261. flags &= ~FLAGS_PAD_NIL;
  262. break;
  263. case '#':
  264. flags |= FLAGS_ALT;
  265. break;
  266. case '.':
  267. flags |= FLAGS_PREC;
  268. if('*' == *fmt) {
  269. /* The precision is picked from a specified parameter */
  270. flags |= FLAGS_PRECPARAM;
  271. fmt++;
  272. param_num++;
  273. i = dprintf_DollarString(fmt, &fmt);
  274. if(i)
  275. precision = i;
  276. else
  277. precision = param_num;
  278. if(precision > max_param)
  279. max_param = precision;
  280. }
  281. else {
  282. flags |= FLAGS_PREC;
  283. precision = strtol(fmt, &fmt, 10);
  284. }
  285. break;
  286. case 'h':
  287. flags |= FLAGS_SHORT;
  288. break;
  289. #if defined(MP_HAVE_INT_EXTENSIONS)
  290. case 'I':
  291. #if (CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG)
  292. flags |= FLAGS_LONGLONG;
  293. #else
  294. flags |= FLAGS_LONG;
  295. #endif
  296. break;
  297. #endif
  298. case 'l':
  299. if(flags & FLAGS_LONG)
  300. flags |= FLAGS_LONGLONG;
  301. else
  302. flags |= FLAGS_LONG;
  303. break;
  304. case 'L':
  305. flags |= FLAGS_LONGDOUBLE;
  306. break;
  307. case 'q':
  308. flags |= FLAGS_LONGLONG;
  309. break;
  310. case 'z':
  311. /* the code below generates a warning if -Wunreachable-code is
  312. used */
  313. #if (SIZEOF_SIZE_T > CURL_SIZEOF_LONG)
  314. flags |= FLAGS_LONGLONG;
  315. #else
  316. flags |= FLAGS_LONG;
  317. #endif
  318. break;
  319. case 'O':
  320. #if (CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG)
  321. flags |= FLAGS_LONGLONG;
  322. #else
  323. flags |= FLAGS_LONG;
  324. #endif
  325. break;
  326. case '0':
  327. if(!(flags & FLAGS_LEFT))
  328. flags |= FLAGS_PAD_NIL;
  329. /* FALLTHROUGH */
  330. case '1': case '2': case '3': case '4':
  331. case '5': case '6': case '7': case '8': case '9':
  332. flags |= FLAGS_WIDTH;
  333. width = strtol(fmt-1, &fmt, 10);
  334. break;
  335. case '*': /* Special case */
  336. flags |= FLAGS_WIDTHPARAM;
  337. param_num++;
  338. i = dprintf_DollarString(fmt, &fmt);
  339. if(i)
  340. width = i;
  341. else
  342. width = param_num;
  343. if(width > max_param)
  344. max_param=width;
  345. break;
  346. default:
  347. break;
  348. }
  349. } /* switch */
  350. /* Handle the specifier */
  351. i = this_param - 1;
  352. switch (*fmt) {
  353. case 'S':
  354. flags |= FLAGS_ALT;
  355. /* FALLTHROUGH */
  356. case 's':
  357. vto[i].type = FORMAT_STRING;
  358. break;
  359. case 'n':
  360. vto[i].type = FORMAT_INTPTR;
  361. break;
  362. case 'p':
  363. vto[i].type = FORMAT_PTR;
  364. break;
  365. case 'd': case 'i':
  366. vto[i].type = FORMAT_INT;
  367. break;
  368. case 'u':
  369. vto[i].type = FORMAT_INT;
  370. flags |= FLAGS_UNSIGNED;
  371. break;
  372. case 'o':
  373. vto[i].type = FORMAT_INT;
  374. flags |= FLAGS_OCTAL;
  375. break;
  376. case 'x':
  377. vto[i].type = FORMAT_INT;
  378. flags |= FLAGS_HEX|FLAGS_UNSIGNED;
  379. break;
  380. case 'X':
  381. vto[i].type = FORMAT_INT;
  382. flags |= FLAGS_HEX|FLAGS_UPPER|FLAGS_UNSIGNED;
  383. break;
  384. case 'c':
  385. vto[i].type = FORMAT_INT;
  386. flags |= FLAGS_CHAR;
  387. break;
  388. case 'f':
  389. vto[i].type = FORMAT_DOUBLE;
  390. break;
  391. case 'e':
  392. vto[i].type = FORMAT_DOUBLE;
  393. flags |= FLAGS_FLOATE;
  394. break;
  395. case 'E':
  396. vto[i].type = FORMAT_DOUBLE;
  397. flags |= FLAGS_FLOATE|FLAGS_UPPER;
  398. break;
  399. case 'g':
  400. vto[i].type = FORMAT_DOUBLE;
  401. flags |= FLAGS_FLOATG;
  402. break;
  403. case 'G':
  404. vto[i].type = FORMAT_DOUBLE;
  405. flags |= FLAGS_FLOATG|FLAGS_UPPER;
  406. break;
  407. default:
  408. vto[i].type = FORMAT_UNKNOWN;
  409. break;
  410. } /* switch */
  411. vto[i].flags = flags;
  412. vto[i].width = width;
  413. vto[i].precision = precision;
  414. if(flags & FLAGS_WIDTHPARAM) {
  415. /* we have the width specified from a parameter, so we make that
  416. parameter's info setup properly */
  417. vto[i].width = width - 1;
  418. i = width - 1;
  419. vto[i].type = FORMAT_WIDTH;
  420. vto[i].flags = FLAGS_NEW;
  421. vto[i].precision = vto[i].width = 0; /* can't use width or precision
  422. of width! */
  423. }
  424. if(flags & FLAGS_PRECPARAM) {
  425. /* we have the precision specified from a parameter, so we make that
  426. parameter's info setup properly */
  427. vto[i].precision = precision - 1;
  428. i = precision - 1;
  429. vto[i].type = FORMAT_WIDTH;
  430. vto[i].flags = FLAGS_NEW;
  431. vto[i].precision = vto[i].width = 0; /* can't use width or precision
  432. of width! */
  433. }
  434. *endpos++ = fmt + 1; /* end of this sequence */
  435. }
  436. }
  437. /* Read the arg list parameters into our data list */
  438. for(i=0; i<max_param; i++) {
  439. if((i + 1 < max_param) && (vto[i + 1].type == FORMAT_WIDTH)) {
  440. /* Width/precision arguments must be read before the main argument
  441. * they are attached to
  442. */
  443. vto[i + 1].data.num.as_signed = (mp_intmax_t)va_arg(arglist, int);
  444. }
  445. switch (vto[i].type) {
  446. case FORMAT_STRING:
  447. vto[i].data.str = va_arg(arglist, char *);
  448. break;
  449. case FORMAT_INTPTR:
  450. case FORMAT_UNKNOWN:
  451. case FORMAT_PTR:
  452. vto[i].data.ptr = va_arg(arglist, void *);
  453. break;
  454. case FORMAT_INT:
  455. #ifdef HAVE_LONG_LONG_TYPE
  456. if((vto[i].flags & FLAGS_LONGLONG) && (vto[i].flags & FLAGS_UNSIGNED))
  457. vto[i].data.num.as_unsigned =
  458. (mp_uintmax_t)va_arg(arglist, mp_uintmax_t);
  459. else if(vto[i].flags & FLAGS_LONGLONG)
  460. vto[i].data.num.as_signed =
  461. (mp_intmax_t)va_arg(arglist, mp_intmax_t);
  462. else
  463. #endif
  464. {
  465. if((vto[i].flags & FLAGS_LONG) && (vto[i].flags & FLAGS_UNSIGNED))
  466. vto[i].data.num.as_unsigned =
  467. (mp_uintmax_t)va_arg(arglist, unsigned long);
  468. else if(vto[i].flags & FLAGS_LONG)
  469. vto[i].data.num.as_signed =
  470. (mp_intmax_t)va_arg(arglist, long);
  471. else if(vto[i].flags & FLAGS_UNSIGNED)
  472. vto[i].data.num.as_unsigned =
  473. (mp_uintmax_t)va_arg(arglist, unsigned int);
  474. else
  475. vto[i].data.num.as_signed =
  476. (mp_intmax_t)va_arg(arglist, int);
  477. }
  478. break;
  479. case FORMAT_DOUBLE:
  480. vto[i].data.dnum = va_arg(arglist, double);
  481. break;
  482. case FORMAT_WIDTH:
  483. /* Argument has been read. Silently convert it into an integer
  484. * for later use
  485. */
  486. vto[i].type = FORMAT_INT;
  487. break;
  488. default:
  489. break;
  490. }
  491. }
  492. return max_param;
  493. }
  494. static int dprintf_formatf(
  495. void *data, /* untouched by format(), just sent to the stream() function in
  496. the second argument */
  497. /* function pointer called for each output character */
  498. int (*stream)(int, FILE *),
  499. const char *format, /* %-formatted string */
  500. va_list ap_save) /* list of parameters */
  501. {
  502. /* Base-36 digits for numbers. */
  503. const char *digits = lower_digits;
  504. /* Pointer into the format string. */
  505. char *f;
  506. /* Number of characters written. */
  507. int done = 0;
  508. long param; /* current parameter to read */
  509. long param_num=0; /* parameter counter */
  510. va_stack_t vto[MAX_PARAMETERS];
  511. char *endpos[MAX_PARAMETERS];
  512. char **end;
  513. char work[BUFFSIZE];
  514. va_stack_t *p;
  515. /* Do the actual %-code parsing */
  516. dprintf_Pass1(format, vto, endpos, ap_save);
  517. end = &endpos[0]; /* the initial end-position from the list dprintf_Pass1()
  518. created for us */
  519. f = (char *)format;
  520. while(*f != '\0') {
  521. /* Format spec modifiers. */
  522. int is_alt;
  523. /* Width of a field. */
  524. long width;
  525. /* Precision of a field. */
  526. long prec;
  527. /* Decimal integer is negative. */
  528. int is_neg;
  529. /* Base of a number to be written. */
  530. long base;
  531. /* Integral values to be written. */
  532. mp_uintmax_t num;
  533. /* Used to convert negative in positive. */
  534. mp_intmax_t signed_num;
  535. if(*f != '%') {
  536. /* This isn't a format spec, so write everything out until the next one
  537. OR end of string is reached. */
  538. do {
  539. OUTCHAR(*f);
  540. } while(*++f && ('%' != *f));
  541. continue;
  542. }
  543. ++f;
  544. /* Check for "%%". Note that although the ANSI standard lists
  545. '%' as a conversion specifier, it says "The complete format
  546. specification shall be `%%'," so we can avoid all the width
  547. and precision processing. */
  548. if(*f == '%') {
  549. ++f;
  550. OUTCHAR('%');
  551. continue;
  552. }
  553. /* If this is a positional parameter, the position must follow immediately
  554. after the %, thus create a %<num>$ sequence */
  555. param=dprintf_DollarString(f, &f);
  556. if(!param)
  557. param = param_num;
  558. else
  559. --param;
  560. param_num++; /* increase this always to allow "%2$s %1$s %s" and then the
  561. third %s will pick the 3rd argument */
  562. p = &vto[param];
  563. /* pick up the specified width */
  564. if(p->flags & FLAGS_WIDTHPARAM)
  565. width = (long)vto[p->width].data.num.as_signed;
  566. else
  567. width = p->width;
  568. /* pick up the specified precision */
  569. if(p->flags & FLAGS_PRECPARAM) {
  570. prec = (long)vto[p->precision].data.num.as_signed;
  571. param_num++; /* since the precision is extraced from a parameter, we
  572. must skip that to get to the next one properly */
  573. }
  574. else if(p->flags & FLAGS_PREC)
  575. prec = p->precision;
  576. else
  577. prec = -1;
  578. is_alt = (p->flags & FLAGS_ALT) ? 1 : 0;
  579. switch (p->type) {
  580. case FORMAT_INT:
  581. num = p->data.num.as_unsigned;
  582. if(p->flags & FLAGS_CHAR) {
  583. /* Character. */
  584. if(!(p->flags & FLAGS_LEFT))
  585. while(--width > 0)
  586. OUTCHAR(' ');
  587. OUTCHAR((char) num);
  588. if(p->flags & FLAGS_LEFT)
  589. while(--width > 0)
  590. OUTCHAR(' ');
  591. break;
  592. }
  593. if(p->flags & FLAGS_OCTAL) {
  594. /* Octal unsigned integer. */
  595. base = 8;
  596. goto unsigned_number;
  597. }
  598. else if(p->flags & FLAGS_HEX) {
  599. /* Hexadecimal unsigned integer. */
  600. digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits;
  601. base = 16;
  602. goto unsigned_number;
  603. }
  604. else if(p->flags & FLAGS_UNSIGNED) {
  605. /* Decimal unsigned integer. */
  606. base = 10;
  607. goto unsigned_number;
  608. }
  609. /* Decimal integer. */
  610. base = 10;
  611. is_neg = (p->data.num.as_signed < (mp_intmax_t)0) ? 1 : 0;
  612. if(is_neg) {
  613. /* signed_num might fail to hold absolute negative minimum by 1 */
  614. signed_num = p->data.num.as_signed + (mp_intmax_t)1;
  615. signed_num = -signed_num;
  616. num = (mp_uintmax_t)signed_num;
  617. num += (mp_uintmax_t)1;
  618. }
  619. goto number;
  620. unsigned_number:
  621. /* Unsigned number of base BASE. */
  622. is_neg = 0;
  623. number:
  624. /* Number of base BASE. */
  625. {
  626. char *workend = &work[sizeof(work) - 1];
  627. char *w;
  628. /* Supply a default precision if none was given. */
  629. if(prec == -1)
  630. prec = 1;
  631. /* Put the number in WORK. */
  632. w = workend;
  633. while(num > 0) {
  634. *w-- = digits[num % base];
  635. num /= base;
  636. }
  637. width -= (long)(workend - w);
  638. prec -= (long)(workend - w);
  639. if(is_alt && base == 8 && prec <= 0) {
  640. *w-- = '0';
  641. --width;
  642. }
  643. if(prec > 0) {
  644. width -= prec;
  645. while(prec-- > 0)
  646. *w-- = '0';
  647. }
  648. if(is_alt && base == 16)
  649. width -= 2;
  650. if(is_neg || (p->flags & FLAGS_SHOWSIGN) || (p->flags & FLAGS_SPACE))
  651. --width;
  652. if(!(p->flags & FLAGS_LEFT) && !(p->flags & FLAGS_PAD_NIL))
  653. while(width-- > 0)
  654. OUTCHAR(' ');
  655. if(is_neg)
  656. OUTCHAR('-');
  657. else if(p->flags & FLAGS_SHOWSIGN)
  658. OUTCHAR('+');
  659. else if(p->flags & FLAGS_SPACE)
  660. OUTCHAR(' ');
  661. if(is_alt && base == 16) {
  662. OUTCHAR('0');
  663. if(p->flags & FLAGS_UPPER)
  664. OUTCHAR('X');
  665. else
  666. OUTCHAR('x');
  667. }
  668. if(!(p->flags & FLAGS_LEFT) && (p->flags & FLAGS_PAD_NIL))
  669. while(width-- > 0)
  670. OUTCHAR('0');
  671. /* Write the number. */
  672. while(++w <= workend) {
  673. OUTCHAR(*w);
  674. }
  675. if(p->flags & FLAGS_LEFT)
  676. while(width-- > 0)
  677. OUTCHAR(' ');
  678. }
  679. break;
  680. case FORMAT_STRING:
  681. /* String. */
  682. {
  683. static const char null[] = "(nil)";
  684. const char *str;
  685. size_t len;
  686. str = (char *) p->data.str;
  687. if(str == NULL) {
  688. /* Write null[] if there's space. */
  689. if(prec == -1 || prec >= (long) sizeof(null) - 1) {
  690. str = null;
  691. len = sizeof(null) - 1;
  692. /* Disable quotes around (nil) */
  693. p->flags &= (~FLAGS_ALT);
  694. }
  695. else {
  696. str = "";
  697. len = 0;
  698. }
  699. }
  700. else if(prec != -1)
  701. len = (size_t)prec;
  702. else
  703. len = strlen(str);
  704. width -= (long)len;
  705. if(p->flags & FLAGS_ALT)
  706. OUTCHAR('"');
  707. if(!(p->flags&FLAGS_LEFT))
  708. while(width-- > 0)
  709. OUTCHAR(' ');
  710. while((len-- > 0) && *str)
  711. OUTCHAR(*str++);
  712. if(p->flags&FLAGS_LEFT)
  713. while(width-- > 0)
  714. OUTCHAR(' ');
  715. if(p->flags & FLAGS_ALT)
  716. OUTCHAR('"');
  717. }
  718. break;
  719. case FORMAT_PTR:
  720. /* Generic pointer. */
  721. {
  722. void *ptr;
  723. ptr = (void *) p->data.ptr;
  724. if(ptr != NULL) {
  725. /* If the pointer is not NULL, write it as a %#x spec. */
  726. base = 16;
  727. digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits;
  728. is_alt = 1;
  729. num = (size_t) ptr;
  730. is_neg = 0;
  731. goto number;
  732. }
  733. else {
  734. /* Write "(nil)" for a nil pointer. */
  735. static const char strnil[] = "(nil)";
  736. const char *point;
  737. width -= (long)(sizeof(strnil) - 1);
  738. if(p->flags & FLAGS_LEFT)
  739. while(width-- > 0)
  740. OUTCHAR(' ');
  741. for(point = strnil; *point != '\0'; ++point)
  742. OUTCHAR(*point);
  743. if(! (p->flags & FLAGS_LEFT))
  744. while(width-- > 0)
  745. OUTCHAR(' ');
  746. }
  747. }
  748. break;
  749. case FORMAT_DOUBLE:
  750. {
  751. char formatbuf[32]="%";
  752. char *fptr = &formatbuf[1];
  753. size_t left = sizeof(formatbuf)-strlen(formatbuf);
  754. int len;
  755. width = -1;
  756. if(p->flags & FLAGS_WIDTH)
  757. width = p->width;
  758. else if(p->flags & FLAGS_WIDTHPARAM)
  759. width = (long)vto[p->width].data.num.as_signed;
  760. prec = -1;
  761. if(p->flags & FLAGS_PREC)
  762. prec = p->precision;
  763. else if(p->flags & FLAGS_PRECPARAM)
  764. prec = (long)vto[p->precision].data.num.as_signed;
  765. if(p->flags & FLAGS_LEFT)
  766. *fptr++ = '-';
  767. if(p->flags & FLAGS_SHOWSIGN)
  768. *fptr++ = '+';
  769. if(p->flags & FLAGS_SPACE)
  770. *fptr++ = ' ';
  771. if(p->flags & FLAGS_ALT)
  772. *fptr++ = '#';
  773. *fptr = 0;
  774. if(width >= 0) {
  775. /* RECURSIVE USAGE */
  776. len = curl_msnprintf(fptr, left, "%ld", width);
  777. fptr += len;
  778. left -= len;
  779. }
  780. if(prec >= 0) {
  781. /* RECURSIVE USAGE */
  782. len = curl_msnprintf(fptr, left, ".%ld", prec);
  783. fptr += len;
  784. }
  785. if(p->flags & FLAGS_LONG)
  786. *fptr++ = 'l';
  787. if(p->flags & FLAGS_FLOATE)
  788. *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'E':'e');
  789. else if(p->flags & FLAGS_FLOATG)
  790. *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'G' : 'g');
  791. else
  792. *fptr++ = 'f';
  793. *fptr = 0; /* and a final zero termination */
  794. /* NOTE NOTE NOTE!! Not all sprintf implementations return number of
  795. output characters */
  796. (sprintf)(work, formatbuf, p->data.dnum);
  797. for(fptr=work; *fptr; fptr++)
  798. OUTCHAR(*fptr);
  799. }
  800. break;
  801. case FORMAT_INTPTR:
  802. /* Answer the count of characters written. */
  803. #ifdef HAVE_LONG_LONG_TYPE
  804. if(p->flags & FLAGS_LONGLONG)
  805. *(LONG_LONG_TYPE *) p->data.ptr = (LONG_LONG_TYPE)done;
  806. else
  807. #endif
  808. if(p->flags & FLAGS_LONG)
  809. *(long *) p->data.ptr = (long)done;
  810. else if(!(p->flags & FLAGS_SHORT))
  811. *(int *) p->data.ptr = (int)done;
  812. else
  813. *(short *) p->data.ptr = (short)done;
  814. break;
  815. default:
  816. break;
  817. }
  818. f = *end++; /* goto end of %-code */
  819. }
  820. return done;
  821. }
  822. /* fputc() look-alike */
  823. static int addbyter(int output, FILE *data)
  824. {
  825. struct nsprintf *infop=(struct nsprintf *)data;
  826. unsigned char outc = (unsigned char)output;
  827. if(infop->length < infop->max) {
  828. /* only do this if we haven't reached max length yet */
  829. infop->buffer[0] = outc; /* store */
  830. infop->buffer++; /* increase pointer */
  831. infop->length++; /* we are now one byte larger */
  832. return outc; /* fputc() returns like this on success */
  833. }
  834. return -1;
  835. }
  836. int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format,
  837. va_list ap_save)
  838. {
  839. int retcode;
  840. struct nsprintf info;
  841. info.buffer = buffer;
  842. info.length = 0;
  843. info.max = maxlength;
  844. retcode = dprintf_formatf(&info, addbyter, format, ap_save);
  845. if(info.max) {
  846. /* we terminate this with a zero byte */
  847. if(info.max == info.length)
  848. /* we're at maximum, scrap the last letter */
  849. info.buffer[-1] = 0;
  850. else
  851. info.buffer[0] = 0;
  852. }
  853. return retcode;
  854. }
  855. int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...)
  856. {
  857. int retcode;
  858. va_list ap_save; /* argument pointer */
  859. va_start(ap_save, format);
  860. retcode = curl_mvsnprintf(buffer, maxlength, format, ap_save);
  861. va_end(ap_save);
  862. return retcode;
  863. }
  864. /* fputc() look-alike */
  865. static int alloc_addbyter(int output, FILE *data)
  866. {
  867. struct asprintf *infop=(struct asprintf *)data;
  868. unsigned char outc = (unsigned char)output;
  869. if(!infop->buffer) {
  870. infop->buffer = malloc(32);
  871. if(!infop->buffer) {
  872. infop->fail = 1;
  873. return -1; /* fail */
  874. }
  875. infop->alloc = 32;
  876. infop->len =0;
  877. }
  878. else if(infop->len+1 >= infop->alloc) {
  879. char *newptr;
  880. newptr = realloc(infop->buffer, infop->alloc*2);
  881. if(!newptr) {
  882. infop->fail = 1;
  883. return -1; /* fail */
  884. }
  885. infop->buffer = newptr;
  886. infop->alloc *= 2;
  887. }
  888. infop->buffer[ infop->len ] = outc;
  889. infop->len++;
  890. return outc; /* fputc() returns like this on success */
  891. }
  892. char *curl_maprintf(const char *format, ...)
  893. {
  894. va_list ap_save; /* argument pointer */
  895. int retcode;
  896. struct asprintf info;
  897. info.buffer = NULL;
  898. info.len = 0;
  899. info.alloc = 0;
  900. info.fail = 0;
  901. va_start(ap_save, format);
  902. retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save);
  903. va_end(ap_save);
  904. if((-1 == retcode) || info.fail) {
  905. if(info.alloc)
  906. free(info.buffer);
  907. return NULL;
  908. }
  909. if(info.alloc) {
  910. info.buffer[info.len] = 0; /* we terminate this with a zero byte */
  911. return info.buffer;
  912. }
  913. else
  914. return strdup("");
  915. }
  916. char *curl_mvaprintf(const char *format, va_list ap_save)
  917. {
  918. int retcode;
  919. struct asprintf info;
  920. info.buffer = NULL;
  921. info.len = 0;
  922. info.alloc = 0;
  923. info.fail = 0;
  924. retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save);
  925. if((-1 == retcode) || info.fail) {
  926. if(info.alloc)
  927. free(info.buffer);
  928. return NULL;
  929. }
  930. if(info.alloc) {
  931. info.buffer[info.len] = 0; /* we terminate this with a zero byte */
  932. return info.buffer;
  933. }
  934. else
  935. return strdup("");
  936. }
  937. static int storebuffer(int output, FILE *data)
  938. {
  939. char **buffer = (char **)data;
  940. unsigned char outc = (unsigned char)output;
  941. **buffer = outc;
  942. (*buffer)++;
  943. return outc; /* act like fputc() ! */
  944. }
  945. int curl_msprintf(char *buffer, const char *format, ...)
  946. {
  947. va_list ap_save; /* argument pointer */
  948. int retcode;
  949. va_start(ap_save, format);
  950. retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save);
  951. va_end(ap_save);
  952. *buffer=0; /* we terminate this with a zero byte */
  953. return retcode;
  954. }
  955. int curl_mprintf(const char *format, ...)
  956. {
  957. int retcode;
  958. va_list ap_save; /* argument pointer */
  959. va_start(ap_save, format);
  960. retcode = dprintf_formatf(stdout, fputc, format, ap_save);
  961. va_end(ap_save);
  962. return retcode;
  963. }
  964. int curl_mfprintf(FILE *whereto, const char *format, ...)
  965. {
  966. int retcode;
  967. va_list ap_save; /* argument pointer */
  968. va_start(ap_save, format);
  969. retcode = dprintf_formatf(whereto, fputc, format, ap_save);
  970. va_end(ap_save);
  971. return retcode;
  972. }
  973. int curl_mvsprintf(char *buffer, const char *format, va_list ap_save)
  974. {
  975. int retcode;
  976. retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save);
  977. *buffer=0; /* we terminate this with a zero byte */
  978. return retcode;
  979. }
  980. int curl_mvprintf(const char *format, va_list ap_save)
  981. {
  982. return dprintf_formatf(stdout, fputc, format, ap_save);
  983. }
  984. int curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save)
  985. {
  986. return dprintf_formatf(whereto, fputc, format, ap_save);
  987. }