mprintf.c 29 KB

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