diff --git a/harbour/source/hbpp/harb.h b/harbour/source/hbpp/harb.h index 8988147042..35df092667 100644 --- a/harbour/source/hbpp/harb.h +++ b/harbour/source/hbpp/harb.h @@ -16,6 +16,12 @@ typedef long LONG; typedef unsigned long ULONG; #define PATH_DELIMITER "/\\" #define IS_PATH_SEP( c ) (strchr(PATH_DELIMITER, (c))!=NULL) +#define OPT_DELIMITER "/-" +#define IS_OPT_SEP( c ) (strchr(OPT_DELIMITER, (c))!=NULL) +typedef struct _PATHNAMES { /* the list of pathnames to search with #include */ + char *szPath; + struct _PATHNAMES *pNext; +} PATHNAMES; #ifndef _POSIX_PATH_MAX #define _POSIX_PATH_MAX 255 #endif diff --git a/harbour/source/hbpp/hbpp.c b/harbour/source/hbpp/hbpp.c index 87f320abb5..3bb79113a7 100644 --- a/harbour/source/hbpp/hbpp.c +++ b/harbour/source/hbpp/hbpp.c @@ -58,6 +58,7 @@ int strotrim ( char* ); char* strodup ( char * ); int NextWord ( char**, char*, int); int NextName ( char**, char*, char**); +int Include( char *, PATHNAMES *, FILE** ); #define isname(c) (isalnum(c) || c=='_' || (c) > 0x7e) #define SKIPTABSPACES(sptr) while ( *sptr == ' ' || *sptr == '\t' ) (sptr)++ @@ -90,6 +91,7 @@ int nline=0; int Repeate; char groupchar; +extern PATHNAMES *_pIncludePath; extern DEFINES aDefines[] ; extern int koldef; DEFINES *aDefnew ; @@ -134,9 +136,9 @@ int ParseDirective( char* sLine ) if ( *(sLine+i) != '\"' ) return 1000; *(sLine+i) = '\0'; - if ((handl_i = fopen(sLine, "r")) == NULL) +// if ((handl_i = fopen(sLine, "r")) == NULL) + if ( !OpenInclude( sLine, _pIncludePath, &handl_i ) ) { printf("\nCan't open %s",sLine); return 1001; } - lInclude++; Hp_Parse(handl_i, 0 ); lInclude--; @@ -1400,3 +1402,33 @@ int NextName ( char** sSource, char* sDest, char **sOut ) *sDest = '\0'; return i; } + +int OpenInclude( char * szFileName, PATHNAMES *pSearch, FILE** fptr ) +{ + if( ! ( *fptr = fopen( szFileName, "r" ) ) ) + { + if( pSearch ) + { + FILENAME *pFileName =SplitFilename( szFileName ); + char szFName[ _POSIX_PATH_MAX ]; /* filename to parse */ + + pFileName->name =szFileName; + pFileName->extension =NULL; + while( pSearch && !*fptr ) + { + pFileName->path =pSearch->szPath; + MakeFilename( szFName, pFileName ); + if( ! ( *fptr = fopen( szFName, "r" ) ) ) + { + pSearch = pSearch->pNext; + if( ! pSearch ) + return 0; + } + } + _xfree( pFileName ); + } + else + return 0; + } + return 1; +} \ No newline at end of file diff --git a/harbour/source/hbpp/hbppmain.c b/harbour/source/hbpp/hbppmain.c index cd6619c5c8..0f4a4f7f14 100644 --- a/harbour/source/hbpp/hbppmain.c +++ b/harbour/source/hbpp/hbppmain.c @@ -33,33 +33,74 @@ extern DEFINES *aDefnew ; extern COMMANDS *aCommnew ; extern TRANSLATES *aTranslates ; +PATHNAMES *_pIncludePath = NULL; +void AddSearchPath( char *, PATHNAMES * * ); /* add pathname to a search list */ + int main (int argc,char* argv[]) { FILE *handl_i,*handl_o; char szFileName[ _POSIX_PATH_MAX ]; FILENAME *pFileName =NULL; +int iArg = 1; - if(argc<2) { printf("File name absent"); return 1; } - pFileName =SplitFilename( argv[1] ); - if( !pFileName->extension ) - pFileName->extension =".prg"; - MakeFilename( szFileName, pFileName ); + while( iArg < argc ) + { + if( IS_OPT_SEP(argv[ iArg ][ 0 ])) + { + switch( argv[ iArg ][ 1 ] ) + { + case 'd': + case 'D': /* defines a Lex #define from the command line */ + { + unsigned int i = 0; + char * szDefText = strdup( argv[ iArg ] + 2 ); + while( i < strolen( szDefText ) && szDefText[ i ] != '=' ) + i++; + if( szDefText[ i ] != '=' ) + AddDefine( szDefText, 0 ); + else + { + szDefText[ i ] = 0; + AddDefine( szDefText, szDefText + i + 1 ); + } + free( szDefText ); + } + break; + case 'i': + case 'I': + AddSearchPath( argv[ iArg ]+2, &_pIncludePath ); + break; + default: + printf( "Invalid command line option: %s\n", &argv[ iArg ][ 1 ] ); + break; + } + } + else pFileName =SplitFilename( argv[ iArg ] ); + iArg++; + } + if( pFileName ) + { + if( !pFileName->extension ) pFileName->extension =".prg"; + MakeFilename( szFileName, pFileName ); - if ((handl_i = fopen(szFileName, "r")) == NULL) - { printf("Can't open %s",szFileName); return 1; } + if ((handl_i = fopen(szFileName, "r")) == NULL) + { printf("Can't open %s",szFileName); return 1; } + } + else { printf("File name absent"); return 1; } - pFileName->extension =".ppo"; - MakeFilename( szFileName, pFileName ); - if ((handl_o = fopen(szFileName, "wt" )) == NULL) - { printf("Can't open %s",szFileName); return 1; } + pFileName->extension =".ppo"; + MakeFilename( szFileName, pFileName ); - aCondCompile = (int*) _xgrab( sizeof(int) * 5 ); - aDefnew = ( DEFINES * ) _xgrab( sizeof(DEFINES) * 50 ); - aCommnew = ( COMMANDS * ) _xgrab( sizeof(COMMANDS) * INITIAL_ACOM_SIZE ); - aTranslates = ( TRANSLATES * ) _xgrab( sizeof(TRANSLATES) * 50 ); + if ((handl_o = fopen(szFileName, "wt" )) == NULL) + { printf("Can't open %s",szFileName); return 1; } - Hp_Parse(handl_i,handl_o ); - fclose(handl_i); fclose(handl_o); + aCondCompile = (int*) _xgrab( sizeof(int) * 5 ); + aDefnew = ( DEFINES * ) _xgrab( sizeof(DEFINES) * 50 ); + aCommnew = ( COMMANDS * ) _xgrab( sizeof(COMMANDS) * INITIAL_ACOM_SIZE ); + aTranslates = ( TRANSLATES * ) _xgrab( sizeof(TRANSLATES) * 50 ); + + Hp_Parse(handl_i,handl_o ); + fclose(handl_i); fclose(handl_o); /* for (int i=0;ipNext ) + pPath = pPath->pNext; + pPath->pNext = ( PATHNAMES * ) OurMalloc( sizeof( PATHNAMES ) ); + pPath = pPath->pNext; + } + else + { + *pSearchList =pPath =(PATHNAMES *)OurMalloc( sizeof(PATHNAMES) ); + } + pPath->pNext = NULL; + pPath->szPath = szPath; +} + void * OurMalloc( LONG lSize ) { void * pMem = malloc( lSize );