Changeset 4bf08aa5 in mainline for uspace/app/bdsh/cmds/modules/alias/alias.c
- Timestamp:
- 2018-11-30T03:22:03Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 55e35a22
- Parents:
- 60c332e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/alias/alias.c
r60c332e r4bf08aa5 21 21 22 22 static const char *cmdname = "alias"; 23 static const char* alias_format = "%s /%s";23 static const char* alias_format = "%s='%s'\n"; 24 24 25 26 /*27 #include <types/adt/odict.h>28 extern void odict_initialize(odict_t *, odgetkey_t, odcmp_t);29 extern void odict_finalize(odict_t *);30 extern void odlink_initialize(odlink_t *);31 extern void odict_insert(odlink_t *, odict_t *, odlink_t *);32 extern void odict_remove(odlink_t *);33 34 extern odlink_t *odict_first(odict_t *);35 extern odlink_t *odict_next(odlink_t *, odict_t *);36 extern odlink_t *odict_find_eq(odict_t *, void *, odlink_t *);37 extern errno_t odict_validate(odict_t *);38 */39 25 40 26 … … 51 37 } 52 38 39 53 40 static int print_alias(const char* name) 54 41 { … … 62 49 63 50 64 printf(" No alias with the name '%s' exists", name);51 printf("%s: No alias with the name '%s' exists\n", cmdname, name); 65 52 return CMD_FAILURE; 66 53 } 67 54 55 68 56 static void set_alias(const char* name, const char* value) 69 57 { 70 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); 71 80 } 72 81 … … 78 87 void help_cmd_alias(unsigned int level) 79 88 { 80 printf("This is the %s help for '%s'.\n", 81 level ? EXT_HELP : SHORT_HELP, cmdname); 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"); 82 90 return; 83 91 } … … 87 95 { 88 96 89 if (argv[ 0] == NULL) {97 if (argv[1] == NULL) { 90 98 list_aliases(); 91 99 return CMD_SUCCESS; 92 }else if (argv[ 1] == NULL && strpos(argv[0], "=") == -1) {93 return print_alias(argv[ 0]);100 }else if (argv[2] == NULL && str_chr(argv[1], '=') == NULL) { 101 return print_alias(argv[1]); 94 102 } 95 96 103 104 //concat all it together 97 105 char* str = (char*)malloc(INPUT_MAX * sizeof(char)); 98 106 str[0] = '\0'; 99 107 100 108 size_t i; 101 for (i = 0; argv[i] != NULL; i++) {102 str = strcat(str, argv[i]);109 for (i = 1; argv[i] != NULL; i++) { 110 str_append(str, INPUT_MAX - 1, argv[i]); 103 111 } 104 112 105 106 107 set_alias(name, value); 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); 108 125 return CMD_SUCCESS; 109 126 }
Note:
See TracChangeset
for help on using the changeset viewer.