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


Ignore:
Timestamp:
2011-08-19T14:44:49Z (13 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5992e0e
Parents:
89660f2
Message:

Extend bdsh tokenizer to include more information

File:
1 edited

Legend:

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

    r89660f2 r0662451  
    6767{
    6868        char *cmd[WORD_MAX];
     69        token_t tokens_space[WORD_MAX];
     70        token_t *tokens = tokens_space;
    6971        int rc = 0;
    7072        tokenizer_t tok;
    71         int i, pipe_count, processed_pipes;
    72         int pipe_pos[2];
    73         char **actual_cmd;
     73        unsigned int i, pipe_count, processed_pipes;
     74        unsigned int pipe_pos[2];
    7475        char *redir_from = NULL;
    7576        char *redir_to = NULL;
     
    7879                return CL_EFAIL;
    7980
    80         rc = tok_init(&tok, usr->line, cmd, WORD_MAX);
     81        rc = tok_init(&tok, usr->line, tokens, WORD_MAX);
    8182        if (rc != EOK) {
    8283                goto finit;
    8384        }
    8485       
    85         rc = tok_tokenize(&tok);
     86        size_t tokens_length;
     87        rc = tok_tokenize(&tok, &tokens_length);
    8688        if (rc != EOK) {
    8789                goto finit;
     90        }
     91       
     92        if (tokens_length > 0 && tokens[0].type == TOKTYPE_SPACE) {
     93                tokens++;
     94                tokens_length--;
     95        }
     96       
     97        if (tokens_length > 0 && tokens[tokens_length-1].type == TOKTYPE_SPACE) {
     98                tokens_length--;
    8899        }
    89100       
     
    93104         * First find the pipes and check that there are no more
    94105         */
    95         int cmd_length = 0;
    96         for (i = 0, pipe_count = 0; cmd[i] != NULL; i++, cmd_length++) {
    97                 if (cmd[i][0] == '|') {
     106        for (i = 0, pipe_count = 0; i < tokens_length; i++) {
     107                if (tokens[i].type == TOKTYPE_PIPE) {
    98108                        if (pipe_count >= 2) {
    99109                                print_pipe_usage();
     
    106116        }
    107117       
    108         actual_cmd = cmd;
     118        unsigned int cmd_token_start = 0;
     119        unsigned int cmd_token_end = tokens_length;
     120       
    109121        processed_pipes = 0;
    110122       
    111123        /* Check if the first part (from <file> |) is present */
    112         if (pipe_count > 0 && pipe_pos[0] == 2 && str_cmp(cmd[0], "from") == 0) {
     124        if (pipe_count > 0 && (pipe_pos[0] == 3 || pipe_pos[0] == 4) && str_cmp(tokens[0].text, "from") == 0) {
    113125                /* Ignore the first three tokens (from, file, pipe) and set from */
    114                 redir_from = cmd[1];
    115                 actual_cmd = cmd + 3;
     126                redir_from = tokens[2].text;
     127                cmd_token_start = pipe_pos[0]+1;
     128                printf("set cmd_token_start = %d\n", cmd_token_start);
    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];
     141                printf("set cmd_token_end = %d\n", cmd_token_end);
    127142                processed_pipes++;
    128143        }
     
    134149        }
    135150       
    136         if (actual_cmd[0] == NULL) {
     151        /* Convert tokens of the command to string array */
     152        unsigned int cmd_pos = 0;
     153        for (i = cmd_token_start; i < cmd_token_end; i++) {
     154                if (tokens[i].type != TOKTYPE_SPACE) {
     155                        cmd[cmd_pos++] = tokens[i].text;
     156                        printf("%s\n", tokens[i].text);
     157                }
     158        }
     159        cmd[cmd_pos++] = NULL;
     160       
     161        if (cmd[0] == NULL) {
    137162                print_pipe_usage();
    138163                rc = ENOTSUP;
     
    170195        }
    171196       
    172         rc = run_command(actual_cmd, usr, &new_iostate);
     197        rc = run_command(cmd, usr, &new_iostate);
    173198       
    174199finit_with_files:
Note: See TracChangeset for help on using the changeset viewer.