mprintf.c 28 KB

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