mprintf.c 31 KB

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