Changeset 4bf08aa5 in mainline


Ignore:
Timestamp:
2018-11-30T03:22:03Z (5 years ago)
Author:
Matthieu Riolo <matthieu.riolo@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
55e35a22
Parents:
60c332e
Message:

correcting setting and updating aliases

Location:
uspace/app/bdsh
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/alias/alias.c

    r60c332e r4bf08aa5  
    2121
    2222static const char *cmdname = "alias";
    23 static const char* alias_format = "%s/%s";
     23static const char* alias_format = "%s='%s'\n";
    2424
    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 */
    3925
    4026
     
    5137}
    5238
     39
    5340static int print_alias(const char* name)
    5441{
     
    6249
    6350
    64         printf("No alias with the name '%s' exists", name);
     51        printf("%s: No alias with the name '%s' exists\n", cmdname, name);
    6552        return CMD_FAILURE;
    6653}
    6754
     55
    6856static void set_alias(const char* name, const char* value)
    6957{
    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);
    7180}
    7281
     
    7887void help_cmd_alias(unsigned int level)
    7988{
    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");
    8290        return;
    8391}
     
    8795{
    8896       
    89         if (argv[0] == NULL) {
     97        if (argv[1] == NULL) {
    9098                list_aliases();
    9199                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]);
    94102        }
    95 
    96 
     103       
     104        //concat all it together
    97105        char* str = (char*)malloc(INPUT_MAX * sizeof(char));
    98106        str[0] = '\0';
    99107
    100108        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]);
    103111        }
    104112
    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);
    108125        return CMD_SUCCESS;
    109126}
  • uspace/app/bdsh/input.c

    r60c332e r4bf08aa5  
    171171        }
    172172
     173
     174
     175        /* test if the passed cmd is an alias
     176        odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)&v, NULL);
     177        e = odict_get_instance(alias_link, test_entry_t, alias_dict);
     178        if() {
     179                char* oldLine = usr->line;
     180
     181
     182                //reprocess input after string replace
     183                rc = process_input(usr);
     184                usr->line = oldLine;
     185                goto finit;
     186        }
     187       
     188        */
     189
     190
    173191        iostate_t new_iostate = {
    174192                .stdin = stdin,
     
    219237                usr->line = (char *) NULL;
    220238        }
     239
    221240        tok_fini(&tok);
    222241        free(tokens_buf);
  • uspace/app/bdsh/scli.c

    r60c332e r4bf08aa5  
    6161const char *progname = PACKAGE_NAME;
    6262
    63 static int alias_cmp(void* a, void* b) {
     63static int alias_cmp(void* a, void* b)
     64{
    6465        return str_cmp((char*)a, (char*)b);
    6566}
    6667
     68static void* alias_key(odlink_t *odlink)
     69{
     70        return (void*)odict_get_instance(odlink, alias_t, odict)->name;
     71}
    6772
    6873/* These are not exposed, even to builtins */
  • uspace/app/bdsh/scli.h

    r60c332e r4bf08aa5  
    6060extern odict_t alias_dict;
    6161
    62 extern void* alias_key(odlink_t *odlink);
    6362
    6463typedef struct {
Note: See TracChangeset for help on using the changeset viewer.