/*
   fakefs - Astonishingly Slow File System

   Copyright (C) 2001 Zygo Blaxell

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

/*
   fakevfs-tcl:  language bindings for Tcl to the VFS layer

   This module implements Tcl language bindings for FakeVFS operations.
   It is primarily used by the test suite, but can also be used as a
   stand-alone Tcl shared library loadable module.
*/

#include "config.h"
#include "system.h"

#include "fakefs-tcl.h"
#include "fakevfs.h"

#ifndef HAVE_TCL_NEWWIDEINTOBJ
#define Tcl_NewWideIntObj Tcl_NewLongObj
#endif

/* Prototypes */
int Fakevfs_Init(Tcl_Interp *interp);

typedef struct {
	Fake_Env *	fe;
	Tcl_Command	token;
	void *		magic;
} Fake_Tcl;

void *Fake_Tcl_magic = &Fake_Tcl_magic;

static inline void assert_fake_tcl_magic(Fake_Tcl *tfe) {
	if (tfe) 
		assert(tfe->magic == Fake_Tcl_magic);
}

#define CREATE_COMMAND(name) do { \
	if (Tcl_CreateObjCommand(interp, #name, name##_tcl, 0, 0) == NULL) \
		return TCL_ERROR; \
} while (0)

#define DECLARE_COMMAND(name) \
	static int name##_tcl(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])

#define COUNT_ARGS(count, good, message) do { \
	if (objc != count) { \
		Tcl_WrongNumArgs(interp, good, objv, message); \
		return TCL_ERROR; \
	} \
} while (0)

#define MIN_ARGS(count, good, message) do { \
	if (objc < count) { \
		Tcl_WrongNumArgs(interp, good, objv, message); \
		return TCL_ERROR; \
	} \
} while (0)

static void fakevfs_delete_tcl(ClientData clientData) {
	Fake_Tcl *tfe = clientData;
	assert_fake_tcl_magic(tfe);
	fakevfs_env_close(tfe->fe);
	tfe->fe = 0;
}

static int fakevfs_dispatch_tcl(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
	Fake_Tcl *tfe;
	static char *command_names[] = {
		"close",
		"ino_new",
		0
	};
	int command_index, status;

	tfe = clientData;
	assert(tfe);
	
	MIN_ARGS(2, 1, "command args...");

	status = Tcl_GetIndexFromObj(interp, objv[1], command_names, "command", 0, &command_index);
	if (status != TCL_OK) 
		return status;

	switch (command_index) {
		default:
			Tcl_SetResult(interp, "command_index out of range, how did this happen?", TCL_STATIC);
			return TCL_ERROR;

		case 0: /* close */
			COUNT_ARGS(2, 2, "close");
			return Tcl_DeleteCommandFromToken(interp, tfe->token);

		case 1: /* ino_new */
			COUNT_ARGS(2, 2, "ino_new");
			{
				Fake_Inode ino = -1;

				status = fakevfs_txn_begin(tfe->fe, 0);
				if (status) goto error_return;
				status = fakevfs_ino_new(tfe->fe, &ino);
				if (status) {
					status = fakevfs_txn_abort(tfe->fe);
					goto error_return;
				}
				status = fakevfs_txn_commit(tfe->fe, 0);
				if (status) goto error_return;

				Tcl_SetObjResult(interp, Tcl_NewWideIntObj(ino));
				return TCL_OK;
			}
	}

	return TCL_OK;

error_return:

	Tcl_SetResult(interp, (char *)fakevfs_env_excuse(tfe->fe), TCL_VOLATILE);
	return TCL_ERROR;
}

DECLARE_COMMAND(fakevfs_env_open) {
	static int counter = 0;
	char name[100];
	Fake_Tcl *tfe;
	CONST char *env_name;
	Fake_Status status;

	assert(!clientData);

	COUNT_ARGS(2, 1, "environmentName");

	env_name = Tcl_GetString(objv[1]);
	
	tfe = (void *)Tcl_Alloc(sizeof(Fake_Tcl));
	assert(tfe);

	tfe->fe = fakevfs_env_create();
	if (!tfe->fe) {
		Tcl_SetResult(interp, Tcl_PosixError(interp), TCL_VOLATILE);
		goto clean_up_mess;
	}

	status = fakevfs_env_open(tfe->fe, env_name);
	if (status != FAKEVFS_OK) {
		Tcl_SetResult(interp, (char *)fakevfs_env_excuse(tfe->fe), TCL_VOLATILE);
		goto clean_up_mess;
	}

	sprintf(name, "fakevfs_fe%d", counter++);
	tfe->token = Tcl_CreateObjCommand(interp, name, fakevfs_dispatch_tcl, tfe, fakevfs_delete_tcl);

	if (tfe->token) {
		Tcl_SetResult(interp, name, TCL_VOLATILE);
		tfe->magic = Fake_Tcl_magic;
		return TCL_OK;
	} else {
		Tcl_ResetResult(interp);
		Tcl_AppendResult(interp, "Could not create fakevfs Tcl handle command", name, 0);
	}

clean_up_mess:

	if (tfe->fe) {
		Fake_Excuse excuse = fakevfs_env_close(tfe->fe);
		if (excuse) {
			Tcl_SetResult(interp, (char *)excuse, TCL_VOLATILE);
		}
	}

	return TCL_ERROR;
}

int Fakevfs_Init(Tcl_Interp *interp)
{
	CREATE_COMMAND(fakevfs_env_open);
	return TCL_OK;
}


