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

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

correcting setting and updating aliases

  • Property mode set to 100644
File size: 2.8 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 int 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 CMD_SUCCESS;
47 }
48
49
50
51 printf("%s: No alias with the name '%s' exists\n", cmdname, name);
52 return CMD_FAILURE;
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
66 printf("%s: update value ", cmdname);
67 }else {
68 //add new value
69 alias_t* data = (alias_t*)calloc(1, sizeof(alias_t));
70 data->name = str_dup(name);
71 data->value = str_dup(value);
72
73 odict_insert(&data->odict, &alias_dict, NULL);
74
75
76 printf("%s: insert value ", cmdname);
77 }
78
79 printf(alias_format, name, value);
80}
81
82
83
84
85
86/* Dispays help for alias in various levels */
87void help_cmd_alias(unsigned int level)
88{
89 printf("Set a new alias with \"alias hex='cat --hex'\". Display an alias with \"alias hex\". List all alias by passing no argument.\n");
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 }else if (argv[2] == NULL && str_chr(argv[1], '=') == NULL) {
101 return print_alias(argv[1]);
102 }
103
104 //concat all it together
105 char* str = (char*)malloc(INPUT_MAX * sizeof(char));
106 str[0] = '\0';
107
108 size_t i;
109 for (i = 1; argv[i] != NULL; i++) {
110 str_append(str, INPUT_MAX - 1, argv[i]);
111 }
112
113 //split input
114 char* pos = str_chr(str, '=');
115 if(pos == NULL) {
116 printf("%s: bad formatted input\n", cmdname);
117 return CMD_FAILURE;
118 }
119
120
121 str[pos - str] = '\0';
122 set_alias(str, pos + 1);
123
124 free(str);
125 return CMD_SUCCESS;
126}
127
Note: See TracBrowser for help on using the repository browser.