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