Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
6.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2001
  5. //
  6. // File: rxutil.cpp
  7. //
  8. // Contents: Regular expression based helper functions
  9. //
  10. // History: 1-May-2001 kumarp created
  11. //
  12. //--------------------------------------------------------------------------
  13. #include "pch.cxx"
  14. #pragma hdrstop
  15. #pragma warning( disable : 4018 )
  16. #define assert(a)
  17. #include "syntax.h"
  18. #include "regexpr.h"
  19. using namespace regex;
  20. typedef std::vector< rpattern_c* > rpattern_c_ptr_vector;
  21. typedef std::vector< UINT > uint_vector;
  22. #define MAX_LINE_SIZE 512
  23. rpattern_c_ptr_vector* g_pRegexPatterns = NULL;
  24. uint_vector* g_uiGroupNumbers = NULL;
  25. DWORD
  26. InitRegexPatterns(
  27. IN PCWSTR pszFileName,
  28. OUT rpattern_c_ptr_vector **ppRegexPatterns,
  29. OUT uint_vector **ppGroupNumbers
  30. )
  31. {
  32. FILE *fp;
  33. WCHAR szLine[MAX_LINE_SIZE+1];
  34. UINT GroupNumber;
  35. UINT NumItemsScanned, NumCharsToSkip;
  36. UINT cPattern = 0;
  37. DWORD dwError = NO_ERROR;
  38. rpattern_c* pPattern;
  39. PCWSTR pszPattern;
  40. UINT uiLen;
  41. *ppRegexPatterns = new rpattern_c_ptr_vector;
  42. if ( !*ppRegexPatterns )
  43. {
  44. dwError = ERROR_NOT_ENOUGH_MEMORY;
  45. goto Cleanup;
  46. }
  47. *ppGroupNumbers = new uint_vector;
  48. if ( !*ppGroupNumbers )
  49. {
  50. dwError = ERROR_NOT_ENOUGH_MEMORY;
  51. goto Cleanup;
  52. }
  53. fp = _wfopen( pszFileName, L"r" );
  54. if ( fp )
  55. {
  56. while( fgetws( szLine, MAX_LINE_SIZE, fp ) )
  57. {
  58. if ( szLine[0] == L';' )
  59. {
  60. // ignore comments
  61. continue;
  62. }
  63. uiLen = wcslen(szLine);
  64. //
  65. // get rid of the trailing \n in the line
  66. //
  67. szLine[uiLen-1] = 0;
  68. NumItemsScanned = swscanf( szLine, L"%d", &GroupNumber );
  69. if ( NumItemsScanned == 1 )
  70. {
  71. //
  72. // skip the leading number so that we know where the
  73. // pattern starts
  74. //
  75. NumCharsToSkip = wcsspn( szLine, L"0123456789 \t" );
  76. pszPattern = szLine + NumCharsToSkip;
  77. pPattern = new rpattern_c( pszPattern );
  78. //wprintf( L"%02d: %s\n", cPattern++, pszPattern );
  79. g_pRegexPatterns->push_back(pPattern);
  80. g_uiGroupNumbers->push_back( GroupNumber );
  81. }
  82. else
  83. {
  84. dwError = ERROR_INVALID_DATA;
  85. break;
  86. }
  87. }
  88. }
  89. else
  90. {
  91. dwError = ERROR_FILE_NOT_FOUND;
  92. }
  93. Cleanup:
  94. return dwError;
  95. }
  96. EXTERN_C
  97. BOOL
  98. ParseLine(
  99. IN PCWSTR szLine,
  100. OUT PUINT pMatchStart,
  101. OUT PUINT pMatchLength
  102. )
  103. {
  104. BOOL fResult = FALSE;
  105. regexpr::backref_vector_c backrefs;
  106. UINT g;
  107. regexpr::backref_type br;
  108. for (int i=0; i < g_pRegexPatterns->size(); i++)
  109. {
  110. //wprintf(L"trying %02d for '%s'\n", i, szLine);
  111. //wprintf(L"rp[%02d]: %s\n", i, sz);
  112. br = regexpr::match( szLine, *(*g_pRegexPatterns)[i], &backrefs );
  113. g = (*g_uiGroupNumbers)[i];
  114. if( br && backrefs[g] )
  115. {
  116. *pMatchStart = backrefs[g].first - szLine;
  117. *pMatchLength = backrefs[g].second - backrefs[g].first;
  118. fResult = TRUE;
  119. break;
  120. }
  121. }
  122. return fResult;
  123. }
  124. #ifndef GENFLT_FILE_INIT
  125. PCWSTR g_szPatterns[] =
  126. {
  127. // L"2 ^[ \t]*\\([ \t]*def(un|macro|subst|advice)[ \t]+([*+a-zA-Z0-9_-]+)[ \t]*\\(",
  128. L"2 ^[ \t]*\\([ \t]*def(un|macro|subst|advice)[ \t]+([^ \t\n;]+)[ \t]*\\(",
  129. // L"2 ^[ \t]*\\([ \t]*def(var|custom|group|face|const|ine-skeleton|alias)[ \t]+([*+a-zA-Z0-9_-]+)",
  130. L"2 ^[ \t]*\\([ \t]*def(var|custom|group|face|const|ine-skeleton|alias)[ \t]+([^ \t\n;]+)",
  131. L"1 ^[ \t]*\\([ \t]*defalias[ \t]+'([^ \t\n;]+)",
  132. #ifdef GENFLT_PERL_SUPPORT
  133. L"1 ^[ \t]*sub[ \t]+([a-zA-Z0-9_]+)[ \t]*",
  134. L"1 ^[ \t]*package[ \t]+([a-zA-Z0-9_]+)[ \t]*;",
  135. #endif
  136. // L"2 ^[ \\t]*\\([ \\t]*def(un|macro|subst|advice)[ \\t]+([+a-zA-Z0-9_-]+)[ \\t]*\\(",
  137. // L"2 ^[ \\t]*\\([ \\t]*def(var|custom|group|face|const|ine-skeleton|alias)[ \\t]+([+a-zA-Z0-9_-]+)",
  138. // #ifdef GENFLT_PERL_SUPPORT
  139. // L"1 ^[ \\t]*sub[ \\t]+([a-zA-Z0-9_]+)[ \\t]*",
  140. // L"1 ^[ \\t]*package[ \\t]+([a-zA-Z0-9_]+)[ \\t]*;",
  141. // #endif
  142. };
  143. int c_cPatterns = sizeof(g_szPatterns) / sizeof(PCWSTR);
  144. DWORD
  145. InitRegexPatternsList(
  146. OUT rpattern_c_ptr_vector **ppRegexPatterns,
  147. OUT uint_vector **ppGroupNumbers
  148. )
  149. {
  150. UINT GroupNumber;
  151. UINT NumItemsScanned, NumCharsToSkip;
  152. UINT cPattern = 0;
  153. DWORD dwError = NO_ERROR;
  154. rpattern_c* pPattern;
  155. PCWSTR pszPattern;
  156. UINT uiLen;
  157. PCWSTR pszPatternSpec = NULL;
  158. *ppRegexPatterns = new rpattern_c_ptr_vector;
  159. if ( !*ppRegexPatterns )
  160. {
  161. dwError = ERROR_NOT_ENOUGH_MEMORY;
  162. goto Cleanup;
  163. }
  164. *ppGroupNumbers = new uint_vector;
  165. if ( !*ppGroupNumbers )
  166. {
  167. dwError = ERROR_NOT_ENOUGH_MEMORY;
  168. goto Cleanup;
  169. }
  170. for (int i=0; i < c_cPatterns; i++)
  171. {
  172. pszPatternSpec = g_szPatterns[i];
  173. uiLen = wcslen(pszPatternSpec);
  174. NumItemsScanned = swscanf( pszPatternSpec, L"%d", &GroupNumber );
  175. if ( NumItemsScanned == 1 )
  176. {
  177. //
  178. // skip the leading number so that we know where the
  179. // pattern starts
  180. //
  181. NumCharsToSkip = wcsspn( pszPatternSpec, L"0123456789 \t" );
  182. pszPattern = pszPatternSpec + NumCharsToSkip;
  183. pPattern = new rpattern_c( pszPattern );
  184. #if DBG
  185. //DbgPrint( "InitRegexPatternsList: %02d: %ws\n", cPattern++, pszPattern );
  186. #endif
  187. g_pRegexPatterns->push_back(pPattern);
  188. g_uiGroupNumbers->push_back( GroupNumber );
  189. }
  190. else
  191. {
  192. dwError = ERROR_INVALID_DATA;
  193. break;
  194. }
  195. }
  196. Cleanup:
  197. return dwError;
  198. }
  199. #endif // GENFLT_FILE_INIT
  200. EXTERN_C
  201. DWORD RxInit()
  202. {
  203. static BOOL fRxInitialized = FALSE;
  204. DWORD dwError = NO_ERROR;
  205. if ( fRxInitialized )
  206. {
  207. return NO_ERROR;
  208. }
  209. #ifdef GENFLT_FILE_INIT
  210. dwError = InitRegexPatterns(
  211. L"c:\\rx.ini",
  212. &g_pRegexPatterns,
  213. &g_uiGroupNumbers );
  214. #else
  215. dwError = InitRegexPatternsList(
  216. &g_pRegexPatterns,
  217. &g_uiGroupNumbers );
  218. #endif
  219. if ( dwError == NO_ERROR )
  220. {
  221. fRxInitialized = TRUE;
  222. }
  223. return dwError;
  224. }