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.

187 lines
4.6 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. /*************************************************************************
  3. *
  4. * QUERY.C
  5. * This module is the QUERY utility code.
  6. *
  7. *
  8. *************************************************************************/
  9. #include <stdio.h>
  10. #include <windows.h>
  11. #include <winstaw.h>
  12. #include <regapi.h>
  13. #include <stdlib.h>
  14. #include <time.h>
  15. #include <utilsub.h>
  16. #include <process.h>
  17. #include <string.h>
  18. #include <malloc.h>
  19. #include <locale.h>
  20. #include <printfoa.h>
  21. #include <winnlsp.h>
  22. #include "query.h"
  23. // max length of the locale string
  24. #define MAX_LOCALE_STRING 64
  25. /*-----------------------------------------------------------------------
  26. -- Supported commands (now obtained from registry)
  27. ------------------------------------------------------------------------*/
  28. PPROGRAMCALL pProgList = NULL;
  29. /*
  30. * Local function prototypes.
  31. */
  32. void Usage( BOOLEAN bError );
  33. /*************************************************************************
  34. *
  35. * main
  36. * Main function and entry point of the text-based query
  37. * menu utility.
  38. *
  39. * ENTRY:
  40. * argc (input)
  41. * count of the command line arguments.
  42. * argv (input)
  43. * vector of strings containing the command line arguments;
  44. * (not used due to always being ANSI strings).
  45. *
  46. * EXIT
  47. * (int) exit code: SUCCESS for success; FAILURE for error.
  48. *
  49. *************************************************************************/
  50. int __cdecl
  51. main( INT argc,
  52. CHAR **argv )
  53. {
  54. PWCHAR arg, *argvW;
  55. PPROGRAMCALL pProg, pProgramCall = NULL;
  56. int len, j, status = FAILURE;
  57. LONG regstatus;
  58. WCHAR wszString[MAX_LOCALE_STRING + 1];
  59. setlocale(LC_ALL, ".OCP");
  60. // We don't want LC_CTYPE set the same as the others or else we will see
  61. // garbage output in the localized version, so we need to explicitly
  62. // set it to correct console output code page
  63. _snwprintf(wszString, sizeof(wszString)/sizeof(WCHAR), L".%d", GetConsoleOutputCP());
  64. wszString[sizeof(wszString)/sizeof(WCHAR) - 1] = L'\0';
  65. _wsetlocale(LC_CTYPE, wszString);
  66. SetThreadUILanguage(0);
  67. /*
  68. * Obtain the supported QUERY commands from registry.
  69. */
  70. if ( (regstatus =
  71. RegQueryUtilityCommandList( UTILITY_REG_NAME_QUERY, &pProgList ))
  72. != ERROR_SUCCESS ) {
  73. ErrorPrintf(IDS_ERROR_REGISTRY_FAILURE, UTILITY_NAME, regstatus);
  74. goto exit;
  75. }
  76. /*
  77. * Massage the command line.
  78. */
  79. argvW = MassageCommandLine((DWORD)argc);
  80. if (argvW == NULL) {
  81. ErrorPrintf(IDS_ERROR_MALLOC);
  82. goto exit;
  83. }
  84. /*
  85. * Check for valid utility name and execute.
  86. */
  87. if ( argc > 1 && *(argvW[1]) ) {
  88. len = wcslen(arg = argvW[1]);
  89. for ( pProg = pProgList->pFirst; pProg != NULL; pProg = pProg->pNext ) {
  90. if ( (len >= pProg->CommandLen) &&
  91. !_wcsnicmp( arg, pProg->Command, len ) ) {
  92. pProgramCall = pProg;
  93. break;
  94. }
  95. }
  96. if ( pProgramCall ) {
  97. if ( ExecProgram(pProgramCall, argc - 2, &argvW[2]) )
  98. goto exit;
  99. } else if ( ((arg[0] == L'-') || (arg[0] == L'/')) &&
  100. (arg[1] == L'?') ) {
  101. /*
  102. * Help requested.
  103. */
  104. Usage(FALSE);
  105. status = SUCCESS;
  106. goto exit;
  107. } else {
  108. /*
  109. * Bad command line.
  110. */
  111. Usage(TRUE);
  112. goto exit;
  113. }
  114. } else {
  115. /*
  116. * Nothing on command line.
  117. */
  118. Usage(TRUE);
  119. goto exit;
  120. }
  121. exit:
  122. if ( pProgList )
  123. RegFreeUtilityCommandList(pProgList); // let's be tidy
  124. return(status);
  125. } /* main() */
  126. /*******************************************************************************
  127. *
  128. * Usage
  129. *
  130. * Output the usage message for this utility.
  131. *
  132. * ENTRY:
  133. * bError (input)
  134. * TRUE if the 'invalid parameter(s)' message should preceed the usage
  135. * message and the output go to stderr; FALSE for no such error
  136. * string and output goes to stdout.
  137. *
  138. * EXIT:
  139. *
  140. *
  141. ******************************************************************************/
  142. void
  143. Usage( BOOLEAN bError )
  144. {
  145. if ( bError ) {
  146. ErrorPrintf(IDS_ERROR_INVALID_PARAMETERS);
  147. }
  148. ProgramUsage(UTILITY_NAME, pProgList, bError);
  149. } /* Usage() */