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_REMOTE
) != FILE_TYPE_CHAR
)
32 * It may be. Perform another test... The idea comes from the
33 * MSDN description of the WriteConsole API:
35 * "WriteConsole fails if it is used with a standard handle
36 * that is redirected to a file. If an application processes
37 * multilingual output that can be redirected, determine whether
38 * the output handle is a console handle (one method is to call
39 * the GetConsoleMode function and check whether it succeeds).
40 * If the handle is a console handle, call WriteConsole. If the
41 * handle is not a console handle, the output is redirected and
42 * you should call WriteFile to perform the I/O."
44 return GetConsoleMode(hHandle
, &dwMode
);
47 VOID
PrintResourceString(INT resID
, ...)
49 HANDLE OutputHandle
= GetStdHandle(STD_OUTPUT_HANDLE
);
50 WCHAR tmpBuffer
[RC_STRING_MAX_SIZE
];
53 va_start(arg_ptr
, resID
);
54 LoadStringW(GetModuleHandleW(NULL
), resID
, tmpBuffer
, RC_STRING_MAX_SIZE
);
56 // FIXME: Optimize by using Win32 console functions.
57 if (IsConsoleHandle(OutputHandle
))
59 _vcwprintf(tmpBuffer
, arg_ptr
);
63 vwprintf(tmpBuffer
, arg_ptr
);
69 BOOL
IsInternalCommand(LPCWSTR Cmd
)
75 if (!Cmd
) return FALSE
;
77 for (i
= 0; i
< sizeof(InternalCommands
)/sizeof(InternalCommands
[0]); ++i
)
79 res
= _wcsicmp(InternalCommands
[i
], Cmd
);
82 /* This is an internal command */
88 * The internal commands list is sorted in alphabetical order.
89 * We can quit the loop immediately since the current internal
90 * command is lexically greater than the command to be tested.
96 /* Command not found */
100 int wmain(int argc
, WCHAR
* argv
[])
102 WCHAR CmdLine
[CMDLINE_LENGTH
];
105 * If the user hasn't asked for specific help,
106 * then print out the list of available commands.
110 PrintResourceString(IDS_HELP1
);
111 PrintResourceString(IDS_HELP2
);
116 * Bad usage (too much options) or we use the /? switch.
117 * Display help for the help command.
119 if ((argc
> 2) || (wcscmp(argv
[1], L
"/?") == 0))
121 PrintResourceString(IDS_USAGE
);
126 * If the command is not an internal one,
127 * display an information message and exit.
129 if (!IsInternalCommand(argv
[1]))
131 PrintResourceString(IDS_NO_ENTRY
, argv
[1]);
136 * Run "<command> /?" in the current command processor.
138 wcsncpy(CmdLine
, argv
[1], CMDLINE_LENGTH
- wcslen(CmdLine
));
139 wcsncat(CmdLine
, L
" /?" , CMDLINE_LENGTH
- wcslen(CmdLine
));
142 return _wsystem(CmdLine
);