mprintf.c 30 KB

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