Index: src/printf.c ================================================================== --- src/printf.c +++ src/printf.c @@ -50,17 +50,10 @@ ** + All functions are fully reentrant. ** */ #include "sqliteInt.h" -/* -** Undefine COMPATIBILITY to make some slight changes in the way things -** work. I think the changes are an improvement, but they are not -** backwards compatible. -*/ -/* #define COMPATIBILITY / * Compatible with SUN OS 4.1 */ - /* ** Conversion types fall into various categories as defined by the ** following enumeration. */ #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ @@ -76,10 +69,12 @@ /* The rest are extensions, not normally found in printf() */ #define etCHARLIT 11 /* Literal characters. %' */ #define etSQLESCAPE 12 /* Strings with '\'' doubled. %q */ #define etSQLESCAPE2 13 /* Strings with '\'' doubled and enclosed in '', NULL pointers replaced by SQL NULL. %Q */ +#define etTOKEN 14 /* a pointer to a Token structure */ +#define etSRCLIST 15 /* a pointer to a SrcList */ /* ** An "etByte" is an 8-bit unsigned value. */ @@ -127,10 +122,12 @@ { 'G', 0, 1, etGENERIC, "E", 0 }, { 'i', 10, 1, etRADIX, "0123456789", 0 }, { 'n', 0, 0, etSIZE, 0, 0 }, { '%', 0, 0, etPERCENT, 0, 0 }, { 'p', 10, 0, etRADIX, "0123456789", 0 }, + { 'T', 0, 2, etTOKEN, 0, 0 }, + { 'S', 0, 2, etSRCLIST, 0, 0 }, }; #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) /* ** If NOFLOATINGPOINT is defined, then none of the floating point @@ -190,14 +187,15 @@ ** Note that the order in which automatic variables are declared below ** seems to make a big difference in determining how fast this beast ** will run. */ static int vxprintf( - void (*func)(void*,char*,int), - void *arg, - const char *fmt, - va_list ap + void (*func)(void*,const char*,int), /* Consumer of text */ + void *arg, /* First argument to the consumer */ + int useExtended, /* Allow extended %-conversions */ + const char *fmt, /* Format string */ + va_list ap /* arguments */ ){ int c; /* Next character in the format string */ char *bufpt; /* Pointer to the conversion buffer */ int precision; /* Precision of the current field */ int length; /* Length of the field */ @@ -282,14 +280,11 @@ if( c=='.' ){ precision = 0; c = *++fmt; if( c=='*' ){ precision = va_arg(ap,int); -#ifndef etCOMPATIBILITY - /* This is sensible, but SUN OS 4.1 doesn't do it. */ if( precision<0 ) precision = -precision; -#endif c = *++fmt; }else{ while( c>='0' && c<='9' ){ precision = precision*10 + c - '0'; c = *++fmt; @@ -311,11 +306,13 @@ infop = 0; xtype = etERROR; for(idx=0; idxtype; + if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ + xtype = infop->type; + } break; } } zExtra = 0; @@ -339,11 +336,11 @@ */ switch( xtype ){ case etRADIX: if( flag_long ) longvalue = va_arg(ap,long); else longvalue = va_arg(ap,int); -#ifdef etCOMPATIBILITY +#if 1 /* For the format %#x, the value zero is printed "0" not "0x0". ** I think this is stupid. */ if( longvalue==0 ) flag_alternateform = 0; #else /* More sensible: turn off the prefix for octal (to prevent "00"), @@ -401,11 +398,11 @@ else if( flag_blanksign ) prefix = ' '; else prefix = 0; } if( infop->type==etGENERIC && precision>0 ) precision--; rounder = 0.0; -#ifdef COMPATIBILITY +#if 0 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); #else /* It makes more sense to use 0.5 */ for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1); @@ -569,10 +566,29 @@ bufpt[j] = 0; length = j; if( precision>=0 && precisionz, pToken->n); + length = width = 0; + break; + } + case etSRCLIST: { + SrcList *pSrc = va_arg(ap, SrcList*); + int k = va_arg(ap, int); + struct SrcList_item *pItem = &pSrc->a[k]; + assert( k>=0 && knSrc ); + if( pItem->zDatabase && pItem->zDatabase[0] ){ + (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase)); + (*func)(arg, ".", 1); + } + (*func)(arg, pItem->zName, strlen(pItem->zName)); + length = width = 0; + break; + } case etERROR: buf[0] = '%'; buf[1] = c; errorflag = 0; idx = 1+(c!=0); @@ -613,15 +629,11 @@ } if( nspace>0 ) (*func)(arg,spaces,nspace); } } if( zExtra ){ - if( xtype==etDYNSTRING ){ - free(zExtra); - }else{ - sqliteFree(zExtra); - } + sqliteFree(zExtra); } }/* End for loop over the format string */ return errorflag ? -1 : count; } /* End of function */ @@ -631,151 +643,144 @@ */ struct sgMprintf { char *zBase; /* A base allocation */ char *zText; /* The string collected so far */ int nChar; /* Length of the string so far */ + int nTotal; /* Output size if unconstrained */ int nAlloc; /* Amount of space allocated in zText */ + void *(*xRealloc)(void*,int); /* Function used to realloc memory */ }; /* ** This function implements the callback from vxprintf. ** ** This routine add nNewChar characters of text in zNewText to ** the sgMprintf structure pointed to by "arg". */ -static void mout(void *arg, char *zNewText, int nNewChar){ - struct sgMprintf *pM = (struct sgMprintf*)arg; - if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ - pM->nAlloc = pM->nChar + nNewChar*2 + 1; - if( pM->zText==pM->zBase ){ - pM->zText = sqliteMalloc(pM->nAlloc); - if( pM->zText && pM->nChar ) memcpy(pM->zText,pM->zBase,pM->nChar); - }else{ - char *z = sqliteRealloc(pM->zText, pM->nAlloc); - if( z==0 ){ - sqliteFree(pM->zText); - pM->nChar = 0; - pM->nAlloc = 0; - } - pM->zText = z; - } - } - if( pM->zText ){ +static void mout(void *arg, const char *zNewText, int nNewChar){ + struct sgMprintf *pM = (struct sgMprintf*)arg; + pM->nTotal += nNewChar; + if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ + if( pM->xRealloc==0 ){ + nNewChar = pM->nAlloc - pM->nChar - 1; + }else{ + pM->nAlloc = pM->nChar + nNewChar*2 + 1; + if( pM->zText==pM->zBase ){ + pM->zText = pM->xRealloc(0, pM->nAlloc); + if( pM->zText && pM->nChar ){ + memcpy(pM->zText, pM->zBase, pM->nChar); + } + }else{ + pM->zText = pM->xRealloc(pM->zText, pM->nAlloc); + } + } + } + if( pM->zText && nNewChar>0 ){ memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); pM->nChar += nNewChar; pM->zText[pM->nChar] = 0; } } /* -** sqlite_mprintf() works like printf(), but allocations memory to hold the -** resulting string and returns a pointer to the allocated memory. Use -** sqliteFree() to release the memory allocated. +** This routine is a wrapper around xprintf() that invokes mout() as +** the consumer. +*/ +static char *base_vprintf( + void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */ + int useInternal, /* Use internal %-conversions if true */ + char *zInitBuf, /* Initially write here, before mallocing */ + int nInitBuf, /* Size of zInitBuf[] */ + const char *zFormat, /* format string */ + va_list ap /* arguments */ +){ + struct sgMprintf sM; + sM.zBase = sM.zText = zInitBuf; + sM.nChar = sM.nTotal = 0; + sM.nAlloc = nInitBuf; + sM.xRealloc = xRealloc; + vxprintf(mout, &sM, useInternal, zFormat, ap); + if( xRealloc ){ + if( sM.zText==sM.zBase ){ + sM.zText = xRealloc(0, sM.nChar+1); + memcpy(sM.zText, sM.zBase, sM.nChar+1); + }else if( sM.nAlloc>sM.nChar+10 ){ + sM.zText = xRealloc(sM.zText, sM.nChar+1); + } + } + return sM.zText; +} + +/* +** Realloc that is a real function, not a macro. +*/ +static void *printf_realloc(void *old, int size){ + return sqliteRealloc(old,size); +} + +/* +** Print into memory obtained from sqliteMalloc(). Use the internal +** %-conversion extensions. +*/ +char *sqliteVMPrintf(const char *zFormat, va_list ap){ + char zBase[1000]; + return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); +} + +/* +** Print into memory obtained from sqliteMalloc(). Use the internal +** %-conversion extensions. */ char *sqliteMPrintf(const char *zFormat, ...){ va_list ap; - struct sgMprintf sMprintf; - char zBuf[200]; - - sMprintf.nChar = 0; - sMprintf.nAlloc = sizeof(zBuf); - sMprintf.zText = zBuf; - sMprintf.zBase = zBuf; - va_start(ap,zFormat); - vxprintf(mout,&sMprintf,zFormat,ap); + char *z; + char zBase[1000]; + va_start(ap, zFormat); + z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); va_end(ap); - sMprintf.zText[sMprintf.nChar] = 0; - return sqliteRealloc(sMprintf.zText, sMprintf.nChar+1); + return z; } /* -** sqlite_mprintf() works like printf(), but allocations memory to hold the -** resulting string and returns a pointer to the allocated memory. Use -** sqliteFree() to release the memory allocated. +** Print into memory obtained from malloc(). Do not use the internal +** %-conversion extensions. This routine is for use by external users. */ char *sqlite_mprintf(const char *zFormat, ...){ va_list ap; - struct sgMprintf sMprintf; - char *zNew; + char *z; char zBuf[200]; - sMprintf.nChar = 0; - sMprintf.nAlloc = sizeof(zBuf); - sMprintf.zText = zBuf; - sMprintf.zBase = zBuf; va_start(ap,zFormat); - vxprintf(mout,&sMprintf,zFormat,ap); + z = base_vprintf((void*(*)(void*,int))realloc, 0, + zBuf, sizeof(zBuf), zFormat, ap); va_end(ap); - sMprintf.zText[sMprintf.nChar] = 0; - zNew = malloc( sMprintf.nChar+1 ); - if( zNew ) strcpy(zNew,sMprintf.zText); - if( sMprintf.zText!=sMprintf.zBase ){ - sqliteFree(sMprintf.zText); - } - return zNew; + return z; } /* This is the varargs version of sqlite_mprintf. */ char *sqlite_vmprintf(const char *zFormat, va_list ap){ - struct sgMprintf sMprintf; - char *zNew; - char zBuf[200]; - sMprintf.nChar = 0; - sMprintf.zText = zBuf; - sMprintf.nAlloc = sizeof(zBuf); - sMprintf.zBase = zBuf; - vxprintf(mout,&sMprintf,zFormat,ap); - sMprintf.zText[sMprintf.nChar] = 0; - zNew = malloc( sMprintf.nChar+1 ); - if( zNew ) strcpy(zNew,sMprintf.zText); - if( sMprintf.zText!=sMprintf.zBase ){ - sqliteFree(sMprintf.zText); - } - return zNew; -} - -/* -** This function implements the callback from vxprintf. -** -** This routine add nNewChar characters of text in zNewText to -** the sgMprintf structure pointed to by "arg". Unlike mout() above, -** this routine does not allocate new space when the buffer fills. -** It just truncates. -*/ -static void sout(void *arg, char *zNewText, int nNewChar){ - struct sgMprintf *pM = (struct sgMprintf*)arg; - if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ - nNewChar = pM->nAlloc - pM->nChar - 1; - if( nNewChar<=0 ) return; - } - memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); - pM->nChar += nNewChar; - pM->zText[pM->nChar] = 0; -} - -/* -** sqlite_sprintf() works like sprintf() except that it ignores the + char zBuf[200]; + return base_vprintf((void*(*)(void*,int))realloc, 0, + zBuf, sizeof(zBuf), zFormat, ap); +} + +/* +** sqlite_snprintf() works like snprintf() except that it ignores the ** current locale settings. This is important for SQLite because we ** are not able to use a "," as the decimal point in place of "." as ** specified by some locales. */ -int sqlite_snprintf(int n, char *zBuf, const char *zFormat, ...){ - va_list ap; - struct sgMprintf sMprintf; - - sMprintf.nChar = 0; - sMprintf.nAlloc = n; - sMprintf.zText = zBuf; - sMprintf.zBase = zBuf; - va_start(ap,zFormat); - vxprintf(sout,&sMprintf,zFormat,ap); - va_end(ap); - return sMprintf.nChar; -} - - +char *sqlite_snprintf(int n, char *zBuf, const char *zFormat, ...){ + char *z; + va_list ap; + + va_start(ap,zFormat); + z = base_vprintf(0, 0, zBuf, n, zFormat, ap); + va_end(ap); + return z; +} /* ** The following four routines implement the varargs versions of the ** sqlite_exec() and sqlite_get_table() interfaces. See the sqlite.h ** header files for a more detailed description of how these interfaces Index: src/sqliteInt.h ================================================================== --- src/sqliteInt.h +++ src/sqliteInt.h @@ -9,11 +9,11 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.216 2004/02/21 13:31:10 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.217 2004/02/21 19:02:30 drh Exp $ */ #include "config.h" #include "sqlite.h" #include "hash.h" #include "parse.h" @@ -180,10 +180,11 @@ # define sqliteRealloc(X,Y) sqliteRealloc_(X,Y,__FILE__,__LINE__) # define sqliteStrDup(X) sqliteStrDup_(X,__FILE__,__LINE__) # define sqliteStrNDup(X,Y) sqliteStrNDup_(X,Y,__FILE__,__LINE__) void sqliteStrRealloc(char**); #else +# define sqliteRealloc_(X,Y) sqliteRealloc(X,Y) # define sqliteStrRealloc(X) #endif /* ** This variable gets set if malloc() ever fails. After it gets set, @@ -1109,11 +1110,12 @@ void *sqliteRealloc(void*,int); char *sqliteStrDup(const char*); char *sqliteStrNDup(const char*, int); # define sqliteCheckMemory(a,b) #endif -char *sqliteMPrintf(const char *,...); +char *sqliteMPrintf(const char*, ...); +char *sqliteVMPrintf(const char*, va_list); void sqliteSetString(char **, const char *, ...); void sqliteSetNString(char **, ...); void sqliteErrorMsg(Parse*, const char*, ...); void sqliteDequote(char*); int sqliteKeywordCode(const char*, int); @@ -1254,7 +1256,7 @@ int sqliteFixSelect(DbFixer*, Select*); int sqliteFixExpr(DbFixer*, Expr*); int sqliteFixExprList(DbFixer*, ExprList*); int sqliteFixTriggerStep(DbFixer*, TriggerStep*); double sqliteAtoF(const char *z); -int sqlite_snprintf(int,char*,const char*,...); +char *sqlite_snprintf(int,char*,const char*,...); int sqliteFitsIn32Bits(const char *); Index: src/test1.c ================================================================== --- src/test1.c +++ src/test1.c @@ -11,11 +11,11 @@ ************************************************************************* ** Code for testing the printf() interface to SQLite. This code ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test1.c,v 1.33 2004/02/08 18:07:35 drh Exp $ +** $Id: test1.c,v 1.34 2004/02/21 19:02:30 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" #include "os.h" #include @@ -178,14 +178,14 @@ ){ char *zResult = 0; int i; for(i=2; i #include @@ -415,124 +415,15 @@ ** %T Insert a token ** %S Insert the first element of a SrcList */ void sqliteErrorMsg(Parse *pParse, const char *zFormat, ...){ va_list ap; - int nByte; - int i, j; - char *z; - static char zNull[] = "NULL"; - - pParse->nErr++; - nByte = 1 + strlen(zFormat); - va_start(ap, zFormat); - for(i=0; zFormat[i]; i++){ - if( zFormat[i]!='%' || zFormat[i+1]==0 ) continue; - i++; - switch( zFormat[i] ){ - case 'd': { - (void)va_arg(ap, int); - nByte += 20; - break; - } - case 'z': - case 's': { - char *z2 = va_arg(ap, char*); - if( z2==0 ) z2 = zNull; - nByte += strlen(z2); - break; - } - case 'T': { - Token *p = va_arg(ap, Token*); - nByte += p->n; - break; - } - case 'S': { - SrcList *p = va_arg(ap, SrcList*); - int k = va_arg(ap, int); - assert( p->nSrc>k && k>=0 ); - nByte += strlen(p->a[k].zName); - if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){ - nByte += strlen(p->a[k].zDatabase)+1; - } - break; - } - default: { - nByte++; - break; - } - } - } - va_end(ap); - z = sqliteMalloc( nByte ); - if( z==0 ) return; - sqliteFree(pParse->zErrMsg); - pParse->zErrMsg = z; - va_start(ap, zFormat); - for(i=j=0; zFormat[i]; i++){ - if( zFormat[i]!='%' || zFormat[i+1]==0 ) continue; - if( i>j ){ - memcpy(z, &zFormat[j], i-j); - z += i-j; - } - j = i+2; - i++; - switch( zFormat[i] ){ - case 'd': { - int x = va_arg(ap, int); - sprintf(z, "%d", x); - z += strlen(z); - break; - } - case 'z': - case 's': { - int len; - char *z2 = va_arg(ap, char*); - if( z2==0 ) z2 = zNull; - len = strlen(z2); - memcpy(z, z2, len); - z += len; - if( zFormat[i]=='z' && z2!=zNull ){ - sqliteFree(z2); - } - break; - } - case 'T': { - Token *p = va_arg(ap, Token*); - memcpy(z, p->z, p->n); - z += p->n; - break; - } - case 'S': { - int len; - SrcList *p = va_arg(ap, SrcList*); - int k = va_arg(ap, int); - assert( p->nSrc>k && k>=0 ); - if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){ - len = strlen(p->a[k].zDatabase); - memcpy(z, p->a[k].zDatabase, len); - z += len; - *(z++) = '.'; - } - len = strlen(p->a[k].zName); - memcpy(z, p->a[k].zName, len); - z += len; - break; - } - default: { - *(z++) = zFormat[i]; - break; - } - } - } - va_end(ap); - if( i>j ){ - memcpy(z, &zFormat[j], i-j); - z += i-j; - } - assert( (z - pParse->zErrMsg) < nByte ); - *z = 0; + pParse->nErr++; + sqliteFree(pParse->zErrMsg); + va_start(ap, zFormat); + pParse->zErrMsg = sqliteVMPrintf(zFormat, ap); + va_end(ap); } /* ** Convert an SQL-style quoted string into a normal string by removing ** the quote characters. The conversion is done in-place. If the Index: src/vdbeaux.c ================================================================== --- src/vdbeaux.c +++ src/vdbeaux.c @@ -785,10 +785,12 @@ } sqliteFree(p->keylistStack); p->keylistStackDepth = 0; p->keylistStack = 0; } + sqliteFree(p->contextStack); + p->contextStack = 0; sqliteFree(p->zErrMsg); p->zErrMsg = 0; } /*