mprintf.c 31 KB

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