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.

323 lines
7.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: M A I N . C P P
  7. //
  8. // Contents: Code to provide a simple cmdline interface to
  9. // the sample code functions
  10. //
  11. // Notes: The code in this file is not required to access any
  12. // netcfg functionality. It merely provides a simple cmdline
  13. // interface to the sample code functions provided in
  14. // file snetcfg.cpp.
  15. //
  16. // Author: kumarp 28-September-98
  17. //
  18. //----------------------------------------------------------------------------
  19. #include "pch.h"
  20. #pragma hdrstop
  21. #include "snetcfg.h"
  22. // ----------------------------------------------------------------------
  23. // Global vars
  24. //
  25. BOOL g_fVerbose=FALSE;
  26. static WCHAR* optarg;
  27. // ----------------------------------------------------------------------
  28. void ShowUsage();
  29. WCHAR getopt(ULONG Argc, WCHAR* Argv[], WCHAR* Opts);
  30. enum NetClass MapToNetClass(WCHAR ch);
  31. // ----------------------------------------------------------------------
  32. //
  33. // Function: wmain
  34. //
  35. // Purpose: The main function
  36. //
  37. // Arguments: standard main args
  38. //
  39. // Returns: 0 on success, non-zero otherwise
  40. //
  41. // Author: kumarp 25-December-97
  42. //
  43. // Notes:
  44. //
  45. EXTERN_C int __cdecl wmain(int argc, WCHAR* argv[])
  46. {
  47. HRESULT hr=S_OK;
  48. WCHAR ch;
  49. enum NetClass nc=NC_Unknown;
  50. // use simple cmd line parsing to get parameters for actions
  51. // we want to perform. the order of parameters supplied is significant.
  52. static const WCHAR c_szValidOptions[] =
  53. L"hH?c:C:l:L:i:I:u:U:vVp:P:s:S:b:B:q:Q:";
  54. WCHAR szFileFullPath[MAX_PATH+1];
  55. PWSTR szFileComponent;
  56. while (_istprint(ch = getopt(argc, argv, (WCHAR*) c_szValidOptions)))
  57. {
  58. switch (tolower(ch))
  59. {
  60. case 'q':
  61. FindIfComponentInstalled(optarg);
  62. break;
  63. case 'b':
  64. hr = HrShowBindingPathsOfComponent(optarg);
  65. break;
  66. case 'c':
  67. nc = MapToNetClass(optarg[0]);
  68. break;
  69. case 'l':
  70. wcscpy(szFileFullPath, optarg);
  71. break;
  72. case 'i':
  73. if (nc != NC_Unknown)
  74. {
  75. hr = HrInstallNetComponent(optarg, nc, szFileFullPath);
  76. }
  77. else
  78. {
  79. ShowUsage();
  80. exit(-1);
  81. }
  82. break;
  83. case 'u':
  84. hr = HrUninstallNetComponent(optarg);
  85. break;
  86. case 's':
  87. switch(tolower(optarg[0]))
  88. {
  89. case 'a':
  90. hr = HrShowNetAdapters();
  91. break;
  92. case 'n':
  93. hr = HrShowNetComponents();
  94. break;
  95. default:
  96. ShowUsage();
  97. exit(-1);
  98. break;
  99. }
  100. break;
  101. case 'v':
  102. g_fVerbose = TRUE;
  103. break;
  104. case EOF:
  105. break;
  106. default:
  107. case 'h':
  108. case '?':
  109. ShowUsage();
  110. exit(0);
  111. break;
  112. }
  113. }
  114. return hr;
  115. }
  116. //+---------------------------------------------------------------------------
  117. //
  118. // Function: MapToNetClass
  119. //
  120. // Purpose: Map a character to the corresponding net class enum
  121. //
  122. // Arguments:
  123. // ch [in] char to map
  124. //
  125. // Returns: enum for net class
  126. //
  127. // Author: kumarp 06-October-98
  128. //
  129. // Notes:
  130. //
  131. enum NetClass MapToNetClass(WCHAR ch)
  132. {
  133. switch(tolower(ch))
  134. {
  135. case 'a':
  136. return NC_NetAdapter;
  137. case 'p':
  138. return NC_NetProtocol;
  139. case 's':
  140. return NC_NetService;
  141. case 'c':
  142. return NC_NetClient;
  143. default:
  144. return NC_Unknown;
  145. }
  146. }
  147. // ----------------------------------------------------------------------
  148. //
  149. // Function: ShowUsage
  150. //
  151. // Purpose: Display program usage help
  152. //
  153. // Arguments: None
  154. //
  155. // Returns: None
  156. //
  157. // Author: kumarp 24-December-97
  158. //
  159. // Notes:
  160. //
  161. void ShowUsage()
  162. {
  163. static const WCHAR c_szUsage[] =
  164. L"snetcfg [-v] [-l <full-path-to-component-INF>] -c <p|s|c> -i <comp-id>\n"
  165. L" where,\n"
  166. L" -l\tprovides the location of INF\n"
  167. L" -c\tprovides the class of the component to be installed\n"
  168. L" \tp == Protocol, s == Service, c == Client\n"
  169. L" -i\tprovides the component ID\n\n"
  170. L" The arguments must be passed in the order shown.\n\n"
  171. L" Examples:\n"
  172. L" snetcfg -l c:\\oemdir\\foo.inf -c p -i foo\n"
  173. L" ...installs protocol 'foo' using c:\\oemdir\\foo.inf\n\n"
  174. L" snetcfg -c s -i MS_Server\n"
  175. L" ...installs service 'MS_Server'\n"
  176. L"\nOR\n\n"
  177. L"snetcfg [-v] -q <comp-id>\n"
  178. L" Example:\n"
  179. L" snetcfg -q MS_IPX\n"
  180. L" ...displays if component 'MS_IPX' is installed\n"
  181. L"\nOR\n\n"
  182. L"snetcfg [-v] -u <comp-id>\n"
  183. L" Example:\n"
  184. L" snetcfg -u MS_IPX\n"
  185. L" ...uninstalls component 'MS_IPX'\n"
  186. L"\nOR\n\n"
  187. L"snetcfg [-v] -s <a|n>\n"
  188. L" where,\n"
  189. L" -s\tprovides the type of components to show\n"
  190. L" \ta == adapters, n == net components\n"
  191. L" Examples:\n"
  192. L" snetcfg -s n\n"
  193. L" ...shows all installed net components\n\n"
  194. L"\n"
  195. L"\nOR\n\n"
  196. L"snetcfg [-v] -b <comp-id>\n"
  197. L" Examples:\n"
  198. L" snetcfg -b ms_tcpip\n"
  199. L" ...shows binding paths containing 'ms_tcpip'\n\n"
  200. L"\n"
  201. L"General Notes:\n"
  202. L" -v\t turns on the verbose mode\n"
  203. L" -?\t Displays this help\n"
  204. L"\n";
  205. _tprintf(c_szUsage);
  206. }
  207. //+---------------------------------------------------------------------------
  208. //
  209. // Function: getopt
  210. //
  211. // Purpose: Parse cmdline and return one argument each time
  212. // this function is called.
  213. //
  214. // Arguments:
  215. // Argc [in] standard main argc
  216. // Argv [in] standard main argv
  217. // Opts [in] valid options
  218. //
  219. // Returns:
  220. //
  221. // Author: kumarp 06-October-98
  222. //
  223. // Notes:
  224. //
  225. WCHAR getopt (ULONG Argc, WCHAR* Argv[], WCHAR* Opts)
  226. {
  227. static ULONG optind=1;
  228. static ULONG optcharind;
  229. static ULONG hyphen=0;
  230. WCHAR ch;
  231. WCHAR* indx;
  232. do {
  233. if (optind >= Argc) {
  234. return EOF;
  235. }
  236. ch = Argv[optind][optcharind++];
  237. if (ch == '\0') {
  238. optind++; optcharind=0;
  239. hyphen = 0;
  240. continue;
  241. }
  242. if ( hyphen || (ch == '-') || (ch == '/')) {
  243. if (!hyphen) {
  244. ch = Argv[optind][optcharind++];
  245. if (ch == '\0') {
  246. optind++;
  247. return EOF;
  248. }
  249. } else if (ch == '\0') {
  250. optind++;
  251. optcharind = 0;
  252. continue;
  253. }
  254. indx = wcschr(Opts, ch);
  255. if (indx == NULL) {
  256. continue;
  257. }
  258. if (*(indx+1) == ':') {
  259. if (Argv[optind][optcharind] != '\0'){
  260. optarg = &Argv[optind][optcharind];
  261. } else {
  262. if ((optind + 1) >= Argc ||
  263. (Argv[optind+1][0] == '-' ||
  264. Argv[optind+1][0] == '/' )) {
  265. return 0;
  266. }
  267. optarg = Argv[++optind];
  268. }
  269. optind++;
  270. hyphen = optcharind = 0;
  271. return ch;
  272. }
  273. hyphen = 1;
  274. return ch;
  275. } else {
  276. return EOF;
  277. }
  278. } while (1);
  279. }