[36ab7c7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Tim Post
|
---|
[216d6fc] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
[36ab7c7] | 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
[216d6fc] | 8 | *
|
---|
[36ab7c7] | 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.
|
---|
[216d6fc] | 16 | *
|
---|
[36ab7c7] | 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.
|
---|
[216d6fc] | 27 | */
|
---|
| 28 |
|
---|
[7c3fb9b] | 29 | /*
|
---|
| 30 | * NOTES:
|
---|
[216d6fc] | 31 | * module_* functions are pretty much identical to builtin_* functions at this
|
---|
| 32 | * point. On the surface, it would appear that making each function dual purpose
|
---|
| 33 | * would be economical.
|
---|
| 34 | *
|
---|
| 35 | * These are kept separate because the structures (module_t and builtin_t) may
|
---|
| 36 | * grow apart and become rather different, even though they're identical at this
|
---|
| 37 | * point.
|
---|
| 38 | *
|
---|
| 39 | * To keep things easy to hack, everything is separated. In reality this only adds
|
---|
[7c3fb9b] | 40 | * 6 - 8 extra functions, but keeps each function very easy to read and modify.
|
---|
| 41 | */
|
---|
[216d6fc] | 42 |
|
---|
[7c3fb9b] | 43 | /*
|
---|
| 44 | * TODO:
|
---|
[216d6fc] | 45 | * Many of these could be unsigned, provided the modules and builtins themselves
|
---|
[7c3fb9b] | 46 | * can follow suit. Long term goal.
|
---|
| 47 | */
|
---|
[216d6fc] | 48 |
|
---|
| 49 | #include <stdio.h>
|
---|
| 50 | #include <stdlib.h>
|
---|
[582a0b8] | 51 | #include <stddef.h>
|
---|
[19f857a] | 52 | #include <str.h>
|
---|
[216d6fc] | 53 | #include "errors.h"
|
---|
| 54 | #include "cmds.h"
|
---|
| 55 | #include "module_aliases.h"
|
---|
| 56 |
|
---|
| 57 | extern volatile unsigned int cli_interactive;
|
---|
| 58 |
|
---|
[7c3fb9b] | 59 | /** Checks if an entry function matching command exists in modules[]
|
---|
| 60 | *
|
---|
| 61 | * If so, its position in the array is returned
|
---|
| 62 | */
|
---|
[216d6fc] | 63 | int is_module(const char *command)
|
---|
| 64 | {
|
---|
| 65 | module_t *mod;
|
---|
| 66 | unsigned int i = 0;
|
---|
| 67 |
|
---|
| 68 | if (NULL == command)
|
---|
| 69 | return -2;
|
---|
| 70 |
|
---|
| 71 | for (mod = modules; mod->name != NULL; mod++, i++) {
|
---|
[92fd52d7] | 72 | if (!str_cmp(mod->name, command))
|
---|
[216d6fc] | 73 | return i;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | return -1;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[7c3fb9b] | 79 | /*
|
---|
| 80 | * Checks if a module is an alias (sharing an entry point with another
|
---|
| 81 | * module). Returns 1 if so
|
---|
| 82 | */
|
---|
[216d6fc] | 83 | int is_module_alias(const char *command)
|
---|
| 84 | {
|
---|
| 85 | unsigned int i = 0;
|
---|
| 86 |
|
---|
| 87 | if (NULL == command)
|
---|
| 88 | return -1;
|
---|
| 89 |
|
---|
[1433ecda] | 90 | for (i = 0; mod_aliases[i] != NULL; i += 2) {
|
---|
[92fd52d7] | 91 | if (!str_cmp(mod_aliases[i], command))
|
---|
[216d6fc] | 92 | return 1;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | return 0;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /* Returns the name of the module that an alias points to */
|
---|
| 99 | char *alias_for_module(const char *command)
|
---|
| 100 | {
|
---|
| 101 | unsigned int i = 0;
|
---|
| 102 |
|
---|
| 103 | if (NULL == command)
|
---|
| 104 | return (char *)NULL;
|
---|
| 105 |
|
---|
[1433ecda] | 106 | for (i = 0; mod_aliases[i] != NULL; i += 2) {
|
---|
[92fd52d7] | 107 | if (!str_cmp(mod_aliases[i], command))
|
---|
[1433ecda] | 108 | return (char *)mod_aliases[i + 1];
|
---|
[216d6fc] | 109 | }
|
---|
| 110 |
|
---|
| 111 | return (char *)NULL;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[7c3fb9b] | 114 | /** Invokes the 'help' entry function for the module at position (int) module
|
---|
| 115 | *
|
---|
| 116 | * which wants an unsigned int to determine brief or extended display.
|
---|
| 117 | */
|
---|
[216d6fc] | 118 | int help_module(int module, unsigned int extended)
|
---|
| 119 | {
|
---|
| 120 | module_t *mod = modules;
|
---|
| 121 |
|
---|
| 122 | mod += module;
|
---|
| 123 |
|
---|
| 124 | if (NULL != mod->help) {
|
---|
| 125 | mod->help(extended);
|
---|
| 126 | return CL_EOK;
|
---|
[b0889d05] | 127 | } else {
|
---|
[216d6fc] | 128 | return CL_ENOENT;
|
---|
[b0889d05] | 129 | }
|
---|
[216d6fc] | 130 | }
|
---|
| 131 |
|
---|
[7c3fb9b] | 132 | /** Invokes the module entry point modules[module]
|
---|
| 133 | *
|
---|
| 134 | * passing argv[] as an argument stack.
|
---|
| 135 | */
|
---|
[6ea9a1d] | 136 | int run_module(int module, char *argv[], iostate_t *new_iostate)
|
---|
[216d6fc] | 137 | {
|
---|
[6ea9a1d] | 138 | int rc;
|
---|
[216d6fc] | 139 | module_t *mod = modules;
|
---|
| 140 |
|
---|
| 141 | mod += module;
|
---|
[a35b458] | 142 |
|
---|
[6ea9a1d] | 143 | iostate_t *old_iostate = get_iostate();
|
---|
| 144 | set_iostate(new_iostate);
|
---|
[216d6fc] | 145 |
|
---|
[6ea9a1d] | 146 | if (NULL != mod->entry) {
|
---|
| 147 | rc = ((int)mod->entry(argv));
|
---|
[b0889d05] | 148 | } else {
|
---|
[6ea9a1d] | 149 | rc = CL_ENOENT;
|
---|
| 150 | }
|
---|
[a35b458] | 151 |
|
---|
[6ea9a1d] | 152 | set_iostate(old_iostate);
|
---|
[216d6fc] | 153 |
|
---|
[6ea9a1d] | 154 | return rc;
|
---|
[216d6fc] | 155 | }
|
---|