Subversion Repositories HelenOS

Rev

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

Rev 4153 Rev 4327
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
/* NOTES:
31
/* NOTES:
32
 * module_* functions are pretty much identical to builtin_* functions at this
32
 * module_* functions are pretty much identical to builtin_* functions at this
33
 * point. On the surface, it would appear that making each function dual purpose
33
 * point. On the surface, it would appear that making each function dual purpose
34
 * would be economical.
34
 * would be economical.
35
 *
35
 *
36
 * These are kept separate because the structures (module_t and builtin_t) may
36
 * These are kept separate because the structures (module_t and builtin_t) may
37
 * grow apart and become rather different, even though they're identical at this
37
 * grow apart and become rather different, even though they're identical at this
38
 * point.
38
 * point.
39
 *
39
 *
40
 * To keep things easy to hack, everything is separated. In reality this only adds
40
 * To keep things easy to hack, everything is separated. In reality this only adds
41
 * 6 - 8 extra functions, but keeps each function very easy to read and modify. */
41
 * 6 - 8 extra functions, but keeps each function very easy to read and modify. */
42
 
42
 
43
/* TODO:
43
/* TODO:
44
 * Many of these could be unsigned, provided the modules and builtins themselves
44
 * Many of these could be unsigned, provided the modules and builtins themselves
45
 * can follow suit. Long term goal. */
45
 * can follow suit. Long term goal. */
46
 
46
 
47
#include <stdio.h>
47
#include <stdio.h>
48
#include <stdlib.h>
48
#include <stdlib.h>
49
#include <string.h>
49
#include <string.h>
50
#include "errors.h"
50
#include "errors.h"
51
#include "cmds.h"
51
#include "cmds.h"
52
#include "module_aliases.h"
52
#include "module_aliases.h"
53
 
53
 
54
extern volatile unsigned int cli_interactive;
54
extern volatile unsigned int cli_interactive;
55
 
55
 
56
/* Checks if an entry function matching command exists in modules[], if so
56
/* Checks if an entry function matching command exists in modules[], if so
57
 * its position in the array is returned */
57
 * its position in the array is returned */
58
int is_module(const char *command)
58
int is_module(const char *command)
59
{
59
{
60
    module_t *mod;
60
    module_t *mod;
61
    unsigned int i = 0;
61
    unsigned int i = 0;
62
 
62
 
63
    if (NULL == command)
63
    if (NULL == command)
64
        return -2;
64
        return -2;
65
 
65
 
66
    for (mod = modules; mod->name != NULL; mod++, i++) {
66
    for (mod = modules; mod->name != NULL; mod++, i++) {
67
        if (!strcmp(mod->name, command))
67
        if (!str_cmp(mod->name, command))
68
            return i;
68
            return i;
69
    }
69
    }
70
 
70
 
71
    return -1;
71
    return -1;
72
}
72
}
73
 
73
 
74
/* Checks if a module is an alias (sharing an entry point with another
74
/* Checks if a module is an alias (sharing an entry point with another
75
 * module). Returns 1 if so */
75
 * module). Returns 1 if so */
76
int is_module_alias(const char *command)
76
int is_module_alias(const char *command)
77
{
77
{
78
    unsigned int i = 0;
78
    unsigned int i = 0;
79
 
79
 
80
    if (NULL == command)
80
    if (NULL == command)
81
        return -1;
81
        return -1;
82
 
82
 
83
    for(i=0; mod_aliases[i] != NULL; i+=2) {
83
    for(i=0; mod_aliases[i] != NULL; i+=2) {
84
        if (!strcmp(mod_aliases[i], command))
84
        if (!str_cmp(mod_aliases[i], command))
85
            return 1;
85
            return 1;
86
    }
86
    }
87
 
87
 
88
    return 0;
88
    return 0;
89
}
89
}
90
 
90
 
91
/* Returns the name of the module that an alias points to */
91
/* Returns the name of the module that an alias points to */
92
char *alias_for_module(const char *command)
92
char *alias_for_module(const char *command)
93
{
93
{
94
    unsigned int i = 0;
94
    unsigned int i = 0;
95
 
95
 
96
    if (NULL == command)
96
    if (NULL == command)
97
        return (char *)NULL;
97
        return (char *)NULL;
98
 
98
 
99
    for(i=0; mod_aliases[i] != NULL; i++) {
99
    for(i=0; mod_aliases[i] != NULL; i++) {
100
        if (!strcmp(mod_aliases[i], command))
100
        if (!str_cmp(mod_aliases[i], command))
101
            return (char *)mod_aliases[++i];
101
            return (char *)mod_aliases[++i];
102
        i++;
102
        i++;
103
    }
103
    }
104
 
104
 
105
    return (char *)NULL;
105
    return (char *)NULL;
106
}
106
}
107
 
107
 
108
 
108
 
109
/* Invokes the 'help' entry function for the module at position (int) module,
109
/* Invokes the 'help' entry function for the module at position (int) module,
110
 * which wants an unsigned int to determine brief or extended display. */
110
 * which wants an unsigned int to determine brief or extended display. */
111
int help_module(int module, unsigned int extended)
111
int help_module(int module, unsigned int extended)
112
{
112
{
113
    module_t *mod = modules;
113
    module_t *mod = modules;
114
 
114
 
115
    mod += module;
115
    mod += module;
116
 
116
 
117
    if (NULL != mod->help) {
117
    if (NULL != mod->help) {
118
        mod->help(extended);
118
        mod->help(extended);
119
        return CL_EOK;
119
        return CL_EOK;
120
    } else
120
    } else
121
        return CL_ENOENT;
121
        return CL_ENOENT;
122
}
122
}
123
 
123
 
124
/* Invokes the module entry point modules[module], passing argv[] as an argument
124
/* Invokes the module entry point modules[module], passing argv[] as an argument
125
 * stack. */
125
 * stack. */
126
int run_module(int module, char *argv[])
126
int run_module(int module, char *argv[])
127
{
127
{
128
    module_t *mod = modules;
128
    module_t *mod = modules;
129
 
129
 
130
    mod += module;
130
    mod += module;
131
 
131
 
132
    if (NULL != mod->entry)
132
    if (NULL != mod->entry)
133
        return ((int)mod->entry(argv));
133
        return ((int)mod->entry(argv));
134
 
134
 
135
    return CL_ENOENT;
135
    return CL_ENOENT;
136
}
136
}
137
 
137