Subversion Repositories HelenOS

Rev

Rev 3364 | Rev 3376 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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