Changeset 5fb32c5 in mainline for uspace/app/bdsh/input.c


Ignore:
Timestamp:
2011-08-20T09:05:14Z (13 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c916dfc
Parents:
921b84f (diff), a0fc4be (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.
Message:

Merge mainline changes.

File:
1 edited

Legend:

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

    r921b84f r5fb32c5  
    22 * Copyright (c) 2008 Tim Post
    33 * Copyright (c) 2011 Jiri Svoboda
     4 * Copyright (c) 2011 Martin Sucha
    45 * All rights reserved.
    56 *
     
    6768{
    6869        char *cmd[WORD_MAX];
     70        token_t tokens_space[WORD_MAX];
     71        token_t *tokens = tokens_space;
    6972        int rc = 0;
    7073        tokenizer_t tok;
    71         int i, pipe_count, processed_pipes;
    72         int pipe_pos[2];
    73         char **actual_cmd;
     74        unsigned int i, pipe_count, processed_pipes;
     75        unsigned int pipe_pos[2];
    7476        char *redir_from = NULL;
    7577        char *redir_to = NULL;
     
    7880                return CL_EFAIL;
    7981
    80         rc = tok_init(&tok, usr->line, cmd, WORD_MAX);
     82        rc = tok_init(&tok, usr->line, tokens, WORD_MAX);
    8183        if (rc != EOK) {
    8284                goto finit;
    8385        }
    8486       
    85         rc = tok_tokenize(&tok);
     87        size_t tokens_length;
     88        rc = tok_tokenize(&tok, &tokens_length);
    8689        if (rc != EOK) {
    8790                goto finit;
     91        }
     92       
     93        if (tokens_length > 0 && tokens[0].type == TOKTYPE_SPACE) {
     94                tokens++;
     95                tokens_length--;
     96        }
     97       
     98        if (tokens_length > 0 && tokens[tokens_length-1].type == TOKTYPE_SPACE) {
     99                tokens_length--;
    88100        }
    89101       
     
    93105         * First find the pipes and check that there are no more
    94106         */
    95         int cmd_length = 0;
    96         for (i = 0, pipe_count = 0; cmd[i] != NULL; i++, cmd_length++) {
    97                 if (cmd[i][0] == '|') {
     107        for (i = 0, pipe_count = 0; i < tokens_length; i++) {
     108                if (tokens[i].type == TOKTYPE_PIPE) {
    98109                        if (pipe_count >= 2) {
    99110                                print_pipe_usage();
     
    106117        }
    107118       
    108         actual_cmd = cmd;
     119        unsigned int cmd_token_start = 0;
     120        unsigned int cmd_token_end = tokens_length;
     121       
    109122        processed_pipes = 0;
    110123       
    111124        /* Check if the first part (from <file> |) is present */
    112         if (pipe_count > 0 && pipe_pos[0] == 2 && str_cmp(cmd[0], "from") == 0) {
     125        if (pipe_count > 0 && (pipe_pos[0] == 3 || pipe_pos[0] == 4) && str_cmp(tokens[0].text, "from") == 0) {
    113126                /* Ignore the first three tokens (from, file, pipe) and set from */
    114                 redir_from = cmd[1];
    115                 actual_cmd = cmd + 3;
     127                redir_from = tokens[2].text;
     128                cmd_token_start = pipe_pos[0]+1;
    116129                processed_pipes++;
    117130        }
     
    119132        /* Check if the second part (| to <file>) is present */
    120133        if ((pipe_count - processed_pipes) > 0 &&
    121             pipe_pos[processed_pipes] == cmd_length - 3 &&
    122             str_cmp(cmd[cmd_length-2], "to") == 0) {
     134            (pipe_pos[processed_pipes] == tokens_length - 4 ||
     135            (pipe_pos[processed_pipes] == tokens_length - 5 &&
     136            tokens[tokens_length-4].type == TOKTYPE_SPACE )) &&
     137            str_cmp(tokens[tokens_length-3].text, "to") == 0) {
    123138                /* Ignore the last three tokens (pipe, to, file) and set to */
    124                 redir_to = cmd[cmd_length-1];
    125                 cmd[cmd_length-3] = NULL;
    126                 cmd_length -= 3;
     139                redir_to = tokens[tokens_length-1].text;
     140                cmd_token_end = pipe_pos[processed_pipes];
    127141                processed_pipes++;
    128142        }
     
    134148        }
    135149       
    136         if (actual_cmd[0] == NULL) {
     150        /* Convert tokens of the command to string array */
     151        unsigned int cmd_pos = 0;
     152        for (i = cmd_token_start; i < cmd_token_end; i++) {
     153                if (tokens[i].type != TOKTYPE_SPACE) {
     154                        cmd[cmd_pos++] = tokens[i].text;
     155                }
     156        }
     157        cmd[cmd_pos++] = NULL;
     158       
     159        if (cmd[0] == NULL) {
    137160                print_pipe_usage();
    138161                rc = ENOTSUP;
     
    170193        }
    171194
    172         if (str_cmp(actual_cmd[0], "batch") == 0 && actual_cmd[1] != NULL) {
    173                 FILE *batch = fopen(actual_cmd[1], "r");
     195        if (str_cmp(cmd[0], "batch") == 0 && cmd[1] != NULL) {
     196                FILE *batch = fopen(cmd[1], "r");
    174197                if (batch == NULL) {
    175                         printf("Cannot open file %s\n", actual_cmd[1]);
     198                        printf("Cannot open file %s\n", cmd[1]);
    176199                        rc = errno;
    177200                } else {
     
    211234                }
    212235        } else {
    213                 rc = run_command(actual_cmd, usr, &new_iostate);
    214         }       
     236                rc = run_command(cmd, usr, &new_iostate);
     237        }
    215238       
    216239finit_with_files:
Note: See TracChangeset for help on using the changeset viewer.