/*

Copyright © 2003, Russell C. Hay
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

i. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 

ii. 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. 

iii. Neither the name of the dead[protocol]society nor the names of its
     contributors may be used to endorse or promote products derived from this
     software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include "stl.h"
#include "action.h"
#include "protocol.h"

#define DEFAULTFACTORY(name, str, ACTIONNAME)										\
	action protocol::name ## ActionFactory(char *s, int len)						\
	{																								\
		action id(ACTIONNAME);																\
		return id;																				\
	}

#define SINGLECOMMAND(cmdltr, str, _len, argparsefun)								\
	action protocol::parse ## cmdltr ## Cmds(char *s, int len)						\
	{																								\
		action retval;																			\
																									\
		if (!strncasecmp(s+1, str, _len))												\
		{																							\
			retval = argparsefun(s+_len+2, len - _len - 3);							\
			return retval;																		\
   	} else																					\
		{																							\
			return parseerrorActionFactory(s,len);										\
		}																							\
	}
	
#define EMPTYFUNCTION(cmd)																	\
	action protocol::cmd(char *s, int len)										\
	{																								\
		printf("Unimplimented function called\n");									\
		return -1;																				\
	}

protocol::protocol()
{
}

protocol::~protocol()
{
}

action protocol::parse(char *s, int len)
{
   int i=0;
	action retval;

   if (len < 0)
      len = strlen(s);

	switch(toupper(s[i]))
	{
		case 'B':
			retval = parseBCmds(s, len); break;
		case 'C':
			retval = parseCCmds(s, len); break;
		case 'D':
			retval = parseDCmds(s, len); break;
		case 'F':
			retval = parseFCmds(s, len); break;
		case 'G':
			retval = parseGCmds(s, len); break;
		case 'H':
			retval = parseHCmds(s, len); break;
		case 'N':
			retval = parseNCmds(s, len); break;
		case 'O':
			retval = parseOCmds(s, len); break;
		case 'P':
			retval = parsePCmds(s, len); break;
		case 'Q':
			retval = parseQCmds(s, len); break;
		case 'R':
			retval = parseRCmds(s, len); break;
		case 'S':
			retval = parseSCmds(s, len); break;
		case 'V':
			retval = parseVCmds(s, len); break;
		default:
			retval = parseerrorActionFactory(s,len);
			break;
	};

	return retval;
}

SINGLECOMMAND(D, "efine", 5, parseDefineArgs);
SINGLECOMMAND(F, "orward", 6, forwardActionFactory);
SINGLECOMMAND(G, "po", 2, parseGPOArgs);
SINGLECOMMAND(R, "eset", 4, resetActionFactory);
SINGLECOMMAND(S, "tatus", 5, statusActionFactory);
SINGLECOMMAND(V, "graph", 5, parseVGraphArgs);
SINGLECOMMAND(Q, "uit", 3, quitActionFactory);

action protocol::parseCCmds(char *s, int len)
{
	action retval;

	switch(toupper(s[1]))
	{
		case 'U':
			if (!strncasecmp(s+2, "rsor", 4))
				retval = parseCursorArgs(s+7, len-7);
			break;
		case 'L':
			if (!strncasecmp(s+2, "ear", 3))
				retval = clearActionFactory(s, len);
			break;
		default:
			retval = parseerrorActionFactory(s,len);
			break;
	}

	return retval;
}

action protocol::parseGPOArgs(char *s, int len)
{
	char *space = 0;
	int gpo, status;

	space = index(s,' ');
	if (space == 0)
	{
		return parseerrorActionFactory(s, len);
	} else {
		*space = '\0';
		gpo = atoi(s);
		if (!strncasecmp(space+2, "ff", 2))
		{
			status = 0;
		} else {
			status = 1;
		}
		action ret(ACTION_GPO);

		ret.AddActionParam(PARAM_GPO, gpo);
		ret.AddActionParam(PARAM_ONOFF, status);

		return ret;
	}
}

action protocol::parseCursorArgs(char *s, int len)
{
	char *space = 0;
	int x, y;

	printf("'%s'/%d\n", s, len);
	space = index(s, ' ');
	if (space == 0)
	{
		return parseerrorActionFactory(s, len);
	} else {
		*space = '\0';
		x = atoi(s);
		y = atoi(space+1);
		action ret(ACTION_CURSOR);
		ret.AddActionParam(PARAM_XPOS, x);
		ret.AddActionParam(PARAM_YPOS, y);
		return ret;
	}
	return -1;
};

DEFAULTFACTORY(forward, "FORWARD", ACTION_FORWARD);
DEFAULTFACTORY(reset, "RESET", ACTION_RESET);
DEFAULTFACTORY(status, "STATUS", ACTION_STATUS);
DEFAULTFACTORY(back, "BACK", ACTION_BACK);
DEFAULTFACTORY(home, "HOME", ACTION_HOME);
DEFAULTFACTORY(nstart, "START KEY NOTIFY", ACTION_NSTART);
DEFAULTFACTORY(nstop, "STOP KEY NOTIFY", ACTION_NSTOP);
DEFAULTFACTORY(parseerror, "Evil Parse Error", ACTION_PARSE);
DEFAULTFACTORY(clear, "CLEAR", ACTION_CLEAR);
DEFAULTFACTORY(quit, "QUIT", ACTION_QUIT);

action protocol::parseVGraphArgs(char *s, int len)
{
	action ret;

	ret = parseGraphArgs(s,len);
	if (ret.GetActionType() != ACTION_PARSE)
		ret.SetActionType(ACTION_VGRAPH);
	// Check bounds, if they are within limits, set action to VGraph, and pass
	// otherwise OOB error

	return ret;
}

action protocol::parseHGraphArgs(char *s, int len)
{
	action ret;

	ret = parseGraphArgs(s,len);
	if (ret.GetActionType() != ACTION_PARSE)
		ret.SetActionType(ACTION_HGRAPH);
	// Check bounds, if they are within limits, set action to VGraph, and pass
	// otherwise OOB error

	return ret;
}

action protocol::parseGraphArgs(char *s, int len)
{
	action ret(-1);
	char *space1 = 0, *space2 = 0;
	int x, y, val;
	
	space1 = index(s, ' ');
	if (space1 == 0)
	{
		return parseerrorActionFactory(s, len);
	} else {
		*space1 = 0;
		space1++;

		space2 = index(space1, ' ');

		if (space2 == 0)
		{
			return parseerrorActionFactory(s, len); 
		} else {
			*space2 = 0;
			space2++;

			x = atoi(s);
			y = atoi(space1);
			val = atoi(space2);

			if (ret.AddActionParam(PARAM_XPOS, x) == -1)
				perror("AddActionParam XPOS");
			if (ret.AddActionParam(PARAM_YPOS, y) == -1)
				perror("AddActionParam YPOS");
			if (ret.AddActionParam(PARAM_VALUE, val) == -1)
				perror("AddActionParam VALUE");

			return ret;
		}
	}
}

action protocol::parsePCmds(char *s, int len)
{
	if (!strncasecmp(s+1, "rist", 4))
	{
		switch(toupper(s[5]))
		{
			case 'A':
				if (!strncasecmp(s+6, "rt", 2))
				{
					return ACTION_PRI_START;
				}
				break;
			case 'O':
				if (!strncasecmp(s+6, "p", 1))
				{
					return ACTION_PRI_STOP;
				}
				break;
			default:
				return parseerrorActionFactory(s, len);
		}
	} else
		return parseerrorActionFactory(s, len);

	return 0;
}

action protocol::parseBCmds(char *s, int len)
{
	switch(toupper(s[1]))
	{
		case 'A':
			if (!strncasecmp(s+2, "ck", 2))
				return backActionFactory(s, len);
			else
			{
				return parseerrorActionFactory(s, len);
			}
			break;
		case 'I':
			if (toupper(s[2]) == 'G')
			{
				return parseBigArgs(s+4, len-4);
			} else
			{
				return parseerrorActionFactory(s, len);
			}
			break;
		default:
			return parseerrorActionFactory(s, len);
			break;
	}

	return -1;
}

action protocol::parseBigArgs(char *s, int len)
{
	action ret;

	ret = parseGraphArgs(s, len);
	if (ret.GetActionType() == -1)
		ret.SetActionType(ACTION_BIG);
	else
		return parseerrorActionFactory(s,len);

	return ret;
}

action protocol::parseNCmds(char *s, int len)
{
	if (!strncasecmp(s+1, "st", 2))
	{
		if ((toupper(s[3]) == 'A')&&(!strncasecmp(s+4, "rt", 2)))
		{
			return nstartActionFactory(s, len);
		} else if ((toupper(s[3]) == 'O')&&(toupper(s[4])=='P'))
		{
			return nstopActionFactory(s, len);
		} else {
			return parseerrorActionFactory(s, len);
		}
	} else {
		return parseerrorActionFactory(s, len);
	}

}

action protocol::parseHCmds(char *s, int len)
{
	if ((toupper(s[1]) == 'O')&&(!strncasecmp(s+2, "me", 2)))
	{
		return homeActionFactory(s,len);
	} else if ((toupper(s[1]) == 'G')&&(!strncasecmp(s+2, "raph", 4)))
	{
		return parseHGraphArgs(s+7,len-7);
	} else
		return parseerrorActionFactory(s, len);

}

action protocol::parseOCmds(char *s, int len)
{
	if ((toupper(s[1]) == 'U')&&(toupper(s[2]) == 'T'))
	{
		return parseOutArgs(s+3, len-3);
	} else if ((toupper(s[1]) == 'P')&&(toupper(s[2]) == 'T'))
	{
		return parseOptArgs(s+3, len-3);
	} else {
		return parseerrorActionFactory(s,len);
	}
}

action protocol::parseOutArgs(char *s, int len)
{
	action ret(ACTION_OUT);

	ret.AddActionParam(PARAM_TEXT, s+1);

	return ret;
}

EMPTYFUNCTION(parseOptArgs);
EMPTYFUNCTION(parseDefineArgs);


syntax highlighted by Code2HTML, v. 0.9.1