[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 |
|
---|
[4deb8b5] | 45 | #define HELP_IS_COMMANDS 2
|
---|
| 46 | #define HELP_IS_MODULE 1
|
---|
| 47 | #define HELP_IS_BUILTIN 0
|
---|
| 48 | #define HELP_IS_RUBBISH -1
|
---|
[216d6fc] | 49 |
|
---|
| 50 | volatile int mod_switch = -1;
|
---|
| 51 |
|
---|
| 52 | /* Just use a pointer here, no need for mod_switch */
|
---|
[b510d52] | 53 | static int is_mod_or_builtin(char *cmd)
|
---|
[216d6fc] | 54 | {
|
---|
| 55 | int rc = HELP_IS_RUBBISH;
|
---|
| 56 |
|
---|
[4deb8b5] | 57 | if (str_cmp(cmd, "commands") == 0)
|
---|
| 58 | return HELP_IS_COMMANDS;
|
---|
| 59 |
|
---|
[216d6fc] | 60 | rc = is_builtin(cmd);
|
---|
| 61 | if (rc > -1) {
|
---|
| 62 | mod_switch = rc;
|
---|
| 63 | return HELP_IS_BUILTIN;
|
---|
| 64 | }
|
---|
| 65 | rc = is_module(cmd);
|
---|
| 66 | if (rc > -1) {
|
---|
| 67 | mod_switch = rc;
|
---|
| 68 | return HELP_IS_MODULE;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | return HELP_IS_RUBBISH;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[809813d] | 74 | void help_cmd_help(unsigned int level)
|
---|
[216d6fc] | 75 | {
|
---|
| 76 | if (level == HELP_SHORT) {
|
---|
| 77 | printf(
|
---|
| 78 | "\n %s [command] <extended>\n"
|
---|
| 79 | " Use help [command] extended for detailed help on [command] "
|
---|
| 80 | ", even `help'\n\n", cmdname);
|
---|
| 81 | } else {
|
---|
| 82 | printf(
|
---|
| 83 | "\n `%s' - shows help for commands\n"
|
---|
| 84 | " Examples:\n"
|
---|
| 85 | " %s [command] Show help for [command]\n"
|
---|
| 86 | " %s [command] extended Show extended help for [command]\n"
|
---|
| 87 | "\n If no argument is given to %s, a list of commands are shown\n\n",
|
---|
| 88 | cmdname, cmdname, cmdname, cmdname);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[809813d] | 91 | return;
|
---|
[216d6fc] | 92 | }
|
---|
| 93 |
|
---|
[4deb8b5] | 94 | static void help_commands(void)
|
---|
[216d6fc] | 95 | {
|
---|
| 96 | builtin_t *cmd;
|
---|
[4deb8b5] | 97 | module_t *mod;
|
---|
| 98 | unsigned int i;
|
---|
| 99 |
|
---|
| 100 | printf("\n Bdsh built-in commands:\n");
|
---|
| 101 | printf(" ------------------------------------------------------------\n");
|
---|
| 102 |
|
---|
| 103 | /* First, show a list of built in commands that are available in this mode */
|
---|
| 104 | for (cmd = builtins; cmd->name != NULL; cmd++, i++) {
|
---|
| 105 | if (is_builtin_alias(cmd->name))
|
---|
| 106 | printf(" %-16s\tAlias for `%s'\n", cmd->name,
|
---|
| 107 | alias_for_builtin(cmd->name));
|
---|
| 108 | else
|
---|
| 109 | printf(" %-16s\t%s\n", cmd->name, cmd->desc);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | i = 0;
|
---|
| 113 |
|
---|
| 114 | /* Now, show a list of module commands that are available in this mode */
|
---|
| 115 | for (mod = modules; mod->name != NULL; mod++, i++) {
|
---|
| 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 | printf("Don't panic!\n\n");
|
---|
| 131 |
|
---|
| 132 | printf("This is Bdsh, the Brain dead shell, currently "
|
---|
| 133 | "the primary user interface to HelenOS. Bdsh allows you to enter "
|
---|
| 134 | "commands and supports history (Up, Down arrow keys), "
|
---|
| 135 | "line editing (Left Arrow, Right Arrow, Home, End, Backspace), "
|
---|
| 136 | "selection (Shift + movement keys), copy and paste (Ctrl-C, "
|
---|
| 137 | "Ctrl-V), similar to common desktop environments.\n\n");
|
---|
| 138 |
|
---|
| 139 | printf("The most basic filesystem commands are Bdsh builtins. Type "
|
---|
| 140 | "'help commands' [Enter] to see the list of Bdsh builtin commands. "
|
---|
| 141 | "Other commands are external executables located in the /app and "
|
---|
| 142 | "/srv directories. Type 'ls /app' [Enter] and 'ls /srv' [Enter] "
|
---|
| 143 | "to see their list. You can execute an external command simply "
|
---|
| 144 | "by entering its name (e.g. type 'tetris' [Enter]).\n\n");
|
---|
| 145 |
|
---|
| 146 | printf("HelenOS has virtual consoles (VCs). You can switch between "
|
---|
| 147 | "these using the F1-F11 keys.\n\n");
|
---|
| 148 |
|
---|
| 149 | printf("This is but a small glimpse of what you can do with HelenOS. "
|
---|
| 150 | "To learn more please point your browser to the HelenOS User's "
|
---|
| 151 | "Guide: http://trac.helenos.org/trac.fcgi/wiki/UsersGuide\n\n");
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | int cmd_help(char *argv[])
|
---|
| 155 | {
|
---|
[216d6fc] | 156 | int rc = 0;
|
---|
| 157 | int argc;
|
---|
| 158 | int level = HELP_SHORT;
|
---|
| 159 |
|
---|
[43e02a6] | 160 | argc = cli_count_args(argv);
|
---|
[216d6fc] | 161 |
|
---|
| 162 | if (argc > 3) {
|
---|
| 163 | printf("\nToo many arguments to `%s', try:\n", cmdname);
|
---|
| 164 | help_cmd_help(HELP_SHORT);
|
---|
| 165 | return CMD_FAILURE;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | if (argc == 3) {
|
---|
[92fd52d7] | 169 | if (!str_cmp("extended", argv[2]))
|
---|
[216d6fc] | 170 | level = HELP_LONG;
|
---|
| 171 | else
|
---|
| 172 | level = HELP_SHORT;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | if (argc > 1) {
|
---|
| 176 | rc = is_mod_or_builtin(argv[1]);
|
---|
| 177 | switch (rc) {
|
---|
| 178 | case HELP_IS_RUBBISH:
|
---|
[4deb8b5] | 179 | printf("Invalid topic %s\n", argv[1]);
|
---|
[216d6fc] | 180 | return CMD_FAILURE;
|
---|
[4deb8b5] | 181 | case HELP_IS_COMMANDS:
|
---|
| 182 | help_commands();
|
---|
| 183 | return CMD_SUCCESS;
|
---|
[216d6fc] | 184 | case HELP_IS_MODULE:
|
---|
| 185 | help_module(mod_switch, level);
|
---|
| 186 | return CMD_SUCCESS;
|
---|
| 187 | case HELP_IS_BUILTIN:
|
---|
| 188 | help_builtin(mod_switch, level);
|
---|
| 189 | return CMD_SUCCESS;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[4deb8b5] | 193 | help_survival();
|
---|
[216d6fc] | 194 |
|
---|
| 195 | return CMD_SUCCESS;
|
---|
| 196 | }
|
---|