1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
|
---|
2 | * All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions are met:
|
---|
6 | *
|
---|
7 | * Redistributions of source code must retain the above copyright notice, this
|
---|
8 | * list of conditions and the following disclaimer.
|
---|
9 | *
|
---|
10 | * Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | * this list of conditions and the following disclaimer in the documentation
|
---|
12 | * and/or other materials provided with the distribution.
|
---|
13 | *
|
---|
14 | * Neither the name of the original program's authors nor the names of its
|
---|
15 | * contributors may be used to endorse or promote products derived from this
|
---|
16 | * software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
---|
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
---|
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
---|
28 | * POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <str.h>
|
---|
34 |
|
---|
35 | #include "config.h"
|
---|
36 | #include "entry.h"
|
---|
37 | #include "help.h"
|
---|
38 | #include "cmds.h"
|
---|
39 | #include "modules.h"
|
---|
40 | #include "builtins.h"
|
---|
41 | #include "errors.h"
|
---|
42 | #include "util.h"
|
---|
43 |
|
---|
44 | static const char *cmdname = "help";
|
---|
45 | extern const char *progname;
|
---|
46 |
|
---|
47 | #define HELP_IS_COMMANDS 2
|
---|
48 | #define HELP_IS_MODULE 1
|
---|
49 | #define HELP_IS_BUILTIN 0
|
---|
50 | #define HELP_IS_RUBBISH -1
|
---|
51 |
|
---|
52 | volatile int mod_switch = -1;
|
---|
53 |
|
---|
54 | /* Just use a pointer here, no need for mod_switch */
|
---|
55 | static int is_mod_or_builtin(char *cmd)
|
---|
56 | {
|
---|
57 | int rc = HELP_IS_RUBBISH;
|
---|
58 |
|
---|
59 | if (str_cmp(cmd, "commands") == 0)
|
---|
60 | return HELP_IS_COMMANDS;
|
---|
61 |
|
---|
62 | rc = is_builtin(cmd);
|
---|
63 | if (rc > -1) {
|
---|
64 | mod_switch = rc;
|
---|
65 | return HELP_IS_BUILTIN;
|
---|
66 | }
|
---|
67 | rc = is_module(cmd);
|
---|
68 | if (rc > -1) {
|
---|
69 | mod_switch = rc;
|
---|
70 | return HELP_IS_MODULE;
|
---|
71 | }
|
---|
72 |
|
---|
73 | return HELP_IS_RUBBISH;
|
---|
74 | }
|
---|
75 |
|
---|
76 | void help_cmd_help(unsigned int level)
|
---|
77 | {
|
---|
78 | if (level == HELP_SHORT) {
|
---|
79 | printf(
|
---|
80 | "\n %s [command] <extended>\n"
|
---|
81 | " Use help [command] extended for detailed help on [command] "
|
---|
82 | ", even `help'\n\n", cmdname);
|
---|
83 | } else {
|
---|
84 | printf(
|
---|
85 | "\n `%s' - shows help for commands\n"
|
---|
86 | " Examples:\n"
|
---|
87 | " %s [command] Show help for [command]\n"
|
---|
88 | " %s [command] extended Show extended help for [command]\n"
|
---|
89 | "\n If no argument is given to %s, a list of commands are shown\n\n",
|
---|
90 | cmdname, cmdname, cmdname, cmdname);
|
---|
91 | }
|
---|
92 |
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | static void help_commands(void)
|
---|
97 | {
|
---|
98 | builtin_t *cmd;
|
---|
99 | module_t *mod;
|
---|
100 | unsigned int i;
|
---|
101 |
|
---|
102 | printf("\n Bdsh built-in commands:\n");
|
---|
103 | printf(" ------------------------------------------------------------\n");
|
---|
104 |
|
---|
105 | /* First, show a list of built in commands that are available in this mode */
|
---|
106 | for (cmd = builtins; cmd->name != NULL; cmd++, i++) {
|
---|
107 | if (is_builtin_alias(cmd->name))
|
---|
108 | printf(" %-16s\tAlias for `%s'\n", cmd->name,
|
---|
109 | alias_for_builtin(cmd->name));
|
---|
110 | else
|
---|
111 | printf(" %-16s\t%s\n", cmd->name, cmd->desc);
|
---|
112 | }
|
---|
113 |
|
---|
114 | i = 0;
|
---|
115 |
|
---|
116 | /* Now, show a list of module commands that are available in this mode */
|
---|
117 | for (mod = modules; mod->name != NULL; mod++, i++) {
|
---|
118 | if (is_module_alias(mod->name))
|
---|
119 | printf(" %-16s\tAlias for `%s'\n", mod->name,
|
---|
120 | alias_for_module(mod->name));
|
---|
121 | else
|
---|
122 | printf(" %-16s\t%s\n", mod->name, mod->desc);
|
---|
123 | }
|
---|
124 |
|
---|
125 | printf("\n Try %s %s for more information on how `%s' works.\n\n",
|
---|
126 | cmdname, cmdname, cmdname);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Display survival tips. ('help' without arguments) */
|
---|
130 | static void help_survival(void)
|
---|
131 | {
|
---|
132 | printf("Don't panic!\n\n");
|
---|
133 |
|
---|
134 | printf("This is Bdsh, the Brain dead shell, currently "
|
---|
135 | "the primary user interface to HelenOS. Bdsh allows you to enter "
|
---|
136 | "commands and supports history (Up, Down arrow keys), "
|
---|
137 | "line editing (Left Arrow, Right Arrow, Home, End, Backspace), "
|
---|
138 | "selection (Shift + movement keys), copy and paste (Ctrl-C, "
|
---|
139 | "Ctrl-V), similar to common desktop environments.\n\n");
|
---|
140 |
|
---|
141 | printf("The most basic filesystem commands are Bdsh builtins. Type "
|
---|
142 | "'help commands' [Enter] to see the list of Bdsh builtin commands. "
|
---|
143 | "Other commands are external executables located in the /app and "
|
---|
144 | "/srv directories. Type 'ls /app' [Enter] and 'ls /srv' [Enter] "
|
---|
145 | "to see their list. You can execute an external command simply "
|
---|
146 | "by entering its name (e.g. type 'tetris' [Enter]).\n\n");
|
---|
147 |
|
---|
148 | printf("HelenOS has virtual consoles (VCs). You can switch between "
|
---|
149 | "these using the F1-F11 keys.\n\n");
|
---|
150 |
|
---|
151 | printf("This is but a small glimpse of what you can do with HelenOS. "
|
---|
152 | "To learn more please point your browser to the HelenOS User's "
|
---|
153 | "Guide: http://trac.helenos.org/trac.fcgi/wiki/UsersGuide\n\n");
|
---|
154 | }
|
---|
155 |
|
---|
156 | int cmd_help(char *argv[])
|
---|
157 | {
|
---|
158 | int rc = 0;
|
---|
159 | int argc;
|
---|
160 | int level = HELP_SHORT;
|
---|
161 |
|
---|
162 | argc = cli_count_args(argv);
|
---|
163 |
|
---|
164 | if (argc > 3) {
|
---|
165 | printf("\nToo many arguments to `%s', try:\n", cmdname);
|
---|
166 | help_cmd_help(HELP_SHORT);
|
---|
167 | return CMD_FAILURE;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (argc == 3) {
|
---|
171 | if (!str_cmp("extended", argv[2]))
|
---|
172 | level = HELP_LONG;
|
---|
173 | else
|
---|
174 | level = HELP_SHORT;
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (argc > 1) {
|
---|
178 | rc = is_mod_or_builtin(argv[1]);
|
---|
179 | switch (rc) {
|
---|
180 | case HELP_IS_RUBBISH:
|
---|
181 | printf("Invalid topic %s\n", argv[1]);
|
---|
182 | return CMD_FAILURE;
|
---|
183 | case HELP_IS_COMMANDS:
|
---|
184 | help_commands();
|
---|
185 | return CMD_SUCCESS;
|
---|
186 | case HELP_IS_MODULE:
|
---|
187 | help_module(mod_switch, level);
|
---|
188 | return CMD_SUCCESS;
|
---|
189 | case HELP_IS_BUILTIN:
|
---|
190 | help_builtin(mod_switch, level);
|
---|
191 | return CMD_SUCCESS;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | help_survival();
|
---|
196 |
|
---|
197 | return CMD_SUCCESS;
|
---|
198 | }
|
---|