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

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

removing unnecessary newlines

  • 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
41
42static const char *cmdname = "alias";
43static const char* alias_format = "%s='%s'\n";
44
45
46
47static void list_aliases()
48{
49 odlink_t *alias_link = odict_first(&alias_dict);
50 while (alias_link != NULL) {
51 alias_t *data = odict_get_instance(alias_link, alias_t, odict);
52 printf(alias_format, data->name, data->value);
53 alias_link = odict_next(alias_link, &alias_dict);
54 }
55}
56
57
58static bool print_alias(const char* name)
59{
60 odlink_t *alias_link = odict_find_eq(&alias_dict, (void*)name, NULL);
61 if (alias_link != NULL) {
62 alias_t* data = odict_get_instance(alias_link, alias_t, odict);
63 printf(alias_format, data->name, data->value);
64 return true;
65 }
66
67
68
69 printf("%s: No alias with the name '%s' exists\n", cmdname, name);
70 return false;
71}
72
73
74static void set_alias(const char* name, const char* value)
75{
76 odlink_t *alias_link = odict_find_eq(&alias_dict, (void*)name, NULL);
77
78 if (alias_link != NULL) {
79 //update existing value
80 alias_t* data = odict_get_instance(alias_link, alias_t, odict);
81 free(data->value);
82 data->value = str_dup(value);
83 } else {
84 //add new value
85 alias_t* data = (alias_t*)calloc(1, sizeof(alias_t));
86 data->name = str_dup(name);
87 data->value = str_dup(value);
88
89 odict_insert(&data->odict, &alias_dict, NULL);
90 }
91}
92
93
94
95
96
97/* Dispays help for alias in various levels */
98void help_cmd_alias(unsigned int level)
99{
100 printf("`%s' sets an alias, displays an alias or lists all aliases\n", cmdname);
101 return;
102}
103
104/* Main entry point for alias, accepts an array of arguments */
105int cmd_alias(char **argv)
106{
107
108 if (argv[1] == NULL) {
109 list_aliases();
110 return CMD_SUCCESS;
111 }
112
113
114 size_t i;
115 for (i = 1; argv[i] != NULL; i++) {
116 char* name = str_dup(argv[i]);
117 char* value;
118 if ((value = str_chr(name, '=')) != NULL) {
119 name[value - name] = '\0';
120 set_alias(name, value + 1);
121 } else {
122 if(!print_alias(name)) {
123 free(name);
124 return CMD_FAILURE;
125 }
126 }
127
128 free(name);
129 }
130
131 return CMD_SUCCESS;
132}
133
Note: See TracBrowser for help on using the repository browser.