00001 /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> 00002 * All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * 00007 * Redistributions of source code must retain the above copyright notice, this 00008 * list of conditions and the following disclaimer. 00009 * 00010 * Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 00014 * Neither the name of the original program's authors nor the names of its 00015 * contributors may be used to endorse or promote products derived from this 00016 * software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00022 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00028 * POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00031 /* NOTES: 00032 * module_* functions are pretty much identical to builtin_* functions at this 00033 * point. On the surface, it would appear that making each function dual purpose 00034 * would be economical. 00035 * 00036 * These are kept separate because the structures (module_t and builtin_t) may 00037 * grow apart and become rather different, even though they're identical at this 00038 * point. 00039 * 00040 * To keep things easy to hack, everything is separated. In reality this only adds 00041 * 6 - 8 extra functions, but keeps each function very easy to read and modify. */ 00042 00043 /* TODO: 00044 * Many of these could be unsigned, provided the modules and builtins themselves 00045 * can follow suit. Long term goal. */ 00046 00047 #include <stdio.h> 00048 #include <stdlib.h> 00049 #include <str.h> 00050 #include "errors.h" 00051 #include "cmds.h" 00052 #include "module_aliases.h" 00053 00054 extern volatile unsigned int cli_interactive; 00055 00056 /* Checks if an entry function matching command exists in modules[], if so 00057 * its position in the array is returned */ 00058 int is_module(const char *command) 00059 { 00060 module_t *mod; 00061 unsigned int i = 0; 00062 00063 if (NULL == command) 00064 return -2; 00065 00066 for (mod = modules; mod->name != NULL; mod++, i++) { 00067 if (!str_cmp(mod->name, command)) 00068 return i; 00069 } 00070 00071 return -1; 00072 } 00073 00074 /* Checks if a module is an alias (sharing an entry point with another 00075 * module). Returns 1 if so */ 00076 int is_module_alias(const char *command) 00077 { 00078 unsigned int i = 0; 00079 00080 if (NULL == command) 00081 return -1; 00082 00083 for(i=0; mod_aliases[i] != NULL; i+=2) { 00084 if (!str_cmp(mod_aliases[i], command)) 00085 return 1; 00086 } 00087 00088 return 0; 00089 } 00090 00091 /* Returns the name of the module that an alias points to */ 00092 char *alias_for_module(const char *command) 00093 { 00094 unsigned int i = 0; 00095 00096 if (NULL == command) 00097 return (char *)NULL; 00098 00099 for(i=0; mod_aliases[i] != NULL; i++) { 00100 if (!str_cmp(mod_aliases[i], command)) 00101 return (char *)mod_aliases[++i]; 00102 i++; 00103 } 00104 00105 return (char *)NULL; 00106 } 00107 00108 00109 /* Invokes the 'help' entry function for the module at position (int) module, 00110 * which wants an unsigned int to determine brief or extended display. */ 00111 int help_module(int module, unsigned int extended) 00112 { 00113 module_t *mod = modules; 00114 00115 mod += module; 00116 00117 if (NULL != mod->help) { 00118 mod->help(extended); 00119 return CL_EOK; 00120 } else 00121 return CL_ENOENT; 00122 } 00123 00124 /* Invokes the module entry point modules[module], passing argv[] as an argument 00125 * stack. */ 00126 int run_module(int module, char *argv[]) 00127 { 00128 module_t *mod = modules; 00129 00130 mod += module; 00131 00132 if (NULL != mod->entry) 00133 return ((int)mod->entry(argv)); 00134 00135 return CL_ENOENT; 00136 }