/**     parse.c
 **
 ** Author: Ted Schmitz
 ** Date of original creation december 10, 2008
 **
 **     This file contains all the routines to handle ASCII command line
 ** parsing and other related tasks.
 **
 **/
#include "diag_mon.h"

#if !defined(__U_BOOT__)
#include <string.h>  /* standard library headers */
#else
#include "common.h"  /* common library headers for Uboot environment */
#endif

/* external references */
extern char gen_wait_for_one_key(void);

/***************************************************************************
 **		Separate Parameters In ASCII String
 **
 **	This routine separates all the parameters in any ASCII string into
 ** separate strings.
 **
 ** INPUT - "input_line" = ASCII string containing a command and/or multiple 
 **	    parameters, terminated with NULL.
 **	  - "parm_ptrs[]" = User supplied storage for list of parsed ASCII 
 **	    string pointers.
 **
 ** OUTPUT - A list of pointers will be created containing the beginning
 **	     address of every ASCII string in the original ASCII string.
 **	     Also every ASCII string separated by a space will be terminated
 **	     with a NULL.
 **
 ** RETURNS - Integer containing the number of parameters on the command line
 **	      including the command.
 **/
int separate_parms(char * input_line, char * parm_ptrs[MAXARGS])
{
   int parm_idx;			/* parameter index */
   int inl_idx;				/* input line index */
   int i;

   /** Now parse the input line into separate parameters by finding all the
    ** places in the input line where a command or parameter starts and 
    ** recording those addresses in the character pointer array.
    **/

   inl_idx = strlen(input_line);	/* get length of input line */

   /* if a newline char is at the end of the string, write a null over it */
   if (input_line[inl_idx-1] == '\n')
     input_line[inl_idx-1] = 00;

   /* first skip over any leading spaces */
   for (i = 0; i < inl_idx; i++)
     if (input_line[i] != ' ')
       break;
   
   /**     Now save the beginning address of each separate ASCII parameter
    ** and terminate each ASCII parameter with a null.  All parameters are
    ** assumed to begin and end with a space.
    **/
   parm_idx = 0;			/* Init parm pointer index */
   for ( ; ((i < inl_idx) && (parm_idx <= MAXARGS)); i++)
     {
     /** If next char in input line is not a space, asume it is the
      ** beginning of the next param.
      **/
     if (input_line[i] != ' ')		
       {
       parm_ptrs[parm_idx] = &input_line[i];/* save beg adrs of this param */

       /* find the end of the command */
       while ((i < inl_idx) && (input_line[i] != ' '))
         i++;

       input_line[i] = 00;		/* terminate this param with a null */
       parm_idx++;			/* inc to next param pointer */
       }
     else
       input_line[i] = 00;		/* conv all spaces to nulls */

     } /* end for */

  /* return number of parameters found in input line */
  return parm_idx;			
}

/*****************************************************************************
 **                Wait For Carriage Return Function
 **
 **  This function waits for a single character to be type on the keyboard.
 ** If the charater pressed is enter or return, this function returns a
 ** "TRUE" status. A "FALSE" status is returned if any other key is pressed.
 **
 ** INPUT - Nothing
 **
 ** OUTPUT - Nothing
 **
 ** RETURN - A "TRUE" status will be returned to the calling code if the "enter"
 **          or "return" key is pressed, "FALSE" returnede otherwise.
 **
 ****************************************************************************/
int wait_for_enter_key(void)
{
  char key;                             /* key operator pressed */

  key = gen_wait_for_one_key();         /* wait for any character */

  /* if key pressed is "enter" or "carriage return" return "TRUE" */
  if ((key != '\r') && (key != '\n'))
    return FALSE;
  else
    return TRUE;

}
