/* 
 * The following is a modified version of:
 *
 * tkConfig.c --
 *
 * Modifications are copyright (c) 1994 Malcolm Beattie, all rights reserved.
 *
 *	This file contains the Tk_ConfigureWidget procedure.
 *
 * Copyright (c) 1990-1993 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

#ifndef lint
static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkConfig.c,v 1.37 93/10/14 11:10:05 ouster Exp $ SPRITE (Berkeley)";
#endif

#include "tkConfig.h"
#include "tkperl.h"

extern Tcl_Interp *tclinterp;

/*
 * Values for "flags" field of Tk_ConfigSpec structures.  Be sure
 * to coordinate these values with those defined in tk.h
 * (TK_CONFIG_COLOR_ONLY, etc.).  There must not be overlap!
 *
 * ALREADYUID -		Non-zero means (char *) things have been
 *			converted to Tk_Uid's.
 */

#define ALREADYUID		0x20

/*
 * Forward declarations for procedures defined later in this file:
 */

STATIC void		DoConfig _ANSI_ARGS_((Tk_Window tkwin,
			    Tk_ConfigSpec *specPtr,
			    Tk_Uid value, int valueIsUid, char *widgRec));
STATIC Tk_ConfigSpec *	FindConfigSpec _ANSI_ARGS_ ((Tk_ConfigSpec *specs,
			    char *argvName, int needFlags, int hateFlags));
STATIC int		TkpFormatConfigInfo _ANSI_ARGS_ ((AV *av, int sp,
			    Tk_Window tkwin, Tk_ConfigSpec *specPtr,
			    char *widgRec));

int
Tk_ConfigureWidget(interp, tkwin, specs, argc, argv, widgRec, flags)
    Tcl_Interp *interp;		/* Interpreter for error reporting. */
    Tk_Window tkwin;		/* Window containing widget (needed to
				 * set up X resources). */
    Tk_ConfigSpec *specs;	/* Describes legal options. */
    int argc;			/* Number of elements in argv. */
    char **argv;		/* Command-line options. */
    char *widgRec;		/* Record whose fields are to be
				 * modified.  Values must be properly
				 * initialized. */
    int flags;			/* Used to specify additional flags
				 * that must be present in config specs
				 * for them to be considered.  Also,
				 * may have TK_CONFIG_ARGV_ONLY set. */
{
    croak("shouldn't be in Tk_ConfigureWidget");
    return 0;
}

void
TkpConfigureWidget(tkwin, specs, sp, items, widgRec, flags)
    Tk_Window tkwin;		/* Window containing widget (needed to
				 * set up X resources). */
    Tk_ConfigSpec *specs;	/* Describes legal options. */
    int sp;
    int items;
    char *widgRec;		/* Record whose fields are to be
				 * modified.  Values must be properly
				 * initialized. */
    int flags;			/* Used to specify additional flags
				 * that must be present in config specs
				 * for them to be considered.  Also,
				 * may have TK_CONFIG_ARGV_ONLY set. */
{
    register Tk_ConfigSpec *specPtr;
    Tk_Uid value;		/* Value of option from database. */
    int needFlags;		/* Specs must contain this set of flags
				 * or else they are not considered. */
    int hateFlags;		/* If a spec contains any bits here, it's
				 * not considered. */
    int i;
    SV **st = stack_base + sp;

    needFlags = flags & ~(TK_CONFIG_USER_BIT - 1);
    if (Tk_GetColorModel(tkwin) != TK_COLOR) {
	hateFlags = TK_CONFIG_COLOR_ONLY;
    } else {
	hateFlags = TK_CONFIG_MONO_ONLY;
    }

    /*
     * Pass one:  scan through all the option specs, replacing strings
     * with Tk_Uids (if this hasn't been done already) and clearing
     * the TK_CONFIG_OPTION_SPECIFIED flags.
     */

    for (specPtr = specs; specPtr->type != TK_CONFIG_END; specPtr++) {
	if (!(specPtr->specFlags & ALREADYUID)
	    && (specPtr->argvName != NULL)) {
	    if (specPtr->dbName != NULL) {
		specPtr->dbName = Tk_GetUid(specPtr->dbName);
	    }
	    if (specPtr->dbClass != NULL) {
		specPtr->dbClass = Tk_GetUid(specPtr->dbClass);
	    }
	    if (specPtr->defValue != NULL) {
		specPtr->defValue = Tk_GetUid(specPtr->defValue);
	    }
	}
	specPtr->specFlags = (specPtr->specFlags & ~TK_CONFIG_OPTION_SPECIFIED)
		| ALREADYUID;
    }

    /*
     * Pass two:  scan through all of the arguments, processing those
     * that match entries in the specs.
     */

    for (i = 1; i <= items; i += 2) {
	specPtr = FindConfigSpec(specs, SvPV(st[i], na), needFlags, hateFlags);
	if (!specPtr)
	    croak("no such configuration option");

	/*
	 * Process the entry.
	 */

	if (i == items)
	    croak("last configuration option given no value");

	DoConfig(tkwin, specPtr, (char *) st[i+1], 0, widgRec);
	specPtr->specFlags |= TK_CONFIG_OPTION_SPECIFIED;
    }

    /*
     * Pass three:  scan through all of the specs again;  if no
     * command-line argument matched a spec, then check for info
     * in the option database.  If there was nothing in the
     * database, then use the default.
     */

    if (!(flags & TK_CONFIG_ARGV_ONLY)) {
	for (specPtr = specs; specPtr->type != TK_CONFIG_END; specPtr++) {
	    if ((specPtr->specFlags & TK_CONFIG_OPTION_SPECIFIED)
		    || (specPtr->argvName == NULL)
		    || (specPtr->type == TK_CONFIG_SYNONYM)) {
		continue;
	    }
	    if (((specPtr->specFlags & needFlags) != needFlags)
		    || (specPtr->specFlags & hateFlags)) {
		continue;
	    }
	    value = NULL;
	    if (specPtr->dbName != NULL) {
		value = Tk_GetOption(tkwin, specPtr->dbName, specPtr->dbClass);
	    }
	    if (value != NULL) {
		DoConfig(tkwin, specPtr, value, 1, widgRec);
	    } else {
		value = specPtr->defValue;
		if ((value != NULL) && !(specPtr->specFlags
			& TK_CONFIG_DONT_SET_DEFAULT)) {
		    DoConfig(tkwin, specPtr, value, 1, widgRec);
		}
	    }
	}
    }
}

/*
 *--------------------------------------------------------------
 *
 * FindConfigSpec --
 *
 *	Search through a table of configuration specs, looking for
 *	one that matches a given argvName.
 *
 * Results:
 *	The return value is a pointer to the matching entry, or NULL
 *	if nothing matched.  In that case an error message is left
 *	in interp->result.
 *
 * Side effects:
 *	None.
 *
 *--------------------------------------------------------------
 */

static Tk_ConfigSpec *
FindConfigSpec(specs, argvName, needFlags, hateFlags)
    Tk_ConfigSpec *specs;	/* Pointer to table of configuration
				 * specifications for a widget. */
    char *argvName;		/* Name (suitable for use in a "config"
				 * command) identifying particular option. */
    int needFlags;		/* Flags that must be present in matching
				 * entry. */
    int hateFlags;		/* Flags that must NOT be present in
				 * matching entry. */
{
    register Tk_ConfigSpec *specPtr;
    register char c;		/* First character of current argument. */
    Tk_ConfigSpec *matchPtr;	/* Matching spec, or NULL. */
    int length;

    c = argvName[1];
    length = strlen(argvName);
    matchPtr = NULL;
    for (specPtr = specs; specPtr->type != TK_CONFIG_END; specPtr++) {
	if (specPtr->argvName == NULL) {
	    continue;
	}
	if ((specPtr->argvName[1] != c)
		|| (strncmp(specPtr->argvName, argvName, length) != 0)) {
	    continue;
	}
	if (((specPtr->specFlags & needFlags) != needFlags)
		|| (specPtr->specFlags & hateFlags)) {
	    continue;
	}
	if (specPtr->argvName[length] == 0) {
	    matchPtr = specPtr;
	    goto gotMatch;
	}
	if (matchPtr != NULL) {
	    croak("ambiguous configuration option");
	}
	matchPtr = specPtr;
    }

    if (matchPtr == NULL) {
	croak("unknown configuration option");
    }

    /*
     * Found a matching entry.  If it's a synonym, then find the
     * entry that it's a synonym for.
     */

    gotMatch:
    specPtr = matchPtr;
    if (specPtr->type == TK_CONFIG_SYNONYM) {
	for (specPtr = specs; ; specPtr++) {
	    if (specPtr->type == TK_CONFIG_END) {
		croak("no synonym for configuration option");
	    }
	    if ((specPtr->dbName == matchPtr->dbName) 
		    && (specPtr->type != TK_CONFIG_SYNONYM)
		    && ((specPtr->specFlags & needFlags) == needFlags)
		    && !(specPtr->specFlags & hateFlags)) {
		break;
	    }
	}
    }
    return specPtr;
}

/*
 *--------------------------------------------------------------
 *
 * DoConfig --
 *
 *	This procedure applies a single configuration option
 *	to a widget record.
 *
 * Side effects:
 *	WidgRec is modified as indicated by specPtr and value.
 *	The old value is recycled, if that is appropriate for
 *	the value type.
 *
 *--------------------------------------------------------------
 */

void DoConfig(tkwin, specPtr, value, valueIsUid, widgRec)
    Tk_Window tkwin;		/* Window containing widget (needed to
				 * set up X resources). */
    Tk_ConfigSpec *specPtr;	/* Specifier to apply. */
    char *value;		/* Value to use to fill in widgRec. */
    int valueIsUid;		/* Non-zero means value is a Tk_Uid;
				 * zero means it's an SV*. */
    char *widgRec;		/* Record whose fields are to be
				 * modified.  Values must be properly
				 * initialized. */
{
    char *ptr;
    Tk_Uid uid;
    int nullValue;
    SV *sv;
    

    if (!valueIsUid)
    {
	sv = (SV *) value;
	value = SvPV(sv, na);
    }
    
    nullValue = 0;
    if ((*value == 0) && (specPtr->specFlags & TK_CONFIG_NULL_OK)) {
	nullValue = 1;
    }

    do {
	ptr = widgRec + specPtr->offset;
	switch (specPtr->type) {
	    /* need to rewrite to use genuine perl types */
	    case TK_CONFIG_BOOLEAN:
		if (Tcl_GetBoolean(tclinterp, value, (int *) ptr) != TCL_OK) {
		    croak("bad Boolean");
		}
		break;
	    case TK_CONFIG_INT:
		if (!valueIsUid)
		    *(int *) ptr = (int) SvIV(sv);
		else if (Tcl_GetInt(tclinterp, value, (int *) ptr) != TCL_OK)
		    croak("bad integer");
		break;
	    case TK_CONFIG_DOUBLE:
		if (!valueIsUid)
		    *(double *) ptr = (double) SvNV(sv);
		else if (Tcl_GetDouble(tclinterp, value, (double *) ptr)
			 != TCL_OK)
		    croak("bad double");
		break;
	    case TK_CONFIG_STRING: {
		char *old, *new;

		if (nullValue) {
		    new = NULL;
		} else {
		    new = (char *) ckalloc((unsigned) (strlen(value) + 1));
		    strcpy(new, value);
		}
		old = *((char **) ptr);
		if (old != NULL) {
		    ckfree(old);
		}
		*((char **) ptr) = new;
		break;
	    }
	    case TK_CONFIG_UID:
		if (nullValue) {
		    *((Tk_Uid *) ptr) = NULL;
		} else {
		    uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		    *((Tk_Uid *) ptr) = uid;
		}
		break;
	    case TK_CONFIG_COLOR: {
		XColor *newPtr, *oldPtr;

		if (nullValue) {
		    newPtr = NULL;
		} else {
		    uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		    newPtr = Tk_GetColor(tclinterp, tkwin, (Colormap) None, uid);
		    if (newPtr == NULL) {
			croak("bad colour");
		    }
		}
		oldPtr = *((XColor **) ptr);
		if (oldPtr != NULL) {
		    Tk_FreeColor(oldPtr);
		}
		*((XColor **) ptr) = newPtr;
		break;
	    }
	    case TK_CONFIG_FONT: {
		XFontStruct *newPtr, *oldPtr;

		if (nullValue) {
		    newPtr = NULL;
		} else {
		    uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		    newPtr = Tk_GetFontStruct(tclinterp, tkwin, uid);
		    if (newPtr == NULL) {
			croak("bad font");
		    }
		}
		oldPtr = *((XFontStruct **) ptr);
		if (oldPtr != NULL) {
		    Tk_FreeFontStruct(oldPtr);
		}
		*((XFontStruct **) ptr) = newPtr;
		break;
	    }
	    case TK_CONFIG_BITMAP: {
		Pixmap new, old;

		if (nullValue) {
		    new = None;
	        } else {
		    uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		    new = Tk_GetBitmap(tclinterp, tkwin, uid);
		    if (new == None) {
			croak("bad bitmap");
		    }
		}
		old = *((Pixmap *) ptr);
		if (old != None) {
		    Tk_FreeBitmap(Tk_Display(tkwin), old);
		}
		*((Pixmap *) ptr) = new;
		break;
	    }
	    case TK_CONFIG_BORDER: {
		Tk_3DBorder new, old;

		if (nullValue) {
		    new = NULL;
		} else {
		    uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		    new = Tk_Get3DBorder(tclinterp, tkwin, (Colormap) None, uid);
		    if (new == NULL) {
			croak("bad border");
		    }
		}
		old = *((Tk_3DBorder *) ptr);
		if (old != NULL) {
		    Tk_Free3DBorder(old);
		}
		*((Tk_3DBorder *) ptr) = new;
		break;
	    }
	    case TK_CONFIG_RELIEF:
		uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		if (Tk_GetRelief(tclinterp, uid, (int *) ptr) != TCL_OK) {
		    croak("bad relief");
		}
		break;
	    case TK_CONFIG_CURSOR:
	    case TK_CONFIG_ACTIVE_CURSOR: {
		Cursor new, old;

		if (nullValue) {
		    new = None;
		} else {
		    uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		    new = Tk_GetCursor(tclinterp, tkwin, uid);
		    if (new == None) {
			croak("bad cursor");
		    }
		}
		old = *((Cursor *) ptr);
		if (old != None) {
		    Tk_FreeCursor(Tk_Display(tkwin), old);
		}
		*((Cursor *) ptr) = new;
		if (specPtr->type == TK_CONFIG_ACTIVE_CURSOR) {
		    Tk_DefineCursor(tkwin, new);
		}
		break;
	    }
	    case TK_CONFIG_JUSTIFY:
		uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		if (Tk_GetJustify(tclinterp, uid, (Tk_Justify *) ptr) != TCL_OK) {
		    croak("bad justify");
		}
		break;
	    case TK_CONFIG_ANCHOR:
		uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		if (Tk_GetAnchor(tclinterp, uid, (Tk_Anchor *) ptr) != TCL_OK) {
		    croak("bad anchor");
		}
		break;
	    case TK_CONFIG_CAP_STYLE:
		uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		if (Tk_GetCapStyle(tclinterp, uid, (int *) ptr) != TCL_OK) {
		    croak("bad cap style");
		}
		break;
	    case TK_CONFIG_JOIN_STYLE:
		uid = valueIsUid ? (Tk_Uid) value : Tk_GetUid(value);
		if (Tk_GetJoinStyle(tclinterp, uid, (int *) ptr) != TCL_OK) {
		    croak("bad join style");
		}
		break;
	    case TK_CONFIG_PIXELS:
		if (Tk_GetPixels(tclinterp, tkwin, value, (int *) ptr)
			!= TCL_OK) {
		    croak("bad pixels");
		}
		break;
	    case TK_CONFIG_MM:
		if (Tk_GetScreenMM(tclinterp, tkwin, value, (double *) ptr)
			!= TCL_OK) {
		    croak("bad MM");
		}
		break;
	    case TK_CONFIG_WINDOW: {
		Tk_Window tkwin2;

		if (nullValue) {
		    tkwin2 = NULL;
		} else {
		    if (!valueIsUid && SvOK(sv) && SvROK(sv))
		    {
			SV *sv2;
			
			sv2 = (SV *) SvRV(sv);
			tkwin2 = Tk_NameToWindow(tclinterp, SvPV(sv2, na), tkwin);
		    }
		    else
			tkwin2 = Tk_NameToWindow(tclinterp, value, tkwin);
		    if (tkwin2 == NULL)
			croak("bad window");
		}
		*((Tk_Window *) ptr) = tkwin2;
		break;
	    }
	    case TK_CONFIG_CUSTOM:
		if ((*specPtr->customPtr->parseProc)(
			specPtr->customPtr->clientData, tclinterp, tkwin,
			value, widgRec, specPtr->offset) != TCL_OK) {
		    croak("bad custom option value");
		}
		break;

	    case TKP_CONFIG_METHOD: {
		/* Similar to TK_CONFIG_STRING except we have to
		 * invalidate the cvcache when method changes */
		char *old, *new;

		if (nullValue)
		    new = NULL;
		else
		{
		    new = (char *) ckalloc((unsigned) (strlen(value) + 1));
		    strcpy(new, value);
		}
		old = ((struct tkpcallback *) ptr)->method;
		if (old)
		    ckfree(old);

		((struct tkpcallback *) ptr)->method = new;
		((struct tkpcallback *) ptr)->cvcache = Nullcv;
		break;
	    }
	    case TKP_CONFIG_SLAVE:
		((struct tkpcallback *) ptr)->slave = newSVsv(sv);
		((struct tkpcallback *) ptr)->cvcache = Nullcv;
		break;

	    case TKP_CONFIG_VARIABLE:
		{
		    /*
		     * If string to sv lookup gets implemented, be careful
		     * to do the proper tainting to stop option table values
		     * from giving access to arbitrary variables.
		     * For now, however,...
		     */
		    SV *oldsv = *(SV **) ptr;
		    
		    if (valueIsUid)
			croak("cannot identify perl variable with string");
		    if (oldsv != sv)
		    {
			if (oldsv)
			    SvREFCNT_dec(sv);
			*(SV **) ptr = SvOK(sv) ? SvREFCNT_inc(sv) : Nullsv;
		    }
		    break;
		}
		
	    default:
		croak("bad value in config table");
	}
	specPtr++;
    } while ((specPtr->argvName == NULL) && (specPtr->type != TK_CONFIG_END));
}

/*
 *--------------------------------------------------------------
 *
 * TkpConfigureInfo --
 *
 *	Return information (on the stack) about the configuration
 *	options for a window, and their current values.
 *
 * Results:
 *	Returns new top of stack. For a non-NULL argvName, pushes
 *	five values onto the stack (option name, database name,
 *	database class, default value, current value (undef if none))
 *	or else two values (option name, full length option name.)
 *	If argvName is NULL, for each possible configuration option
 *	pushes a ref to an anonymous list with information as above.
 *
 * Side effects:
 *	The stack can get reallocated. stack_base and friends are
 *      recovered but 'st' would need resetting.
 *
 *--------------------------------------------------------------
 */

int TkpConfigureInfo(sp, tkwin, specs, widgRec, argvName, flags)
    int sp;                     /* stack offset position for first result */
    Tk_Window tkwin;		/* Window corresponding to widgRec. */
    Tk_ConfigSpec *specs;	/* Describes legal options. */
    char *widgRec;		/* Record whose fields contain current
				 * values for options. */
    char *argvName;		/* If non-NULL, indicates a single option
				 * whose info is to be returned.  Otherwise
				 * info is returned for all options. */
    int flags;			/* Used to specify additional flags
				 * that must be present in config specs
				 * for them to be considered. */
{
    register Tk_ConfigSpec *specPtr;
    int needFlags, hateFlags;
    AV *av;

    needFlags = flags & ~(TK_CONFIG_USER_BIT - 1);
    if (Tk_GetColorModel(tkwin) != TK_COLOR) {
	hateFlags = TK_CONFIG_COLOR_ONLY;
    } else {
	hateFlags = TK_CONFIG_MONO_ONLY;
    }

    /*
     * If information is only wanted for a single configuration
     * spec, then handle that one spec specially.
     */

    if (argvName)
    {
	specPtr = FindConfigSpec(specs, argvName, needFlags, hateFlags);
	if (!specPtr)
	    croak("TkpConfigureInfo got a null specPtr");
	sp = TkpFormatConfigInfo(stack, sp, tkwin, specPtr, widgRec);
	RECOVERSTACK;
	return sp;
    }

    /*
     * Loop through all the specs, creating a big list with all
     * their information.
     */
    
    for (specPtr = specs; specPtr->type != TK_CONFIG_END; specPtr++)
    {
	if (argvName && (specPtr->argvName != argvName)
	    || (specPtr->specFlags & needFlags) != needFlags
	    || (specPtr->specFlags & hateFlags)
	    || !specPtr->argvName)
	    continue;
	
	av = newAV();
	(void) TkpFormatConfigInfo(av, 0, tkwin, specPtr, widgRec);
	av_store(stack, sp++, sv_2mortal(newRV((SV *) av)));
    }
    sp--;
    RECOVERSTACK;
    return sp;
}

/*
 * Takes a spec pointer into a spec table and a widget record and
 * pushes onto av either two or five values corresponding to the
 * option/class names and current value (or just the synonym and real
 * option name if passed a synonym). av will be either the stack
 * or a newly created AV (for returning all config info as a list
 * of refs to anonymous lists). Returns new top of av.
 */
int TkpFormatConfigInfo(av, sp, tkwin, specPtr, widgRec)
AV *av;
int sp;                                 /* av offset to push first result */
Tk_Window tkwin;			/* Window corresponding to widget. */
register Tk_ConfigSpec *specPtr;	/* Pointer to information describing
					 * option. */
char *widgRec;				/* Pointer to record holding current
					 * values of info for widget. */
{
    char *ptr;
    Tcl_FreeProc *freeProc = (Tcl_FreeProc *) NULL;
    
    av_store(av, sp++, newSVpv(specPtr->argvName, 0));
    av_store(av, sp, newSVpv(specPtr->dbName, 0));
    if (specPtr->type == TK_CONFIG_SYNONYM)
	return sp;

    av_store(av, ++sp, newSVpv(specPtr->dbClass, 0));
    av_store(av, ++sp, specPtr->defValue ?
	     newSVpv(specPtr->defValue, 0) : sv_mortalcopy(&sv_undef));
    sp++;
    
    ptr = widgRec + specPtr->offset;
    switch (specPtr->type) {
      case TK_CONFIG_BOOLEAN:
	av_store(av, sp, sv_mortalcopy(*((int *) ptr) ? &sv_yes : &sv_no));
	break;

      case TK_CONFIG_INT:
	av_store(av, sp, newSViv(*(int *) ptr));
	break;

      case TK_CONFIG_DOUBLE:
	av_store(av, sp, newSVnv(*(double *) ptr));
	break;

      case TK_CONFIG_STRING:
	av_store(av, sp, newSVpv(*(char **) ptr, 0));
	break;

      case TK_CONFIG_UID: {
	  Tk_Uid uid = *((Tk_Uid *) ptr);
	  av_store(av, sp, newSVpv(uid, 0));
	  break;
      }
      
      case TK_CONFIG_COLOR: {
	  XColor *colorPtr = *((XColor **) ptr);

	  av_store(av, sp, colorPtr ?
		   newSVpv(Tk_NameOfColor(colorPtr), 0)
		   : sv_mortalcopy(&sv_undef));
	  break;
      }

      case TK_CONFIG_FONT: {
	  XFontStruct *fontStructPtr = *((XFontStruct **) ptr);

	  av_store(av, sp, fontStructPtr ?
		   newSVpv(Tk_NameOfFontStruct(fontStructPtr), 0)
		   : sv_mortalcopy(&sv_undef));     
	  break;
      }

      case TK_CONFIG_BITMAP: {
	  Pixmap pixmap = *((Pixmap *) ptr);

	  av_store(av, sp, (pixmap != None) ?
		   newSVpv(Tk_NameOfBitmap(Tk_Display(tkwin), pixmap), 0)
		   : sv_mortalcopy(&sv_undef));
	  break;
      }

      case TK_CONFIG_BORDER: {
	  Tk_3DBorder border = *((Tk_3DBorder *) ptr);

	  av_store(av, sp, border ?
		   newSVpv(Tk_NameOf3DBorder(border), 0)
		   : sv_mortalcopy(&sv_undef));
	  break;
      }
	
      case TK_CONFIG_RELIEF:
	av_store(av, sp, newSVpv(Tk_NameOfRelief(*((int *) ptr)), 0));
	break;
	
      case TK_CONFIG_CURSOR:
      case TK_CONFIG_ACTIVE_CURSOR: {
	  Cursor cursor = *((Cursor *) ptr);

	  av_store(av, sp, (cursor != None) ?
		   newSVpv(Tk_NameOfCursor(Tk_Display(tkwin), cursor), 0)
		   : sv_mortalcopy(&sv_undef));
	  break;
      }

      case TK_CONFIG_JUSTIFY:
	av_store(av, sp,
		 newSVpv(Tk_NameOfJustify(*((Tk_Justify *) ptr)), 0));
	break;

      case TK_CONFIG_ANCHOR:
	av_store(av, sp,
		 newSVpv(Tk_NameOfAnchor(*((Tk_Anchor *) ptr)), 0));
	break;

      case TK_CONFIG_CAP_STYLE:
	av_store(av, sp, newSVpv(Tk_NameOfCapStyle(*((int *) ptr)), 0));
	break;
	
      case TK_CONFIG_JOIN_STYLE:
	av_store(av, sp, newSVpv(Tk_NameOfJoinStyle(*((int *) ptr)), 0));
	break;
	
      case TK_CONFIG_PIXELS:
	av_store(av, sp, newSViv(*(int *) ptr));
	break;
	
      case TK_CONFIG_MM:
	av_store(av, sp, newSVnv(*(double *) ptr));
	break;

      case TK_CONFIG_WINDOW: {
	  Tk_Window tkwin = *((Tk_Window *) ptr);

	  av_store(av, sp, tkwin ?
		   newSVpv(Tk_PathName(tkwin), 0) : sv_mortalcopy(&sv_undef));
      }

      case TK_CONFIG_CUSTOM:
	av_store(av, sp, newSVpv((*specPtr->customPtr->printProc)(
			          specPtr->customPtr->clientData, tkwin,
                                  widgRec, specPtr->offset, &freeProc), 0));
	break;

      case TKP_CONFIG_VARIABLE:
	{
	    SV *sv = *(SV **) ptr;
	    
	    av_store(av, sp, sv_mortalcopy(sv ? sv : &sv_undef));
	    break;
	}
	
      case TKP_CONFIG_METHOD:
	av_store(av, sp, newSVpv(((struct tkpcallback *) ptr)->method, 0));
	break;
	
      case TKP_CONFIG_SLAVE:
	{
	    SV *sv = ((struct tkpcallback *) ptr)->slave;

	    av_store(av, sp, sv_mortalcopy(sv ? sv : &sv_undef));
	    break;
	}
	
      default: 
	croak("unknown type");
	/* NOTREACHED */
    }

    return sp;
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_FreeOptions --
 *
 *	Free up all resources associated with configuration options.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Any resource in widgRec that is controlled by a configuration
 *	option (e.g. a Tk_3DBorder or XColor) is freed in the appropriate
 *	fashion.
 *
 *----------------------------------------------------------------------
 */

	/* ARGSUSED */
void
Tk_FreeOptions(specs, widgRec, display, needFlags)
    Tk_ConfigSpec *specs;	/* Describes legal options. */
    char *widgRec;		/* Record whose fields contain current
				 * values for options. */
    Display *display;		/* X display; needed for freeing some
				 * resources. */
    int needFlags;		/* Used to specify additional flags
				 * that must be present in config specs
				 * for them to be considered. */
{
    register Tk_ConfigSpec *specPtr;
    char *ptr;

    for (specPtr = specs; specPtr->type != TK_CONFIG_END; specPtr++) {
	if ((specPtr->specFlags & needFlags) != needFlags) {
	    continue;
	}
	ptr = widgRec + specPtr->offset;
	switch (specPtr->type) {
	    case TK_CONFIG_STRING:
	        if (*((char **) ptr) != NULL) {
		    ckfree(*((char **) ptr));
		    *((char **) ptr) = NULL;
		}
		break;
	    case TK_CONFIG_COLOR:
		if (*((XColor **) ptr) != NULL) {
		    Tk_FreeColor(*((XColor **) ptr));
		    *((XColor **) ptr) = NULL;
		}
		break;
	    case TK_CONFIG_FONT:
		if (*((XFontStruct **) ptr) != NULL) {
		    Tk_FreeFontStruct(*((XFontStruct **) ptr));
		    *((XFontStruct **) ptr) = NULL;
		}
		break;
	    case TK_CONFIG_BITMAP:
		if (*((Pixmap *) ptr) != None) {
		    Tk_FreeBitmap(display, *((Pixmap *) ptr));
		    *((Pixmap *) ptr) = None;
		}
		break;
	    case TK_CONFIG_BORDER:
		if (*((Tk_3DBorder *) ptr) != NULL) {
		    Tk_Free3DBorder(*((Tk_3DBorder *) ptr));
		    *((Tk_3DBorder *) ptr) = NULL;
		}
		break;
	    case TK_CONFIG_CURSOR:
	    case TK_CONFIG_ACTIVE_CURSOR:
		if (*((Cursor *) ptr) != None) {
		    Tk_FreeCursor(display, *((Cursor *) ptr));
		    *((Cursor *) ptr) = None;
		}
		break;
	    case TKP_CONFIG_METHOD:
		if (((struct tkpcallback *) ptr)->method != NULL) {
		    ckfree(((struct tkpcallback *) ptr)->method);
		    ((struct tkpcallback *) ptr)->method = NULL;
		}
		break;
	    case TKP_CONFIG_SLAVE:
		if (((struct tkpcallback *) ptr)->slave != Nullsv) {
		    SvREFCNT_dec(((struct tkpcallback *) ptr)->slave);
		    ((struct tkpcallback *) ptr)->slave = Nullsv;
		}
		break;
	    case TKP_CONFIG_VARIABLE:
	        if (*((SV **) ptr) != Nullsv) {
		    SvREFCNT_dec(*(SV **) ptr);
		    *(SV **) ptr = Nullsv;
		}
	        break;
	}
    }
}

int
Tk_ConfigureInfo(interp, tkwin, specs, widgRec, argvName, flags)
    Tcl_Interp *interp;		/* Interpreter for error reporting. */
    Tk_Window tkwin;		/* Window corresponding to widgRec. */
    Tk_ConfigSpec *specs;	/* Describes legal options. */
    char *widgRec;		/* Record whose fields contain current
				 * values for options. */
    char *argvName;		/* If non-NULL, indicates a single option
				 * whose info is to be returned.  Otherwise
				 * info is returned for all options. */
    int flags;			/* Used to specify additional flags
				 * that must be present in config specs
				 * for them to be considered. */
{
    croak("shouldn't call Tk_ConfigureInfo");
    return 0;
}

int
Tk_ButtonCmd(clientData, interp, argc, argv)
    ClientData clientData;	/* Main window associated with
				 * interpreter. */
    Tcl_Interp *interp;		/* Current interpreter. */
    int argc;			/* Number of arguments. */
    char **argv;		/* Argument strings. */
{
    croak("shouldn't call Tk_ButtonCmd");
    return 0;
}
