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.

133 lines
4.6 KiB

  1. #include <precomp.h>
  2. #include "ErrCtrl.h"
  3. #include "PrmDescr.h"
  4. #include "utils.h"
  5. //-------------------------------------------------------
  6. // Parser code for the command line parameters.
  7. // Parameters:
  8. // [IN] nArgc: number of command line parameters
  9. // [IN] pwszArgv: array of pointers to the command line parameteres
  10. // [OUT] pnErrArgi: index to the faulty parameter (in case of error)
  11. // Returns:
  12. // ERROR_SUCCESS in case everything went fine
  13. // any win32 error in case of failure. On error, pnErrArgi indicates
  14. // the faulty parameter.
  15. DWORD
  16. WZCToolParse(UINT nPrmC, LPWSTR *pwszPrmV, UINT *pnErrPrmI)
  17. {
  18. DWORD dwErr = ERROR_SUCCESS;
  19. UINT nPrmI = 0;
  20. for (nPrmI = 0; dwErr == ERROR_SUCCESS && nPrmI < nPrmC; nPrmI++)
  21. {
  22. LPWSTR pArg;
  23. PHASH_NODE pPrmNode;
  24. PPARAM_DESCR pPrmDescr;
  25. // get the pointer to the parameter's argument, if any
  26. pArg = wcschr(pwszPrmV[nPrmI], L'=');
  27. if (pArg != NULL)
  28. *pArg = L'\0';
  29. // get the hash node for the parameter, if it is present in the hash
  30. pPrmNode = NULL;
  31. dwErr = HshQueryObjectRef(
  32. g_PDHash.pRoot,
  33. pwszPrmV[nPrmI],
  34. &pPrmNode);
  35. // restore the '=' in the parameter string
  36. if (pArg != NULL)
  37. {
  38. *pArg = L'=';
  39. pArg++;
  40. }
  41. // if the parameter is valid (found in the hash)
  42. if (dwErr == ERROR_SUCCESS)
  43. {
  44. // get the pointer to the parameter descriptor
  45. pPrmDescr = (PPARAM_DESCR)pPrmNode->pObject;
  46. // check if this is not a duplicated parameter
  47. if (g_PDData.dwExistingParams & pPrmDescr->nParamID)
  48. // if it is, set the error
  49. dwErr = ERROR_DUPLICATE_TAG;
  50. else
  51. {
  52. // else mark the parameter does exist now
  53. g_PDData.dwExistingParams |= pPrmDescr->nParamID;
  54. // if this is a command parameter and one has been found already,
  55. // only accepts one command per call.
  56. if (pPrmDescr->pfnCommand != NULL && g_PDData.pfnCommand != NULL)
  57. dwErr = ERROR_INVALID_FUNCTION;
  58. }
  59. }
  60. // if everything is ok so far and this param has an argument..
  61. if (dwErr == ERROR_SUCCESS && pArg != NULL)
  62. {
  63. // ..if the param does not support arguments..
  64. if (pPrmDescr->pfnArgParser == NULL)
  65. // set the error
  66. dwErr = ERROR_NOT_SUPPORTED;
  67. else
  68. // otherwise have the param to parse its argument.
  69. dwErr = pPrmDescr->pfnArgParser(&g_PDData, pPrmDescr, pArg);
  70. }
  71. // if the parameter is entirely successful parsing its argument (if any) and is a
  72. // command parameter (it is the first) encountered, save its command handler
  73. if (dwErr == ERROR_SUCCESS && pPrmDescr->pfnCommand != NULL)
  74. g_PDData.pfnCommand = pPrmDescr->pfnCommand;
  75. }
  76. if (dwErr != ERROR_SUCCESS && pnErrPrmI != NULL)
  77. *pnErrPrmI = nPrmI;
  78. return dwErr;
  79. }
  80. void _cdecl main()
  81. {
  82. LPWSTR *pwszPrmV = NULL;
  83. UINT nPrmC = 0;
  84. UINT nErrPrmI;
  85. DWORD dwErr;
  86. // get OS version
  87. g_verInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  88. if (!GetVersionEx((LPOSVERSIONINFO)&g_verInfoEx))
  89. _Asrt(FALSE, L"Error %d determining the OS version\n", GetLastError());
  90. // get the command line in WCHAR
  91. pwszPrmV = CommandLineToArgvW(GetCommandLineW(), &nPrmC);
  92. _Asrt(nPrmC >= 2, L"Invalid parameters count (%d)\n", nPrmC);
  93. _Asrt(pwszPrmV != NULL, L"Invalid parameters array (%p)\n", pwszPrmV);
  94. // initialize and fill in the parameter's list,
  95. // initialize the parameter descriptor data
  96. dwErr = PDInitialize();
  97. _Asrt(dwErr == ERROR_SUCCESS, L"Unexpected error (%d) in param hash initialization.\n", dwErr);
  98. // scan command line parameters
  99. dwErr = WZCToolParse(nPrmC-1, pwszPrmV+1, &nErrPrmI);
  100. _Asrt(dwErr == ERROR_SUCCESS, L"Error %d encountered while parsing parameter \"%s\"\n",
  101. dwErr,
  102. dwErr != ERROR_SUCCESS ? pwszPrmV[nErrPrmI] : NULL);
  103. _Asrt(g_PDData.pfnCommand != NULL,
  104. L"Noop: No action parameter provided.\n");
  105. dwErr = g_PDData.pfnCommand(&g_PDData);
  106. _Asrt(dwErr == ERROR_SUCCESS,L"Error %d encountered while executing the command.\n",dwErr);
  107. // cleanout whatever resources we might have had allocated
  108. PDDestroy();
  109. // set the %errorlevel% environment variable
  110. exit(dwErr);
  111. }