PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/beta3/harbour/utils/hbpp/hbppcore.c

#
C | 2237 lines | 1835 code | 262 blank | 140 comment | 601 complexity | e8305463d130aa6f1e92891c0293baa7 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * $Id: hbppcore.c 7547 2007-06-13 22:34:17Z $
  3. */
  4. /*
  5. * Harbour Project source code:
  6. * Preprocessor core module
  7. *
  8. * Copyright 1999 Alexander S.Kresin <alex@belacy.belgorod.su>
  9. * www - http://www.harbour-project.org
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this software; see the file COPYING. If not, write to
  23. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  24. * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
  25. *
  26. * As a special exception, the Harbour Project gives permission for
  27. * additional uses of the text contained in its release of Harbour.
  28. *
  29. * The exception is that, if you link the Harbour libraries with other
  30. * files to produce an executable, this does not by itself cause the
  31. * resulting executable to be covered by the GNU General Public License.
  32. * Your use of that executable is in no way restricted on account of
  33. * linking the Harbour library code into it.
  34. *
  35. * This exception does not however invalidate any other reasons why
  36. * the executable file might be covered by the GNU General Public License.
  37. *
  38. * This exception applies only to the code released by the Harbour
  39. * Project under the name Harbour. If you copy code from other
  40. * Harbour Project or Free Software Foundation releases into a copy of
  41. * Harbour, as the General Public License permits, the exception does
  42. * not apply to the code that you add in this way. To avoid misleading
  43. * anyone as to the status of such modified files, you must delete
  44. * this exception notice from them.
  45. *
  46. * If you write modifications of your own for Harbour, it is your choice
  47. * whether to permit this exception to apply to your modifications.
  48. * If you do not wish that, delete this exception notice.
  49. *
  50. */
  51. /*
  52. * The following parts are Copyright of the individual authors.
  53. * www - http://www.harbour-project.org
  54. *
  55. * Copyright 1999-2001 Viktor Szakats <viktor.szakats@syenar.hu>
  56. * __DATE__, __TIME__, __HB_MAIN__ support
  57. *
  58. * Copyright 2000 Ron Pinkas <Ron@Profit-Master.com>
  59. *
  60. * hb_pp_SetRules_() and related code for supportting
  61. * replaceable rules with -w switch
  62. *
  63. * See doc/license.txt for licensing terms.
  64. *
  65. */
  66. /*
  67. * Avoid tracing in preprocessor/compiler.
  68. */
  69. #if ! defined(HB_TRACE_UTILS)
  70. #if defined(HB_TRACE_LEVEL)
  71. #undef HB_TRACE_LEVEL
  72. #endif
  73. #endif
  74. #include <time.h>
  75. #include <errno.h>
  76. #include "hbppdef.h"
  77. #include "hbcomp.h"
  78. #if defined( OS_UNIX_COMPATIBLE )
  79. #include <sys/timeb.h>
  80. #else
  81. #include <sys/timeb.h>
  82. #endif
  83. int hb_pp_ParseDefine_( char * ); /* Process #define directive */
  84. static COMMANDS *AddCommand( char * ); /* Add new #command to an array */
  85. static COMMANDS *AddTranslate( char * ); /* Add new #translate to an array */
  86. static DEFINES *DefSearch( char *, int, BOOL * );
  87. static COMMANDS *ComSearch( char *, COMMANDS * );
  88. static COMMANDS *TraSearch( char *, COMMANDS * );
  89. static int ParseUndef( char * ); /* Process #undef directive */
  90. static int ParseIfdef( char *, int ); /* Process #ifdef directive */
  91. static void ParseCommand( char *, BOOL, BOOL ); /* Process #command or #translate directive */
  92. static void ConvertPatterns( char *, int, char *, int ); /* Converting result pattern in #command and #translate */
  93. static int WorkDefine( char **, char *, DEFINES * ); /* Replace fragment of code with a #defined result text */
  94. static int WorkPseudoF( char **, char *, DEFINES * ); /* Replace pseudofunction with a #defined result text */
  95. static int WorkCommand( char *, char *, COMMANDS * );
  96. static int WorkTranslate( char *, char *, COMMANDS *, int * );
  97. static int CommandStuff( char *, char *, char *, int *, BOOL, BOOL );
  98. static int RemoveSlash( char * );
  99. static int WorkMarkers( char **, char **, char *, int *, BOOL, BOOL );
  100. static int getExpReal( char *, char **, BOOL, int, BOOL, BOOL );
  101. static BOOL isExpres( char *, BOOL );
  102. static BOOL TestOptional( char *, char * );
  103. static BOOL CheckOptional( char *, char *, char *, int *, BOOL, BOOL );
  104. static void SkipOptional( char ** );
  105. static void SearnRep( char *, char *, int, char *, int * );
  106. static int ReplacePattern( char, char *, int, char *, int );
  107. static void pp_rQuotes( char *, char * );
  108. static int md_strAt( char *, int, char *, BOOL, BOOL, BOOL, int );
  109. #define MD_STR_AT_IGNORECASE 0 /* search ignoring case */
  110. #define MD_STR_AT_USESUBCASE 1 /* use case specified in search string (old) */
  111. static char *PrevSquare( char *, char *, int * );
  112. static int IsInStr( char, char * );
  113. static int stroncpy( char *, char *, int );
  114. static int strincpy( char *, char * );
  115. static BOOL truncmp( char **, char **, BOOL );
  116. static BOOL strincmp( char *, char **, BOOL );
  117. static int strotrim( char *, BOOL ); /* Ron Pinkas 2001-02-14 added 2nd parameter */
  118. static int NextWord( char **, char *, BOOL );
  119. static int NextName( char **, char * );
  120. static int NextParm( char **, char * );
  121. static BOOL OpenInclude( char *, HB_PATHNAMES *, PHB_FNAME, BOOL bStandardOnly, char * );
  122. static BOOL IsIdentifier( char *szProspect );
  123. static int IsMacroVar( char *szText, BOOL isCommand );
  124. static void RemoveOptional( char *cpatt );
  125. static int ConvertOptional( char *cpatt, int len, BOOL bLeft );
  126. #define ISNAME( c ) ( isalnum( c ) || ( c ) == '_' || ( c ) > 0x7E )
  127. #define MAX_NAME 255
  128. #define MAX_EXP 2048
  129. #define PATTERN_SIZE 2048
  130. #define STATE_INIT 0
  131. #define STATE_NORMAL 1
  132. #define STATE_COMMENT 2
  133. #define STATE_QUOTE1 3
  134. #define STATE_QUOTE2 4
  135. #define STATE_QUOTE3 5
  136. #define STATE_ID_END 6
  137. #define STATE_ID 7
  138. #define STATE_EXPRES 8
  139. #define STATE_EXPRES_ID 9
  140. #define STATE_BRACKET 10
  141. #define IT_EXPR 1
  142. #define IT_ID 2
  143. #define IT_COMMA 3
  144. #define IT_ID_OR_EXPR 4
  145. #define HB_PP_MAX_INCLUDES FOPEN_MAX - 5 - 1
  146. #define HB_PP_MATCH_MARK '\1'
  147. #define HB_PP_OPT_START '\2'
  148. #define HB_PP_OPT_END '\3'
  149. /* Ron Pinkas added 2000-01-24 */
  150. #define IS_2CHAR_OPERATOR( p ) ( p[0] && p[1] && ( strncmp( p, ":=", 2 ) == 0 || \
  151. strncmp( p, "+=", 2 ) == 0 || \
  152. strncmp( p, "-=", 2 ) == 0 || \
  153. strncmp( p, "*=", 2 ) == 0 || \
  154. strncmp( p, "/=", 2 ) == 0 || \
  155. strncmp( p, "^=", 2 ) == 0 || \
  156. strncmp( p, "==", 2 ) == 0 || \
  157. strncmp( p, "<>", 2 ) == 0 || \
  158. strncmp( p, "<=", 2 ) == 0 || \
  159. strncmp( p, ">=", 2 ) == 0 || \
  160. strncmp( p, "++", 2 ) == 0 || \
  161. strncmp( p, "--", 2 ) == 0 || \
  162. strncmp( p, "->", 2 ) == 0 ) )
  163. /* END, Ron Pinkas added 2000-01-24 */
  164. static int s_kolAddDefs = 0;
  165. static int s_kolAddComs = 0;
  166. static int s_kolAddTras = 0;
  167. static int s_ParseState;
  168. static int s_maxCondCompile;
  169. static int s_aIsRepeate[5];
  170. static int s_Repeate;
  171. static BOOL s_bReplacePat = TRUE;
  172. static int s_numBrackets;
  173. static char s_groupchar;
  174. static char s_prevchar;
  175. /* additional buffers for expressions */
  176. static char *s_expreal = NULL; /* allocation inside WorkMarkers */
  177. static char *s_expcopy = NULL; /* allocation inside SearnExp */
  178. /* global variables */
  179. int *hb_pp_aCondCompile = NULL;
  180. int hb_pp_nCondCompile = 0;
  181. BOOL hb_pp_NestedLiteralString = FALSE;
  182. BOOL hb_pp_LiteralEscSeq = FALSE;
  183. unsigned int hb_pp_MaxTranslateCycles = 1024;
  184. int hb_pp_StreamBlock = 0;
  185. char *hb_pp_STD_CH = NULL;
  186. /* Ron Pinkas added 2000-11-21 */
  187. static BOOL s_bArray = FALSE;
  188. /* Table with parse errors */
  189. const char *hb_pp_szErrors[] = {
  190. "Can\'t open #include file: \'%s\'; %s",
  191. "#else does not match #ifdef",
  192. "#endif does not match #ifdef",
  193. "Bad filename in #include",
  194. "#define without parameters",
  195. "Missing => in #translate/#command \'%s\' [%s]'",
  196. "Error in pattern definition",
  197. "Cycled #define",
  198. "Invalid name follows #: \'%s\'",
  199. "\'%s\'",
  200. "Memory allocation error",
  201. "Memory reallocation error",
  202. "Freeing a NULL memory pointer",
  203. "Value out of range in #pragma directive",
  204. "Can\'t open command definitions file: \'%s\'",
  205. "Invalid command definitions file name: \'%s\'",
  206. "Too many nested #includes, can\'t open: \'%s\'",
  207. "Input buffer overflow",
  208. "Label missing in #define '%s'",
  209. "Comma or right parenthesis missing in #define '%s'",
  210. "Label duplicated in #define '%s(%s)'",
  211. };
  212. /* Table with warnings */
  213. const char *hb_pp_szWarnings[] = {
  214. "1Redefinition or duplicate definition of #define %s",
  215. "1No directives in command definitions file"
  216. };
  217. void hb_pp_SetRules_( HB_INCLUDE_FUNC_PTR pIncludeFunc, BOOL bQuiet )
  218. {
  219. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_SetRules_()" ) );
  220. if( hb_pp_STD_CH )
  221. {
  222. if( *hb_pp_STD_CH > ' ' )
  223. {
  224. hb_comp_pFileName = hb_fsFNameSplit( hb_pp_STD_CH );
  225. if( hb_comp_pFileName->szName )
  226. {
  227. char szFileName[_POSIX_PATH_MAX];
  228. if( !hb_comp_pFileName->szExtension )
  229. hb_comp_pFileName->szExtension = ".ch";
  230. hb_fsFNameMerge( szFileName, hb_comp_pFileName );
  231. if( ( *pIncludeFunc ) ( szFileName, hb_comp_pIncludePath ) )
  232. {
  233. hb_pp_Init( );
  234. hb_pp_ReadRules( );
  235. if( s_kolAddComs || s_kolAddTras || s_kolAddDefs > 3 )
  236. {
  237. if( !bQuiet )
  238. printf( "Loaded: %i Commands, %i Translates, %i Defines from: %s\n", s_kolAddComs, s_kolAddTras, s_kolAddDefs - 3, szFileName );
  239. }
  240. else
  241. {
  242. hb_compGenWarning( NULL, hb_pp_szWarnings, 'I', HB_PP_WARN_NO_DIRECTIVES, NULL /*szFileName */ , NULL );
  243. }
  244. fclose( hb_comp_files.pLast->handle );
  245. hb_xfree( hb_comp_files.pLast->pBuffer );
  246. hb_xfree( hb_comp_files.pLast->szFileName );
  247. hb_xfree( hb_comp_files.pLast );
  248. hb_comp_files.pLast = NULL;
  249. hb_comp_files.iFiles = 0;
  250. hb_xfree( ( void * ) hb_comp_pFileName );
  251. hb_comp_pFileName = NULL;
  252. }
  253. else
  254. {
  255. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_CANNOT_OPEN_RULES, szFileName, NULL );
  256. }
  257. }
  258. else
  259. {
  260. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_BAD_RULES_FILE_NAME, hb_pp_STD_CH, NULL );
  261. }
  262. }
  263. else
  264. {
  265. if( !bQuiet )
  266. printf( "Standard command definitions excluded.\n" );
  267. hb_pp_Init( );
  268. }
  269. hb_xfree( hb_pp_STD_CH );
  270. }
  271. else
  272. {
  273. hb_pp_Table( );
  274. hb_pp_Init( );
  275. }
  276. }
  277. void hb_pp_Free( void )
  278. {
  279. DEFINES *stdef;
  280. COMMANDS *stcmd;
  281. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_Free()" ) );
  282. while( s_kolAddDefs )
  283. {
  284. stdef = hb_pp_topDefine;
  285. if( stdef->pars )
  286. hb_xfree( stdef->pars );
  287. if( stdef->value )
  288. hb_xfree( stdef->value );
  289. if( stdef->name )
  290. hb_xfree( stdef->name );
  291. hb_pp_topDefine = stdef->last;
  292. hb_xfree( stdef );
  293. s_kolAddDefs--;
  294. }
  295. while( s_kolAddComs )
  296. {
  297. stcmd = hb_pp_topCommand;
  298. if( stcmd->mpatt )
  299. hb_xfree( stcmd->mpatt );
  300. if( stcmd->value )
  301. hb_xfree( stcmd->value );
  302. hb_xfree( stcmd->name );
  303. hb_pp_topCommand = stcmd->last;
  304. hb_xfree( stcmd );
  305. s_kolAddComs--;
  306. }
  307. while( s_kolAddTras )
  308. {
  309. stcmd = hb_pp_topTranslate;
  310. if( stcmd->mpatt )
  311. hb_xfree( stcmd->mpatt );
  312. if( stcmd->value )
  313. hb_xfree( stcmd->value );
  314. hb_xfree( stcmd->name );
  315. hb_pp_topTranslate = stcmd->last;
  316. hb_xfree( stcmd );
  317. s_kolAddTras--;
  318. }
  319. if( hb_pp_aCondCompile )
  320. {
  321. hb_xfree( ( void * ) hb_pp_aCondCompile );
  322. hb_pp_aCondCompile = NULL;
  323. }
  324. hb_pp_InternalFree();
  325. if( s_expreal )
  326. {
  327. hb_xfree( ( void *) s_expreal );
  328. s_expreal = NULL;
  329. }
  330. if( s_expcopy )
  331. {
  332. hb_xfree( ( void *) s_expcopy );
  333. s_expcopy = NULL;
  334. }
  335. }
  336. void hb_pp_Init( void )
  337. {
  338. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_Init()" ) );
  339. hb_pp_Free( );
  340. s_ParseState = 0;
  341. s_maxCondCompile = 5;
  342. s_bReplacePat = TRUE;
  343. s_prevchar = 'A';
  344. if( !hb_pp_aCondCompile )
  345. hb_pp_aCondCompile = ( int * ) hb_xgrab( sizeof( int ) * 5 );
  346. hb_pp_nCondCompile = 0;
  347. {
  348. char sOS[64];
  349. char sVer[64];
  350. char *pSrc, *pDst;
  351. char *szPlatform = hb_verPlatform( );
  352. int n;
  353. hb_strncpy( sOS, "__PLATFORM__", sizeof( sOS ) - 1 );
  354. pSrc = szPlatform;
  355. n = strlen( sOS );
  356. pDst = sOS;
  357. while( *pSrc && *pSrc != ' ' && n < ( int ) sizeof( sOS ) - 1 )
  358. {
  359. if( *pSrc == '_' || ( *pSrc >= 'A' && *pSrc <= 'Z' ) || ( *pSrc >= 'a' && *pSrc <= 'z' ) || ( *pSrc >= '0' && *pSrc <= '9' ) )
  360. {
  361. pDst[n++] = *pSrc;
  362. }
  363. pSrc++;
  364. }
  365. pDst[n] = 0;
  366. n = 0;
  367. pDst = sVer;
  368. pDst[n++] = '"';
  369. if( *pSrc == ' ' )
  370. {
  371. while( *( ++pSrc ) && n < ( int ) sizeof( sVer ) - 2 )
  372. pDst[n++] = *pSrc;
  373. }
  374. pDst[n++] = '"';
  375. pDst[n] = 0;
  376. hb_pp_AddDefine_( sOS, sVer );
  377. #ifdef HB_OS_UNIX
  378. hb_strncpy( &sOS[12], "UNIX", sizeof( sOS ) - 13 );
  379. hb_pp_AddDefine_( sOS, sVer );
  380. #endif
  381. hb_xfree( szPlatform );
  382. }
  383. {
  384. char szResult[6];
  385. USHORT usHarbour = ( 256 * HB_VER_MAJOR ) + HB_VER_MINOR;
  386. /*
  387. This updates __HARBOUR__ on every change of HB_VER_MAJOR / HB_VER_MINOR
  388. HIBYTE is the HB_VER_MAJOR value and the LOBYTE is the HB_VER_MINOR value.
  389. The check below is to ensure that __HARBOUR__ gets the
  390. value of 1 by default
  391. */
  392. snprintf( szResult, sizeof( szResult ), "%05d", ( usHarbour ? usHarbour : 1 ) );
  393. hb_pp_AddDefine_( "__HARBOUR__", szResult );
  394. }
  395. {
  396. char szResult[11];
  397. time_t t;
  398. struct tm *oTime;
  399. time( &t );
  400. oTime = localtime( &t );
  401. snprintf( szResult, sizeof( szResult ), "\"%04d%02d%02d\"", oTime->tm_year + 1900, oTime->tm_mon + 1, oTime->tm_mday );
  402. hb_pp_AddDefine_( "__DATE__", szResult );
  403. snprintf( szResult, sizeof( szResult ), "\"%02d:%02d:%02d\"", oTime->tm_hour, oTime->tm_min, oTime->tm_sec );
  404. hb_pp_AddDefine_( "__TIME__", szResult );
  405. }
  406. {
  407. char szResult[11];
  408. snprintf( szResult, sizeof( szResult ), "%d", ( int ) sizeof( void * ) );
  409. #if defined( HB_ARCH_16BIT )
  410. hb_pp_AddDefine_( "__ARCH16BIT__", szResult );
  411. #elif defined( HB_ARCH_32BIT )
  412. hb_pp_AddDefine_( "__ARCH32BIT__", szResult );
  413. #elif defined( HB_ARCH_64BIT )
  414. hb_pp_AddDefine_( "__ARCH64BIT__", szResult );
  415. #endif
  416. #if defined( HB_LITTLE_ENDIAN )
  417. hb_pp_AddDefine_( "__LITTLE_ENDIAN__", szResult );
  418. #elif defined( HB_BIG_ENDIAN )
  419. hb_pp_AddDefine_( "__BIG_ENDIAN__", szResult );
  420. #elif defined( HB_PDP_ENDIAN )
  421. hb_pp_AddDefine_( "__PDP_ENDIAN__", szResult );
  422. #endif
  423. }
  424. #ifdef HARBOUR_START_PROCEDURE
  425. hb_pp_AddDefine_( "__HB_MAIN__", HARBOUR_START_PROCEDURE );
  426. #endif
  427. }
  428. /* Table with parse warnings */
  429. /* NOTE: The first character stores the warning's level that triggers this
  430. * warning. The warning's level is set by -w<n> command line option.
  431. */
  432. int hb_pp_ParseDirective_( char *sLine )
  433. {
  434. char sDirective[MAX_NAME];
  435. char szInclude[_POSIX_PATH_MAX];
  436. int i;
  437. int bIgnore = 1;
  438. char *sParse;
  439. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_ParseDirective_(%s)", sLine ) );
  440. strotrim( sLine, TRUE );
  441. hb_pp_strocpy( sLine, sLine + 1 );
  442. sParse = sLine;
  443. i = NextName( &sLine, sDirective );
  444. hb_strupr( sDirective );
  445. HB_SKIPTABSPACES( sLine );
  446. if( i == 4 && memcmp( sDirective, "ELSE", 4 ) == 0 )
  447. { /* --- #else --- */
  448. if( hb_pp_nCondCompile == 0 )
  449. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_DIRECTIVE_ELSE, NULL, NULL );
  450. else if( hb_pp_nCondCompile == 1 || hb_pp_aCondCompile[hb_pp_nCondCompile - 2] )
  451. hb_pp_aCondCompile[hb_pp_nCondCompile - 1] = 1 - hb_pp_aCondCompile[hb_pp_nCondCompile - 1];
  452. }
  453. else if( i >= 4 && i <= 5 && memcmp( sDirective, "ENDIF", i ) == 0 )
  454. { /* --- #endif --- */
  455. if( hb_pp_nCondCompile == 0 )
  456. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_DIRECTIVE_ENDIF, NULL, NULL );
  457. else
  458. hb_pp_nCondCompile--;
  459. }
  460. else if( i >= 4 && i <= 5 && memcmp( sDirective, "IFDEF", i ) == 0 )
  461. ParseIfdef( sLine, TRUE ); /* --- #ifdef --- */
  462. else if( i >= 4 && i <= 6 && memcmp( sDirective, "IFNDEF", i ) == 0 )
  463. ParseIfdef( sLine, FALSE ); /* --- #ifndef --- */
  464. else if( hb_pp_nCondCompile == 0 || hb_pp_aCondCompile[hb_pp_nCondCompile - 1] )
  465. {
  466. if( i >= 4 && i <= 7 && memcmp( sDirective, "INCLUDE", i ) == 0 )
  467. { /* --- #include --- */
  468. char cDelimChar;
  469. if( *sLine != '\"' && *sLine != '\'' && *sLine != '<' )
  470. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_WRONG_NAME, NULL, NULL );
  471. cDelimChar = *sLine;
  472. if( cDelimChar == '<' )
  473. cDelimChar = '>';
  474. else if( cDelimChar == '`' )
  475. cDelimChar = '\'';
  476. sLine++;
  477. i = 0;
  478. while( *( sLine + i ) != '\0' && *( sLine + i ) != cDelimChar )
  479. i++;
  480. if( *( sLine + i ) != cDelimChar )
  481. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_WRONG_NAME, NULL, NULL );
  482. *( sLine + i ) = '\0';
  483. if( !OpenInclude( sLine, hb_comp_pIncludePath, hb_comp_pFileName, ( cDelimChar == '>' ), szInclude ) )
  484. {
  485. if( errno == 0 || errno == EMFILE )
  486. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_TOO_MANY_INCLUDES, sLine, NULL );
  487. else
  488. {
  489. #if defined(__CYGWIN__) || defined(__IBMCPP__) || defined(__LCC__)
  490. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_CANNOT_OPEN, sLine, "" );
  491. #else
  492. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_CANNOT_OPEN, sLine, strerror( errno ) );
  493. #endif
  494. }
  495. }
  496. }
  497. else if( i >= 4 && i <= 6 && memcmp( sDirective, "DEFINE", i ) == 0 )
  498. hb_pp_ParseDefine_( sLine ); /* --- #define --- */
  499. else if( i >= 4 && i <= 5 && memcmp( sDirective, "UNDEF", i ) == 0 )
  500. ParseUndef( sLine ); /* --- #undef --- */
  501. else if( ( i >= 4 && i <= 7 && memcmp( sDirective, "COMMAND", i ) == 0 ) || ( i >= 4 && i <= 8 && memcmp( sDirective, "XCOMMAND", i ) == 0 ) )
  502. /* --- #command --- */
  503. ParseCommand( sLine, ( i == 7 ) ? FALSE : TRUE, TRUE );
  504. else
  505. if( ( i >= 4 && i <= 9 && memcmp( sDirective, "TRANSLATE", i ) == 0 )
  506. || ( i >= 4 && i <= 10 && memcmp( sDirective, "XTRANSLATE", i ) == 0 ) )
  507. /* --- #translate --- */
  508. ParseCommand( sLine, ( i == 9 ) ? FALSE : TRUE, FALSE );
  509. else if( i >= 4 && i <= 6 && memcmp( sDirective, "STDOUT", i ) == 0 )
  510. printf( "%s\n", sLine ); /* --- #stdout --- */
  511. else if( i >= 4 && i <= 5 && memcmp( sDirective, "ERROR", i ) == 0 )
  512. /* --- #error --- */
  513. hb_compGenError( NULL, hb_pp_szErrors, 'E', HB_PP_ERR_EXPLICIT, sLine, NULL );
  514. else if( i == 4 && memcmp( sDirective, "LINE", 4 ) == 0 )
  515. return -1;
  516. else if( i == 6 && memcmp( sDirective, "PRAGMA", 6 ) == 0 )
  517. {
  518. hb_pp_strocpy( sParse, sParse + 6 );
  519. bIgnore = hb_pp_ParsePragma( sParse ); /* --- #pragma --- */
  520. }
  521. else
  522. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_WRONG_DIRECTIVE, sDirective, NULL );
  523. }
  524. return bIgnore;
  525. }
  526. int hb_pp_ParseDefine_( char *sLine )
  527. {
  528. char defname[MAX_NAME], pars[MAX_NAME + 1];
  529. int i, npars = -1;
  530. DEFINES *lastdef;
  531. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_ParseDefine_(%s)", sLine ) );
  532. HB_SKIPTABSPACES( sLine );
  533. if( ISNAME( ( BYTE ) * sLine ) )
  534. {
  535. char *cParams = NULL;
  536. NextName( &sLine, defname );
  537. if( *sLine == '(' ) /* If pseudofunction was found */
  538. {
  539. int iParLen = 0;
  540. int iLen;
  541. sLine++;
  542. HB_SKIPTABSPACES( sLine );
  543. npars = 0;
  544. while( *sLine && *sLine != ')' )
  545. {
  546. if( ISNAME( ( BYTE ) * sLine ) )
  547. {
  548. NextName( &sLine, pars );
  549. iLen = strlen( pars );
  550. if( cParams == NULL )
  551. {
  552. /* 'xy0' -> '~xy0' */
  553. cParams = ( char * ) hb_xgrab( iLen + 2 );
  554. }
  555. else
  556. {
  557. /* '~xy0' -> '~xy,~ab0' */
  558. char *cPos;
  559. cPos = strstr( cParams, pars );
  560. if( cPos && ( cPos[iLen] == ',' || cPos[iLen] == '\0' ) )
  561. {
  562. cPos--;
  563. if( *cPos == '\001' )
  564. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_LABEL_DUPL_IN_DEFINE, defname, pars );
  565. }
  566. cParams = ( char * ) hb_xrealloc( cParams, iParLen + iLen + 3 );
  567. cParams[iParLen++] = ',';
  568. cParams[iParLen] = '\0';
  569. }
  570. cParams[iParLen] = '\001';
  571. memcpy( cParams + iParLen + 1, pars, iLen + 1 );
  572. iParLen += iLen + 1;
  573. npars++;
  574. HB_SKIPTABSPACES( sLine );
  575. }
  576. else
  577. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_LABEL_MISSING_IN_DEFINE, defname, NULL );
  578. if( *sLine == ',' )
  579. {
  580. sLine++;
  581. HB_SKIPTABSPACES( sLine );
  582. if( *sLine == ')' )
  583. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_LABEL_MISSING_IN_DEFINE, defname, NULL );
  584. }
  585. }
  586. HB_SKIPTABSPACES( sLine );
  587. if( *sLine == '\0' )
  588. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_PARE_MISSING_IN_DEFINE, defname, NULL );
  589. sLine++;
  590. }
  591. HB_SKIPTABSPACES( sLine );
  592. if( cParams )
  593. {
  594. char *tmp = cParams;
  595. char *cPos;
  596. int iPar, iLen, iPos, iOldPos;
  597. iLen = strlen( sLine );
  598. for( i = 0; i < npars; i++ )
  599. {
  600. /*1z,1y */
  601. cPos = strchr( tmp, ',' );
  602. if( cPos )
  603. iPar = cPos - tmp;
  604. else
  605. iPar = strlen( tmp );
  606. memcpy( pars, tmp, iPar );
  607. pars[iPar] = '\0';
  608. iOldPos = 0;
  609. while( ( iPos = md_strAt( pars + 1, iPar - 1, sLine + iOldPos, TRUE, FALSE, FALSE, MD_STR_AT_IGNORECASE ) ) != 0 )
  610. {
  611. if( sLine[iOldPos + iPos] != '\001' )
  612. {
  613. hb_pp_Stuff( pars, sLine + iOldPos + iPos - 1, iPar, iPar - 1, iLen - iPos - iOldPos );
  614. iLen++;
  615. }
  616. iOldPos += iPos + iPar;
  617. }
  618. if( cPos )
  619. tmp = cPos + 1;
  620. }
  621. }
  622. lastdef = hb_pp_AddDefine_( defname, ( *sLine == '\0' ) ? NULL : sLine );
  623. if( lastdef )
  624. {
  625. lastdef->npars = npars;
  626. lastdef->pars = cParams;
  627. }
  628. else if( cParams )
  629. hb_xfree( cParams );
  630. }
  631. else
  632. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_DEFINE_ABSENT, NULL, NULL );
  633. return 0;
  634. }
  635. DEFINES *hb_pp_AddDefine_( char *defname, char *value )
  636. {
  637. BOOL isNew;
  638. DEFINES *stdef;
  639. int len = strlen( defname );
  640. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_AddDefine_(%s, %s)", defname, value ) );
  641. stdef = DefSearch( defname, len, &isNew );
  642. if( stdef != NULL )
  643. {
  644. hb_compGenWarning( NULL, hb_pp_szWarnings, 'I', HB_PP_WARN_DEFINE_REDEF, defname, NULL );
  645. if( isNew )
  646. {
  647. if( stdef->pars )
  648. hb_xfree( stdef->pars );
  649. if( stdef->value )
  650. hb_xfree( stdef->value );
  651. }
  652. else
  653. return NULL;
  654. }
  655. else
  656. {
  657. stdef = ( DEFINES * ) hb_xgrab( sizeof( DEFINES ) );
  658. stdef->last = hb_pp_topDefine;
  659. hb_pp_topDefine = stdef;
  660. stdef->name = hb_strdup( defname );
  661. stdef->namelen = len;
  662. stdef->npars = -1;
  663. s_kolAddDefs++;
  664. }
  665. stdef->value = ( value == NULL ) ? NULL : hb_strdup( value );
  666. stdef->pars = NULL;
  667. return stdef;
  668. }
  669. static int ParseUndef( char *sLine )
  670. {
  671. char defname[MAX_NAME];
  672. DEFINES *stdef;
  673. BOOL isNew;
  674. int len;
  675. HB_TRACE( HB_TR_DEBUG, ( "ParseUndef(%s)", sLine ) );
  676. NextWord( &sLine, defname, FALSE );
  677. len = strlen( defname );
  678. if( ( stdef = DefSearch( defname, len, &isNew ) ) != NULL )
  679. {
  680. if( isNew )
  681. {
  682. if( stdef->pars )
  683. hb_xfree( stdef->pars );
  684. if( stdef->value )
  685. hb_xfree( stdef->value );
  686. hb_xfree( stdef->name );
  687. }
  688. stdef->pars = NULL;
  689. stdef->value = NULL;
  690. stdef->name = NULL;
  691. stdef->namelen = 0;
  692. }
  693. return 0;
  694. }
  695. static int ParseIfdef( char *sLine, int usl )
  696. {
  697. char defname[MAX_NAME];
  698. DEFINES *stdef;
  699. int len = 0;
  700. HB_TRACE( HB_TR_DEBUG, ( "ParseIfdef(%s, %d)", sLine, usl ) );
  701. if( hb_pp_nCondCompile == 0 || hb_pp_aCondCompile[hb_pp_nCondCompile - 1] )
  702. {
  703. len = NextWord( &sLine, defname, FALSE );
  704. if( *defname == '\0' )
  705. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_DEFINE_ABSENT, NULL, NULL );
  706. }
  707. if( hb_pp_nCondCompile == s_maxCondCompile )
  708. {
  709. s_maxCondCompile += 5;
  710. hb_pp_aCondCompile = ( int * ) hb_xrealloc( hb_pp_aCondCompile, sizeof( int ) * s_maxCondCompile );
  711. }
  712. if( hb_pp_nCondCompile == 0 || hb_pp_aCondCompile[hb_pp_nCondCompile - 1] )
  713. {
  714. if( ( ( stdef = DefSearch( defname, len, NULL ) ) != NULL && usl ) || ( stdef == NULL && !usl ) )
  715. hb_pp_aCondCompile[hb_pp_nCondCompile] = 1;
  716. else
  717. hb_pp_aCondCompile[hb_pp_nCondCompile] = 0;
  718. }
  719. else
  720. hb_pp_aCondCompile[hb_pp_nCondCompile] = 0;
  721. hb_pp_nCondCompile++;
  722. return 0;
  723. }
  724. static DEFINES *DefSearch( char *defname, int len, BOOL * isNew )
  725. {
  726. int kol = 0, j;
  727. DEFINES *stdef = hb_pp_topDefine;
  728. HB_TRACE( HB_TR_DEBUG, ( "DefSearch(%s)", defname ) );
  729. while( stdef != NULL )
  730. {
  731. kol++;
  732. if( stdef->name != NULL && stdef->namelen == len )
  733. {
  734. for( j = 0; *( stdef->name + j ) == *( defname + j ) && *( stdef->name + j ) != '\0'; j++ ) ;
  735. if( *( stdef->name + j ) == *( defname + j ) )
  736. {
  737. if( isNew )
  738. *isNew = ( s_kolAddDefs >= kol );
  739. return stdef;
  740. }
  741. }
  742. stdef = stdef->last;
  743. }
  744. return NULL;
  745. }
  746. static COMMANDS *ComSearch( char *cmdname, COMMANDS * stcmdStart )
  747. {
  748. COMMANDS *stcmd = ( stcmdStart ) ? stcmdStart : hb_pp_topCommand;
  749. HB_TRACE( HB_TR_DEBUG, ( "ComSearch(%s, %p)", cmdname, stcmdStart ) );
  750. while( stcmd != NULL )
  751. {
  752. int j;
  753. for( j = 0; ( *( stcmd->name + j ) == toupper( *( cmdname + j ) ) ) &&
  754. ( *( stcmd->name + j ) != '\0' ) && ( ( stcmd->com_or_xcom ) ? 1 : ( j < 4 || ISNAME( ( BYTE ) * ( cmdname + j + 1 ) ) ) ); j++ ) ;
  755. if( ( *( stcmd->name + j ) == toupper( *( cmdname + j ) ) )
  756. || ( !stcmd->com_or_xcom && j >= 4 && *( stcmd->name + j ) != '\0' && *( cmdname + j ) == '\0' ) )
  757. break;
  758. stcmd = stcmd->last;
  759. }
  760. return stcmd;
  761. }
  762. static COMMANDS *TraSearch( char *cmdname, COMMANDS * sttraStart )
  763. {
  764. int j;
  765. COMMANDS *sttra = ( sttraStart ) ? sttraStart : hb_pp_topTranslate;
  766. HB_TRACE( HB_TR_DEBUG, ( "TraSearch(%s, %p)", cmdname, sttraStart ) );
  767. while( sttra != NULL )
  768. {
  769. for( j = 0; *( sttra->name + j ) == toupper( *( cmdname + j ) ) &&
  770. *( sttra->name + j ) != '\0' && ( ( sttra->com_or_xcom ) ? 1 : ( j < 4 || ISNAME( ( BYTE ) * ( cmdname + j + 1 ) ) ) ); j++ ) ;
  771. if( *( sttra->name + j ) == toupper( *( cmdname + j ) )
  772. || ( !sttra->com_or_xcom && j >= 4 && *( sttra->name + j ) != '\0' && *( cmdname + j ) == '\0' ) )
  773. break;
  774. sttra = sttra->last;
  775. }
  776. return sttra;
  777. }
  778. static void ParseCommand( char *sLine, BOOL com_or_xcom, BOOL com_or_tra )
  779. {
  780. #if !defined(HB_PP_DEBUG_MEMORY)
  781. static char mpatt[PATTERN_SIZE];
  782. #else
  783. char *mpatt = ( char * ) hb_xgrab( PATTERN_SIZE );
  784. #endif
  785. char *rpatt;
  786. char cmdname[MAX_NAME];
  787. COMMANDS *stcmd;
  788. int mlen, rlen;
  789. int ipos;
  790. /* Ron Pinkas added 2000-12-03 */
  791. BOOL bOk = FALSE;
  792. HB_TRACE( HB_TR_DEBUG, ( "ParseCommand(%s, %d, %d)", sLine, com_or_xcom, com_or_tra ) );
  793. HB_SKIPTABSPACES( sLine );
  794. ipos = 0;
  795. /* JFL 2000-09-19 */
  796. /* This was the original line as Alexander wrote it */
  797. /* while( *sLine != '\0' && *sLine != ' ' && *sLine != '\t' && *sLine != '<' && *sLine != '=' && ( *sLine != '(' || ipos == 0 ) ) */
  798. /* Now the line #xtranslate = name(.. => will be allowed */
  799. /* I changed it to the following to allow < and = to be the first char within a translate or xtranslate */
  800. while( *sLine != '\0' && *sLine != ' ' && *sLine != '\t'
  801. && ( *sLine != '<' || ipos == 0 ) && ( *sLine != '=' || ipos == 0 ) && ( *sLine != '(' || ipos == 0 ) )
  802. {
  803. /* Ron Pinkas added 2000-01-24 */
  804. if( !ISNAME( ( BYTE ) * sLine ) )
  805. {
  806. if( *sLine == '[' && ipos )
  807. break;
  808. if( IS_2CHAR_OPERATOR( sLine ) )
  809. {
  810. *( cmdname + ipos++ ) = *sLine++;
  811. *( cmdname + ipos++ ) = *sLine++;
  812. break;
  813. }
  814. else
  815. {
  816. *( cmdname + ipos++ ) = *sLine++;
  817. break;
  818. }
  819. }
  820. /* END, Ron Pinkas added 2000-01-24 */
  821. *( cmdname + ipos++ ) = *sLine++;
  822. }
  823. *( cmdname + ipos ) = '\0';
  824. if( !ipos )
  825. {
  826. #if defined(HB_PP_DEBUG_MEMORY)
  827. hb_xfree( ( void * ) mpatt );
  828. #endif
  829. return;
  830. }
  831. hb_strupr( cmdname );
  832. HB_SKIPTABSPACES( sLine );
  833. /* Ron Pinkas added 2000-12-03 */
  834. ipos = 0;
  835. while( *sLine )
  836. {
  837. mpatt[ipos++] = *sLine;
  838. if( *sLine == '=' )
  839. {
  840. int i = ipos;
  841. sLine++;
  842. mpatt[i++] = *sLine;
  843. while( *sLine && ( *sLine == ' ' || *sLine == '\t' ) )
  844. {
  845. sLine++;
  846. mpatt[i++] = *sLine;
  847. }
  848. if( *sLine == '>' )
  849. {
  850. ipos = ipos - 2;
  851. while( mpatt[ipos] == ' ' || mpatt[ipos] == '\t' )
  852. {
  853. ipos--;
  854. }
  855. mpatt[ipos + 1] = '\0';
  856. sLine++;
  857. bOk = TRUE;
  858. break;
  859. }
  860. ipos = i;
  861. }
  862. sLine++;
  863. }
  864. /* End - Ron Pinkas added 2000-12-03 */
  865. /* Ron Pinkas modified 2000-12-03
  866. if( (ipos = hb_strAt( "=>", 2, sLine, strlen(sLine) )) > 0 ) */
  867. if( bOk )
  868. {
  869. /* Ron Pinkas removed 2000-12-03
  870. stroncpy( mpatt, sLine, ipos-1 ); */
  871. mlen = strotrim( mpatt, TRUE );
  872. /* Ron Pinkas removed 2000-12-03
  873. sLine += ipos + 1; */
  874. HB_SKIPTABSPACES( sLine );
  875. /* hb_pp_strocpy( rpatt, sLine ); */
  876. rpatt = sLine;
  877. rlen = strotrim( rpatt, TRUE );
  878. ConvertPatterns( mpatt, mlen, rpatt, rlen );
  879. RemoveSlash( mpatt );
  880. rlen = RemoveSlash( rpatt );
  881. if( com_or_tra )
  882. stcmd = AddCommand( cmdname );
  883. else
  884. stcmd = AddTranslate( cmdname );
  885. stcmd->com_or_xcom = com_or_xcom;
  886. stcmd->mpatt = hb_strdup( mpatt );
  887. stcmd->value = ( rlen > 0 ) ? hb_strdup( rpatt ) : NULL;
  888. }
  889. else
  890. {
  891. sLine -= ( ipos + 1 );
  892. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_COMMAND_DEFINITION, cmdname, sLine );
  893. }
  894. #if defined(HB_PP_DEBUG_MEMORY)
  895. hb_xfree( ( void * ) mpatt );
  896. #endif
  897. }
  898. /* Remove escape characters and check '[' optional markers
  899. */
  900. static int ConvertOptional( char *cpatt, int len, BOOL bLeft )
  901. {
  902. int i = 0;
  903. while( cpatt[i] != '\0' )
  904. {
  905. if( cpatt[i] == '"' || cpatt[i] == '\'' )
  906. {
  907. char c = cpatt[i];
  908. i++;
  909. while( cpatt[i] && cpatt[i] != c )
  910. {
  911. i++;
  912. }
  913. i++;
  914. continue; /* skip "strings" */
  915. }
  916. if( cpatt[i] == '[' )
  917. {
  918. if( i && cpatt[i - 1] == '\\' )
  919. {
  920. hb_pp_Stuff( "", cpatt + i - 1, 0, 1, len - i + 1 );
  921. len--;
  922. continue;
  923. }
  924. else
  925. {
  926. int j = i + 1;
  927. int iOpenBrackets = 1;
  928. BOOL bOption = FALSE;
  929. while( cpatt[j] && iOpenBrackets )
  930. {
  931. if( cpatt[j] == '[' && cpatt[j - 1] != '\\' )
  932. iOpenBrackets++;
  933. else if( cpatt[j] == ']' && cpatt[j - 1] != '\\' )
  934. {
  935. if( --iOpenBrackets == 0 && ( bOption || bLeft ) )
  936. {
  937. cpatt[i] = HB_PP_OPT_START;
  938. cpatt[j] = HB_PP_OPT_END;
  939. }
  940. }
  941. else if( cpatt[j] == '<' )
  942. {
  943. j++;
  944. while( cpatt[j] == ' ' || cpatt[j] == '\t' )
  945. j++;
  946. if( strchr( "*(!-{.\"", cpatt[j] ) || ISNAME( ( BYTE ) cpatt[j] ) )
  947. {
  948. bOption = TRUE;
  949. continue;
  950. }
  951. }
  952. else if( cpatt[j] == '"' || cpatt[j] == '\'' )
  953. {
  954. char c = cpatt[j];
  955. j++;
  956. while( cpatt[j] && cpatt[j] != c )
  957. {
  958. j++;
  959. }
  960. }
  961. j++;
  962. }
  963. if( iOpenBrackets )
  964. {
  965. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_PATTERN_DEFINITION, cpatt + i, NULL );
  966. }
  967. }
  968. }
  969. else if( cpatt[i] == ']' )
  970. {
  971. if( i && cpatt[i - 1] == '\\' )
  972. {
  973. hb_pp_Stuff( "", cpatt + i - 1, 0, 1, len - i + 1 );
  974. len--;
  975. continue;
  976. }
  977. }
  978. i++;
  979. }
  980. return len;
  981. }
  982. static void RemoveOptional( char *cpatt )
  983. {
  984. int i = 0;
  985. int len = strlen( cpatt );
  986. int iOpenBra = 0;
  987. while( cpatt[i] != '\0' )
  988. {
  989. if( cpatt[i] == '"' || cpatt[i] == '\'' )
  990. {
  991. char c = cpatt[i++];
  992. while( cpatt[i] && cpatt[i] != c )
  993. {
  994. i++;
  995. }
  996. if( cpatt[i] )
  997. i++;
  998. continue; /* skip "strings" */
  999. }
  1000. if( cpatt[i] == '[' )
  1001. {
  1002. i++;
  1003. iOpenBra++;
  1004. while( cpatt[i] && iOpenBra )
  1005. {
  1006. if( cpatt[i] == '[' )
  1007. iOpenBra++;
  1008. else if( cpatt[i] == ']' )
  1009. iOpenBra--;
  1010. i++;
  1011. }
  1012. continue; /* skip [strings] */
  1013. }
  1014. if( cpatt[i] == HB_PP_OPT_START || cpatt[i] == HB_PP_OPT_END )
  1015. {
  1016. hb_pp_Stuff( "", cpatt + i, 0, 1, len - i + 1 );
  1017. len--;
  1018. }
  1019. else
  1020. i++;
  1021. }
  1022. }
  1023. /* ConvertPatterns()
  1024. * Converts result pattern in #command and #translate to inner format
  1025. */
  1026. static void ConvertPatterns( char *mpatt, int mlen, char *rpatt, int rlen )
  1027. {
  1028. int i = 0, ipos, ifou;
  1029. int explen, rmlen;
  1030. char exppatt[MAX_NAME], expreal[5] = " 0";
  1031. char lastchar = '@', exptype;
  1032. char *ptr, *ptrtmp;
  1033. HB_TRACE( HB_TR_DEBUG, ( "ConvertPatterns(%s, %d, %s, %d)", mpatt, mlen, rpatt, rlen ) );
  1034. expreal[0] = HB_PP_MATCH_MARK;
  1035. mlen = ConvertOptional( mpatt, mlen, TRUE ); /* left pattern */
  1036. rlen = ConvertOptional( rpatt, rlen, FALSE ); /* right pattern */
  1037. while( *( mpatt + i ) != '\0' )
  1038. {
  1039. if( mpatt[i] == '"' || mpatt[i] == '\'' )
  1040. {
  1041. char c = mpatt[i];
  1042. i++;
  1043. while( mpatt[i] && mpatt[i] != c )
  1044. {
  1045. i++;
  1046. }
  1047. i++;
  1048. continue; /* skip "strings" */
  1049. }
  1050. if( *( mpatt + i ) == '<' )
  1051. {
  1052. if( i && mpatt[ i-1 ] == '\\' )
  1053. {
  1054. i++;
  1055. continue;
  1056. }
  1057. /* Drag match marker, determine it type */
  1058. explen = 0;
  1059. ipos = i;
  1060. i++;
  1061. exptype = '0';
  1062. while( *( mpatt + i ) == ' ' || *( mpatt + i ) == '\t' )
  1063. i++;
  1064. if( *( mpatt + i ) == '*' ) /* Wild match marker */
  1065. {
  1066. exptype = '3';
  1067. i++;
  1068. }
  1069. else if( *( mpatt + i ) == '(' ) /* Extended expression match marker */
  1070. {
  1071. exptype = '4';
  1072. i++;
  1073. }
  1074. else if( *( mpatt + i ) == '!' ) /* Minimal expression match marker */
  1075. {
  1076. exptype = '5';
  1077. i++;
  1078. }
  1079. ptr = mpatt + i;
  1080. while( *ptr != '>' )
  1081. {
  1082. if( *ptr == '\0' || *ptr == '<' || *ptr == '[' || *ptr == ']' )
  1083. {
  1084. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_PATTERN_DEFINITION, NULL, NULL );
  1085. return;
  1086. }
  1087. ptr++;
  1088. }
  1089. while( *( mpatt + i ) != '>' )
  1090. {
  1091. if( *( mpatt + i ) == ',' ) /* List match marker */
  1092. {
  1093. exptype = '1';
  1094. while( *( mpatt + i ) != '>' )
  1095. i++;
  1096. break;
  1097. }
  1098. else if( *( mpatt + i ) == ':' ) /* Restricted match marker */
  1099. {
  1100. exptype = '2';
  1101. *( mpatt + i-- ) = ' ';
  1102. break;
  1103. }
  1104. if( *( mpatt + i ) != ' ' && *( mpatt + i ) != '\t' )
  1105. *( exppatt + explen++ ) = *( mpatt + i );
  1106. i++;
  1107. }
  1108. if( exptype == '3' )
  1109. {
  1110. if( *( exppatt + explen - 1 ) == '*' )
  1111. explen--;
  1112. else
  1113. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_PATTERN_DEFINITION, NULL, NULL );
  1114. }
  1115. else if( exptype == '4' )
  1116. {
  1117. if( *( exppatt + explen - 1 ) == ')' )
  1118. explen--;
  1119. else
  1120. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_PATTERN_DEFINITION, NULL, NULL );
  1121. }
  1122. else if( exptype == '5' )
  1123. {
  1124. if( *( exppatt + explen - 1 ) == '!' )
  1125. explen--;
  1126. else
  1127. hb_compGenError( NULL, hb_pp_szErrors, 'F', HB_PP_ERR_PATTERN_DEFINITION, NULL, NULL );
  1128. }
  1129. rmlen = i - ipos + 1;
  1130. /* Convert match marker into inner format */
  1131. lastchar = ( lastchar != 'Z' ) ? ( ( char ) ( ( unsigned int ) lastchar + 1 ) ) : 'a';
  1132. expreal[1] = lastchar;
  1133. expreal[2] = exptype;
  1134. hb_pp_Stuff( expreal, mpatt + ipos, 4, rmlen, mlen - ipos );
  1135. mlen += 4 - rmlen;
  1136. i += 4 - rmlen;
  1137. /* Look for appropriate result markers */
  1138. ptr = rpatt;
  1139. while( ( ifou = hb_strAt( exppatt, explen, ptr, rlen - ( ptr - rpatt ) ) ) > 0 )
  1140. {
  1141. /* Convert result marker into inner format */
  1142. ifou--;
  1143. ptr += ifou;
  1144. ptrtmp = ptr + 1;
  1145. rmlen = explen;
  1146. exptype = '0'; /* regular result marker */
  1147. do
  1148. {
  1149. ptr--;
  1150. rmlen++;
  1151. ifou--;
  1152. if( *ptr == '<' )
  1153. continue;
  1154. else if( *ptr == '\"' )
  1155. exptype = '2'; /* normal stringify result marker */
  1156. else if( *ptr == '(' )
  1157. exptype = '3'; /* Smart stringify result marker */
  1158. else if( *ptr == '{' )
  1159. exptype = '4'; /* Blockify result marker */
  1160. else if( *ptr == '.' )
  1161. exptype = '5'; /* Logify result marker */
  1162. else if( *ptr == '-' )
  1163. exptype = '6'; /* ommit (remove) result marker */
  1164. else if( *ptr == ' ' || *ptr == '\t' )
  1165. continue;
  1166. else
  1167. ifou = -1;
  1168. }
  1169. while( ifou >= 0 && *ptr != '<' && *( ptr - 1 ) != '\\' );
  1170. if( ifou >= 0 && *ptr == '<' )
  1171. {
  1172. ptr += rmlen++;
  1173. while( *ptr != '\0' && *ptr != '>' && *( ptr - 1 ) != '\\' )
  1174. {
  1175. if( *ptr != ' ' && *ptr != '\t' && *ptr != '\"' && *ptr != ')' && *ptr != '}' && *ptr != '.' && *ptr != '-' )
  1176. {
  1177. ifou = -1;
  1178. break;
  1179. }
  1180. rmlen++;
  1181. ptr++;
  1182. }
  1183. if( ifou >= 0 && *ptr == '>' )
  1184. {
  1185. ptr -= rmlen;
  1186. ptr++;
  1187. if( exptype == '0' && *( ptr - 1 ) == '#' && *( ptr - 2 ) != '\\' )
  1188. {
  1189. exptype = '1'; /* dumb stringify result marker */
  1190. ptr--;
  1191. rmlen++;
  1192. }
  1193. expreal[2] = exptype;
  1194. hb_pp_Stuff( expreal, ptr, 4, rmlen, rlen + ( rpatt - ptr ) );
  1195. rlen += 4 - rmlen;
  1196. }
  1197. else
  1198. ptr = ptrtmp;
  1199. }
  1200. else
  1201. ptr = ptrtmp;
  1202. }
  1203. }
  1204. i++;
  1205. }
  1206. }
  1207. static COMMANDS *AddCommand( char *cmdname )
  1208. {
  1209. COMMANDS *stcmd;
  1210. HB_TRACE( HB_TR_DEBUG, ( "AddCommand(%s)", cmdname ) );
  1211. stcmd = ( COMMANDS * ) hb_xgrab( sizeof( COMMANDS ) );
  1212. stcmd->last = hb_pp_topCommand;
  1213. hb_pp_topCommand = stcmd;
  1214. stcmd->name = hb_strdup( cmdname );
  1215. stcmd->namelen = strlen( cmdname );
  1216. s_kolAddComs++;
  1217. return stcmd;
  1218. }
  1219. static COMMANDS *AddTranslate( char *traname )
  1220. {
  1221. COMMANDS *sttra;
  1222. HB_TRACE( HB_TR_DEBUG, ( "AddTranslate(%s)", traname ) );
  1223. sttra = ( COMMANDS * ) hb_xgrab( sizeof( COMMANDS ) );
  1224. sttra->last = hb_pp_topTranslate;
  1225. hb_pp_topTranslate = sttra;
  1226. sttra->name = hb_strdup( traname );
  1227. sttra->namelen = strlen( traname );
  1228. s_kolAddTras++;
  1229. return sttra;
  1230. }
  1231. int hb_pp_ParseExpression( char *sLine, char *sOutLine, BOOL bSplitLines )
  1232. {
  1233. #if !defined(HB_PP_DEBUG_MEMORY)
  1234. static char rpatt[PATTERN_SIZE];
  1235. #else
  1236. char *rpatt = ( char * ) hb_xgrab( PATTERN_SIZE );
  1237. #endif
  1238. char sToken[MAX_NAME];
  1239. char *ptri, *ptro, *ptrb;
  1240. int lenToken, i, ipos, isdvig, lens;
  1241. int ifou;
  1242. int rezDef, rezTra, rezCom;
  1243. unsigned int kolpass = 0;
  1244. DEFINES *stdef;
  1245. COMMANDS *stcmd;
  1246. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_ParseExpression(%s, %s)", sLine, sOutLine ) );
  1247. do
  1248. {
  1249. strotrim( sLine, FALSE );
  1250. rezDef = 0;
  1251. rezTra = 0;
  1252. rezCom = 0;
  1253. isdvig = 0;
  1254. do
  1255. {
  1256. ptro = sOutLine;
  1257. ptri = sLine + isdvig;
  1258. if( bSplitLines )
  1259. ipos = md_strAt( ";", 1, ptri, TRUE, FALSE, FALSE, MD_STR_AT_IGNORECASE );
  1260. else
  1261. ipos = 0;
  1262. if( ipos > 0 )
  1263. {
  1264. *( ptri + ipos - 1 ) = '\0';
  1265. }
  1266. HB_SKIPTABSPACES( ptri );
  1267. if( *ptri == '#' )
  1268. {
  1269. int bIgnore;
  1270. hb_strncpy( rpatt, ptri, PATTERN_SIZE - 1 );
  1271. bIgnore = hb_pp_ParseDirective_( rpatt );
  1272. if( ipos > 0 )
  1273. {
  1274. ipos--;
  1275. *( sLine + isdvig + ipos - 1 ) = ';';
  1276. *( sLine + isdvig + ipos ) = ' ';
  1277. }
  1278. lens = strlen( sLine + isdvig );
  1279. if( bIgnore )
  1280. hb_pp_Stuff( " ", sLine + isdvig, 0, ( ipos ) ? ipos : lens, lens );
  1281. else
  1282. hb_pp_Stuff( rpatt, sLine + isdvig, strlen( rpatt ), ( ipos ) ? ipos : lens, lens );
  1283. if( ipos > 0 )
  1284. {
  1285. ipos = 1;
  1286. }
  1287. }
  1288. else
  1289. { /* Look for macros from #define */
  1290. while( ( lenToken = NextName( &ptri, sToken ) ) > 0 )
  1291. {
  1292. #if 0
  1293. printf( "Token: >%s< Line: >%s<\n", sToken, sLine );
  1294. #endif
  1295. if( ( stdef = DefSearch( sToken, lenToken, NULL ) ) != NULL )
  1296. {
  1297. ptrb = ptri - lenToken;
  1298. if( ( i = WorkDefine( &ptri, ptro, stdef ) ) >= 0 )
  1299. {
  1300. rezDef++;
  1301. lens = strlen( ptrb );
  1302. if( ipos > 0 )
  1303. {
  1304. *( ptrb + lens ) = ';';
  1305. lens += strlen( ptrb + lens + 1 );
  1306. }
  1307. hb_pp_Stuff( ptro, ptrb, i, ptri - ptrb, lens + 1 );
  1308. if( ipos > 0 )
  1309. {
  1310. ipos += i - ( ptri - ptrb );
  1311. *( sLine + isdvig + ipos - 1 ) = '\0';
  1312. }
  1313. ptri += i - ( ptri - ptrb );
  1314. }
  1315. }
  1316. }
  1317. if( rezDef == 0 )
  1318. {
  1319. /* Look for definitions from #translate */
  1320. stcmd = hb_pp_topTranslate;
  1321. while( stcmd != NULL )
  1322. {
  1323. ptri = sLine + isdvig;
  1324. lenToken = stcmd->namelen;
  1325. while( ( ifou = md_strAt( stcmd->name, lenToken, ptri, TRUE, FALSE, FALSE, MD_STR_AT_USESUBCASE ) ) > 0 )
  1326. {
  1327. ptri += ifou - 1;
  1328. if( ( i = WorkTranslate( ptri + lenToken, ptro, stcmd, &lens ) ) >= 0 )
  1329. {
  1330. lens += lenToken;
  1331. while( lens > 0 && ( *( ptri + lens - 1 ) == ' ' || *( ptri + lens - 1 ) == '\t' ) )
  1332. {
  1333. lens--;
  1334. }
  1335. if( ipos > 0 )
  1336. {
  1337. *( sLine + isdvig + ipos - 1 ) = ';';
  1338. }
  1339. hb_pp_Stuff( ptro, ptri, i, lens, strlen( ptri ) );
  1340. rezTra = 1;
  1341. if( ipos > 0 )
  1342. {
  1343. ipos += i - lens;
  1344. *( sLine + isdvig + ipos - 1 ) = '\0';
  1345. }
  1346. ptri += i;
  1347. }
  1348. else
  1349. {
  1350. ptri += lenToken;
  1351. }
  1352. }
  1353. stcmd = stcmd->last;
  1354. }
  1355. } /* rezDef == 0 */
  1356. /* Look for definitions from #command */
  1357. /* JFL ! Was 3 but insufficient in most cases */
  1358. /* I know this is a new hardcoded limit ... any better idea's welcome */
  1359. if( rezDef == 0 && rezTra == 0 && kolpass < 20 )
  1360. {
  1361. ptri = sLine + isdvig;
  1362. HB_SKIPTABSPACES( ptri );
  1363. if( ISNAME( ( BYTE ) * ptri ) )
  1364. {
  1365. NextName( &ptri, sToken );
  1366. }
  1367. else
  1368. {
  1369. /* Ron Pinkas commented 2000-01-24
  1370. i = 0;
  1371. while( *ptri != ' ' && *ptri != '\t' && *ptri != '\0' && *ptri != '\"' && *ptri != '\'' && *ptri != '(' && !ISNAME( ( BYTE ) *ptri ) )
  1372. {
  1373. *(sToken+i) = *ptri++;
  1374. i++;
  1375. }
  1376. *(sToken+i) = '\0';
  1377. */
  1378. /* Ron Pinkas added 2000-01-24 */
  1379. if( IS_2CHAR_OPERATOR( ptri ) )
  1380. {
  1381. sToken[0] = *ptri++;
  1382. sToken[1] = *ptri++;
  1383. sToken[2] = '\0';
  1384. }
  1385. else
  1386. {
  1387. sToken[0] = *ptri++;
  1388. sToken[1] = '\0';
  1389. }
  1390. /* END, Ron Pinkas added 2000-01-24 */
  1391. }
  1392. HB_SKIPTABSPACES( ptri );
  1393. if( ( *ptri == '\0'
  1394. || ( *ptri != '='
  1395. && ( !IsInStr( *ptri, ":/+*-%^" )
  1396. || *( ptri + 1 ) != '=' ) && ( *ptri != '-'
  1397. || *( ptri + 1 ) != '>' ) ) ) && ( stcmd = ComSearch( sToken, NULL ) ) != NULL )
  1398. {
  1399. ptro = sOutLine;
  1400. i = WorkCommand( ptri, ptro, stcmd );
  1401. ptri = sLine + isdvig;
  1402. if( ipos > 0 )
  1403. {
  1404. *( ptri + ipos - 1 ) = ';';
  1405. }
  1406. if( i >= 0 )
  1407. {
  1408. if( isdvig + ipos > 0 )
  1409. {
  1410. lens = strlen( sLine + isdvig );
  1411. hb_pp_Stuff( ptro, sLine + isdvig, i, ( ipos ) ? ipos - 1 : lens, lens );
  1412. if( ipos > 0 )
  1413. {
  1414. ipos = i + 1;
  1415. }
  1416. }
  1417. else
  1418. {
  1419. memcpy( sLine, sOutLine, i + 1 );
  1420. }
  1421. }
  1422. rezCom = 1;
  1423. }

Large files files are truncated, but you can click here to view the full file