Ignore:
File:
1 edited

Legend:

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

    re62f8e3 r09ab0a9a  
    33 * Copyright (c) 2011 Jiri Svoboda
    44 * Copyright (c) 2011 Martin Sucha
    5  * Copyright (c) 2018 Matthieu Riolo
    65 * All rights reserved.
    76 *
     
    4443#include <stdbool.h>
    4544#include <tinput.h>
    46 #include <adt/odict.h>
    47 #include <adt/list.h>
    4845
    4946#include "config.h"
     
    6562static void print_pipe_usage(void);
    6663
    67 typedef struct {
    68         link_t alias_hup_link;
    69         alias_t *alias;
    70 } alias_hup_t;
    71 
    72 static 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 
    8364/*
    8465 * Tokenizes input from console, sees if the first word is a built-in, if so
     
    8667 * the handler
    8768 */
    88 static 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 
     69errno_t process_input(cliuser_t *usr)
     70{
    9571        token_t *tokens_buf = calloc(WORD_MAX, sizeof(token_t));
    9672        if (tokens_buf == NULL)
     
    195171        }
    196172
    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 
    247173        iostate_t new_iostate = {
    248174                .stdin = stdin,
     
    299225}
    300226
    301 errno_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 
    316227void print_pipe_usage(void)
    317228{
Note: See TracChangeset for help on using the changeset viewer.