PageRenderTime 82ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release_old_item/harbour/source/hbpp/hbpp.c

#
C | 1458 lines | 1324 code | 114 blank | 20 comment | 725 complexity | fbfc9d46ccc6a79e6ced79679d4690ba 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
  1. /*
  2. * $Id: hbpp.c 575 1999-06-27 03:03:30Z ajahja $
  3. */
  4. /* Harbour Preprocessor , version 0.9
  5. author - Alexander Kresin */
  6. #if defined(__GNUC__)
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #else
  11. #if (defined(_MSC_VER) || defined(__IBMCPP__))
  12. #include <memory.h>
  13. #else
  14. #include <mem.h>
  15. #endif
  16. #endif
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include "harb.h"
  20. int Hp_Parse( FILE*, FILE* );
  21. int ParseDirective( char* );
  22. int ParseDefine( char* );
  23. DEFINES* AddDefine ( char*, char* );
  24. int ParseUndef( char* );
  25. int ParseIfdef( char*, int);
  26. int ParseCommand( char*, int, int );
  27. int ConvertPatterns ( char*, int, char*, int );
  28. void AddCommand ( char * );
  29. void AddTranslate ( char * );
  30. COMMANDS* getCommand ( int );
  31. int ParseExpression( char*, char* );
  32. int WorkDefine ( char**, char**, DEFINES *, int );
  33. void WorkPseudoF ( char**, char**, DEFINES*);
  34. int WorkCommand ( char*, char*, char*, int);
  35. int CommandStuff ( char *, char *, char *, int*, int );
  36. int WorkTranslate ( char*, char**, char*, int);
  37. int WorkMarkers( char**, char**, char*, int*, int );
  38. int getExpReal ( char *, char **, int );
  39. int isExpres ( char* );
  40. void SkipOptional( char**, char*, int*);
  41. DEFINES* DefSearch(char *);
  42. int ComSearch(char *,int);
  43. int TraSearch(char *,int);
  44. void SearnRep( char*,char*,int,char*,int*);
  45. int ReplacePattern ( char, char*, int, char*, int );
  46. int RdStr(FILE*,char *,int,int,char*,int*,int*);
  47. int WrStr(FILE*,char *);
  48. int hb_strAt(char *, int, char*, int);
  49. int md_strAt(char *, int, char*);
  50. int IsInStr ( char, char*);
  51. void Stuff (char*, char*, int, int, int);
  52. int strocpy (char*, char* );
  53. int stroncpy (char*, char*, int);
  54. int strincpy (char*, char*);
  55. int strincmp (char*, char**);
  56. int strolen ( char* );
  57. int strotrim ( char* );
  58. char* strodup ( char * );
  59. int NextWord ( char**, char*, int);
  60. int NextName ( char**, char*, char**);
  61. int Include( char *, PATHNAMES *, FILE** );
  62. int OpenInclude( char *, PATHNAMES *, FILE** );
  63. #define isname(c) (isalnum(c) || c=='_' || (c) > 0x7e)
  64. #define SKIPTABSPACES(sptr) while ( *sptr == ' ' || *sptr == '\t' ) (sptr)++
  65. #define MAX_NAME 255
  66. #define BUFF_SIZE 2048
  67. #define STR_SIZE 2048
  68. #define FALSE 0
  69. #define TRUE 1
  70. #define STATE_INIT 0
  71. #define STATE_NORMAL 1
  72. #define STATE_COMMENT 2
  73. #define STATE_QUOTE1 3
  74. #define STATE_QUOTE2 4
  75. #define STATE_ID_END 5
  76. #define STATE_ID 6
  77. #define STATE_EXPRES 7
  78. #define STATE_EXPRES_ID 8
  79. #define STATE_BRACKET 9
  80. #define IT_EXPR 1
  81. #define IT_ID 2
  82. #define IT_COMMA 3
  83. #define IT_ID_OR_EXPR 4
  84. int ParseState = 0;
  85. int lInclude = 0;
  86. int *aCondCompile, nCondCompile = 0, maxCondCompile = 5;
  87. int nline=0;
  88. int Repeate;
  89. char groupchar;
  90. extern PATHNAMES *_pIncludePath;
  91. extern DEFINES aDefines[] ;
  92. extern int koldef;
  93. DEFINES *aDefnew ;
  94. int koldefines = 0, maxdefines = 50;
  95. #define INITIAL_ACOM_SIZE 200
  96. extern COMMANDS aCommands[] ;
  97. extern int kolcomm;
  98. COMMANDS *aCommnew ;
  99. int kolcommands = 0, maxcommands = INITIAL_ACOM_SIZE;
  100. TRANSLATES *aTranslates ;
  101. int koltranslates = 0, maxtranslates = 50;
  102. int ParseDirective( char* sLine )
  103. {
  104. char sDirective[MAX_NAME];
  105. int i;
  106. FILE* handl_i;
  107. i = NextWord( &sLine, sDirective, TRUE );
  108. SKIPTABSPACES(sLine);
  109. if ( i == 4 && memcmp ( sDirective, "else", 4 ) == 0 )
  110. { /* --- #else --- */
  111. if ( nCondCompile == 0 ) return 3001;
  112. else aCondCompile[nCondCompile-1] = 1 - aCondCompile[nCondCompile-1];
  113. }
  114. else if ( i == 5 && memcmp ( sDirective, "endif", 5 ) == 0 )
  115. { /* --- #endif --- */
  116. if ( nCondCompile == 0 ) return 3001; else nCondCompile--;
  117. }
  118. else if ( nCondCompile==0 || aCondCompile[nCondCompile-1])
  119. {
  120. if ( i == 7 && memcmp ( sDirective, "include", 7 ) == 0 )
  121. { /* --- #include --- */
  122. if ( *sLine != '\"' ) return 1000;
  123. sLine++; i = 0;
  124. while ( *(sLine+i) != '\0' && *(sLine+i) != '\"' ) i++;
  125. if ( *(sLine+i) != '\"' ) return 1000;
  126. *(sLine+i) = '\0';
  127. /* if ((handl_i = fopen(sLine, "r")) == NULL) */
  128. if ( !OpenInclude( sLine, _pIncludePath, &handl_i ) )
  129. { printf("\nCan't open %s\n",sLine); return 1001; }
  130. lInclude++;
  131. Hp_Parse(handl_i, 0 );
  132. lInclude--;
  133. fclose(handl_i);
  134. }
  135. else if ( i == 6 && memcmp ( sDirective, "define", 6 ) == 0 )
  136. ParseDefine ( sLine ); /* --- #define --- */
  137. else if ( i == 5 && memcmp ( sDirective, "undef", 5 ) == 0 )
  138. ParseUndef ( sLine ); /* --- #undef --- */
  139. else if ( i == 5 && memcmp ( sDirective, "ifdef", 5 ) == 0 )
  140. ParseIfdef ( sLine, TRUE ); /* --- #ifdef --- */
  141. else if ( i == 6 && memcmp ( sDirective, "ifndef", 6 ) == 0 )
  142. ParseIfdef ( sLine, FALSE ); /* --- #ifndef --- */
  143. else if ( (i == 7 && memcmp ( sDirective, "command", 7 ) == 0) ||
  144. (i == 8 && memcmp ( sDirective, "xcommand", 8 ) == 0) )
  145. /* --- #command --- */
  146. ParseCommand ( sLine, (i==7)? FALSE:TRUE, TRUE );
  147. else if ( (i == 9 && memcmp ( sDirective, "translate", 9 ) == 0) ||
  148. (i == 10 && memcmp ( sDirective, "xtranslate", 10 ) == 0) )
  149. /* --- #translate --- */
  150. ParseCommand ( sLine, (i==9)? FALSE:TRUE, FALSE );
  151. else if ( i == 6 && memcmp ( sDirective, "stdout", 6 ) == 0 )
  152. printf ( "%s", sLine ); /* --- #stdout --- */
  153. else if ( i == 5 && memcmp ( sDirective, "error", 5 ) == 0 )
  154. { /* --- #error --- */
  155. printf ( "\n#error: %s\n", sLine );
  156. return 2000;
  157. }
  158. else return 1;
  159. }
  160. return 0;
  161. }
  162. int ParseDefine( char* sLine)
  163. {
  164. char defname[MAX_NAME], pars[MAX_NAME];
  165. int i = 0, npars = 0;
  166. DEFINES *lastdef;
  167. /* Drag identifier */
  168. while ( *sLine != '\0' && *sLine != ' ')
  169. {
  170. if ( *sLine == '(' ) break; /* If pseudofunction */
  171. *(defname+i++) = *sLine++;
  172. }
  173. *(defname+i) = '\0';
  174. if ( *sLine == '(' ) /* If pseudofunction was found */
  175. {
  176. sLine++; i = 0;
  177. while ( *sLine != '\0' && *sLine != ')')
  178. {
  179. if ( *sLine == ',' ) npars++;
  180. if ( *sLine != ' ' && *sLine != '\t' ) *(pars+i++) = *sLine;
  181. sLine++;
  182. }
  183. if ( i > 0 ) npars++;
  184. *(pars+i) = '\0';
  185. sLine++;
  186. }
  187. SKIPTABSPACES(sLine);
  188. lastdef = AddDefine ( defname, ( *sLine == '\0' )? NULL : sLine );
  189. lastdef->npars = npars;
  190. lastdef->pars = ( npars == 0 )? NULL : strodup ( pars );
  191. return 0;
  192. }
  193. DEFINES* AddDefine ( char* defname, char* value )
  194. {
  195. DEFINES* stdef = DefSearch( defname );
  196. if ( stdef != NULL )
  197. {
  198. #if 0
  199. if ( stdef->pars != NULL ) _xfree ( stdef->pars );
  200. _xfree ( stdef->value );
  201. #endif
  202. stdef->pars = NULL;
  203. }
  204. else
  205. {
  206. if ( koldefines == maxdefines ) /* Add new entry to defines table */
  207. {
  208. maxdefines += 50;
  209. aDefnew = (DEFINES *)_xrealloc( aDefnew, sizeof( DEFINES ) * maxdefines );
  210. }
  211. stdef = &aDefnew[koldefines++];
  212. stdef->name = strodup ( defname );
  213. }
  214. stdef->value = ( value == NULL )? NULL : strodup ( value );
  215. return stdef;
  216. }
  217. int ParseUndef( char* sLine)
  218. {
  219. char defname[MAX_NAME];
  220. DEFINES* stdef;
  221. NextWord( &sLine, defname, FALSE );
  222. if ( ( stdef = DefSearch(defname) ) != NULL )
  223. {
  224. stdef->name = NULL;
  225. }
  226. return 0;
  227. }
  228. int ParseIfdef( char* sLine, int usl)
  229. {
  230. char defname[MAX_NAME];
  231. DEFINES *stdef;
  232. NextWord( &sLine, defname, FALSE );
  233. if ( *defname == '\0' ) return 3000;
  234. if ( nCondCompile == maxCondCompile )
  235. {
  236. maxCondCompile += 5;
  237. aCondCompile = (int*)_xrealloc( aCondCompile, sizeof( int ) * maxCondCompile );
  238. }
  239. if ( ( (stdef = DefSearch(defname)) != NULL && usl )
  240. || ( stdef == NULL && !usl ) ) aCondCompile[nCondCompile] = 1;
  241. else aCondCompile[nCondCompile] = 0;
  242. nCondCompile++;
  243. return 0;
  244. }
  245. DEFINES* DefSearch(char *defname)
  246. {
  247. int i,j;
  248. for ( i=koldefines-1; i>=0; i-- )
  249. {
  250. for ( j=0; *(aDefnew[i].name+j)==*(defname+j) &&
  251. *(aDefnew[i].name+j)!='\0'; j++ );
  252. if ( *(aDefnew[i].name+j)==*(defname+j) ) return &aDefnew[i];
  253. }
  254. for ( i=koldef-1; i>=0; i-- )
  255. {
  256. for ( j=0; *(aDefines[i].name+j)==*(defname+j) &&
  257. *(aDefines[i].name+j)!='\0'; j++ );
  258. if ( *(aDefines[i].name+j)==*(defname+j) ) return &aDefines[i];
  259. }
  260. return NULL;
  261. }
  262. int ComSearch(char *cmdname, int ncmd)
  263. {
  264. int i,j;
  265. if ( !ncmd || ncmd > kolcomm )
  266. for ( i=(ncmd)? ncmd-kolcomm-1:kolcommands-1; i >= 0; i-- )
  267. {
  268. for ( j=0; (*(aCommnew[i].name+j)==toupper(*(cmdname+j))) &&
  269. (*(aCommnew[i].name+j)!='\0') &&
  270. ((aCommnew[i].com_or_xcom)? 1:(j<4 || isname(*(cmdname+j+1)))); j++ );
  271. if ( (*(aCommnew[i].name+j)==toupper(*(cmdname+j))) ||
  272. ( !aCommnew[i].com_or_xcom && j >= 4 && *(aCommnew[i].name+j)!='\0') )
  273. return kolcomm+i;
  274. }
  275. for ( i=(ncmd && ncmd<=kolcomm)? ncmd-1:kolcomm-1; i >= 0; i-- )
  276. {
  277. for ( j=0; (*(aCommands[i].name+j)==toupper(*(cmdname+j))) &&
  278. (*(aCommands[i].name+j)!='\0') &&
  279. ((aCommands[i].com_or_xcom)? 1:(j<4 || isname(*(cmdname+j+1)))); j++ );
  280. if ( (*(aCommands[i].name+j)==toupper(*(cmdname+j))) ||
  281. ( !aCommands[i].com_or_xcom && j >= 4 && *(aCommands[i].name+j)!='\0'
  282. && *(cmdname+j) == '\0' ) )
  283. break;
  284. }
  285. return i;
  286. }
  287. int TraSearch(char *cmdname, int ncmd)
  288. {
  289. int i,j;
  290. for ( i=(ncmd)? ncmd:koltranslates-1; i >= 0; i-- )
  291. {
  292. for ( j=0; *(aTranslates[i].name+j)==toupper(*(cmdname+j)) &&
  293. *(aTranslates[i].name+j)!='\0' &&
  294. ((aTranslates[i].com_or_xcom)? 1:(j<4)); j++ );
  295. if ( *(aTranslates[i].name+j)==toupper(*(cmdname+j)) ||
  296. ( !aTranslates[i].com_or_xcom && j == 4 && *(aTranslates[i].name+j)!='\0') )
  297. break;
  298. }
  299. return i;
  300. }
  301. int ParseCommand( char* sLine, int com_or_xcom, int com_or_tra )
  302. {
  303. char cmdname[MAX_NAME];
  304. char mpatt[STR_SIZE], rpatt[STR_SIZE];
  305. int mlen,rlen;
  306. int ipos, rez;
  307. NextWord( &sLine, cmdname, FALSE );
  308. SKIPTABSPACES(sLine);
  309. if ( (ipos = hb_strAt( "=>", 2, sLine, strolen(sLine) )) > 0 )
  310. mlen = stroncpy( mpatt, sLine, ipos-1 );
  311. else return 4000; /* Quit, if '=>' absent */
  312. mlen = strotrim( mpatt );
  313. sLine += ipos + 1;
  314. SKIPTABSPACES(sLine);
  315. rlen = strocpy( rpatt, sLine );
  316. rlen = strotrim( rpatt );
  317. if ( (rez = ConvertPatterns ( mpatt, mlen, rpatt, rlen )) > 0 ) return rez;
  318. if ( com_or_tra )
  319. {
  320. AddCommand ( cmdname );
  321. aCommnew[kolcommands-1].com_or_xcom = com_or_xcom;
  322. aCommnew[kolcommands-1].mpatt = strodup ( mpatt );
  323. aCommnew[kolcommands-1].value = ( rlen > 0 )? strodup ( rpatt ) : NULL;
  324. }
  325. else
  326. {
  327. AddTranslate ( cmdname );
  328. aTranslates[koltranslates-1].com_or_xcom = com_or_xcom;
  329. aTranslates[koltranslates-1].mpatt = strodup ( mpatt );
  330. aTranslates[koltranslates-1].value = ( rlen > 0 )? strodup ( rpatt ) : NULL;
  331. }
  332. return 0;
  333. }
  334. int ConvertPatterns ( char *mpatt, int mlen, char *rpatt, int rlen )
  335. {
  336. int i = 0, ipos, ifou;
  337. int explen,rmlen;
  338. char exppatt[MAX_NAME], expreal[5] = "\1 0";
  339. char lastchar = '@', exptype;
  340. char *ptr;
  341. while ( *(mpatt+i) != '\0' )
  342. {
  343. if ( *(mpatt+i) == '<' )
  344. { /* Drag match marker, determine it type */
  345. explen = 0; ipos = i; i++; exptype = '0';
  346. if ( *(mpatt+i) == '*' ) { exptype = '3'; i++; }
  347. else if ( *(mpatt+i) == '(' ) { exptype = '4'; i++; }
  348. while ( *(mpatt+i) != '>' )
  349. {
  350. if ( *(mpatt+i) == ',' )
  351. {
  352. exptype = '1';
  353. while ( *(mpatt+i) != '>' ) i++;
  354. break;
  355. }
  356. else if ( *(mpatt+i) == ':' ) { exptype = '2'; break; }
  357. *(exppatt+explen++) = *(mpatt+i++);
  358. }
  359. if ( exptype == '3' )
  360. { if ( *(exppatt+explen-1) == '*' ) explen--; else return 4001; }
  361. else if ( exptype == '4' )
  362. { if ( *(exppatt+explen-1) == ')' ) explen--; else return 4001; }
  363. rmlen = i - ipos + 1;
  364. /* Replace match marker with new marker */
  365. lastchar = (char) ( (unsigned int)lastchar + 1 );
  366. expreal[1] = lastchar;
  367. expreal[2] = exptype;
  368. Stuff ( expreal, mpatt+ipos, 4, rmlen, mlen );
  369. mlen += 4 - rmlen; i += 4 - rmlen;
  370. ptr = rpatt;
  371. while ( (ifou = hb_strAt( exppatt, explen, ptr, rlen-(ptr-rpatt) )) > 0 )
  372. {
  373. ptr += ifou;
  374. if ( *(ptr-2) == '<' && *(ptr+explen-1) == '>' )
  375. {
  376. if ( *(ptr-3) == '#' ) { exptype = '1'; ptr -= 3; rmlen = explen+3; }
  377. else { exptype = '0'; ptr -= 2; rmlen = explen+2; }
  378. }
  379. else if ( *(ptr-3) == '<' && *(ptr+explen) == '>' )
  380. {
  381. ptr -= 2;
  382. if ( *ptr == '\"' ) exptype = '2';
  383. else if ( *ptr == '(' ) exptype = '3';
  384. else if ( *ptr == '{' ) exptype = '4';
  385. else if ( *ptr == '.' ) exptype = '5';
  386. ptr--;
  387. rmlen = explen+4;
  388. }
  389. else continue;
  390. expreal[2] = exptype;
  391. Stuff ( expreal, ptr, 4, rmlen, rlen );
  392. rlen += 4 - rmlen;
  393. }
  394. }
  395. i++;
  396. }
  397. return 0;
  398. }
  399. void AddCommand ( char *cmdname )
  400. {
  401. if ( kolcommands == maxcommands )
  402. {
  403. maxcommands += 50;
  404. aCommnew = (COMMANDS *)_xrealloc( aCommnew, sizeof( COMMANDS ) * maxcommands );
  405. }
  406. aCommnew[kolcommands].name = strodup ( cmdname );
  407. kolcommands++;
  408. }
  409. void AddTranslate ( char *cmdname )
  410. {
  411. if ( koltranslates == maxtranslates )
  412. {
  413. maxtranslates += 50;
  414. aTranslates = (TRANSLATES *)_xrealloc( aTranslates, sizeof( TRANSLATES ) * maxtranslates );
  415. }
  416. aTranslates[koltranslates].name = strodup ( cmdname );
  417. koltranslates++;
  418. }
  419. COMMANDS* getCommand ( int ndef )
  420. {
  421. return (ndef>=kolcomm)? &(aCommnew[ndef-kolcomm]):&(aCommands[ndef]);
  422. }
  423. int ParseExpression( char* sLine, char* sOutLine )
  424. {
  425. char sToken[MAX_NAME];
  426. char *ptri, *ptro;
  427. int lenToken, i, ndef, ipos, isdvig, lens;
  428. int rezDef, rezCom, kolpass = 0;
  429. int kolused = 0, lastused;
  430. DEFINES *aUsed[100], *stdef;
  431. strotrim ( sLine );
  432. do
  433. {
  434. ptri = sLine; ptro = sOutLine;
  435. rezDef = 0; rezCom = 0;
  436. lastused = kolused;
  437. /* Look for macros from #define */
  438. while ( ( lenToken = NextName(&ptri, sToken, &ptro) ) > 0 )
  439. if ( (stdef=DefSearch(sToken)) != NULL )
  440. {
  441. for(i=0;i<kolused;i++) if ( aUsed[i] == stdef ) break;
  442. if ( i < kolused ) { if ( i < lastused ) return 1000; }
  443. else
  444. aUsed[kolused++] = stdef;
  445. rezDef += WorkDefine ( &ptri, &ptro, stdef, lenToken );
  446. }
  447. *ptro = '\0';
  448. memcpy ( sLine, sOutLine, ptro - sOutLine + 1);
  449. /* Look for definitions from #translate */
  450. ptri = sLine; ptro = sOutLine;
  451. while ( ( lenToken = NextName(&ptri, sToken, &ptro) ) > 0 )
  452. if ( (ndef=TraSearch(sToken,0)) >= 0 )
  453. WorkTranslate( sToken, &ptri, ptro, ndef );
  454. /* Look for definitions from #command */
  455. if ( kolpass < 2 )
  456. {
  457. ptri = sLine; isdvig = 0;
  458. do
  459. {
  460. ptri = sLine + isdvig;
  461. ipos = md_strAt( ";", 1, ptri );
  462. if ( ipos > 0 ) *(ptri+ipos-1) = '\0';
  463. SKIPTABSPACES( ptri );
  464. if ( isname(*ptri) )
  465. lenToken = NextName( &ptri, sToken, NULL);
  466. else
  467. { *sToken = *ptri++; *(sToken+1) = '\0'; lenToken = 1; }
  468. SKIPTABSPACES( ptri );
  469. if ( *ptri != ':' && *ptri != '=' && (isname(*ptri) || *(ptri+1) != '=')
  470. && (ndef=ComSearch(sToken,0)) >= 0 )
  471. {
  472. ptro = sOutLine;
  473. i = WorkCommand( sToken, ptri, ptro, ndef );
  474. if ( ipos > 0 ) *(sLine+isdvig+ipos-1) = ';';
  475. if ( i >= 0 )
  476. {
  477. if ( isdvig + ipos > 0 )
  478. {
  479. lens = strolen( sLine+isdvig );
  480. Stuff ( ptro, sLine+isdvig, i, (ipos)? ipos-1:lens, lens );
  481. ipos = i + 1;
  482. }
  483. else
  484. memcpy ( sLine, sOutLine, i+1);
  485. }
  486. rezCom = 1;
  487. }
  488. else if ( ipos > 0 ) *(sLine+isdvig+ipos-1) = ';';
  489. isdvig += ipos;
  490. }
  491. while ( ipos != 0 );
  492. }
  493. kolpass++;
  494. }
  495. while ( rezDef || rezCom );
  496. return 0;
  497. }
  498. int WorkDefine ( char** ptri, char** ptro, DEFINES *stdef, int lenToken )
  499. {
  500. int rezDef = 0, npars, i;
  501. if ( stdef->pars == NULL )
  502. {
  503. rezDef = 1;
  504. *ptro -= lenToken;
  505. lenToken = 0;
  506. while ( *(stdef->value+lenToken) != '\0' )
  507. *(*ptro)++ = *(stdef->value+lenToken++);
  508. }
  509. else
  510. {
  511. SKIPTABSPACES( *ptri );
  512. if ( **ptri == '(' )
  513. {
  514. npars=0; i = 0;
  515. while ( *(*ptri+i) != ')' && *(*ptri+i) != '\0' )
  516. {
  517. if ( *(*ptri+i) == ',' ) npars++;
  518. i++;
  519. }
  520. if ( stdef->npars == npars + 1 )
  521. {
  522. rezDef = 1;
  523. *ptro -= lenToken;
  524. WorkPseudoF( ptri, ptro, stdef );
  525. }
  526. }
  527. else *(*ptro)++ = ' ';
  528. }
  529. return rezDef;
  530. }
  531. void WorkPseudoF ( char** ptri, char** ptro, DEFINES *stdef )
  532. {
  533. char parfict[MAX_NAME], parreal[MAX_NAME];
  534. char *ptrb;
  535. int ipos = 0, ifou, ibeg;
  536. int lenfict, lenreal, lenres;
  537. while ( *(stdef->value+ipos) != '\0' ) /* Copying value of macro */
  538. { /* to destination string */
  539. *(*ptro+ipos) = *(stdef->value+ipos);
  540. ipos++;
  541. }
  542. *(*ptro+ipos) = '\0';
  543. lenres = ipos;
  544. ipos = 0; ibeg = 0;
  545. do /* Parsing through parameters */
  546. { /* in macro definition */
  547. if ( *(stdef->pars+ipos)==',' || *(stdef->pars+ipos)=='\0' )
  548. {
  549. *(parfict+ipos-ibeg) = '\0';
  550. lenfict = ipos - ibeg;
  551. if ( **ptri != ')' )
  552. {
  553. (*ptri)++; lenreal = 0; /* Parsing through real parameters */
  554. while ( **ptri != ',' && **ptri != ')' )
  555. {
  556. *(parreal+lenreal++) = **ptri;
  557. (*ptri)++;
  558. }
  559. *(parreal+lenreal) = '\0';
  560. ptrb = *ptro;
  561. while ( (ifou = hb_strAt( parfict, lenfict, ptrb, lenres-(ptrb-*ptro) )) > 0 )
  562. {
  563. ptrb = ptrb+ifou-1;
  564. if ( !isname(*(ptrb-1)) && !isname(*(ptrb+lenfict)) )
  565. {
  566. Stuff ( parreal, ptrb, lenreal, lenfict, lenres );
  567. lenres += lenreal - lenfict;
  568. }
  569. else ptrb++;
  570. }
  571. ibeg = ipos+1;
  572. }
  573. }
  574. else *(parfict+ipos-ibeg) = *(stdef->pars+ipos);
  575. if ( *(stdef->pars+ipos) == '\0' ) break;
  576. ipos++;
  577. }
  578. while ( 1 );
  579. (*ptri)++;
  580. *ptro += lenres;
  581. }
  582. int WorkCommand ( char* sToken, char* ptri, char* ptro, int ndef )
  583. {
  584. int rez;
  585. int lenres;
  586. char *ptrmp;
  587. do
  588. {
  589. lenres = strocpy ( ptro, getCommand(ndef)->value ); /* Copying result pattern */
  590. ptrmp = getCommand(ndef)->mpatt; /* Pointer to a match pattern */
  591. Repeate = 0;
  592. groupchar = '@';
  593. rez = CommandStuff ( ptrmp, ptri, ptro, &lenres, TRUE );
  594. if ( !rez ) ndef = ComSearch(sToken,ndef);
  595. }
  596. while ( !rez && ndef >= 0 );
  597. *(ptro+lenres) = '\0';
  598. if ( rez ) return lenres;
  599. return -1;
  600. }
  601. int WorkTranslate ( char* sToken, char** ptri, char* ptro, int ndef )
  602. {
  603. int rez;
  604. int lenres;
  605. char *ptrmp;
  606. do
  607. {
  608. lenres = strocpy ( ptro, aTranslates[ndef].value );
  609. ptrmp = aTranslates[ndef].mpatt;
  610. Repeate = 0;
  611. groupchar = '@';
  612. rez = CommandStuff ( ptrmp, *ptri, ptro, &lenres, FALSE );
  613. if ( !rez ) ndef = TraSearch(sToken,ndef-1);
  614. }
  615. while ( !rez && ndef >= 0 );
  616. *(ptro+lenres) = '\0';
  617. if ( rez )
  618. {
  619. Stuff( ptro, *ptri, lenres, rez, strolen(*ptri) );
  620. return lenres;
  621. }
  622. return 0;
  623. }
  624. int CommandStuff ( char *ptrmp, char *inputLine, char * ptro, int *lenres, int com_or_tra )
  625. {
  626. int nbr = 0, endTranslation = FALSE;
  627. char *lastopti[2];
  628. char *ptri = inputLine, *ptr;
  629. SKIPTABSPACES( ptri );
  630. if ( ptrmp == NULL ) { if ( *ptri != '\0' ) return 0; }
  631. else
  632. while ( *ptri != '\0' && !endTranslation )
  633. {
  634. SKIPTABSPACES( ptrmp );
  635. SKIPTABSPACES( ptri );
  636. switch ( *ptrmp ) {
  637. case '[':
  638. nbr++;
  639. ptrmp++;
  640. lastopti[Repeate] = ptrmp;
  641. break;
  642. case ']':
  643. if ( Repeate ) { Repeate--; ptrmp = lastopti[Repeate]; }
  644. else { nbr--; ptrmp++; }
  645. break;
  646. case ',':
  647. if ( *ptri == ',' ) { ptrmp++; ptri++; }
  648. else
  649. {
  650. if ( nbr ) { SkipOptional( &ptrmp, ptro, lenres); }
  651. else return 0;
  652. }
  653. break;
  654. case '\1': /* Match marker */
  655. if ( !WorkMarkers( &ptrmp, &ptri, ptro, lenres, nbr ) )
  656. return 0;
  657. break;
  658. case '\0':
  659. if ( com_or_tra ) return 0; else endTranslation = TRUE;
  660. default: /* Key word */
  661. ptr = ptrmp;
  662. if ( *ptri == ',' || strincmp(ptri, &ptrmp ) )
  663. {
  664. if ( nbr ) { SkipOptional( &ptrmp, ptro, lenres); }
  665. else return 0;
  666. }
  667. else if ( *ptri != ',' ) ptri += (ptrmp - ptr);
  668. }
  669. };
  670. if ( *ptrmp != '\0' )
  671. {
  672. if ( Repeate ) { Repeate = 0; ptrmp = lastopti[0] - 1; }
  673. do
  674. {
  675. SKIPTABSPACES( ptrmp );
  676. if ( *ptrmp != '\0' )
  677. switch ( *ptrmp ) {
  678. case '[':
  679. ptrmp++;
  680. SkipOptional( &ptrmp, ptro, lenres);
  681. ptrmp++;
  682. break;
  683. case ']': ptrmp++; break;
  684. default:
  685. return 0;
  686. }
  687. }
  688. while ( *ptrmp != '\0' );
  689. }
  690. if ( com_or_tra ) return 1; else return (ptri-inputLine);
  691. }
  692. int WorkMarkers( char **ptrmp, char **ptri, char *ptro, int *lenres, int nbr )
  693. {
  694. char expreal[MAX_NAME], exppatt[MAX_NAME];
  695. int lenreal = 0, lenpatt;
  696. int rezrestr, ipos;
  697. char *ptr, *ptrtemp;
  698. if ( **ptri == ',' ) return 0;
  699. /* Copying a match pattern to 'exppatt' */
  700. lenpatt = stroncpy ( exppatt, *ptrmp, 4 );
  701. *ptrmp += 4;
  702. SKIPTABSPACES ( *ptrmp );
  703. if ( *(exppatt+2) != '2' && **ptrmp != '\1' && **ptrmp != ',' &&
  704. **ptrmp != '[' && **ptrmp != ']' && **ptrmp != '\0' )
  705. {
  706. lenreal = strincpy ( expreal, *ptrmp );
  707. if ( (ipos = md_strAt( expreal, lenreal, *ptri )) > 0 )
  708. {
  709. lenreal = stroncpy( expreal, *ptri, ipos-1 );
  710. if ( isExpres ( expreal ) )
  711. *ptri += lenreal;
  712. else return 0;
  713. }
  714. else return 0;
  715. }
  716. if ( *(exppatt+2) == '4' ) /* ---- extended match marker */
  717. {
  718. if ( !lenreal ) lenreal = getExpReal ( expreal, ptri, FALSE );
  719. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  720. }
  721. else if ( *(exppatt+2) == '3' ) /* ---- wild match marker */
  722. {
  723. lenreal = strocpy ( expreal, *ptri );
  724. *ptri += lenreal;
  725. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  726. }
  727. else if ( *(exppatt+2) == '2' ) /* ---- restricted match marker */
  728. {
  729. while ( **ptrmp != '>' ) *(exppatt+lenpatt++) = *((*ptrmp)++);
  730. *(exppatt+lenpatt) = '\0';
  731. (*ptrmp)++;
  732. ptr = exppatt + 4;
  733. rezrestr = 0;
  734. while ( *ptr != '\0' )
  735. {
  736. if ( *ptr == '&' )
  737. {
  738. if ( **ptri == '&' )
  739. {
  740. rezrestr = 1;
  741. (*ptri)++;
  742. lenreal = getExpReal ( expreal, ptri, FALSE );
  743. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  744. break;
  745. }
  746. else ptr++;
  747. }
  748. else
  749. {
  750. SKIPTABSPACES( ptr );
  751. /* Comparing real parameter and restriction value */
  752. ptrtemp = ptr;
  753. if ( !strincmp ( *ptri, &ptr ) )
  754. {
  755. lenreal = stroncpy( expreal, *ptri, (ptr-ptrtemp) );
  756. *ptri += lenreal;
  757. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  758. rezrestr = 1;
  759. break;
  760. }
  761. else
  762. {
  763. while ( *ptr != ',' && *ptr != '\0' ) ptr++;
  764. if ( *ptr == ',' ) ptr++;
  765. }
  766. }
  767. }
  768. if ( rezrestr == 0 )
  769. { /* If restricted match marker doesn't correspond to real parameter */
  770. if ( nbr ) SearnRep( exppatt,"",0,ptro,lenres);
  771. else return 0;
  772. }
  773. }
  774. else if ( *(exppatt+2) == '1' ) /* ---- list match marker */
  775. {
  776. if ( !lenreal ) lenreal = getExpReal ( expreal, ptri, TRUE );
  777. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  778. }
  779. else /* ---- regular match marker */
  780. {
  781. /* Copying a real expression to 'expreal' */
  782. if ( !lenreal ) lenreal = getExpReal ( expreal, ptri, FALSE );
  783. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  784. }
  785. return 1;
  786. }
  787. int getExpReal ( char *expreal, char **ptri, int prlist )
  788. {
  789. int lens = 0;
  790. char *sZnaki = "+-=><*/$.&:";
  791. int State;
  792. int StBr1 = 0, StBr2 = 0, StBr3 = 0;
  793. int rez = 0;
  794. SKIPTABSPACES ( *ptri );
  795. State = (**ptri=='\'' || **ptri=='\"')? STATE_EXPRES:STATE_ID;
  796. while ( **ptri != '\0' && !rez )
  797. {
  798. switch ( State ) {
  799. case STATE_QUOTE1:
  800. if(**ptri=='\'')
  801. State = (StBr1==0 && StBr2==0 && StBr3==0)? STATE_ID_END:STATE_BRACKET;
  802. break;
  803. case STATE_QUOTE2:
  804. if(**ptri=='\"')
  805. State = (StBr1==0 && StBr2==0 && StBr3==0)? STATE_ID_END:STATE_BRACKET;
  806. break;
  807. case STATE_BRACKET:
  808. if ( **ptri == '\'' ) State = STATE_QUOTE1;
  809. else if ( **ptri == '\"' ) State = STATE_QUOTE2;
  810. else if ( **ptri == '(' ) StBr1++;
  811. else if ( **ptri == '[' ) StBr2++;
  812. else if ( **ptri == '{' ) StBr3++;
  813. else if ( **ptri == ')' )
  814. { StBr1--; if (StBr1==0 && StBr2==0 && StBr3==0) State = STATE_ID_END; }
  815. else if ( **ptri == ']' )
  816. { StBr2--; if (StBr1==0 && StBr2==0 && StBr3==0) State = STATE_ID_END; }
  817. else if ( **ptri == '}' )
  818. { StBr3--; if (StBr1==0 && StBr2==0 && StBr3==0) State = STATE_ID_END; }
  819. break;
  820. case STATE_ID:
  821. case STATE_ID_END:
  822. if ( ( (isname(**ptri) || **ptri=='\\') && State == STATE_ID_END ) ||
  823. **ptri==',' || **ptri=='\'' || **ptri=='\"')
  824. {
  825. if ( **ptri == ',' )
  826. {
  827. if ( !prlist ) rez = 1;
  828. State = STATE_EXPRES;
  829. }
  830. else rez = 1;
  831. }
  832. else if ( IsInStr( **ptri, sZnaki ) )
  833. {
  834. State = STATE_EXPRES;
  835. }
  836. else if ( **ptri == '(' )
  837. {
  838. State = STATE_BRACKET;
  839. StBr1 = 1;
  840. }
  841. else if ( **ptri == '[' )
  842. {
  843. State = STATE_BRACKET;
  844. StBr2 = 1;
  845. }
  846. else if ( **ptri == '{' )
  847. {
  848. State = STATE_BRACKET;
  849. StBr3 = 1;
  850. }
  851. else if ( **ptri == ' ' ) State = STATE_ID_END;
  852. break;
  853. case STATE_EXPRES:
  854. case STATE_EXPRES_ID:
  855. if ( **ptri == '\'' ) State = STATE_QUOTE1;
  856. else if ( **ptri == '\"' ) State = STATE_QUOTE2;
  857. else if ( isname(**ptri) ) State = STATE_EXPRES_ID;
  858. else if ( **ptri == ' ' && State == STATE_EXPRES_ID ) State = STATE_ID_END;
  859. else if ( **ptri == '(' ) { StBr1++; State = STATE_BRACKET; }
  860. else if ( **ptri == '[' ) { StBr2++; State = STATE_BRACKET; }
  861. else if ( **ptri == '{' ) { StBr3++; State = STATE_BRACKET; }
  862. else if ( **ptri == ',' ) { if ( !prlist ) rez = 1; State = STATE_EXPRES; }
  863. else State = STATE_EXPRES;
  864. break;
  865. }
  866. if ( !rez )
  867. {
  868. if ( expreal != NULL ) *expreal++ = **ptri;
  869. (*ptri)++;
  870. lens++;
  871. }
  872. }
  873. if ( expreal != NULL )
  874. {
  875. if ( *(expreal-1) == ' ' ) { expreal--; lens--; };
  876. *expreal = '\0';
  877. }
  878. return lens;
  879. }
  880. int isExpres ( char* stroka )
  881. {
  882. if ( strolen ( stroka ) > getExpReal ( NULL, &stroka, FALSE ) )
  883. return 0;
  884. else
  885. return 1;
  886. }
  887. void SkipOptional( char** ptri, char *ptro, int* lenres)
  888. {
  889. int nbr = 0;
  890. char exppatt[MAX_NAME];
  891. int lenpatt;
  892. while ( isname(**ptri) ) (*ptri)++;
  893. SKIPTABSPACES( *ptri );
  894. while ( **ptri != ']' || nbr )
  895. {
  896. switch ( **ptri ) {
  897. case '[': nbr++; break;
  898. case ']': nbr--; break;
  899. case '\1':
  900. for ( lenpatt=0; lenpatt<4; lenpatt++ ) *(exppatt+lenpatt) = *((*ptri)++);
  901. (*ptri)--;
  902. if ( exppatt[2] == '2' )
  903. while ( **ptri != '>' ) (*ptri)++;
  904. SearnRep( exppatt,"",0,ptro,lenres);
  905. break;
  906. }
  907. (*ptri)++;
  908. }
  909. }
  910. void SearnRep( char *exppatt,char *expreal,int lenreal,char *ptro, int *lenres)
  911. {
  912. int ifou, isdvig = 0, rezs;
  913. int kolmarkers;
  914. int lennew, i;
  915. char lastchar = '0';
  916. char expnew[MAX_NAME];
  917. char *ptr, *ptr2;
  918. while ( (ifou = hb_strAt( exppatt, 2, ptro+isdvig, *lenres-isdvig )) > 0 )
  919. {
  920. rezs = 0;
  921. ptr = ptro+isdvig+ifou-2;
  922. kolmarkers = 0;
  923. while ( ptr >= ptro+isdvig )
  924. {
  925. if ( *ptr == '[' || *ptr == ']' ) break;
  926. if ( *ptr == '\1' ) kolmarkers++;
  927. ptr--;
  928. }
  929. if ( *ptr == '[' )
  930. {
  931. ptr2 = ptro+isdvig+ifou+3;
  932. while ( *ptr2 != ']' ) { if ( *ptr2 == '\1' ) kolmarkers++; ptr2++; }
  933. if ( Repeate && lenreal && kolmarkers) return;
  934. if ( lenreal == 0 )
  935. {
  936. Stuff ( "", ptr, 0, ptr2-ptr+1, *lenres-(ptr-ptro) );
  937. *lenres -= ptr2-ptr+1;
  938. isdvig = ptr - ptro;
  939. rezs = 1;
  940. }
  941. else
  942. {
  943. lennew = ptr2-ptr-1;
  944. memcpy ( expnew, ptr+1, lennew );
  945. *(expnew + lennew) = '\0';
  946. while ( (i = hb_strAt( exppatt, 2, expnew, lennew )) > 0 )
  947. lennew += ReplacePattern ( exppatt[2], expreal, lenreal, expnew+i-1, lennew );
  948. if ( kolmarkers )
  949. {
  950. groupchar = (char) ( (unsigned int)groupchar + 1 );
  951. for ( i=0; i<lennew; i++ )
  952. if ( *(expnew+i) == '\1' )
  953. {
  954. *(expnew+i+3) = groupchar;
  955. i += 4;
  956. }
  957. }
  958. Stuff ( expnew, ptr, lennew, 0, *lenres-(ptr-ptro)+1 );
  959. *lenres += lennew;
  960. isdvig = ptr - ptro + (ptr2-ptr-1) + lennew;
  961. rezs = 1;
  962. Repeate++;
  963. }
  964. }
  965. if ( !rezs )
  966. {
  967. if ( *(ptro+isdvig+ifou+2) != '0' )
  968. {
  969. if ( lastchar == '0' ) lastchar = *(ptro+isdvig+ifou+2);
  970. if ( lastchar != *(ptro+isdvig+ifou+2) )
  971. { isdvig += ifou + 3; continue; }
  972. }
  973. *lenres += ReplacePattern ( exppatt[2], expreal, lenreal,
  974. ptro+isdvig+ifou-1, *lenres-isdvig-ifou+1 );
  975. isdvig += ifou;
  976. }
  977. }
  978. }
  979. int ReplacePattern ( char patttype, char *expreal, int lenreal, char *ptro, int lenres )
  980. {
  981. int rmlen = lenreal;
  982. switch ( *(ptro+2) ) {
  983. case '0': /* Regular result marker */
  984. Stuff ( expreal, ptro, lenreal, 4, lenres );
  985. break;
  986. case '1': /* Dumb stringify result marker */
  987. Stuff ( "\"\"", ptro, 2, 4, lenres );
  988. if ( lenreal )
  989. Stuff ( expreal, ptro+1, lenreal, 0, lenres );
  990. rmlen = lenreal + 2;
  991. break;
  992. case '2': /* Normal stringify result marker */
  993. if ( !lenreal )
  994. Stuff ( "", ptro, 0, 4, lenres );
  995. else if ( patttype == '1' ) /* list match marker */
  996. {
  997. }
  998. else
  999. {
  1000. Stuff ( "\"\"", ptro, 2, 4, lenres );
  1001. Stuff ( expreal, ptro+1, lenreal, 0, lenres );
  1002. rmlen = lenreal + 2;
  1003. }
  1004. break;
  1005. case '3': /* Smart stringify result marker */
  1006. if ( patttype == '1' ) /* list match marker */
  1007. {
  1008. }
  1009. else if ( !lenreal || *expreal == '(' )
  1010. Stuff ( expreal, ptro, lenreal, 4, lenres );
  1011. else
  1012. {
  1013. Stuff ( "\"\"", ptro, 2, 4, lenres );
  1014. Stuff ( expreal, ptro+1, lenreal, 0, lenres );
  1015. rmlen = lenreal + 2;
  1016. }
  1017. break;
  1018. case '4': /* Blockify result marker */
  1019. if ( !lenreal )
  1020. Stuff ( expreal, ptro, lenreal, 4, lenres );
  1021. else if ( patttype == '1' ) /* list match marker */
  1022. {
  1023. }
  1024. else
  1025. {
  1026. Stuff ( "{||}", ptro, 4, 4, lenres );
  1027. Stuff ( expreal, ptro+3, lenreal, 0, lenres );
  1028. rmlen = lenreal + 4;
  1029. }
  1030. break;
  1031. case '5': /* Logify result marker */
  1032. rmlen = 3;
  1033. if ( !lenreal )
  1034. {
  1035. Stuff ( ".F.", ptro, 3, 4, lenres );
  1036. }
  1037. else
  1038. Stuff ( ".T.", ptro, 3, 4, lenres );
  1039. break;
  1040. }
  1041. return rmlen - 4;
  1042. }
  1043. int RdStr(FILE* handl_i,char *buffer,int maxlen,int lDropSpaces,char* sBuffer, int* lenBuffer, int* iBuffer)
  1044. {
  1045. int readed = 0;
  1046. int State = 0;
  1047. char cha,cLast='\0';
  1048. if ( *lenBuffer == 0 ) return -1;
  1049. while(1)
  1050. {
  1051. if ( *iBuffer == *lenBuffer )
  1052. {
  1053. if ( (*lenBuffer = fread(sBuffer,1,BUFF_SIZE,handl_i)) < 1 ) sBuffer[0] = '\n';
  1054. *iBuffer = 0;
  1055. }
  1056. cha = sBuffer[*iBuffer];
  1057. (*iBuffer)++;
  1058. if( cha == '\n' ) break;
  1059. if ( maxlen > 0 )
  1060. {
  1061. switch ( ParseState ) {
  1062. case STATE_COMMENT:
  1063. if ( cha == '/' && cLast == '*' )
  1064. { ParseState = STATE_NORMAL; cha = ' '; }
  1065. cLast = cha;
  1066. break;
  1067. case STATE_QUOTE1: if(cha=='\'') ParseState = STATE_NORMAL; break;
  1068. case STATE_QUOTE2: if(cha=='\"') ParseState = STATE_NORMAL; break;
  1069. default:
  1070. switch ( cha ) {
  1071. case '\"': ParseState = STATE_QUOTE2; break;
  1072. case '\'': ParseState = STATE_QUOTE1; break;
  1073. case '&': if ( readed>0 && buffer[readed-1] == '&' ) { maxlen = 0; readed--; } break;
  1074. case '/': if ( readed>0 && buffer[readed-1] == '/' ) { maxlen = 0; readed--; } break;
  1075. case '*':
  1076. if ( readed > 0 && buffer[readed-1] == '/' )
  1077. { ParseState = STATE_COMMENT; readed--; }
  1078. else if ( !State ) maxlen = readed = 0;
  1079. break;
  1080. }
  1081. }
  1082. if ( cha != ' ' && cha != '\t' ) State = 1;
  1083. if( lDropSpaces && State ) lDropSpaces = 0;
  1084. if( readed<maxlen && (!lDropSpaces || readed==0) &&
  1085. ParseState != STATE_COMMENT )
  1086. buffer[readed++]=cha;
  1087. }
  1088. }
  1089. while(--readed >= 0 && ( buffer[readed] == ' ' || buffer[readed] == '\t') );
  1090. readed++;
  1091. buffer[readed]='\0';
  1092. return readed;
  1093. }
  1094. int WrStr(FILE* handl_o,char *buffer)
  1095. {
  1096. int lens = strolen(buffer);
  1097. fwrite(buffer,lens,1,handl_o);
  1098. if ( *(buffer+lens-1) != '\n' ) fwrite("\n",1,1,handl_o);
  1099. return 0;
  1100. }
  1101. /* locates a substring in a string */
  1102. int hb_strAt(char *szSub, int lSubLen, char *szText, int lLen)
  1103. {
  1104. if( lSubLen )
  1105. {
  1106. if( lLen >= lSubLen )
  1107. {
  1108. long lPos = 0, lSubPos = 0;
  1109. while( lPos < lLen && lSubPos < lSubLen )
  1110. {
  1111. if( *(szText + lPos) == *(szSub + lSubPos) )
  1112. {
  1113. lSubPos++;
  1114. lPos++;
  1115. }
  1116. else if( lSubPos )
  1117. lSubPos = 0;
  1118. else
  1119. lPos++;
  1120. }
  1121. return (lSubPos < lSubLen? 0: lPos - lSubLen + 1);
  1122. }
  1123. else
  1124. return 0;
  1125. }
  1126. else
  1127. return 1;
  1128. }
  1129. int md_strAt(char *szSub, int lSubLen, char *szText)
  1130. {
  1131. int State = STATE_NORMAL;
  1132. long lPos = 0, lSubPos = 0;
  1133. while( *(szText+lPos) != '\0' && lSubPos < lSubLen )
  1134. {
  1135. if( State == STATE_QUOTE1 )
  1136. {
  1137. if ( *(szText+lPos) == '\'' ) State = STATE_NORMAL;
  1138. lPos++;
  1139. }
  1140. else if( State == STATE_QUOTE2 )
  1141. {
  1142. if ( *(szText+lPos) == '\"' ) State = STATE_NORMAL;
  1143. lPos++;
  1144. }
  1145. else
  1146. {
  1147. if ( *(szText+lPos) == '\"' )
  1148. {
  1149. State = STATE_QUOTE2;
  1150. lPos++;
  1151. continue;
  1152. }
  1153. else if ( *(szText+lPos) == '\'' )
  1154. {
  1155. State = STATE_QUOTE1;
  1156. lPos++;
  1157. continue;
  1158. }
  1159. if( toupper(*(szText + lPos)) == toupper(*(szSub + lSubPos)) )
  1160. {
  1161. lSubPos++;
  1162. lPos++;
  1163. if ( lSubPos >= lSubLen &&
  1164. ( ( isname(*szSub) && lPos>lSubPos && isname(*(szText+lPos-lSubPos-1)) ) ||
  1165. ( isname(*(szSub+lSubLen-1)) && isname(*(szText+lPos)) ) ) )
  1166. lSubPos = 0;
  1167. }
  1168. else if( lSubPos ) lSubPos = 0;
  1169. else lPos++;
  1170. }
  1171. }
  1172. return (lSubPos < lSubLen? 0: lPos - lSubLen + 1);
  1173. }
  1174. int IsInStr ( char symb, char* s )
  1175. {
  1176. while ( *s != '\0' ) if ( *s++ == symb ) return 1;
  1177. return 0;
  1178. }
  1179. void Stuff (char *ptri, char * ptro, int len1, int len2, int lenres )
  1180. {
  1181. char *ptr1, *ptr2;
  1182. int i;
  1183. if ( len1 > len2 )
  1184. {
  1185. ptr1 = ptro+lenres;
  1186. ptr2 = ptro + lenres + len1 - len2;
  1187. for ( ; ptr1 >= ptro; ptr1--,ptr2-- ) *ptr2 = *ptr1;
  1188. }
  1189. else
  1190. {
  1191. ptr1 = ptro + len2;
  1192. ptr2 = ptro + len1;
  1193. for ( ; ptr1 <= ptro+lenres; ptr1++,ptr2++ ) *ptr2 = *ptr1;
  1194. }
  1195. ptr2 = ptro;
  1196. for ( i=0; i < len1; i++ ) *ptr2++ = *(ptri+i);
  1197. }
  1198. int strocpy (char* ptro, char* ptri )
  1199. {
  1200. int lens = 0;
  1201. if ( ptri != NULL )
  1202. while ( *ptri != '\0' )
  1203. {
  1204. *ptro++ = *ptri++;
  1205. lens++;
  1206. }
  1207. *ptro = '\0';
  1208. return lens;
  1209. }
  1210. int stroncpy (char* ptro, char* ptri, int lens )
  1211. {
  1212. int i = 0;
  1213. for ( ; i < lens; i++ ) *(ptro+i) = *ptri++;
  1214. i--;
  1215. while ( i > 0 && *(ptro+i) == ' ' ) i--;
  1216. i++;
  1217. *(ptro+i) = '\0';
  1218. return i;
  1219. }
  1220. int strincmp (char* ptro, char** ptri )
  1221. {
  1222. for ( ; **ptri != ' ' && **ptri != ',' && **ptri != '[' && **ptri != ']' &&
  1223. **ptri != '\1' && **ptri != '\0' && toupper(**ptri)==toupper(*ptro);
  1224. ptro++, (*ptri)++ );
  1225. if ( **ptri == ' ' || **ptri == ',' || **ptri == '[' ||
  1226. **ptri == ']' || **ptri == '\1' || **ptri == '\0' ) return 0;
  1227. return 1;
  1228. }
  1229. int strincpy (char* ptro, char* ptri )
  1230. {
  1231. int lens = 0;
  1232. for ( ; *ptri != ' ' && *ptri != ',' && *ptri != '[' && *ptri != ']' &&
  1233. *ptri != '\1' && *ptri != '\0'; ptro++, ptri++, lens++ )
  1234. *ptro = *ptri;
  1235. return lens;
  1236. }
  1237. char* strodup ( char *stroka )
  1238. {
  1239. char *ptr;
  1240. int lens = 0;
  1241. while ( *(stroka+lens) != '\0' ) lens++;
  1242. ptr = ( char * ) _xgrab( lens + 1 );
  1243. memcpy( ptr, stroka, lens+1 );
  1244. *(ptr+lens) = '\0';
  1245. return ptr;
  1246. }
  1247. int strolen ( char *stroka )
  1248. {
  1249. int lens = 0;
  1250. while ( *(stroka+lens) != '\0' ) lens++;
  1251. return lens;
  1252. }
  1253. int strotrim ( char *stroka )
  1254. {
  1255. char *ptr = stroka, lastc = '0';
  1256. int lens = 0, State = STATE_NORMAL;
  1257. while ( *stroka != '\0' )
  1258. {
  1259. if ( State == STATE_QUOTE1 ) { if (*stroka == '\'') State = STATE_NORMAL; }
  1260. else if ( State == STATE_QUOTE2 ) { if (*stroka=='\"') State = STATE_NORMAL; }
  1261. else
  1262. {
  1263. if ( *stroka == '\'' ) State = STATE_QUOTE1;
  1264. else if ( *stroka == '\"' ) State = STATE_QUOTE2;
  1265. }
  1266. /* if ( State != STATE_NORMAL || (*stroka != ' ' && *stroka != '\t') ||
  1267. ( (isname(lastc) || lastc=='>') && (isname(*(stroka+1)) || *(stroka+1)=='<') ) )
  1268. */
  1269. if ( State != STATE_NORMAL || (*stroka != ' ' && *stroka != '\t') ||
  1270. ( *stroka==' ' && lastc != ' ' && lastc != ',' && lastc != '(' && *(stroka+1)!=',') )
  1271. {
  1272. *ptr++ = *stroka;
  1273. lastc = *stroka;
  1274. lens++;
  1275. }
  1276. stroka++;
  1277. }
  1278. *ptr = '\0';
  1279. return lens;
  1280. }
  1281. int NextWord ( char** sSource, char* sDest, int lLower )
  1282. {
  1283. int i = 0;
  1284. SKIPTABSPACES( (*sSource) );
  1285. while ( **sSource != '\0' && **sSource != ' ')
  1286. {
  1287. *sDest++ = (lLower)? tolower(**sSource):**sSource;
  1288. (*sSource)++;
  1289. i++;
  1290. }
  1291. *sDest = '\0';
  1292. return i;
  1293. }
  1294. int NextName ( char** sSource, char* sDest, char **sOut )
  1295. {
  1296. int lenName = 0, State = STATE_NORMAL;
  1297. while ( **sSource != '\0' && ( !isname(**sSource) || State != STATE_NORMAL ) )
  1298. {
  1299. if ( State == STATE_QUOTE1 )
  1300. { if ( **sSource == '\'' ) State = STATE_NORMAL; }
  1301. else if ( State == STATE_QUOTE2 )
  1302. { if ( **sSource == '\"' ) State = STATE_NORMAL; }
  1303. else if ( **sSource == '\"' ) State = STATE_QUOTE2;
  1304. else if ( **sSource == '\'' ) State = STATE_QUOTE1;
  1305. if ( sOut !=NULL ) *(*sOut)++ = **sSource;
  1306. (*sSource)++;
  1307. }
  1308. while ( **sSource != '\0' && isname(**sSource) )
  1309. {
  1310. if ( sOut !=NULL ) *(*sOut)++ = **sSource;
  1311. *sDest++ = *(*sSource)++;
  1312. lenName++;
  1313. }
  1314. *sDest = '\0';
  1315. return lenName;
  1316. }
  1317. int OpenInclude( char * szFileName, PATHNAMES *pSearch, FILE** fptr )
  1318. {
  1319. if( ! ( *fptr = fopen( szFileName, "r" ) ) )
  1320. {
  1321. if( pSearch )
  1322. {
  1323. FILENAME *pFileName =SplitFilename( szFileName );
  1324. char szFName[ _POSIX_PATH_MAX ]; /* filename to parse */
  1325. pFileName->name =szFileName;
  1326. pFileName->extension =NULL;
  1327. while( pSearch && !*fptr )
  1328. {
  1329. pFileName->path =pSearch->szPath;
  1330. MakeFilename( szFName, pFileName );
  1331. if( ! ( *fptr = fopen( szFName, "r" ) ) )
  1332. {
  1333. pSearch = pSearch->pNext;
  1334. if( ! pSearch )
  1335. return 0;
  1336. }
  1337. }
  1338. _xfree( pFileName );
  1339. }
  1340. else
  1341. return 0;
  1342. }
  1343. return 1;
  1344. }