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)
14 #define WIN32_NO_STATUS
23 BOOL
IsConsoleHandle(HANDLE hHandle
)
27 /* Check whether the handle may be that of a console... */
28 if ((GetFileType(hHandle
) & FILE_TYPE_CHAR
) == 0) return FALSE
;
31 * It may be. Perform another test... The idea comes from the
32 * MSDN description of the WriteConsole API:
34 * "WriteConsole fails if it is used with a standard handle
35 * that is redirected to a file. If an application processes
36 * multilingual output that can be redirected, determine whether
37 * the output handle is a console handle (one method is to call
38 * the GetConsoleMode function and check whether it succeeds).
39 * If the handle is a console handle, call WriteConsole. If the
40 * handle is not a console handle, the output is redirected and
41 * you should call WriteFile to perform the I/O."
43 return GetConsoleMode(hHandle
, &dwMode
);
46 VOID
PrintResourceString(INT resID
, ...)
48 HANDLE OutputHandle
= GetStdHandle(STD_OUTPUT_HANDLE
);
49 WCHAR tmpBuffer
[RC_STRING_MAX_SIZE
];
52 va_start(arg_ptr
, resID
);
53 LoadStringW(GetModuleHandleW(NULL
), resID
, tmpBuffer
, RC_STRING_MAX_SIZE
);
55 // FIXME: Optimize by using Win32 console functions.
56 if (IsConsoleHandle(OutputHandle
))
58 _vcwprintf(tmpBuffer
, arg_ptr
);
62 vwprintf(tmpBuffer
, arg_ptr
);
68 BOOL
IsInternalCommand(LPCWSTR Cmd
)
74 if (!Cmd
) return FALSE
;
76 for (i
= 0; i
< sizeof(InternalCommands
)/sizeof(InternalCommands
[0]); ++i
)
78 res
= _wcsicmp(InternalCommands
[i
], Cmd
);
81 /* This is an internal command */
87 * The internal commands list is sorted in alphabetical order.
88 * We can quit the loop immediately since the current internal
89 * command is lexically greater than the command to be tested.
95 /* Command not found */
99 int wmain(int argc
, WCHAR
* argv
[])
101 WCHAR CmdLine
[CMDLINE_LENGTH
];
104 * If the user hasn't asked for specific help,
105 * then print out the list of available commands.
109 PrintResourceString(IDS_HELP1
);
110 PrintResourceString(IDS_HELP2
);
115 * Bad usage (too much options) or we use the /? switch.
116 * Display help for the help command.
118 if ((argc
> 2) || (wcscmp(argv
[1], L
"/?") == 0))
120 PrintResourceString(IDS_USAGE
);
125 * If the command is not an internal one,
126 * display an information message and exit.
128 if (!IsInternalCommand(argv
[1]))
130 PrintResourceString(IDS_NO_ENTRY
, argv
[1]);
135 * Run "<command> /?" in the current command processor.
137 wcsncpy(CmdLine
, argv
[1], CMDLINE_LENGTH
- wcslen(CmdLine
));
138 wcsncat(CmdLine
, L
" /?" , CMDLINE_LENGTH
- wcslen(CmdLine
));
141 return _wsystem(CmdLine
);