[36ab7c7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Tim Post
|
---|
[216d6fc] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
[36ab7c7] | 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
[216d6fc] | 8 | *
|
---|
[36ab7c7] | 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.
|
---|
[216d6fc] | 16 | *
|
---|
[36ab7c7] | 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.
|
---|
[216d6fc] | 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <stdio.h>
|
---|
| 30 | #include <stdlib.h>
|
---|
[19f857a] | 31 | #include <str.h>
|
---|
[43e02a6] | 32 |
|
---|
| 33 | #include "config.h"
|
---|
[216d6fc] | 34 | #include "entry.h"
|
---|
| 35 | #include "help.h"
|
---|
| 36 | #include "cmds.h"
|
---|
| 37 | #include "modules.h"
|
---|
| 38 | #include "builtins.h"
|
---|
| 39 | #include "errors.h"
|
---|
[43e02a6] | 40 | #include "util.h"
|
---|
[216d6fc] | 41 |
|
---|
[a000878c] | 42 | static const char *cmdname = "help";
|
---|
[216d6fc] | 43 | extern const char *progname;
|
---|
| 44 |
|
---|
[6d100fd] | 45 | #define HELP_IS_BATCH 3
|
---|
[4deb8b5] | 46 | #define HELP_IS_COMMANDS 2
|
---|
| 47 | #define HELP_IS_MODULE 1
|
---|
| 48 | #define HELP_IS_BUILTIN 0
|
---|
| 49 | #define HELP_IS_RUBBISH -1
|
---|
[216d6fc] | 50 |
|
---|
| 51 | volatile int mod_switch = -1;
|
---|
| 52 |
|
---|
| 53 | /* Just use a pointer here, no need for mod_switch */
|
---|
[b510d52] | 54 | static int is_mod_or_builtin(char *cmd)
|
---|
[216d6fc] | 55 | {
|
---|
| 56 | int rc = HELP_IS_RUBBISH;
|
---|
| 57 |
|
---|
[6d100fd] | 58 | if (str_cmp(cmd, "batch") == 0)
|
---|
| 59 | return HELP_IS_BATCH;
|
---|
| 60 |
|
---|
[4deb8b5] | 61 | if (str_cmp(cmd, "commands") == 0)
|
---|
| 62 | return HELP_IS_COMMANDS;
|
---|
| 63 |
|
---|
[216d6fc] | 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 |
|
---|
[809813d] | 78 | void help_cmd_help(unsigned int level)
|
---|
[216d6fc] | 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 |
|
---|
[809813d] | 95 | return;
|
---|
[216d6fc] | 96 | }
|
---|
| 97 |
|
---|
[6d100fd] | 98 | static 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 |
|
---|
[4deb8b5] | 118 | static void help_commands(void)
|
---|
[216d6fc] | 119 | {
|
---|
| 120 | builtin_t *cmd;
|
---|
[4deb8b5] | 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 |
|
---|
[6d100fd] | 147 | printf(" %-16s\t%s\n", "batch", "Issue batch of commands");
|
---|
[4deb8b5] | 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) */
|
---|
| 153 | static 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 |
|
---|
| 179 | int cmd_help(char *argv[])
|
---|
| 180 | {
|
---|
[216d6fc] | 181 | int rc = 0;
|
---|
| 182 | int argc;
|
---|
| 183 | int level = HELP_SHORT;
|
---|
| 184 |
|
---|
[43e02a6] | 185 | argc = cli_count_args(argv);
|
---|
[216d6fc] | 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) {
|
---|
[92fd52d7] | 194 | if (!str_cmp("extended", argv[2]))
|
---|
[216d6fc] | 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:
|
---|
[4deb8b5] | 204 | printf("Invalid topic %s\n", argv[1]);
|
---|
[216d6fc] | 205 | return CMD_FAILURE;
|
---|
[4deb8b5] | 206 | case HELP_IS_COMMANDS:
|
---|
| 207 | help_commands();
|
---|
| 208 | return CMD_SUCCESS;
|
---|
[6d100fd] | 209 | case HELP_IS_BATCH:
|
---|
| 210 | help_batch(level);
|
---|
| 211 | return CMD_SUCCESS;
|
---|
[216d6fc] | 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 |
|
---|
[4deb8b5] | 221 | help_survival();
|
---|
[216d6fc] | 222 |
|
---|
| 223 | return CMD_SUCCESS;
|
---|
| 224 | }
|
---|