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

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

correcting argument handling in cmd alias

  • Property mode set to 100644
File size: 2.6 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
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 }
101
102
103 size_t i;
104 for (i = 1; argv[i] != NULL; i++) {
105 char* pos;
106 if ((pos = str_chr(argv[i], '=')) != NULL) {
107 argv[i][pos - argv[i]] = '\0';
108 set_alias(argv[i], pos + 1);
109 }else {
110 if(!print_alias(argv[i])) {
111 return CMD_FAILURE;
112 }
113 }
114 }
115
116 return CMD_SUCCESS;
117}
118
Note: See TracBrowser for help on using the repository browser.