[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 | * Almost identical (for now) to mod_cmds.c, however this will not be the case
|
---|
| 31 | * soon as builtin_t is going to grow way beyond module_t
|
---|
| 32 | */
|
---|
[216d6fc] | 33 |
|
---|
| 34 | #include <stdio.h>
|
---|
| 35 | #include <stdlib.h>
|
---|
[582a0b8] | 36 | #include <stddef.h>
|
---|
[19f857a] | 37 | #include <str.h>
|
---|
[216d6fc] | 38 | #include "errors.h"
|
---|
| 39 | #include "cmds.h"
|
---|
| 40 | #include "builtin_aliases.h"
|
---|
[6ea9a1d] | 41 | #include "scli.h"
|
---|
[216d6fc] | 42 |
|
---|
| 43 | extern volatile unsigned int cli_interactive;
|
---|
| 44 |
|
---|
| 45 | int is_builtin(const char *command)
|
---|
| 46 | {
|
---|
| 47 | builtin_t *cmd;
|
---|
| 48 | unsigned int i = 0;
|
---|
| 49 |
|
---|
| 50 | if (NULL == command)
|
---|
| 51 | return -2;
|
---|
| 52 |
|
---|
| 53 | for (cmd = builtins; cmd->name != NULL; cmd++, i++) {
|
---|
[92fd52d7] | 54 | if (!str_cmp(cmd->name, command))
|
---|
[216d6fc] | 55 | return i;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | return -1;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | int is_builtin_alias(const char *command)
|
---|
| 62 | {
|
---|
| 63 | unsigned int i = 0;
|
---|
| 64 |
|
---|
| 65 | if (NULL == command)
|
---|
| 66 | return -1;
|
---|
| 67 |
|
---|
[1433ecda] | 68 | for (i = 0; builtin_aliases[i] != NULL; i += 2) {
|
---|
[92fd52d7] | 69 | if (!str_cmp(builtin_aliases[i], command))
|
---|
[216d6fc] | 70 | return 1;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | return 0;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | char *alias_for_builtin(const char *command)
|
---|
| 77 | {
|
---|
| 78 | unsigned int i = 0;
|
---|
| 79 |
|
---|
| 80 | if (NULL == command)
|
---|
| 81 | return (char *)NULL;
|
---|
| 82 |
|
---|
[1433ecda] | 83 | for (i = 0; builtin_aliases[i] != NULL; i += 2) {
|
---|
[92fd52d7] | 84 | if (!str_cmp(builtin_aliases[i], command))
|
---|
[1433ecda] | 85 | return (char *)builtin_aliases[i + 1];
|
---|
[216d6fc] | 86 | }
|
---|
| 87 |
|
---|
| 88 | return (char *)NULL;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | int help_builtin(int builtin, unsigned int extended)
|
---|
| 92 | {
|
---|
| 93 | builtin_t *cmd = builtins;
|
---|
| 94 |
|
---|
| 95 | cmd += builtin;
|
---|
| 96 |
|
---|
| 97 | if (NULL != cmd->help) {
|
---|
| 98 | cmd->help(extended);
|
---|
| 99 | return CL_EOK;
|
---|
[b0889d05] | 100 | } else {
|
---|
[216d6fc] | 101 | return CL_ENOENT;
|
---|
[b0889d05] | 102 | }
|
---|
[216d6fc] | 103 | }
|
---|
| 104 |
|
---|
[6ea9a1d] | 105 | int run_builtin(int builtin, char *argv[], cliuser_t *usr, iostate_t *new_iostate)
|
---|
[216d6fc] | 106 | {
|
---|
[6ea9a1d] | 107 | int rc;
|
---|
[216d6fc] | 108 | builtin_t *cmd = builtins;
|
---|
| 109 |
|
---|
| 110 | cmd += builtin;
|
---|
[a35b458] | 111 |
|
---|
[6ea9a1d] | 112 | iostate_t *old_iostate = get_iostate();
|
---|
| 113 | set_iostate(new_iostate);
|
---|
[a35b458] | 114 |
|
---|
[6ea9a1d] | 115 | if (NULL != cmd->entry) {
|
---|
| 116 | rc = ((int)cmd->entry(argv, usr));
|
---|
[b0889d05] | 117 | } else {
|
---|
[6ea9a1d] | 118 | rc = CL_ENOENT;
|
---|
| 119 | }
|
---|
[a35b458] | 120 |
|
---|
[6ea9a1d] | 121 | set_iostate(old_iostate);
|
---|
[216d6fc] | 122 |
|
---|
[6ea9a1d] | 123 | return rc;
|
---|
[216d6fc] | 124 | }
|
---|