1 | /*
|
---|
2 | * Copyright (c) 2008 Tim Post
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
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.
|
---|
16 | *
|
---|
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.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /* Almost identical (for now) to mod_cmds.c , however this will not be the case
|
---|
30 | * soon as builtin_t is going to grow way beyond module_t */
|
---|
31 |
|
---|
32 | #include <stdio.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 | #include <str.h>
|
---|
35 | #include "errors.h"
|
---|
36 | #include "cmds.h"
|
---|
37 | #include "builtin_aliases.h"
|
---|
38 | #include "scli.h"
|
---|
39 |
|
---|
40 | extern volatile unsigned int cli_interactive;
|
---|
41 |
|
---|
42 | int is_builtin(const char *command)
|
---|
43 | {
|
---|
44 | builtin_t *cmd;
|
---|
45 | unsigned int i = 0;
|
---|
46 |
|
---|
47 | if (NULL == command)
|
---|
48 | return -2;
|
---|
49 |
|
---|
50 | for (cmd = builtins; cmd->name != NULL; cmd++, i++) {
|
---|
51 | if (!str_cmp(cmd->name, command))
|
---|
52 | return i;
|
---|
53 | }
|
---|
54 |
|
---|
55 | return -1;
|
---|
56 | }
|
---|
57 |
|
---|
58 | int is_builtin_alias(const char *command)
|
---|
59 | {
|
---|
60 | unsigned int i = 0;
|
---|
61 |
|
---|
62 | if (NULL == command)
|
---|
63 | return -1;
|
---|
64 |
|
---|
65 | for(i=0; builtin_aliases[i] != NULL; i+=2) {
|
---|
66 | if (!str_cmp(builtin_aliases[i], command))
|
---|
67 | return 1;
|
---|
68 | }
|
---|
69 |
|
---|
70 | return 0;
|
---|
71 | }
|
---|
72 |
|
---|
73 | char *alias_for_builtin(const char *command)
|
---|
74 | {
|
---|
75 | unsigned int i = 0;
|
---|
76 |
|
---|
77 | if (NULL == command)
|
---|
78 | return (char *)NULL;
|
---|
79 |
|
---|
80 | for(i=0; builtin_aliases[i] != NULL; i++) {
|
---|
81 | if (!str_cmp(builtin_aliases[i], command))
|
---|
82 | return (char *)builtin_aliases[++i];
|
---|
83 | i++;
|
---|
84 | }
|
---|
85 |
|
---|
86 | return (char *)NULL;
|
---|
87 | }
|
---|
88 |
|
---|
89 | int help_builtin(int builtin, unsigned int extended)
|
---|
90 | {
|
---|
91 | builtin_t *cmd = builtins;
|
---|
92 |
|
---|
93 | cmd += builtin;
|
---|
94 |
|
---|
95 | if (NULL != cmd->help) {
|
---|
96 | cmd->help(extended);
|
---|
97 | return CL_EOK;
|
---|
98 | } else
|
---|
99 | return CL_ENOENT;
|
---|
100 | }
|
---|
101 |
|
---|
102 | int run_builtin(int builtin, char *argv[], cliuser_t *usr, iostate_t *new_iostate)
|
---|
103 | {
|
---|
104 | int rc;
|
---|
105 | builtin_t *cmd = builtins;
|
---|
106 |
|
---|
107 | cmd += builtin;
|
---|
108 |
|
---|
109 | iostate_t *old_iostate = get_iostate();
|
---|
110 | set_iostate(new_iostate);
|
---|
111 |
|
---|
112 | if (NULL != cmd->entry) {
|
---|
113 | rc = ((int)cmd->entry(argv, usr));
|
---|
114 | }
|
---|
115 | else {
|
---|
116 | rc = CL_ENOENT;
|
---|
117 | }
|
---|
118 |
|
---|
119 | set_iostate(old_iostate);
|
---|
120 |
|
---|
121 | return rc;
|
---|
122 | }
|
---|