Subversion Repositories HelenOS

Rev

Rev 4341 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3265 post 1
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
2
 * All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions are met:
6
 *
7
 * Redistributions of source code must retain the above copyright notice, this
8
 * list of conditions and the following disclaimer.
9
 *
10
 * Redistributions in binary form must reproduce the above copyright notice,
11
 * this list of conditions and the following disclaimer in the documentation
12
 * and/or other materials provided with the distribution.
13
 *
14
 * Neither the name of the original program's authors nor the names of its
15
 * contributors may be used to endorse or promote products derived from this
16
 * software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 */
30
 
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <string.h>
3376 post 34
 
35
#include "config.h"
3265 post 36
#include "entry.h"
37
#include "help.h"
38
#include "cmds.h"
39
#include "modules.h"
40
#include "builtins.h"
41
#include "errors.h"
3376 post 42
#include "util.h"
3265 post 43
 
3269 post 44
static char *cmdname = "help";
3265 post 45
extern const char *progname;
46
 
47
#define HELP_IS_MODULE   1
48
#define HELP_IS_BUILTIN  0
49
#define HELP_IS_RUBBISH  -1
50
 
51
volatile int mod_switch = -1;
52
 
53
/* Just use a pointer here, no need for mod_switch */
3364 post 54
static int is_mod_or_builtin(char *cmd)
3265 post 55
{
56
    int rc = HELP_IS_RUBBISH;
57
 
58
    rc = is_builtin(cmd);
59
    if (rc > -1) {
60
        mod_switch = rc;
61
        return HELP_IS_BUILTIN;
62
    }
63
    rc = is_module(cmd);
64
    if (rc > -1) {
65
        mod_switch = rc;
66
        return HELP_IS_MODULE;
67
    }
68
 
69
    return HELP_IS_RUBBISH;
70
}
71
 
3448 svoboda 72
void help_cmd_help(unsigned int level)
3265 post 73
{
74
    if (level == HELP_SHORT) {
75
        printf(
76
        "\n  %s [command] <extended>\n"
77
        "  Use help [command] extended for detailed help on [command] "
78
        ", even `help'\n\n", cmdname);
79
    } else {
80
        printf(
81
        "\n  `%s' - shows help for commands\n"
82
        "  Examples:\n"
83
        "   %s [command]           Show help for [command]\n"
84
        "   %s [command] extended  Show extended help for [command]\n"
85
        "\n  If no argument is given to %s, a list of commands are shown\n\n",
86
        cmdname, cmdname, cmdname, cmdname);
87
    }
88
 
3448 svoboda 89
    return;
3265 post 90
}
91
 
3448 svoboda 92
int cmd_help(char *argv[])
3265 post 93
{
94
    module_t *mod;
95
    builtin_t *cmd;
96
    unsigned int i = 0;
97
    int rc = 0;
98
    int argc;
99
    int level = HELP_SHORT;
100
 
3376 post 101
    argc = cli_count_args(argv);
3265 post 102
 
103
    if (argc > 3) {
104
        printf("\nToo many arguments to `%s', try:\n", cmdname);
105
        help_cmd_help(HELP_SHORT);
106
        return CMD_FAILURE;
107
    }
108
 
109
    if (argc == 3) {
4348 svoboda 110
        if (!str_cmp("extended", argv[2]))
3265 post 111
            level = HELP_LONG;
112
        else
113
            level = HELP_SHORT;
114
    }
115
 
116
    if (argc > 1) {
117
        rc = is_mod_or_builtin(argv[1]);
118
        switch (rc) {
119
        case HELP_IS_RUBBISH:
120
            printf("Invalid command %s\n", argv[1]);
121
            return CMD_FAILURE;
122
        case HELP_IS_MODULE:
123
            help_module(mod_switch, level);
124
            return CMD_SUCCESS;
125
        case HELP_IS_BUILTIN:
126
            help_builtin(mod_switch, level);
127
            return CMD_SUCCESS;
128
        }
129
    }
130
 
3366 post 131
    printf("\n  Available commands are:\n");
132
    printf("  ------------------------------------------------------------\n");
3265 post 133
 
134
    /* First, show a list of built in commands that are available in this mode */
135
    for (cmd = builtins; cmd->name != NULL; cmd++, i++) {
136
            if (is_builtin_alias(cmd->name))
137
                printf("   %-16s\tAlias for `%s'\n", cmd->name,
138
                    alias_for_builtin(cmd->name));
139
            else
140
                printf("   %-16s\t%s\n", cmd->name, cmd->desc);
141
    }
142
 
143
    i = 0;
144
 
145
    /* Now, show a list of module commands that are available in this mode */
146
    for (mod = modules; mod->name != NULL; mod++, i++) {
147
            if (is_module_alias(mod->name))
148
                printf("   %-16s\tAlias for `%s'\n", mod->name,
149
                    alias_for_module(mod->name));
150
            else
151
                printf("   %-16s\t%s\n", mod->name, mod->desc);
152
    }
153
 
3366 post 154
    printf("\n  Try %s %s for more information on how `%s' works.\n\n",
155
        cmdname, cmdname, cmdname);
3265 post 156
 
157
    return CMD_SUCCESS;
158
}