blob: 0f66bc29f5c3d2e8e74f363c3c8cd8794538f7a5 [file] [log] [blame]
drha18c5682000-10-08 22:20:57 +00001/*
2** The "printf" code that follows dates from the 1980's. It is in
drh38b41492015-06-08 15:08:15 +00003** the public domain.
drhe84a3062004-02-02 12:29:25 +00004**
5**************************************************************************
drha18c5682000-10-08 22:20:57 +00006**
drhed1fddf2011-10-12 18:52:59 +00007** This file contains code for a set of "printf"-like routines. These
8** routines format strings much like the printf() from the standard C
9** library, though the implementation here has enhancements to support
mistachkinf5b5f9b2015-06-08 17:42:57 +000010** SQLite.
drha18c5682000-10-08 22:20:57 +000011*/
12#include "sqliteInt.h"
drh7c68d602000-10-11 19:28:51 +000013
drha18c5682000-10-08 22:20:57 +000014/*
drha18c5682000-10-08 22:20:57 +000015** Conversion types fall into various categories as defined by the
16** following enumeration.
17*/
drh2c338a92017-02-10 19:38:36 +000018#define etRADIX 0 /* non-decimal integer types. %x %o */
drhad5a9d72016-05-05 11:53:12 +000019#define etFLOAT 1 /* Floating point. %f */
20#define etEXP 2 /* Exponentional notation. %e and %E */
21#define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */
22#define etSIZE 4 /* Return number of characters processed so far. %n */
23#define etSTRING 5 /* Strings. %s */
24#define etDYNSTRING 6 /* Dynamically allocated strings. %z */
25#define etPERCENT 7 /* Percent symbol. %% */
26#define etCHARX 8 /* Characters. %c */
drha18c5682000-10-08 22:20:57 +000027/* The rest are extensions, not normally found in printf() */
drhad5a9d72016-05-05 11:53:12 +000028#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
29#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
chw0cfcf3f2002-06-16 04:55:48 +000030 NULL pointers replaced by SQL NULL. %Q */
drhad5a9d72016-05-05 11:53:12 +000031#define etTOKEN 11 /* a pointer to a Token structure */
32#define etSRCLIST 12 /* a pointer to a SrcList */
33#define etPOINTER 13 /* The %p conversion */
34#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
35#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
drh2c338a92017-02-10 19:38:36 +000036#define etDECIMAL 16 /* %d or %u, but not %x, %o */
drhe84a3062004-02-02 12:29:25 +000037
drh2c338a92017-02-10 19:38:36 +000038#define etINVALID 17 /* Any unrecognized conversion type */
drh874ba042009-04-08 16:10:04 +000039
drhe84a3062004-02-02 12:29:25 +000040
41/*
42** An "etByte" is an 8-bit unsigned value.
43*/
44typedef unsigned char etByte;
drha18c5682000-10-08 22:20:57 +000045
46/*
47** Each builtin conversion character (ex: the 'd' in "%d") is described
48** by an instance of the following structure
49*/
50typedef struct et_info { /* Information about each format field */
drhe84a3062004-02-02 12:29:25 +000051 char fmttype; /* The format field code letter */
52 etByte base; /* The base for radix conversion */
53 etByte flags; /* One or more of FLAG_ constants below */
54 etByte type; /* Conversion paradigm */
drh76ff3a02004-09-24 22:32:30 +000055 etByte charset; /* Offset into aDigits[] of the digits string */
56 etByte prefix; /* Offset into aPrefix[] of the prefix string */
drha18c5682000-10-08 22:20:57 +000057} et_info;
58
59/*
drhe84a3062004-02-02 12:29:25 +000060** Allowed values for et_info.flags
61*/
drh2c338a92017-02-10 19:38:36 +000062#define FLAG_SIGNED 1 /* True if the value to convert is signed */
63#define FLAG_STRING 4 /* Allow infinite precision */
drhe84a3062004-02-02 12:29:25 +000064
65
66/*
drha18c5682000-10-08 22:20:57 +000067** The following table is searched linearly, so it is good to put the
68** most frequently used conversion types first.
69*/
drh76ff3a02004-09-24 22:32:30 +000070static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
71static const char aPrefix[] = "-x0\000X0";
drh57196282004-10-06 15:41:16 +000072static const et_info fmtinfo[] = {
drh2c338a92017-02-10 19:38:36 +000073 { 'd', 10, 1, etDECIMAL, 0, 0 },
drh4794f732004-11-05 17:17:50 +000074 { 's', 0, 4, etSTRING, 0, 0 },
drh557cc602005-08-13 12:59:14 +000075 { 'g', 0, 1, etGENERIC, 30, 0 },
drh153c62c2007-08-24 03:51:33 +000076 { 'z', 0, 4, etDYNSTRING, 0, 0 },
drh4794f732004-11-05 17:17:50 +000077 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
78 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
danielk1977f3b863e2007-06-24 06:32:17 +000079 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +000080 { 'c', 0, 0, etCHARX, 0, 0 },
81 { 'o', 8, 0, etRADIX, 0, 2 },
drh2c338a92017-02-10 19:38:36 +000082 { 'u', 10, 0, etDECIMAL, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +000083 { 'x', 16, 0, etRADIX, 16, 1 },
84 { 'X', 16, 0, etRADIX, 0, 4 },
drhb37df7b2005-10-13 02:09:49 +000085#ifndef SQLITE_OMIT_FLOATING_POINT
drh76ff3a02004-09-24 22:32:30 +000086 { 'f', 0, 1, etFLOAT, 0, 0 },
87 { 'e', 0, 1, etEXP, 30, 0 },
88 { 'E', 0, 1, etEXP, 14, 0 },
drh76ff3a02004-09-24 22:32:30 +000089 { 'G', 0, 1, etGENERIC, 14, 0 },
drhb37df7b2005-10-13 02:09:49 +000090#endif
drh2c338a92017-02-10 19:38:36 +000091 { 'i', 10, 1, etDECIMAL, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +000092 { 'n', 0, 0, etSIZE, 0, 0 },
93 { '%', 0, 0, etPERCENT, 0, 0 },
94 { 'p', 16, 0, etPOINTER, 0, 1 },
drh7e3ff5d2009-04-08 11:49:42 +000095
drh8236f682017-01-04 00:26:28 +000096 /* All the rest are undocumented and are for internal use only */
97 { 'T', 0, 0, etTOKEN, 0, 0 },
98 { 'S', 0, 0, etSRCLIST, 0, 0 },
99 { 'r', 10, 1, etORDINAL, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000100};
drha18c5682000-10-08 22:20:57 +0000101
102/*
drhb37df7b2005-10-13 02:09:49 +0000103** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
drha18c5682000-10-08 22:20:57 +0000104** conversions will work.
105*/
drhb37df7b2005-10-13 02:09:49 +0000106#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000107/*
108** "*val" is a double such that 0.1 <= *val < 10.0
109** Return the ascii code for the leading digit of *val, then
110** multiply "*val" by 10.0 to renormalize.
111**
112** Example:
113** input: *val = 3.14159
114** output: *val = 1.4159 function return = '3'
115**
116** The counter *cnt is incremented each time. After counter exceeds
117** 16 (the number of significant digits in a 64-bit float) '0' is
118** always returned.
119*/
drhea678832008-12-10 19:26:22 +0000120static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000121 int digit;
drh384eef32004-01-07 03:04:27 +0000122 LONGDOUBLE_TYPE d;
drh72b3fbc2012-06-19 03:11:25 +0000123 if( (*cnt)<=0 ) return '0';
124 (*cnt)--;
drha18c5682000-10-08 22:20:57 +0000125 digit = (int)*val;
126 d = digit;
127 digit += '0';
128 *val = (*val - d)*10.0;
drhea678832008-12-10 19:26:22 +0000129 return (char)digit;
drha18c5682000-10-08 22:20:57 +0000130}
drhb37df7b2005-10-13 02:09:49 +0000131#endif /* SQLITE_OMIT_FLOATING_POINT */
drha18c5682000-10-08 22:20:57 +0000132
drh79158e12005-09-06 21:40:45 +0000133/*
drha6353a32013-12-09 19:03:26 +0000134** Set the StrAccum object to an error mode.
135*/
drha5c14162013-12-17 15:03:06 +0000136static void setStrAccumError(StrAccum *p, u8 eError){
drh0cdbe1a2018-05-09 13:46:26 +0000137 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
drha6353a32013-12-09 19:03:26 +0000138 p->accError = eError;
drh255a81f2019-02-22 15:42:10 +0000139 if( p->mxAlloc ) sqlite3_str_reset(p);
drhc3dcdba2019-04-09 21:32:46 +0000140 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
drha6353a32013-12-09 19:03:26 +0000141}
142
143/*
drha5c14162013-12-17 15:03:06 +0000144** Extra argument values from a PrintfArguments object
145*/
146static sqlite3_int64 getIntArg(PrintfArguments *p){
147 if( p->nArg<=p->nUsed ) return 0;
148 return sqlite3_value_int64(p->apArg[p->nUsed++]);
149}
150static double getDoubleArg(PrintfArguments *p){
151 if( p->nArg<=p->nUsed ) return 0.0;
152 return sqlite3_value_double(p->apArg[p->nUsed++]);
153}
154static char *getTextArg(PrintfArguments *p){
155 if( p->nArg<=p->nUsed ) return 0;
156 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
157}
158
drh29642252019-02-01 20:29:04 +0000159/*
160** Allocate memory for a temporary buffer needed for printf rendering.
161**
162** If the requested size of the temp buffer is larger than the size
163** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
164** Do the size check before the memory allocation to prevent rogue
165** SQL from requesting large allocations using the precision or width
166** field of the printf() function.
167*/
168static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
169 char *z;
drh255a81f2019-02-22 15:42:10 +0000170 if( pAccum->accError ) return 0;
drh29642252019-02-01 20:29:04 +0000171 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
172 setStrAccumError(pAccum, SQLITE_TOOBIG);
173 return 0;
174 }
175 z = sqlite3DbMallocRaw(pAccum->db, n);
176 if( z==0 ){
177 setStrAccumError(pAccum, SQLITE_NOMEM);
178 }
179 return z;
180}
drha5c14162013-12-17 15:03:06 +0000181
182/*
drh79158e12005-09-06 21:40:45 +0000183** On machines with a small stack size, you can redefine the
drhed1fddf2011-10-12 18:52:59 +0000184** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
drh79158e12005-09-06 21:40:45 +0000185*/
186#ifndef SQLITE_PRINT_BUF_SIZE
drh59eedf72011-10-11 17:54:54 +0000187# define SQLITE_PRINT_BUF_SIZE 70
drh79158e12005-09-06 21:40:45 +0000188#endif
189#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
drha18c5682000-10-08 22:20:57 +0000190
191/*
drhed1fddf2011-10-12 18:52:59 +0000192** Render a string given by "fmt" into the StrAccum object.
drha18c5682000-10-08 22:20:57 +0000193*/
drh0cdbe1a2018-05-09 13:46:26 +0000194void sqlite3_str_vappendf(
195 sqlite3_str *pAccum, /* Accumulate results here */
drha5c14162013-12-17 15:03:06 +0000196 const char *fmt, /* Format string */
197 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000198){
drhe84a3062004-02-02 12:29:25 +0000199 int c; /* Next character in the format string */
200 char *bufpt; /* Pointer to the conversion buffer */
201 int precision; /* Precision of the current field */
202 int length; /* Length of the field */
203 int idx; /* A general purpose loop counter */
drhe84a3062004-02-02 12:29:25 +0000204 int width; /* Width of the current field */
205 etByte flag_leftjustify; /* True if "-" flag is present */
drh2c338a92017-02-10 19:38:36 +0000206 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
drhe84a3062004-02-02 12:29:25 +0000207 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42 +0000208 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25 +0000209 etByte flag_zeropad; /* True if field width constant starts with zero */
drh2c338a92017-02-10 19:38:36 +0000210 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
drh3e9aeec2005-08-13 13:39:02 +0000211 etByte done; /* Loop termination flag */
drh2c338a92017-02-10 19:38:36 +0000212 etByte cThousand; /* Thousands separator for %d and %u */
drhad5a9d72016-05-05 11:53:12 +0000213 etByte xtype = etINVALID; /* Conversion paradigm */
drha5c14162013-12-17 15:03:06 +0000214 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
drhed1fddf2011-10-12 18:52:59 +0000215 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
drh27436af2006-03-28 23:57:17 +0000216 sqlite_uint64 longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000217 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000218 const et_info *infop; /* Pointer to the appropriate info structure */
drh59eedf72011-10-11 17:54:54 +0000219 char *zOut; /* Rendering buffer */
220 int nOut; /* Size of the rendering buffer */
drhaf8f5132014-10-29 18:20:18 +0000221 char *zExtra = 0; /* Malloced memory used by some conversion */
drhb37df7b2005-10-13 02:09:49 +0000222#ifndef SQLITE_OMIT_FLOATING_POINT
drh557cc602005-08-13 12:59:14 +0000223 int exp, e2; /* exponent of real numbers */
drhed1fddf2011-10-12 18:52:59 +0000224 int nsd; /* Number of significant digits returned */
drhe84a3062004-02-02 12:29:25 +0000225 double rounder; /* Used for rounding floating point values */
226 etByte flag_dp; /* True if decimal point should be shown */
227 etByte flag_rtz; /* True if trailing zeros should be removed */
drha18c5682000-10-08 22:20:57 +0000228#endif
drha5c14162013-12-17 15:03:06 +0000229 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
drhed1fddf2011-10-12 18:52:59 +0000230 char buf[etBUFSIZE]; /* Conversion buffer */
drha18c5682000-10-08 22:20:57 +0000231
drhcc398962018-02-20 15:23:37 +0000232 /* pAccum never starts out with an empty buffer that was obtained from
233 ** malloc(). This precondition is required by the mprintf("%z...")
234 ** optimization. */
235 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
236
drha18c5682000-10-08 22:20:57 +0000237 bufpt = 0;
drh8236f682017-01-04 00:26:28 +0000238 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
239 pArgList = va_arg(ap, PrintfArguments*);
240 bArgList = 1;
drha5c14162013-12-17 15:03:06 +0000241 }else{
drh8236f682017-01-04 00:26:28 +0000242 bArgList = 0;
drha5c14162013-12-17 15:03:06 +0000243 }
drha18c5682000-10-08 22:20:57 +0000244 for(; (c=(*fmt))!=0; ++fmt){
245 if( c!='%' ){
drha18c5682000-10-08 22:20:57 +0000246 bufpt = (char *)fmt;
drh760b1592014-09-18 01:50:09 +0000247#if HAVE_STRCHRNUL
248 fmt = strchrnul(fmt, '%');
249#else
250 do{ fmt++; }while( *fmt && *fmt != '%' );
251#endif
drh0cdbe1a2018-05-09 13:46:26 +0000252 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
drh760b1592014-09-18 01:50:09 +0000253 if( *fmt==0 ) break;
drha18c5682000-10-08 22:20:57 +0000254 }
255 if( (c=(*++fmt))==0 ){
drh0cdbe1a2018-05-09 13:46:26 +0000256 sqlite3_str_append(pAccum, "%", 1);
drha18c5682000-10-08 22:20:57 +0000257 break;
258 }
259 /* Find out what flags are present */
drh2c338a92017-02-10 19:38:36 +0000260 flag_leftjustify = flag_prefix = cThousand =
drh557cc602005-08-13 12:59:14 +0000261 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02 +0000262 done = 0;
drh9a6d01b2019-02-01 18:46:41 +0000263 width = 0;
264 flag_long = 0;
265 precision = -1;
drha18c5682000-10-08 22:20:57 +0000266 do{
267 switch( c ){
drh3e9aeec2005-08-13 13:39:02 +0000268 case '-': flag_leftjustify = 1; break;
drh2c338a92017-02-10 19:38:36 +0000269 case '+': flag_prefix = '+'; break;
270 case ' ': flag_prefix = ' '; break;
drh3e9aeec2005-08-13 13:39:02 +0000271 case '#': flag_alternateform = 1; break;
272 case '!': flag_altform2 = 1; break;
273 case '0': flag_zeropad = 1; break;
drh2c338a92017-02-10 19:38:36 +0000274 case ',': cThousand = ','; break;
drh3e9aeec2005-08-13 13:39:02 +0000275 default: done = 1; break;
drh9a6d01b2019-02-01 18:46:41 +0000276 case 'l': {
277 flag_long = 1;
278 c = *++fmt;
279 if( c=='l' ){
280 c = *++fmt;
281 flag_long = 2;
282 }
283 done = 1;
284 break;
285 }
286 case '1': case '2': case '3': case '4': case '5':
287 case '6': case '7': case '8': case '9': {
288 unsigned wx = c - '0';
289 while( (c = *++fmt)>='0' && c<='9' ){
290 wx = wx*10 + c - '0';
291 }
292 testcase( wx>0x7fffffff );
293 width = wx & 0x7fffffff;
294#ifdef SQLITE_PRINTF_PRECISION_LIMIT
295 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
296 width = SQLITE_PRINTF_PRECISION_LIMIT;
297 }
298#endif
299 if( c!='.' && c!='l' ){
300 done = 1;
301 }else{
302 fmt--;
303 }
304 break;
305 }
306 case '*': {
307 if( bArgList ){
308 width = (int)getIntArg(pArgList);
309 }else{
310 width = va_arg(ap,int);
311 }
312 if( width<0 ){
313 flag_leftjustify = 1;
314 width = width >= -2147483647 ? -width : 0;
315 }
316#ifdef SQLITE_PRINTF_PRECISION_LIMIT
317 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
318 width = SQLITE_PRINTF_PRECISION_LIMIT;
319 }
320#endif
321 if( (c = fmt[1])!='.' && c!='l' ){
322 c = *++fmt;
323 done = 1;
324 }
325 break;
326 }
327 case '.': {
328 c = *++fmt;
329 if( c=='*' ){
330 if( bArgList ){
331 precision = (int)getIntArg(pArgList);
332 }else{
333 precision = va_arg(ap,int);
334 }
335 if( precision<0 ){
336 precision = precision >= -2147483647 ? -precision : -1;
337 }
338 c = *++fmt;
339 }else{
340 unsigned px = 0;
341 while( c>='0' && c<='9' ){
342 px = px*10 + c - '0';
343 c = *++fmt;
344 }
345 testcase( px>0x7fffffff );
346 precision = px & 0x7fffffff;
347 }
348#ifdef SQLITE_PRINTF_PRECISION_LIMIT
349 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
350 precision = SQLITE_PRINTF_PRECISION_LIMIT;
351 }
352#endif
353 if( c=='l' ){
354 --fmt;
355 }else{
356 done = 1;
357 }
358 break;
359 }
drha18c5682000-10-08 22:20:57 +0000360 }
drh3e9aeec2005-08-13 13:39:02 +0000361 }while( !done && (c=(*++fmt))!=0 );
dan8c069142015-04-07 14:38:57 +0000362
drha18c5682000-10-08 22:20:57 +0000363 /* Fetch the info entry for the field */
drh874ba042009-04-08 16:10:04 +0000364 infop = &fmtinfo[0];
365 xtype = etINVALID;
danielk197700e13612008-11-17 19:18:54 +0000366 for(idx=0; idx<ArraySize(fmtinfo); idx++){
drha18c5682000-10-08 22:20:57 +0000367 if( c==fmtinfo[idx].fmttype ){
368 infop = &fmtinfo[idx];
drh8236f682017-01-04 00:26:28 +0000369 xtype = infop->type;
drha18c5682000-10-08 22:20:57 +0000370 break;
371 }
372 }
drh43617e92006-03-06 20:55:46 +0000373
drha18c5682000-10-08 22:20:57 +0000374 /*
375 ** At this point, variables are initialized as follows:
376 **
377 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000378 ** flag_altform2 TRUE if a '!' is present.
drh2c338a92017-02-10 19:38:36 +0000379 ** flag_prefix '+' or ' ' or zero
drha18c5682000-10-08 22:20:57 +0000380 ** flag_leftjustify TRUE if a '-' is present or if the
381 ** field width was negative.
382 ** flag_zeropad TRUE if the width began with 0.
drh2c338a92017-02-10 19:38:36 +0000383 ** flag_long 1 for "l", 2 for "ll"
drha18c5682000-10-08 22:20:57 +0000384 ** width The specified field width. This is
385 ** always non-negative. Zero is the default.
386 ** precision The specified precision. The default
387 ** is -1.
388 ** xtype The class of the conversion.
389 ** infop Pointer to the appropriate info struct.
390 */
391 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000392 case etPOINTER:
drh2c338a92017-02-10 19:38:36 +0000393 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
394 sizeof(char*)==sizeof(long int) ? 1 : 0;
drhfe63d1c2004-09-08 20:13:04 +0000395 /* Fall through into the next case */
drh9a993342007-12-13 02:45:31 +0000396 case etORDINAL:
drh2c338a92017-02-10 19:38:36 +0000397 case etRADIX:
398 cThousand = 0;
399 /* Fall through into the next case */
400 case etDECIMAL:
drhe84a3062004-02-02 12:29:25 +0000401 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000402 i64 v;
drha5c14162013-12-17 15:03:06 +0000403 if( bArgList ){
404 v = getIntArg(pArgList);
drheeb23a42009-05-04 20:20:16 +0000405 }else if( flag_long ){
drh2c338a92017-02-10 19:38:36 +0000406 if( flag_long==2 ){
407 v = va_arg(ap,i64) ;
408 }else{
409 v = va_arg(ap,long int);
410 }
drheeb23a42009-05-04 20:20:16 +0000411 }else{
412 v = va_arg(ap,int);
413 }
drhe9707672004-06-25 01:10:48 +0000414 if( v<0 ){
drh158b9cb2011-03-05 20:59:46 +0000415 if( v==SMALLEST_INT64 ){
416 longvalue = ((u64)1)<<63;
417 }else{
418 longvalue = -v;
419 }
drhe9707672004-06-25 01:10:48 +0000420 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000421 }else{
drhe9707672004-06-25 01:10:48 +0000422 longvalue = v;
drh2c338a92017-02-10 19:38:36 +0000423 prefix = flag_prefix;
danielk1977cfcdaef2004-05-12 07:33:33 +0000424 }
drhe9707672004-06-25 01:10:48 +0000425 }else{
drha5c14162013-12-17 15:03:06 +0000426 if( bArgList ){
427 longvalue = (u64)getIntArg(pArgList);
drheeb23a42009-05-04 20:20:16 +0000428 }else if( flag_long ){
drh2c338a92017-02-10 19:38:36 +0000429 if( flag_long==2 ){
430 longvalue = va_arg(ap,u64);
431 }else{
432 longvalue = va_arg(ap,unsigned long int);
433 }
drheeb23a42009-05-04 20:20:16 +0000434 }else{
435 longvalue = va_arg(ap,unsigned int);
436 }
drhe9707672004-06-25 01:10:48 +0000437 prefix = 0;
438 }
439 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000440 if( flag_zeropad && precision<width-(prefix!=0) ){
441 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000442 }
drh2c338a92017-02-10 19:38:36 +0000443 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
drh59eedf72011-10-11 17:54:54 +0000444 nOut = etBUFSIZE;
445 zOut = buf;
446 }else{
drh7ba03ea2019-02-01 21:08:27 +0000447 u64 n;
448 n = (u64)precision + 10;
449 if( cThousand ) n += precision/3;
drh29642252019-02-01 20:29:04 +0000450 zOut = zExtra = printfTempBuf(pAccum, n);
451 if( zOut==0 ) return;
drh5f429952017-03-20 16:34:18 +0000452 nOut = (int)n;
drh59eedf72011-10-11 17:54:54 +0000453 }
454 bufpt = &zOut[nOut-1];
drh9a993342007-12-13 02:45:31 +0000455 if( xtype==etORDINAL ){
drh43f6e062007-12-13 17:50:22 +0000456 static const char zOrd[] = "thstndrd";
drhea678832008-12-10 19:26:22 +0000457 int x = (int)(longvalue % 10);
drh43f6e062007-12-13 17:50:22 +0000458 if( x>=4 || (longvalue/10)%10==1 ){
459 x = 0;
460 }
drh59eedf72011-10-11 17:54:54 +0000461 *(--bufpt) = zOrd[x*2+1];
462 *(--bufpt) = zOrd[x*2];
drh9a993342007-12-13 02:45:31 +0000463 }
drha18c5682000-10-08 22:20:57 +0000464 {
drh0e682092014-03-17 15:06:57 +0000465 const char *cset = &aDigits[infop->charset];
466 u8 base = infop->base;
drha18c5682000-10-08 22:20:57 +0000467 do{ /* Convert to ascii */
468 *(--bufpt) = cset[longvalue%base];
469 longvalue = longvalue/base;
470 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000471 }
drh59eedf72011-10-11 17:54:54 +0000472 length = (int)(&zOut[nOut-1]-bufpt);
drh2c338a92017-02-10 19:38:36 +0000473 while( precision>length ){
drha18c5682000-10-08 22:20:57 +0000474 *(--bufpt) = '0'; /* Zero pad */
drh2c338a92017-02-10 19:38:36 +0000475 length++;
476 }
477 if( cThousand ){
478 int nn = (length - 1)/3; /* Number of "," to insert */
479 int ix = (length - 1)%3 + 1;
480 bufpt -= nn;
481 for(idx=0; nn>0; idx++){
482 bufpt[idx] = bufpt[idx+nn];
483 ix--;
484 if( ix==0 ){
485 bufpt[++idx] = cThousand;
486 nn--;
487 ix = 3;
488 }
489 }
drh9adf9ac2002-05-15 11:44:13 +0000490 }
drha18c5682000-10-08 22:20:57 +0000491 if( prefix ) *(--bufpt) = prefix; /* Add sign */
492 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000493 const char *pre;
494 char x;
495 pre = &aPrefix[infop->prefix];
drhaf005fb2008-07-09 16:51:51 +0000496 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drha18c5682000-10-08 22:20:57 +0000497 }
drh59eedf72011-10-11 17:54:54 +0000498 length = (int)(&zOut[nOut-1]-bufpt);
drha18c5682000-10-08 22:20:57 +0000499 break;
500 case etFLOAT:
501 case etEXP:
502 case etGENERIC:
drha5c14162013-12-17 15:03:06 +0000503 if( bArgList ){
504 realvalue = getDoubleArg(pArgList);
505 }else{
506 realvalue = va_arg(ap,double);
507 }
drh69ef7032010-03-04 17:11:31 +0000508#ifdef SQLITE_OMIT_FLOATING_POINT
509 length = 0;
510#else
drha18c5682000-10-08 22:20:57 +0000511 if( precision<0 ) precision = 6; /* Set default precision */
drha18c5682000-10-08 22:20:57 +0000512 if( realvalue<0.0 ){
513 realvalue = -realvalue;
514 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000515 }else{
drh2c338a92017-02-10 19:38:36 +0000516 prefix = flag_prefix;
drh9adf9ac2002-05-15 11:44:13 +0000517 }
drh3e9aeec2005-08-13 13:39:02 +0000518 if( xtype==etGENERIC && precision>0 ) precision--;
drha30d22a2015-04-07 13:28:41 +0000519 testcase( precision>0xfff );
drh74b42272015-04-07 12:41:17 +0000520 for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
drh3e9aeec2005-08-13 13:39:02 +0000521 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000522 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
523 exp = 0;
drhea678832008-12-10 19:26:22 +0000524 if( sqlite3IsNaN((double)realvalue) ){
drh53c14022007-05-10 17:23:11 +0000525 bufpt = "NaN";
526 length = 3;
527 break;
528 }
drha18c5682000-10-08 22:20:57 +0000529 if( realvalue>0.0 ){
drh72b3fbc2012-06-19 03:11:25 +0000530 LONGDOUBLE_TYPE scale = 1.0;
drh4ef94132012-06-19 03:35:05 +0000531 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
drh2a8f6712015-09-02 21:00:48 +0000532 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
drh72b3fbc2012-06-19 03:11:25 +0000533 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
534 realvalue /= scale;
drh93a960a2008-07-10 00:32:42 +0000535 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
536 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
drhaf005fb2008-07-09 16:51:51 +0000537 if( exp>350 ){
drh2a8f6712015-09-02 21:00:48 +0000538 bufpt = buf;
539 buf[0] = prefix;
540 memcpy(buf+(prefix!=0),"Inf",4);
541 length = 3+(prefix!=0);
drha18c5682000-10-08 22:20:57 +0000542 break;
543 }
drh9adf9ac2002-05-15 11:44:13 +0000544 }
drha18c5682000-10-08 22:20:57 +0000545 bufpt = buf;
546 /*
547 ** If the field type is etGENERIC, then convert to either etEXP
548 ** or etFLOAT, as appropriate.
549 */
drha18c5682000-10-08 22:20:57 +0000550 if( xtype!=etFLOAT ){
551 realvalue += rounder;
552 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
553 }
554 if( xtype==etGENERIC ){
555 flag_rtz = !flag_alternateform;
556 if( exp<-4 || exp>precision ){
557 xtype = etEXP;
558 }else{
559 precision = precision - exp;
560 xtype = etFLOAT;
561 }
drh9adf9ac2002-05-15 11:44:13 +0000562 }else{
drh72b3fbc2012-06-19 03:11:25 +0000563 flag_rtz = flag_altform2;
drh9adf9ac2002-05-15 11:44:13 +0000564 }
drh557cc602005-08-13 12:59:14 +0000565 if( xtype==etEXP ){
566 e2 = 0;
567 }else{
568 e2 = exp;
569 }
drh29642252019-02-01 20:29:04 +0000570 {
571 i64 szBufNeeded; /* Size of a temporary buffer needed */
572 szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
573 if( szBufNeeded > etBUFSIZE ){
574 bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
575 if( bufpt==0 ) return;
drh59eedf72011-10-11 17:54:54 +0000576 }
577 }
578 zOut = bufpt;
drh72b3fbc2012-06-19 03:11:25 +0000579 nsd = 16 + flag_altform2*10;
drhea678832008-12-10 19:26:22 +0000580 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
drh557cc602005-08-13 12:59:14 +0000581 /* The sign in front of the number */
582 if( prefix ){
583 *(bufpt++) = prefix;
584 }
585 /* Digits prior to the decimal point */
586 if( e2<0 ){
587 *(bufpt++) = '0';
588 }else{
589 for(; e2>=0; e2--){
590 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000591 }
drh9adf9ac2002-05-15 11:44:13 +0000592 }
drh557cc602005-08-13 12:59:14 +0000593 /* The decimal point */
594 if( flag_dp ){
595 *(bufpt++) = '.';
596 }
597 /* "0" digits after the decimal point but before the first
598 ** significant digit of the number */
drhaf005fb2008-07-09 16:51:51 +0000599 for(e2++; e2<0; precision--, e2++){
600 assert( precision>0 );
drh557cc602005-08-13 12:59:14 +0000601 *(bufpt++) = '0';
602 }
603 /* Significant digits after the decimal point */
604 while( (precision--)>0 ){
605 *(bufpt++) = et_getdigit(&realvalue,&nsd);
606 }
607 /* Remove trailing zeros and the "." if no digits follow the "." */
608 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000609 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
drh59eedf72011-10-11 17:54:54 +0000610 assert( bufpt>zOut );
drh3e9aeec2005-08-13 13:39:02 +0000611 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000612 if( flag_altform2 ){
613 *(bufpt++) = '0';
614 }else{
615 *(--bufpt) = 0;
616 }
617 }
618 }
619 /* Add the "eNNN" suffix */
drh59eedf72011-10-11 17:54:54 +0000620 if( xtype==etEXP ){
drh557cc602005-08-13 12:59:14 +0000621 *(bufpt++) = aDigits[infop->charset];
622 if( exp<0 ){
623 *(bufpt++) = '-'; exp = -exp;
624 }else{
625 *(bufpt++) = '+';
626 }
627 if( exp>=100 ){
drhea678832008-12-10 19:26:22 +0000628 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
drh557cc602005-08-13 12:59:14 +0000629 exp %= 100;
630 }
drhea678832008-12-10 19:26:22 +0000631 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
632 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
drh557cc602005-08-13 12:59:14 +0000633 }
634 *bufpt = 0;
635
drha18c5682000-10-08 22:20:57 +0000636 /* The converted number is in buf[] and zero terminated. Output it.
637 ** Note that the number is in the usual order, not reversed as with
638 ** integer conversions. */
drh59eedf72011-10-11 17:54:54 +0000639 length = (int)(bufpt-zOut);
640 bufpt = zOut;
drha18c5682000-10-08 22:20:57 +0000641
642 /* Special case: Add leading zeros if the flag_zeropad flag is
643 ** set and we are not left justified */
644 if( flag_zeropad && !flag_leftjustify && length < width){
645 int i;
646 int nPad = width - length;
647 for(i=width; i>=nPad; i--){
648 bufpt[i] = bufpt[i-nPad];
649 }
650 i = prefix!=0;
651 while( nPad-- ) bufpt[i++] = '0';
652 length = width;
653 }
drh69ef7032010-03-04 17:11:31 +0000654#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
drha18c5682000-10-08 22:20:57 +0000655 break;
656 case etSIZE:
drhfc6ee9d2013-12-17 15:58:42 +0000657 if( !bArgList ){
658 *(va_arg(ap,int*)) = pAccum->nChar;
659 }
drha18c5682000-10-08 22:20:57 +0000660 length = width = 0;
661 break;
662 case etPERCENT:
663 buf[0] = '%';
664 bufpt = buf;
665 length = 1;
666 break;
drha18c5682000-10-08 22:20:57 +0000667 case etCHARX:
drha5c14162013-12-17 15:03:06 +0000668 if( bArgList ){
drhfc6ee9d2013-12-17 15:58:42 +0000669 bufpt = getTextArg(pArgList);
drha15a7c32018-02-19 21:58:16 +0000670 length = 1;
drh136102b2018-02-19 18:56:52 +0000671 if( bufpt ){
672 buf[0] = c = *(bufpt++);
drh136102b2018-02-19 18:56:52 +0000673 if( (c&0xc0)==0xc0 ){
674 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
675 buf[length++] = *(bufpt++);
676 }
677 }
drha15a7c32018-02-19 21:58:16 +0000678 }else{
679 buf[0] = 0;
drh136102b2018-02-19 18:56:52 +0000680 }
drha5c14162013-12-17 15:03:06 +0000681 }else{
drh136102b2018-02-19 18:56:52 +0000682 unsigned int ch = va_arg(ap,unsigned int);
683 if( ch<0x00080 ){
684 buf[0] = ch & 0xff;
685 length = 1;
686 }else if( ch<0x00800 ){
687 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
688 buf[1] = 0x80 + (u8)(ch & 0x3f);
689 length = 2;
690 }else if( ch<0x10000 ){
691 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
692 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
693 buf[2] = 0x80 + (u8)(ch & 0x3f);
694 length = 3;
695 }else{
696 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
697 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
698 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
699 buf[3] = 0x80 + (u8)(ch & 0x3f);
700 length = 4;
701 }
drha5c14162013-12-17 15:03:06 +0000702 }
drhaf8f5132014-10-29 18:20:18 +0000703 if( precision>1 ){
704 width -= precision-1;
705 if( width>1 && !flag_leftjustify ){
drh0cdbe1a2018-05-09 13:46:26 +0000706 sqlite3_str_appendchar(pAccum, width-1, ' ');
drhaf8f5132014-10-29 18:20:18 +0000707 width = 0;
708 }
drh136102b2018-02-19 18:56:52 +0000709 while( precision-- > 1 ){
drh0cdbe1a2018-05-09 13:46:26 +0000710 sqlite3_str_append(pAccum, buf, length);
drh136102b2018-02-19 18:56:52 +0000711 }
drh9adf9ac2002-05-15 11:44:13 +0000712 }
drha18c5682000-10-08 22:20:57 +0000713 bufpt = buf;
drhcf7c8372018-02-19 20:23:20 +0000714 flag_altform2 = 1;
715 goto adjust_width_for_utf8;
drha18c5682000-10-08 22:20:57 +0000716 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000717 case etDYNSTRING:
drha5c14162013-12-17 15:03:06 +0000718 if( bArgList ){
719 bufpt = getTextArg(pArgList);
drh2a8f6712015-09-02 21:00:48 +0000720 xtype = etSTRING;
drha5c14162013-12-17 15:03:06 +0000721 }else{
722 bufpt = va_arg(ap,char*);
723 }
drhd93d8a82003-06-16 03:08:18 +0000724 if( bufpt==0 ){
725 bufpt = "";
drh2a8f6712015-09-02 21:00:48 +0000726 }else if( xtype==etDYNSTRING ){
drhaf524a62018-09-13 17:07:12 +0000727 if( pAccum->nChar==0
728 && pAccum->mxAlloc
729 && width==0
730 && precision<0
731 && pAccum->accError==0
732 ){
drhcc398962018-02-20 15:23:37 +0000733 /* Special optimization for sqlite3_mprintf("%z..."):
734 ** Extend an existing memory allocation rather than creating
735 ** a new one. */
736 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
737 pAccum->zText = bufpt;
738 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
739 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
740 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
741 length = 0;
742 break;
743 }
drhd93d8a82003-06-16 03:08:18 +0000744 zExtra = bufpt;
745 }
drhe5090942008-04-29 15:22:27 +0000746 if( precision>=0 ){
drh62856462018-02-19 17:03:23 +0000747 if( flag_altform2 ){
748 /* Set length to the number of bytes needed in order to display
749 ** precision characters */
750 unsigned char *z = (unsigned char*)bufpt;
751 while( precision-- > 0 && z[0] ){
752 SQLITE_SKIP_UTF8(z);
753 }
754 length = (int)(z - (unsigned char*)bufpt);
755 }else{
756 for(length=0; length<precision && bufpt[length]; length++){}
757 }
drhe5090942008-04-29 15:22:27 +0000758 }else{
drhc84ddf12017-08-19 20:38:18 +0000759 length = 0x7fffffff & (int)strlen(bufpt);
drhe5090942008-04-29 15:22:27 +0000760 }
drh57e3ba72018-02-19 18:03:10 +0000761 adjust_width_for_utf8:
drh62856462018-02-19 17:03:23 +0000762 if( flag_altform2 && width>0 ){
763 /* Adjust width to account for extra bytes in UTF-8 characters */
764 int ii = length - 1;
765 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
766 }
drha18c5682000-10-08 22:20:57 +0000767 break;
drh57e3ba72018-02-19 18:03:10 +0000768 case etSQLESCAPE: /* %q: Escape ' characters */
769 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
770 case etSQLESCAPE3: { /* %w: Escape " characters */
drh8965b502009-11-25 16:53:37 +0000771 int i, j, k, n, isnull;
drh5eba8c02005-08-19 02:26:27 +0000772 int needQuote;
drhea678832008-12-10 19:26:22 +0000773 char ch;
danielk1977f3b863e2007-06-24 06:32:17 +0000774 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
drha5c14162013-12-17 15:03:06 +0000775 char *escarg;
776
777 if( bArgList ){
778 escarg = getTextArg(pArgList);
779 }else{
780 escarg = va_arg(ap,char*);
781 }
danielk1977f0113002006-01-24 12:09:17 +0000782 isnull = escarg==0;
783 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
drh57e3ba72018-02-19 18:03:10 +0000784 /* For %q, %Q, and %w, the precision is the number of byte (or
785 ** characters if the ! flags is present) to use from the input.
786 ** Because of the extra quoting characters inserted, the number
787 ** of output characters may be larger than the precision.
788 */
drh8965b502009-11-25 16:53:37 +0000789 k = precision;
dan60d4a302010-03-04 17:58:45 +0000790 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
danielk1977f3b863e2007-06-24 06:32:17 +0000791 if( ch==q ) n++;
drh57e3ba72018-02-19 18:03:10 +0000792 if( flag_altform2 && (ch&0xc0)==0xc0 ){
793 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
794 }
drha18c5682000-10-08 22:20:57 +0000795 }
drh5eba8c02005-08-19 02:26:27 +0000796 needQuote = !isnull && xtype==etSQLESCAPE2;
drh2a8f6712015-09-02 21:00:48 +0000797 n += i + 3;
drh5eba8c02005-08-19 02:26:27 +0000798 if( n>etBUFSIZE ){
drh29642252019-02-01 20:29:04 +0000799 bufpt = zExtra = printfTempBuf(pAccum, n);
800 if( bufpt==0 ) return;
drh5eba8c02005-08-19 02:26:27 +0000801 }else{
802 bufpt = buf;
803 }
804 j = 0;
danielk1977f3b863e2007-06-24 06:32:17 +0000805 if( needQuote ) bufpt[j++] = q;
drh8965b502009-11-25 16:53:37 +0000806 k = i;
807 for(i=0; i<k; i++){
808 bufpt[j++] = ch = escarg[i];
danielk1977f3b863e2007-06-24 06:32:17 +0000809 if( ch==q ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000810 }
danielk1977f3b863e2007-06-24 06:32:17 +0000811 if( needQuote ) bufpt[j++] = q;
drh5eba8c02005-08-19 02:26:27 +0000812 bufpt[j] = 0;
813 length = j;
drh57e3ba72018-02-19 18:03:10 +0000814 goto adjust_width_for_utf8;
drh5eba8c02005-08-19 02:26:27 +0000815 }
drh5f968432004-02-21 19:02:30 +0000816 case etTOKEN: {
drh8236f682017-01-04 00:26:28 +0000817 Token *pToken;
818 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
819 pToken = va_arg(ap, Token*);
drha5c14162013-12-17 15:03:06 +0000820 assert( bArgList==0 );
drha9ab4812013-12-11 11:00:44 +0000821 if( pToken && pToken->n ){
drh0cdbe1a2018-05-09 13:46:26 +0000822 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000823 }
drh5f968432004-02-21 19:02:30 +0000824 length = width = 0;
825 break;
826 }
827 case etSRCLIST: {
drh8236f682017-01-04 00:26:28 +0000828 SrcList *pSrc;
829 int k;
830 struct SrcList_item *pItem;
831 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
832 pSrc = va_arg(ap, SrcList*);
833 k = va_arg(ap, int);
834 pItem = &pSrc->a[k];
drha5c14162013-12-17 15:03:06 +0000835 assert( bArgList==0 );
drh5f968432004-02-21 19:02:30 +0000836 assert( k>=0 && k<pSrc->nSrc );
drh93a960a2008-07-10 00:32:42 +0000837 if( pItem->zDatabase ){
drh0cdbe1a2018-05-09 13:46:26 +0000838 sqlite3_str_appendall(pAccum, pItem->zDatabase);
839 sqlite3_str_append(pAccum, ".", 1);
drh5f968432004-02-21 19:02:30 +0000840 }
drh0cdbe1a2018-05-09 13:46:26 +0000841 sqlite3_str_appendall(pAccum, pItem->zName);
drh5f968432004-02-21 19:02:30 +0000842 length = width = 0;
843 break;
844 }
drh874ba042009-04-08 16:10:04 +0000845 default: {
846 assert( xtype==etINVALID );
847 return;
848 }
drha18c5682000-10-08 22:20:57 +0000849 }/* End switch over the format type */
850 /*
851 ** The text of the conversion is pointed to by "bufpt" and is
852 ** "length" characters long. The field width is "width". Do
drh62856462018-02-19 17:03:23 +0000853 ** the output. Both length and width are in bytes, not characters,
854 ** at this point. If the "!" flag was present on string conversions
855 ** indicating that width and precision should be expressed in characters,
856 ** then the values have been translated prior to reaching this point.
drha18c5682000-10-08 22:20:57 +0000857 */
drha70a0732014-03-17 14:24:27 +0000858 width -= length;
drh8236f682017-01-04 00:26:28 +0000859 if( width>0 ){
drh0cdbe1a2018-05-09 13:46:26 +0000860 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
861 sqlite3_str_append(pAccum, bufpt, length);
862 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
drh8236f682017-01-04 00:26:28 +0000863 }else{
drh0cdbe1a2018-05-09 13:46:26 +0000864 sqlite3_str_append(pAccum, bufpt, length);
drh8236f682017-01-04 00:26:28 +0000865 }
drha70a0732014-03-17 14:24:27 +0000866
drhaf8f5132014-10-29 18:20:18 +0000867 if( zExtra ){
drh96ceaf82015-11-14 22:04:22 +0000868 sqlite3DbFree(pAccum->db, zExtra);
drhaf8f5132014-10-29 18:20:18 +0000869 zExtra = 0;
870 }
drha18c5682000-10-08 22:20:57 +0000871 }/* End for loop over the format string */
drha18c5682000-10-08 22:20:57 +0000872} /* End of function */
873
drhade86482007-11-28 22:36:40 +0000874/*
drha70a0732014-03-17 14:24:27 +0000875** Enlarge the memory allocation on a StrAccum object so that it is
876** able to accept at least N more bytes of text.
877**
878** Return the number of bytes of text that StrAccum is able to accept
879** after the attempted enlargement. The value returned might be zero.
880*/
881static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
882 char *zNew;
drha30d22a2015-04-07 13:28:41 +0000883 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
drha70a0732014-03-17 14:24:27 +0000884 if( p->accError ){
drh0cdbe1a2018-05-09 13:46:26 +0000885 testcase(p->accError==SQLITE_TOOBIG);
886 testcase(p->accError==SQLITE_NOMEM);
drha70a0732014-03-17 14:24:27 +0000887 return 0;
888 }
drhc0490572015-05-02 11:45:53 +0000889 if( p->mxAlloc==0 ){
drh0cdbe1a2018-05-09 13:46:26 +0000890 setStrAccumError(p, SQLITE_TOOBIG);
drh255a81f2019-02-22 15:42:10 +0000891 return p->nAlloc - p->nChar - 1;
drha70a0732014-03-17 14:24:27 +0000892 }else{
drh5f4a6862016-01-30 12:50:25 +0000893 char *zOld = isMalloced(p) ? p->zText : 0;
drha70a0732014-03-17 14:24:27 +0000894 i64 szNew = p->nChar;
895 szNew += N + 1;
drh7b4d7802014-11-03 14:46:29 +0000896 if( szNew+p->nChar<=p->mxAlloc ){
897 /* Force exponential buffer size growth as long as it does not overflow,
898 ** to avoid having to call this routine too often */
899 szNew += p->nChar;
900 }
drha70a0732014-03-17 14:24:27 +0000901 if( szNew > p->mxAlloc ){
drh0cdbe1a2018-05-09 13:46:26 +0000902 sqlite3_str_reset(p);
903 setStrAccumError(p, SQLITE_TOOBIG);
drha70a0732014-03-17 14:24:27 +0000904 return 0;
905 }else{
906 p->nAlloc = (int)szNew;
907 }
drhc0490572015-05-02 11:45:53 +0000908 if( p->db ){
drha70a0732014-03-17 14:24:27 +0000909 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
910 }else{
drhf3cdcdc2015-04-29 16:50:28 +0000911 zNew = sqlite3_realloc64(zOld, p->nAlloc);
drha70a0732014-03-17 14:24:27 +0000912 }
913 if( zNew ){
drh7ef4d1c2014-05-31 15:39:53 +0000914 assert( p->zText!=0 || p->nChar==0 );
drh5f4a6862016-01-30 12:50:25 +0000915 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
drha70a0732014-03-17 14:24:27 +0000916 p->zText = zNew;
drh7f5a7ec2014-11-03 13:24:12 +0000917 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
drh5f4a6862016-01-30 12:50:25 +0000918 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
drha70a0732014-03-17 14:24:27 +0000919 }else{
drh0cdbe1a2018-05-09 13:46:26 +0000920 sqlite3_str_reset(p);
921 setStrAccumError(p, SQLITE_NOMEM);
drha70a0732014-03-17 14:24:27 +0000922 return 0;
923 }
924 }
925 return N;
926}
927
928/*
drhaf8f5132014-10-29 18:20:18 +0000929** Append N copies of character c to the given string buffer.
drha70a0732014-03-17 14:24:27 +0000930*/
drh0cdbe1a2018-05-09 13:46:26 +0000931void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
drha30d22a2015-04-07 13:28:41 +0000932 testcase( p->nChar + (i64)N > 0x7fffffff );
933 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
934 return;
935 }
drhaf8f5132014-10-29 18:20:18 +0000936 while( (N--)>0 ) p->zText[p->nChar++] = c;
drha70a0732014-03-17 14:24:27 +0000937}
938
939/*
940** The StrAccum "p" is not large enough to accept N new bytes of z[].
941** So enlarge if first, then do the append.
942**
drh0cdbe1a2018-05-09 13:46:26 +0000943** This is a helper routine to sqlite3_str_append() that does special-case
drha70a0732014-03-17 14:24:27 +0000944** work (enlarging the buffer) using tail recursion, so that the
drh0cdbe1a2018-05-09 13:46:26 +0000945** sqlite3_str_append() routine can use fast calling semantics.
drha70a0732014-03-17 14:24:27 +0000946*/
drh172087f2014-08-22 15:40:20 +0000947static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
drha70a0732014-03-17 14:24:27 +0000948 N = sqlite3StrAccumEnlarge(p, N);
949 if( N>0 ){
950 memcpy(&p->zText[p->nChar], z, N);
951 p->nChar += N;
952 }
953}
954
955/*
956** Append N bytes of text from z to the StrAccum object. Increase the
957** size of the memory allocation for StrAccum if necessary.
drha18c5682000-10-08 22:20:57 +0000958*/
drh0cdbe1a2018-05-09 13:46:26 +0000959void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
drh34573382015-04-15 05:38:35 +0000960 assert( z!=0 || N==0 );
drha6353a32013-12-09 19:03:26 +0000961 assert( p->zText!=0 || p->nChar==0 || p->accError );
962 assert( N>=0 );
drh255a81f2019-02-22 15:42:10 +0000963 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
drhade86482007-11-28 22:36:40 +0000964 if( p->nChar+N >= p->nAlloc ){
drha70a0732014-03-17 14:24:27 +0000965 enlargeAndAppend(p,z,N);
dan895decf2016-12-30 14:15:56 +0000966 }else if( N ){
drh172087f2014-08-22 15:40:20 +0000967 assert( p->zText );
968 p->nChar += N;
969 memcpy(&p->zText[p->nChar-N], z, N);
drh5f968432004-02-21 19:02:30 +0000970 }
drh483750b2003-01-29 18:46:51 +0000971}
972
973/*
drha6353a32013-12-09 19:03:26 +0000974** Append the complete text of zero-terminated string z[] to the p string.
975*/
drh0cdbe1a2018-05-09 13:46:26 +0000976void sqlite3_str_appendall(sqlite3_str *p, const char *z){
977 sqlite3_str_append(p, z, sqlite3Strlen30(z));
drha6353a32013-12-09 19:03:26 +0000978}
979
980
981/*
drhade86482007-11-28 22:36:40 +0000982** Finish off a string by making sure it is zero-terminated.
983** Return a pointer to the resulting string. Return a NULL
984** pointer if any kind of error was encountered.
drh5f968432004-02-21 19:02:30 +0000985*/
drh043e5862016-11-25 15:11:26 +0000986static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
drh3f18e6d2017-08-12 02:01:55 +0000987 char *zText;
drh043e5862016-11-25 15:11:26 +0000988 assert( p->mxAlloc>0 && !isMalloced(p) );
drh3f18e6d2017-08-12 02:01:55 +0000989 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
990 if( zText ){
991 memcpy(zText, p->zText, p->nChar+1);
drh043e5862016-11-25 15:11:26 +0000992 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
993 }else{
drh0cdbe1a2018-05-09 13:46:26 +0000994 setStrAccumError(p, SQLITE_NOMEM);
drh043e5862016-11-25 15:11:26 +0000995 }
drh3f18e6d2017-08-12 02:01:55 +0000996 p->zText = zText;
997 return zText;
drh043e5862016-11-25 15:11:26 +0000998}
drhade86482007-11-28 22:36:40 +0000999char *sqlite3StrAccumFinish(StrAccum *p){
1000 if( p->zText ){
1001 p->zText[p->nChar] = 0;
drh5f4a6862016-01-30 12:50:25 +00001002 if( p->mxAlloc>0 && !isMalloced(p) ){
drh043e5862016-11-25 15:11:26 +00001003 return strAccumFinishRealloc(p);
drhade86482007-11-28 22:36:40 +00001004 }
1005 }
1006 return p->zText;
1007}
1008
drhf80bba92018-05-16 15:35:03 +00001009/*
1010** This singleton is an sqlite3_str object that is returned if
1011** sqlite3_malloc() fails to provide space for a real one. This
1012** sqlite3_str object accepts no new text and always returns
1013** an SQLITE_NOMEM error.
1014*/
1015static sqlite3_str sqlite3OomStr = {
drh3e62ddb2018-05-30 00:59:09 +00001016 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
drhf80bba92018-05-16 15:35:03 +00001017};
1018
drh0cdbe1a2018-05-09 13:46:26 +00001019/* Finalize a string created using sqlite3_str_new().
1020*/
1021char *sqlite3_str_finish(sqlite3_str *p){
1022 char *z;
drhf80bba92018-05-16 15:35:03 +00001023 if( p!=0 && p!=&sqlite3OomStr ){
drh0cdbe1a2018-05-09 13:46:26 +00001024 z = sqlite3StrAccumFinish(p);
drh446135d2018-05-09 14:29:40 +00001025 sqlite3_free(p);
drh0cdbe1a2018-05-09 13:46:26 +00001026 }else{
1027 z = 0;
1028 }
1029 return z;
1030}
1031
1032/* Return any error code associated with p */
1033int sqlite3_str_errcode(sqlite3_str *p){
1034 return p ? p->accError : SQLITE_NOMEM;
1035}
1036
1037/* Return the current length of p in bytes */
1038int sqlite3_str_length(sqlite3_str *p){
1039 return p ? p->nChar : 0;
1040}
1041
1042/* Return the current value for p */
1043char *sqlite3_str_value(sqlite3_str *p){
drh446135d2018-05-09 14:29:40 +00001044 if( p==0 || p->nChar==0 ) return 0;
1045 p->zText[p->nChar] = 0;
1046 return p->zText;
drh0cdbe1a2018-05-09 13:46:26 +00001047}
1048
drhade86482007-11-28 22:36:40 +00001049/*
1050** Reset an StrAccum string. Reclaim all malloced memory.
1051*/
drh0cdbe1a2018-05-09 13:46:26 +00001052void sqlite3_str_reset(StrAccum *p){
drh5f4a6862016-01-30 12:50:25 +00001053 if( isMalloced(p) ){
drhc0490572015-05-02 11:45:53 +00001054 sqlite3DbFree(p->db, p->zText);
drh5f4a6862016-01-30 12:50:25 +00001055 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
drhade86482007-11-28 22:36:40 +00001056 }
drh446135d2018-05-09 14:29:40 +00001057 p->nAlloc = 0;
1058 p->nChar = 0;
drhf089aa42008-07-08 19:34:06 +00001059 p->zText = 0;
drhade86482007-11-28 22:36:40 +00001060}
1061
1062/*
drhc0490572015-05-02 11:45:53 +00001063** Initialize a string accumulator.
1064**
1065** p: The accumulator to be initialized.
1066** db: Pointer to a database connection. May be NULL. Lookaside
1067** memory is used if not NULL. db->mallocFailed is set appropriately
1068** when not NULL.
1069** zBase: An initial buffer. May be NULL in which case the initial buffer
1070** is malloced.
1071** n: Size of zBase in bytes. If total space requirements never exceed
1072** n then no memory allocations ever occur.
1073** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
1074** allocations will ever occur.
drhade86482007-11-28 22:36:40 +00001075*/
drhc0490572015-05-02 11:45:53 +00001076void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
drh3f18e6d2017-08-12 02:01:55 +00001077 p->zText = zBase;
drhc0490572015-05-02 11:45:53 +00001078 p->db = db;
drhade86482007-11-28 22:36:40 +00001079 p->nAlloc = n;
drhbb4957f2008-03-20 14:03:29 +00001080 p->mxAlloc = mx;
drh3f18e6d2017-08-12 02:01:55 +00001081 p->nChar = 0;
drhb49bc862013-08-21 21:12:10 +00001082 p->accError = 0;
drh5f4a6862016-01-30 12:50:25 +00001083 p->printfFlags = 0;
drh5f968432004-02-21 19:02:30 +00001084}
1085
drh0cdbe1a2018-05-09 13:46:26 +00001086/* Allocate and initialize a new dynamic string object */
1087sqlite3_str *sqlite3_str_new(sqlite3 *db){
drh446135d2018-05-09 14:29:40 +00001088 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
drh0cdbe1a2018-05-09 13:46:26 +00001089 if( p ){
drh446135d2018-05-09 14:29:40 +00001090 sqlite3StrAccumInit(p, 0, 0, 0,
1091 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
drhf80bba92018-05-16 15:35:03 +00001092 }else{
1093 p = &sqlite3OomStr;
drh0cdbe1a2018-05-09 13:46:26 +00001094 }
1095 return p;
1096}
1097
drh5f968432004-02-21 19:02:30 +00001098/*
1099** Print into memory obtained from sqliteMalloc(). Use the internal
1100** %-conversion extensions.
1101*/
drh17435752007-08-16 04:30:38 +00001102char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
1103 char *z;
drh79158e12005-09-06 21:40:45 +00001104 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +00001105 StrAccum acc;
drhbc6160b2009-04-08 15:45:31 +00001106 assert( db!=0 );
drhc0490572015-05-02 11:45:53 +00001107 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
drhbc6160b2009-04-08 15:45:31 +00001108 db->aLimit[SQLITE_LIMIT_LENGTH]);
drh5f4a6862016-01-30 12:50:25 +00001109 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
drh0cdbe1a2018-05-09 13:46:26 +00001110 sqlite3_str_vappendf(&acc, zFormat, ap);
drhade86482007-11-28 22:36:40 +00001111 z = sqlite3StrAccumFinish(&acc);
drh0cdbe1a2018-05-09 13:46:26 +00001112 if( acc.accError==SQLITE_NOMEM ){
drh4a642b62016-02-05 01:55:27 +00001113 sqlite3OomFault(db);
drh17435752007-08-16 04:30:38 +00001114 }
1115 return z;
drh5f968432004-02-21 19:02:30 +00001116}
1117
1118/*
1119** Print into memory obtained from sqliteMalloc(). Use the internal
1120** %-conversion extensions.
1121*/
drh17435752007-08-16 04:30:38 +00001122char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +00001123 va_list ap;
1124 char *z;
drh5f968432004-02-21 19:02:30 +00001125 va_start(ap, zFormat);
drhade86482007-11-28 22:36:40 +00001126 z = sqlite3VMPrintf(db, zFormat, ap);
drh5f968432004-02-21 19:02:30 +00001127 va_end(ap);
1128 return z;
1129}
1130
1131/*
drh28dd4792006-06-26 21:35:44 +00001132** Print into memory obtained from sqlite3_malloc(). Omit the internal
1133** %-conversion extensions.
1134*/
1135char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drhade86482007-11-28 22:36:40 +00001136 char *z;
drh28dd4792006-06-26 21:35:44 +00001137 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +00001138 StrAccum acc;
drh9ca95732014-10-24 00:35:58 +00001139
1140#ifdef SQLITE_ENABLE_API_ARMOR
1141 if( zFormat==0 ){
1142 (void)SQLITE_MISUSE_BKPT;
1143 return 0;
1144 }
1145#endif
drhff1590e2008-07-14 12:52:53 +00001146#ifndef SQLITE_OMIT_AUTOINIT
1147 if( sqlite3_initialize() ) return 0;
1148#endif
drhc0490572015-05-02 11:45:53 +00001149 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
drh0cdbe1a2018-05-09 13:46:26 +00001150 sqlite3_str_vappendf(&acc, zFormat, ap);
drhade86482007-11-28 22:36:40 +00001151 z = sqlite3StrAccumFinish(&acc);
1152 return z;
drh28dd4792006-06-26 21:35:44 +00001153}
1154
1155/*
1156** Print into memory obtained from sqlite3_malloc()(). Omit the internal
1157** %-conversion extensions.
drh483750b2003-01-29 18:46:51 +00001158*/
danielk19776f8a5032004-05-10 10:34:51 +00001159char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +00001160 va_list ap;
drh5f968432004-02-21 19:02:30 +00001161 char *z;
drhff1590e2008-07-14 12:52:53 +00001162#ifndef SQLITE_OMIT_AUTOINIT
1163 if( sqlite3_initialize() ) return 0;
1164#endif
drh28dd4792006-06-26 21:35:44 +00001165 va_start(ap, zFormat);
drhb3738b62007-03-31 15:02:49 +00001166 z = sqlite3_vmprintf(zFormat, ap);
drha18c5682000-10-08 22:20:57 +00001167 va_end(ap);
drh5f968432004-02-21 19:02:30 +00001168 return z;
drha18c5682000-10-08 22:20:57 +00001169}
1170
drh93a5c6b2003-12-23 02:17:35 +00001171/*
danielk19776f8a5032004-05-10 10:34:51 +00001172** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +00001173** current locale settings. This is important for SQLite because we
1174** are not able to use a "," as the decimal point in place of "." as
1175** specified by some locales.
drhdb26d4c2011-01-05 12:20:09 +00001176**
1177** Oops: The first two arguments of sqlite3_snprintf() are backwards
1178** from the snprintf() standard. Unfortunately, it is too late to change
1179** this without breaking compatibility, so we just have to live with the
1180** mistake.
1181**
1182** sqlite3_vsnprintf() is the varargs version.
drh93a5c6b2003-12-23 02:17:35 +00001183*/
drhdb26d4c2011-01-05 12:20:09 +00001184char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
1185 StrAccum acc;
1186 if( n<=0 ) return zBuf;
drh9ca95732014-10-24 00:35:58 +00001187#ifdef SQLITE_ENABLE_API_ARMOR
1188 if( zBuf==0 || zFormat==0 ) {
1189 (void)SQLITE_MISUSE_BKPT;
drh96c707a2015-02-13 16:36:14 +00001190 if( zBuf ) zBuf[0] = 0;
drh9ca95732014-10-24 00:35:58 +00001191 return zBuf;
1192 }
1193#endif
drhc0490572015-05-02 11:45:53 +00001194 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
drh0cdbe1a2018-05-09 13:46:26 +00001195 sqlite3_str_vappendf(&acc, zFormat, ap);
drhe9bb5662016-11-25 15:47:53 +00001196 zBuf[acc.nChar] = 0;
1197 return zBuf;
drhdb26d4c2011-01-05 12:20:09 +00001198}
danielk19776f8a5032004-05-10 10:34:51 +00001199char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +00001200 char *z;
drh93a5c6b2003-12-23 02:17:35 +00001201 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +00001202 va_start(ap,zFormat);
drhdb26d4c2011-01-05 12:20:09 +00001203 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +00001204 va_end(ap);
drh5f968432004-02-21 19:02:30 +00001205 return z;
drh93a5c6b2003-12-23 02:17:35 +00001206}
1207
drh3f280702010-02-18 18:45:09 +00001208/*
drh7c0c4602010-03-03 22:25:18 +00001209** This is the routine that actually formats the sqlite3_log() message.
1210** We house it in a separate routine from sqlite3_log() to avoid using
1211** stack space on small-stack systems when logging is disabled.
1212**
1213** sqlite3_log() must render into a static buffer. It cannot dynamically
1214** allocate memory because it might be called while the memory allocator
1215** mutex is held.
drha8dbd522015-07-14 22:43:37 +00001216**
drh0cdbe1a2018-05-09 13:46:26 +00001217** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
drha8dbd522015-07-14 22:43:37 +00001218** certain format characters (%q) or for very large precisions or widths.
1219** Care must be taken that any sqlite3_log() calls that occur while the
1220** memory mutex is held do not use these mechanisms.
drh7c0c4602010-03-03 22:25:18 +00001221*/
1222static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
drha64fa912010-03-04 00:53:32 +00001223 StrAccum acc; /* String accumulator */
1224 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
drh7c0c4602010-03-03 22:25:18 +00001225
drhc0490572015-05-02 11:45:53 +00001226 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
drh0cdbe1a2018-05-09 13:46:26 +00001227 sqlite3_str_vappendf(&acc, zFormat, ap);
drh7c0c4602010-03-03 22:25:18 +00001228 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
1229 sqlite3StrAccumFinish(&acc));
1230}
1231
1232/*
drh3f280702010-02-18 18:45:09 +00001233** Format and write a message to the log if logging is enabled.
1234*/
drha7564662010-02-22 19:32:31 +00001235void sqlite3_log(int iErrCode, const char *zFormat, ...){
drh3f280702010-02-18 18:45:09 +00001236 va_list ap; /* Vararg list */
drh7c0c4602010-03-03 22:25:18 +00001237 if( sqlite3GlobalConfig.xLog ){
drh3f280702010-02-18 18:45:09 +00001238 va_start(ap, zFormat);
drh7c0c4602010-03-03 22:25:18 +00001239 renderLogMsg(iErrCode, zFormat, ap);
drh3f280702010-02-18 18:45:09 +00001240 va_end(ap);
drh3f280702010-02-18 18:45:09 +00001241 }
1242}
1243
mistachkin02b0e262015-04-16 03:37:19 +00001244#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
drhe54ca3f2004-06-07 01:52:14 +00001245/*
1246** A version of printf() that understands %lld. Used for debugging.
1247** The printf() built into some versions of windows does not understand %lld
1248** and segfaults if you give it a long long int.
1249*/
1250void sqlite3DebugPrintf(const char *zFormat, ...){
1251 va_list ap;
drhade86482007-11-28 22:36:40 +00001252 StrAccum acc;
drhe54ca3f2004-06-07 01:52:14 +00001253 char zBuf[500];
drhc0490572015-05-02 11:45:53 +00001254 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drhade86482007-11-28 22:36:40 +00001255 va_start(ap,zFormat);
drh0cdbe1a2018-05-09 13:46:26 +00001256 sqlite3_str_vappendf(&acc, zFormat, ap);
drhe54ca3f2004-06-07 01:52:14 +00001257 va_end(ap);
drhbed8e7e2007-12-08 17:55:35 +00001258 sqlite3StrAccumFinish(&acc);
mistachkin4a9ff912017-11-09 17:29:04 +00001259#ifdef SQLITE_OS_TRACE_PROC
1260 {
1261 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
1262 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
1263 }
1264#else
drh485f0032007-01-26 19:23:33 +00001265 fprintf(stdout,"%s", zBuf);
drh2ac3ee92004-06-07 16:27:46 +00001266 fflush(stdout);
mistachkin4a9ff912017-11-09 17:29:04 +00001267#endif
drhe54ca3f2004-06-07 01:52:14 +00001268}
1269#endif
drhc7bc4fd2009-11-25 18:03:42 +00001270
drh4fa4a542014-09-30 12:33:33 +00001271
drhc7bc4fd2009-11-25 18:03:42 +00001272/*
drh0cdbe1a2018-05-09 13:46:26 +00001273** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
drhd37bea52015-09-02 15:37:50 +00001274** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
drhc7bc4fd2009-11-25 18:03:42 +00001275*/
drh0cdbe1a2018-05-09 13:46:26 +00001276void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
drhc7bc4fd2009-11-25 18:03:42 +00001277 va_list ap;
1278 va_start(ap,zFormat);
drh0cdbe1a2018-05-09 13:46:26 +00001279 sqlite3_str_vappendf(p, zFormat, ap);
drhc7bc4fd2009-11-25 18:03:42 +00001280 va_end(ap);
1281}