| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Tim Post
|
|---|
| 3 | * Copyright (c) 2011 Martin Sucha
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 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.
|
|---|
| 17 | *
|
|---|
| 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.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 | #include <stdlib.h>
|
|---|
| 32 | #include <stddef.h>
|
|---|
| 33 | #include <str.h>
|
|---|
| 34 | #include <fmtutil.h>
|
|---|
| 35 |
|
|---|
| 36 | #include "config.h"
|
|---|
| 37 | #include "entry.h"
|
|---|
| 38 | #include "help.h"
|
|---|
| 39 | #include "cmds.h"
|
|---|
| 40 | #include "modules.h"
|
|---|
| 41 | #include "builtins.h"
|
|---|
| 42 | #include "errors.h"
|
|---|
| 43 | #include "util.h"
|
|---|
| 44 |
|
|---|
| 45 | static const char *cmdname = "help";
|
|---|
| 46 | extern const char *progname;
|
|---|
| 47 |
|
|---|
| 48 | #define HELP_IS_COMMANDS 2
|
|---|
| 49 | #define HELP_IS_MODULE 1
|
|---|
| 50 | #define HELP_IS_BUILTIN 0
|
|---|
| 51 | #define HELP_IS_RUBBISH -1
|
|---|
| 52 |
|
|---|
| 53 | volatile int mod_switch = -1;
|
|---|
| 54 |
|
|---|
| 55 | /* Just use a pointer here, no need for mod_switch */
|
|---|
| 56 | static int is_mod_or_builtin(char *cmd)
|
|---|
| 57 | {
|
|---|
| 58 | int rc = HELP_IS_RUBBISH;
|
|---|
| 59 |
|
|---|
| 60 | if (str_cmp(cmd, "commands") == 0)
|
|---|
| 61 | return HELP_IS_COMMANDS;
|
|---|
| 62 |
|
|---|
| 63 | rc = is_builtin(cmd);
|
|---|
| 64 | if (rc > -1) {
|
|---|
| 65 | mod_switch = rc;
|
|---|
| 66 | return HELP_IS_BUILTIN;
|
|---|
| 67 | }
|
|---|
| 68 | rc = is_module(cmd);
|
|---|
| 69 | if (rc > -1) {
|
|---|
| 70 | mod_switch = rc;
|
|---|
| 71 | return HELP_IS_MODULE;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | return HELP_IS_RUBBISH;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void help_cmd_help(unsigned int level)
|
|---|
| 78 | {
|
|---|
| 79 | if (level == HELP_SHORT) {
|
|---|
| 80 | printf(
|
|---|
| 81 | "\n %s [command] <extended>\n"
|
|---|
| 82 | " Use help [command] extended for detailed help on [command] "
|
|---|
| 83 | ", even `help'\n\n", cmdname);
|
|---|
| 84 | } else {
|
|---|
| 85 | printf(
|
|---|
| 86 | "\n `%s' - shows help for commands\n"
|
|---|
| 87 | " Examples:\n"
|
|---|
| 88 | " %s [command] Show help for [command]\n"
|
|---|
| 89 | " %s [command] extended Show extended help for [command]\n"
|
|---|
| 90 | "\n If no argument is given to %s, a list of commands are shown\n\n",
|
|---|
| 91 | cmdname, cmdname, cmdname, cmdname);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | return;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | static void help_commands(void)
|
|---|
| 98 | {
|
|---|
| 99 | builtin_t *cmd;
|
|---|
| 100 | module_t *mod;
|
|---|
| 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++) {
|
|---|
| 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 | /* Now, show a list of module commands that are available in this mode */
|
|---|
| 115 | for (mod = modules; mod->name != NULL; mod++) {
|
|---|
| 116 | if (is_module_alias(mod->name))
|
|---|
| 117 | printf(" %-16s\tAlias for `%s'\n", mod->name,
|
|---|
| 118 | alias_for_module(mod->name));
|
|---|
| 119 | else
|
|---|
| 120 | printf(" %-16s\t%s\n", mod->name, mod->desc);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | printf("\n Try %s %s for more information on how `%s' works.\n\n",
|
|---|
| 124 | cmdname, cmdname, cmdname);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /** Display survival tips. ('help' without arguments) */
|
|---|
| 128 | static void help_survival(void)
|
|---|
| 129 | {
|
|---|
| 130 | print_wrapped_console(
|
|---|
| 131 | "Don't panic!\n\n"
|
|---|
| 132 |
|
|---|
| 133 | "This is Bdsh, the Brain dead shell, currently "
|
|---|
| 134 | "the primary user interface to HelenOS. Bdsh allows you to enter "
|
|---|
| 135 | "commands and supports history (Up, Down arrow keys), "
|
|---|
| 136 | "line editing (Left Arrow, Right Arrow, Home, End, Backspace), "
|
|---|
| 137 | "selection (Shift + movement keys), copy and paste (Ctrl-C, "
|
|---|
| 138 | "Ctrl-V), similar to common desktop environments.\n\n"
|
|---|
| 139 |
|
|---|
| 140 | "The most basic filesystem commands are Bdsh builtins. Type "
|
|---|
| 141 | "'help commands' [Enter] to see the list of Bdsh builtin commands. "
|
|---|
| 142 | "Other commands are external executables located in the /app and "
|
|---|
| 143 | "/srv directories. Type 'ls /app' [Enter] and 'ls /srv' [Enter] "
|
|---|
| 144 | "to see their list. You can execute an external command simply "
|
|---|
| 145 | "by entering its name (e.g. type 'tetris' [Enter]).\n\n"
|
|---|
| 146 |
|
|---|
| 147 | "HelenOS has virtual consoles (VCs). You can switch between "
|
|---|
| 148 | "these using the F1-F11 keys.\n\n"
|
|---|
| 149 |
|
|---|
| 150 | "This is but a small glimpse of what you can do with HelenOS. "
|
|---|
| 151 | "To learn more please point your browser to the HelenOS User's "
|
|---|
| 152 | "Guide: http://trac.helenos.org/wiki/UsersGuide\n\n",
|
|---|
| 153 | ALIGN_LEFT);
|
|---|
| 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 | }
|
|---|