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.

398 lines
8.1 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: main.cxx
  7. //
  8. // Contents: Main for oledscmd
  9. //
  10. // History: 01-Aug-96 t-danal created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "main.hxx"
  14. #include "disptabl.hxx"
  15. //-------------------------------------------------------------------------
  16. //
  17. // Local functions declarations
  18. //
  19. //-------------------------------------------------------------------------
  20. char *
  21. GetProgName(
  22. char *progname
  23. );
  24. void
  25. PrintUsage(
  26. char *szProgName
  27. );
  28. void
  29. PrintHelp(
  30. char *szProgName,
  31. char *szAction
  32. );
  33. void
  34. StartUp(
  35. );
  36. void
  37. Exit(
  38. int code
  39. );
  40. //-------------------------------------------------------------------------
  41. //
  42. // Local function definitions
  43. //
  44. //-------------------------------------------------------------------------
  45. char *
  46. GetProgName(
  47. char *progname
  48. )
  49. {
  50. return progname;
  51. }
  52. void
  53. PrintUsage(
  54. char *szProgName
  55. )
  56. {
  57. int i;
  58. int n;
  59. if (g_nDispTable) {
  60. n = g_nDispTable - 1;
  61. printf("usage: %s [", szProgName);
  62. for (i = 0; i < n; i++)
  63. printf(" %s |", g_DispTable[i].action);
  64. printf(" %s ]\n", g_DispTable[i].action);
  65. }
  66. if (g_nHelpTable) {
  67. n = g_nHelpTable - 1;
  68. printf("for help, use: %s [ ", szProgName);
  69. for (i = 0; i < n; i++)
  70. printf(" %s |", g_HelpTable[i]);
  71. printf(" %s ]\n", g_HelpTable[i]);
  72. }
  73. }
  74. void
  75. PrintHelp(
  76. char *szProgName,
  77. char *szAction
  78. )
  79. {
  80. if (!szAction)
  81. printf("Help for %s goes here.", szProgName);
  82. else
  83. printf("%s: Could not find help for %s", szProgName, szAction);
  84. }
  85. void
  86. MissingHelpText(
  87. char *szProgName,
  88. char *szAction
  89. )
  90. {
  91. if (!szAction)
  92. printf("%s: Missing help text", szProgName);
  93. else
  94. printf("%s: Missing help text for %s", szProgName, szAction);
  95. }
  96. void
  97. StartUp(
  98. )
  99. {
  100. HRESULT hr;
  101. hr = CoInitialize(NULL);
  102. if (FAILED(hr)) {
  103. printf("CoInitialize failed\n");
  104. Exit(1);
  105. }
  106. }
  107. void
  108. Exit(
  109. int code
  110. )
  111. {
  112. CoUninitialize();
  113. exit(code);
  114. }
  115. //-------------------------------------------------------------------------
  116. //
  117. // Global function definitions
  118. //
  119. //-------------------------------------------------------------------------
  120. void
  121. PrintUsage(
  122. char *szProgName,
  123. char *szActions,
  124. char *extra
  125. )
  126. {
  127. if (extra)
  128. printf("usage: %s %s %s\n", szProgName, szActions, extra);
  129. else
  130. printf("usage: %s %s\n", szProgName, szActions);
  131. }
  132. void
  133. PrintUsage(
  134. char *szProgName,
  135. char *szActions,
  136. DISPENTRY *DispTable,
  137. int nDispTable
  138. )
  139. {
  140. int i;
  141. int n;
  142. if (nDispTable) {
  143. n = nDispTable - 1;
  144. printf("usage: %s %s [", szProgName, szActions);
  145. for (i = 0; i < n; i++)
  146. printf(" %s |", DispTable[i].action);
  147. printf(" %s ]\n", DispTable[i].action);
  148. }
  149. if (g_nHelpTable) {
  150. n = g_nHelpTable - 1;
  151. printf("for help, use: %s %s [ ", szProgName, szActions);
  152. for (i = 0; i < n; i++)
  153. printf(" %s |", g_HelpTable[i]);
  154. printf(" %s ]\n", g_HelpTable[i]);
  155. }
  156. }
  157. BOOL
  158. IsHelp(
  159. char *szAction
  160. )
  161. {
  162. int i;
  163. for (i = 0; i < g_nHelpTable; i++)
  164. if (IsSameAction(szAction, g_HelpTable[i]))
  165. return TRUE;
  166. return FALSE;
  167. }
  168. BOOL
  169. IsValidAction(
  170. char *szAction,
  171. DISPENTRY *DispTable,
  172. int nDispTable
  173. )
  174. {
  175. int i;
  176. for (i = 0; i < nDispTable; i++)
  177. if (IsSameAction(szAction, DispTable[i].action))
  178. return TRUE;
  179. return FALSE;
  180. }
  181. BOOL
  182. IsSameAction(
  183. char *action1,
  184. char *action2
  185. )
  186. {
  187. if (action1 && action2) // neither is NULL
  188. return !_stricmp(action1, action2) ? TRUE : FALSE;
  189. else if (!action1 && !action2) // both are NULL
  190. return TRUE;
  191. else if (action1) // action2 is NULL
  192. return !*action1 ? TRUE : FALSE;
  193. else // action1 is NULL
  194. return !*action2 ? TRUE : FALSE;
  195. }
  196. BOOL
  197. DispatchHelp(
  198. DISPENTRY *DispTable,
  199. int nDispTable,
  200. char *szProgName,
  201. char *szPrevActions,
  202. char *szAction
  203. )
  204. {
  205. HELPFUNC help = NULL;
  206. BOOL found = FALSE;
  207. int i;
  208. for (i = 0; i < nDispTable; i++)
  209. if (IsSameAction(szAction, DispTable[i].action)) {
  210. help = DispTable[i].help;
  211. found = TRUE;
  212. break;
  213. }
  214. if (found) {
  215. char *szActions = AllocAction(szPrevActions, szAction);
  216. if (help)
  217. help(szProgName, szActions);
  218. else
  219. MissingHelpText(szProgName, szActions);
  220. FreeAction(szActions);
  221. return TRUE;
  222. }
  223. return FALSE;
  224. }
  225. int
  226. DispatchExec(
  227. DISPENTRY *DispTable,
  228. int nDispTable,
  229. char *szProgName,
  230. char *szPrevActions,
  231. char *szAction,
  232. int argc,
  233. char *argv[]
  234. )
  235. {
  236. EXECFUNC exec = NULL;
  237. int i;
  238. for (i = 0; i < nDispTable; i++)
  239. if (IsSameAction(szAction, DispTable[i].action)) {
  240. exec = DispTable[i].exec;
  241. break;
  242. }
  243. if (exec) {
  244. char *szActions = AllocAction(szPrevActions, szAction);
  245. int status = exec(szProgName, szActions, argc, argv);
  246. FreeAction(szActions);
  247. return status;
  248. }
  249. PrintUsage(szProgName, szPrevActions, DispTable, nDispTable);
  250. return 1;
  251. }
  252. char *
  253. AllocAction(
  254. char *action1,
  255. char *action2
  256. )
  257. {
  258. char *str;
  259. int len;
  260. int len1 = action1?strlen(action1):0;
  261. int len2 = action2?strlen(action2):0;
  262. if (len1 && len2) {
  263. str = (char *) malloc((len1 + len2 + 2) * sizeof(char));
  264. strcpy(str, action1);
  265. strcpy(str + len1, " ");
  266. strcpy(str + len1 + 1, action2);
  267. return str;
  268. }
  269. if (len1) {
  270. str = (char*) malloc((len1 + 1) * sizeof(char));
  271. strcpy(str, action1);
  272. return str;
  273. }
  274. if (len2) {
  275. str = (char*) malloc((len2 + 1) * sizeof(char));
  276. strcpy(str, action2);
  277. return str;
  278. }
  279. return NULL;
  280. }
  281. void
  282. FreeAction(
  283. char *action
  284. )
  285. {
  286. if (action)
  287. free(action);
  288. return;
  289. }
  290. BOOL
  291. DoHelp(
  292. char *szProgName,
  293. char *szPrevActions,
  294. char *szCurrentAction,
  295. char *szNextAction,
  296. DISPENTRY *DispTable,
  297. int nDispTable,
  298. HELPFUNC DefaultHelp
  299. )
  300. {
  301. BOOL needhelp = FALSE;
  302. BOOL helped = FALSE;
  303. if (needhelp = IsHelp(szCurrentAction)) {
  304. if (szNextAction) {
  305. helped = DispatchHelp(DispTable, nDispTable,
  306. szProgName,
  307. szPrevActions, szNextAction);
  308. } else if (DefaultHelp) {
  309. DefaultHelp(szProgName, szPrevActions);
  310. helped = TRUE;
  311. }
  312. } else if (needhelp = (szNextAction &&
  313. IsHelp(szNextAction))) {
  314. helped = DispatchHelp(DispTable, nDispTable,
  315. szProgName,
  316. szPrevActions, szCurrentAction);
  317. }
  318. if (needhelp && !helped) {
  319. char *action = AllocAction(szPrevActions, szCurrentAction);
  320. PrintHelp(szProgName, action);
  321. FreeAction(action);
  322. }
  323. return needhelp;
  324. }
  325. //-------------------------------------------------------------------------
  326. //
  327. // main
  328. //
  329. //-------------------------------------------------------------------------
  330. INT __cdecl
  331. main(int argc, char * argv[])
  332. {
  333. HRESULT hr;
  334. int status;
  335. char* szAction;
  336. char* szProgName;
  337. StartUp();
  338. szProgName = GetProgName(argv[0]);
  339. if (argc < 2) {
  340. PrintUsage(szProgName);
  341. Exit(1);
  342. }
  343. szAction = argv[1];
  344. argc-=2;
  345. argv+=2;
  346. if (DoHelp(szProgName, NULL, szAction, NULL,
  347. g_DispTable, g_nDispTable,
  348. PrintHelp))
  349. Exit(0);
  350. status = DispatchExec(g_DispTable, g_nDispTable,
  351. szProgName, NULL, szAction,
  352. argc, argv);
  353. Exit(status);
  354. return 0;
  355. }