/******************************************************************************

   menu.c - Routines to build and parse a menu
            by Micromint Support <support@micromint.com>    May-2011

  Copyright (c) 2008-2011 Micromint USA LLC  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 Micromint 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 <string.h>
#include "menu.h"

/******************************************************************************

 Defines the maximum number of arguments that can be parsed.

*******************************************************************************/
#ifndef MAX_ARGS_MENU
#define MAX_ARGS_MENU        8
#endif

/*******************************************************************************
 
 Process the menu entry

*******************************************************************************/
int MenuProcessor(char *pcMenuEntry)
{
  static char *argument[MAX_ARGS_MENU + 1];
  char *pcCharacter;
  int argcounter;
  int bFindArgument = 1;
  tMenuEntry *pMenuEntrant;

  /* Initialize the argument counter, and point to the beginning of the menu */
  argcounter = 0;
  pcCharacter = pcMenuEntry;

  /* Advance through the menu until a zero character is found. */
  while(*pcCharacter)
  {
    /* Replace the space with a zero and set a flag to search for the next
       argument                                                             */
    if(*pcCharacter == ' ')
    {
      *pcCharacter = 0;
      bFindArgument = 1;
    }
    /* Must be a character that is part of the argument. */
    else
    { 
      /* bFindArgument is set so look for the next argument */
      if(bFindArgument)
      {
        /* Maximum number of arguments has not been reached. Save a pointer to 
           the start of this new arggument */
        if(argcounter < MAX_ARGS_MENU)
        {
          argument[argcounter] = pcCharacter;
          argcounter++;
          bFindArgument = 0;
        }
        /* Too many arguments in the menu entry so return error */
        else return(TOO_MANY_ARGS_MENU);
      }
    }
    /* Advance to the next character in the menu entry */
    pcCharacter++;
  }
  /* Process the menu entry if an argument was found */
  if(argcounter)
  {
    /* Look for a matching menu entry at the beginning of the menu table. */
    pMenuEntrant = &g_sMenu[0];
    
    /* Scan through the menu table until a null is found */
    while(pMenuEntrant->pcMenu)
    {
      /* Menu entry matches the first argument so call the function and pass the 
         arguments */
      if(!strcmp(argument[0], pMenuEntrant->pcMenu))
      {
        return(pMenuEntrant->pfnMenuEntrant(argcounter, argument));
      }
      /* Was not a match check next entry. */
      pMenuEntrant++;
    }
  }
  /* Return an error due to no matching entry */
  return(BAD_MENU_ENTRY);
}
