builtin_cmds.c

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 /* Almost identical (for now) to mod_cmds.c , however this will not be the case
00032  * soon as builtin_t is going to grow way beyond module_t */
00033 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <str.h>
00037 #include "errors.h"
00038 #include "cmds.h"
00039 #include "builtin_aliases.h"
00040 
00041 extern volatile unsigned int cli_interactive;
00042 
00043 int is_builtin(const char *command)
00044 {
00045         builtin_t *cmd;
00046         unsigned int i = 0;
00047 
00048         if (NULL == command)
00049                 return -2;
00050 
00051         for (cmd = builtins; cmd->name != NULL; cmd++, i++) {
00052                 if (!str_cmp(cmd->name, command))
00053                         return i;
00054         }
00055 
00056         return -1;
00057 }
00058 
00059 int is_builtin_alias(const char *command)
00060 {
00061         unsigned int i = 0;
00062 
00063         if (NULL == command)
00064                 return -1;
00065 
00066         for(i=0; builtin_aliases[i] != NULL; i+=2) {
00067                 if (!str_cmp(builtin_aliases[i], command))
00068                         return 1;
00069         }
00070 
00071         return 0;
00072 }
00073 
00074 char *alias_for_builtin(const char *command)
00075 {
00076         unsigned int i = 0;
00077 
00078         if (NULL == command)
00079                 return (char *)NULL;
00080 
00081         for(i=0; builtin_aliases[i] != NULL; i++) {
00082                 if (!str_cmp(builtin_aliases[i], command))
00083                         return (char *)builtin_aliases[++i];
00084                 i++;
00085         }
00086 
00087         return (char *)NULL;
00088 }
00089 
00090 int help_builtin(int builtin, unsigned int extended)
00091 {
00092         builtin_t *cmd = builtins;
00093 
00094         cmd += builtin;
00095 
00096         if (NULL != cmd->help) {
00097                 cmd->help(extended);
00098                 return CL_EOK;
00099         } else
00100                 return CL_ENOENT;
00101 }
00102 
00103 int run_builtin(int builtin, char *argv[], cliuser_t *usr)
00104 {
00105         builtin_t *cmd = builtins;
00106 
00107         cmd += builtin;
00108 
00109         if (NULL != cmd->entry)
00110                 return((int)cmd->entry(argv, usr));
00111 
00112         return CL_ENOENT;
00113 }

Generated on Thu Jun 2 07:45:40 2011 for HelenOS/USB by  doxygen 1.4.7