tcl_db_pkg.c 74.1 KB
Newer Older
unknown's avatar
unknown committed
1 2 3
/*-
 * See the file LICENSE for redistribution information.
 *
unknown's avatar
unknown committed
4
 * Copyright (c) 1999-2002
unknown's avatar
unknown committed
5 6 7 8 9 10
 *	Sleepycat Software.  All rights reserved.
 */

#include "db_config.h"

#ifndef lint
unknown's avatar
unknown committed
11
static const char revid[] = "$Id: tcl_db_pkg.c,v 11.141 2002/08/14 20:15:47 bostic Exp $";
unknown's avatar
unknown committed
12 13 14 15 16 17 18 19 20 21
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <stdlib.h>
#include <string.h>
#include <tcl.h>
#endif

unknown's avatar
unknown committed
22
#if CONFIG_TEST
unknown's avatar
unknown committed
23
#define	DB_DBM_HSEARCH 1
unknown's avatar
unknown committed
24
#endif
unknown's avatar
unknown committed
25 26

#include "db_int.h"
unknown's avatar
unknown committed
27 28 29 30 31 32
#include "dbinc/db_page.h"
#include "dbinc/hash.h"
#include "dbinc/tcl_db.h"

/* XXX we must declare global data in just one place */
DBTCL_GLOBAL __dbtcl_global;
unknown's avatar
unknown committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

/*
 * Prototypes for procedures defined later in this file:
 */
static int	berkdb_Cmd __P((ClientData, Tcl_Interp *, int,
    Tcl_Obj * CONST*));
static int	bdb_EnvOpen __P((Tcl_Interp *, int, Tcl_Obj * CONST*,
    DBTCL_INFO *, DB_ENV **));
static int	bdb_DbOpen __P((Tcl_Interp *, int, Tcl_Obj * CONST*,
    DBTCL_INFO *, DB **));
static int	bdb_DbRemove __P((Tcl_Interp *, int, Tcl_Obj * CONST*));
static int	bdb_DbRename __P((Tcl_Interp *, int, Tcl_Obj * CONST*));
static int	bdb_DbUpgrade __P((Tcl_Interp *, int, Tcl_Obj * CONST*));
static int	bdb_DbVerify __P((Tcl_Interp *, int, Tcl_Obj * CONST*));
static int	bdb_Version __P((Tcl_Interp *, int, Tcl_Obj * CONST*));
static int	bdb_Handles __P((Tcl_Interp *, int, Tcl_Obj * CONST*));

unknown's avatar
unknown committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63
static int	tcl_bt_compare __P((DB *, const DBT *, const DBT *));
static int	tcl_compare_callback __P((DB *, const DBT *, const DBT *,
    Tcl_Obj *, char *));
static int	tcl_dup_compare __P((DB *, const DBT *, const DBT *));
static u_int32_t tcl_h_hash __P((DB *, const void *, u_int32_t));
static int	tcl_rep_send __P((DB_ENV *,
    const DBT *, const DBT *, int, u_int32_t));

#ifdef TEST_ALLOC
static void *	tcl_db_malloc __P((size_t));
static void *	tcl_db_realloc __P((void *, size_t));
static void	tcl_db_free __P((void *));
#endif

unknown's avatar
unknown committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/*
 * Db_tcl_Init --
 *
 * This is a package initialization procedure, which is called by Tcl when
 * this package is to be added to an interpreter.  The name is based on the
 * name of the shared library, currently libdb_tcl-X.Y.so, which Tcl uses
 * to determine the name of this function.
 */
int
Db_tcl_Init(interp)
	Tcl_Interp *interp;		/* Interpreter in which the package is
					 * to be made available. */
{
	int code;

	code = Tcl_PkgProvide(interp, "Db_tcl", "1.0");
	if (code != TCL_OK)
		return (code);

	Tcl_CreateObjCommand(interp, "berkdb", (Tcl_ObjCmdProc *)berkdb_Cmd,
	    (ClientData)0, NULL);
	/*
	 * Create shared global debugging variables
	 */
	Tcl_LinkVar(interp, "__debug_on", (char *)&__debug_on, TCL_LINK_INT);
	Tcl_LinkVar(interp, "__debug_print", (char *)&__debug_print,
	    TCL_LINK_INT);
	Tcl_LinkVar(interp, "__debug_stop", (char *)&__debug_stop,
	    TCL_LINK_INT);
	Tcl_LinkVar(interp, "__debug_test", (char *)&__debug_test,
	    TCL_LINK_INT);
	LIST_INIT(&__db_infohead);
	return (TCL_OK);
}

/*
 * berkdb_cmd --
 *	Implements the "berkdb" command.
 *	This command supports three sub commands:
 *	berkdb version - Returns a list {major minor patch}
 *	berkdb env - Creates a new DB_ENV and returns a binding
 *	  to a new command of the form dbenvX, where X is an
 *	  integer starting at 0 (dbenv0, dbenv1, ...)
 *	berkdb open - Creates a new DB (optionally within
 *	  the given environment.  Returns a binding to a new
 *	  command of the form dbX, where X is an integer
 *	  starting at 0 (db0, db1, ...)
 */
static int
berkdb_Cmd(notused, interp, objc, objv)
	ClientData notused;		/* Not used. */
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
{
	static char *berkdbcmds[] = {
unknown's avatar
unknown committed
120 121 122 123 124
#if CONFIG_TEST
		"dbverify",
		"handles",
		"upgrade",
#endif
unknown's avatar
unknown committed
125 126 127 128 129 130
		"dbremove",
		"dbrename",
		"env",
		"envremove",
		"open",
		"version",
unknown's avatar
unknown committed
131
#if CONFIG_TEST
unknown's avatar
unknown committed
132 133 134 135 136
		/* All below are compatibility functions */
		"hcreate",	"hsearch",	"hdestroy",
		"dbminit",	"fetch",	"store",
		"delete",	"firstkey",	"nextkey",
		"ndbm_open",	"dbmclose",
unknown's avatar
unknown committed
137
#endif
unknown's avatar
unknown committed
138 139 140 141 142 143 144 145 146
		/* All below are convenience functions */
		"rand",		"random_int",	"srand",
		"debug_check",
		NULL
	};
	/*
	 * All commands enums below ending in X are compatibility
	 */
	enum berkdbcmds {
unknown's avatar
unknown committed
147 148 149 150 151
#if CONFIG_TEST
		BDB_DBVERIFY,
		BDB_HANDLES,
		BDB_UPGRADE,
#endif
unknown's avatar
unknown committed
152 153 154 155 156 157
		BDB_DBREMOVE,
		BDB_DBRENAME,
		BDB_ENV,
		BDB_ENVREMOVE,
		BDB_OPEN,
		BDB_VERSION,
unknown's avatar
unknown committed
158
#if CONFIG_TEST
unknown's avatar
unknown committed
159 160 161 162
		BDB_HCREATEX,	BDB_HSEARCHX,	BDB_HDESTROYX,
		BDB_DBMINITX,	BDB_FETCHX,	BDB_STOREX,
		BDB_DELETEX,	BDB_FIRSTKEYX,	BDB_NEXTKEYX,
		BDB_NDBMOPENX,	BDB_DBMCLOSEX,
unknown's avatar
unknown committed
163
#endif
unknown's avatar
unknown committed
164 165 166 167 168 169 170
		BDB_RANDX,	BDB_RAND_INTX,	BDB_SRANDX,
		BDB_DBGCKX
	};
	static int env_id = 0;
	static int db_id = 0;

	DB *dbp;
unknown's avatar
unknown committed
171
#if CONFIG_TEST
unknown's avatar
unknown committed
172
	DBM *ndbmp;
unknown's avatar
unknown committed
173 174
	static int ndbm_id = 0;
#endif
unknown's avatar
unknown committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	DBTCL_INFO *ip;
	DB_ENV *envp;
	Tcl_Obj *res;
	int cmdindex, result;
	char newname[MSG_SIZE];

	COMPQUIET(notused, NULL);

	Tcl_ResetResult(interp);
	memset(newname, 0, MSG_SIZE);
	result = TCL_OK;
	if (objc <= 1) {
		Tcl_WrongNumArgs(interp, 1, objv, "command cmdargs");
		return (TCL_ERROR);
	}

	/*
	 * Get the command name index from the object based on the berkdbcmds
	 * defined above.
	 */
	if (Tcl_GetIndexFromObj(interp,
	    objv[1], berkdbcmds, "command", TCL_EXACT, &cmdindex) != TCL_OK)
		return (IS_HELP(objv[1]));
	res = NULL;
	switch ((enum berkdbcmds)cmdindex) {
unknown's avatar
unknown committed
200 201 202
#if CONFIG_TEST
	case BDB_DBVERIFY:
		result = bdb_DbVerify(interp, objc, objv);
unknown's avatar
unknown committed
203 204 205 206
		break;
	case BDB_HANDLES:
		result = bdb_Handles(interp, objc, objv);
		break;
unknown's avatar
unknown committed
207 208 209 210 211 212 213 214
	case BDB_UPGRADE:
		result = bdb_DbUpgrade(interp, objc, objv);
		break;
#endif
	case BDB_VERSION:
		_debug_check();
		result = bdb_Version(interp, objc, objv);
		break;
unknown's avatar
unknown committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	case BDB_ENV:
		snprintf(newname, sizeof(newname), "env%d", env_id);
		ip = _NewInfo(interp, NULL, newname, I_ENV);
		if (ip != NULL) {
			result = bdb_EnvOpen(interp, objc, objv, ip, &envp);
			if (result == TCL_OK && envp != NULL) {
				env_id++;
				Tcl_CreateObjCommand(interp, newname,
				    (Tcl_ObjCmdProc *)env_Cmd,
				    (ClientData)envp, NULL);
				/* Use ip->i_name - newname is overwritten */
				res =
				    Tcl_NewStringObj(newname, strlen(newname));
				_SetInfoData(ip, envp);
			} else
				_DeleteInfo(ip);
		} else {
			Tcl_SetResult(interp, "Could not set up info",
			    TCL_STATIC);
			result = TCL_ERROR;
		}
		break;
	case BDB_DBREMOVE:
		result = bdb_DbRemove(interp, objc, objv);
		break;
	case BDB_DBRENAME:
		result = bdb_DbRename(interp, objc, objv);
		break;
	case BDB_ENVREMOVE:
		result = tcl_EnvRemove(interp, objc, objv, NULL, NULL);
		break;
	case BDB_OPEN:
		snprintf(newname, sizeof(newname), "db%d", db_id);
		ip = _NewInfo(interp, NULL, newname, I_DB);
		if (ip != NULL) {
			result = bdb_DbOpen(interp, objc, objv, ip, &dbp);
			if (result == TCL_OK && dbp != NULL) {
				db_id++;
				Tcl_CreateObjCommand(interp, newname,
				    (Tcl_ObjCmdProc *)db_Cmd,
				    (ClientData)dbp, NULL);
				/* Use ip->i_name - newname is overwritten */
				res =
				    Tcl_NewStringObj(newname, strlen(newname));
				_SetInfoData(ip, dbp);
			} else
				_DeleteInfo(ip);
		} else {
			Tcl_SetResult(interp, "Could not set up info",
			    TCL_STATIC);
			result = TCL_ERROR;
		}
		break;
unknown's avatar
unknown committed
268
#if CONFIG_TEST
unknown's avatar
unknown committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	case BDB_HCREATEX:
	case BDB_HSEARCHX:
	case BDB_HDESTROYX:
		result = bdb_HCommand(interp, objc, objv);
		break;
	case BDB_DBMINITX:
	case BDB_DBMCLOSEX:
	case BDB_FETCHX:
	case BDB_STOREX:
	case BDB_DELETEX:
	case BDB_FIRSTKEYX:
	case BDB_NEXTKEYX:
		result = bdb_DbmCommand(interp, objc, objv, DBTCL_DBM, NULL);
		break;
	case BDB_NDBMOPENX:
		snprintf(newname, sizeof(newname), "ndbm%d", ndbm_id);
		ip = _NewInfo(interp, NULL, newname, I_NDBM);
		if (ip != NULL) {
			result = bdb_NdbmOpen(interp, objc, objv, &ndbmp);
			if (result == TCL_OK) {
				ndbm_id++;
				Tcl_CreateObjCommand(interp, newname,
				    (Tcl_ObjCmdProc *)ndbm_Cmd,
				    (ClientData)ndbmp, NULL);
				/* Use ip->i_name - newname is overwritten */
				res =
				    Tcl_NewStringObj(newname, strlen(newname));
				_SetInfoData(ip, ndbmp);
			} else
				_DeleteInfo(ip);
		} else {
			Tcl_SetResult(interp, "Could not set up info",
			    TCL_STATIC);
			result = TCL_ERROR;
		}
		break;
unknown's avatar
unknown committed
305
#endif
unknown's avatar
unknown committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	case BDB_RANDX:
	case BDB_RAND_INTX:
	case BDB_SRANDX:
		result = bdb_RandCommand(interp, objc, objv);
		break;
	case BDB_DBGCKX:
		_debug_check();
		res = Tcl_NewIntObj(0);
		break;
	}
	/*
	 * For each different arg call different function to create
	 * new commands (or if version, get/return it).
	 */
	if (result == TCL_OK && res != NULL)
		Tcl_SetObjResult(interp, res);
	return (result);
}

/*
 * bdb_EnvOpen -
 *	Implements the environment open command.
 *	There are many, many options to the open command.
 *	Here is the general flow:
 *
 *	1.  Call db_env_create to create the env handle.
 *	2.  Parse args tracking options.
 *	3.  Make any pre-open setup calls necessary.
unknown's avatar
unknown committed
334
 *	4.  Call DB_ENV->open to open the env.
unknown's avatar
unknown committed
335 336 337 338 339 340 341 342 343 344 345
 *	5.  Return env widget handle to user.
 */
static int
bdb_EnvOpen(interp, objc, objv, ip, env)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
	DBTCL_INFO *ip;			/* Our internal info */
	DB_ENV **env;			/* Environment pointer */
{
	static char *envopen[] = {
unknown's avatar
unknown committed
346 347
#if CONFIG_TEST
		"-auto_commit",
unknown's avatar
unknown committed
348 349 350 351 352 353 354 355 356 357
		"-cdb",
		"-cdb_alldb",
		"-client_timeout",
		"-lock",
		"-lock_conflict",
		"-lock_detect",
		"-lock_max",
		"-lock_max_locks",
		"-lock_max_lockers",
		"-lock_max_objects",
unknown's avatar
unknown committed
358
		"-lock_timeout",
unknown's avatar
unknown committed
359 360 361
		"-log",
		"-log_buffer",
		"-log_max",
unknown's avatar
unknown committed
362
		"-log_regionmax",
unknown's avatar
unknown committed
363 364
		"-mmapsize",
		"-nommap",
unknown's avatar
unknown committed
365
		"-overwrite",
unknown's avatar
unknown committed
366
		"-region_init",
unknown's avatar
unknown committed
367 368 369 370
		"-rep_client",
		"-rep_logsonly",
		"-rep_master",
		"-rep_transport",
unknown's avatar
unknown committed
371 372
		"-server",
		"-server_timeout",
unknown's avatar
unknown committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		"-txn_timeout",
		"-txn_timestamp",
		"-verbose",
		"-wrnosync",
#endif
		"-cachesize",
		"-create",
		"-data_dir",
		"-encryptaes",
		"-encryptany",
		"-errfile",
		"-errpfx",
		"-home",
		"-log_dir",
		"-mode",
		"-private",
		"-recover",
		"-recover_fatal",
unknown's avatar
unknown committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
		"-shm_key",
		"-system_mem",
		"-tmp_dir",
		"-txn",
		"-txn_max",
		"-use_environ",
		"-use_environ_root",
		NULL
	};
	/*
	 * !!!
	 * These have to be in the same order as the above,
	 * which is close to but not quite alphabetical.
	 */
	enum envopen {
unknown's avatar
unknown committed
406 407
#if CONFIG_TEST
		ENV_AUTO_COMMIT,
unknown's avatar
unknown committed
408 409 410 411 412 413 414 415 416 417
		ENV_CDB,
		ENV_CDB_ALLDB,
		ENV_CLIENT_TO,
		ENV_LOCK,
		ENV_CONFLICT,
		ENV_DETECT,
		ENV_LOCK_MAX,
		ENV_LOCK_MAX_LOCKS,
		ENV_LOCK_MAX_LOCKERS,
		ENV_LOCK_MAX_OBJECTS,
unknown's avatar
unknown committed
418
		ENV_LOCK_TIMEOUT,
unknown's avatar
unknown committed
419 420 421
		ENV_LOG,
		ENV_LOG_BUFFER,
		ENV_LOG_MAX,
unknown's avatar
unknown committed
422
		ENV_LOG_REGIONMAX,
unknown's avatar
unknown committed
423 424
		ENV_MMAPSIZE,
		ENV_NOMMAP,
unknown's avatar
unknown committed
425
		ENV_OVERWRITE,
unknown's avatar
unknown committed
426
		ENV_REGION_INIT,
unknown's avatar
unknown committed
427 428 429 430
		ENV_REP_CLIENT,
		ENV_REP_LOGSONLY,
		ENV_REP_MASTER,
		ENV_REP_TRANSPORT,
unknown's avatar
unknown committed
431 432
		ENV_SERVER,
		ENV_SERVER_TO,
unknown's avatar
unknown committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
		ENV_TXN_TIMEOUT,
		ENV_TXN_TIME,
		ENV_VERBOSE,
		ENV_WRNOSYNC,
#endif
		ENV_CACHESIZE,
		ENV_CREATE,
		ENV_DATA_DIR,
		ENV_ENCRYPT_AES,
		ENV_ENCRYPT_ANY,
		ENV_ERRFILE,
		ENV_ERRPFX,
		ENV_HOME,
		ENV_LOG_DIR,
		ENV_MODE,
		ENV_PRIVATE,
		ENV_RECOVER,
		ENV_RECOVER_FATAL,
unknown's avatar
unknown committed
451 452 453 454 455 456
		ENV_SHM_KEY,
		ENV_SYSTEM_MEM,
		ENV_TMP_DIR,
		ENV_TXN,
		ENV_TXN_MAX,
		ENV_USE_ENVIRON,
unknown's avatar
unknown committed
457
		ENV_USE_ENVIRON_ROOT
unknown's avatar
unknown committed
458 459
	};
	Tcl_Obj **myobjv, **myobjv1;
unknown's avatar
unknown committed
460 461 462
	time_t timestamp;
	u_int32_t detect, gbytes, bytes, ncaches, logbufset, logmaxset;
	u_int32_t open_flags, rep_flags, set_flags, size, uintarg;
unknown's avatar
unknown committed
463
	u_int8_t *conflicts;
unknown's avatar
unknown committed
464 465
	int i, intarg, j, mode, myobjc, nmodes, optindex;
	int result, ret, temp;
unknown's avatar
unknown committed
466
	long client_to, server_to, shm;
unknown's avatar
unknown committed
467
	char *arg, *home, *passwd, *server;
unknown's avatar
unknown committed
468 469 470

	result = TCL_OK;
	mode = 0;
unknown's avatar
unknown committed
471
	rep_flags = set_flags = 0;
unknown's avatar
unknown committed
472
	home = NULL;
unknown's avatar
unknown committed
473

unknown's avatar
unknown committed
474 475 476
	/*
	 * XXX
	 * If/when our Tcl interface becomes thread-safe, we should enable
unknown's avatar
unknown committed
477 478 479 480 481 482 483 484 485 486
	 * DB_THREAD here in all cases.  For now, turn it on only when testing
	 * so that we exercise MUTEX_THREAD_LOCK cases.
	 *
	 * Historically, a key stumbling block was the log_get interface,
	 * which could only do relative operations in a non-threaded
	 * environment.  This is no longer an issue, thanks to log cursors,
	 * but we need to look at making sure DBTCL_INFO structs
	 * are safe to share across threads (they're not mutex-protected)
	 * before we declare the Tcl interface thread-safe.  Meanwhile,
	 * there's no strong reason to enable DB_THREAD.
unknown's avatar
unknown committed
487
	 */
unknown's avatar
unknown committed
488 489 490 491 492 493
	open_flags = DB_JOINENV |
#ifdef TEST_THREAD
	    DB_THREAD;
#else
	    0;
#endif
unknown's avatar
unknown committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	logmaxset = logbufset = 0;

	if (objc <= 2) {
		Tcl_WrongNumArgs(interp, 2, objv, "?args?");
		return (TCL_ERROR);
	}

	/*
	 * Server code must go before the call to db_env_create.
	 */
	server = NULL;
	server_to = client_to = 0;
	i = 2;
	while (i < objc) {
		if (Tcl_GetIndexFromObj(interp, objv[i++], envopen, "option",
		    TCL_EXACT, &optindex) != TCL_OK) {
			Tcl_ResetResult(interp);
			continue;
		}
		switch ((enum envopen)optindex) {
unknown's avatar
unknown committed
514
#if CONFIG_TEST
unknown's avatar
unknown committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
		case ENV_SERVER:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-server hostname");
				result = TCL_ERROR;
				break;
			}
			server = Tcl_GetStringFromObj(objv[i++], NULL);
			break;
		case ENV_SERVER_TO:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-server_to secs");
				result = TCL_ERROR;
				break;
			}
			result = Tcl_GetLongFromObj(interp, objv[i++],
			    &server_to);
			break;
		case ENV_CLIENT_TO:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-client_to secs");
				result = TCL_ERROR;
				break;
			}
			result = Tcl_GetLongFromObj(interp, objv[i++],
			    &client_to);
			break;
unknown's avatar
unknown committed
544
#endif
unknown's avatar
unknown committed
545 546 547 548 549 550 551
		default:
			break;
		}
	}
	if (server != NULL) {
		ret = db_env_create(env, DB_CLIENT);
		if (ret)
unknown's avatar
unknown committed
552 553
			return (_ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "db_env_create"));
unknown's avatar
unknown committed
554 555
		(*env)->set_errpfx((*env), ip->i_name);
		(*env)->set_errcall((*env), _ErrorFunc);
unknown's avatar
unknown committed
556
		if ((ret = (*env)->set_rpc_server((*env), NULL, server,
unknown's avatar
unknown committed
557 558 559 560 561 562 563 564 565 566 567
		    client_to, server_to, 0)) != 0) {
			result = TCL_ERROR;
			goto error;
		}
	} else {
		/*
		 * Create the environment handle before parsing the args
		 * since we'll be modifying the environment as we parse.
		 */
		ret = db_env_create(env, 0);
		if (ret)
unknown's avatar
unknown committed
568 569
			return (_ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "db_env_create"));
unknown's avatar
unknown committed
570 571 572 573
		(*env)->set_errpfx((*env), ip->i_name);
		(*env)->set_errcall((*env), _ErrorFunc);
	}

unknown's avatar
unknown committed
574 575 576 577 578 579 580 581 582 583 584
	/* Hang our info pointer on the env handle, so we can do callbacks. */
	(*env)->app_private = ip;

	/*
	 * Use a Tcl-local alloc and free function so that we're sure to
	 * test whether we use umalloc/ufree in the right places.
	 */
#ifdef TEST_ALLOC
	(*env)->set_alloc(*env, tcl_db_malloc, tcl_db_realloc, tcl_db_free);
#endif

unknown's avatar
unknown committed
585 586 587 588 589 590
	/*
	 * Get the command name index from the object based on the bdbcmds
	 * defined above.
	 */
	i = 2;
	while (i < objc) {
unknown's avatar
unknown committed
591
		Tcl_ResetResult(interp);
unknown's avatar
unknown committed
592 593 594 595 596 597 598
		if (Tcl_GetIndexFromObj(interp, objv[i], envopen, "option",
		    TCL_EXACT, &optindex) != TCL_OK) {
			result = IS_HELP(objv[i]);
			goto error;
		}
		i++;
		switch ((enum envopen)optindex) {
unknown's avatar
unknown committed
599
#if CONFIG_TEST
unknown's avatar
unknown committed
600 601 602 603 604 605 606 607
		case ENV_SERVER:
		case ENV_SERVER_TO:
		case ENV_CLIENT_TO:
			/*
			 * Already handled these, skip them and their arg.
			 */
			i++;
			break;
unknown's avatar
unknown committed
608 609 610
		case ENV_AUTO_COMMIT:
			FLD_SET(set_flags, DB_AUTO_COMMIT);
			break;
unknown's avatar
unknown committed
611 612 613 614 615
		case ENV_CDB:
			FLD_SET(open_flags, DB_INIT_CDB | DB_INIT_MPOOL);
			FLD_CLR(open_flags, DB_JOINENV);
			break;
		case ENV_CDB_ALLDB:
unknown's avatar
unknown committed
616
			FLD_SET(set_flags, DB_CDB_ALLDB);
unknown's avatar
unknown committed
617 618 619 620 621
			break;
		case ENV_LOCK:
			FLD_SET(open_flags, DB_INIT_LOCK | DB_INIT_MPOOL);
			FLD_CLR(open_flags, DB_JOINENV);
			break;
unknown's avatar
unknown committed
622
		case ENV_CONFLICT:
unknown's avatar
unknown committed
623
			/*
unknown's avatar
unknown committed
624 625 626 627 628
			 * Get conflict list.  List is:
			 * {nmodes {matrix}}
			 *
			 * Where matrix must be nmodes*nmodes big.
			 * Set up conflicts array to pass.
unknown's avatar
unknown committed
629 630 631 632 633 634 635 636 637
			 */
			result = Tcl_ListObjGetElements(interp, objv[i],
			    &myobjc, &myobjv);
			if (result == TCL_OK)
				i++;
			else
				break;
			if (myobjc != 2) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
638
				    "?-lock_conflict {nmodes {matrix}}?");
unknown's avatar
unknown committed
639 640 641
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
642 643
			result = Tcl_GetIntFromObj(interp, myobjv[0], &nmodes);
			if (result != TCL_OK)
unknown's avatar
unknown committed
644
				break;
unknown's avatar
unknown committed
645 646 647
			result = Tcl_ListObjGetElements(interp, myobjv[1],
			    &myobjc, &myobjv1);
			if (myobjc != (nmodes * nmodes)) {
unknown's avatar
unknown committed
648
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
649
				    "?-lock_conflict {nmodes {matrix}}?");
unknown's avatar
unknown committed
650 651 652
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
653 654 655 656
			size = sizeof(u_int8_t) * nmodes*nmodes;
			ret = __os_malloc(*env, size, &conflicts);
			if (ret != 0) {
				result = TCL_ERROR;
unknown's avatar
unknown committed
657
				break;
unknown's avatar
unknown committed
658 659 660 661 662 663 664 665 666 667
			}
			for (j = 0; j < myobjc; j++) {
				result = Tcl_GetIntFromObj(interp, myobjv1[j],
				    &temp);
				conflicts[j] = temp;
				if (result != TCL_OK) {
					__os_free(NULL, conflicts);
					break;
				}
			}
unknown's avatar
unknown committed
668
			_debug_check();
unknown's avatar
unknown committed
669 670 671 672 673
			ret = (*env)->set_lk_conflicts(*env,
			    (u_int8_t *)conflicts, nmodes);
			__os_free(NULL, conflicts);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_lk_conflicts");
unknown's avatar
unknown committed
674
			break;
unknown's avatar
unknown committed
675
		case ENV_DETECT:
unknown's avatar
unknown committed
676 677
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
678
				    "?-lock_detect policy?");
unknown's avatar
unknown committed
679 680 681
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			if (strcmp(arg, "default") == 0)
				detect = DB_LOCK_DEFAULT;
			else if (strcmp(arg, "expire") == 0)
				detect = DB_LOCK_EXPIRE;
			else if (strcmp(arg, "maxlocks") == 0)
				detect = DB_LOCK_MAXLOCKS;
			else if (strcmp(arg, "minlocks") == 0)
				detect = DB_LOCK_MINLOCKS;
			else if (strcmp(arg, "minwrites") == 0)
				detect = DB_LOCK_MINWRITE;
			else if (strcmp(arg, "oldest") == 0)
				detect = DB_LOCK_OLDEST;
			else if (strcmp(arg, "youngest") == 0)
				detect = DB_LOCK_YOUNGEST;
			else if (strcmp(arg, "random") == 0)
				detect = DB_LOCK_RANDOM;
			else {
				Tcl_AddErrorInfo(interp,
				    "lock_detect: illegal policy");
				result = TCL_ERROR;
				break;
unknown's avatar
unknown committed
704
			}
unknown's avatar
unknown committed
705 706 707 708
			_debug_check();
			ret = (*env)->set_lk_detect(*env, detect);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "lock_detect");
unknown's avatar
unknown committed
709
			break;
unknown's avatar
unknown committed
710 711 712 713
		case ENV_LOCK_MAX:
		case ENV_LOCK_MAX_LOCKS:
		case ENV_LOCK_MAX_LOCKERS:
		case ENV_LOCK_MAX_OBJECTS:
unknown's avatar
unknown committed
714 715
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
716
				    "?-lock_max max?");
unknown's avatar
unknown committed
717 718 719
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
720
			result = _GetUInt32(interp, objv[i++], &uintarg);
unknown's avatar
unknown committed
721 722
			if (result == TCL_OK) {
				_debug_check();
unknown's avatar
unknown committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
				switch ((enum envopen)optindex) {
				case ENV_LOCK_MAX:
					ret = (*env)->set_lk_max(*env,
					    uintarg);
					break;
				case ENV_LOCK_MAX_LOCKS:
					ret = (*env)->set_lk_max_locks(*env,
					    uintarg);
					break;
				case ENV_LOCK_MAX_LOCKERS:
					ret = (*env)->set_lk_max_lockers(*env,
					    uintarg);
					break;
				case ENV_LOCK_MAX_OBJECTS:
					ret = (*env)->set_lk_max_objects(*env,
					    uintarg);
					break;
				default:
					break;
				}
				result = _ReturnSetup(interp, ret,
				    DB_RETOK_STD(ret), "lock_max");
			}
			break;
		case ENV_TXN_TIME:
		case ENV_TXN_TIMEOUT:
		case ENV_LOCK_TIMEOUT:
unknown's avatar
unknown committed
750 751
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
752
				    "?-txn_timestamp time?");
unknown's avatar
unknown committed
753 754 755
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
756 757 758
			result = Tcl_GetLongFromObj(interp, objv[i++],
			    (long *)&timestamp);
			if (result == TCL_OK) {
unknown's avatar
unknown committed
759
				_debug_check();
unknown's avatar
unknown committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
				if (optindex == ENV_TXN_TIME)
					ret = (*env)->
					    set_tx_timestamp(*env, &timestamp);
				else
					ret = (*env)->set_timeout(*env,
					    (db_timeout_t)timestamp,
					    optindex == ENV_TXN_TIMEOUT ?
					    DB_SET_TXN_TIMEOUT :
					    DB_SET_LOCK_TIMEOUT);
				result = _ReturnSetup(interp, ret,
				    DB_RETOK_STD(ret), "txn_timestamp");
			}
			break;
		case ENV_LOG:
			FLD_SET(open_flags, DB_INIT_LOG | DB_INIT_MPOOL);
			FLD_CLR(open_flags, DB_JOINENV);
unknown's avatar
unknown committed
776 777 778 779 780 781 782 783
			break;
		case ENV_LOG_BUFFER:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-log_buffer size?");
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
784
			result = _GetUInt32(interp, objv[i++], &uintarg);
unknown's avatar
unknown committed
785 786
			if (result == TCL_OK) {
				_debug_check();
unknown's avatar
unknown committed
787 788 789
				ret = (*env)->set_lg_bsize(*env, uintarg);
				result = _ReturnSetup(interp, ret,
				    DB_RETOK_STD(ret), "log_bsize");
unknown's avatar
unknown committed
790 791 792 793
				logbufset = 1;
				if (logmaxset) {
					_debug_check();
					ret = (*env)->set_lg_max(*env,
unknown's avatar
unknown committed
794
					    logmaxset);
unknown's avatar
unknown committed
795
					result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
796
					    DB_RETOK_STD(ret), "log_max");
unknown's avatar
unknown committed
797 798 799 800 801
					logmaxset = 0;
					logbufset = 0;
				}
			}
			break;
unknown's avatar
unknown committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
		case ENV_LOG_MAX:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-log_max max?");
				result = TCL_ERROR;
				break;
			}
			result = _GetUInt32(interp, objv[i++], &uintarg);
			if (result == TCL_OK && logbufset) {
				_debug_check();
				ret = (*env)->set_lg_max(*env, uintarg);
				result = _ReturnSetup(interp, ret,
				    DB_RETOK_STD(ret), "log_max");
				logbufset = 0;
			} else
				logmaxset = uintarg;
			break;
		case ENV_LOG_REGIONMAX:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-log_regionmax size?");
				result = TCL_ERROR;
				break;
			}
			result = _GetUInt32(interp, objv[i++], &uintarg);
			if (result == TCL_OK) {
				_debug_check();
				ret = (*env)->set_lg_regionmax(*env, uintarg);
				result =
				    _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
					"log_regionmax");
			}
			break;
		case ENV_MMAPSIZE:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-mmapsize size?");
				result = TCL_ERROR;
				break;
			}
			result = Tcl_GetIntFromObj(interp, objv[i++], &intarg);
			if (result == TCL_OK) {
				_debug_check();
				ret = (*env)->set_mp_mmapsize(*env,
				    (size_t)intarg);
				result = _ReturnSetup(interp, ret,
				    DB_RETOK_STD(ret), "mmapsize");
			}
			break;
		case ENV_NOMMAP:
			FLD_SET(set_flags, DB_NOMMAP);
			break;
		case ENV_OVERWRITE:
			FLD_SET(set_flags, DB_OVERWRITE);
			break;
		case ENV_REGION_INIT:
			_debug_check();
			ret = (*env)->set_flags(*env, DB_REGION_INIT, 1);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "region_init");
			break;
		case ENV_REP_CLIENT:
			rep_flags = DB_REP_CLIENT;
			break;
		case ENV_REP_LOGSONLY:
			rep_flags = DB_REP_LOGSONLY;
			break;
		case ENV_REP_MASTER:
			rep_flags = DB_REP_MASTER;
			break;
		case ENV_REP_TRANSPORT:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-rep_transport {envid sendproc}");
				result = TCL_ERROR;
				break;
			}

unknown's avatar
unknown committed
880
			/*
unknown's avatar
unknown committed
881 882 883 884 885 886
			 * Store the objects containing the machine ID
			 * and the procedure name.  We don't need to crack
			 * the send procedure out now, but we do convert the
			 * machine ID to an int, since set_rep_transport needs
			 * it.  Even so, it'll be easier later to deal with
			 * the Tcl_Obj *, so we save that, not the int.
unknown's avatar
unknown committed
887
			 *
unknown's avatar
unknown committed
888 889 890
			 * Note that we Tcl_IncrRefCount both objects
			 * independently;  Tcl is free to discard the list
			 * that they're bundled into.
unknown's avatar
unknown committed
891
			 */
unknown's avatar
unknown committed
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
			result = Tcl_ListObjGetElements(interp, objv[i++],
			    &myobjc, &myobjv);
			if (myobjc != 2) {
				Tcl_SetResult(interp,
				    "List must be {envid sendproc}",
				    TCL_STATIC);
				result = TCL_ERROR;
				break;
			}

			/*
			 * Check that the machine ID is an int.  Note that
			 * we do want to use GetIntFromObj;  the machine
			 * ID is explicitly an int, not a u_int32_t.
			 */
			ip->i_rep_eid = myobjv[0];
			Tcl_IncrRefCount(ip->i_rep_eid);
			result = Tcl_GetIntFromObj(interp,
			    ip->i_rep_eid, &intarg);
			if (result != TCL_OK)
				break;

			ip->i_rep_send = myobjv[1];
			Tcl_IncrRefCount(ip->i_rep_send);
			_debug_check();
			ret = (*env)->set_rep_transport(*env,
			    intarg, tcl_rep_send);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_rep_transport");
			break;
		case ENV_VERBOSE:
unknown's avatar
unknown committed
923 924 925 926 927 928 929 930
			result = Tcl_ListObjGetElements(interp, objv[i],
			    &myobjc, &myobjv);
			if (result == TCL_OK)
				i++;
			else
				break;
			if (myobjc != 2) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
931
				    "?-verbose {which on|off}?");
unknown's avatar
unknown committed
932 933 934
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
			result = tcl_EnvVerbose(interp, *env,
			    myobjv[0], myobjv[1]);
			break;
		case ENV_WRNOSYNC:
			FLD_SET(set_flags, DB_TXN_WRITE_NOSYNC);
			break;
#endif
		case ENV_TXN:
			FLD_SET(open_flags, DB_INIT_LOCK |
			    DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN);
			FLD_CLR(open_flags, DB_JOINENV);
			/* Make sure we have an arg to check against! */
			if (i < objc) {
				arg = Tcl_GetStringFromObj(objv[i], NULL);
				if (strcmp(arg, "nosync") == 0) {
					FLD_SET(set_flags, DB_TXN_NOSYNC);
					i++;
				}
			}
			break;
		case ENV_CREATE:
			FLD_SET(open_flags, DB_CREATE | DB_INIT_MPOOL);
			FLD_CLR(open_flags, DB_JOINENV);
			break;
		case ENV_ENCRYPT_AES:
			/* Make sure we have an arg to check against! */
			if (i >= objc) {
unknown's avatar
unknown committed
962
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
963
				    "?-encryptaes passwd?");
unknown's avatar
unknown committed
964 965 966
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
967 968 969 970 971 972 973 974 975 976 977
			passwd = Tcl_GetStringFromObj(objv[i++], NULL);
			_debug_check();
			ret = (*env)->set_encrypt(*env, passwd, DB_ENCRYPT_AES);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_encrypt");
			break;
		case ENV_ENCRYPT_ANY:
			/* Make sure we have an arg to check against! */
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-encryptany passwd?");
unknown's avatar
unknown committed
978 979 980
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
981
			passwd = Tcl_GetStringFromObj(objv[i++], NULL);
unknown's avatar
unknown committed
982
			_debug_check();
unknown's avatar
unknown committed
983 984 985
			ret = (*env)->set_encrypt(*env, passwd, 0);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_encrypt");
unknown's avatar
unknown committed
986
			break;
unknown's avatar
unknown committed
987 988
		case ENV_HOME:
			/* Make sure we have an arg to check against! */
unknown's avatar
unknown committed
989 990
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
991
				    "?-home dir?");
unknown's avatar
unknown committed
992 993 994
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
995 996 997 998 999 1000
			home = Tcl_GetStringFromObj(objv[i++], NULL);
			break;
		case ENV_MODE:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-mode mode?");
unknown's avatar
unknown committed
1001 1002 1003
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
1004 1005 1006 1007 1008 1009 1010
			/*
			 * Don't need to check result here because
			 * if TCL_ERROR, the error message is already
			 * set up, and we'll bail out below.  If ok,
			 * the mode is set and we go on.
			 */
			result = Tcl_GetIntFromObj(interp, objv[i++], &mode);
unknown's avatar
unknown committed
1011
			break;
unknown's avatar
unknown committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
		case ENV_PRIVATE:
			FLD_SET(open_flags, DB_PRIVATE | DB_INIT_MPOOL);
			FLD_CLR(open_flags, DB_JOINENV);
			break;
		case ENV_RECOVER:
			FLD_SET(open_flags, DB_RECOVER);
			break;
		case ENV_RECOVER_FATAL:
			FLD_SET(open_flags, DB_RECOVER_FATAL);
			break;
		case ENV_SYSTEM_MEM:
			FLD_SET(open_flags, DB_SYSTEM_MEM);
			break;
		case ENV_USE_ENVIRON_ROOT:
			FLD_SET(open_flags, DB_USE_ENVIRON_ROOT);
			break;
		case ENV_USE_ENVIRON:
			FLD_SET(open_flags, DB_USE_ENVIRON);
			break;
		case ENV_CACHESIZE:
			result = Tcl_ListObjGetElements(interp, objv[i],
			    &myobjc, &myobjv);
			if (result == TCL_OK)
				i++;
			else
				break;
			if (myobjc != 3) {
unknown's avatar
unknown committed
1039
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
1040
				    "?-cachesize {gbytes bytes ncaches}?");
unknown's avatar
unknown committed
1041 1042 1043
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
			result = _GetUInt32(interp, myobjv[0], &gbytes);
			if (result != TCL_OK)
				break;
			result = _GetUInt32(interp, myobjv[1], &bytes);
			if (result != TCL_OK)
				break;
			result = _GetUInt32(interp, myobjv[2], &ncaches);
			if (result != TCL_OK)
				break;
			_debug_check();
			ret = (*env)->set_cachesize(*env, gbytes, bytes,
			    ncaches);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_cachesize");
unknown's avatar
unknown committed
1058
			break;
unknown's avatar
unknown committed
1059
		case ENV_SHM_KEY:
unknown's avatar
unknown committed
1060 1061
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
1062
				    "?-shm_key key?");
unknown's avatar
unknown committed
1063 1064 1065
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
1066
			result = Tcl_GetLongFromObj(interp, objv[i++], &shm);
unknown's avatar
unknown committed
1067 1068
			if (result == TCL_OK) {
				_debug_check();
unknown's avatar
unknown committed
1069 1070 1071
				ret = (*env)->set_shm_key(*env, shm);
				result = _ReturnSetup(interp, ret,
				    DB_RETOK_STD(ret), "shm_key");
unknown's avatar
unknown committed
1072 1073
			}
			break;
unknown's avatar
unknown committed
1074
		case ENV_TXN_MAX:
unknown's avatar
unknown committed
1075 1076
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
1077
				    "?-txn_max max?");
unknown's avatar
unknown committed
1078 1079 1080
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
1081
			result = _GetUInt32(interp, objv[i++], &uintarg);
unknown's avatar
unknown committed
1082 1083
			if (result == TCL_OK) {
				_debug_check();
unknown's avatar
unknown committed
1084
				ret = (*env)->set_tx_max(*env, uintarg);
unknown's avatar
unknown committed
1085
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1086
				    DB_RETOK_STD(ret), "txn_max");
unknown's avatar
unknown committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
			}
			break;
		case ENV_ERRFILE:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-errfile file");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			/*
			 * If the user already set one, close it.
			 */
			if (ip->i_err != NULL)
				fclose(ip->i_err);
			ip->i_err = fopen(arg, "a");
			if (ip->i_err != NULL) {
				_debug_check();
				(*env)->set_errfile(*env, ip->i_err);
			}
			break;
		case ENV_ERRPFX:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-errpfx prefix");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			/*
			 * If the user already set one, free it.
			 */
			if (ip->i_errpfx != NULL)
unknown's avatar
unknown committed
1120
				__os_free(NULL, ip->i_errpfx);
unknown's avatar
unknown committed
1121 1122 1123
			if ((ret =
			    __os_strdup(*env, arg, &ip->i_errpfx)) != 0) {
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1124
				    DB_RETOK_STD(ret), "__os_strdup");
unknown's avatar
unknown committed
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
				break;
			}
			if (ip->i_errpfx != NULL) {
				_debug_check();
				(*env)->set_errpfx(*env, ip->i_errpfx);
			}
			break;
		case ENV_DATA_DIR:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-data_dir dir");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			_debug_check();
			ret = (*env)->set_data_dir(*env, arg);
unknown's avatar
unknown committed
1142 1143
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_data_dir");
unknown's avatar
unknown committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
			break;
		case ENV_LOG_DIR:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-log_dir dir");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			_debug_check();
			ret = (*env)->set_lg_dir(*env, arg);
unknown's avatar
unknown committed
1155 1156
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_lg_dir");
unknown's avatar
unknown committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
			break;
		case ENV_TMP_DIR:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-tmp_dir dir");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			_debug_check();
			ret = (*env)->set_tmp_dir(*env, arg);
unknown's avatar
unknown committed
1168 1169
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_tmp_dir");
unknown's avatar
unknown committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
			break;
		}
		/*
		 * If, at any time, parsing the args we get an error,
		 * bail out and return.
		 */
		if (result != TCL_OK)
			goto error;
	}

	/*
	 * We have to check this here.  We want to set the log buffer
	 * size first, if it is specified.  So if the user did so,
	 * then we took care of it above.  But, if we get out here and
	 * logmaxset is non-zero, then they set the log_max without
	 * resetting the log buffer size, so we now have to do the
	 * call to set_lg_max, since we didn't do it above.
	 */
	if (logmaxset) {
		_debug_check();
		ret = (*env)->set_lg_max(*env, (u_int32_t)logmaxset);
unknown's avatar
unknown committed
1191 1192
		result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
		    "log_max");
unknown's avatar
unknown committed
1193 1194 1195 1196 1197
	}

	if (result != TCL_OK)
		goto error;

unknown's avatar
unknown committed
1198 1199 1200 1201
	if (set_flags) {
		ret = (*env)->set_flags(*env, set_flags, 1);
		result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
		    "set_flags");
unknown's avatar
unknown committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
		if (result == TCL_ERROR)
			goto error;
		/*
		 * If we are successful, clear the result so that the
		 * return from set_flags isn't part of the result.
		 */
		Tcl_ResetResult(interp);
	}
	/*
	 * When we get here, we have already parsed all of our args
	 * and made all our calls to set up the environment.  Everything
	 * is okay so far, no errors, if we get here.
	 *
	 * Now open the environment.
	 */
	_debug_check();
	ret = (*env)->open(*env, home, open_flags, mode);
unknown's avatar
unknown committed
1219
	result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "env open");
unknown's avatar
unknown committed
1220

unknown's avatar
unknown committed
1221 1222 1223 1224 1225 1226 1227 1228
	if (rep_flags != 0 && result == TCL_OK) {
		_debug_check();
		ret = (*env)->rep_start(*env, NULL, rep_flags);
		result = _ReturnSetup(interp,
		    ret, DB_RETOK_STD(ret), "rep_start");
	}

error:	if (result == TCL_ERROR) {
unknown's avatar
unknown committed
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
		if (ip->i_err) {
			fclose(ip->i_err);
			ip->i_err = NULL;
		}
		(void)(*env)->close(*env, 0);
		*env = NULL;
	}
	return (result);
}

/*
 * bdb_DbOpen --
 *	Implements the "db_create/db_open" command.
 *	There are many, many options to the open command.
 *	Here is the general flow:
 *
 *	0.  Preparse args to determine if we have -env.
 *	1.  Call db_create to create the db handle.
 *	2.  Parse args tracking options.
 *	3.  Make any pre-open setup calls necessary.
 *	4.  Call DB->open to open the database.
 *	5.  Return db widget handle to user.
 */
static int
bdb_DbOpen(interp, objc, objv, ip, dbp)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
	DBTCL_INFO *ip;			/* Our internal info */
	DB **dbp;			/* DB handle */
{
	static char *bdbenvopen[] = {
		"-env",	NULL
	};
	enum bdbenvopen {
		TCL_DB_ENV0
	};
	static char *bdbopen[] = {
unknown's avatar
unknown committed
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
#if CONFIG_TEST
		"-btcompare",
		"-dirty",
		"-dupcompare",
		"-hashproc",
		"-lorder",
		"-minkey",
		"-nommap",
		"-revsplitoff",
		"-test",
#endif
		"-auto_commit",
unknown's avatar
unknown committed
1279 1280
		"-btree",
		"-cachesize",
unknown's avatar
unknown committed
1281
		"-chksum",
unknown's avatar
unknown committed
1282 1283 1284 1285
		"-create",
		"-delim",
		"-dup",
		"-dupsort",
unknown's avatar
unknown committed
1286 1287 1288
		"-encrypt",
		"-encryptaes",
		"-encryptany",
unknown's avatar
unknown committed
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
		"-env",
		"-errfile",
		"-errpfx",
		"-excl",
		"-extent",
		"-ffactor",
		"-hash",
		"-len",
		"-mode",
		"-nelem",
		"-pad",
		"-pagesize",
		"-queue",
		"-rdonly",
		"-recno",
		"-recnum",
		"-renumber",
		"-snapshot",
		"-source",
		"-truncate",
unknown's avatar
unknown committed
1309
		"-txn",
unknown's avatar
unknown committed
1310 1311 1312 1313 1314
		"-unknown",
		"--",
		NULL
	};
	enum bdbopen {
unknown's avatar
unknown committed
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
#if CONFIG_TEST
		TCL_DB_BTCOMPARE,
		TCL_DB_DIRTY,
		TCL_DB_DUPCOMPARE,
		TCL_DB_HASHPROC,
		TCL_DB_LORDER,
		TCL_DB_MINKEY,
		TCL_DB_NOMMAP,
		TCL_DB_REVSPLIT,
		TCL_DB_TEST,
#endif
		TCL_DB_AUTO_COMMIT,
unknown's avatar
unknown committed
1327 1328
		TCL_DB_BTREE,
		TCL_DB_CACHESIZE,
unknown's avatar
unknown committed
1329
		TCL_DB_CHKSUM,
unknown's avatar
unknown committed
1330 1331 1332 1333
		TCL_DB_CREATE,
		TCL_DB_DELIM,
		TCL_DB_DUP,
		TCL_DB_DUPSORT,
unknown's avatar
unknown committed
1334 1335 1336
		TCL_DB_ENCRYPT,
		TCL_DB_ENCRYPT_AES,
		TCL_DB_ENCRYPT_ANY,
unknown's avatar
unknown committed
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
		TCL_DB_ENV,
		TCL_DB_ERRFILE,
		TCL_DB_ERRPFX,
		TCL_DB_EXCL,
		TCL_DB_EXTENT,
		TCL_DB_FFACTOR,
		TCL_DB_HASH,
		TCL_DB_LEN,
		TCL_DB_MODE,
		TCL_DB_NELEM,
		TCL_DB_PAD,
		TCL_DB_PAGESIZE,
		TCL_DB_QUEUE,
		TCL_DB_RDONLY,
		TCL_DB_RECNO,
		TCL_DB_RECNUM,
		TCL_DB_RENUMBER,
		TCL_DB_SNAPSHOT,
		TCL_DB_SOURCE,
		TCL_DB_TRUNCATE,
unknown's avatar
unknown committed
1357
		TCL_DB_TXN,
unknown's avatar
unknown committed
1358 1359 1360 1361 1362
		TCL_DB_UNKNOWN,
		TCL_DB_ENDARG
	};

	DBTCL_INFO *envip, *errip;
unknown's avatar
unknown committed
1363
	DB_TXN *txn;
unknown's avatar
unknown committed
1364 1365 1366
	DBTYPE type;
	DB_ENV *envp;
	Tcl_Obj **myobjv;
unknown's avatar
unknown committed
1367 1368 1369
	u_int32_t gbytes, bytes, ncaches, open_flags, uintarg;
	int endarg, i, intarg, mode, myobjc;
	int optindex, result, ret, set_err, set_flags, set_pfx, subdblen;
unknown's avatar
unknown committed
1370
	u_char *subdbtmp;
unknown's avatar
unknown committed
1371
	char *arg, *db, *passwd, *subdb, msg[MSG_SIZE];
unknown's avatar
unknown committed
1372 1373

	type = DB_UNKNOWN;
unknown's avatar
unknown committed
1374
	endarg = mode = set_err = set_flags = set_pfx = 0;
unknown's avatar
unknown committed
1375 1376 1377 1378 1379 1380 1381
	result = TCL_OK;
	subdbtmp = NULL;
	db = subdb = NULL;

	/*
	 * XXX
	 * If/when our Tcl interface becomes thread-safe, we should enable
unknown's avatar
unknown committed
1382 1383 1384
	 * DB_THREAD here in all cases.  See comment in bdb_EnvOpen().
	 * For now, just turn it on when testing so that we exercise
	 * MUTEX_THREAD_LOCK cases.
unknown's avatar
unknown committed
1385
	 */
unknown's avatar
unknown committed
1386 1387 1388 1389 1390 1391
	open_flags =
#ifdef TEST_THREAD
	    DB_THREAD;
#else
	    0;
#endif
unknown's avatar
unknown committed
1392
	envp = NULL;
unknown's avatar
unknown committed
1393
	txn = NULL;
unknown's avatar
unknown committed
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433

	if (objc < 2) {
		Tcl_WrongNumArgs(interp, 2, objv, "?args?");
		return (TCL_ERROR);
	}

	/*
	 * We must first parse for the environment flag, since that
	 * is needed for db_create.  Then create the db handle.
	 */
	i = 2;
	while (i < objc) {
		if (Tcl_GetIndexFromObj(interp, objv[i++], bdbenvopen,
		    "option", TCL_EXACT, &optindex) != TCL_OK) {
			/*
			 * Reset the result so we don't get
			 * an errant error message if there is another error.
			 */
			Tcl_ResetResult(interp);
			continue;
		}
		switch ((enum bdbenvopen)optindex) {
		case TCL_DB_ENV0:
			arg = Tcl_GetStringFromObj(objv[i], NULL);
			envp = NAME_TO_ENV(arg);
			if (envp == NULL) {
				Tcl_SetResult(interp,
				    "db open: illegal environment", TCL_STATIC);
				return (TCL_ERROR);
			}
		}
		break;
	}

	/*
	 * Create the db handle before parsing the args
	 * since we'll be modifying the database options as we parse.
	 */
	ret = db_create(dbp, envp, 0);
	if (ret)
unknown's avatar
unknown committed
1434 1435 1436 1437 1438
		return (_ReturnSetup(interp, ret, DB_RETOK_STD(ret),
		    "db_create"));

	/* Hang our info pointer on the DB handle, so we can do callbacks. */
	(*dbp)->api_internal = ip;
unknown's avatar
unknown committed
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468

	/*
	 * XXX Remove restriction when err stuff is not tied to env.
	 *
	 * The DB->set_err* functions actually overwrite in the
	 * environment.  So, if we are explicitly using an env,
	 * don't overwrite what we have already set up.  If we are
	 * not using one, then we set up since we get a private
	 * default env.
	 */
	/* XXX  - remove this conditional if/when err is not tied to env */
	if (envp == NULL) {
		(*dbp)->set_errpfx((*dbp), ip->i_name);
		(*dbp)->set_errcall((*dbp), _ErrorFunc);
	}
	envip = _PtrToInfo(envp); /* XXX */
	/*
	 * If we are using an env, we keep track of err info in the env's ip.
	 * Otherwise use the DB's ip.
	 */
	if (envip)
		errip = envip;
	else
		errip = ip;
	/*
	 * Get the option name index from the object based on the args
	 * defined above.
	 */
	i = 2;
	while (i < objc) {
unknown's avatar
unknown committed
1469
		Tcl_ResetResult(interp);
unknown's avatar
unknown committed
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
		if (Tcl_GetIndexFromObj(interp, objv[i], bdbopen, "option",
		    TCL_EXACT, &optindex) != TCL_OK) {
			arg = Tcl_GetStringFromObj(objv[i], NULL);
			if (arg[0] == '-') {
				result = IS_HELP(objv[i]);
				goto error;
			} else
				Tcl_ResetResult(interp);
			break;
		}
		i++;
		switch ((enum bdbopen)optindex) {
unknown's avatar
unknown committed
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
#if CONFIG_TEST
		case TCL_DB_BTCOMPARE:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-btcompare compareproc");
				result = TCL_ERROR;
				break;
			}

			/*
			 * Store the object containing the procedure name.
			 * We don't need to crack it out now--we'll want
			 * to bundle it up to pass into Tcl_EvalObjv anyway.
			 * Tcl's object refcounting will--I hope--take care
			 * of the memory management here.
			 */
			ip->i_btcompare = objv[i++];
			Tcl_IncrRefCount(ip->i_btcompare);
			_debug_check();
			ret = (*dbp)->set_bt_compare(*dbp, tcl_bt_compare);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_bt_compare");
			break;
		case TCL_DB_DIRTY:
			open_flags |= DB_DIRTY_READ;
			break;
		case TCL_DB_DUPCOMPARE:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-dupcompare compareproc");
				result = TCL_ERROR;
				break;
			}

			/*
			 * Store the object containing the procedure name.
			 * See TCL_DB_BTCOMPARE.
			 */
			ip->i_dupcompare = objv[i++];
			Tcl_IncrRefCount(ip->i_dupcompare);
			_debug_check();
			ret = (*dbp)->set_dup_compare(*dbp, tcl_dup_compare);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_dup_compare");
			break;
		case TCL_DB_HASHPROC:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-hashproc hashproc");
				result = TCL_ERROR;
				break;
			}

			/*
			 * Store the object containing the procedure name.
			 * See TCL_DB_BTCOMPARE.
			 */
			ip->i_hashproc = objv[i++];
			Tcl_IncrRefCount(ip->i_hashproc);
			_debug_check();
			ret = (*dbp)->set_h_hash(*dbp, tcl_h_hash);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_h_hash");
			break;
		case TCL_DB_LORDER:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-lorder 1234|4321");
				result = TCL_ERROR;
				break;
			}
			result = _GetUInt32(interp, objv[i++], &uintarg);
			if (result == TCL_OK) {
				_debug_check();
				ret = (*dbp)->set_lorder(*dbp, uintarg);
				result = _ReturnSetup(interp, ret,
				    DB_RETOK_STD(ret), "set_lorder");
			}
			break;
		case TCL_DB_MINKEY:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-minkey minkey");
				result = TCL_ERROR;
				break;
			}
			result = _GetUInt32(interp, objv[i++], &uintarg);
			if (result == TCL_OK) {
				_debug_check();
				ret = (*dbp)->set_bt_minkey(*dbp, uintarg);
				result = _ReturnSetup(interp, ret,
				    DB_RETOK_STD(ret), "set_bt_minkey");
			}
			break;
		case TCL_DB_NOMMAP:
			open_flags |= DB_NOMMAP;
			break;
		case TCL_DB_REVSPLIT:
			set_flags |= DB_REVSPLITOFF;
			break;
		case TCL_DB_TEST:
			(*dbp)->set_h_hash(*dbp, __ham_test);
			break;
#endif
		case TCL_DB_AUTO_COMMIT:
			open_flags |= DB_AUTO_COMMIT;
			break;
unknown's avatar
unknown committed
1589 1590 1591 1592 1593 1594
		case TCL_DB_ENV:
			/*
			 * Already parsed this, skip it and the env pointer.
			 */
			i++;
			continue;
unknown's avatar
unknown committed
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
		case TCL_DB_TXN:
			if (i > (objc - 1)) {
				Tcl_WrongNumArgs(interp, 2, objv, "?-txn id?");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			txn = NAME_TO_TXN(arg);
			if (txn == NULL) {
				snprintf(msg, MSG_SIZE,
				    "Put: Invalid txn: %s\n", arg);
				Tcl_SetResult(interp, msg, TCL_VOLATILE);
				result = TCL_ERROR;
			}
			break;
unknown's avatar
unknown committed
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
		case TCL_DB_BTREE:
			if (type != DB_UNKNOWN) {
				Tcl_SetResult(interp,
				    "Too many DB types specified", TCL_STATIC);
				result = TCL_ERROR;
				goto error;
			}
			type = DB_BTREE;
			break;
		case TCL_DB_HASH:
			if (type != DB_UNKNOWN) {
				Tcl_SetResult(interp,
				    "Too many DB types specified", TCL_STATIC);
				result = TCL_ERROR;
				goto error;
			}
			type = DB_HASH;
			break;
		case TCL_DB_RECNO:
			if (type != DB_UNKNOWN) {
				Tcl_SetResult(interp,
				    "Too many DB types specified", TCL_STATIC);
				result = TCL_ERROR;
				goto error;
			}
			type = DB_RECNO;
			break;
		case TCL_DB_QUEUE:
			if (type != DB_UNKNOWN) {
				Tcl_SetResult(interp,
				    "Too many DB types specified", TCL_STATIC);
				result = TCL_ERROR;
				goto error;
			}
			type = DB_QUEUE;
			break;
		case TCL_DB_UNKNOWN:
			if (type != DB_UNKNOWN) {
				Tcl_SetResult(interp,
				    "Too many DB types specified", TCL_STATIC);
				result = TCL_ERROR;
				goto error;
			}
			break;
		case TCL_DB_CREATE:
			open_flags |= DB_CREATE;
			break;
		case TCL_DB_EXCL:
			open_flags |= DB_EXCL;
			break;
		case TCL_DB_RDONLY:
			open_flags |= DB_RDONLY;
			break;
		case TCL_DB_TRUNCATE:
			open_flags |= DB_TRUNCATE;
			break;
		case TCL_DB_MODE:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-mode mode?");
				result = TCL_ERROR;
				break;
			}
			/*
			 * Don't need to check result here because
			 * if TCL_ERROR, the error message is already
			 * set up, and we'll bail out below.  If ok,
			 * the mode is set and we go on.
			 */
			result = Tcl_GetIntFromObj(interp, objv[i++], &mode);
			break;
		case TCL_DB_DUP:
unknown's avatar
unknown committed
1682
			set_flags |= DB_DUP;
unknown's avatar
unknown committed
1683 1684
			break;
		case TCL_DB_DUPSORT:
unknown's avatar
unknown committed
1685
			set_flags |= DB_DUPSORT;
unknown's avatar
unknown committed
1686 1687
			break;
		case TCL_DB_RECNUM:
unknown's avatar
unknown committed
1688
			set_flags |= DB_RECNUM;
unknown's avatar
unknown committed
1689 1690
			break;
		case TCL_DB_RENUMBER:
unknown's avatar
unknown committed
1691
			set_flags |= DB_RENUMBER;
unknown's avatar
unknown committed
1692 1693
			break;
		case TCL_DB_SNAPSHOT:
unknown's avatar
unknown committed
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
			set_flags |= DB_SNAPSHOT;
			break;
		case TCL_DB_CHKSUM:
			set_flags |= DB_CHKSUM_SHA1;
			break;
		case TCL_DB_ENCRYPT:
			set_flags |= DB_ENCRYPT;
			break;
		case TCL_DB_ENCRYPT_AES:
			/* Make sure we have an arg to check against! */
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-encryptaes passwd?");
				result = TCL_ERROR;
				break;
			}
			passwd = Tcl_GetStringFromObj(objv[i++], NULL);
			_debug_check();
			ret = (*dbp)->set_encrypt(*dbp, passwd, DB_ENCRYPT_AES);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_encrypt");
unknown's avatar
unknown committed
1715
			break;
unknown's avatar
unknown committed
1716 1717
		case TCL_DB_ENCRYPT_ANY:
			/* Make sure we have an arg to check against! */
unknown's avatar
unknown committed
1718 1719
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
1720
				    "?-encryptany passwd?");
unknown's avatar
unknown committed
1721 1722 1723
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
1724 1725 1726 1727 1728
			passwd = Tcl_GetStringFromObj(objv[i++], NULL);
			_debug_check();
			ret = (*dbp)->set_encrypt(*dbp, passwd, 0);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_encrypt");
unknown's avatar
unknown committed
1729
			break;
unknown's avatar
unknown committed
1730
		case TCL_DB_FFACTOR:
unknown's avatar
unknown committed
1731 1732
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
1733
				    "-ffactor density");
unknown's avatar
unknown committed
1734 1735 1736
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
1737
			result = _GetUInt32(interp, objv[i++], &uintarg);
unknown's avatar
unknown committed
1738 1739
			if (result == TCL_OK) {
				_debug_check();
unknown's avatar
unknown committed
1740
				ret = (*dbp)->set_h_ffactor(*dbp, uintarg);
unknown's avatar
unknown committed
1741
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1742
				    DB_RETOK_STD(ret), "set_h_ffactor");
unknown's avatar
unknown committed
1743 1744
			}
			break;
unknown's avatar
unknown committed
1745
		case TCL_DB_NELEM:
unknown's avatar
unknown committed
1746 1747
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
unknown's avatar
unknown committed
1748
				    "-nelem nelem");
unknown's avatar
unknown committed
1749 1750 1751
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
1752
			result = _GetUInt32(interp, objv[i++], &uintarg);
unknown's avatar
unknown committed
1753 1754
			if (result == TCL_OK) {
				_debug_check();
unknown's avatar
unknown committed
1755
				ret = (*dbp)->set_h_nelem(*dbp, uintarg);
unknown's avatar
unknown committed
1756
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1757
				    DB_RETOK_STD(ret), "set_h_nelem");
unknown's avatar
unknown committed
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
			}
			break;
		case TCL_DB_DELIM:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-delim delim");
				result = TCL_ERROR;
				break;
			}
			result = Tcl_GetIntFromObj(interp, objv[i++], &intarg);
			if (result == TCL_OK) {
				_debug_check();
				ret = (*dbp)->set_re_delim(*dbp, intarg);
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1772
				    DB_RETOK_STD(ret), "set_re_delim");
unknown's avatar
unknown committed
1773 1774 1775 1776 1777 1778 1779 1780 1781
			}
			break;
		case TCL_DB_LEN:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-len length");
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
1782
			result = _GetUInt32(interp, objv[i++], &uintarg);
unknown's avatar
unknown committed
1783 1784
			if (result == TCL_OK) {
				_debug_check();
unknown's avatar
unknown committed
1785
				ret = (*dbp)->set_re_len(*dbp, uintarg);
unknown's avatar
unknown committed
1786
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1787
				    DB_RETOK_STD(ret), "set_re_len");
unknown's avatar
unknown committed
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
			}
			break;
		case TCL_DB_PAD:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-pad pad");
				result = TCL_ERROR;
				break;
			}
			result = Tcl_GetIntFromObj(interp, objv[i++], &intarg);
			if (result == TCL_OK) {
				_debug_check();
				ret = (*dbp)->set_re_pad(*dbp, intarg);
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1802
				    DB_RETOK_STD(ret), "set_re_pad");
unknown's avatar
unknown committed
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
			}
			break;
		case TCL_DB_SOURCE:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-source file");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			_debug_check();
			ret = (*dbp)->set_re_source(*dbp, arg);
unknown's avatar
unknown committed
1815 1816
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_re_source");
unknown's avatar
unknown committed
1817 1818 1819 1820 1821 1822 1823 1824
			break;
		case TCL_DB_EXTENT:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-extent size");
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
1825
			result = _GetUInt32(interp, objv[i++], &uintarg);
unknown's avatar
unknown committed
1826 1827
			if (result == TCL_OK) {
				_debug_check();
unknown's avatar
unknown committed
1828
				ret = (*dbp)->set_q_extentsize(*dbp, uintarg);
unknown's avatar
unknown committed
1829
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1830
				    DB_RETOK_STD(ret), "set_q_extentsize");
unknown's avatar
unknown committed
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
			}
			break;
		case TCL_DB_CACHESIZE:
			result = Tcl_ListObjGetElements(interp, objv[i++],
			    &myobjc, &myobjv);
			if (result != TCL_OK)
				break;
			if (myobjc != 3) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-cachesize {gbytes bytes ncaches}?");
				result = TCL_ERROR;
				break;
			}
unknown's avatar
unknown committed
1844
			result = _GetUInt32(interp, myobjv[0], &gbytes);
unknown's avatar
unknown committed
1845 1846
			if (result != TCL_OK)
				break;
unknown's avatar
unknown committed
1847
			result = _GetUInt32(interp, myobjv[1], &bytes);
unknown's avatar
unknown committed
1848 1849
			if (result != TCL_OK)
				break;
unknown's avatar
unknown committed
1850
			result = _GetUInt32(interp, myobjv[2], &ncaches);
unknown's avatar
unknown committed
1851 1852 1853 1854 1855 1856
			if (result != TCL_OK)
				break;
			_debug_check();
			ret = (*dbp)->set_cachesize(*dbp, gbytes, bytes,
			    ncaches);
			result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1857
			    DB_RETOK_STD(ret), "set_cachesize");
unknown's avatar
unknown committed
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
			break;
		case TCL_DB_PAGESIZE:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-pagesize size?");
				result = TCL_ERROR;
				break;
			}
			result = Tcl_GetIntFromObj(interp, objv[i++], &intarg);
			if (result == TCL_OK) {
				_debug_check();
				ret = (*dbp)->set_pagesize(*dbp,
				    (size_t)intarg);
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1872
				    DB_RETOK_STD(ret), "set pagesize");
unknown's avatar
unknown committed
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
			}
			break;
		case TCL_DB_ERRFILE:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-errfile file");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			/*
			 * If the user already set one, close it.
			 */
			if (errip->i_err != NULL)
				fclose(errip->i_err);
			errip->i_err = fopen(arg, "a");
			if (errip->i_err != NULL) {
				_debug_check();
				(*dbp)->set_errfile(*dbp, errip->i_err);
				set_err = 1;
			}
			break;
		case TCL_DB_ERRPFX:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-errpfx prefix");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			/*
			 * If the user already set one, free it.
			 */
			if (errip->i_errpfx != NULL)
unknown's avatar
unknown committed
1907
				__os_free(NULL, errip->i_errpfx);
unknown's avatar
unknown committed
1908 1909 1910
			if ((ret = __os_strdup((*dbp)->dbenv,
			    arg, &errip->i_errpfx)) != 0) {
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
1911
				    DB_RETOK_STD(ret), "__os_strdup");
unknown's avatar
unknown committed
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
				break;
			}
			if (errip->i_errpfx != NULL) {
				_debug_check();
				(*dbp)->set_errpfx(*dbp, errip->i_errpfx);
				set_pfx = 1;
			}
			break;
		case TCL_DB_ENDARG:
			endarg = 1;
			break;
		} /* switch */

		/*
		 * If, at any time, parsing the args we get an error,
		 * bail out and return.
		 */
		if (result != TCL_OK)
			goto error;
		if (endarg)
			break;
	}
	if (result != TCL_OK)
		goto error;

	/*
	 * Any args we have left, (better be 0, 1 or 2 left) are
	 * file names.  If we have 0, then an in-memory db.  If
	 * there is 1, a db name, if 2 a db and subdb name.
	 */
	if (i != objc) {
		/*
		 * Dbs must be NULL terminated file names, but subdbs can
		 * be anything.  Use Strings for the db name and byte
		 * arrays for the subdb.
		 */
		db = Tcl_GetStringFromObj(objv[i++], NULL);
		if (i != objc) {
			subdbtmp =
			    Tcl_GetByteArrayFromObj(objv[i++], &subdblen);
			if ((ret = __os_malloc(envp,
unknown's avatar
unknown committed
1953
			   subdblen + 1, &subdb)) != 0) {
unknown's avatar
unknown committed
1954 1955 1956 1957 1958 1959 1960 1961
				Tcl_SetResult(interp, db_strerror(ret),
				    TCL_STATIC);
				return (0);
			}
			memcpy(subdb, subdbtmp, subdblen);
			subdb[subdblen] = '\0';
		}
	}
unknown's avatar
unknown committed
1962 1963 1964 1965
	if (set_flags) {
		ret = (*dbp)->set_flags(*dbp, set_flags);
		result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
		    "set_flags");
unknown's avatar
unknown committed
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
		if (result == TCL_ERROR)
			goto error;
		/*
		 * If we are successful, clear the result so that the
		 * return from set_flags isn't part of the result.
		 */
		Tcl_ResetResult(interp);
	}

	/*
	 * When we get here, we have already parsed all of our args and made
	 * all our calls to set up the database.  Everything is okay so far,
	 * no errors, if we get here.
	 */
	_debug_check();

	/* Open the database. */
unknown's avatar
unknown committed
1983 1984
	ret = (*dbp)->open(*dbp, txn, db, subdb, type, open_flags, mode);
	result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db open");
unknown's avatar
unknown committed
1985 1986 1987

error:
	if (subdb)
unknown's avatar
unknown committed
1988
		__os_free(envp, subdb);
unknown's avatar
unknown committed
1989
	if (result == TCL_ERROR) {
unknown's avatar
unknown committed
1990
		(void)(*dbp)->close(*dbp, 0);
unknown's avatar
unknown committed
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
		/*
		 * If we opened and set up the error file in the environment
		 * on this open, but we failed for some other reason, clean
		 * up and close the file.
		 *
		 * XXX when err stuff isn't tied to env, change to use ip,
		 * instead of envip.  Also, set_err is irrelevant when that
		 * happens.  It will just read:
		 * if (ip->i_err)
		 *	fclose(ip->i_err);
		 */
		if (set_err && errip && errip->i_err != NULL) {
			fclose(errip->i_err);
			errip->i_err = NULL;
		}
		if (set_pfx && errip && errip->i_errpfx != NULL) {
unknown's avatar
unknown committed
2007
			__os_free(envp, errip->i_errpfx);
unknown's avatar
unknown committed
2008 2009 2010 2011 2012 2013 2014 2015 2016
			errip->i_errpfx = NULL;
		}
		*dbp = NULL;
	}
	return (result);
}

/*
 * bdb_DbRemove --
unknown's avatar
unknown committed
2017
 *	Implements the DB_ENV->remove and DB->remove command.
unknown's avatar
unknown committed
2018 2019 2020 2021 2022 2023 2024 2025
 */
static int
bdb_DbRemove(interp, objc, objv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
{
	static char *bdbrem[] = {
unknown's avatar
unknown committed
2026 2027 2028 2029 2030 2031 2032 2033
		"-auto_commit",
		"-encrypt",
		"-encryptaes",
		"-encryptany",
		"-env",
		"-txn",
		"--",
		NULL
unknown's avatar
unknown committed
2034 2035
	};
	enum bdbrem {
unknown's avatar
unknown committed
2036 2037 2038 2039
		TCL_DBREM_AUTOCOMMIT,
		TCL_DBREM_ENCRYPT,
		TCL_DBREM_ENCRYPT_AES,
		TCL_DBREM_ENCRYPT_ANY,
unknown's avatar
unknown committed
2040
		TCL_DBREM_ENV,
unknown's avatar
unknown committed
2041
		TCL_DBREM_TXN,
unknown's avatar
unknown committed
2042 2043 2044
		TCL_DBREM_ENDARG
	};
	DB *dbp;
unknown's avatar
unknown committed
2045 2046
	DB_ENV *envp;
	DB_TXN *txn;
unknown's avatar
unknown committed
2047
	int endarg, i, optindex, result, ret, subdblen;
unknown's avatar
unknown committed
2048
	u_int32_t enc_flag, iflags, set_flags;
unknown's avatar
unknown committed
2049
	u_char *subdbtmp;
unknown's avatar
unknown committed
2050
	char *arg, *db, msg[MSG_SIZE], *passwd, *subdb;
unknown's avatar
unknown committed
2051

unknown's avatar
unknown committed
2052
	db = subdb = NULL;
unknown's avatar
unknown committed
2053
	dbp = NULL;
unknown's avatar
unknown committed
2054 2055 2056 2057
	endarg = 0;
	envp = NULL;
	iflags = enc_flag = set_flags = 0;
	passwd = NULL;
unknown's avatar
unknown committed
2058 2059
	result = TCL_OK;
	subdbtmp = NULL;
unknown's avatar
unknown committed
2060
	txn = NULL;
unknown's avatar
unknown committed
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084

	if (objc < 2) {
		Tcl_WrongNumArgs(interp, 2, objv, "?args? filename ?database?");
		return (TCL_ERROR);
	}

	/*
	 * We must first parse for the environment flag, since that
	 * is needed for db_create.  Then create the db handle.
	 */
	i = 2;
	while (i < objc) {
		if (Tcl_GetIndexFromObj(interp, objv[i], bdbrem,
		    "option", TCL_EXACT, &optindex) != TCL_OK) {
			arg = Tcl_GetStringFromObj(objv[i], NULL);
			if (arg[0] == '-') {
				result = IS_HELP(objv[i]);
				goto error;
			} else
				Tcl_ResetResult(interp);
			break;
		}
		i++;
		switch ((enum bdbrem)optindex) {
unknown's avatar
unknown committed
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
		case TCL_DBREM_AUTOCOMMIT:
			iflags |= DB_AUTO_COMMIT;
			_debug_check();
			break;
		case TCL_DBREM_ENCRYPT:
			set_flags |= DB_ENCRYPT;
			_debug_check();
			break;
		case TCL_DBREM_ENCRYPT_AES:
			/* Make sure we have an arg to check against! */
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-encryptaes passwd?");
				result = TCL_ERROR;
				break;
			}
			passwd = Tcl_GetStringFromObj(objv[i++], NULL);
			enc_flag = DB_ENCRYPT_AES;
			break;
		case TCL_DBREM_ENCRYPT_ANY:
			/* Make sure we have an arg to check against! */
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-encryptany passwd?");
				result = TCL_ERROR;
				break;
			}
			passwd = Tcl_GetStringFromObj(objv[i++], NULL);
			enc_flag = 0;
			break;
unknown's avatar
unknown committed
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
		case TCL_DBREM_ENV:
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			envp = NAME_TO_ENV(arg);
			if (envp == NULL) {
				Tcl_SetResult(interp,
				    "db remove: illegal environment",
				    TCL_STATIC);
				return (TCL_ERROR);
			}
			break;
		case TCL_DBREM_ENDARG:
			endarg = 1;
			break;
unknown's avatar
unknown committed
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
		case TCL_DBREM_TXN:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv, "?-txn id?");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			txn = NAME_TO_TXN(arg);
			if (txn == NULL) {
				snprintf(msg, MSG_SIZE,
				    "Put: Invalid txn: %s\n", arg);
				Tcl_SetResult(interp, msg, TCL_VOLATILE);
				result = TCL_ERROR;
			}
			break;
unknown's avatar
unknown committed
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
		}
		/*
		 * If, at any time, parsing the args we get an error,
		 * bail out and return.
		 */
		if (result != TCL_OK)
			goto error;
		if (endarg)
			break;
	}
	if (result != TCL_OK)
		goto error;
	/*
	 * Any args we have left, (better be 1 or 2 left) are
	 * file names. If there is 1, a db name, if 2 a db and subdb name.
	 */
	if ((i != (objc - 1)) || (i != (objc - 2))) {
		/*
		 * Dbs must be NULL terminated file names, but subdbs can
		 * be anything.  Use Strings for the db name and byte
		 * arrays for the subdb.
		 */
		db = Tcl_GetStringFromObj(objv[i++], NULL);
		if (i != objc) {
			subdbtmp =
			    Tcl_GetByteArrayFromObj(objv[i++], &subdblen);
			if ((ret = __os_malloc(envp, subdblen + 1,
unknown's avatar
unknown committed
2170
			    &subdb)) != 0) { Tcl_SetResult(interp,
unknown's avatar
unknown committed
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
				    db_strerror(ret), TCL_STATIC);
				return (0);
			}
			memcpy(subdb, subdbtmp, subdblen);
			subdb[subdblen] = '\0';
		}
	} else {
		Tcl_WrongNumArgs(interp, 2, objv, "?args? filename ?database?");
		result = TCL_ERROR;
		goto error;
	}
unknown's avatar
unknown committed
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
	if (envp == NULL) {
		ret = db_create(&dbp, envp, 0);
		if (ret) {
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "db_create");
			goto error;
		}

		if (passwd != NULL) {
			ret = dbp->set_encrypt(dbp, passwd, enc_flag);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_encrypt");
		}
		if (set_flags != 0) {
			ret = dbp->set_flags(dbp, set_flags);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_flags");
		}
unknown's avatar
unknown committed
2200
	}
unknown's avatar
unknown committed
2201

unknown's avatar
unknown committed
2202 2203 2204
	/*
	 * No matter what, we NULL out dbp after this call.
	 */
unknown's avatar
unknown committed
2205 2206 2207 2208 2209 2210 2211
	_debug_check();
	if (dbp == NULL)
		ret = envp->dbremove(envp, txn, db, subdb, iflags);
	else
		ret = dbp->remove(dbp, db, subdb, 0);

	result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db remove");
unknown's avatar
unknown committed
2212 2213 2214
	dbp = NULL;
error:
	if (subdb)
unknown's avatar
unknown committed
2215 2216
		__os_free(envp, subdb);
	if (result == TCL_ERROR && dbp != NULL)
unknown's avatar
unknown committed
2217 2218 2219 2220 2221 2222
		(void)dbp->close(dbp, 0);
	return (result);
}

/*
 * bdb_DbRename --
unknown's avatar
unknown committed
2223
 *	Implements the DBENV->dbrename and DB->rename commands.
unknown's avatar
unknown committed
2224 2225 2226 2227 2228 2229 2230 2231
 */
static int
bdb_DbRename(interp, objc, objv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
{
	static char *bdbmv[] = {
unknown's avatar
unknown committed
2232 2233 2234 2235 2236 2237 2238 2239
		"-auto_commit",
		"-encrypt",
		"-encryptaes",
		"-encryptany",
		"-env",
		"-txn",
		"--",
		NULL
unknown's avatar
unknown committed
2240 2241
	};
	enum bdbmv {
unknown's avatar
unknown committed
2242 2243 2244 2245
		TCL_DBMV_AUTOCOMMIT,
		TCL_DBMV_ENCRYPT,
		TCL_DBMV_ENCRYPT_AES,
		TCL_DBMV_ENCRYPT_ANY,
unknown's avatar
unknown committed
2246
		TCL_DBMV_ENV,
unknown's avatar
unknown committed
2247
		TCL_DBMV_TXN,
unknown's avatar
unknown committed
2248 2249 2250
		TCL_DBMV_ENDARG
	};
	DB *dbp;
unknown's avatar
unknown committed
2251 2252 2253
	DB_ENV *envp;
	DB_TXN *txn;
	u_int32_t enc_flag, iflags, set_flags;
unknown's avatar
unknown committed
2254 2255
	int endarg, i, newlen, optindex, result, ret, subdblen;
	u_char *subdbtmp;
unknown's avatar
unknown committed
2256
	char *arg, *db, msg[MSG_SIZE], *newname, *passwd, *subdb;
unknown's avatar
unknown committed
2257

unknown's avatar
unknown committed
2258
	db = newname = subdb = NULL;
unknown's avatar
unknown committed
2259
	dbp = NULL;
unknown's avatar
unknown committed
2260 2261 2262 2263
	endarg = 0;
	envp = NULL;
	iflags = enc_flag = set_flags = 0;
	passwd = NULL;
unknown's avatar
unknown committed
2264 2265
	result = TCL_OK;
	subdbtmp = NULL;
unknown's avatar
unknown committed
2266
	txn = NULL;
unknown's avatar
unknown committed
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291

	if (objc < 2) {
		Tcl_WrongNumArgs(interp,
			3, objv, "?args? filename ?database? ?newname?");
		return (TCL_ERROR);
	}

	/*
	 * We must first parse for the environment flag, since that
	 * is needed for db_create.  Then create the db handle.
	 */
	i = 2;
	while (i < objc) {
		if (Tcl_GetIndexFromObj(interp, objv[i], bdbmv,
		    "option", TCL_EXACT, &optindex) != TCL_OK) {
			arg = Tcl_GetStringFromObj(objv[i], NULL);
			if (arg[0] == '-') {
				result = IS_HELP(objv[i]);
				goto error;
			} else
				Tcl_ResetResult(interp);
			break;
		}
		i++;
		switch ((enum bdbmv)optindex) {
unknown's avatar
unknown committed
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
		case TCL_DBMV_AUTOCOMMIT:
			iflags |= DB_AUTO_COMMIT;
			_debug_check();
			break;
		case TCL_DBMV_ENCRYPT:
			set_flags |= DB_ENCRYPT;
			_debug_check();
			break;
		case TCL_DBMV_ENCRYPT_AES:
			/* Make sure we have an arg to check against! */
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-encryptaes passwd?");
				result = TCL_ERROR;
				break;
			}
			passwd = Tcl_GetStringFromObj(objv[i++], NULL);
			enc_flag = DB_ENCRYPT_AES;
			break;
		case TCL_DBMV_ENCRYPT_ANY:
			/* Make sure we have an arg to check against! */
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-encryptany passwd?");
				result = TCL_ERROR;
				break;
			}
			passwd = Tcl_GetStringFromObj(objv[i++], NULL);
			enc_flag = 0;
			break;
unknown's avatar
unknown committed
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
		case TCL_DBMV_ENV:
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			envp = NAME_TO_ENV(arg);
			if (envp == NULL) {
				Tcl_SetResult(interp,
				    "db rename: illegal environment",
				    TCL_STATIC);
				return (TCL_ERROR);
			}
			break;
		case TCL_DBMV_ENDARG:
			endarg = 1;
			break;
unknown's avatar
unknown committed
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
		case TCL_DBMV_TXN:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv, "?-txn id?");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			txn = NAME_TO_TXN(arg);
			if (txn == NULL) {
				snprintf(msg, MSG_SIZE,
				    "Put: Invalid txn: %s\n", arg);
				Tcl_SetResult(interp, msg, TCL_VOLATILE);
				result = TCL_ERROR;
			}
			break;
unknown's avatar
unknown committed
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
		}
		/*
		 * If, at any time, parsing the args we get an error,
		 * bail out and return.
		 */
		if (result != TCL_OK)
			goto error;
		if (endarg)
			break;
	}
	if (result != TCL_OK)
		goto error;
	/*
	 * Any args we have left, (better be 2 or 3 left) are
	 * file names. If there is 2, a file name, if 3 a file and db name.
	 */
	if ((i != (objc - 2)) || (i != (objc - 3))) {
		/*
		 * Dbs must be NULL terminated file names, but subdbs can
		 * be anything.  Use Strings for the db name and byte
		 * arrays for the subdb.
		 */
		db = Tcl_GetStringFromObj(objv[i++], NULL);
		if (i == objc - 2) {
			subdbtmp =
			    Tcl_GetByteArrayFromObj(objv[i++], &subdblen);
			if ((ret = __os_malloc(envp, subdblen + 1,
unknown's avatar
unknown committed
2377
			    &subdb)) != 0) {
unknown's avatar
unknown committed
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
				Tcl_SetResult(interp,
				    db_strerror(ret), TCL_STATIC);
				return (0);
			}
			memcpy(subdb, subdbtmp, subdblen);
			subdb[subdblen] = '\0';
		}
		subdbtmp =
		    Tcl_GetByteArrayFromObj(objv[i++], &newlen);
		if ((ret = __os_malloc(envp, newlen + 1,
unknown's avatar
unknown committed
2388
		    &newname)) != 0) {
unknown's avatar
unknown committed
2389 2390 2391 2392 2393 2394 2395
			Tcl_SetResult(interp,
			    db_strerror(ret), TCL_STATIC);
			return (0);
		}
		memcpy(newname, subdbtmp, newlen);
		newname[newlen] = '\0';
	} else {
unknown's avatar
unknown committed
2396 2397
		Tcl_WrongNumArgs(
		    interp, 3, objv, "?args? filename ?database? ?newname?");
unknown's avatar
unknown committed
2398 2399 2400
		result = TCL_ERROR;
		goto error;
	}
unknown's avatar
unknown committed
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
	if (envp == NULL) {
		ret = db_create(&dbp, envp, 0);
		if (ret) {
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "db_create");
			goto error;
		}
		if (passwd != NULL) {
			ret = dbp->set_encrypt(dbp, passwd, enc_flag);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_encrypt");
		}
		if (set_flags != 0) {
			ret = dbp->set_flags(dbp, set_flags);
			result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
			    "set_flags");
		}
unknown's avatar
unknown committed
2418
	}
unknown's avatar
unknown committed
2419

unknown's avatar
unknown committed
2420 2421 2422
	/*
	 * No matter what, we NULL out dbp after this call.
	 */
unknown's avatar
unknown committed
2423 2424 2425 2426 2427
	if (dbp == NULL)
		ret = envp->dbrename(envp, txn, db, subdb, newname, iflags);
	else
		ret = dbp->rename(dbp, db, subdb, newname, 0);
	result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db rename");
unknown's avatar
unknown committed
2428 2429 2430
	dbp = NULL;
error:
	if (subdb)
unknown's avatar
unknown committed
2431
		__os_free(envp, subdb);
unknown's avatar
unknown committed
2432
	if (newname)
unknown's avatar
unknown committed
2433 2434
		__os_free(envp, newname);
	if (result == TCL_ERROR && dbp != NULL)
unknown's avatar
unknown committed
2435 2436 2437 2438
		(void)dbp->close(dbp, 0);
	return (result);
}

unknown's avatar
unknown committed
2439
#if CONFIG_TEST
unknown's avatar
unknown committed
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
/*
 * bdb_DbVerify --
 *	Implements the DB->verify command.
 */
static int
bdb_DbVerify(interp, objc, objv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
{
	static char *bdbverify[] = {
unknown's avatar
unknown committed
2451 2452 2453 2454 2455 2456 2457 2458
		"-encrypt",
		"-encryptaes",
		"-encryptany",
		"-env",
		"-errfile",
		"-errpfx",
		"--",
		NULL
unknown's avatar
unknown committed
2459 2460
	};
	enum bdbvrfy {
unknown's avatar
unknown committed
2461 2462 2463
		TCL_DBVRFY_ENCRYPT,
		TCL_DBVRFY_ENCRYPT_AES,
		TCL_DBVRFY_ENCRYPT_ANY,
unknown's avatar
unknown committed
2464 2465 2466 2467 2468 2469 2470 2471
		TCL_DBVRFY_ENV,
		TCL_DBVRFY_ERRFILE,
		TCL_DBVRFY_ERRPFX,
		TCL_DBVRFY_ENDARG
	};
	DB_ENV *envp;
	DB *dbp;
	FILE *errf;
unknown's avatar
unknown committed
2472 2473 2474
	u_int32_t enc_flag, flags, set_flags;
	int endarg, i, optindex, result, ret;
	char *arg, *db, *errpfx, *passwd;
unknown's avatar
unknown committed
2475 2476 2477

	envp = NULL;
	dbp = NULL;
unknown's avatar
unknown committed
2478
	passwd = NULL;
unknown's avatar
unknown committed
2479 2480 2481 2482
	result = TCL_OK;
	db = errpfx = NULL;
	errf = NULL;
	flags = endarg = 0;
unknown's avatar
unknown committed
2483
	enc_flag = set_flags = 0;
unknown's avatar
unknown committed
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507

	if (objc < 2) {
		Tcl_WrongNumArgs(interp, 2, objv, "?args? filename");
		return (TCL_ERROR);
	}

	/*
	 * We must first parse for the environment flag, since that
	 * is needed for db_create.  Then create the db handle.
	 */
	i = 2;
	while (i < objc) {
		if (Tcl_GetIndexFromObj(interp, objv[i], bdbverify,
		    "option", TCL_EXACT, &optindex) != TCL_OK) {
			arg = Tcl_GetStringFromObj(objv[i], NULL);
			if (arg[0] == '-') {
				result = IS_HELP(objv[i]);
				goto error;
			} else
				Tcl_ResetResult(interp);
			break;
		}
		i++;
		switch ((enum bdbvrfy)optindex) {
unknown's avatar
unknown committed
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
		case TCL_DBVRFY_ENCRYPT:
			set_flags |= DB_ENCRYPT;
			_debug_check();
			break;
		case TCL_DBVRFY_ENCRYPT_AES:
			/* Make sure we have an arg to check against! */
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-encryptaes passwd?");
				result = TCL_ERROR;
				break;
			}
			passwd = Tcl_GetStringFromObj(objv[i++], NULL);
			enc_flag = DB_ENCRYPT_AES;
			break;
		case TCL_DBVRFY_ENCRYPT_ANY:
			/* Make sure we have an arg to check against! */
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "?-encryptany passwd?");
				result = TCL_ERROR;
				break;
			}
			passwd = Tcl_GetStringFromObj(objv[i++], NULL);
			enc_flag = 0;
			break;
unknown's avatar
unknown committed
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
		case TCL_DBVRFY_ENV:
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			envp = NAME_TO_ENV(arg);
			if (envp == NULL) {
				Tcl_SetResult(interp,
				    "db verify: illegal environment",
				    TCL_STATIC);
				result = TCL_ERROR;
				break;
			}
			break;
		case TCL_DBVRFY_ERRFILE:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-errfile file");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			/*
			 * If the user already set one, close it.
			 */
			if (errf != NULL)
				fclose(errf);
			errf = fopen(arg, "a");
			break;
		case TCL_DBVRFY_ERRPFX:
			if (i >= objc) {
				Tcl_WrongNumArgs(interp, 2, objv,
				    "-errpfx prefix");
				result = TCL_ERROR;
				break;
			}
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			/*
			 * If the user already set one, free it.
			 */
			if (errpfx != NULL)
unknown's avatar
unknown committed
2572
				__os_free(envp, errpfx);
unknown's avatar
unknown committed
2573 2574
			if ((ret = __os_strdup(NULL, arg, &errpfx)) != 0) {
				result = _ReturnSetup(interp, ret,
unknown's avatar
unknown committed
2575
				    DB_RETOK_STD(ret), "__os_strdup");
unknown's avatar
unknown committed
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
				break;
			}
			break;
		case TCL_DBVRFY_ENDARG:
			endarg = 1;
			break;
		}
		/*
		 * If, at any time, parsing the args we get an error,
		 * bail out and return.
		 */
		if (result != TCL_OK)
			goto error;
		if (endarg)
			break;
	}
	if (result != TCL_OK)
		goto error;
	/*
	 * The remaining arg is the db filename.
	 */
	if (i == (objc - 1))
		db = Tcl_GetStringFromObj(objv[i++], NULL);
	else {
		Tcl_WrongNumArgs(interp, 2, objv, "?args? filename");
		result = TCL_ERROR;
		goto error;
	}
	ret = db_create(&dbp, envp, 0);
	if (ret) {
unknown's avatar
unknown committed
2606 2607
		result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
		    "db_create");
unknown's avatar
unknown committed
2608 2609 2610
		goto error;
	}

unknown's avatar
unknown committed
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
	if (passwd != NULL) {
		ret = dbp->set_encrypt(dbp, passwd, enc_flag);
		result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
		    "set_encrypt");
	}

	if (set_flags != 0) {
		ret = dbp->set_flags(dbp, set_flags);
		result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
		    "set_flags");
	}
unknown's avatar
unknown committed
2622 2623 2624 2625 2626 2627
	if (errf != NULL)
		dbp->set_errfile(dbp, errf);
	if (errpfx != NULL)
		dbp->set_errpfx(dbp, errpfx);

	ret = dbp->verify(dbp, db, NULL, NULL, flags);
unknown's avatar
unknown committed
2628
	result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db verify");
unknown's avatar
unknown committed
2629 2630 2631 2632
error:
	if (errf != NULL)
		fclose(errf);
	if (errpfx != NULL)
unknown's avatar
unknown committed
2633
		__os_free(envp, errpfx);
unknown's avatar
unknown committed
2634 2635 2636 2637
	if (dbp)
		(void)dbp->close(dbp, 0);
	return (result);
}
unknown's avatar
unknown committed
2638
#endif
unknown's avatar
unknown committed
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714

/*
 * bdb_Version --
 *	Implements the version command.
 */
static int
bdb_Version(interp, objc, objv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
{
	static char *bdbver[] = {
		"-string", NULL
	};
	enum bdbver {
		TCL_VERSTRING
	};
	int i, optindex, maj, min, patch, result, string, verobjc;
	char *arg, *v;
	Tcl_Obj *res, *verobjv[3];

	result = TCL_OK;
	string = 0;

	if (objc < 2) {
		Tcl_WrongNumArgs(interp, 2, objv, "?args?");
		return (TCL_ERROR);
	}

	/*
	 * We must first parse for the environment flag, since that
	 * is needed for db_create.  Then create the db handle.
	 */
	i = 2;
	while (i < objc) {
		if (Tcl_GetIndexFromObj(interp, objv[i], bdbver,
		    "option", TCL_EXACT, &optindex) != TCL_OK) {
			arg = Tcl_GetStringFromObj(objv[i], NULL);
			if (arg[0] == '-') {
				result = IS_HELP(objv[i]);
				goto error;
			} else
				Tcl_ResetResult(interp);
			break;
		}
		i++;
		switch ((enum bdbver)optindex) {
		case TCL_VERSTRING:
			string = 1;
			break;
		}
		/*
		 * If, at any time, parsing the args we get an error,
		 * bail out and return.
		 */
		if (result != TCL_OK)
			goto error;
	}
	if (result != TCL_OK)
		goto error;

	v = db_version(&maj, &min, &patch);
	if (string)
		res = Tcl_NewStringObj(v, strlen(v));
	else {
		verobjc = 3;
		verobjv[0] = Tcl_NewIntObj(maj);
		verobjv[1] = Tcl_NewIntObj(min);
		verobjv[2] = Tcl_NewIntObj(patch);
		res = Tcl_NewListObj(verobjc, verobjv);
	}
	Tcl_SetObjResult(interp, res);
error:
	return (result);
}

unknown's avatar
unknown committed
2715
#if CONFIG_TEST
unknown's avatar
unknown committed
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746
/*
 * bdb_Handles --
 *	Implements the handles command.
 */
static int
bdb_Handles(interp, objc, objv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
{
	DBTCL_INFO *p;
	Tcl_Obj *res, *handle;

	/*
	 * No args.  Error if we have some
	 */
	if (objc != 2) {
		Tcl_WrongNumArgs(interp, 2, objv, "");
		return (TCL_ERROR);
	}
	res = Tcl_NewListObj(0, NULL);

	for (p = LIST_FIRST(&__db_infohead); p != NULL;
	    p = LIST_NEXT(p, entries)) {
		handle = Tcl_NewStringObj(p->i_name, strlen(p->i_name));
		if (Tcl_ListObjAppendElement(interp, res, handle) != TCL_OK)
			return (TCL_ERROR);
	}
	Tcl_SetObjResult(interp, res);
	return (TCL_OK);
}
unknown's avatar
unknown committed
2747
#endif
unknown's avatar
unknown committed
2748

unknown's avatar
unknown committed
2749
#if CONFIG_TEST
unknown's avatar
unknown committed
2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769
/*
 * bdb_DbUpgrade --
 *	Implements the DB->upgrade command.
 */
static int
bdb_DbUpgrade(interp, objc, objv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
{
	static char *bdbupg[] = {
		"-dupsort", "-env", "--", NULL
	};
	enum bdbupg {
		TCL_DBUPG_DUPSORT,
		TCL_DBUPG_ENV,
		TCL_DBUPG_ENDARG
	};
	DB_ENV *envp;
	DB *dbp;
unknown's avatar
unknown committed
2770 2771
	u_int32_t flags;
	int endarg, i, optindex, result, ret;
unknown's avatar
unknown committed
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
	char *arg, *db;

	envp = NULL;
	dbp = NULL;
	result = TCL_OK;
	db = NULL;
	flags = endarg = 0;

	if (objc < 2) {
		Tcl_WrongNumArgs(interp, 2, objv, "?args? filename");
		return (TCL_ERROR);
	}

	i = 2;
	while (i < objc) {
		if (Tcl_GetIndexFromObj(interp, objv[i], bdbupg,
		    "option", TCL_EXACT, &optindex) != TCL_OK) {
			arg = Tcl_GetStringFromObj(objv[i], NULL);
			if (arg[0] == '-') {
				result = IS_HELP(objv[i]);
				goto error;
			} else
				Tcl_ResetResult(interp);
			break;
		}
		i++;
		switch ((enum bdbupg)optindex) {
		case TCL_DBUPG_DUPSORT:
			flags |= DB_DUPSORT;
			break;
		case TCL_DBUPG_ENV:
			arg = Tcl_GetStringFromObj(objv[i++], NULL);
			envp = NAME_TO_ENV(arg);
			if (envp == NULL) {
				Tcl_SetResult(interp,
				    "db upgrade: illegal environment",
				    TCL_STATIC);
				return (TCL_ERROR);
			}
			break;
		case TCL_DBUPG_ENDARG:
			endarg = 1;
			break;
		}
		/*
		 * If, at any time, parsing the args we get an error,
		 * bail out and return.
		 */
		if (result != TCL_OK)
			goto error;
		if (endarg)
			break;
	}
	if (result != TCL_OK)
		goto error;
	/*
	 * The remaining arg is the db filename.
	 */
	if (i == (objc - 1))
		db = Tcl_GetStringFromObj(objv[i++], NULL);
	else {
		Tcl_WrongNumArgs(interp, 2, objv, "?args? filename");
		result = TCL_ERROR;
		goto error;
	}
	ret = db_create(&dbp, envp, 0);
	if (ret) {
unknown's avatar
unknown committed
2839 2840
		result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
		    "db_create");
unknown's avatar
unknown committed
2841 2842 2843 2844
		goto error;
	}

	ret = dbp->upgrade(dbp, db, flags);
unknown's avatar
unknown committed
2845
	result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "db upgrade");
unknown's avatar
unknown committed
2846 2847 2848 2849 2850
error:
	if (dbp)
		(void)dbp->close(dbp, 0);
	return (result);
}
unknown's avatar
unknown committed
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117
#endif

/*
 * tcl_bt_compare and tcl_dup_compare --
 *	These two are basically identical internally, so may as well
 * share code.  The only differences are the name used in error
 * reporting and the Tcl_Obj representing their respective procs.
 */
static int
tcl_bt_compare(dbp, dbta, dbtb)
	DB *dbp;
	const DBT *dbta, *dbtb;
{
	return (tcl_compare_callback(dbp, dbta, dbtb,
	    ((DBTCL_INFO *)dbp->api_internal)->i_btcompare, "bt_compare"));
}

static int
tcl_dup_compare(dbp, dbta, dbtb)
	DB *dbp;
	const DBT *dbta, *dbtb;
{
	return (tcl_compare_callback(dbp, dbta, dbtb,
	    ((DBTCL_INFO *)dbp->api_internal)->i_dupcompare, "dup_compare"));
}

/*
 * tcl_compare_callback --
 *	Tcl callback for set_bt_compare and set_dup_compare. What this
 * function does is stuff the data fields of the two DBTs into Tcl ByteArray
 * objects, then call the procedure stored in ip->i_btcompare on the two
 * objects.  Then we return that procedure's result as the comparison.
 */
static int
tcl_compare_callback(dbp, dbta, dbtb, procobj, errname)
	DB *dbp;
	const DBT *dbta, *dbtb;
	Tcl_Obj *procobj;
	char *errname;
{
	DBTCL_INFO *ip;
	Tcl_Interp *interp;
	Tcl_Obj *a, *b, *resobj, *objv[3];
	int result, cmp;

	ip = (DBTCL_INFO *)dbp->api_internal;
	interp = ip->i_interp;
	objv[0] = procobj;

	/*
	 * Create two ByteArray objects, with the two data we've been passed.
	 * This will involve a copy, which is unpleasantly slow, but there's
	 * little we can do to avoid this (I think).
	 */
	a = Tcl_NewByteArrayObj(dbta->data, dbta->size);
	Tcl_IncrRefCount(a);
	b = Tcl_NewByteArrayObj(dbtb->data, dbtb->size);
	Tcl_IncrRefCount(b);

	objv[1] = a;
	objv[2] = b;

	result = Tcl_EvalObjv(interp, 3, objv, 0);
	if (result != TCL_OK) {
		/*
		 * XXX
		 * If this or the next Tcl call fails, we're doomed.
		 * There's no way to return an error from comparison functions,
		 * no way to determine what the correct sort order is, and
		 * so no way to avoid corrupting the database if we proceed.
		 * We could play some games stashing return values on the
		 * DB handle, but it's not worth the trouble--no one with
		 * any sense is going to be using this other than for testing,
		 * and failure typically means that the bt_compare proc
		 * had a syntax error in it or something similarly dumb.
		 *
		 * So, drop core.  If we're not running with diagnostic
		 * mode, panic--and always return a negative number. :-)
		 */
panic:		__db_err(dbp->dbenv, "Tcl %s callback failed", errname);
		DB_ASSERT(0);
		return (__db_panic(dbp->dbenv, DB_RUNRECOVERY));
	}

	resobj = Tcl_GetObjResult(interp);
	result = Tcl_GetIntFromObj(interp, resobj, &cmp);
	if (result != TCL_OK)
		goto panic;

	Tcl_DecrRefCount(a);
	Tcl_DecrRefCount(b);
	return (cmp);
}

/*
 * tcl_h_hash --
 *	Tcl callback for the hashing function.  See tcl_compare_callback--
 * this works much the same way, only we're given a buffer and a length
 * instead of two DBTs.
 */
static u_int32_t
tcl_h_hash(dbp, buf, len)
	DB *dbp;
	const void *buf;
	u_int32_t len;
{
	DBTCL_INFO *ip;
	Tcl_Interp *interp;
	Tcl_Obj *objv[2];
	int result, hval;

	ip = (DBTCL_INFO *)dbp->api_internal;
	interp = ip->i_interp;
	objv[0] = ip->i_hashproc;

	/*
	 * Create a ByteArray for the buffer.
	 */
	objv[1] = Tcl_NewByteArrayObj((void *)buf, len);
	Tcl_IncrRefCount(objv[1]);
	result = Tcl_EvalObjv(interp, 2, objv, 0);
	if (result != TCL_OK) {
		/*
		 * XXX
		 * We drop core on error.  See the comment in
		 * tcl_compare_callback.
		 */
panic:		__db_err(dbp->dbenv, "Tcl h_hash callback failed");
		DB_ASSERT(0);
		return (__db_panic(dbp->dbenv, DB_RUNRECOVERY));
	}

	result = Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &hval);
	if (result != TCL_OK)
		goto panic;

	Tcl_DecrRefCount(objv[1]);
	return (hval);
}

/*
 * tcl_rep_send --
 *	Replication send callback.
 */
static int
tcl_rep_send(dbenv, control, rec, eid, flags)
	DB_ENV *dbenv;
	const DBT *control, *rec;
	int eid;
	u_int32_t flags;
{
	DBTCL_INFO *ip;
	Tcl_Interp *interp;
	Tcl_Obj *control_o, *eid_o, *origobj, *rec_o, *resobj, *objv[5];
	int result, ret;

	COMPQUIET(flags, 0);

	ip = (DBTCL_INFO *)dbenv->app_private;
	interp = ip->i_interp;
	objv[0] = ip->i_rep_send;

	control_o = Tcl_NewByteArrayObj(control->data, control->size);
	Tcl_IncrRefCount(control_o);

	rec_o = Tcl_NewByteArrayObj(rec->data, rec->size);
	Tcl_IncrRefCount(rec_o);

	eid_o = Tcl_NewIntObj(eid);
	Tcl_IncrRefCount(eid_o);

	objv[1] = control_o;
	objv[2] = rec_o;
	objv[3] = ip->i_rep_eid;	/* From ID */
	objv[4] = eid_o;		/* To ID */

	/*
	 * We really want to return the original result to the
	 * user.  So, save the result obj here, and then after
	 * we've taken care of the Tcl_EvalObjv, set the result
	 * back to this original result.
	 */
	origobj = Tcl_GetObjResult(interp);
	Tcl_IncrRefCount(origobj);
	result = Tcl_EvalObjv(interp, 5, objv, 0);
	if (result != TCL_OK) {
		/*
		 * XXX
		 * This probably isn't the right error behavior, but
		 * this error should only happen if the Tcl callback is
		 * somehow invalid, which is a fatal scripting bug.
		 */
err:		__db_err(dbenv, "Tcl rep_send failure");
		return (EINVAL);
	}

	resobj = Tcl_GetObjResult(interp);
	result = Tcl_GetIntFromObj(interp, resobj, &ret);
	if (result != TCL_OK)
		goto err;

	Tcl_SetObjResult(interp, origobj);
	Tcl_DecrRefCount(origobj);
	Tcl_DecrRefCount(control_o);
	Tcl_DecrRefCount(rec_o);
	Tcl_DecrRefCount(eid_o);

	return (ret);
}

#ifdef TEST_ALLOC
/*
 * tcl_db_malloc, tcl_db_realloc, tcl_db_free --
 *	Tcl-local malloc, realloc, and free functions to use for user data
 * to exercise umalloc/urealloc/ufree.  Allocate the memory as a Tcl object
 * so we're sure to exacerbate and catch any shared-library issues.
 */
static void *
tcl_db_malloc(size)
	size_t size;
{
	Tcl_Obj *obj;
	void *buf;

	obj = Tcl_NewObj();
	if (obj == NULL)
		return (NULL);
	Tcl_IncrRefCount(obj);

	Tcl_SetObjLength(obj, size + sizeof(Tcl_Obj *));
	buf = Tcl_GetString(obj);
	memcpy(buf, &obj, sizeof(&obj));

	buf = (Tcl_Obj **)buf + 1;
	return (buf);
}

static void *
tcl_db_realloc(ptr, size)
	void *ptr;
	size_t size;
{
	Tcl_Obj *obj;

	if (ptr == NULL)
		return (tcl_db_malloc(size));

	obj = *(Tcl_Obj **)((Tcl_Obj **)ptr - 1);
	Tcl_SetObjLength(obj, size + sizeof(Tcl_Obj *));

	ptr = Tcl_GetString(obj);
	memcpy(ptr, &obj, sizeof(&obj));

	ptr = (Tcl_Obj **)ptr + 1;
	return (ptr);
}

static void
tcl_db_free(ptr)
	void *ptr;
{
	Tcl_Obj *obj;

	obj = *(Tcl_Obj **)((Tcl_Obj **)ptr - 1);
	Tcl_DecrRefCount(obj);
}
#endif