Source code of Windows XP (NT5)
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.

242 lines
7.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 2000
  6. //
  7. // File: main.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #pragma hdrstop
  12. #include <stdio.h>
  13. #include "util.h"
  14. #include "cscpin.h"
  15. #include "console.h"
  16. #include "exitcode.h"
  17. void
  18. ShowUsage(
  19. void
  20. )
  21. {
  22. fwprintf(stderr, L"\aUsage: cscpin -p | -u <filename> | -f <listfile> [-v] [-l <logfile>]\n\n");
  23. fwprintf(stderr, L"\t-p = Pin files.\n");
  24. fwprintf(stderr, L"\t-u = Unpin files.\n");
  25. fwprintf(stderr, L"\t-f = Process paths in <listfile>.\n");
  26. fwprintf(stderr, L"\t-l = Log results to <logfile>.\n");
  27. fwprintf(stderr, L"\t-v = Verbose mode.\n");
  28. fwprintf(stderr, L"\t<filename> = name of file or folder to pin/unpin.\n\n");
  29. fwprintf(stderr, L"Examples:\n\n");
  30. fwprintf(stderr, L"\tcscpin -v -p \\\\server\\share\\dir\n\n");
  31. fwprintf(stderr, L"\tcscpin -u \\\\server\\share2\\dir\\foo.txt\n\n");
  32. fwprintf(stderr, L"\tcscpin -f pinthese.txt -l cscpin.log\n\n");
  33. }
  34. int __cdecl
  35. wmain(
  36. int argc,
  37. WCHAR **argv
  38. )
  39. {
  40. const WCHAR CH_DASH = '-';
  41. const WCHAR CH_SLASH = '/';
  42. CSCPIN_INFO info;
  43. ZeroMemory(&info, sizeof(info));
  44. const DWORD OPTION_PIN_OR_UNPIN = 0x00000001;
  45. const DWORD OPTION_VERBOSE = 0x00000002;
  46. const DWORD OPTION_INPUTFILE = 0x00000004;
  47. const DWORD OPTION_LOGFILE = 0x00000008;
  48. const DWORD OPTION_ALL = (OPTION_PIN_OR_UNPIN |
  49. OPTION_VERBOSE |
  50. OPTION_LOGFILE |
  51. OPTION_INPUTFILE);
  52. DWORD dwOptions = 0;
  53. bool bShowUsage = false;
  54. for (int i = 1; i < argc && !bShowUsage && (OPTION_ALL != dwOptions); i++)
  55. {
  56. if (CH_DASH == argv[i][0] || CH_SLASH == argv[i][0])
  57. {
  58. switch(argv[i][1])
  59. {
  60. case L'U':
  61. case L'u':
  62. if (0 == (OPTION_PIN_OR_UNPIN & dwOptions))
  63. {
  64. info.bPin = FALSE;
  65. info.bPinDefaultSet = TRUE;
  66. dwOptions |= OPTION_PIN_OR_UNPIN;
  67. }
  68. else
  69. {
  70. fwprintf(stderr, L"Only one [-u] or [-p] allowed.\n\n");
  71. bShowUsage = true;
  72. }
  73. break;
  74. case L'P':
  75. case L'p':
  76. if (0 == (OPTION_PIN_OR_UNPIN & dwOptions))
  77. {
  78. info.bPin = TRUE;
  79. info.bPinDefaultSet = TRUE;
  80. dwOptions |= OPTION_PIN_OR_UNPIN;
  81. }
  82. else
  83. {
  84. fwprintf(stderr, L"Only one [-u] or [-p] allowed.\n\n");
  85. bShowUsage = true;
  86. }
  87. break;
  88. case L'V':
  89. case L'v':
  90. info.bVerbose = TRUE;
  91. dwOptions |= OPTION_VERBOSE;
  92. break;
  93. case L'F':
  94. case L'f':
  95. if (0 == (OPTION_INPUTFILE & dwOptions))
  96. {
  97. if (++i < argc)
  98. {
  99. if (NULL == info.pszFile)
  100. {
  101. info.pszFile = argv[i];
  102. info.bUseListFile = TRUE;
  103. dwOptions |= OPTION_INPUTFILE;
  104. }
  105. else
  106. {
  107. fwprintf(stderr, L"Specify a list file using -F or a single file, not both.\n\n");
  108. bShowUsage = true;
  109. }
  110. }
  111. else
  112. {
  113. fwprintf(stderr, L"<filename> expected following -F\n\n");
  114. bShowUsage = true;
  115. }
  116. }
  117. else
  118. {
  119. fwprintf(stderr, L"Multiple input files specified.\n\n");
  120. bShowUsage = true;
  121. }
  122. break;
  123. case L'L':
  124. case L'l':
  125. if (0 == (OPTION_LOGFILE & dwOptions))
  126. {
  127. if (++i < argc)
  128. {
  129. info.pszLogFile = argv[i];
  130. dwOptions |= OPTION_LOGFILE;
  131. }
  132. else
  133. {
  134. fwprintf(stderr, L"<filename> expected following -L\n\n");
  135. bShowUsage = true;
  136. }
  137. }
  138. else
  139. {
  140. fwprintf(stderr, L"Multiple -L options specified.\n\n");
  141. bShowUsage = true;
  142. }
  143. break;
  144. default:
  145. fwprintf(stderr, L"Unknown option '%c' specified.\n\n", argv[i][1]);
  146. SetExitCode(CSCPIN_EXIT_INVALID_PARAMETER);
  147. bShowUsage = true;
  148. }
  149. }
  150. else if (NULL == info.pszFile && 0 == (OPTION_INPUTFILE & dwOptions))
  151. {
  152. //
  153. // Assume a file path without a cmd line switch is a single
  154. // file to be pinned or unpinned.
  155. //
  156. info.pszFile = argv[i];
  157. info.bUseListFile = FALSE;
  158. dwOptions |= OPTION_INPUTFILE;
  159. }
  160. else
  161. {
  162. fwprintf(stderr, L"Multiple input files specified.\n\n");
  163. bShowUsage = true;
  164. }
  165. }
  166. //
  167. // Now validate what the user entered.
  168. //
  169. if (0 == (OPTION_INPUTFILE & dwOptions))
  170. {
  171. fwprintf(stderr, L"<filename> or -f <listfile> argument required.\n\n");
  172. bShowUsage = true;
  173. }
  174. else
  175. {
  176. if (!info.bUseListFile)
  177. {
  178. if (!info.bPinDefaultSet)
  179. {
  180. //
  181. // Not providing a listing file and didn't indicate
  182. // 'pin' or 'unpin' on the command line.
  183. //
  184. fwprintf(stderr, L"-p or -u argument required.\n\n");
  185. bShowUsage = true;
  186. }
  187. }
  188. }
  189. if (bShowUsage)
  190. {
  191. //
  192. // User input is not 100% valid.
  193. //
  194. SetExitCode(CSCPIN_EXIT_INVALID_PARAMETER);
  195. ShowUsage();
  196. }
  197. else
  198. {
  199. //
  200. // User input is valid.
  201. //
  202. ConsoleInitialize();
  203. HRESULT hr = CoInitialize(NULL);
  204. if (SUCCEEDED(hr))
  205. {
  206. CCscPin cscpin(info);
  207. cscpin.Run();
  208. CoUninitialize();
  209. }
  210. if (ConsoleHasCtrlEventOccured())
  211. {
  212. SetExitCode(CSCPIN_EXIT_APPLICATION_ABORT);
  213. }
  214. ConsoleUninitialize();
  215. }
  216. #if DBG
  217. fwprintf(stderr, L"Exit code = %d\n", GetExitCode());
  218. #endif
  219. return GetExitCode();
  220. }