/**
Date: 31st March 1993
General command line to string or numeric variable reading
Modified for command line parsing for EPLD multi-fitter program
Mike James 7 Feb 1996
* $Log: cmdutil.c,v $
* Revision 1.1.1.1 2003/11/04 23:34:56 mjames
* Imported into local repositrory
*
* Revision 1.13 2002/09/09 10:26:56 mjames
* Removed set generic range and replaced it with a set generic value command
* that takes both integers and ranges.
*
* Revision 1.12 2002/08/14 11:56:13 mjames
* Removed NT specific file opening , as recent Cygwin DLL does not
* like the code I was using to protect NT from screwing up on opening
* read-only files for write (it crashes as the first byte is written)
*
* Revision 1.11 2002/08/06 12:52:59 mjames
* Merge in from latest version
*
*
Revision 1.12 2002/04/10 14:29:10 mjames
Moved setting debug level to cmdutil.c
Amended print external command to list all net names on socket pins
whether routed or not.
Revision 1.11 2002/03/21 17:14:15 mjames
Added search path to vertical file opening for read
Revision 1.10 2002/01/16 11:22:42 mjames
database.h header file is read in first as it undefined DLL stuff irrelevant
to HPUX
Revision 1.9 2001/12/13 22:14:25 mjames
Corrected nested command handlers to allow variable passing without corruption.
Revision 1.8 2001/11/19 10:41:52 mjames
Merged back DTC release
Revision 1.7.2.1 2001/11/16 15:08:13 mjames
Fixed incorrect printf specifier.
Added () barackets to conditional expressions
Revision 1.7 2001/10/31 22:20:01 mjames
Tidying up problematical comments caused by CVS
'intelligent' comment guessing
Revision 1.6 2001/08/31 08:19:18 mjames
Added stuff to prevent errors with "sys/stat.h"
Revision 1.5 2001/06/20 13:43:33 mjames
Corrected logical expression required to open a file : need user rights to open this file
either 'r' and read rights or 'w' and write rights in trap_fopen call.
Revision 1.4 2001/06/19 05:19:52 mjames
Created a trap_fopen to overcome trying to write to read only files.
If this attempted in NT the file can be opened but not written to.
Revision 1.3 2001/06/06 12:10:24 mjames
Move from HPUX
Revision 1.2 2001/02/06 22:41:15 mjames
Added correct argument passing for 'read file comp_suffix arg0 arg1 arg2 ...
Revision 1.1.1.1 2000/10/19 21:58:35 mjames
Mike put it here
* Revision 1.34 2000/10/04 10:37:03 10:37:03 mjames (Mike James)
* COnversion to Vertical2, supports signals and components
*
* Revision 1.34 2000/10/04 10:37:03 10:37:03 mjames (Mike James)
* Part of Release PSAVAT01
*
* Revision 1.33 2000/10/02 11:04:10 11:04:10 mjames (Mike James)
* new_vhdl
*
* Revision 1.32 2000/09/27 14:42:10 14:42:10 mjames (Mike James)
* Part of Release Sep_27_ST_2000
*
* Revision 1.31 2000/09/21 10:15:40 10:15:40 mjames (Mike James)
* Part of Release Sep21Alpha
*
* Revision 1.30 2000/08/25 09:57:09 09:57:09 mjames (Mike James)
* Part of Release Aug25_alpha
*
* Revision 1.29 2000/08/16 08:57:26 08:57:26 mjames (Mike James)
* Part of Release CD01_Aug2000
*
* Revision 1.28 2000/08/14 14:45:07 14:45:07 mjames (Mike James)
* Part of Release Aug_14_2000
*
* Revision 1.27 2000/08/11 08:30:28 08:30:28 mjames (Mike James)
* Part of Release Aug_11_2000
*
* Revision 1.26 2000/08/09 10:31:41 10:31:41 mjames (Mike James)
* Part of Release Aug__9_2000
*
* Revision 1.25 2000/05/31 11:42:48 11:42:48 mjames (Mike James)
* Part of Release May_31_2000
*
* Revision 1.24 2000/05/08 17:01:33 17:01:33 mjames (Mike James)
* Part of Release May__8_2000
*
* Revision 1.23 2000/05/08 16:59:26 16:59:26 mjames (Mike James)
* Part of Release May__8_2000
*
* Revision 1.22 2000/05/08 16:57:03 16:57:03 mjames (Mike James)
* Part of Release May__8_2000
*
* Revision 1.21 2000/03/08 16:18:44 16:18:44 mjames (Mike James)
* New version including PC
*
* Revision 1.18 2000/01/20 15:58:41 15:58:41 mjames (Mike James)
* Part of Release R22
*
* Revision 1.17 99/12/22 11:15:22 11:15:22 mjames (Mike James)
* Part of Release Dec_22_1999
*
* Revision 1.16 99/06/25 14:34:40 14:34:40 mjames (Mike James)
* Added in reference to expression.h, but no changes made
* to the function of acfread yet.
*
* Revision 1.15 99/06/18 09:23:13 09:23:13 mjames (Mike James)
*
* Revision 1.14 1999/06/14 10:54:11 mjames
* *** empty log message ***
*
* Revision 1.14 1999/06/14 10:54:11 mjames
* *** empty log message ***
*
* Revision 1.13 98/03/16 11:37:05 11:37:05 mjames (Mike James)
* Added LOG_ERROR to logging system . Allows output of serious errors to stderr
*
* Revision 1.12 98/02/11 11:25:47 11:25:47 mjames (Mike James)
* Checked in for version 6.2a
*
* Revision 1.11 97/04/23 08:42:47 08:42:47 mjames (Mike James)
* CHecked in for release rel23041997
*
* Revision 1.10 96/07/19 14:38:13 14:38:13 mjames (Mike James)
* Added list of objects to SET DEL EXT commands
*
* Revision 1.9 1996/07/12 15:52:12 mjames
* Sorted out things like Alias and Jumpers
* Work Correctly
* Print COrrectly
*
* Revision 1.8 96/05/21 14:14:52 14:14:52 mjames (Mike James)
* Altered return codes form utilities to be a more accurate
* representation of reality.
*
* Revision 1.7 96/03/29 14:46:03 14:46:03 mjames (Mike James)
* Added VHDL netlist writing to the capabilities of ACFREAD
*
* Revision 1.6 96/03/18 13:50:27 13:50:27 mjames (Mike James)
* Real Revision 2.1
*
* Revision 1.4 96/02/13 09:13:13 09:13:13 mjames (Mike James)
* Updated to be version 2.0 with net joining
*
* Revision 1.3 96/02/08 15:28:12 15:28:12 mjames (Mike James)
* First release
*
* Revision 1.2 96/02/07 16:01:23 16:01:23 mjames (Mike James)
* Added correct RCS header
*
* Revision 1.1 96/02/07 15:49:47 15:49:47 mjames (Mike James)
* Initial revision
*
**********************************************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/stat.h>
#include "vertcl_main.h"
#include "cmdparse.h"
#include "cmdutil.h"
#include "cmdlog.h"
#include "expression.h"
#include "generic.h"
#include "database.h"
#include "lx_support.h"
#ident "@(#)$Header: c:\\cygwin\\cvsroot/Vert03/cmdlib/cmdutil.c,v 1.1.1.1 2003/11/04 23:34:56 mjames Exp $"
/***************************************************************/
/* this routine is used to trap opening read-only files for write */
/* writing files is always into the current directory. Reading uses a search path */
/* #if defined IS_HPUX */
#if 1
#define safe_fopen fopen
#else
#define safe_fopen nt_fopen
/* and then define the NT protection function */
FILE * nt_fopen(char * name, char * mode)
{
struct stat buf;
if(ISNULLSTR(name))
{
return(NULL);
}
stat( name,&buf );
if(level & 16)
{
printf("stat %08o '%s' '%s'\n",buf.
st_mode,name
,mode
);
}
/* no file there attempt open anyway */
if (buf.st_mode == 0)
{
return (fopen(name
,mode
));
}
if (S_ISREG(buf.st_mode ))
{
if(((mode[0]=='w') && ((buf.st_mode & S_IRWXU)==S_IWUSR)) ||
((mode[0]=='r') && ((buf.st_mode & S_IRWXU)==S_IRUSR)))
{
return (fopen(name
,mode
));
}
}
return NULL;
};
#endif
/* we only search for 'r' mode files down the search path */
/* or if name does not start with '.' or '/' and does not contain ':' on a PC */
FILE * trap_fopen(char * name, char * mode)
{
FILE * f;
#if defined HP_UX
if (strcmp("r",mode
)!=0 || name
[0]=='/' || name
[0] == '.' )
#else
if (strcmp("r",mode
)!=0 || name
[0]=='/' || name
[0] == '.' || strchr(name
,':') )
#endif
{
if(level & 16)
printf("Trying to open '%s' (no path) mode '%s'\n",name
,mode
);
f= (safe_fopen(name,mode));
}
else
{
char * sp;
char pathbuff [MAXIDLEN];
char namebuff [MAXIDLEN];
expand_string( "$(VERTICAL_PATH)" ,pathbuff, 0, NULL) ;
f = NULL;
for(sp
=pathbuff
;(f
== NULL
) && (sp
=strtok(sp
,";")) ;sp
=NULL
)
{
sprintf(namebuff
,"%s/%s",sp
,name
); /* make up pathname */
if(level & 16)
printf("Trying to open '%s' mode '%s'\n",namebuff
,mode
);
f = safe_fopen(namebuff,mode);
}
}
if(level & 16)
{
printf("File Pointer returned = %p\n",f
);
}
return f;
};
/*********************************************************************************************************/
int ConvertToDecimal (char *Input, long *Output)
{
int Base = 10, Stat = OKCMD;
char *CurrentChar, *End;
CurrentChar = Input;
while(*CurrentChar && *CurrentChar ==' ')
{
CurrentChar++;
}
if (*CurrentChar == '0')
{
CurrentChar++; /* Read first character to calculate the base */
switch(*CurrentChar)
{
case '\0' : *Output=0; return (OKCMD); /* Trap for single digit zero */
case 'x' : Base=16; CurrentChar++; break;
case 'b' : Base=2; CurrentChar++; break;
default : Base=8; break; /* Default base is octal */
}
}
else
{
{
Base=10;
}
else
{
Stat =FAILED;
}
}
if(Stat==OKCMD)
{
*Output
= strtol(CurrentChar
, &End
, Base
);
}
if (*End != 0 )
{
Stat = FAILED;
}
return(Stat);
}
void set_debug_level(int lev)
{
level = lev;
}