help.c

00001 /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
00002  * All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *
00007  * Redistributions of source code must retain the above copyright notice, this
00008  * list of conditions and the following disclaimer.
00009  *
00010  * Redistributions in binary form must reproduce the above copyright notice,
00011  * this list of conditions and the following disclaimer in the documentation
00012  * and/or other materials provided with the distribution.
00013  *
00014  * Neither the name of the original program's authors nor the names of its
00015  * contributors may be used to endorse or promote products derived from this
00016  * software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <str.h>
00034 
00035 #include "config.h"
00036 #include "entry.h"
00037 #include "help.h"
00038 #include "cmds.h"
00039 #include "modules.h"
00040 #include "builtins.h"
00041 #include "errors.h"
00042 #include "util.h"
00043 
00044 static const char *cmdname = "help";
00045 extern const char *progname;
00046 
00047 #define HELP_IS_COMMANDS        2
00048 #define HELP_IS_MODULE          1
00049 #define HELP_IS_BUILTIN         0
00050 #define HELP_IS_RUBBISH         -1
00051 
00052 volatile int mod_switch = -1;
00053 
00054 /* Just use a pointer here, no need for mod_switch */
00055 static int is_mod_or_builtin(char *cmd)
00056 {
00057         int rc = HELP_IS_RUBBISH;
00058 
00059         if (str_cmp(cmd, "commands") == 0)
00060                 return HELP_IS_COMMANDS;
00061 
00062         rc = is_builtin(cmd);
00063         if (rc > -1) {
00064                 mod_switch = rc;
00065                 return HELP_IS_BUILTIN;
00066         }
00067         rc = is_module(cmd);
00068         if (rc > -1) {
00069                 mod_switch = rc;
00070                 return HELP_IS_MODULE;
00071         }
00072 
00073         return HELP_IS_RUBBISH;
00074 }
00075 
00076 void help_cmd_help(unsigned int level)
00077 {
00078         if (level == HELP_SHORT) {
00079                 printf(
00080                 "\n  %s [command] <extended>\n"
00081                 "  Use help [command] extended for detailed help on [command] "
00082                 ", even `help'\n\n", cmdname);
00083         } else {
00084                 printf(
00085                 "\n  `%s' - shows help for commands\n"
00086                 "  Examples:\n"
00087                 "   %s [command]           Show help for [command]\n"
00088                 "   %s [command] extended  Show extended help for [command]\n"
00089                 "\n  If no argument is given to %s, a list of commands are shown\n\n",
00090                 cmdname, cmdname, cmdname, cmdname);
00091         }
00092 
00093         return;
00094 }
00095 
00096 static void help_commands(void)
00097 {
00098         builtin_t *cmd;
00099         module_t *mod;
00100         unsigned int i;
00101 
00102         printf("\n  Bdsh built-in commands:\n");
00103         printf("  ------------------------------------------------------------\n");
00104 
00105         /* First, show a list of built in commands that are available in this mode */
00106         for (cmd = builtins; cmd->name != NULL; cmd++, i++) {
00107                         if (is_builtin_alias(cmd->name))
00108                                 printf("   %-16s\tAlias for `%s'\n", cmd->name,
00109                                         alias_for_builtin(cmd->name));
00110                         else
00111                                 printf("   %-16s\t%s\n", cmd->name, cmd->desc);
00112         }
00113 
00114         i = 0;
00115 
00116         /* Now, show a list of module commands that are available in this mode */
00117         for (mod = modules; mod->name != NULL; mod++, i++) {
00118                         if (is_module_alias(mod->name))
00119                                 printf("   %-16s\tAlias for `%s'\n", mod->name,
00120                                         alias_for_module(mod->name));
00121                         else
00122                                 printf("   %-16s\t%s\n", mod->name, mod->desc);
00123         }
00124 
00125         printf("\n  Try %s %s for more information on how `%s' works.\n\n",
00126                 cmdname, cmdname, cmdname);
00127 }
00128 
00130 static void help_survival(void)
00131 {
00132         printf("Don't panic!\n\n");
00133 
00134         printf("This is Bdsh, the Brain dead shell, currently "
00135             "the primary user interface to HelenOS. Bdsh allows you to enter "
00136             "commands and supports history (Up, Down arrow keys), "
00137             "line editing (Left Arrow, Right Arrow, Home, End, Backspace), "
00138             "selection (Shift + movement keys), copy and paste (Ctrl-C, "
00139             "Ctrl-V), similar to common desktop environments.\n\n");
00140 
00141         printf("The most basic filesystem commands are Bdsh builtins. Type "
00142             "'help commands' [Enter] to see the list of Bdsh builtin commands. "
00143             "Other commands are external executables located in the /app and "
00144             "/srv directories. Type 'ls /app' [Enter] and 'ls /srv' [Enter] "
00145             "to see their list. You can execute an external command simply "
00146             "by entering its name (e.g. type 'tetris' [Enter]).\n\n");
00147 
00148         printf("HelenOS has virtual consoles (VCs). You can switch between "
00149             "these using the F1-F11 keys.\n\n");
00150 
00151         printf("This is but a small glimpse of what you can do with HelenOS. "
00152             "To learn more please point your browser to the HelenOS User's "
00153             "Guide: http://trac.helenos.org/trac.fcgi/wiki/UsersGuide\n\n");
00154 }
00155 
00156 int cmd_help(char *argv[])
00157 {
00158         int rc = 0;
00159         int argc;
00160         int level = HELP_SHORT;
00161 
00162         argc = cli_count_args(argv);
00163 
00164         if (argc > 3) {
00165                 printf("\nToo many arguments to `%s', try:\n", cmdname);
00166                 help_cmd_help(HELP_SHORT);
00167                 return CMD_FAILURE;
00168         }
00169 
00170         if (argc == 3) {
00171                 if (!str_cmp("extended", argv[2]))
00172                         level = HELP_LONG;
00173                 else
00174                         level = HELP_SHORT;
00175         }
00176 
00177         if (argc > 1) {
00178                 rc = is_mod_or_builtin(argv[1]);
00179                 switch (rc) {
00180                 case HELP_IS_RUBBISH:
00181                         printf("Invalid topic %s\n", argv[1]);
00182                         return CMD_FAILURE;
00183                 case HELP_IS_COMMANDS:
00184                         help_commands();
00185                         return CMD_SUCCESS;
00186                 case HELP_IS_MODULE:
00187                         help_module(mod_switch, level);
00188                         return CMD_SUCCESS;
00189                 case HELP_IS_BUILTIN:
00190                         help_builtin(mod_switch, level);
00191                         return CMD_SUCCESS;
00192                 }
00193         }
00194 
00195         help_survival();
00196 
00197         return CMD_SUCCESS;
00198 }

Generated on Thu Jun 2 07:45:40 2011 for HelenOS/USB by  doxygen 1.4.7