/*
 * Copyright (c) 1994 Malcolm Beattie, all rights reserved.
 */
#include "tkConfig.h"
#include "tkInt.h"
#include "tkperl.h"

extern Tcl_Interp *tclinterp;
extern Tk_Window mainwin;

static int TkpAddFromString P((char *string, int priority));

int TkpOptionCmd(ix, sp, items)
int ix;
int sp;
int items;
{
    SV **st = stack_base + sp;
    int priority;
    
    st[0] = sv_mortalcopy(&sv_undef);
    switch (ix)
    {
      case US_OPTADD:
	if (items < 2 || items > 3)
	    croak("Usage: optadd($pattern, $value [, $priority])");
	if (items == 2)
	    priority = 80;
	else
	{
	    priority = SvIV(st[3]);
	    if (priority < 0 || priority > 100)
		croak("bad priority for optadd");
	}
	Tk_AddOption(mainwin, SvPV(st[1], na), SvPV(st[2], na), priority);
	break;
	
      case US_OPTCLEAR:
	{
	    char *argv[3];
	    
	    if (items != 0)
		croak("Usage: optclear()");

	    /*
	     * We have to call Tk_OptionCmd instead of doing it
	     * ourselves because we have to reset cachedWindow which is
	     * declared static, and hence only visible in, tkOption.c :-(
	     */
	    argv[0] = "option";
	    argv[1] = "clear";
	    argv[2] = NULL;
	    (void) Tk_OptionCmd((ClientData) mainwin, tclinterp, 2, argv);
	    break;
	}

      case US_OPTGET:
	{
	    Tk_Window window;
	    Tk_Uid value;

	    if (items != 3)
		croak("Usage: optget($window, $name, $class)");
	    if (!(window = WrefToWindow(st[1], "Option", 6)))
		croak("bad window in optget");
	    value = Tk_GetOption(window, SvPV(st[2], na), SvPV(st[3], na));
	    if (value)
		st[0] = newSVpv(value, 0);
	    break;
	}
	
      case US_OPTADDSTRING:
	{
	    SV *sv;
	    char *argv[5];

	    if (items != 2)
		croak("Usage: optaddstring($string [, $priority])");
	    
	    if (items == 1)
		priority = 80;
	    else
	    {
		priority = SvIV(st[2]);
		if (priority < 0 || priority > 100)
		    croak("bad priority for optaddstring");
	    }
	    /*
	     * TkpAddFromString trashes the string you pass it.
	     * I think (?) this sv_mortalcopy will make a new copy of
	     * the string if and only if it cannot be safely stolen.
	     */
	    sv = sv_mortalcopy(st[1]);
	    st[0] = sv_mortalcopy(TkpAddFromString(SvPV(sv, na), priority)
				  ? &sv_yes : &sv_no);
	    break;
	}	    
      default:
	croak("bad ix passed");
	/* NOTREACHED */
    }
    return sp;
}

/*
 * AddFromString is in tkOption.c but is declared static so we have to
 * copy it here. Ho hum. We might as well tweak it a little bit...
 * Here comes the copyright for the original routine...
 *
 * 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.
 */


static int
TkpAddFromString(string, priority)
char *string;
int priority;
{
    register char *src, *dst;
    char *name, *value;
    int lineNum;

    src = string;
    lineNum = 1;
    while (1) {

	/*
	 * Skip leading white space and empty lines and comment lines, and
	 * check for the end of the spec.
	 */

	while ((*src == ' ') || (*src == '\t')) {
	    src++;
	}
	if ((*src == '#') || (*src == '!')) {
	    do {
		src++;
		if ((src[0] == '\\') && (src[1] == '\n')) {
		    src += 2;
		    lineNum++;
		}
	    } while ((*src != '\n') && (*src != 0));
	}
	if (*src == '\n') {
	    src++;
	    lineNum++;
	    continue;
	} 
	if (*src == '\0') {
	    break;
	}

	/*
	 * Parse off the option name, collapsing out backslash-newline
	 * sequences of course.
	 */

	dst = name = src;
	while (*src != ':') {
	    if ((*src == '\0') || (*src == '\n'))
		return 0;
	    if ((src[0] == '\\') && (src[1] == '\n')) {
		src += 2;
		lineNum++;
	    } else {
		*dst = *src;
		dst++;
		src++;
	    }
	}

	/*
	 * Eliminate trailing white space on the name, and null-terminate
	 * it.
	 */

	while ((dst != name) && ((dst[-1] == ' ') || (dst[-1] == '\t'))) {
	    dst--;
	}
	*dst = '\0';

	/*
	 * Skip white space between the name and the value.
	 */

	src++;
	while ((*src == ' ') || (*src == '\t')) {
	    src++;
	}
	if (*src == '\0')
	    return 0;

	/*
	 * Parse off the value, squeezing out backslash-newline sequences
	 * along the way.
	 */

	dst = value = src;
	while (*src != '\n') {
	    if (*src == '\0')
		return 0;
	    if ((src[0] == '\\') && (src[1] == '\n')) {
		src += 2;
		lineNum++;
	    } else {
		*dst = *src;
		dst++;
		src++;
	    }
	}
	*dst = 0;

	/*
	 * Enter the option into the database.
	 */

	Tk_AddOption(mainwin, name, value, priority);
	src++;
	lineNum++;
    }
    return 1;
}
