Changeset b781cc49 in mainline for uspace/app/bdsh/input.c


Ignore:
Timestamp:
2019-07-05T18:37:31Z (5 years ago)
Author:
GitHub <noreply@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
fa603e99
Parents:
53afa639 (diff), 46288ee (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2019-07-05 18:37:31)
git-committer:
GitHub <noreply@…> (2019-07-05 18:37:31)
Message:

Merge pull request #135 from matthieuriolo/bdsh_alias

Implements alias/unalias commands in bdsh.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/input.c

    r53afa639 rb781cc49  
    33 * Copyright (c) 2011 Jiri Svoboda
    44 * Copyright (c) 2011 Martin Sucha
     5 * Copyright (c) 2018 Matthieu Riolo
    56 * All rights reserved.
    67 *
     
    4344#include <stdbool.h>
    4445#include <tinput.h>
     46#include <adt/odict.h>
     47#include <adt/list.h>
    4548
    4649#include "config.h"
     
    6265static void print_pipe_usage(void);
    6366
     67typedef struct {
     68        link_t alias_hup_link;
     69        alias_t *alias;
     70} alias_hup_t;
     71
     72static bool find_alias_hup(alias_t *alias, list_t *alias_hups)
     73{
     74        list_foreach(*alias_hups, alias_hup_link, alias_hup_t, link) {
     75                if (alias == link->alias) {
     76                        return true;
     77                }
     78        }
     79
     80        return false;
     81}
     82
    6483/*
    6584 * Tokenizes input from console, sees if the first word is a built-in, if so
     
    6786 * the handler
    6887 */
    69 errno_t process_input(cliuser_t *usr)
    70 {
     88static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t count_executed_hups)
     89{
     90        if (count_executed_hups >= HUBS_MAX) {
     91                cli_error(CL_EFAIL, "%s: maximal alias hubs reached\n", PACKAGE_NAME);
     92                return ELIMIT;
     93        }
     94
    7195        token_t *tokens_buf = calloc(WORD_MAX, sizeof(token_t));
    7296        if (tokens_buf == NULL)
     
    171195        }
    172196
     197        /* test if the passed cmd is an alias */
     198        odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)cmd[0], NULL);
     199        if (alias_link != NULL) {
     200                alias_t *data = odict_get_instance(alias_link, alias_t, odict);
     201                /* check if the alias already has been resolved once */
     202                if (!find_alias_hup(data, alias_hups)) {
     203                        alias_hup_t *hup = (alias_hup_t *)calloc(1, sizeof(alias_hup_t));
     204                        if (hup == NULL) {
     205                                cli_error(CL_EFAIL, "%s: cannot allocate alias structure\n", PACKAGE_NAME);
     206                                rc = ENOMEM;
     207                                goto finit;
     208                        }
     209
     210                        hup->alias = data;
     211                        list_append(&hup->alias_hup_link, alias_hups);
     212
     213                        char *oldLine = usr->line;
     214                        const size_t input_length = str_size(usr->line) - str_size(cmd[0]) + str_size(data->value) + 1;
     215                        usr->line = (char *)malloc(input_length);
     216                        if (usr->line == NULL) {
     217                                cli_error(CL_EFAIL, "%s: cannot allocate input structure\n", PACKAGE_NAME);
     218                                rc = ENOMEM;
     219                                goto finit;
     220                        }
     221
     222                        usr->line[0] = '\0';
     223
     224                        unsigned int cmd_replace_index = cmd_token_start;
     225                        for (i = 0; i < tokens_length; i++) {
     226                                if (i == cmd_replace_index) {
     227                                        /* if there is a pipe symbol than cmd_token_start will point at the SPACE after the pipe symbol */
     228                                        if (tokens[i].type == TOKTYPE_SPACE) {
     229                                                cmd_replace_index++;
     230                                                str_append(usr->line, input_length, tokens[i].text);
     231                                                continue;
     232                                        }
     233
     234                                        str_append(usr->line, input_length, data->value);
     235                                } else {
     236                                        str_append(usr->line, input_length, tokens[i].text);
     237                                }
     238                        }
     239
     240                        /* reprocess input after string replace */
     241                        rc = process_input_nohup(usr, alias_hups, count_executed_hups + 1);
     242                        usr->line = oldLine;
     243                        goto finit;
     244                }
     245        }
     246
    173247        iostate_t new_iostate = {
    174248                .stdin = stdin,
     
    225299}
    226300
     301errno_t process_input(cliuser_t *usr)
     302{
     303        list_t alias_hups;
     304        list_initialize(&alias_hups);
     305
     306        errno_t rc = process_input_nohup(usr, &alias_hups, 0);
     307
     308        list_foreach_safe(alias_hups, cur_link, next_link) {
     309                alias_hup_t *cur_item = list_get_instance(cur_link, alias_hup_t, alias_hup_link);
     310                free(cur_item);
     311        }
     312
     313        return rc;
     314}
     315
    227316void print_pipe_usage(void)
    228317{
Note: See TracChangeset for help on using the changeset viewer.