xref: /illumos-gate/usr/src/lib/libsqlite/src/printf.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate ** The "printf" code that follows dates from the 1980's.  It is in
37c478bd9Sstevel@tonic-gate ** the public domain.  The original comments are included here for
47c478bd9Sstevel@tonic-gate ** completeness.  They are very out-of-date but might be useful as
57c478bd9Sstevel@tonic-gate ** an historical reference.  Most of the "enhancements" have been backed
67c478bd9Sstevel@tonic-gate ** out so that the functionality is now the same as standard printf().
77c478bd9Sstevel@tonic-gate **
87c478bd9Sstevel@tonic-gate **************************************************************************
97c478bd9Sstevel@tonic-gate **
107c478bd9Sstevel@tonic-gate ** The following modules is an enhanced replacement for the "printf" subroutines
117c478bd9Sstevel@tonic-gate ** found in the standard C library.  The following enhancements are
127c478bd9Sstevel@tonic-gate ** supported:
137c478bd9Sstevel@tonic-gate **
147c478bd9Sstevel@tonic-gate **      +  Additional functions.  The standard set of "printf" functions
157c478bd9Sstevel@tonic-gate **         includes printf, fprintf, sprintf, vprintf, vfprintf, and
167c478bd9Sstevel@tonic-gate **         vsprintf.  This module adds the following:
177c478bd9Sstevel@tonic-gate **
187c478bd9Sstevel@tonic-gate **           *  snprintf -- Works like sprintf, but has an extra argument
197c478bd9Sstevel@tonic-gate **                          which is the size of the buffer written to.
207c478bd9Sstevel@tonic-gate **
217c478bd9Sstevel@tonic-gate **           *  mprintf --  Similar to sprintf.  Writes output to memory
227c478bd9Sstevel@tonic-gate **                          obtained from malloc.
237c478bd9Sstevel@tonic-gate **
247c478bd9Sstevel@tonic-gate **           *  xprintf --  Calls a function to dispose of output.
257c478bd9Sstevel@tonic-gate **
267c478bd9Sstevel@tonic-gate **           *  nprintf --  No output, but returns the number of characters
277c478bd9Sstevel@tonic-gate **                          that would have been output by printf.
287c478bd9Sstevel@tonic-gate **
297c478bd9Sstevel@tonic-gate **           *  A v- version (ex: vsnprintf) of every function is also
307c478bd9Sstevel@tonic-gate **              supplied.
317c478bd9Sstevel@tonic-gate **
327c478bd9Sstevel@tonic-gate **      +  A few extensions to the formatting notation are supported:
337c478bd9Sstevel@tonic-gate **
347c478bd9Sstevel@tonic-gate **           *  The "=" flag (similar to "-") causes the output to be
357c478bd9Sstevel@tonic-gate **              be centered in the appropriately sized field.
367c478bd9Sstevel@tonic-gate **
377c478bd9Sstevel@tonic-gate **           *  The %b field outputs an integer in binary notation.
387c478bd9Sstevel@tonic-gate **
397c478bd9Sstevel@tonic-gate **           *  The %c field now accepts a precision.  The character output
407c478bd9Sstevel@tonic-gate **              is repeated by the number of times the precision specifies.
417c478bd9Sstevel@tonic-gate **
427c478bd9Sstevel@tonic-gate **           *  The %' field works like %c, but takes as its character the
437c478bd9Sstevel@tonic-gate **              next character of the format string, instead of the next
447c478bd9Sstevel@tonic-gate **              argument.  For example,  printf("%.78'-")  prints 78 minus
457c478bd9Sstevel@tonic-gate **              signs, the same as  printf("%.78c",'-').
467c478bd9Sstevel@tonic-gate **
477c478bd9Sstevel@tonic-gate **      +  When compiled using GCC on a SPARC, this version of printf is
487c478bd9Sstevel@tonic-gate **         faster than the library printf for SUN OS 4.1.
497c478bd9Sstevel@tonic-gate **
507c478bd9Sstevel@tonic-gate **      +  All functions are fully reentrant.
517c478bd9Sstevel@tonic-gate **
527c478bd9Sstevel@tonic-gate */
537c478bd9Sstevel@tonic-gate #include "sqliteInt.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate ** Conversion types fall into various categories as defined by the
577c478bd9Sstevel@tonic-gate ** following enumeration.
587c478bd9Sstevel@tonic-gate */
597c478bd9Sstevel@tonic-gate #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
607c478bd9Sstevel@tonic-gate #define etFLOAT       2 /* Floating point.  %f */
617c478bd9Sstevel@tonic-gate #define etEXP         3 /* Exponentional notation. %e and %E */
627c478bd9Sstevel@tonic-gate #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
637c478bd9Sstevel@tonic-gate #define etSIZE        5 /* Return number of characters processed so far. %n */
647c478bd9Sstevel@tonic-gate #define etSTRING      6 /* Strings. %s */
657c478bd9Sstevel@tonic-gate #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
667c478bd9Sstevel@tonic-gate #define etPERCENT     8 /* Percent symbol. %% */
677c478bd9Sstevel@tonic-gate #define etCHARX       9 /* Characters. %c */
687c478bd9Sstevel@tonic-gate #define etERROR      10 /* Used to indicate no such conversion type */
697c478bd9Sstevel@tonic-gate /* The rest are extensions, not normally found in printf() */
707c478bd9Sstevel@tonic-gate #define etCHARLIT    11 /* Literal characters.  %' */
717c478bd9Sstevel@tonic-gate #define etSQLESCAPE  12 /* Strings with '\'' doubled.  %q */
727c478bd9Sstevel@tonic-gate #define etSQLESCAPE2 13 /* Strings with '\'' doubled and enclosed in '',
737c478bd9Sstevel@tonic-gate                           NULL pointers replaced by SQL NULL.  %Q */
747c478bd9Sstevel@tonic-gate #define etTOKEN      14 /* a pointer to a Token structure */
757c478bd9Sstevel@tonic-gate #define etSRCLIST    15 /* a pointer to a SrcList */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate ** An "etByte" is an 8-bit unsigned value.
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate typedef unsigned char etByte;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate ** Each builtin conversion character (ex: the 'd' in "%d") is described
857c478bd9Sstevel@tonic-gate ** by an instance of the following structure
867c478bd9Sstevel@tonic-gate */
877c478bd9Sstevel@tonic-gate typedef struct et_info {   /* Information about each format field */
887c478bd9Sstevel@tonic-gate   char fmttype;            /* The format field code letter */
897c478bd9Sstevel@tonic-gate   etByte base;             /* The base for radix conversion */
907c478bd9Sstevel@tonic-gate   etByte flags;            /* One or more of FLAG_ constants below */
917c478bd9Sstevel@tonic-gate   etByte type;             /* Conversion paradigm */
927c478bd9Sstevel@tonic-gate   char *charset;           /* The character set for conversion */
937c478bd9Sstevel@tonic-gate   char *prefix;            /* Prefix on non-zero values in alt format */
947c478bd9Sstevel@tonic-gate } et_info;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate ** Allowed values for et_info.flags
987c478bd9Sstevel@tonic-gate */
997c478bd9Sstevel@tonic-gate #define FLAG_SIGNED  1     /* True if the value to convert is signed */
1007c478bd9Sstevel@tonic-gate #define FLAG_INTERN  2     /* True if for internal use only */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate ** The following table is searched linearly, so it is good to put the
1057c478bd9Sstevel@tonic-gate ** most frequently used conversion types first.
1067c478bd9Sstevel@tonic-gate */
1077c478bd9Sstevel@tonic-gate static et_info fmtinfo[] = {
1087c478bd9Sstevel@tonic-gate   {  'd', 10, 1, etRADIX,      "0123456789",       0    },
1097c478bd9Sstevel@tonic-gate   {  's',  0, 0, etSTRING,     0,                  0    },
1107c478bd9Sstevel@tonic-gate   {  'z',  0, 2, etDYNSTRING,  0,                  0    },
1117c478bd9Sstevel@tonic-gate   {  'q',  0, 0, etSQLESCAPE,  0,                  0    },
1127c478bd9Sstevel@tonic-gate   {  'Q',  0, 0, etSQLESCAPE2, 0,                  0    },
1137c478bd9Sstevel@tonic-gate   {  'c',  0, 0, etCHARX,      0,                  0    },
1147c478bd9Sstevel@tonic-gate   {  'o',  8, 0, etRADIX,      "01234567",         "0"  },
1157c478bd9Sstevel@tonic-gate   {  'u', 10, 0, etRADIX,      "0123456789",       0    },
1167c478bd9Sstevel@tonic-gate   {  'x', 16, 0, etRADIX,      "0123456789abcdef", "x0" },
1177c478bd9Sstevel@tonic-gate   {  'X', 16, 0, etRADIX,      "0123456789ABCDEF", "X0" },
1187c478bd9Sstevel@tonic-gate   {  'f',  0, 1, etFLOAT,      0,                  0    },
1197c478bd9Sstevel@tonic-gate   {  'e',  0, 1, etEXP,        "e",                0    },
1207c478bd9Sstevel@tonic-gate   {  'E',  0, 1, etEXP,        "E",                0    },
1217c478bd9Sstevel@tonic-gate   {  'g',  0, 1, etGENERIC,    "e",                0    },
1227c478bd9Sstevel@tonic-gate   {  'G',  0, 1, etGENERIC,    "E",                0    },
1237c478bd9Sstevel@tonic-gate   {  'i', 10, 1, etRADIX,      "0123456789",       0    },
1247c478bd9Sstevel@tonic-gate   {  'n',  0, 0, etSIZE,       0,                  0    },
1257c478bd9Sstevel@tonic-gate   {  '%',  0, 0, etPERCENT,    0,                  0    },
1267c478bd9Sstevel@tonic-gate   {  'p', 10, 0, etRADIX,      "0123456789",       0    },
1277c478bd9Sstevel@tonic-gate   {  'T',  0, 2, etTOKEN,      0,                  0    },
1287c478bd9Sstevel@tonic-gate   {  'S',  0, 2, etSRCLIST,    0,                  0    },
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate #define etNINFO  (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate ** If NOFLOATINGPOINT is defined, then none of the floating point
1347c478bd9Sstevel@tonic-gate ** conversions will work.
1357c478bd9Sstevel@tonic-gate */
1367c478bd9Sstevel@tonic-gate #ifndef etNOFLOATINGPOINT
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate ** "*val" is a double such that 0.1 <= *val < 10.0
1397c478bd9Sstevel@tonic-gate ** Return the ascii code for the leading digit of *val, then
1407c478bd9Sstevel@tonic-gate ** multiply "*val" by 10.0 to renormalize.
1417c478bd9Sstevel@tonic-gate **
1427c478bd9Sstevel@tonic-gate ** Example:
1437c478bd9Sstevel@tonic-gate **     input:     *val = 3.14159
1447c478bd9Sstevel@tonic-gate **     output:    *val = 1.4159    function return = '3'
1457c478bd9Sstevel@tonic-gate **
1467c478bd9Sstevel@tonic-gate ** The counter *cnt is incremented each time.  After counter exceeds
1477c478bd9Sstevel@tonic-gate ** 16 (the number of significant digits in a 64-bit float) '0' is
1487c478bd9Sstevel@tonic-gate ** always returned.
1497c478bd9Sstevel@tonic-gate */
et_getdigit(LONGDOUBLE_TYPE * val,int * cnt)1507c478bd9Sstevel@tonic-gate static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
1517c478bd9Sstevel@tonic-gate   int digit;
1527c478bd9Sstevel@tonic-gate   LONGDOUBLE_TYPE d;
1537c478bd9Sstevel@tonic-gate   if( (*cnt)++ >= 16 ) return '0';
1547c478bd9Sstevel@tonic-gate   digit = (int)*val;
1557c478bd9Sstevel@tonic-gate   d = digit;
1567c478bd9Sstevel@tonic-gate   digit += '0';
1577c478bd9Sstevel@tonic-gate   *val = (*val - d)*10.0;
1587c478bd9Sstevel@tonic-gate   return digit;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate #endif
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate #define etBUFSIZE 1000  /* Size of the output buffer */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate ** The root program.  All variations call this core.
1667c478bd9Sstevel@tonic-gate **
1677c478bd9Sstevel@tonic-gate ** INPUTS:
1687c478bd9Sstevel@tonic-gate **   func   This is a pointer to a function taking three arguments
1697c478bd9Sstevel@tonic-gate **            1. A pointer to anything.  Same as the "arg" parameter.
1707c478bd9Sstevel@tonic-gate **            2. A pointer to the list of characters to be output
1717c478bd9Sstevel@tonic-gate **               (Note, this list is NOT null terminated.)
1727c478bd9Sstevel@tonic-gate **            3. An integer number of characters to be output.
1737c478bd9Sstevel@tonic-gate **               (Note: This number might be zero.)
1747c478bd9Sstevel@tonic-gate **
1757c478bd9Sstevel@tonic-gate **   arg    This is the pointer to anything which will be passed as the
1767c478bd9Sstevel@tonic-gate **          first argument to "func".  Use it for whatever you like.
1777c478bd9Sstevel@tonic-gate **
1787c478bd9Sstevel@tonic-gate **   fmt    This is the format string, as in the usual print.
1797c478bd9Sstevel@tonic-gate **
1807c478bd9Sstevel@tonic-gate **   ap     This is a pointer to a list of arguments.  Same as in
1817c478bd9Sstevel@tonic-gate **          vfprint.
1827c478bd9Sstevel@tonic-gate **
1837c478bd9Sstevel@tonic-gate ** OUTPUTS:
1847c478bd9Sstevel@tonic-gate **          The return value is the total number of characters sent to
1857c478bd9Sstevel@tonic-gate **          the function "func".  Returns -1 on a error.
1867c478bd9Sstevel@tonic-gate **
1877c478bd9Sstevel@tonic-gate ** Note that the order in which automatic variables are declared below
1887c478bd9Sstevel@tonic-gate ** seems to make a big difference in determining how fast this beast
1897c478bd9Sstevel@tonic-gate ** will run.
1907c478bd9Sstevel@tonic-gate */
vxprintf(void (* func)(void *,const char *,int),void * arg,int useExtended,const char * fmt,va_list ap)1917c478bd9Sstevel@tonic-gate static int vxprintf(
1927c478bd9Sstevel@tonic-gate   void (*func)(void*,const char*,int),     /* Consumer of text */
1937c478bd9Sstevel@tonic-gate   void *arg,                         /* First argument to the consumer */
1947c478bd9Sstevel@tonic-gate   int useExtended,                   /* Allow extended %-conversions */
1957c478bd9Sstevel@tonic-gate   const char *fmt,                   /* Format string */
1967c478bd9Sstevel@tonic-gate   va_list ap                         /* arguments */
1977c478bd9Sstevel@tonic-gate ){
1987c478bd9Sstevel@tonic-gate   int c;                     /* Next character in the format string */
1997c478bd9Sstevel@tonic-gate   char *bufpt;               /* Pointer to the conversion buffer */
2007c478bd9Sstevel@tonic-gate   int precision;             /* Precision of the current field */
2017c478bd9Sstevel@tonic-gate   int length;                /* Length of the field */
2027c478bd9Sstevel@tonic-gate   int idx;                   /* A general purpose loop counter */
2037c478bd9Sstevel@tonic-gate   int count;                 /* Total number of characters output */
2047c478bd9Sstevel@tonic-gate   int width;                 /* Width of the current field */
2057c478bd9Sstevel@tonic-gate   etByte flag_leftjustify;   /* True if "-" flag is present */
2067c478bd9Sstevel@tonic-gate   etByte flag_plussign;      /* True if "+" flag is present */
2077c478bd9Sstevel@tonic-gate   etByte flag_blanksign;     /* True if " " flag is present */
2087c478bd9Sstevel@tonic-gate   etByte flag_alternateform; /* True if "#" flag is present */
2097c478bd9Sstevel@tonic-gate   etByte flag_zeropad;       /* True if field width constant starts with zero */
2107c478bd9Sstevel@tonic-gate   etByte flag_long;          /* True if "l" flag is present */
2117c478bd9Sstevel@tonic-gate   unsigned long longvalue;   /* Value for integer types */
2127c478bd9Sstevel@tonic-gate   LONGDOUBLE_TYPE realvalue; /* Value for real types */
2137c478bd9Sstevel@tonic-gate   et_info *infop;            /* Pointer to the appropriate info structure */
2147c478bd9Sstevel@tonic-gate   char buf[etBUFSIZE];       /* Conversion buffer */
2157c478bd9Sstevel@tonic-gate   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
2167c478bd9Sstevel@tonic-gate   etByte errorflag = 0;      /* True if an error is encountered */
2177c478bd9Sstevel@tonic-gate   etByte xtype;              /* Conversion paradigm */
2187c478bd9Sstevel@tonic-gate   char *zExtra;              /* Extra memory used for etTCLESCAPE conversions */
2197c478bd9Sstevel@tonic-gate   static char spaces[] = "                                                  ";
2207c478bd9Sstevel@tonic-gate #define etSPACESIZE (sizeof(spaces)-1)
2217c478bd9Sstevel@tonic-gate #ifndef etNOFLOATINGPOINT
2227c478bd9Sstevel@tonic-gate   int  exp;                  /* exponent of real numbers */
2237c478bd9Sstevel@tonic-gate   double rounder;            /* Used for rounding floating point values */
2247c478bd9Sstevel@tonic-gate   etByte flag_dp;            /* True if decimal point should be shown */
2257c478bd9Sstevel@tonic-gate   etByte flag_rtz;           /* True if trailing zeros should be removed */
2267c478bd9Sstevel@tonic-gate   etByte flag_exp;           /* True to force display of the exponent */
2277c478bd9Sstevel@tonic-gate   int nsd;                   /* Number of significant digits returned */
2287c478bd9Sstevel@tonic-gate #endif
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate   func(arg,"",0);
2317c478bd9Sstevel@tonic-gate   count = length = 0;
2327c478bd9Sstevel@tonic-gate   bufpt = 0;
2337c478bd9Sstevel@tonic-gate   for(; (c=(*fmt))!=0; ++fmt){
2347c478bd9Sstevel@tonic-gate     if( c!='%' ){
2357c478bd9Sstevel@tonic-gate       int amt;
2367c478bd9Sstevel@tonic-gate       bufpt = (char *)fmt;
2377c478bd9Sstevel@tonic-gate       amt = 1;
2387c478bd9Sstevel@tonic-gate       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
2397c478bd9Sstevel@tonic-gate       (*func)(arg,bufpt,amt);
2407c478bd9Sstevel@tonic-gate       count += amt;
2417c478bd9Sstevel@tonic-gate       if( c==0 ) break;
2427c478bd9Sstevel@tonic-gate     }
2437c478bd9Sstevel@tonic-gate     if( (c=(*++fmt))==0 ){
2447c478bd9Sstevel@tonic-gate       errorflag = 1;
2457c478bd9Sstevel@tonic-gate       (*func)(arg,"%",1);
2467c478bd9Sstevel@tonic-gate       count++;
2477c478bd9Sstevel@tonic-gate       break;
2487c478bd9Sstevel@tonic-gate     }
2497c478bd9Sstevel@tonic-gate     /* Find out what flags are present */
250*1da57d55SToomas Soome     flag_leftjustify = flag_plussign = flag_blanksign =
2517c478bd9Sstevel@tonic-gate      flag_alternateform = flag_zeropad = 0;
2527c478bd9Sstevel@tonic-gate     do{
2537c478bd9Sstevel@tonic-gate       switch( c ){
2547c478bd9Sstevel@tonic-gate         case '-':   flag_leftjustify = 1;     c = 0;   break;
2557c478bd9Sstevel@tonic-gate         case '+':   flag_plussign = 1;        c = 0;   break;
2567c478bd9Sstevel@tonic-gate         case ' ':   flag_blanksign = 1;       c = 0;   break;
2577c478bd9Sstevel@tonic-gate         case '#':   flag_alternateform = 1;   c = 0;   break;
2587c478bd9Sstevel@tonic-gate         case '0':   flag_zeropad = 1;         c = 0;   break;
2597c478bd9Sstevel@tonic-gate         default:                                       break;
2607c478bd9Sstevel@tonic-gate       }
2617c478bd9Sstevel@tonic-gate     }while( c==0 && (c=(*++fmt))!=0 );
2627c478bd9Sstevel@tonic-gate     /* Get the field width */
2637c478bd9Sstevel@tonic-gate     width = 0;
2647c478bd9Sstevel@tonic-gate     if( c=='*' ){
2657c478bd9Sstevel@tonic-gate       width = va_arg(ap,int);
2667c478bd9Sstevel@tonic-gate       if( width<0 ){
2677c478bd9Sstevel@tonic-gate         flag_leftjustify = 1;
2687c478bd9Sstevel@tonic-gate         width = -width;
2697c478bd9Sstevel@tonic-gate       }
2707c478bd9Sstevel@tonic-gate       c = *++fmt;
2717c478bd9Sstevel@tonic-gate     }else{
2727c478bd9Sstevel@tonic-gate       while( c>='0' && c<='9' ){
2737c478bd9Sstevel@tonic-gate         width = width*10 + c - '0';
2747c478bd9Sstevel@tonic-gate         c = *++fmt;
2757c478bd9Sstevel@tonic-gate       }
2767c478bd9Sstevel@tonic-gate     }
2777c478bd9Sstevel@tonic-gate     if( width > etBUFSIZE-10 ){
2787c478bd9Sstevel@tonic-gate       width = etBUFSIZE-10;
2797c478bd9Sstevel@tonic-gate     }
2807c478bd9Sstevel@tonic-gate     /* Get the precision */
2817c478bd9Sstevel@tonic-gate     if( c=='.' ){
2827c478bd9Sstevel@tonic-gate       precision = 0;
2837c478bd9Sstevel@tonic-gate       c = *++fmt;
2847c478bd9Sstevel@tonic-gate       if( c=='*' ){
2857c478bd9Sstevel@tonic-gate         precision = va_arg(ap,int);
2867c478bd9Sstevel@tonic-gate         if( precision<0 ) precision = -precision;
2877c478bd9Sstevel@tonic-gate         c = *++fmt;
2887c478bd9Sstevel@tonic-gate       }else{
2897c478bd9Sstevel@tonic-gate         while( c>='0' && c<='9' ){
2907c478bd9Sstevel@tonic-gate           precision = precision*10 + c - '0';
2917c478bd9Sstevel@tonic-gate           c = *++fmt;
2927c478bd9Sstevel@tonic-gate         }
2937c478bd9Sstevel@tonic-gate       }
2947c478bd9Sstevel@tonic-gate       /* Limit the precision to prevent overflowing buf[] during conversion */
2957c478bd9Sstevel@tonic-gate       if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40;
2967c478bd9Sstevel@tonic-gate     }else{
2977c478bd9Sstevel@tonic-gate       precision = -1;
2987c478bd9Sstevel@tonic-gate     }
2997c478bd9Sstevel@tonic-gate     /* Get the conversion type modifier */
3007c478bd9Sstevel@tonic-gate     if( c=='l' ){
3017c478bd9Sstevel@tonic-gate       flag_long = 1;
3027c478bd9Sstevel@tonic-gate       c = *++fmt;
3037c478bd9Sstevel@tonic-gate     }else{
3047c478bd9Sstevel@tonic-gate       flag_long = 0;
3057c478bd9Sstevel@tonic-gate     }
3067c478bd9Sstevel@tonic-gate     /* Fetch the info entry for the field */
3077c478bd9Sstevel@tonic-gate     infop = 0;
3087c478bd9Sstevel@tonic-gate     xtype = etERROR;
3097c478bd9Sstevel@tonic-gate     for(idx=0; idx<etNINFO; idx++){
3107c478bd9Sstevel@tonic-gate       if( c==fmtinfo[idx].fmttype ){
3117c478bd9Sstevel@tonic-gate         infop = &fmtinfo[idx];
3127c478bd9Sstevel@tonic-gate         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
3137c478bd9Sstevel@tonic-gate           xtype = infop->type;
3147c478bd9Sstevel@tonic-gate         }
3157c478bd9Sstevel@tonic-gate         break;
3167c478bd9Sstevel@tonic-gate       }
3177c478bd9Sstevel@tonic-gate     }
3187c478bd9Sstevel@tonic-gate     zExtra = 0;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate     /*
3217c478bd9Sstevel@tonic-gate     ** At this point, variables are initialized as follows:
3227c478bd9Sstevel@tonic-gate     **
3237c478bd9Sstevel@tonic-gate     **   flag_alternateform          TRUE if a '#' is present.
3247c478bd9Sstevel@tonic-gate     **   flag_plussign               TRUE if a '+' is present.
3257c478bd9Sstevel@tonic-gate     **   flag_leftjustify            TRUE if a '-' is present or if the
3267c478bd9Sstevel@tonic-gate     **                               field width was negative.
3277c478bd9Sstevel@tonic-gate     **   flag_zeropad                TRUE if the width began with 0.
3287c478bd9Sstevel@tonic-gate     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
3297c478bd9Sstevel@tonic-gate     **                               the conversion character.
3307c478bd9Sstevel@tonic-gate     **   flag_blanksign              TRUE if a ' ' is present.
3317c478bd9Sstevel@tonic-gate     **   width                       The specified field width.  This is
3327c478bd9Sstevel@tonic-gate     **                               always non-negative.  Zero is the default.
3337c478bd9Sstevel@tonic-gate     **   precision                   The specified precision.  The default
3347c478bd9Sstevel@tonic-gate     **                               is -1.
3357c478bd9Sstevel@tonic-gate     **   xtype                       The class of the conversion.
3367c478bd9Sstevel@tonic-gate     **   infop                       Pointer to the appropriate info struct.
3377c478bd9Sstevel@tonic-gate     */
3387c478bd9Sstevel@tonic-gate     switch( xtype ){
3397c478bd9Sstevel@tonic-gate       case etRADIX:
3407c478bd9Sstevel@tonic-gate         if( flag_long )  longvalue = va_arg(ap,long);
3417c478bd9Sstevel@tonic-gate         else             longvalue = va_arg(ap,int);
3427c478bd9Sstevel@tonic-gate #if 1
3437c478bd9Sstevel@tonic-gate         /* For the format %#x, the value zero is printed "0" not "0x0".
3447c478bd9Sstevel@tonic-gate         ** I think this is stupid. */
3457c478bd9Sstevel@tonic-gate         if( longvalue==0 ) flag_alternateform = 0;
3467c478bd9Sstevel@tonic-gate #else
3477c478bd9Sstevel@tonic-gate         /* More sensible: turn off the prefix for octal (to prevent "00"),
3487c478bd9Sstevel@tonic-gate         ** but leave the prefix for hex. */
3497c478bd9Sstevel@tonic-gate         if( longvalue==0 && infop->base==8 ) flag_alternateform = 0;
3507c478bd9Sstevel@tonic-gate #endif
3517c478bd9Sstevel@tonic-gate         if( infop->flags & FLAG_SIGNED ){
3527c478bd9Sstevel@tonic-gate           if( *(long*)&longvalue<0 ){
3537c478bd9Sstevel@tonic-gate             longvalue = -*(long*)&longvalue;
3547c478bd9Sstevel@tonic-gate             prefix = '-';
3557c478bd9Sstevel@tonic-gate           }else if( flag_plussign )  prefix = '+';
3567c478bd9Sstevel@tonic-gate           else if( flag_blanksign )  prefix = ' ';
3577c478bd9Sstevel@tonic-gate           else                       prefix = 0;
3587c478bd9Sstevel@tonic-gate         }else                        prefix = 0;
3597c478bd9Sstevel@tonic-gate         if( flag_zeropad && precision<width-(prefix!=0) ){
3607c478bd9Sstevel@tonic-gate           precision = width-(prefix!=0);
3617c478bd9Sstevel@tonic-gate         }
3627c478bd9Sstevel@tonic-gate         bufpt = &buf[etBUFSIZE-1];
3637c478bd9Sstevel@tonic-gate         {
3647c478bd9Sstevel@tonic-gate           register char *cset;      /* Use registers for speed */
3657c478bd9Sstevel@tonic-gate           register int base;
3667c478bd9Sstevel@tonic-gate           cset = infop->charset;
3677c478bd9Sstevel@tonic-gate           base = infop->base;
3687c478bd9Sstevel@tonic-gate           do{                                           /* Convert to ascii */
3697c478bd9Sstevel@tonic-gate             *(--bufpt) = cset[longvalue%base];
3707c478bd9Sstevel@tonic-gate             longvalue = longvalue/base;
3717c478bd9Sstevel@tonic-gate           }while( longvalue>0 );
3727c478bd9Sstevel@tonic-gate         }
3737c478bd9Sstevel@tonic-gate         length = &buf[etBUFSIZE-1]-bufpt;
3747c478bd9Sstevel@tonic-gate         for(idx=precision-length; idx>0; idx--){
3757c478bd9Sstevel@tonic-gate           *(--bufpt) = '0';                             /* Zero pad */
3767c478bd9Sstevel@tonic-gate         }
3777c478bd9Sstevel@tonic-gate         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
3787c478bd9Sstevel@tonic-gate         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
3797c478bd9Sstevel@tonic-gate           char *pre, x;
3807c478bd9Sstevel@tonic-gate           pre = infop->prefix;
3817c478bd9Sstevel@tonic-gate           if( *bufpt!=pre[0] ){
3827c478bd9Sstevel@tonic-gate             for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x;
3837c478bd9Sstevel@tonic-gate           }
3847c478bd9Sstevel@tonic-gate         }
3857c478bd9Sstevel@tonic-gate         length = &buf[etBUFSIZE-1]-bufpt;
3867c478bd9Sstevel@tonic-gate         break;
3877c478bd9Sstevel@tonic-gate       case etFLOAT:
3887c478bd9Sstevel@tonic-gate       case etEXP:
3897c478bd9Sstevel@tonic-gate       case etGENERIC:
3907c478bd9Sstevel@tonic-gate         realvalue = va_arg(ap,double);
3917c478bd9Sstevel@tonic-gate #ifndef etNOFLOATINGPOINT
3927c478bd9Sstevel@tonic-gate         if( precision<0 ) precision = 6;         /* Set default precision */
3937c478bd9Sstevel@tonic-gate         if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10;
3947c478bd9Sstevel@tonic-gate         if( realvalue<0.0 ){
3957c478bd9Sstevel@tonic-gate           realvalue = -realvalue;
3967c478bd9Sstevel@tonic-gate           prefix = '-';
3977c478bd9Sstevel@tonic-gate         }else{
3987c478bd9Sstevel@tonic-gate           if( flag_plussign )          prefix = '+';
3997c478bd9Sstevel@tonic-gate           else if( flag_blanksign )    prefix = ' ';
4007c478bd9Sstevel@tonic-gate           else                         prefix = 0;
4017c478bd9Sstevel@tonic-gate         }
4027c478bd9Sstevel@tonic-gate         if( infop->type==etGENERIC && precision>0 ) precision--;
4037c478bd9Sstevel@tonic-gate         rounder = 0.0;
4047c478bd9Sstevel@tonic-gate #if 0
4057c478bd9Sstevel@tonic-gate         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
4067c478bd9Sstevel@tonic-gate         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
4077c478bd9Sstevel@tonic-gate #else
4087c478bd9Sstevel@tonic-gate         /* It makes more sense to use 0.5 */
4097c478bd9Sstevel@tonic-gate         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
4107c478bd9Sstevel@tonic-gate #endif
4117c478bd9Sstevel@tonic-gate         if( infop->type==etFLOAT ) realvalue += rounder;
4127c478bd9Sstevel@tonic-gate         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
4137c478bd9Sstevel@tonic-gate         exp = 0;
4147c478bd9Sstevel@tonic-gate         if( realvalue>0.0 ){
4157c478bd9Sstevel@tonic-gate           while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
4167c478bd9Sstevel@tonic-gate           while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
4177c478bd9Sstevel@tonic-gate           while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
4187c478bd9Sstevel@tonic-gate           while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
4197c478bd9Sstevel@tonic-gate           if( exp>350 || exp<-350 ){
4207c478bd9Sstevel@tonic-gate             bufpt = "NaN";
4217c478bd9Sstevel@tonic-gate             length = 3;
4227c478bd9Sstevel@tonic-gate             break;
4237c478bd9Sstevel@tonic-gate           }
4247c478bd9Sstevel@tonic-gate         }
4257c478bd9Sstevel@tonic-gate         bufpt = buf;
4267c478bd9Sstevel@tonic-gate         /*
4277c478bd9Sstevel@tonic-gate         ** If the field type is etGENERIC, then convert to either etEXP
4287c478bd9Sstevel@tonic-gate         ** or etFLOAT, as appropriate.
4297c478bd9Sstevel@tonic-gate         */
4307c478bd9Sstevel@tonic-gate         flag_exp = xtype==etEXP;
4317c478bd9Sstevel@tonic-gate         if( xtype!=etFLOAT ){
4327c478bd9Sstevel@tonic-gate           realvalue += rounder;
4337c478bd9Sstevel@tonic-gate           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
4347c478bd9Sstevel@tonic-gate         }
4357c478bd9Sstevel@tonic-gate         if( xtype==etGENERIC ){
4367c478bd9Sstevel@tonic-gate           flag_rtz = !flag_alternateform;
4377c478bd9Sstevel@tonic-gate           if( exp<-4 || exp>precision ){
4387c478bd9Sstevel@tonic-gate             xtype = etEXP;
4397c478bd9Sstevel@tonic-gate           }else{
4407c478bd9Sstevel@tonic-gate             precision = precision - exp;
4417c478bd9Sstevel@tonic-gate             xtype = etFLOAT;
4427c478bd9Sstevel@tonic-gate           }
4437c478bd9Sstevel@tonic-gate         }else{
4447c478bd9Sstevel@tonic-gate           flag_rtz = 0;
4457c478bd9Sstevel@tonic-gate         }
4467c478bd9Sstevel@tonic-gate         /*
4477c478bd9Sstevel@tonic-gate         ** The "exp+precision" test causes output to be of type etEXP if
4487c478bd9Sstevel@tonic-gate         ** the precision is too large to fit in buf[].
4497c478bd9Sstevel@tonic-gate         */
4507c478bd9Sstevel@tonic-gate         nsd = 0;
4517c478bd9Sstevel@tonic-gate         if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){
4527c478bd9Sstevel@tonic-gate           flag_dp = (precision>0 || flag_alternateform);
4537c478bd9Sstevel@tonic-gate           if( prefix ) *(bufpt++) = prefix;         /* Sign */
4547c478bd9Sstevel@tonic-gate           if( exp<0 )  *(bufpt++) = '0';            /* Digits before "." */
4557c478bd9Sstevel@tonic-gate           else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd);
4567c478bd9Sstevel@tonic-gate           if( flag_dp ) *(bufpt++) = '.';           /* The decimal point */
4577c478bd9Sstevel@tonic-gate           for(exp++; exp<0 && precision>0; precision--, exp++){
4587c478bd9Sstevel@tonic-gate             *(bufpt++) = '0';
4597c478bd9Sstevel@tonic-gate           }
4607c478bd9Sstevel@tonic-gate           while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
4617c478bd9Sstevel@tonic-gate           *(bufpt--) = 0;                           /* Null terminate */
4627c478bd9Sstevel@tonic-gate           if( flag_rtz && flag_dp ){     /* Remove trailing zeros and "." */
4637c478bd9Sstevel@tonic-gate             while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
4647c478bd9Sstevel@tonic-gate             if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
4657c478bd9Sstevel@tonic-gate           }
4667c478bd9Sstevel@tonic-gate           bufpt++;                            /* point to next free slot */
4677c478bd9Sstevel@tonic-gate         }else{    /* etEXP or etGENERIC */
4687c478bd9Sstevel@tonic-gate           flag_dp = (precision>0 || flag_alternateform);
4697c478bd9Sstevel@tonic-gate           if( prefix ) *(bufpt++) = prefix;   /* Sign */
4707c478bd9Sstevel@tonic-gate           *(bufpt++) = et_getdigit(&realvalue,&nsd);  /* First digit */
4717c478bd9Sstevel@tonic-gate           if( flag_dp ) *(bufpt++) = '.';     /* Decimal point */
4727c478bd9Sstevel@tonic-gate           while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
4737c478bd9Sstevel@tonic-gate           bufpt--;                            /* point to last digit */
4747c478bd9Sstevel@tonic-gate           if( flag_rtz && flag_dp ){          /* Remove tail zeros */
4757c478bd9Sstevel@tonic-gate             while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
4767c478bd9Sstevel@tonic-gate             if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
4777c478bd9Sstevel@tonic-gate           }
4787c478bd9Sstevel@tonic-gate           bufpt++;                            /* point to next free slot */
4797c478bd9Sstevel@tonic-gate           if( exp || flag_exp ){
4807c478bd9Sstevel@tonic-gate             *(bufpt++) = infop->charset[0];
4817c478bd9Sstevel@tonic-gate             if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */
4827c478bd9Sstevel@tonic-gate             else       { *(bufpt++) = '+'; }
4837c478bd9Sstevel@tonic-gate             if( exp>=100 ){
4847c478bd9Sstevel@tonic-gate               *(bufpt++) = (exp/100)+'0';                /* 100's digit */
4857c478bd9Sstevel@tonic-gate               exp %= 100;
4867c478bd9Sstevel@tonic-gate             }
4877c478bd9Sstevel@tonic-gate             *(bufpt++) = exp/10+'0';                     /* 10's digit */
4887c478bd9Sstevel@tonic-gate             *(bufpt++) = exp%10+'0';                     /* 1's digit */
4897c478bd9Sstevel@tonic-gate           }
4907c478bd9Sstevel@tonic-gate         }
4917c478bd9Sstevel@tonic-gate         /* The converted number is in buf[] and zero terminated. Output it.
4927c478bd9Sstevel@tonic-gate         ** Note that the number is in the usual order, not reversed as with
4937c478bd9Sstevel@tonic-gate         ** integer conversions. */
4947c478bd9Sstevel@tonic-gate         length = bufpt-buf;
4957c478bd9Sstevel@tonic-gate         bufpt = buf;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate         /* Special case:  Add leading zeros if the flag_zeropad flag is
4987c478bd9Sstevel@tonic-gate         ** set and we are not left justified */
4997c478bd9Sstevel@tonic-gate         if( flag_zeropad && !flag_leftjustify && length < width){
5007c478bd9Sstevel@tonic-gate           int i;
5017c478bd9Sstevel@tonic-gate           int nPad = width - length;
5027c478bd9Sstevel@tonic-gate           for(i=width; i>=nPad; i--){
5037c478bd9Sstevel@tonic-gate             bufpt[i] = bufpt[i-nPad];
5047c478bd9Sstevel@tonic-gate           }
5057c478bd9Sstevel@tonic-gate           i = prefix!=0;
5067c478bd9Sstevel@tonic-gate           while( nPad-- ) bufpt[i++] = '0';
5077c478bd9Sstevel@tonic-gate           length = width;
5087c478bd9Sstevel@tonic-gate         }
5097c478bd9Sstevel@tonic-gate #endif
5107c478bd9Sstevel@tonic-gate         break;
5117c478bd9Sstevel@tonic-gate       case etSIZE:
5127c478bd9Sstevel@tonic-gate         *(va_arg(ap,int*)) = count;
5137c478bd9Sstevel@tonic-gate         length = width = 0;
5147c478bd9Sstevel@tonic-gate         break;
5157c478bd9Sstevel@tonic-gate       case etPERCENT:
5167c478bd9Sstevel@tonic-gate         buf[0] = '%';
5177c478bd9Sstevel@tonic-gate         bufpt = buf;
5187c478bd9Sstevel@tonic-gate         length = 1;
5197c478bd9Sstevel@tonic-gate         break;
5207c478bd9Sstevel@tonic-gate       case etCHARLIT:
5217c478bd9Sstevel@tonic-gate       case etCHARX:
5227c478bd9Sstevel@tonic-gate         c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
5237c478bd9Sstevel@tonic-gate         if( precision>=0 ){
5247c478bd9Sstevel@tonic-gate           for(idx=1; idx<precision; idx++) buf[idx] = c;
5257c478bd9Sstevel@tonic-gate           length = precision;
5267c478bd9Sstevel@tonic-gate         }else{
5277c478bd9Sstevel@tonic-gate           length =1;
5287c478bd9Sstevel@tonic-gate         }
5297c478bd9Sstevel@tonic-gate         bufpt = buf;
5307c478bd9Sstevel@tonic-gate         break;
5317c478bd9Sstevel@tonic-gate       case etSTRING:
5327c478bd9Sstevel@tonic-gate       case etDYNSTRING:
5337c478bd9Sstevel@tonic-gate         bufpt = va_arg(ap,char*);
5347c478bd9Sstevel@tonic-gate         if( bufpt==0 ){
5357c478bd9Sstevel@tonic-gate           bufpt = "";
5367c478bd9Sstevel@tonic-gate         }else if( xtype==etDYNSTRING ){
5377c478bd9Sstevel@tonic-gate           zExtra = bufpt;
5387c478bd9Sstevel@tonic-gate         }
5397c478bd9Sstevel@tonic-gate         length = strlen(bufpt);
5407c478bd9Sstevel@tonic-gate         if( precision>=0 && precision<length ) length = precision;
5417c478bd9Sstevel@tonic-gate         break;
5427c478bd9Sstevel@tonic-gate       case etSQLESCAPE:
5437c478bd9Sstevel@tonic-gate       case etSQLESCAPE2:
5447c478bd9Sstevel@tonic-gate         {
5457c478bd9Sstevel@tonic-gate           int i, j, n, c, isnull;
5467c478bd9Sstevel@tonic-gate           char *arg = va_arg(ap,char*);
5477c478bd9Sstevel@tonic-gate           isnull = arg==0;
5487c478bd9Sstevel@tonic-gate           if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
5497c478bd9Sstevel@tonic-gate           for(i=n=0; (c=arg[i])!=0; i++){
5507c478bd9Sstevel@tonic-gate             if( c=='\'' )  n++;
5517c478bd9Sstevel@tonic-gate           }
5527c478bd9Sstevel@tonic-gate           n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0);
5537c478bd9Sstevel@tonic-gate           if( n>etBUFSIZE ){
5547c478bd9Sstevel@tonic-gate             bufpt = zExtra = sqliteMalloc( n );
5557c478bd9Sstevel@tonic-gate             if( bufpt==0 ) return -1;
5567c478bd9Sstevel@tonic-gate           }else{
5577c478bd9Sstevel@tonic-gate             bufpt = buf;
5587c478bd9Sstevel@tonic-gate           }
5597c478bd9Sstevel@tonic-gate           j = 0;
5607c478bd9Sstevel@tonic-gate           if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
5617c478bd9Sstevel@tonic-gate           for(i=0; (c=arg[i])!=0; i++){
5627c478bd9Sstevel@tonic-gate             bufpt[j++] = c;
5637c478bd9Sstevel@tonic-gate             if( c=='\'' ) bufpt[j++] = c;
5647c478bd9Sstevel@tonic-gate           }
5657c478bd9Sstevel@tonic-gate           if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
5667c478bd9Sstevel@tonic-gate           bufpt[j] = 0;
5677c478bd9Sstevel@tonic-gate           length = j;
5687c478bd9Sstevel@tonic-gate           if( precision>=0 && precision<length ) length = precision;
5697c478bd9Sstevel@tonic-gate         }
5707c478bd9Sstevel@tonic-gate         break;
5717c478bd9Sstevel@tonic-gate       case etTOKEN: {
5727c478bd9Sstevel@tonic-gate         Token *pToken = va_arg(ap, Token*);
5737c478bd9Sstevel@tonic-gate         (*func)(arg, pToken->z, pToken->n);
5747c478bd9Sstevel@tonic-gate         length = width = 0;
5757c478bd9Sstevel@tonic-gate         break;
5767c478bd9Sstevel@tonic-gate       }
5777c478bd9Sstevel@tonic-gate       case etSRCLIST: {
5787c478bd9Sstevel@tonic-gate         SrcList *pSrc = va_arg(ap, SrcList*);
5797c478bd9Sstevel@tonic-gate         int k = va_arg(ap, int);
5807c478bd9Sstevel@tonic-gate         struct SrcList_item *pItem = &pSrc->a[k];
5817c478bd9Sstevel@tonic-gate         assert( k>=0 && k<pSrc->nSrc );
5827c478bd9Sstevel@tonic-gate         if( pItem->zDatabase && pItem->zDatabase[0] ){
5837c478bd9Sstevel@tonic-gate           (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
5847c478bd9Sstevel@tonic-gate           (*func)(arg, ".", 1);
5857c478bd9Sstevel@tonic-gate         }
5867c478bd9Sstevel@tonic-gate         (*func)(arg, pItem->zName, strlen(pItem->zName));
5877c478bd9Sstevel@tonic-gate         length = width = 0;
5887c478bd9Sstevel@tonic-gate         break;
5897c478bd9Sstevel@tonic-gate       }
5907c478bd9Sstevel@tonic-gate       case etERROR:
5917c478bd9Sstevel@tonic-gate         buf[0] = '%';
5927c478bd9Sstevel@tonic-gate         buf[1] = c;
5937c478bd9Sstevel@tonic-gate         errorflag = 0;
5947c478bd9Sstevel@tonic-gate         idx = 1+(c!=0);
5957c478bd9Sstevel@tonic-gate         (*func)(arg,"%",idx);
5967c478bd9Sstevel@tonic-gate         count += idx;
5977c478bd9Sstevel@tonic-gate         if( c==0 ) fmt--;
5987c478bd9Sstevel@tonic-gate         break;
5997c478bd9Sstevel@tonic-gate     }/* End switch over the format type */
6007c478bd9Sstevel@tonic-gate     /*
6017c478bd9Sstevel@tonic-gate     ** The text of the conversion is pointed to by "bufpt" and is
6027c478bd9Sstevel@tonic-gate     ** "length" characters long.  The field width is "width".  Do
6037c478bd9Sstevel@tonic-gate     ** the output.
6047c478bd9Sstevel@tonic-gate     */
6057c478bd9Sstevel@tonic-gate     if( !flag_leftjustify ){
6067c478bd9Sstevel@tonic-gate       register int nspace;
6077c478bd9Sstevel@tonic-gate       nspace = width-length;
6087c478bd9Sstevel@tonic-gate       if( nspace>0 ){
6097c478bd9Sstevel@tonic-gate         count += nspace;
6107c478bd9Sstevel@tonic-gate         while( nspace>=etSPACESIZE ){
6117c478bd9Sstevel@tonic-gate           (*func)(arg,spaces,etSPACESIZE);
6127c478bd9Sstevel@tonic-gate           nspace -= etSPACESIZE;
6137c478bd9Sstevel@tonic-gate         }
6147c478bd9Sstevel@tonic-gate         if( nspace>0 ) (*func)(arg,spaces,nspace);
6157c478bd9Sstevel@tonic-gate       }
6167c478bd9Sstevel@tonic-gate     }
6177c478bd9Sstevel@tonic-gate     if( length>0 ){
6187c478bd9Sstevel@tonic-gate       (*func)(arg,bufpt,length);
6197c478bd9Sstevel@tonic-gate       count += length;
6207c478bd9Sstevel@tonic-gate     }
6217c478bd9Sstevel@tonic-gate     if( flag_leftjustify ){
6227c478bd9Sstevel@tonic-gate       register int nspace;
6237c478bd9Sstevel@tonic-gate       nspace = width-length;
6247c478bd9Sstevel@tonic-gate       if( nspace>0 ){
6257c478bd9Sstevel@tonic-gate         count += nspace;
6267c478bd9Sstevel@tonic-gate         while( nspace>=etSPACESIZE ){
6277c478bd9Sstevel@tonic-gate           (*func)(arg,spaces,etSPACESIZE);
6287c478bd9Sstevel@tonic-gate           nspace -= etSPACESIZE;
6297c478bd9Sstevel@tonic-gate         }
6307c478bd9Sstevel@tonic-gate         if( nspace>0 ) (*func)(arg,spaces,nspace);
6317c478bd9Sstevel@tonic-gate       }
6327c478bd9Sstevel@tonic-gate     }
6337c478bd9Sstevel@tonic-gate     if( zExtra ){
6347c478bd9Sstevel@tonic-gate       sqliteFree(zExtra);
6357c478bd9Sstevel@tonic-gate     }
6367c478bd9Sstevel@tonic-gate   }/* End for loop over the format string */
6377c478bd9Sstevel@tonic-gate   return errorflag ? -1 : count;
6387c478bd9Sstevel@tonic-gate } /* End of function */
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate /* This structure is used to store state information about the
6427c478bd9Sstevel@tonic-gate ** write to memory that is currently in progress.
6437c478bd9Sstevel@tonic-gate */
6447c478bd9Sstevel@tonic-gate struct sgMprintf {
6457c478bd9Sstevel@tonic-gate   char *zBase;     /* A base allocation */
6467c478bd9Sstevel@tonic-gate   char *zText;     /* The string collected so far */
6477c478bd9Sstevel@tonic-gate   int  nChar;      /* Length of the string so far */
6487c478bd9Sstevel@tonic-gate   int  nTotal;     /* Output size if unconstrained */
6497c478bd9Sstevel@tonic-gate   int  nAlloc;     /* Amount of space allocated in zText */
6507c478bd9Sstevel@tonic-gate   void *(*xRealloc)(void*,int);  /* Function used to realloc memory */
6517c478bd9Sstevel@tonic-gate };
6527c478bd9Sstevel@tonic-gate 
653*1da57d55SToomas Soome /*
654*1da57d55SToomas Soome ** This function implements the callback from vxprintf.
6557c478bd9Sstevel@tonic-gate **
6567c478bd9Sstevel@tonic-gate ** This routine add nNewChar characters of text in zNewText to
6577c478bd9Sstevel@tonic-gate ** the sgMprintf structure pointed to by "arg".
6587c478bd9Sstevel@tonic-gate */
mout(void * arg,const char * zNewText,int nNewChar)6597c478bd9Sstevel@tonic-gate static void mout(void *arg, const char *zNewText, int nNewChar){
6607c478bd9Sstevel@tonic-gate   struct sgMprintf *pM = (struct sgMprintf*)arg;
6617c478bd9Sstevel@tonic-gate   pM->nTotal += nNewChar;
6627c478bd9Sstevel@tonic-gate   if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
6637c478bd9Sstevel@tonic-gate     if( pM->xRealloc==0 ){
6647c478bd9Sstevel@tonic-gate       nNewChar =  pM->nAlloc - pM->nChar - 1;
6657c478bd9Sstevel@tonic-gate     }else{
6667c478bd9Sstevel@tonic-gate       pM->nAlloc = pM->nChar + nNewChar*2 + 1;
6677c478bd9Sstevel@tonic-gate       if( pM->zText==pM->zBase ){
6687c478bd9Sstevel@tonic-gate         pM->zText = pM->xRealloc(0, pM->nAlloc);
6697c478bd9Sstevel@tonic-gate         if( pM->zText && pM->nChar ){
6707c478bd9Sstevel@tonic-gate           memcpy(pM->zText, pM->zBase, pM->nChar);
6717c478bd9Sstevel@tonic-gate         }
6727c478bd9Sstevel@tonic-gate       }else{
6737c478bd9Sstevel@tonic-gate         pM->zText = pM->xRealloc(pM->zText, pM->nAlloc);
6747c478bd9Sstevel@tonic-gate       }
6757c478bd9Sstevel@tonic-gate     }
6767c478bd9Sstevel@tonic-gate   }
6777c478bd9Sstevel@tonic-gate   if( pM->zText ){
6787c478bd9Sstevel@tonic-gate     if( nNewChar>0 ){
6797c478bd9Sstevel@tonic-gate       memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
6807c478bd9Sstevel@tonic-gate       pM->nChar += nNewChar;
6817c478bd9Sstevel@tonic-gate     }
6827c478bd9Sstevel@tonic-gate     pM->zText[pM->nChar] = 0;
6837c478bd9Sstevel@tonic-gate   }
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate ** This routine is a wrapper around xprintf() that invokes mout() as
688*1da57d55SToomas Soome ** the consumer.
6897c478bd9Sstevel@tonic-gate */
base_vprintf(void * (* xRealloc)(void *,int),int useInternal,char * zInitBuf,int nInitBuf,const char * zFormat,va_list ap)6907c478bd9Sstevel@tonic-gate static char *base_vprintf(
6917c478bd9Sstevel@tonic-gate   void *(*xRealloc)(void*,int),   /* Routine to realloc memory. May be NULL */
6927c478bd9Sstevel@tonic-gate   int useInternal,                /* Use internal %-conversions if true */
6937c478bd9Sstevel@tonic-gate   char *zInitBuf,                 /* Initially write here, before mallocing */
6947c478bd9Sstevel@tonic-gate   int nInitBuf,                   /* Size of zInitBuf[] */
6957c478bd9Sstevel@tonic-gate   const char *zFormat,            /* format string */
6967c478bd9Sstevel@tonic-gate   va_list ap                      /* arguments */
6977c478bd9Sstevel@tonic-gate ){
6987c478bd9Sstevel@tonic-gate   struct sgMprintf sM;
6997c478bd9Sstevel@tonic-gate   sM.zBase = sM.zText = zInitBuf;
7007c478bd9Sstevel@tonic-gate   sM.nChar = sM.nTotal = 0;
7017c478bd9Sstevel@tonic-gate   sM.nAlloc = nInitBuf;
7027c478bd9Sstevel@tonic-gate   sM.xRealloc = xRealloc;
7037c478bd9Sstevel@tonic-gate   vxprintf(mout, &sM, useInternal, zFormat, ap);
7047c478bd9Sstevel@tonic-gate   if( xRealloc ){
7057c478bd9Sstevel@tonic-gate     if( sM.zText==sM.zBase ){
7067c478bd9Sstevel@tonic-gate       sM.zText = xRealloc(0, sM.nChar+1);
7077c478bd9Sstevel@tonic-gate       memcpy(sM.zText, sM.zBase, sM.nChar+1);
7087c478bd9Sstevel@tonic-gate     }else if( sM.nAlloc>sM.nChar+10 ){
7097c478bd9Sstevel@tonic-gate       sM.zText = xRealloc(sM.zText, sM.nChar+1);
7107c478bd9Sstevel@tonic-gate     }
7117c478bd9Sstevel@tonic-gate   }
7127c478bd9Sstevel@tonic-gate   return sM.zText;
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate /*
7167c478bd9Sstevel@tonic-gate ** Realloc that is a real function, not a macro.
7177c478bd9Sstevel@tonic-gate */
printf_realloc(void * old,int size)7187c478bd9Sstevel@tonic-gate static void *printf_realloc(void *old, int size){
7197c478bd9Sstevel@tonic-gate   return sqliteRealloc(old,size);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate ** Print into memory obtained from sqliteMalloc().  Use the internal
7247c478bd9Sstevel@tonic-gate ** %-conversion extensions.
7257c478bd9Sstevel@tonic-gate */
sqliteVMPrintf(const char * zFormat,va_list ap)7267c478bd9Sstevel@tonic-gate char *sqliteVMPrintf(const char *zFormat, va_list ap){
7277c478bd9Sstevel@tonic-gate   char zBase[1000];
7287c478bd9Sstevel@tonic-gate   return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate /*
7327c478bd9Sstevel@tonic-gate ** Print into memory obtained from sqliteMalloc().  Use the internal
7337c478bd9Sstevel@tonic-gate ** %-conversion extensions.
7347c478bd9Sstevel@tonic-gate */
sqliteMPrintf(const char * zFormat,...)7357c478bd9Sstevel@tonic-gate char *sqliteMPrintf(const char *zFormat, ...){
7367c478bd9Sstevel@tonic-gate   va_list ap;
7377c478bd9Sstevel@tonic-gate   char *z;
7387c478bd9Sstevel@tonic-gate   char zBase[1000];
7397c478bd9Sstevel@tonic-gate   va_start(ap, zFormat);
7407c478bd9Sstevel@tonic-gate   z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
7417c478bd9Sstevel@tonic-gate   va_end(ap);
7427c478bd9Sstevel@tonic-gate   return z;
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate ** Print into memory obtained from malloc().  Do not use the internal
7477c478bd9Sstevel@tonic-gate ** %-conversion extensions.  This routine is for use by external users.
7487c478bd9Sstevel@tonic-gate */
sqlite_mprintf(const char * zFormat,...)7497c478bd9Sstevel@tonic-gate char *sqlite_mprintf(const char *zFormat, ...){
7507c478bd9Sstevel@tonic-gate   va_list ap;
7517c478bd9Sstevel@tonic-gate   char *z;
7527c478bd9Sstevel@tonic-gate   char zBuf[200];
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate   va_start(ap,zFormat);
755*1da57d55SToomas Soome   z = base_vprintf((void*(*)(void*,int))realloc, 0,
7567c478bd9Sstevel@tonic-gate                    zBuf, sizeof(zBuf), zFormat, ap);
7577c478bd9Sstevel@tonic-gate   va_end(ap);
7587c478bd9Sstevel@tonic-gate   return z;
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
761*1da57d55SToomas Soome /* This is the varargs version of sqlite_mprintf.
7627c478bd9Sstevel@tonic-gate */
sqlite_vmprintf(const char * zFormat,va_list ap)7637c478bd9Sstevel@tonic-gate char *sqlite_vmprintf(const char *zFormat, va_list ap){
7647c478bd9Sstevel@tonic-gate   char zBuf[200];
7657c478bd9Sstevel@tonic-gate   return base_vprintf((void*(*)(void*,int))realloc, 0,
7667c478bd9Sstevel@tonic-gate                       zBuf, sizeof(zBuf), zFormat, ap);
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate /*
7707c478bd9Sstevel@tonic-gate ** sqlite_snprintf() works like snprintf() except that it ignores the
7717c478bd9Sstevel@tonic-gate ** current locale settings.  This is important for SQLite because we
7727c478bd9Sstevel@tonic-gate ** are not able to use a "," as the decimal point in place of "." as
7737c478bd9Sstevel@tonic-gate ** specified by some locales.
7747c478bd9Sstevel@tonic-gate */
sqlite_snprintf(int n,char * zBuf,const char * zFormat,...)7757c478bd9Sstevel@tonic-gate char *sqlite_snprintf(int n, char *zBuf, const char *zFormat, ...){
7767c478bd9Sstevel@tonic-gate   char *z;
7777c478bd9Sstevel@tonic-gate   va_list ap;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate   va_start(ap,zFormat);
7807c478bd9Sstevel@tonic-gate   z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
7817c478bd9Sstevel@tonic-gate   va_end(ap);
7827c478bd9Sstevel@tonic-gate   return z;
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate /*
7867c478bd9Sstevel@tonic-gate ** The following four routines implement the varargs versions of the
7877c478bd9Sstevel@tonic-gate ** sqlite_exec() and sqlite_get_table() interfaces.  See the sqlite.h
7887c478bd9Sstevel@tonic-gate ** header files for a more detailed description of how these interfaces
7897c478bd9Sstevel@tonic-gate ** work.
7907c478bd9Sstevel@tonic-gate **
7917c478bd9Sstevel@tonic-gate ** These routines are all just simple wrappers.
7927c478bd9Sstevel@tonic-gate */
sqlite_exec_printf(sqlite * db,const char * sqlFormat,sqlite_callback xCallback,void * pArg,char ** errmsg,...)7937c478bd9Sstevel@tonic-gate int sqlite_exec_printf(
7947c478bd9Sstevel@tonic-gate   sqlite *db,                   /* An open database */
7957c478bd9Sstevel@tonic-gate   const char *sqlFormat,        /* printf-style format string for the SQL */
7967c478bd9Sstevel@tonic-gate   sqlite_callback xCallback,    /* Callback function */
7977c478bd9Sstevel@tonic-gate   void *pArg,                   /* 1st argument to callback function */
7987c478bd9Sstevel@tonic-gate   char **errmsg,                /* Error msg written here */
7997c478bd9Sstevel@tonic-gate   ...                           /* Arguments to the format string. */
8007c478bd9Sstevel@tonic-gate ){
8017c478bd9Sstevel@tonic-gate   va_list ap;
8027c478bd9Sstevel@tonic-gate   int rc;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate   va_start(ap, errmsg);
8057c478bd9Sstevel@tonic-gate   rc = sqlite_exec_vprintf(db, sqlFormat, xCallback, pArg, errmsg, ap);
8067c478bd9Sstevel@tonic-gate   va_end(ap);
8077c478bd9Sstevel@tonic-gate   return rc;
8087c478bd9Sstevel@tonic-gate }
sqlite_exec_vprintf(sqlite * db,const char * sqlFormat,sqlite_callback xCallback,void * pArg,char ** errmsg,va_list ap)8097c478bd9Sstevel@tonic-gate int sqlite_exec_vprintf(
8107c478bd9Sstevel@tonic-gate   sqlite *db,                   /* An open database */
8117c478bd9Sstevel@tonic-gate   const char *sqlFormat,        /* printf-style format string for the SQL */
8127c478bd9Sstevel@tonic-gate   sqlite_callback xCallback,    /* Callback function */
8137c478bd9Sstevel@tonic-gate   void *pArg,                   /* 1st argument to callback function */
8147c478bd9Sstevel@tonic-gate   char **errmsg,                /* Error msg written here */
8157c478bd9Sstevel@tonic-gate   va_list ap                    /* Arguments to the format string. */
8167c478bd9Sstevel@tonic-gate ){
8177c478bd9Sstevel@tonic-gate   char *zSql;
8187c478bd9Sstevel@tonic-gate   int rc;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate   zSql = sqlite_vmprintf(sqlFormat, ap);
8217c478bd9Sstevel@tonic-gate   rc = sqlite_exec(db, zSql, xCallback, pArg, errmsg);
8227c478bd9Sstevel@tonic-gate   free(zSql);
8237c478bd9Sstevel@tonic-gate   return rc;
8247c478bd9Sstevel@tonic-gate }
sqlite_get_table_printf(sqlite * db,const char * sqlFormat,char *** resultp,int * nrow,int * ncol,char ** errmsg,...)8257c478bd9Sstevel@tonic-gate int sqlite_get_table_printf(
8267c478bd9Sstevel@tonic-gate   sqlite *db,            /* An open database */
8277c478bd9Sstevel@tonic-gate   const char *sqlFormat, /* printf-style format string for the SQL */
8287c478bd9Sstevel@tonic-gate   char ***resultp,       /* Result written to a char *[]  that this points to */
8297c478bd9Sstevel@tonic-gate   int *nrow,             /* Number of result rows written here */
8307c478bd9Sstevel@tonic-gate   int *ncol,             /* Number of result columns written here */
8317c478bd9Sstevel@tonic-gate   char **errmsg,         /* Error msg written here */
8327c478bd9Sstevel@tonic-gate   ...                    /* Arguments to the format string */
8337c478bd9Sstevel@tonic-gate ){
8347c478bd9Sstevel@tonic-gate   va_list ap;
8357c478bd9Sstevel@tonic-gate   int rc;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate   va_start(ap, errmsg);
8387c478bd9Sstevel@tonic-gate   rc = sqlite_get_table_vprintf(db, sqlFormat, resultp, nrow, ncol, errmsg, ap);
8397c478bd9Sstevel@tonic-gate   va_end(ap);
8407c478bd9Sstevel@tonic-gate   return rc;
8417c478bd9Sstevel@tonic-gate }
sqlite_get_table_vprintf(sqlite * db,const char * sqlFormat,char *** resultp,int * nrow,int * ncolumn,char ** errmsg,va_list ap)8427c478bd9Sstevel@tonic-gate int sqlite_get_table_vprintf(
8437c478bd9Sstevel@tonic-gate   sqlite *db,            /* An open database */
8447c478bd9Sstevel@tonic-gate   const char *sqlFormat, /* printf-style format string for the SQL */
8457c478bd9Sstevel@tonic-gate   char ***resultp,       /* Result written to a char *[]  that this points to */
8467c478bd9Sstevel@tonic-gate   int *nrow,             /* Number of result rows written here */
8477c478bd9Sstevel@tonic-gate   int *ncolumn,          /* Number of result columns written here */
8487c478bd9Sstevel@tonic-gate   char **errmsg,         /* Error msg written here */
8497c478bd9Sstevel@tonic-gate   va_list ap             /* Arguments to the format string */
8507c478bd9Sstevel@tonic-gate ){
8517c478bd9Sstevel@tonic-gate   char *zSql;
8527c478bd9Sstevel@tonic-gate   int rc;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate   zSql = sqlite_vmprintf(sqlFormat, ap);
8557c478bd9Sstevel@tonic-gate   rc = sqlite_get_table(db, zSql, resultp, nrow, ncolumn, errmsg);
8567c478bd9Sstevel@tonic-gate   free(zSql);
8577c478bd9Sstevel@tonic-gate   return rc;
8587c478bd9Sstevel@tonic-gate }
859