Create the AHCI branch for Aman's work
[reactos.git] / base / applications / cmdutils / help / help.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS help utility
4 * FILE: base/applications/cmdutils/help/help.c
5 * PURPOSE: Provide help for command-line utilities
6 * PROGRAMMERS: Lee Schroeder (spaceseel at gmail dot com)
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <wchar.h>
13
14 #define WIN32_NO_STATUS
15 #include <windef.h>
16 #include <winbase.h>
17 #include <winuser.h>
18 #include <wincon.h>
19 #include <strsafe.h>
20
21 #include "help.h"
22 #include "resource.h"
23
24 VOID PrintResourceString(INT resID, ...)
25 {
26 WCHAR bufSrc[RC_STRING_MAX_SIZE];
27 WCHAR bufFormatted[RC_STRING_MAX_SIZE];
28 CHAR bufFormattedOem[RC_STRING_MAX_SIZE];
29
30 va_list args;
31 va_start(args, resID);
32
33 LoadStringW(GetModuleHandleW(NULL), resID, bufSrc, ARRAYSIZE(bufSrc));
34 vswprintf(bufFormatted, bufSrc, args);
35 CharToOemW(bufFormatted, bufFormattedOem);
36 fputs(bufFormattedOem, stdout);
37
38 va_end(args);
39 }
40
41 BOOL IsInternalCommand(LPCWSTR Cmd)
42 {
43 size_t i;
44 int res;
45
46 /* Invalid command */
47 if (!Cmd) return FALSE;
48
49 for (i = 0; i < ARRAYSIZE(InternalCommands); ++i)
50 {
51 res = _wcsicmp(InternalCommands[i], Cmd);
52 if (res == 0)
53 {
54 /* This is an internal command */
55 return TRUE;
56 }
57 else if (res > 0)
58 {
59 /*
60 * The internal commands list is sorted in alphabetical order.
61 * We can quit the loop immediately since the current internal
62 * command is lexically greater than the command to be tested.
63 */
64 break;
65 }
66 }
67
68 /* Command not found */
69 return FALSE;
70 }
71
72 int wmain(int argc, WCHAR* argv[])
73 {
74 WCHAR CmdLine[CMDLINE_LENGTH];
75
76 /*
77 * If the user hasn't asked for specific help,
78 * then print out the list of available commands.
79 */
80 if (argc <= 1)
81 {
82 PrintResourceString(IDS_HELP1);
83 PrintResourceString(IDS_HELP2);
84 return 0;
85 }
86
87 /*
88 * Bad usage (too much options) or we use the /? switch.
89 * Display help for the help command.
90 */
91 if ((argc > 2) || (wcscmp(argv[1], L"/?") == 0))
92 {
93 PrintResourceString(IDS_USAGE);
94 return 0;
95 }
96
97 /*
98 * If the command is not an internal one,
99 * display an information message and exit.
100 */
101 if (!IsInternalCommand(argv[1]))
102 {
103 PrintResourceString(IDS_NO_ENTRY, argv[1]);
104 return 0;
105 }
106
107 /*
108 * Run "<command> /?" in the current command processor.
109 */
110 StringCbPrintfW(CmdLine, sizeof(CmdLine), L"%ls /?", argv[1]);
111
112 _flushall();
113 return _wsystem(CmdLine);
114 }
115
116 /* EOF */