-C Flag\spragmas\slike\svdbe_trace\snow\sreturn\stheir\scurrent\ssetting\sif\sthey\nare\scalled\swith\sno\sarguments.\s(CVS\s1257)
-D 2004-02-21T14:00:29
+C Cleanup\sthe\sprintf\scode\sto\smake\sit\ssmaller\sand\smore\smodular.\r\nFix\sa\smemory\sleak\sin\sthe\snew\sOP_ContextPush\sopcode.\s(CVS\s1258)
+D 2004-02-21T19:02:30
F Makefile.in cfd75c46b335881999333a9e4b982fa8491f200b
F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906
F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd
F src/pager.h 82332878799280145639a48d88cdb4058925e3f6
F src/parse.y 226bbdba2dee362d4b1cacc424bd82f7740071ee
F src/pragma.c a8d43661193ba3114da787f43969d0a34f0ed07c
-F src/printf.c ef1838bd06246d5d323600dc592d337b1c0762b0
+F src/printf.c 2a8bf0fa0dcc1cab62067da3b7e984d56527fe40
F src/random.c 775913e0b7fbd6295d21f12a7bd35b46387c44b2
F src/select.c 9a41dace754f0dab5e991e402c05fa3c24d04f19
F src/shell.c c3d3404fa82bb0808444fda9884d1bb572fd18b9
F src/sqlite.h.in 64f016cd5ce190643a0f47760188fdf4e0b2227e
-F src/sqliteInt.h c647413de0ab8f6e70a1972126da6ed2a84fb5cc
+F src/sqliteInt.h de32ca5481b9ffc30c3ec0d9ff97b505eaa2f016
F src/table.c d845cb101b5afc1f7fea083c99e3d2fa7998d895
F src/tclsqlite.c b84dafe3a8532ff534c36e96bd38880e4b9cedf3
-F src/test1.c 56e9a156df3ad5e4e98df776776e963effc727f7
+F src/test1.c ec7b13dec8faf33b47551b9e9f4e2e0e1a8b122f
F src/test2.c 75819b0f2c63c6a0fd6995445881f2eb94036996
F src/test3.c 30985ebdfaf3ee1462a9b0652d3efbdc8d9798f5
F src/test4.c dcbbbb382626fd466a7c46907f74db35fc8bad64
F src/tokenize.c 6676b946fd8825b67ab52140af4fdc57a70bda48
F src/trigger.c a9927b57c865b6f3df3fb5e40c9824d722660ded
F src/update.c e6eed1a4a429cc28f57533365c72293794c904cf
-F src/util.c 64995b5949a5d377629ffd2598747bc771cade1e
+F src/util.c 8fbed02d3d5d29d4b236772f779778599c911e21
F src/vacuum.c d9e80c2b36ee1f623dbf1bdf3cedad24a23f87ac
F src/vdbe.c f14e8e2ef82cb8480394697c40644d70195598e5
F src/vdbe.h b1b22ffca48f593d375fd846c583679d49c2e5c9
F src/vdbeInt.h b40ff02ce39fd076e6ff3369e19c1bbfe1986682
-F src/vdbeaux.c 2268af6441a55461ec80e5d8f57d77267cd46423
+F src/vdbeaux.c 88ebe3aa963478f3cee9fb6e3d8ca9db1bafacb2
F src/where.c 1302d728bd338c237e6a8282e4e3eadbbdf11e45
F test/all.test 569a92a8ee88f5300c057cc4a8f50fbbc69a3242
F test/attach.test ba8261d38da6b6a7d4f78ec543c548c4418582ef
F www/tclsqlite.tcl b9271d44dcf147a93c98f8ecf28c927307abd6da
F www/vdbe.tcl 9b9095d4495f37697fd1935d10e14c6015e80aa1
F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4
-P 8e3eda2a909bd80b2b14ace36ab44303750a4409
-R a43c4de14dbe7097b5ca3dff718096d1
+P 6a5fb5b89a98307060bb4a92a499b5d3dba74553
+R 23ba520dcd63d47bd6ccb825a335f506
U drh
-Z 74241342cf04355015de5f00644a1b7e
+Z af3bc531c9f40f1ba9bd7423a0477add
*/
#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 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 */
/*
{ '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]))
** 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 */
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' ){
for(idx=0; idx<etNINFO; idx++){
if( c==fmtinfo[idx].fmttype ){
infop = &fmtinfo[idx];
- xtype = infop->type;
+ if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
+ xtype = infop->type;
+ }
break;
}
}
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;
}
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
if( precision>=0 && precision<length ) length = precision;
}
break;
+ case etTOKEN: {
+ Token *pToken = va_arg(ap, Token*);
+ (*func)(arg, pToken->z, 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 && k<pSrc->nSrc );
+ 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;
}
}
if( zExtra ){
- if( xtype==etDYNSTRING ){
- free(zExtra);
- }else{
- sqliteFree(zExtra);
- }
+ sqliteFree(zExtra);
}
}/* End for loop over the format string */
return errorflag ? -1 : count;
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 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){
+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 ){
- 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);
+ if( pM->xRealloc==0 ){
+ nNewChar = pM->nAlloc - pM->nChar - 1;
}else{
- char *z = sqliteRealloc(pM->zText, pM->nAlloc);
- if( z==0 ){
- sqliteFree(pM->zText);
- pM->nChar = 0;
- pM->nAlloc = 0;
+ 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);
}
- pM->zText = z;
}
}
- if( pM->zText ){
+ 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;
+ return base_vprintf((void*(*)(void*,int))realloc, 0,
+ zBuf, sizeof(zBuf), zFormat, ap);
}
/*
-** sqlite_sprintf() works like sprintf() except that it ignores the
+** 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, ...){
+char *sqlite_snprintf(int n, char *zBuf, const char *zFormat, ...){
+ char *z;
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);
+ z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
va_end(ap);
- return sMprintf.nChar;
+ return z;
}
-
-
/*
** The following four routines implement the varargs versions of the
** sqlite_exec() and sqlite_get_table() interfaces. See the sqlite.h
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
-** $Id: util.c,v 1.72 2004/01/07 03:41:04 drh Exp $
+** $Id: util.c,v 1.73 2004/02/21 19:02:31 drh Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
*/
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;
- }
- }
- }
+ pParse->zErrMsg = sqliteVMPrintf(zFormat, ap);
va_end(ap);
- if( i>j ){
- memcpy(z, &zFormat[j], i-j);
- z += i-j;
- }
- assert( (z - pParse->zErrMsg) < nByte );
- *z = 0;
}
/*