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.

218 lines
4.5 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name :
  4. parse.hxx
  5. Abstract:
  6. This class is used to parse a line in the inf file
  7. Author:
  8. Christopher Achille (cachille)
  9. Project:
  10. Internet Services Setup
  11. Revision History:
  12. June 2001: Created
  13. --*/
  14. #include "stdafx.h"
  15. #include "Parse.hxx"
  16. // function: FindNextOccurance
  17. //
  18. // Find the next occurance of a character. This will parse through a line,
  19. // and find the next occurance of a character that is not inside '(','{'
  20. // or '"'.
  21. //
  22. // Parameters:
  23. // szLine - The Line to parse, at the position you want to start the search
  24. // chTerminator - The character that you are looking for
  25. //
  26. // Return
  27. // NULL - Could not find character
  28. // pointer - the address of that terminator character
  29. LPTSTR
  30. CParse::FindNextOccurance(LPTSTR szLine, TCHAR chTerminator)
  31. {
  32. DWORD dwParen = 0;
  33. DWORD dwCurlyBraces = 0;
  34. BOOL bQuotes = FALSE;
  35. if (!szLine)
  36. {
  37. ASSERT(szLine);
  38. return NULL;
  39. }
  40. while (*szLine != '\0')
  41. {
  42. switch (*szLine)
  43. {
  44. case '"':
  45. bQuotes = !bQuotes;
  46. break;
  47. case '(':
  48. dwParen++;
  49. break;
  50. case ')':
  51. dwParen--;
  52. break;
  53. case '{':
  54. dwCurlyBraces++;
  55. break;
  56. case '}':
  57. dwCurlyBraces--;
  58. break;
  59. default:
  60. break;
  61. }
  62. if ( ( *szLine == chTerminator) &&
  63. ( dwParen == 0 ) &&
  64. ( dwCurlyBraces == 0 ) &&
  65. ( bQuotes == FALSE )
  66. )
  67. {
  68. return szLine;
  69. }
  70. szLine++;
  71. }
  72. return NULL;
  73. }
  74. // function: ParseLine
  75. //
  76. // Parse the line that has been send in, and interpret it accordingly, calling
  77. // the appropriate functions
  78. //
  79. // Parameters:
  80. // szLine - The line to be interpreted. THIS WILL BE MODIFIED accordingly, and
  81. // may not be returned in the state it was sent in. This paremeter must
  82. // not be static
  83. //
  84. // return:
  85. // TRUE - Result returned is TRUE
  86. // FALSE - Result retuened is FALSE
  87. BOOL
  88. CParse::ParseLine(CFunctionDictionary *pDict, LPTSTR szLine)
  89. {
  90. LPTSTR szThenStatement = NULL;
  91. LPTSTR szElseStatement = NULL;
  92. BOOL bRet = TRUE;
  93. if (!szLine)
  94. {
  95. ASSERT(!szLine);
  96. return FALSE;
  97. }
  98. szThenStatement = FindNextOccurance(szLine,'?');
  99. if ( szThenStatement )
  100. {
  101. szElseStatement = FindNextOccurance(szThenStatement,':');
  102. if ( szElseStatement )
  103. {
  104. *szElseStatement = '\0';
  105. }
  106. *szThenStatement = '\0';
  107. }
  108. if (szThenStatement || szElseStatement)
  109. {
  110. // If we could break this line apart, then lets try to break it apart again
  111. bRet = ParseLine(pDict, szLine);
  112. }
  113. else
  114. if ( LPTSTR szOpenCurly = _tcschr( szLine, _T('{') ) )
  115. {
  116. // If this line contains { }, it needs further breaking apart
  117. LPTSTR szCloseCurly = _tcsrchr( szOpenCurly, _T('}') );
  118. if ( szCloseCurly )
  119. {
  120. *szCloseCurly = '\0';
  121. bRet = ParseLine(pDict, szOpenCurly+1);
  122. *szCloseCurly = '}';
  123. }
  124. }
  125. else
  126. {
  127. bRet = CallFunction(pDict, szLine);
  128. }
  129. if ( (bRet) && (szThenStatement) )
  130. {
  131. // Since this returned TRUE, call the Then part
  132. ParseLine(pDict,szThenStatement+1);
  133. }
  134. if ( (!bRet) && (szElseStatement) )
  135. {
  136. // Since this returned FALSE, call the Else part
  137. ParseLine(pDict,szElseStatement+1);
  138. }
  139. if (szThenStatement)
  140. {
  141. *szThenStatement = '?';
  142. }
  143. if (szElseStatement)
  144. {
  145. *szElseStatement = ':';
  146. }
  147. return bRet;
  148. }
  149. // function: CallFunction
  150. //
  151. // This function calls the appropriate function for this szLine
  152. //
  153. // Parameters:
  154. // szLine - The function to call with its parameters
  155. //
  156. // Return:
  157. // TRUE: The function returned true
  158. // FALSE: The function returned false
  159. //
  160. BOOL
  161. CParse::CallFunction(CFunctionDictionary *pDict, LPTSTR szLine)
  162. {
  163. LPTSTR szFunctionName = szLine;
  164. LPTSTR szOpenParen = _tcschr( szLine, _T('(') );
  165. LPTSTR szCloseParen = szOpenParen ? _tcsrchr( szOpenParen, _T(')') ) : NULL;
  166. LPTSTR szParameters = szOpenParen + 1;
  167. BOOL bRet;
  168. if (!szOpenParen || !szCloseParen)
  169. {
  170. // Report error, this function had no parameters!
  171. return FALSE;
  172. }
  173. *szOpenParen = '\0';
  174. *szCloseParen = '\0';
  175. bRet = pDict->CallFunction(szFunctionName, szParameters);
  176. *szOpenParen = '(';
  177. *szCloseParen = ')';
  178. return bRet;
  179. }