[63087f1d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 Matthieu Riolo
|
---|
| 3 | * All rights reserved.
|
---|
[60c332e] | 4 | *
|
---|
[63087f1d] | 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 | */
|
---|
[60c332e] | 28 |
|
---|
| 29 | #include <stdio.h>
|
---|
| 30 | #include <stdlib.h>
|
---|
| 31 | #include <str.h>
|
---|
| 32 | #include "config.h"
|
---|
| 33 | #include "util.h"
|
---|
| 34 | #include "errors.h"
|
---|
| 35 | #include "entry.h"
|
---|
| 36 | #include "alias.h"
|
---|
| 37 | #include "cmds.h"
|
---|
| 38 |
|
---|
| 39 | #include <adt/odict.h>
|
---|
| 40 |
|
---|
| 41 | static const char *cmdname = "alias";
|
---|
[a02aa5c] | 42 | static const char *alias_format = "%s='%s'\n";
|
---|
[60c332e] | 43 |
|
---|
| 44 | static void list_aliases()
|
---|
| 45 | {
|
---|
| 46 | odlink_t *alias_link = odict_first(&alias_dict);
|
---|
| 47 | while (alias_link != NULL) {
|
---|
| 48 | alias_t *data = odict_get_instance(alias_link, alias_t, odict);
|
---|
| 49 | printf(alias_format, data->name, data->value);
|
---|
| 50 | alias_link = odict_next(alias_link, &alias_dict);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[a02aa5c] | 54 | static bool print_alias(const char *name)
|
---|
[60c332e] | 55 | {
|
---|
[a02aa5c] | 56 | odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)name, NULL);
|
---|
[60c332e] | 57 | if (alias_link != NULL) {
|
---|
[a02aa5c] | 58 | alias_t *data = odict_get_instance(alias_link, alias_t, odict);
|
---|
[60c332e] | 59 | printf(alias_format, data->name, data->value);
|
---|
[229d114e] | 60 | return true;
|
---|
[60c332e] | 61 | }
|
---|
[ab936440] | 62 |
|
---|
[a8c09f91] | 63 | cli_error(CL_ENOENT, "%s: No alias with the name '%s' exists\n", cmdname, name);
|
---|
[229d114e] | 64 | return false;
|
---|
[60c332e] | 65 | }
|
---|
| 66 |
|
---|
[a02aa5c] | 67 | static void set_alias(const char *name, const char *value)
|
---|
[60c332e] | 68 | {
|
---|
[a02aa5c] | 69 | odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)name, NULL);
|
---|
[4bf08aa5] | 70 |
|
---|
| 71 | if (alias_link != NULL) {
|
---|
[506cbf0] | 72 | /* update existing value */
|
---|
[a02aa5c] | 73 | alias_t *data = odict_get_instance(alias_link, alias_t, odict);
|
---|
[4bf08aa5] | 74 | free(data->value);
|
---|
| 75 | data->value = str_dup(value);
|
---|
[08bd04c] | 76 | } else {
|
---|
[506cbf0] | 77 | /* add new value */
|
---|
[a02aa5c] | 78 | alias_t *data = (alias_t *)calloc(1, sizeof(alias_t));
|
---|
[4bf08aa5] | 79 | data->name = str_dup(name);
|
---|
| 80 | data->value = str_dup(value);
|
---|
| 81 |
|
---|
| 82 | odict_insert(&data->odict, &alias_dict, NULL);
|
---|
| 83 | }
|
---|
[60c332e] | 84 | }
|
---|
| 85 |
|
---|
[7f7817a9] | 86 | static bool valide_name(const char *name)
|
---|
| 87 | {
|
---|
| 88 | while (*name != '\0') {
|
---|
| 89 | if (*name == '/')
|
---|
| 90 | return false;
|
---|
| 91 | if (*name == ' ')
|
---|
| 92 | return false;
|
---|
| 93 | if (*name == '\"')
|
---|
| 94 | return false;
|
---|
| 95 | if (*name == '\'')
|
---|
| 96 | return false;
|
---|
| 97 | if (*name == '|')
|
---|
| 98 | return false;
|
---|
| 99 |
|
---|
| 100 | name++;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | return true;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[60c332e] | 106 | /* Dispays help for alias in various levels */
|
---|
| 107 | void help_cmd_alias(unsigned int level)
|
---|
| 108 | {
|
---|
[3301452] | 109 | if (level == HELP_SHORT) {
|
---|
| 110 | printf("`%s' sets an alias, displays an alias or lists all aliases\n", cmdname);
|
---|
| 111 | } else {
|
---|
| 112 | help_cmd_alias(HELP_SHORT);
|
---|
| 113 | printf("Usage: `%s' [newalias[='existingCMD --flags] ...]'\n\n"
|
---|
| 114 | "If no parameters are given it will display all existing aliases.\n"
|
---|
| 115 | "If a parameter without an assignment is given, the value of the given alias will be returned.\n"
|
---|
| 116 | "If a parameter with an assignment is given, the alias will be created or updated for the given value. "
|
---|
| 117 | "It is possible to create an alias to a different alias. A circularity will prevent an alias to be resolved.\n",
|
---|
| 118 | cmdname);
|
---|
| 119 | }
|
---|
[60c332e] | 120 | }
|
---|
| 121 |
|
---|
| 122 | /* Main entry point for alias, accepts an array of arguments */
|
---|
| 123 | int cmd_alias(char **argv)
|
---|
| 124 | {
|
---|
[ab936440] | 125 |
|
---|
[4bf08aa5] | 126 | if (argv[1] == NULL) {
|
---|
[60c332e] | 127 | list_aliases();
|
---|
| 128 | return CMD_SUCCESS;
|
---|
| 129 | }
|
---|
[229d114e] | 130 |
|
---|
[60c332e] | 131 | size_t i;
|
---|
[4bf08aa5] | 132 | for (i = 1; argv[i] != NULL; i++) {
|
---|
[a02aa5c] | 133 | char *name = str_dup(argv[i]);
|
---|
| 134 | char *value;
|
---|
[08bd04c] | 135 | if ((value = str_chr(name, '=')) != NULL) {
|
---|
| 136 | name[value - name] = '\0';
|
---|
[7f7817a9] | 137 | if (!valide_name(name)) {
|
---|
| 138 | cli_error(CL_EFAIL, "%s: invalid alias name given\n", cmdname);
|
---|
| 139 | free(name);
|
---|
| 140 | return CMD_FAILURE;
|
---|
| 141 | }
|
---|
[ab936440] | 142 |
|
---|
[08bd04c] | 143 | set_alias(name, value + 1);
|
---|
| 144 | } else {
|
---|
[a02aa5c] | 145 | if (!print_alias(name)) {
|
---|
[08bd04c] | 146 | free(name);
|
---|
[229d114e] | 147 | return CMD_FAILURE;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
[08bd04c] | 150 |
|
---|
| 151 | free(name);
|
---|
[4bf08aa5] | 152 | }
|
---|
[ab936440] | 153 |
|
---|
[60c332e] | 154 | return CMD_SUCCESS;
|
---|
| 155 | }
|
---|