PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/build46/harbour/source/pp/ppcore.c

#
C | 2253 lines | 1856 code | 262 blank | 135 comment | 594 complexity | 1c0bd222b836e96740bb945a3bb3017f 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: ppcore.c 6682 2006-06-02 11:43:30Z rglab $
  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 "hbpp.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 );
  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. 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. 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 hb_compInclude, BOOL hb_comp_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( ( *hb_compInclude ) ( 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( !hb_comp_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( 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. if( hb_comp_files.pLast->yyBuffer )
  248. {
  249. hb_compParserStop( ); /* uses hb_comp_files.pLast */
  250. }
  251. hb_xfree( hb_comp_files.pLast );
  252. hb_comp_files.pLast = NULL;
  253. hb_comp_files.iFiles = 0;
  254. hb_xfree( ( void * ) hb_comp_pFileName );
  255. hb_comp_pFileName = NULL;
  256. }
  257. else
  258. {
  259. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_CANNOT_OPEN_RULES, szFileName, NULL );
  260. }
  261. }
  262. else
  263. {
  264. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_BAD_RULES_FILE_NAME, hb_pp_STD_CH, NULL );
  265. }
  266. }
  267. else
  268. {
  269. if( !hb_comp_bQuiet )
  270. printf( "Standard command definitions excluded.\n" );
  271. hb_pp_Init( );
  272. }
  273. hb_xfree( hb_pp_STD_CH );
  274. }
  275. else
  276. {
  277. hb_pp_Table( );
  278. hb_pp_Init( );
  279. }
  280. }
  281. void hb_pp_Free( void )
  282. {
  283. DEFINES *stdef;
  284. COMMANDS *stcmd;
  285. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_Free()" ) );
  286. while( s_kolAddDefs )
  287. {
  288. stdef = hb_pp_topDefine;
  289. if( stdef->pars )
  290. hb_xfree( stdef->pars );
  291. if( stdef->value )
  292. hb_xfree( stdef->value );
  293. if( stdef->name )
  294. hb_xfree( stdef->name );
  295. hb_pp_topDefine = stdef->last;
  296. hb_xfree( stdef );
  297. s_kolAddDefs--;
  298. }
  299. while( s_kolAddComs )
  300. {
  301. stcmd = hb_pp_topCommand;
  302. if( stcmd->mpatt )
  303. hb_xfree( stcmd->mpatt );
  304. if( stcmd->value )
  305. hb_xfree( stcmd->value );
  306. hb_xfree( stcmd->name );
  307. hb_pp_topCommand = stcmd->last;
  308. hb_xfree( stcmd );
  309. s_kolAddComs--;
  310. }
  311. while( s_kolAddTras )
  312. {
  313. stcmd = hb_pp_topTranslate;
  314. if( stcmd->mpatt )
  315. hb_xfree( stcmd->mpatt );
  316. if( stcmd->value )
  317. hb_xfree( stcmd->value );
  318. hb_xfree( stcmd->name );
  319. hb_pp_topTranslate = stcmd->last;
  320. hb_xfree( stcmd );
  321. s_kolAddTras--;
  322. }
  323. if( hb_pp_aCondCompile )
  324. {
  325. hb_xfree( ( void * ) hb_pp_aCondCompile );
  326. hb_pp_aCondCompile = NULL;
  327. }
  328. hb_pp_InternalFree( );
  329. if( s_expreal )
  330. {
  331. hb_xfree( ( void *) s_expreal );
  332. s_expreal = NULL;
  333. }
  334. if( s_expcopy )
  335. {
  336. hb_xfree( ( void *) s_expcopy );
  337. s_expcopy = NULL;
  338. }
  339. }
  340. void hb_pp_Init( void )
  341. {
  342. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_Init()" ) );
  343. hb_pp_Free( );
  344. s_ParseState = 0;
  345. s_maxCondCompile = 5;
  346. s_bReplacePat = TRUE;
  347. s_prevchar = 'A';
  348. if( !hb_pp_aCondCompile )
  349. hb_pp_aCondCompile = ( int * ) hb_xgrab( sizeof( int ) * 5 );
  350. hb_pp_nCondCompile = 0;
  351. {
  352. char sOS[64];
  353. char sVer[64];
  354. char *pSrc, *pDst;
  355. char *szPlatform = hb_verPlatform( );
  356. int n;
  357. strcpy( sOS, "__PLATFORM__" );
  358. pSrc = szPlatform;
  359. n = strlen( sOS );
  360. pDst = sOS;
  361. while( *pSrc && *pSrc != ' ' && n < ( int ) sizeof( sOS ) - 1 )
  362. {
  363. if( *pSrc == '_' || ( *pSrc >= 'A' && *pSrc <= 'Z' ) || ( *pSrc >= 'a' && *pSrc <= 'z' ) || ( *pSrc >= '0' && *pSrc <= '9' ) )
  364. {
  365. pDst[n++] = *pSrc;
  366. }
  367. pSrc++;
  368. }
  369. pDst[n] = 0;
  370. n = 0;
  371. pDst = sVer;
  372. pDst[n++] = '"';
  373. if( *pSrc == ' ' )
  374. {
  375. while( *( ++pSrc ) && n < ( int ) sizeof( sVer ) - 2 )
  376. pDst[n++] = *pSrc;
  377. }
  378. pDst[n++] = '"';
  379. pDst[n] = 0;
  380. hb_pp_AddDefine( sOS, sVer );
  381. #ifdef HB_OS_UNIX
  382. strcpy( &sOS[12], "UNIX" );
  383. hb_pp_AddDefine( sOS, sVer );
  384. #endif
  385. hb_xfree( szPlatform );
  386. }
  387. {
  388. char szResult[6];
  389. USHORT usHarbour = ( 256 * HB_VER_MAJOR ) + HB_VER_MINOR;
  390. /*
  391. This updates __HARBOUR__ on every change of HB_VER_MAJOR / HB_VER_MINOR
  392. HIBYTE is the HB_VER_MAJOR value and the LOBYTE is the HB_VER_MINOR value.
  393. The check below is to ensure that __HARBOUR__ gets the
  394. value of 1 by default
  395. */
  396. sprintf( szResult, "%05d", ( usHarbour ? usHarbour : 1 ) );
  397. hb_pp_AddDefine( "__HARBOUR__", szResult );
  398. }
  399. {
  400. char szResult[11];
  401. time_t t;
  402. struct tm *oTime;
  403. time( &t );
  404. oTime = localtime( &t );
  405. sprintf( szResult, "\"%04d%02d%02d\"", oTime->tm_year + 1900, oTime->tm_mon + 1, oTime->tm_mday );
  406. hb_pp_AddDefine( "__DATE__", szResult );
  407. sprintf( szResult, "\"%02d:%02d:%02d\"", oTime->tm_hour, oTime->tm_min, oTime->tm_sec );
  408. hb_pp_AddDefine( "__TIME__", szResult );
  409. }
  410. {
  411. char szResult[11];
  412. sprintf( szResult, "%d", ( int ) sizeof( void * ) );
  413. #if defined( HB_ARCH_16BIT )
  414. hb_pp_AddDefine( "__ARCH16BIT__", szResult );
  415. #elif defined( HB_ARCH_32BIT )
  416. hb_pp_AddDefine( "__ARCH32BIT__", szResult );
  417. #elif defined( HB_ARCH_64BIT )
  418. hb_pp_AddDefine( "__ARCH64BIT__", szResult );
  419. #endif
  420. #if defined( HB_LITTLE_ENDIAN )
  421. hb_pp_AddDefine( "__LITTLE_ENDIAN__", szResult );
  422. #elif defined( HB_BIG_ENDIAN )
  423. hb_pp_AddDefine( "__BIG_ENDIAN__", szResult );
  424. #elif defined( HB_PDP_ENDIAN )
  425. hb_pp_AddDefine( "__PDP_ENDIAN__", szResult );
  426. #endif
  427. }
  428. #ifdef HARBOUR_START_PROCEDURE
  429. hb_pp_AddDefine( "__HB_MAIN__", HARBOUR_START_PROCEDURE );
  430. #endif
  431. }
  432. /* Table with parse warnings */
  433. /* NOTE: The first character stores the warning's level that triggers this
  434. * warning. The warning's level is set by -w<n> command line option.
  435. */
  436. int hb_pp_ParseDirective( char *sLine )
  437. {
  438. char sDirective[MAX_NAME];
  439. char szInclude[_POSIX_PATH_MAX];
  440. int i;
  441. int bIgnore = 1;
  442. char *sParse;
  443. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_ParseDirective(%s)", sLine ) );
  444. strotrim( sLine, TRUE );
  445. hb_pp_strocpy( sLine, sLine + 1 );
  446. sParse = sLine;
  447. i = NextName( &sLine, sDirective );
  448. hb_strupr( sDirective );
  449. HB_SKIPTABSPACES( sLine );
  450. if( i == 4 && memcmp( sDirective, "ELSE", 4 ) == 0 )
  451. { /* --- #else --- */
  452. if( hb_pp_nCondCompile == 0 )
  453. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_DIRECTIVE_ELSE, NULL, NULL );
  454. else if( hb_pp_nCondCompile == 1 || hb_pp_aCondCompile[hb_pp_nCondCompile - 2] )
  455. hb_pp_aCondCompile[hb_pp_nCondCompile - 1] = 1 - hb_pp_aCondCompile[hb_pp_nCondCompile - 1];
  456. }
  457. else if( i >= 4 && i <= 5 && memcmp( sDirective, "ENDIF", i ) == 0 )
  458. { /* --- #endif --- */
  459. if( hb_pp_nCondCompile == 0 )
  460. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_DIRECTIVE_ENDIF, NULL, NULL );
  461. else
  462. hb_pp_nCondCompile--;
  463. }
  464. else if( i >= 4 && i <= 5 && memcmp( sDirective, "IFDEF", i ) == 0 )
  465. ParseIfdef( sLine, TRUE ); /* --- #ifdef --- */
  466. else if( i >= 4 && i <= 6 && memcmp( sDirective, "IFNDEF", i ) == 0 )
  467. ParseIfdef( sLine, FALSE ); /* --- #ifndef --- */
  468. else if( hb_pp_nCondCompile == 0 || hb_pp_aCondCompile[hb_pp_nCondCompile - 1] )
  469. {
  470. if( i >= 4 && i <= 7 && memcmp( sDirective, "INCLUDE", i ) == 0 )
  471. { /* --- #include --- */
  472. char cDelimChar;
  473. if( *sLine != '\"' && *sLine != '\'' && *sLine != '<' )
  474. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_WRONG_NAME, NULL, NULL );
  475. cDelimChar = *sLine;
  476. if( cDelimChar == '<' )
  477. cDelimChar = '>';
  478. else if( cDelimChar == '`' )
  479. cDelimChar = '\'';
  480. sLine++;
  481. i = 0;
  482. while( *( sLine + i ) != '\0' && *( sLine + i ) != cDelimChar )
  483. i++;
  484. if( *( sLine + i ) != cDelimChar )
  485. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_WRONG_NAME, NULL, NULL );
  486. *( sLine + i ) = '\0';
  487. if( !OpenInclude( sLine, hb_comp_pIncludePath, hb_comp_pFileName, ( cDelimChar == '>' ), szInclude ) )
  488. {
  489. if( errno == 0 || errno == EMFILE )
  490. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_TOO_MANY_INCLUDES, sLine, NULL );
  491. else
  492. {
  493. #if defined(__CYGWIN__) || defined(__IBMCPP__) || defined(__LCC__)
  494. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_CANNOT_OPEN, sLine, "" );
  495. #else
  496. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_CANNOT_OPEN, sLine, strerror( errno ) );
  497. #endif
  498. }
  499. }
  500. }
  501. else if( i >= 4 && i <= 6 && memcmp( sDirective, "DEFINE", i ) == 0 )
  502. hb_pp_ParseDefine( sLine ); /* --- #define --- */
  503. else if( i >= 4 && i <= 5 && memcmp( sDirective, "UNDEF", i ) == 0 )
  504. ParseUndef( sLine ); /* --- #undef --- */
  505. else if( ( i >= 4 && i <= 7 && memcmp( sDirective, "COMMAND", i ) == 0 ) || ( i >= 4 && i <= 8 && memcmp( sDirective, "XCOMMAND", i ) == 0 ) )
  506. /* --- #command --- */
  507. ParseCommand( sLine, ( i == 7 ) ? FALSE : TRUE, TRUE );
  508. else
  509. if( ( i >= 4 && i <= 9 && memcmp( sDirective, "TRANSLATE", i ) == 0 )
  510. || ( i >= 4 && i <= 10 && memcmp( sDirective, "XTRANSLATE", i ) == 0 ) )
  511. /* --- #translate --- */
  512. ParseCommand( sLine, ( i == 9 ) ? FALSE : TRUE, FALSE );
  513. else if( i >= 4 && i <= 6 && memcmp( sDirective, "STDOUT", i ) == 0 )
  514. printf( "%s\n", sLine ); /* --- #stdout --- */
  515. else if( i >= 4 && i <= 5 && memcmp( sDirective, "ERROR", i ) == 0 )
  516. /* --- #error --- */
  517. hb_compGenError( hb_pp_szErrors, 'E', HB_PP_ERR_EXPLICIT, sLine, NULL );
  518. else if( i == 4 && memcmp( sDirective, "LINE", 4 ) == 0 )
  519. return -1;
  520. else if( i == 6 && memcmp( sDirective, "PRAGMA", 6 ) == 0 )
  521. {
  522. hb_pp_strocpy( sParse, sParse + 6 );
  523. bIgnore = hb_pp_ParsePragma( sParse ); /* --- #pragma --- */
  524. }
  525. else
  526. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_WRONG_DIRECTIVE, sDirective, NULL );
  527. }
  528. return bIgnore;
  529. }
  530. int hb_pp_ParseDefine( char *sLine )
  531. {
  532. char defname[MAX_NAME], pars[MAX_NAME + 1];
  533. int i, npars = -1;
  534. DEFINES *lastdef;
  535. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_ParseDefine(%s)", sLine ) );
  536. HB_SKIPTABSPACES( sLine );
  537. if( ISNAME( ( BYTE ) * sLine ) )
  538. {
  539. char *cParams = NULL;
  540. NextName( &sLine, defname );
  541. if( *sLine == '(' ) /* If pseudofunction was found */
  542. {
  543. int iParLen = 0;
  544. int iLen;
  545. sLine++;
  546. HB_SKIPTABSPACES( sLine );
  547. npars = 0;
  548. while( *sLine && *sLine != ')' )
  549. {
  550. if( ISNAME( ( BYTE ) * sLine ) )
  551. {
  552. NextName( &sLine, pars );
  553. iLen = strlen( pars );
  554. if( cParams == NULL )
  555. {
  556. /* 'xy0' -> '~xy0' */
  557. cParams = ( char * ) hb_xgrab( iLen + 2 );
  558. }
  559. else
  560. {
  561. /* '~xy0' -> '~xy,~ab0' */
  562. char *cPos;
  563. cPos = strstr( cParams, pars );
  564. if( cPos && ( cPos[iLen] == ',' || cPos[iLen] == '\0' ) )
  565. {
  566. cPos--;
  567. if( *cPos == '\001' )
  568. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_LABEL_DUPL_IN_DEFINE, defname, pars );
  569. }
  570. cParams = ( char * ) hb_xrealloc( cParams, iParLen + iLen + 3 );
  571. cParams[iParLen++] = ',';
  572. cParams[iParLen] = '\0';
  573. }
  574. cParams[iParLen] = '\001';
  575. strcpy( cParams + iParLen + 1, pars );
  576. iParLen += iLen + 1;
  577. npars++;
  578. HB_SKIPTABSPACES( sLine );
  579. }
  580. else
  581. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_LABEL_MISSING_IN_DEFINE, defname, NULL );
  582. if( *sLine == ',' )
  583. {
  584. sLine++;
  585. HB_SKIPTABSPACES( sLine );
  586. if( *sLine == ')' )
  587. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_LABEL_MISSING_IN_DEFINE, defname, NULL );
  588. }
  589. }
  590. HB_SKIPTABSPACES( sLine );
  591. if( *sLine == '\0' )
  592. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_PARE_MISSING_IN_DEFINE, defname, NULL );
  593. sLine++;
  594. }
  595. HB_SKIPTABSPACES( sLine );
  596. if( cParams )
  597. {
  598. char *tmp = cParams;
  599. char *cPos;
  600. int iPar, iLen, iPos, iOldPos;
  601. iLen = strlen( sLine );
  602. for( i = 0; i < npars; i++ )
  603. {
  604. /*1z,1y */
  605. cPos = strchr( tmp, ',' );
  606. if( cPos )
  607. iPar = cPos - tmp;
  608. else
  609. iPar = strlen( tmp );
  610. memcpy( pars, tmp, iPar );
  611. pars[iPar] = '\0';
  612. iOldPos = 0;
  613. while( ( iPos = md_strAt( pars + 1, iPar - 1, sLine + iOldPos, TRUE, FALSE, FALSE, MD_STR_AT_IGNORECASE ) ) != 0 )
  614. {
  615. if( sLine[iOldPos + iPos] != '\001' )
  616. {
  617. hb_pp_Stuff( pars, sLine + iOldPos + iPos - 1, iPar, iPar - 1, iLen - iPos - iOldPos );
  618. iLen++;
  619. }
  620. iOldPos += iPos + iPar;
  621. }
  622. if( cPos )
  623. tmp = cPos + 1;
  624. }
  625. }
  626. lastdef = hb_pp_AddDefine( defname, ( *sLine == '\0' ) ? NULL : sLine );
  627. lastdef->npars = npars;
  628. lastdef->pars = cParams;
  629. }
  630. else
  631. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_DEFINE_ABSENT, NULL, NULL );
  632. return 0;
  633. }
  634. DEFINES *hb_pp_AddDefine( char *defname, char *value )
  635. {
  636. BOOL isNew;
  637. DEFINES *stdef;
  638. int len = strlen( defname );
  639. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_AddDefine(%s, %s)", defname, value ) );
  640. stdef = DefSearch( defname, len, &isNew );
  641. if( stdef != NULL )
  642. {
  643. hb_compGenWarning( hb_pp_szWarnings, 'I', HB_PP_WARN_DEFINE_REDEF, defname, NULL );
  644. if( isNew )
  645. {
  646. if( stdef->pars )
  647. hb_xfree( stdef->pars );
  648. if( stdef->value )
  649. hb_xfree( stdef->value );
  650. }
  651. }
  652. else
  653. {
  654. stdef = ( DEFINES * ) hb_xgrab( sizeof( DEFINES ) );
  655. stdef->last = hb_pp_topDefine;
  656. hb_pp_topDefine = stdef;
  657. stdef->name = hb_strdup( defname );
  658. stdef->namelen = len;
  659. stdef->npars = -1;
  660. s_kolAddDefs++;
  661. }
  662. stdef->value = ( value == NULL ) ? NULL : hb_strdup( value );
  663. stdef->pars = NULL;
  664. return stdef;
  665. }
  666. static int ParseUndef( char *sLine )
  667. {
  668. char defname[MAX_NAME];
  669. DEFINES *stdef;
  670. BOOL isNew;
  671. int len;
  672. HB_TRACE( HB_TR_DEBUG, ( "ParseUndef(%s)", sLine ) );
  673. NextWord( &sLine, defname, FALSE );
  674. len = strlen( defname );
  675. if( ( stdef = DefSearch( defname, len, &isNew ) ) != NULL )
  676. {
  677. if( isNew )
  678. {
  679. if( stdef->pars )
  680. hb_xfree( stdef->pars );
  681. if( stdef->value )
  682. hb_xfree( stdef->value );
  683. hb_xfree( stdef->name );
  684. }
  685. stdef->pars = NULL;
  686. stdef->value = NULL;
  687. stdef->name = NULL;
  688. stdef->namelen = 0;
  689. }
  690. return 0;
  691. }
  692. static int ParseIfdef( char *sLine, int usl )
  693. {
  694. char defname[MAX_NAME];
  695. DEFINES *stdef;
  696. int len = 0;
  697. HB_TRACE( HB_TR_DEBUG, ( "ParseIfdef(%s, %d)", sLine, usl ) );
  698. if( hb_pp_nCondCompile == 0 || hb_pp_aCondCompile[hb_pp_nCondCompile - 1] )
  699. {
  700. len = NextWord( &sLine, defname, FALSE );
  701. if( *defname == '\0' )
  702. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_DEFINE_ABSENT, NULL, NULL );
  703. }
  704. if( hb_pp_nCondCompile == s_maxCondCompile )
  705. {
  706. s_maxCondCompile += 5;
  707. hb_pp_aCondCompile = ( int * ) hb_xrealloc( hb_pp_aCondCompile, sizeof( int ) * s_maxCondCompile );
  708. }
  709. if( hb_pp_nCondCompile == 0 || hb_pp_aCondCompile[hb_pp_nCondCompile - 1] )
  710. {
  711. if( ( ( stdef = DefSearch( defname, len, NULL ) ) != NULL && usl ) || ( stdef == NULL && !usl ) )
  712. hb_pp_aCondCompile[hb_pp_nCondCompile] = 1;
  713. else
  714. hb_pp_aCondCompile[hb_pp_nCondCompile] = 0;
  715. }
  716. else
  717. hb_pp_aCondCompile[hb_pp_nCondCompile] = 0;
  718. hb_pp_nCondCompile++;
  719. return 0;
  720. }
  721. static DEFINES *DefSearch( char *defname, int len, BOOL * isNew )
  722. {
  723. int kol = 0, j;
  724. DEFINES *stdef = hb_pp_topDefine;
  725. HB_TRACE( HB_TR_DEBUG, ( "DefSearch(%s)", defname ) );
  726. while( stdef != NULL )
  727. {
  728. kol++;
  729. if( stdef->name != NULL && stdef->namelen == len )
  730. {
  731. for( j = 0; *( stdef->name + j ) == *( defname + j ) && *( stdef->name + j ) != '\0'; j++ ) ;
  732. if( *( stdef->name + j ) == *( defname + j ) )
  733. {
  734. if( isNew )
  735. *isNew = ( s_kolAddDefs >= kol );
  736. return stdef;
  737. }
  738. }
  739. stdef = stdef->last;
  740. }
  741. return NULL;
  742. }
  743. static COMMANDS *ComSearch( char *cmdname, COMMANDS * stcmdStart )
  744. {
  745. COMMANDS *stcmd = ( stcmdStart ) ? stcmdStart : hb_pp_topCommand;
  746. HB_TRACE( HB_TR_DEBUG, ( "ComSearch(%s, %p)", cmdname, stcmdStart ) );
  747. while( stcmd != NULL )
  748. {
  749. int j;
  750. for( j = 0; ( *( stcmd->name + j ) == toupper( *( cmdname + j ) ) ) &&
  751. ( *( stcmd->name + j ) != '\0' ) && ( ( stcmd->com_or_xcom ) ? 1 : ( j < 4 || ISNAME( ( BYTE ) * ( cmdname + j + 1 ) ) ) ); j++ ) ;
  752. if( ( *( stcmd->name + j ) == toupper( *( cmdname + j ) ) )
  753. || ( !stcmd->com_or_xcom && j >= 4 && *( stcmd->name + j ) != '\0' && *( cmdname + j ) == '\0' ) )
  754. break;
  755. stcmd = stcmd->last;
  756. }
  757. return stcmd;
  758. }
  759. static COMMANDS *TraSearch( char *cmdname, COMMANDS * sttraStart )
  760. {
  761. int j;
  762. COMMANDS *sttra = ( sttraStart ) ? sttraStart : hb_pp_topTranslate;
  763. HB_TRACE( HB_TR_DEBUG, ( "TraSearch(%s, %p)", cmdname, sttraStart ) );
  764. while( sttra != NULL )
  765. {
  766. for( j = 0; *( sttra->name + j ) == toupper( *( cmdname + j ) ) &&
  767. *( sttra->name + j ) != '\0' && ( ( sttra->com_or_xcom ) ? 1 : ( j < 4 || ISNAME( ( BYTE ) * ( cmdname + j + 1 ) ) ) ); j++ ) ;
  768. if( *( sttra->name + j ) == toupper( *( cmdname + j ) )
  769. || ( !sttra->com_or_xcom && j >= 4 && *( sttra->name + j ) != '\0' && *( cmdname + j ) == '\0' ) )
  770. break;
  771. sttra = sttra->last;
  772. }
  773. return sttra;
  774. }
  775. static void ParseCommand( char *sLine, BOOL com_or_xcom, BOOL com_or_tra )
  776. {
  777. #if !defined(HB_PP_DEBUG_MEMORY)
  778. static char mpatt[PATTERN_SIZE];
  779. #else
  780. char *mpatt = ( char * ) hb_xgrab( PATTERN_SIZE );
  781. #endif
  782. char *rpatt;
  783. char cmdname[MAX_NAME];
  784. COMMANDS *stcmd;
  785. int mlen, rlen;
  786. int ipos;
  787. /* Ron Pinkas added 2000-12-03 */
  788. BOOL bOk = FALSE;
  789. HB_TRACE( HB_TR_DEBUG, ( "ParseCommand(%s, %d, %d)", sLine, com_or_xcom, com_or_tra ) );
  790. HB_SKIPTABSPACES( sLine );
  791. ipos = 0;
  792. /* JFL 2000-09-19 */
  793. /* This was the original line as Alexander wrote it */
  794. /* while( *sLine != '\0' && *sLine != ' ' && *sLine != '\t' && *sLine != '<' && *sLine != '=' && ( *sLine != '(' || ipos == 0 ) ) */
  795. /* Now the line #xtranslate = name(.. => will be allowed */
  796. /* I changed it to the following to allow < and = to be the first char within a translate or xtranslate */
  797. while( *sLine != '\0' && *sLine != ' ' && *sLine != '\t'
  798. && ( *sLine != '<' || ipos == 0 ) && ( *sLine != '=' || ipos == 0 ) && ( *sLine != '(' || ipos == 0 ) )
  799. {
  800. /* Ron Pinkas added 2000-01-24 */
  801. if( !ISNAME( ( BYTE ) * sLine ) )
  802. {
  803. if( *sLine == '[' && ipos )
  804. break;
  805. if( IS_2CHAR_OPERATOR( sLine ) )
  806. {
  807. *( cmdname + ipos++ ) = *sLine++;
  808. *( cmdname + ipos++ ) = *sLine++;
  809. break;
  810. }
  811. else
  812. {
  813. *( cmdname + ipos++ ) = *sLine++;
  814. break;
  815. }
  816. }
  817. /* END, Ron Pinkas added 2000-01-24 */
  818. *( cmdname + ipos++ ) = *sLine++;
  819. }
  820. *( cmdname + ipos ) = '\0';
  821. if( !ipos )
  822. {
  823. #if defined(HB_PP_DEBUG_MEMORY)
  824. hb_xfree( ( void * ) mpatt );
  825. #endif
  826. return;
  827. }
  828. hb_strupr( cmdname );
  829. HB_SKIPTABSPACES( sLine );
  830. /* Ron Pinkas added 2000-12-03 */
  831. ipos = 0;
  832. while( *sLine )
  833. {
  834. mpatt[ipos++] = *sLine;
  835. if( *sLine == '=' )
  836. {
  837. int i = ipos;
  838. sLine++;
  839. mpatt[i++] = *sLine;
  840. while( *sLine && ( *sLine == ' ' || *sLine == '\t' ) )
  841. {
  842. sLine++;
  843. mpatt[i++] = *sLine;
  844. }
  845. if( *sLine == '>' )
  846. {
  847. ipos = ipos - 2;
  848. while( mpatt[ipos] == ' ' || mpatt[ipos] == '\t' )
  849. {
  850. ipos--;
  851. }
  852. mpatt[ipos + 1] = '\0';
  853. sLine++;
  854. bOk = TRUE;
  855. break;
  856. }
  857. ipos = i;
  858. }
  859. sLine++;
  860. }
  861. /* End - Ron Pinkas added 2000-12-03 */
  862. /* Ron Pinkas modified 2000-12-03
  863. if( (ipos = hb_strAt( "=>", 2, sLine, strlen(sLine) )) > 0 ) */
  864. if( bOk )
  865. {
  866. /* Ron Pinkas removed 2000-12-03
  867. stroncpy( mpatt, sLine, ipos-1 ); */
  868. mlen = strotrim( mpatt, TRUE );
  869. /* Ron Pinkas removed 2000-12-03
  870. sLine += ipos + 1; */
  871. HB_SKIPTABSPACES( sLine );
  872. /* hb_pp_strocpy( rpatt, sLine ); */
  873. rpatt = sLine;
  874. rlen = strotrim( rpatt, TRUE );
  875. ConvertPatterns( mpatt, mlen, rpatt, rlen );
  876. RemoveSlash( mpatt );
  877. rlen = RemoveSlash( rpatt );
  878. if( com_or_tra )
  879. stcmd = AddCommand( cmdname );
  880. else
  881. stcmd = AddTranslate( cmdname );
  882. stcmd->com_or_xcom = com_or_xcom;
  883. stcmd->mpatt = hb_strdup( mpatt );
  884. stcmd->value = ( rlen > 0 ) ? hb_strdup( rpatt ) : NULL;
  885. }
  886. else
  887. {
  888. sLine -= ( ipos + 1 );
  889. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_COMMAND_DEFINITION, cmdname, sLine );
  890. }
  891. #if defined(HB_PP_DEBUG_MEMORY)
  892. hb_xfree( ( void * ) mpatt );
  893. #endif
  894. }
  895. /* Remove escape characters and check '[' optional markers
  896. */
  897. static int ConvertOptional( char *cpatt, int len, BOOL bLeft )
  898. {
  899. int i = 0;
  900. while( cpatt[i] != '\0' )
  901. {
  902. if( cpatt[i] == '"' || cpatt[i] == '\'' )
  903. {
  904. char c = cpatt[i];
  905. i++;
  906. while( cpatt[i] && cpatt[i] != c )
  907. {
  908. i++;
  909. }
  910. i++;
  911. continue; /* skip "strings" */
  912. }
  913. if( cpatt[i] == '[' )
  914. {
  915. if( i && cpatt[i - 1] == '\\' )
  916. {
  917. hb_pp_Stuff( "", cpatt + i - 1, 0, 1, len - i + 1 );
  918. len--;
  919. continue;
  920. }
  921. else
  922. {
  923. int j = i + 1;
  924. int iOpenBrackets = 1;
  925. BOOL bOption = FALSE;
  926. while( cpatt[j] && iOpenBrackets )
  927. {
  928. if( cpatt[j] == '[' && cpatt[j - 1] != '\\' )
  929. iOpenBrackets++;
  930. else if( cpatt[j] == ']' && cpatt[j - 1] != '\\' )
  931. {
  932. if( --iOpenBrackets == 0 && ( bOption || bLeft ) )
  933. {
  934. cpatt[i] = HB_PP_OPT_START;
  935. cpatt[j] = HB_PP_OPT_END;
  936. }
  937. }
  938. else if( cpatt[j] == '<' )
  939. {
  940. j++;
  941. while( cpatt[j] == ' ' || cpatt[j] == '\t' )
  942. j++;
  943. if( strchr( "*(!-{.\"", cpatt[j] ) || ISNAME( cpatt[j] ) )
  944. {
  945. bOption = TRUE;
  946. continue;
  947. }
  948. }
  949. else if( cpatt[j] == '"' || cpatt[j] == '\'' )
  950. {
  951. char c = cpatt[j];
  952. j++;
  953. while( cpatt[j] && cpatt[j] != c )
  954. {
  955. j++;
  956. }
  957. }
  958. j++;
  959. }
  960. if( iOpenBrackets )
  961. {
  962. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_PATTERN_DEFINITION, cpatt + i, NULL );
  963. }
  964. }
  965. }
  966. else if( cpatt[i] == ']' )
  967. {
  968. if( i && cpatt[i - 1] == '\\' )
  969. {
  970. hb_pp_Stuff( "", cpatt + i - 1, 0, 1, len - i + 1 );
  971. len--;
  972. continue;
  973. }
  974. }
  975. i++;
  976. }
  977. return len;
  978. }
  979. static void RemoveOptional( char *cpatt )
  980. {
  981. int i = 0;
  982. int len = strlen( cpatt );
  983. int iOpenBra = 0;
  984. while( cpatt[i] != '\0' )
  985. {
  986. if( cpatt[i] == '"' || cpatt[i] == '\'' )
  987. {
  988. char c = cpatt[i++];
  989. while( cpatt[i] && cpatt[i] != c )
  990. {
  991. i++;
  992. }
  993. if( cpatt[i] )
  994. i++;
  995. continue; /* skip "strings" */
  996. }
  997. if( cpatt[i] == '[' )
  998. {
  999. i++;
  1000. iOpenBra++;
  1001. while( cpatt[i] && iOpenBra )
  1002. {
  1003. if( cpatt[i] == '[' )
  1004. iOpenBra++;
  1005. else if( cpatt[i] == ']' )
  1006. iOpenBra--;
  1007. i++;
  1008. }
  1009. continue; /* skip [strings] */
  1010. }
  1011. if( cpatt[i] == HB_PP_OPT_START || cpatt[i] == HB_PP_OPT_END )
  1012. {
  1013. hb_pp_Stuff( "", cpatt + i, 0, 1, len - i + 1 );
  1014. len--;
  1015. }
  1016. else
  1017. i++;
  1018. }
  1019. }
  1020. /* ConvertPatterns()
  1021. * Converts result pattern in #command and #translate to inner format
  1022. */
  1023. static void ConvertPatterns( char *mpatt, int mlen, char *rpatt, int rlen )
  1024. {
  1025. int i = 0, ipos, ifou;
  1026. int explen, rmlen;
  1027. char exppatt[MAX_NAME], expreal[5] = " 0";
  1028. char lastchar = '@', exptype;
  1029. char *ptr, *ptrtmp;
  1030. HB_TRACE( HB_TR_DEBUG, ( "ConvertPatterns(%s, %d, %s, %d)", mpatt, mlen, rpatt, rlen ) );
  1031. expreal[0] = HB_PP_MATCH_MARK;
  1032. mlen = ConvertOptional( mpatt, mlen, TRUE ); /* left pattern */
  1033. rlen = ConvertOptional( rpatt, rlen, FALSE ); /* right pattern */
  1034. while( *( mpatt + i ) != '\0' )
  1035. {
  1036. if( mpatt[i] == '"' || mpatt[i] == '\'' )
  1037. {
  1038. char c = mpatt[i];
  1039. i++;
  1040. while( mpatt[i] && mpatt[i] != c )
  1041. {
  1042. i++;
  1043. }
  1044. i++;
  1045. continue; /* skip "strings" */
  1046. }
  1047. if( *( mpatt + i ) == '<' )
  1048. {
  1049. if( i && mpatt[ i-1 ] == '\\' )
  1050. {
  1051. i++;
  1052. continue;
  1053. }
  1054. /* Drag match marker, determine it type */
  1055. explen = 0;
  1056. ipos = i;
  1057. i++;
  1058. exptype = '0';
  1059. while( *( mpatt + i ) == ' ' || *( mpatt + i ) == '\t' )
  1060. i++;
  1061. if( *( mpatt + i ) == '*' ) /* Wild match marker */
  1062. {
  1063. exptype = '3';
  1064. i++;
  1065. }
  1066. else if( *( mpatt + i ) == '(' ) /* Extended expression match marker */
  1067. {
  1068. exptype = '4';
  1069. i++;
  1070. }
  1071. else if( *( mpatt + i ) == '!' ) /* Minimal expression match marker */
  1072. {
  1073. exptype = '5';
  1074. i++;
  1075. }
  1076. ptr = mpatt + i;
  1077. while( *ptr != '>' )
  1078. {
  1079. if( *ptr == '\0' || *ptr == '<' || *ptr == '[' || *ptr == ']' )
  1080. {
  1081. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_PATTERN_DEFINITION, NULL, NULL );
  1082. return;
  1083. }
  1084. ptr++;
  1085. }
  1086. while( *( mpatt + i ) != '>' )
  1087. {
  1088. if( *( mpatt + i ) == ',' ) /* List match marker */
  1089. {
  1090. exptype = '1';
  1091. while( *( mpatt + i ) != '>' )
  1092. i++;
  1093. break;
  1094. }
  1095. else if( *( mpatt + i ) == ':' ) /* Restricted match marker */
  1096. {
  1097. exptype = '2';
  1098. *( mpatt + i-- ) = ' ';
  1099. break;
  1100. }
  1101. if( *( mpatt + i ) != ' ' && *( mpatt + i ) != '\t' )
  1102. *( exppatt + explen++ ) = *( mpatt + i );
  1103. i++;
  1104. }
  1105. if( exptype == '3' )
  1106. {
  1107. if( *( exppatt + explen - 1 ) == '*' )
  1108. explen--;
  1109. else
  1110. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_PATTERN_DEFINITION, NULL, NULL );
  1111. }
  1112. else if( exptype == '4' )
  1113. {
  1114. if( *( exppatt + explen - 1 ) == ')' )
  1115. explen--;
  1116. else
  1117. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_PATTERN_DEFINITION, NULL, NULL );
  1118. }
  1119. else if( exptype == '5' )
  1120. {
  1121. if( *( exppatt + explen - 1 ) == '!' )
  1122. explen--;
  1123. else
  1124. hb_compGenError( hb_pp_szErrors, 'F', HB_PP_ERR_PATTERN_DEFINITION, NULL, NULL );
  1125. }
  1126. rmlen = i - ipos + 1;
  1127. /* Convert match marker into inner format */
  1128. lastchar = ( lastchar != 'Z' ) ? ( ( char ) ( ( unsigned int ) lastchar + 1 ) ) : 'a';
  1129. expreal[1] = lastchar;
  1130. expreal[2] = exptype;
  1131. hb_pp_Stuff( expreal, mpatt + ipos, 4, rmlen, mlen - ipos );
  1132. mlen += 4 - rmlen;
  1133. i += 4 - rmlen;
  1134. /* Look for appropriate result markers */
  1135. ptr = rpatt;
  1136. while( ( ifou = hb_strAt( exppatt, explen, ptr, rlen - ( ptr - rpatt ) ) ) > 0 )
  1137. {
  1138. /* Convert result marker into inner format */
  1139. ifou--;
  1140. ptr += ifou;
  1141. ptrtmp = ptr + 1;
  1142. rmlen = explen;
  1143. exptype = '0'; /* regular result marker */
  1144. do
  1145. {
  1146. ptr--;
  1147. rmlen++;
  1148. ifou--;
  1149. if( *ptr == '<' )
  1150. continue;
  1151. else if( *ptr == '\"' )
  1152. exptype = '2'; /* normal stringify result marker */
  1153. else if( *ptr == '(' )
  1154. exptype = '3'; /* Smart stringify result marker */
  1155. else if( *ptr == '{' )
  1156. exptype = '4'; /* Blockify result marker */
  1157. else if( *ptr == '.' )
  1158. exptype = '5'; /* Logify result marker */
  1159. else if( *ptr == '-' )
  1160. exptype = '6'; /* ommit (remove) result marker */
  1161. else if( *ptr == ' ' || *ptr == '\t' )
  1162. continue;
  1163. else
  1164. ifou = -1;
  1165. }
  1166. while( ifou >= 0 && *ptr != '<' && *( ptr - 1 ) != '\\' );
  1167. if( ifou >= 0 && *ptr == '<' )
  1168. {
  1169. ptr += rmlen++;
  1170. while( *ptr != '\0' && *ptr != '>' && *( ptr - 1 ) != '\\' )
  1171. {
  1172. if( *ptr != ' ' && *ptr != '\t' && *ptr != '\"' && *ptr != ')' && *ptr != '}' && *ptr != '.' && *ptr != '-' )
  1173. {
  1174. ifou = -1;
  1175. break;
  1176. }
  1177. rmlen++;
  1178. ptr++;
  1179. }
  1180. if( ifou >= 0 && *ptr == '>' )
  1181. {
  1182. ptr -= rmlen;
  1183. ptr++;
  1184. if( exptype == '0' && *( ptr - 1 ) == '#' && *( ptr - 2 ) != '\\' )
  1185. {
  1186. exptype = '1'; /* dumb stringify result marker */
  1187. ptr--;
  1188. rmlen++;
  1189. }
  1190. expreal[2] = exptype;
  1191. hb_pp_Stuff( expreal, ptr, 4, rmlen, rlen + ( rpatt - ptr ) );
  1192. rlen += 4 - rmlen;
  1193. }
  1194. else
  1195. ptr = ptrtmp;
  1196. }
  1197. else
  1198. ptr = ptrtmp;
  1199. }
  1200. }
  1201. i++;
  1202. }
  1203. }
  1204. static COMMANDS *AddCommand( char *cmdname )
  1205. {
  1206. COMMANDS *stcmd;
  1207. HB_TRACE( HB_TR_DEBUG, ( "AddCommand(%s)", cmdname ) );
  1208. stcmd = ( COMMANDS * ) hb_xgrab( sizeof( COMMANDS ) );
  1209. stcmd->last = hb_pp_topCommand;
  1210. hb_pp_topCommand = stcmd;
  1211. stcmd->name = hb_strdup( cmdname );
  1212. stcmd->namelen = strlen( cmdname );
  1213. s_kolAddComs++;
  1214. return stcmd;
  1215. }
  1216. static COMMANDS *AddTranslate( char *traname )
  1217. {
  1218. COMMANDS *sttra;
  1219. HB_TRACE( HB_TR_DEBUG, ( "AddTranslate(%s)", traname ) );
  1220. sttra = ( COMMANDS * ) hb_xgrab( sizeof( COMMANDS ) );
  1221. sttra->last = hb_pp_topTranslate;
  1222. hb_pp_topTranslate = sttra;
  1223. sttra->name = hb_strdup( traname );
  1224. sttra->namelen = strlen( traname );
  1225. s_kolAddTras++;
  1226. return sttra;
  1227. }
  1228. int hb_pp_ParseExpression( char *sLine, char *sOutLine )
  1229. {
  1230. #if !defined(HB_PP_DEBUG_MEMORY)
  1231. static char rpatt[PATTERN_SIZE];
  1232. #else
  1233. char *rpatt = ( char * ) hb_xgrab( PATTERN_SIZE );
  1234. #endif
  1235. char sToken[MAX_NAME];
  1236. char *ptri, *ptro, *ptrb;
  1237. int lenToken, i, ipos, isdvig, lens;
  1238. int ifou;
  1239. int rezDef, rezTra, rezCom;
  1240. unsigned int kolpass = 0;
  1241. DEFINES *stdef;
  1242. COMMANDS *stcmd;
  1243. HB_TRACE( HB_TR_DEBUG, ( "hb_pp_ParseExpression(%s, %s)", sLine, sOutLine ) );
  1244. do
  1245. {
  1246. strotrim( sLine, FALSE );
  1247. rezDef = 0;
  1248. rezTra = 0;
  1249. rezCom = 0;
  1250. isdvig = 0;
  1251. do
  1252. {
  1253. ptro = sOutLine;
  1254. ptri = sLine + isdvig;
  1255. ipos = md_strAt( ";", 1, ptri, TRUE, FALSE, FALSE, MD_STR_AT_IGNORECASE );
  1256. if( ipos > 0 )
  1257. {
  1258. *( ptri + ipos - 1 ) = '\0';
  1259. }
  1260. HB_SKIPTABSPACES( ptri );
  1261. if( *ptri == '#' )
  1262. {
  1263. int bIgnore;
  1264. strcpy( rpatt, ptri );
  1265. bIgnore = hb_pp_ParseDirective( rpatt );
  1266. if( ipos > 0 )
  1267. {
  1268. ipos--;
  1269. *( sLine + isdvig + ipos - 1 ) = ';';
  1270. *( sLine + isdvig + ipos ) = ' ';
  1271. }
  1272. lens = strlen( sLine + isdvig );
  1273. if( bIgnore )
  1274. hb_pp_Stuff( " ", sLine + isdvig, 0, ( ipos ) ? ipos : lens, lens );
  1275. else
  1276. hb_pp_Stuff( rpatt, sLine + isdvig, strlen( rpatt ), ( ipos ) ? ipos : lens, lens );
  1277. if( ipos > 0 )
  1278. {
  1279. ipos = 1;
  1280. }
  1281. }
  1282. else
  1283. { /* Look for macros from #define */
  1284. while( ( lenToken = NextName( &ptri, sToken ) ) > 0 )
  1285. {
  1286. #if 0
  1287. printf( "Token: >%s< Line: >%s<\n", sToken, sLine );
  1288. #endif
  1289. if( ( stdef = DefSearch( sToken, lenToken, NULL ) ) != NULL )
  1290. {
  1291. ptrb = ptri - lenToken;
  1292. if( ( i = WorkDefine( &ptri, ptro, stdef ) ) >= 0 )
  1293. {
  1294. rezDef++;
  1295. lens = strlen( ptrb );
  1296. if( ipos > 0 )
  1297. {
  1298. *( ptrb + lens ) = ';';
  1299. lens += strlen( ptrb + lens + 1 );
  1300. }
  1301. hb_pp_Stuff( ptro, ptrb, i, ptri - ptrb, lens + 1 );
  1302. if( ipos > 0 )
  1303. {
  1304. ipos += i - ( ptri - ptrb );
  1305. *( sLine + isdvig + ipos - 1 ) = '\0';
  1306. }
  1307. ptri += i - ( ptri - ptrb );
  1308. }
  1309. }
  1310. }
  1311. if( rezDef == 0 )
  1312. {
  1313. /* Look for definitions from #translate */
  1314. stcmd = hb_pp_topTranslate;
  1315. while( stcmd != NULL )
  1316. {
  1317. ptri = sLine + isdvig;
  1318. lenToken = stcmd->namelen;
  1319. while( ( ifou = md_strAt( stcmd->name, lenToken, ptri, TRUE, FALSE, FALSE, MD_STR_AT_USESUBCASE ) ) > 0 )
  1320. {
  1321. ptri += ifou - 1;
  1322. if( ( i = WorkTranslate( ptri + lenToken, ptro, stcmd, &lens ) ) >= 0 )
  1323. {
  1324. lens += lenToken;
  1325. while( lens > 0 && ( *( ptri + lens - 1 ) == ' ' || *( ptri + lens - 1 ) == '\t' ) )
  1326. {
  1327. lens--;
  1328. }
  1329. if( ipos > 0 )
  1330. {
  1331. *( sLine + isdvig + ipos - 1 ) = ';';
  1332. }
  1333. hb_pp_Stuff( ptro, ptri, i, lens, strlen( ptri ) );
  1334. rezTra = 1;
  1335. if( ipos > 0 )
  1336. {
  1337. ipos += i - lens;
  1338. *( sLine + isdvig + ipos - 1 ) = '\0';
  1339. }
  1340. ptri += i;
  1341. }
  1342. else
  1343. {
  1344. ptri += lenToken;
  1345. }
  1346. }
  1347. stcmd = stcmd->last;
  1348. }
  1349. } /* rezDef == 0 */
  1350. /* Look for definitions from #command */
  1351. /* JFL ! Was 3 but insufficient in most cases */
  1352. /* I know this is a new hardcoded limit ... any better idea's welcome */
  1353. if( rezDef == 0 && rezTra == 0 && kolpass < 20 )
  1354. {
  1355. ptri = sLine + isdvig;
  1356. HB_SKIPTABSPACES( ptri );
  1357. if( ISNAME( ( BYTE ) * ptri ) )
  1358. {
  1359. NextName( &ptri, sToken );
  1360. }
  1361. else
  1362. {
  1363. /* Ron Pinkas commented 2000-01-24
  1364. i = 0;
  1365. while( *ptri != ' ' && *ptri != '\t' && *ptri != '\0' && *ptri != '\"' && *ptri != '\'' && *ptri != '(' && !ISNAME( ( BYTE ) *ptri ) )
  1366. {
  1367. *(sToken+i) = *ptri++;
  1368. i++;
  1369. }
  1370. *(sToken+i) = '\0';
  1371. */
  1372. /* Ron Pinkas added 2000-01-24 */
  1373. if( IS_2CHAR_OPERATOR( ptri ) )
  1374. {
  1375. sToken[0] = *ptri++;
  1376. sToken[1] = *ptri++;
  1377. sToken[2] = '\0';
  1378. }
  1379. else
  1380. {
  1381. sToken[0] = *ptri++;
  1382. sToken[1] = '\0';
  1383. }
  1384. /* END, Ron Pinkas added 2000-01-24 */
  1385. }
  1386. HB_SKIPTABSPACES( ptri );
  1387. if( ( *ptri == '\0'
  1388. || ( *ptri != '='
  1389. && ( !IsInStr( *ptri, ":/+*-%^" )
  1390. || *( ptri + 1 ) != '=' ) && ( *ptri != '-'
  1391. || *( ptri + 1 ) != '>' ) ) ) && ( stcmd = ComSearch( sToken, NULL ) ) != NULL )
  1392. {
  1393. ptro = sOutLine;
  1394. i = WorkCommand( ptri, ptro, stcmd );
  1395. ptri = sLine + isdvig;
  1396. if( ipos > 0 )
  1397. {
  1398. *( ptri + ipos - 1 ) = ';';
  1399. }
  1400. if( i >= 0 )
  1401. {
  1402. if( isdvig + ipos > 0 )
  1403. {
  1404. lens = strlen( sLine + isdvig );
  1405. hb_pp_Stuff( ptro, sLine + isdvig, i, ( ipos ) ? ipos - 1 : lens, lens );
  1406. if( ipos > 0 )
  1407. {
  1408. ipos = i + 1;
  1409. }
  1410. }
  1411. else
  1412. {
  1413. memcpy( sLine, sOutLine, i + 1 );
  1414. }
  1415. }
  1416. rezCom = 1;
  1417. }
  1418. else if( ipos > 0 )
  1419. {
  1420. *( sLine + isdvig + ipos - 1 ) = ';';
  1421. }
  1422. }
  1423. else if( ipos > 0 )
  1424. {
  1425. *( sLine + isdvig + ipos - 1 ) = ';';
  1426. }
  1427. }
  1428. isdvig += ipos;
  1429. }
  1430. while( ipos != 0 );
  1431. kolpass++;
  1432. if( kolpass > hb_pp_MaxTranslateCycles

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