| 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 | /*
|
|---|
| 30 | * NOTES:
|
|---|
| 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
|
|---|
| 40 | * 6 - 8 extra functions, but keeps each function very easy to read and modify.
|
|---|
| 41 | */
|
|---|
| 42 |
|
|---|
| 43 | /*
|
|---|
| 44 | * TODO:
|
|---|
| 45 | * Many of these could be unsigned, provided the modules and builtins themselves
|
|---|
| 46 | * can follow suit. Long term goal.
|
|---|
| 47 | */
|
|---|
| 48 |
|
|---|
| 49 | #include <stdio.h>
|
|---|
| 50 | #include <stdlib.h>
|
|---|
| 51 | #include <stddef.h>
|
|---|
| 52 | #include <str.h>
|
|---|
| 53 | #include "errors.h"
|
|---|
| 54 | #include "cmds.h"
|
|---|
| 55 | #include "module_aliases.h"
|
|---|
| 56 |
|
|---|
| 57 | extern volatile unsigned int cli_interactive;
|
|---|
| 58 |
|
|---|
| 59 | /** Checks if an entry function matching command exists in modules[]
|
|---|
| 60 | *
|
|---|
| 61 | * If so, its position in the array is returned
|
|---|
| 62 | */
|
|---|
| 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++) {
|
|---|
| 72 | if (!str_cmp(mod->name, command))
|
|---|
| 73 | return i;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | return -1;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /*
|
|---|
| 80 | * Checks if a module is an alias (sharing an entry point with another
|
|---|
| 81 | * module). Returns 1 if so
|
|---|
| 82 | */
|
|---|
| 83 | int is_module_alias(const char *command)
|
|---|
| 84 | {
|
|---|
| 85 | unsigned int i = 0;
|
|---|
| 86 |
|
|---|
| 87 | if (NULL == command)
|
|---|
| 88 | return -1;
|
|---|
| 89 |
|
|---|
| 90 | for (i = 0; mod_aliases[i] != NULL; i += 2) {
|
|---|
| 91 | if (!str_cmp(mod_aliases[i], command))
|
|---|
| 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 |
|
|---|
| 106 | for (i = 0; mod_aliases[i] != NULL; i += 2) {
|
|---|
| 107 | if (!str_cmp(mod_aliases[i], command))
|
|---|
| 108 | return (char *)mod_aliases[i + 1];
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | return (char *)NULL;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 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 | */
|
|---|
| 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;
|
|---|
| 127 | } else {
|
|---|
| 128 | return CL_ENOENT;
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /** Invokes the module entry point modules[module]
|
|---|
| 133 | *
|
|---|
| 134 | * passing argv[] as an argument stack.
|
|---|
| 135 | */
|
|---|
| 136 | int run_module(int module, char *argv[], iostate_t *new_iostate)
|
|---|
| 137 | {
|
|---|
| 138 | int rc;
|
|---|
| 139 | module_t *mod = modules;
|
|---|
| 140 |
|
|---|
| 141 | mod += module;
|
|---|
| 142 |
|
|---|
| 143 | iostate_t *old_iostate = get_iostate();
|
|---|
| 144 | set_iostate(new_iostate);
|
|---|
| 145 |
|
|---|
| 146 | if (NULL != mod->entry) {
|
|---|
| 147 | rc = ((int)mod->entry(argv));
|
|---|
| 148 | } else {
|
|---|
| 149 | rc = CL_ENOENT;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | set_iostate(old_iostate);
|
|---|
| 153 |
|
|---|
| 154 | return rc;
|
|---|
| 155 | }
|
|---|