source: mainline/uspace/app/bdsh/cmds/modules/alias/alias.c

Last change on this file was 46288ee, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Improvement alias/unlias according to feedback

This commit will change the behaviour of unalias
when a not existing aliasname is given. The command
will newly return CMD_FAILURE and print a failure message.
Existings aliases will be deleted.

When updating an existing alias with the command alias,
then the old data structure will only be freed when
str_dup did not fail. This will prevent the data
of being corrupted in case of a memory failure.

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[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
41static const char *cmdname = "alias";
[a02aa5c]42static const char *alias_format = "%s='%s'\n";
[60c332e]43
44static 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]54static 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
[aa3ca1e]67static errno_t 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);
[46288ee]74 char *dup_value = str_dup(value);
[aa3ca1e]75
[46288ee]76 if (dup_value == NULL) {
[aa3ca1e]77 cli_error(CL_ENOMEM, "%s: failing to allocate memory for value\n", cmdname);
78 return ENOMEM;
79 }
[46288ee]80
81 free(data->value);
82 data->value = dup_value;
[08bd04c]83 } else {
[506cbf0]84 /* add new value */
[a02aa5c]85 alias_t *data = (alias_t *)calloc(1, sizeof(alias_t));
[aa3ca1e]86 if (data == NULL) {
87 cli_error(CL_ENOMEM, "%s: failing to allocate memory for data container\n", cmdname);
88 return ENOMEM;
89 }
90
[4bf08aa5]91 data->name = str_dup(name);
[aa3ca1e]92 if (data->name == NULL) {
93 cli_error(CL_ENOMEM, "%s: failing to allocate memory for name\n", cmdname);
94 free(data);
95 return ENOMEM;
96 }
[4bf08aa5]97
[aa3ca1e]98 data->value = str_dup(value);
99 if (data->value == NULL) {
100 cli_error(CL_ENOMEM, "%s: failing to allocate memory for value\n", cmdname);
101 free(data->name);
102 free(data);
103 return ENOMEM;
104 }
[4bf08aa5]105 odict_insert(&data->odict, &alias_dict, NULL);
106 }
[aa3ca1e]107
108 return EOK;
[60c332e]109}
110
[f31ca47]111static bool validate_name(const char *name)
[7f7817a9]112{
113 while (*name != '\0') {
114 if (*name == '/')
115 return false;
116 if (*name == ' ')
117 return false;
118 if (*name == '\"')
119 return false;
120 if (*name == '\'')
121 return false;
122 if (*name == '|')
123 return false;
124
125 name++;
126 }
127
128 return true;
129}
130
[60c332e]131/* Dispays help for alias in various levels */
132void help_cmd_alias(unsigned int level)
133{
[3301452]134 if (level == HELP_SHORT) {
135 printf("`%s' sets an alias, displays an alias or lists all aliases\n", cmdname);
136 } else {
137 help_cmd_alias(HELP_SHORT);
138 printf("Usage: `%s' [newalias[='existingCMD --flags] ...]'\n\n"
139 "If no parameters are given it will display all existing aliases.\n"
140 "If a parameter without an assignment is given, the value of the given alias will be returned.\n"
141 "If a parameter with an assignment is given, the alias will be created or updated for the given value. "
142 "It is possible to create an alias to a different alias. A circularity will prevent an alias to be resolved.\n",
143 cmdname);
144 }
[60c332e]145}
146
147/* Main entry point for alias, accepts an array of arguments */
148int cmd_alias(char **argv)
149{
[ab936440]150
[4bf08aa5]151 if (argv[1] == NULL) {
[60c332e]152 list_aliases();
153 return CMD_SUCCESS;
154 }
[229d114e]155
[60c332e]156 size_t i;
[4bf08aa5]157 for (i = 1; argv[i] != NULL; i++) {
[aa3ca1e]158 char *name = argv[i];
[a02aa5c]159 char *value;
[08bd04c]160 if ((value = str_chr(name, '=')) != NULL) {
161 name[value - name] = '\0';
[aa3ca1e]162
[f31ca47]163 if (!validate_name(name)) {
[7f7817a9]164 cli_error(CL_EFAIL, "%s: invalid alias name given\n", cmdname);
165 return CMD_FAILURE;
166 }
[ab936440]167
[aa3ca1e]168 if (set_alias(name, value + 1) != EOK) {
169 return CMD_FAILURE;
170 }
[08bd04c]171 } else {
[a02aa5c]172 if (!print_alias(name)) {
[229d114e]173 return CMD_FAILURE;
174 }
175 }
[4bf08aa5]176 }
[ab936440]177
[60c332e]178 return CMD_SUCCESS;
179}
Note: See TracBrowser for help on using the repository browser.