source: mainline/uspace/app/bdsh/cmds/modules/help/help.c@ 6d100fd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6d100fd was 6d100fd, checked in by Petr Koupy <petr.koupy@…>, 14 years ago

Added help printouts for batch command.

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