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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a8c09f91 was a8c09f91, checked in by Matthieu Riolo <matthieu.riolo@…>, 7 years ago

use cli_error instead of printf

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2018 Matthieu Riolo
3 * All rights reserved.
4 *
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 */
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";
42static const char *alias_format = "%s='%s'\n";
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
54static bool print_alias(const char *name)
55{
56 odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)name, NULL);
57 if (alias_link != NULL) {
58 alias_t *data = odict_get_instance(alias_link, alias_t, odict);
59 printf(alias_format, data->name, data->value);
60 return true;
61 }
62
63 cli_error(CL_ENOENT, "%s: No alias with the name '%s' exists\n", cmdname, name);
64 return false;
65}
66
67static void set_alias(const char *name, const char *value)
68{
69 odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)name, NULL);
70
71 if (alias_link != NULL) {
72 //update existing value
73 alias_t *data = odict_get_instance(alias_link, alias_t, odict);
74 free(data->value);
75 data->value = str_dup(value);
76 } else {
77 //add new value
78 alias_t *data = (alias_t *)calloc(1, sizeof(alias_t));
79 data->name = str_dup(name);
80 data->value = str_dup(value);
81
82 odict_insert(&data->odict, &alias_dict, NULL);
83 }
84}
85
86/* Dispays help for alias in various levels */
87void help_cmd_alias(unsigned int level)
88{
89 printf("`%s' sets an alias, displays an alias or lists all aliases\n", cmdname);
90 return;
91}
92
93/* Main entry point for alias, accepts an array of arguments */
94int cmd_alias(char **argv)
95{
96
97 if (argv[1] == NULL) {
98 list_aliases();
99 return CMD_SUCCESS;
100 }
101
102 size_t i;
103 for (i = 1; argv[i] != NULL; i++) {
104 char *name = str_dup(argv[i]);
105 char *value;
106 if ((value = str_chr(name, '=')) != NULL) {
107 name[value - name] = '\0';
108 set_alias(name, value + 1);
109 } else {
110 if (!print_alias(name)) {
111 free(name);
112 return CMD_FAILURE;
113 }
114 }
115
116 free(name);
117 }
118
119 return CMD_SUCCESS;
120}
Note: See TracBrowser for help on using the repository browser.