/*-
 * Copyright (c) 2001 Jordan DeLong
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include "tasklist.h"

/* handler for command entries */
static void handler_task(tasklist_t *tasklist, param_t *param, int type)
{
	param_t *dat;
	char *tmp1, *tmp2;

	/*
	 * command entries are formed like so:
	 * 	param "command" "text" { param "dat" "value"; }
	 * text is the visible text, value is the program to launch.
	 */
	if (param->subparams.count != 1)
   {
		PWARN("invalid subparam structure for 'command', 1 subparam named dat expected");
		return;
	}
	dat = param->subparams.params[0];
	if (strcmp(dat->name, "dat") != 0)
   {
		PWARN("subparam for 'command' must be called dat");
		return;
	}

	if ((tmp1 = strdup(param->value)) == NULL	|| (tmp2 = strdup(dat->value)) == NULL)
   {
		PWARN("out of memory in parseparams, command");
		if (tmp1) free(tmp1);
		return;
	}

	if (tasklist_addent(tasklist, POS_LAST, type, tmp1, (void *) tmp2) == NULL)
   {
		free(tmp1);
		free(tmp2);
	}
}

/* handler for subtasklists */
static void handler_subtasklist(tasklist_t *tasklist, param_t *param, int type)
{
	tasklist_t *subtasklist;
	char *tmp;

	/*
	 * subtasklists are formed like so:
	 *	param "subtasklist" "text" { ...more param stuff here... }
	 * so we just recurse on this function and take it's tasklist and add it
	 */
	if ((subtasklist = tasklist_create()) == NULL)
   {
		PWARN("couldn't get memory for subtasklist %s", param->value);
		return;
	}

	/* parse the subparams of the subtasklist */
	tasklist_parseparams(subtasklist, param);
	if ((tmp = strdup(param->value)) == NULL)
		goto free1;

	if (tasklist_addent(tasklist, POS_LAST, type, tmp, (void *) subtasklist) == NULL)
		goto free2;
	return;

free2:
	free(tmp);
free1:
	tasklist_delete(subtasklist);
	return;
}


/* table of tasklist types and handler functions */
static struct paramhndlr
{
	char	*text;
	int	type;
	void	(*handler)(tasklist_t *tasklist, param_t *param, int type);
} handlers[] = {
	{"task",	         ET_TASK,          handler_task},
	{"subtasklist",	ET_SUBTASKLIST,	handler_subtasklist},
};

/* parse a tasklist tree, this can be recursed */
void tasklist_parseparams(tasklist_t *tasklist, param_t *topparam)
{
	param_t *param;
	int i, n;
   int done;

	/* go through every subparam and handle it */
	SUBPARAMS_FOREACH(i, param, &topparam->subparams)
   {
		for (n = 0; !done && n < sizeof(handlers) / sizeof(struct paramhndlr); n++)
      {
         done = 0;
			if (strcmp(handlers[n].text, param->name) == 0)
         {
				handlers[n].handler(tasklist, param, handlers[n].type);
				done = 1;
			}
		}
      if( !done )
      {
         PWARN("ignoring unknown parameter type %s, under %s", param->name, topparam->name);
      }
	}
}
