xref: /reactos/base/applications/cmdutils/help/help.c (revision d6eebaa4)
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 <stdlib.h>
11 
12 #define WIN32_NO_STATUS
13 #include <windef.h>
14 #include <winbase.h>
15 
16 #include <strsafe.h>
17 
18 #include <conutils.h>
19 
20 #include "help.h"
21 #include "resource.h"
22 
23 static BOOL
24 IsInternalCommand(PCWSTR Cmd)
25 {
26     size_t i;
27     int res;
28 
29     /* Invalid command */
30     if (!Cmd) return FALSE;
31 
32     for (i = 0; i < ARRAYSIZE(InternalCommands); ++i)
33     {
34         res = _wcsicmp(InternalCommands[i], Cmd);
35         if (res == 0)
36         {
37             /* This is an internal command */
38             return TRUE;
39         }
40         else if (res > 0)
41         {
42             /*
43              * The internal commands list is sorted in alphabetical order.
44              * We can quit the loop immediately since the current internal
45              * command is lexically greater than the command to be tested.
46              */
47             break;
48         }
49     }
50 
51     /* Command not found */
52     return FALSE;
53 }
54 
55 int wmain(int argc, WCHAR* argv[])
56 {
57     WCHAR CmdLine[CMDLINE_LENGTH];
58 
59     /* Initialize the Console Standard Streams */
60     ConInitStdStreams();
61 
62     /*
63      * If the user hasn't asked for specific help,
64      * then print out the list of available commands.
65      */
66     if (argc <= 1)
67     {
68         ConResPuts(StdOut, IDS_HELP1);
69         ConResPuts(StdOut, IDS_HELP2);
70         return 0;
71     }
72 
73     /*
74      * Bad usage (too much options) or we use the /? switch.
75      * Display help for the HELP command.
76      */
77     if ((argc > 2) || (wcscmp(argv[1], L"/?") == 0))
78     {
79         ConResPuts(StdOut, IDS_USAGE);
80         return 0;
81     }
82 
83     /*
84      * If the command is not an internal one,
85      * display an information message and exit.
86      */
87     if (!IsInternalCommand(argv[1]))
88     {
89         ConResPrintf(StdOut, IDS_NO_ENTRY, argv[1]);
90         return 0;
91     }
92 
93     /*
94      * Run "<command> /?" in the current command processor.
95      */
96     StringCbPrintfW(CmdLine, sizeof(CmdLine), L"%ls /?", argv[1]);
97 
98     _flushall();
99     return _wsystem(CmdLine);
100 }
101 
102 /* EOF */
103