[36ab7c7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Tim Post
|
---|
[22cf42d9] | 3 | * Copyright (c) 2011 Martin Sucha
|
---|
[216d6fc] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
[36ab7c7] | 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
[216d6fc] | 9 | *
|
---|
[36ab7c7] | 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
[216d6fc] | 17 | *
|
---|
[36ab7c7] | 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[216d6fc] | 28 | */
|
---|
| 29 |
|
---|
| 30 | #include <stdio.h>
|
---|
| 31 | #include <stdlib.h>
|
---|
[19f857a] | 32 | #include <str.h>
|
---|
[22cf42d9] | 33 | #include <fmtutil.h>
|
---|
[43e02a6] | 34 |
|
---|
| 35 | #include "config.h"
|
---|
[216d6fc] | 36 | #include "entry.h"
|
---|
| 37 | #include "help.h"
|
---|
| 38 | #include "cmds.h"
|
---|
| 39 | #include "modules.h"
|
---|
| 40 | #include "builtins.h"
|
---|
| 41 | #include "errors.h"
|
---|
[43e02a6] | 42 | #include "util.h"
|
---|
[216d6fc] | 43 |
|
---|
[a000878c] | 44 | static const char *cmdname = "help";
|
---|
[216d6fc] | 45 | extern const char *progname;
|
---|
| 46 |
|
---|
[4deb8b5] | 47 | #define HELP_IS_COMMANDS 2
|
---|
| 48 | #define HELP_IS_MODULE 1
|
---|
| 49 | #define HELP_IS_BUILTIN 0
|
---|
| 50 | #define HELP_IS_RUBBISH -1
|
---|
[216d6fc] | 51 |
|
---|
| 52 | volatile int mod_switch = -1;
|
---|
| 53 |
|
---|
| 54 | /* Just use a pointer here, no need for mod_switch */
|
---|
[b510d52] | 55 | static int is_mod_or_builtin(char *cmd)
|
---|
[216d6fc] | 56 | {
|
---|
| 57 | int rc = HELP_IS_RUBBISH;
|
---|
| 58 |
|
---|
[4deb8b5] | 59 | if (str_cmp(cmd, "commands") == 0)
|
---|
| 60 | return HELP_IS_COMMANDS;
|
---|
| 61 |
|
---|
[216d6fc] | 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 |
|
---|
[809813d] | 76 | void help_cmd_help(unsigned int level)
|
---|
[216d6fc] | 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 |
|
---|
[809813d] | 93 | return;
|
---|
[216d6fc] | 94 | }
|
---|
| 95 |
|
---|
[4deb8b5] | 96 | static void help_commands(void)
|
---|
[216d6fc] | 97 | {
|
---|
| 98 | builtin_t *cmd;
|
---|
[4deb8b5] | 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 | {
|
---|
[22cf42d9] | 132 | print_wrapped_console(
|
---|
| 133 | "Don't panic!\n\n"
|
---|
[4deb8b5] | 134 |
|
---|
[22cf42d9] | 135 | "This is Bdsh, the Brain dead shell, currently "
|
---|
[4deb8b5] | 136 | "the primary user interface to HelenOS. Bdsh allows you to enter "
|
---|
| 137 | "commands and supports history (Up, Down arrow keys), "
|
---|
| 138 | "line editing (Left Arrow, Right Arrow, Home, End, Backspace), "
|
---|
| 139 | "selection (Shift + movement keys), copy and paste (Ctrl-C, "
|
---|
[22cf42d9] | 140 | "Ctrl-V), similar to common desktop environments.\n\n"
|
---|
[4deb8b5] | 141 |
|
---|
[22cf42d9] | 142 | "The most basic filesystem commands are Bdsh builtins. Type "
|
---|
[4deb8b5] | 143 | "'help commands' [Enter] to see the list of Bdsh builtin commands. "
|
---|
| 144 | "Other commands are external executables located in the /app and "
|
---|
| 145 | "/srv directories. Type 'ls /app' [Enter] and 'ls /srv' [Enter] "
|
---|
| 146 | "to see their list. You can execute an external command simply "
|
---|
[22cf42d9] | 147 | "by entering its name (e.g. type 'tetris' [Enter]).\n\n"
|
---|
[4deb8b5] | 148 |
|
---|
[22cf42d9] | 149 | "HelenOS has virtual consoles (VCs). You can switch between "
|
---|
| 150 | "these using the F1-F11 keys.\n\n"
|
---|
[4deb8b5] | 151 |
|
---|
[22cf42d9] | 152 | "This is but a small glimpse of what you can do with HelenOS. "
|
---|
[4deb8b5] | 153 | "To learn more please point your browser to the HelenOS User's "
|
---|
[22cf42d9] | 154 | "Guide: http://trac.helenos.org/trac.fcgi/wiki/UsersGuide\n\n",
|
---|
[fcfac250] | 155 | ALIGN_JUSTIFY);
|
---|
[4deb8b5] | 156 | }
|
---|
| 157 |
|
---|
| 158 | int cmd_help(char *argv[])
|
---|
| 159 | {
|
---|
[216d6fc] | 160 | int rc = 0;
|
---|
| 161 | int argc;
|
---|
| 162 | int level = HELP_SHORT;
|
---|
| 163 |
|
---|
[43e02a6] | 164 | argc = cli_count_args(argv);
|
---|
[216d6fc] | 165 |
|
---|
| 166 | if (argc > 3) {
|
---|
| 167 | printf("\nToo many arguments to `%s', try:\n", cmdname);
|
---|
| 168 | help_cmd_help(HELP_SHORT);
|
---|
| 169 | return CMD_FAILURE;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | if (argc == 3) {
|
---|
[92fd52d7] | 173 | if (!str_cmp("extended", argv[2]))
|
---|
[216d6fc] | 174 | level = HELP_LONG;
|
---|
| 175 | else
|
---|
| 176 | level = HELP_SHORT;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | if (argc > 1) {
|
---|
| 180 | rc = is_mod_or_builtin(argv[1]);
|
---|
| 181 | switch (rc) {
|
---|
| 182 | case HELP_IS_RUBBISH:
|
---|
[4deb8b5] | 183 | printf("Invalid topic %s\n", argv[1]);
|
---|
[216d6fc] | 184 | return CMD_FAILURE;
|
---|
[4deb8b5] | 185 | case HELP_IS_COMMANDS:
|
---|
| 186 | help_commands();
|
---|
| 187 | return CMD_SUCCESS;
|
---|
[216d6fc] | 188 | case HELP_IS_MODULE:
|
---|
| 189 | help_module(mod_switch, level);
|
---|
| 190 | return CMD_SUCCESS;
|
---|
| 191 | case HELP_IS_BUILTIN:
|
---|
| 192 | help_builtin(mod_switch, level);
|
---|
| 193 | return CMD_SUCCESS;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[4deb8b5] | 197 | help_survival();
|
---|
[216d6fc] | 198 |
|
---|
| 199 | return CMD_SUCCESS;
|
---|
| 200 | }
|
---|