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

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

some formatting issue; making alias more robust for the unexpected; correcting help message

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/* Automatically generated by mknewcmd on Don Nov 29 19:01:45 CET 2018
2 * This is machine generated output. The author of mknewcmd claims no
3 * copyright over the contents of this file. Where legally permitted, the
4 * contents herein are donated to the public domain.
5 *
6 * You should apply any license and copyright that you wish to this file,
7 * replacing this header in its entirety. */
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <str.h>
12#include "config.h"
13#include "util.h"
14#include "errors.h"
15#include "entry.h"
16#include "alias.h"
17#include "cmds.h"
18
19#include <adt/odict.h>
20
21
22static const char *cmdname = "alias";
23static const char* alias_format = "%s='%s'\n";
24
25
26
27static void list_aliases()
28{
29 odlink_t *alias_link = odict_first(&alias_dict);
30 while (alias_link != NULL) {
31 alias_t *data = odict_get_instance(alias_link, alias_t, odict);
32
33 printf(alias_format, data->name, data->value);
34
35 alias_link = odict_next(alias_link, &alias_dict);
36 }
37}
38
39
40static bool print_alias(const char* name)
41{
42 odlink_t *alias_link = odict_find_eq(&alias_dict, (void*)name, NULL);
43 if (alias_link != NULL) {
44 alias_t* data = odict_get_instance(alias_link, alias_t, odict);
45 printf(alias_format, data->name, data->value);
46 return true;
47 }
48
49
50
51 printf("%s: No alias with the name '%s' exists\n", cmdname, name);
52 return false;
53}
54
55
56static void set_alias(const char* name, const char* value)
57{
58 odlink_t *alias_link = odict_find_eq(&alias_dict, (void*)name, NULL);
59
60 if (alias_link != NULL) {
61 //update existing value
62 alias_t* data = odict_get_instance(alias_link, alias_t, odict);
63 free(data->value);
64 data->value = str_dup(value);
65 } else {
66 //add new value
67 alias_t* data = (alias_t*)calloc(1, sizeof(alias_t));
68 data->name = str_dup(name);
69 data->value = str_dup(value);
70
71 odict_insert(&data->odict, &alias_dict, NULL);
72 }
73}
74
75
76
77
78
79/* Dispays help for alias in various levels */
80void help_cmd_alias(unsigned int level)
81{
82 printf("`%s' sets an alias, displays an alias or lists all aliases\n", cmdname);
83 return;
84}
85
86/* Main entry point for alias, accepts an array of arguments */
87int cmd_alias(char **argv)
88{
89
90 if (argv[1] == NULL) {
91 list_aliases();
92 return CMD_SUCCESS;
93 }
94
95
96 size_t i;
97 for (i = 1; argv[i] != NULL; i++) {
98 char* name = str_dup(argv[i]);
99 char* value;
100 if ((value = str_chr(name, '=')) != NULL) {
101 name[value - name] = '\0';
102 set_alias(name, value + 1);
103 } else {
104 if(!print_alias(name)) {
105 free(name);
106 return CMD_FAILURE;
107 }
108 }
109
110 free(name);
111 }
112
113 return CMD_SUCCESS;
114}
115
Note: See TracBrowser for help on using the repository browser.