/********** $Header: c:\\cygwin\\cvsroot/Vert03/cmdparse_nonTCL.c,v 1.1.1.1 2003/11/04 23:34:56
mjames Exp $ **********
Author: Kevin Ross (x6143)
Philips Semiconductors Limited.
Millbrook Industrial Estate, Southampton, SO9 7BH. England.
Date: 13th May 1993
Comment: This program parses the input commands for the SFC3 evaluation software
$Log: cmdparse_nonTCL.c,v $
Revision 1.1.1.1 2003/11/04 23:34:56 mjames
Imported into local repositrory
Revision 1.1.1.1 2002/09/09 15:06:18 mjames
no message
Revision 1.1.1.1 2000/10/19 21:58:35 mjames
Mike put it here
* Revision 1.30 2000/10/04 10:37:03 10:37:03 mjames (Mike James)
* Modified to support VHDL components and SIGNALS
*
* Revision 1.30 2000/10/04 10:37:03 10:37:03 mjames (Mike James)
* Part of Release PSAVAT01
*
* Revision 1.29 2000/10/02 11:04:09 11:04:09 mjames (Mike James)
* new_vhdl
*
* Revision 1.28 2000/09/27 14:42:10 14:42:10 mjames (Mike James)
* Part of Release Sep_27_ST_2000
*
* Revision 1.27 2000/09/21 10:19:57 10:19:57 mjames (Mike James)
* removed diagnostics
*
* Revision 1.26 2000/09/21 10:15:39 10:15:39 mjames (Mike James)
* Part of Release Sep21Alpha
*
* Revision 1.25 2000/09/21 09:43:27 09:43:27 mjames (Mike James)
* Corrected a few mistakes to do with argument expansion.
* Also removed <cr> characters from a DOS edit session
*
* Revision 1.24 2000/08/25 09:57:09 09:57:09 mjames (Mike James)
* Part of Release Aug25_alpha
*
* Revision 1.23 2000/08/16 08:57:25 08:57:25 mjames (Mike James)
* Part of Release CD01_Aug2000
*
* Revision 1.22 2000/08/14 14:45:07 14:45:07 mjames (Mike James)
* Part of Release Aug_14_2000
*
* Revision 1.21 2000/08/11 08:30:27 08:30:27 mjames (Mike James)
* Part of Release Aug_11_2000
*
* Revision 1.20 2000/08/11 08:28:39 08:28:39 mjames (Mike James)
* Modified string expansion to occur once only to
* permit the preservation of escaped sequences
*
* Revision 1.19 2000/08/09 10:31:41 10:31:41 mjames (Mike James)
* Part of Release Aug__9_2000
*
* Revision 1.18 2000/08/09 10:28:26 10:28:26 mjames (Mike James)
* Altered variable expansion to work consistently and return
* $(name) if the variable 'name' is not known.
*
* Revision 1.17 2000/05/31 11:42:47 11:42:47 mjames (Mike James)
* Part of Release May_31_2000
*
* Revision 1.16 2000/05/08 17:01:32 17:01:32 mjames (Mike James)
* Part of Release May__8_2000
*
* Revision 1.15 2000/05/08 16:59:25 16:59:25 mjames (Mike James)
* Part of Release May__8_2000
*
* Revision 1.14 2000/05/08 16:57:02 16:57:02 mjames (Mike James)
* Part of Release May__8_2000
*
* Revision 1.13 2000/05/08 16:41:40 16:41:40 mjames (Mike James)
* Altered null pointer references in both PC and HP
* versions
*
* Revision 1.11 2000/05/03 15:25:19 15:25:19 mjames (Mike James)
* Command handler updated to avoid memory trashing by
* allocating exactly enough memory for expanded command
* line strings rather than the unexpanded version
* ,
*
* Revision 1.10 2000/03/08 16:18:42 16:18:42 mjames (Mike James)
* New version including PC
*
* Revision 1.7 2000/02/18 15:45:22 15:45:22 mjames (Mike James)
* Amended to support PC
*
* Revision 1.6 2000/01/20 15:58:41 15:58:41 mjames (Mike James)
* Part of Release R22
*
* Revision 1.5 99/12/22 11:15:21 11:15:21 mjames (Mike James)
* Part of Release Dec_22_1999
*
* Revision 1.4 99/11/23 13:51:54 13:51:54 mjames (Mike James)
* Addded syntax to support special generics for Certify support
*
* Revision 1.3 99/06/25 14:34:39 14:34:39 mjames (Mike James)
* Added in reference to expression.h, but no changes made
* to the function of acfread yet.
*
* Revision 1.2 99/06/18 09:22:59 09:22:59 mjames (Mike James)
*
* Revision 1.1 1999/06/14 10:47:18 mjames
* Initial revision
*
* Revision 1.1 1999/06/14 10:47:18 mjames
* Initial revision
*
* Revision 1.20 98/10/01 15:30:35 15:30:35 mjames (Mike James)
* Printout of help had 100 dots as this was longest string expected after macro
* expansion.. Now has a separate limit of around 20 (CMDWIDTH)
*
* Revision 1.19 98/08/12 14:19:33 14:19:33 mjames (Mike James)
* Added correct header file list
*
* Revision 1.18 98/07/14 13:23:05 13:23:05 mjames (Mike James)
* added $1 or $(env_var) expansion on command line
*
* Revision 1.17 98/03/16 11:36:57 11:36:57 mjames (Mike James)
* Added LOG_ERROR to logging system . All7ws output of serious errors to stderr
*
* Revision 1.16 98/02/71 11:25:42 11:25:42 mjames (Mike James)
* Checked in for version 6.2a
*
* Revision 1.15 97/04/23 08:42:46 08:42:46 mjames (Mike James)
* CHecked in for release rel23041997
*
* Revision 1.14 96/07/19 14:37:50 14:37:50 mjames (Mike James)
* Added list of objects to SET DEL EXT commands
*
* Revision 1.13 1996/07/12 15:52:12 mjames
* Sorted out things like Alias and Jumpers
* Work Correctly
* Print COrrectly
*
* Revision 1.12 96/06/17 10:51:56 10:51:56 mjames (Mike James)
* Added fix_pins command
*
* Revision 1.11 96/04/15 14:19:21 14:19:21 mjames (Mike James)
* Checkin before datatype printing
* modifications
*
* Revision 1.10 96/03/29 14:46:01 14:46:01 mjames (Mike James)
* Added VHDL netlist writing to the capabilities of ACFREAD
*
* Revision 1.9 96/03/26 07:20:29 07:20:29 mjames (Mike James)
* Added LIST XREF socket-name command
*
* Revision 1.8 96/03/18 13:50:25 13:50:25 mjames (Mike James)
* Real Revision 2.1
*
* Revision 1.5 96/03/13 15:35:20 15:35:20 mjames (Mike James)
* *** empty log message ***
*
* Revision 1.4 96/02/13 09:13:09 09:13:09 mjames (Mike James)
* Updated to be version 2.0 with net joining
*
* Revision 1.3 96/02/08 15:28:07 15:28:07 mjames (Mike James)
* First release
*
* Revision 1.2 96/02/07 16:01:21 16:01:21 mjames (Mike James)
* Added correct RCS header
*
* Revision 1.1 96/02/07 15:49:19 15:49:19 mjames (Mike James)
* Initial revision
*
*********************************************************************************************************/
#include "cmdexec.h"
#include "cmdlog.h"
#include "cmdparse.h"
#include "cmdutil.h"
#include "expression.h"
#include "generic.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* used for dynamic string allocation/deallocation code
* used here for command keywords May 2 2000 */
#include "lx_support.h"
/*********************************************************************************************************/
static char IDstr[] = "@(#)$Header: c:\\cygwin\\cvsroot/Vert03/cmdparse_nonTCL.c,v 1.1.1.1 "
"2003/11/04 23:34:56 mjames Exp $";
/******************************************* Execute a command
* ******************************************/
/* modification to use a datastructure used elsewhere */
static struct str *cmd_first, *cmd_last;
#define WORDWIDTH 100 /* max command word everexpected */
#define CMDWIDTH 20 /*Field width for printing CMD word */
int execute (struct CommandStruct *CmdPtr, struct command Dispatch[], int NumCmds)
{
int i, j, FinalResult = UNKNOWNCMD;
if (CmdPtr->CurrentCommand == CmdPtr->NumberCommands)
return (NARGS);
if (CmdPtr->Words[CmdPtr->CurrentCommand][0] == '?')
{
printf ("Commands available;\nHelp on:");
for (i = 0; i < CmdPtr->CurrentCommand; i++)
printf ("%s ", CmdPtr
->Words
[i
]);
for (i = 0; i < NumCmds; i++)
{
int p;
for (j = 0; j < Dispatch[i].NumChar;
j++) /* upper case significant chars */
for (; Dispatch[i].name[j];
j++) /* and all of the rest, we just print */
j++;
if (Dispatch[i].extras)
{
p = j;
for (; Dispatch[i].extras[j - p];
j++) /* and all of the extras field we just print */
putchar (Dispatch
[i
].
extras[j
- p
]);
};
for (; j < CMDWIDTH; j++) /* create spacing , relies on auto - wrap */
puts (Dispatch
[i
].
helpstr);
}
FinalResult = OKCMD; /* ? is a valid command at this level */
}
else
{
for (i = 0; i < NumCmds; i++)
CmdPtr->Words[CmdPtr->CurrentCommand],
Dispatch[i].name,
Dispatch[i].NumChar) == 0)
{
CmdPtr->CurrentCommand++; /* Dispatch the next level command */
FinalResult = (*Dispatch[i].function) (CmdPtr);
break;
}
}
return (FinalResult);
}
/******************************************** Strip off words
* ********************************************/
/* this needs repairing . This code is unsafe.
* Needs to keep a record of how many chars copied !!
* and stop when in danger of a buffer overflow.
*
* Did not work properly if expanded strings are longer than the
* original strings. This now fixed.
*
* Code modified to use some string allocation functions in lx_support.c
*/
char wordbuff[1024];
int FindWords (char *linebuff, struct CommandStruct *Cmd, int nArgs, char *Args[])
{
char *WordPtr, *Optr;
char *BasePtr;
int i, Len, term = ' ', escape;
/* deallocate any previous structures */
free_strings (&cmd_first, &cmd_last);
if (Cmd->Text)
{
strcpy (linebuff
, Cmd
->Text
); /* make a safe copy */
}
else
Len = 0;
Cmd->NumberCommands = 0;
if (Len > 0)
{
linebuff[Len - 1] = 0; /* kill <CR> */
/* for (i=0; i<Len; i++) */
/* Cmd->Text[i] = tolower(Cmd->Text[i]); */ /* convert text
to lower case
*/
WordPtr = linebuff; /* set LineStart to beginning of string */
escape = 0;
/* Now split off the words */
BasePtr = NULL;
while (*WordPtr && Cmd->NumberCommands < WORDSINLINE)
{
while (*WordPtr && *WordPtr == ' ') /* skip leading spaces */
WordPtr++;
if (!*WordPtr)
break;
if (*WordPtr == '"' || *WordPtr == '\'')
{ /* sigle or double quotes ? */
term = *WordPtr++;
}
else
term = ' ';
BasePtr = WordPtr;
escape = 0;
while (*WordPtr && (escape || *WordPtr != term))
{ /* scan for terminator ' ' or '"' */
escape =
(*WordPtr == '\\' &&
*(WordPtr + 1) == term); /* only escape terminators */
WordPtr++;
}
if (!*WordPtr) /* if end of line */
break;
*WordPtr++ = '\0';
/* make_string now performs an expand_string call */
Cmd->Words[Cmd->NumberCommands++] =
make_string (BasePtr, &cmd_first, &cmd_last);
BasePtr = NULL;
}
if (BasePtr)
{
Cmd->Words[Cmd->NumberCommands++] =
make_string (BasePtr, &cmd_first, &cmd_last);
}
}
/*
printf("Seen %d words on line\n",Cmd->NumberCommands);
for(i=0;i<Cmd->NumberCommands;i++)
printf("word[%d]='%s'\n",i,Cmd->Words[i]);
*/
return (OKCMD);
}
/********************************* executes the command from stdin or a do file
* **************************/
int ExecuteCommand (FILE *CmdFile, struct CommandStruct Cmd, int nArgs, char *Args[])
{
char Buff[PATHLENGTH];
char linebuff[LINELEN];
int i, Status;
char *p;
return (QUITCMD);
p
= fgets (Buff
, PATHLENGTH
- 1, CmdFile
); /* read the command from the input stream */
Cmd.Text = Buff; /* place the command in the command structure */
Cmd.NumberCommands = 0;
Cmd.CurrentCommand = 0;
if (p == NULL || *Buff == '\0')
return (OKCMD);
/* convert control codes to spaces */
i = 0;
while (Buff[i])
{
if (Buff[i] < ' ')
Buff[i] = ' ';
i++;
}
FindWords (linebuff, &Cmd, nArgs, Args); /* split off the words and save in structure
*/
if (CmdFile != stdin)
{
for (i = 0; i < Cmd.NumberCommands; i++)
printf ("%s ", Cmd.
Words[i
]); /* log the command word by word if its
not given interactively */
}
if (Cmd.NumberCommands == 0 || Cmd.Words[0][0] == '-' || Cmd.Words[0][0] == '#')
return (OKCMD); /* comment or a null string */
Status = TopMenu (&Cmd); /* pass the command through the top-level menu */
switch (Status)
{
case UNKNOWNCMD:
Log (LOG_ERROR, "# Unknown command:");
break;
case NARGS:
Log (LOG_ERROR, "# Too few parameters:");
break;
case FAILED:
Log (LOG_ERROR, "# Could not execute command:");
break;
};
if (Status != OKCMD)
{
for (i = 0; i < Cmd.NumberCommands; i++) /* display offending command */
Log (LOG_ERROR, " %s", Cmd.Words[i]);
Log (LOG_ERROR, "\n");
};
return (Status);
}
/********************************* Execute a command string
* ******************************************/
int ExecuteString (char *commandstring, int nArgs, char *Args[])
{
struct CommandStruct Cmd;
char linebuff[LINELEN];
int Status = 0;
Cmd.Text = commandstring; /* place the command in the command structure */
Cmd.NumberCommands = 0;
Cmd.CurrentCommand = 0;
FindWords (linebuff, &Cmd, nArgs, Args); /* split off the words and save in structure
*/
Status = TopMenu (&Cmd); /* pass the command through the top-level menu */
return (Status);
}
/********************************* A parser for the '*.do files
* ******************************************/
static int Depth = 0;
#define MAXDEPTH 10 /* maximum depth of 'do' files */
#define MAXARGV 10
int ParseFile (FILE *CmdFile, int nArgs, char *Args[])
{
struct CommandStruct Cmd;
int Status = 0;
char *argp[MAXARGV];
char **argp_prev;
int nArgs_prev;
int i;
/* diagnosis */
if (nArgs > MAXARGV)
nArgs = MAXARGV;
if (nArgs < 0)
nArgs = 0;
for (i = 0; i < MAXARGV; i++)
{
if (i < nArgs)
argp[i] = strdup (Args[i]);
else
argp[i] = NULL;
/* printf("Arg [%2d]='%s'\n",i,argp[i]); */
}
nArgs_prev = yy_nArgs;
argp_prev = yy_Args;
yy_nArgs = nArgs;
yy_Args = argp;
Depth++; /* keep a count of recursion depth */
printf ("Command file nesting level = %d\n", Depth
);
if (Depth > MAXDEPTH)
{
Log (LOG_ERROR, "Recursive call of files is greater than %d\n", MAXDEPTH);
Depth--;
return (FAILED);
}
while (Status != QUITCMD) /* execute the command from do file input stream */
Status = ExecuteCommand (CmdFile, Cmd, nArgs, argp);
Depth--;
yy_nArgs = nArgs_prev;
yy_Args = argp_prev;
for (i = 0; i < MAXARGV; i++)
if (argp[i])
{
argp[i] = NULL;
}
return (Status);
}
/*********************************************************************************************************/