Changeset 82570ff in mainline


Ignore:
Timestamp:
2021-08-09T21:44:23Z (3 years ago)
Author:
Manuele Conti <manuele.conti@…>
Children:
3d36920e
Parents:
5f9a52e
Message:

Restore alias management

File:
1 edited

Legend:

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

    r5f9a52e r82570ff  
    7070        alias_t *alias;
    7171} alias_hup_t;
    72 #if 0
     72
    7373static bool find_alias_hup(alias_t *alias, list_t *alias_hups)
    7474{
     
    8181        return false;
    8282}
    83 #endif
     83
     84static errno_t find_alias(char **cmd, list_t *alias_hups, alias_t **data)
     85{
     86        errno_t rc = EOK;
     87
     88        /* test if the passed cmd is an alias */
     89        odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)cmd[0], NULL);
     90        if (alias_link != NULL) {
     91                *data = odict_get_instance(alias_link, alias_t, odict);
     92                /* check if the alias already has been resolved once */
     93                if (!find_alias_hup(*data, alias_hups)) {
     94                        alias_hup_t *hup = (alias_hup_t *)calloc(1, sizeof(alias_hup_t));
     95                        if (hup == NULL) {
     96                                cli_error(CL_EFAIL, "%s: cannot allocate alias structure\n", PACKAGE_NAME);
     97                                rc = ENOMEM;
     98                                goto exit;
     99                        }
     100                        hup->alias = *data;
     101                        list_append(&hup->alias_hup_link, alias_hups);
     102                }
     103        }
     104
     105exit:
     106        return rc;
     107}
     108
     109static errno_t replace_alias(token_t * tokens, unsigned int tokens_start, unsigned int tokens_len, alias_t *data, char **cmd, char **line)
     110{
     111        errno_t rc = EOK;
     112        const size_t input_length = str_size(*line) - str_size(cmd[0]) + str_size(data->value) + 1;
     113        *line = (char *)malloc(input_length);
     114        if (*line == NULL) {
     115                cli_error(CL_EFAIL, "%s: cannot allocate input structure\n", PACKAGE_NAME);
     116                rc = ENOMEM;
     117                goto exit;
     118        }
     119
     120        *line[0] = '\0';
     121
     122        unsigned int cmd_replace_index = tokens_start;
     123        for (unsigned int i = 0; i < tokens_len; i++) {
     124                if (i == cmd_replace_index) {
     125                        /* if there is a pipe symbol than cmd_token_start will point at the SPACE after the pipe symbol */
     126                        if (tokens[i].type == TOKTYPE_SPACE) {
     127                                tokens_start++;
     128                                str_append(*line, input_length, tokens[i].text);
     129                                continue;
     130                        }
     131
     132                        str_append(*line, input_length, data->value);
     133                } else {
     134                        str_append(*line, input_length, tokens[i].text);
     135                }
     136        }
     137
     138exit:
     139        return rc;
     140}
     141
    84142/*
    85143 * Tokenizes input from console, sees if the first word is a built-in, if so
     
    209267                }
    210268
     269                alias_t *data = NULL;
     270                rc = find_alias(cmd, alias_hups, &data);
     271                if (rc != EOK) {
     272                        goto finit;
     273                }
     274
     275                if (data != NULL) {
     276                        rc = replace_alias(tokens, cmd_token_start, tokens_length, data, cmd, &usr->line);
     277                        if (rc == EOK) {
     278                                /* reprocess input after string replace */
     279                                rc = process_input_nohup(usr, alias_hups, count_executed_hups + 1);
     280                        }
     281                        goto finit;
     282                }
     283
     284
    211285                if (p < pipe_count - 1) {
    212286                        new_iostate.stdout = to;
     
    241315                printf("Command not found.\n");
    242316                rc = ENOTSUP;
     317                goto finit;
     318        }
     319
     320        alias_t *data = NULL;
     321        rc = find_alias(cmd, alias_hups, &data);
     322        if (rc != EOK) {
     323                goto finit;
     324        }
     325
     326        if (data != NULL) {
     327                rc = replace_alias(tokens, cmd_token_start, tokens_length, data, cmd, &usr->line);
     328                if (rc == EOK) {
     329                        /* reprocess input after string replace */
     330                        rc = process_input_nohup(usr, alias_hups, count_executed_hups + 1);
     331                }
    243332                goto finit;
    244333        }
Note: See TracChangeset for help on using the changeset viewer.