Changeset 0662451 in mainline


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

Location:
uspace/app/bdsh
Files:
3 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:
  • uspace/app/bdsh/tok.c

    r89660f2 r0662451  
    4242static bool tok_pending_chars(tokenizer_t *);
    4343static int tok_finish_string(tokenizer_t *);
     44static void tok_start_token(tokenizer_t *, token_type_t);
    4445
    4546/** Initialize the token parser
     
    6162        tok->outtok = out_tokens;
    6263        tok->outtok_offset = 0;
    63         /* Leave one slot for a null terminator */
    64         assert(max_tokens > 0);
    65         tok->outtok_size = max_tokens - 1;
     64        tok->outtok_size = max_tokens;
    6665       
    6766        /* Prepare a buffer where all the token strings will be stored */
     
    9089
    9190/** Tokenize the input string into the tokens */
    92 int tok_tokenize(tokenizer_t *tok)
     91int tok_tokenize(tokenizer_t *tok, size_t *tokens_length)
    9392{
    9493        int rc;
     
    9897        while ((cur_char = tok_get_char(tok)) != 0) {
    9998                if (cur_char == ' ') {
    100                         /* Spaces delimit tokens, but are not processed in any way
    101                          * Push the token if there is any.
     99                        /* Push the token if there is any.
    102100                         * There may not be any pending char for a token in case
    103101                         * there are several spaces in the input.
     
    109107                                }
    110108                        }
     109                        tok_start_token(tok, TOKTYPE_SPACE);
     110                        /* Eat all spaces */
     111                        while (tok_look_char(tok) == ' ') {
     112                                tok_push_char(tok, tok_get_char(tok));
     113                        }
     114                        tok_push_token(tok);
     115                       
    111116                }
    112117                else if (cur_char == '|') {
     
    121126                        }
    122127                       
     128                        tok_start_token(tok, TOKTYPE_PIPE);
     129                       
    123130                        rc = tok_push_char(tok, '|');
    124131                        if (rc != EOK) {
     
    135142                         * A literal quote is written as ''
    136143                         */
     144                        tok_start_token(tok, TOKTYPE_TEXT);
    137145                        rc = tok_finish_string(tok);
    138146                        if (rc != EOK) {
     
    141149                }
    142150                else {
     151                        if (!tok_pending_chars(tok)) {
     152                                tok_start_token(tok, TOKTYPE_TEXT);
     153                        }
    143154                        /* If we are handling any other character, just append it to
    144155                         * the current token.
     
    159170        }
    160171       
    161         /* We always have a space for the terminator, as we
    162          * reserved it in tok_init */
    163         tok->outtok[tok->outtok_offset] = 0;
     172        *tokens_length = tok->outtok_offset;
    164173       
    165174        return EOK;
     
    211220wchar_t tok_look_char(tokenizer_t *tok)
    212221{
    213         off_t old_offset = tok->in_offset;
    214         off_t old_char_offset = tok->in_char_offset;
     222        unsigned int old_offset = tok->in_offset;
     223        unsigned int old_char_offset = tok->in_char_offset;
    215224        wchar_t ret = tok_get_char(tok);
    216225        tok->in_offset = old_offset;
     
    225234}
    226235
     236void tok_start_token(tokenizer_t *tok, token_type_t type)
     237{
     238        tok->current_type = type;
     239}
     240
    227241/** Push the current token to the output array */
    228242int tok_push_token(tokenizer_t *tok)
     
    237251       
    238252        tok->outbuf[tok->outbuf_offset++] = 0;
    239         token_t *tokinfo = &tok->outtok[tok->outtok_offset++]
    240         tokinfo.text = tok->outbuf + tok->outbuf_last_start;
    241         tokinfo.byte_start = tok->last_in_offset;
    242         tokinfo.byte_length = tok->in_offset - tok->last_in_offset - 1;
    243         tokinfo.char_start = tok->last_in_char_offset;
    244         tokinfo.char_length = tok->in_char_offset - tok->last_in_char_offset
     253        token_t *tokinfo = &tok->outtok[tok->outtok_offset++];
     254        tokinfo->type = tok->current_type;
     255        tokinfo->text = tok->outbuf + tok->outbuf_last_start;
     256        tokinfo->byte_start = tok->last_in_offset;
     257        tokinfo->byte_length = tok->in_offset - tok->last_in_offset - 1;
     258        tokinfo->char_start = tok->last_in_char_offset;
     259        tokinfo->char_length = tok->in_char_offset - tok->last_in_char_offset
    245260            - 1;
    246         tok->outtok[tok->outtok_offset]
    247261        tok->outbuf_last_start = tok->outbuf_offset;
    248262       
  • uspace/app/bdsh/tok.h

    r89660f2 r0662451  
    3838typedef struct {
    3939        char *text;
    40         off_t byte_start;
    41         off_t char_start;
     40        unsigned int byte_start;
     41        unsigned int char_start;
    4242        size_t byte_length;
    4343        size_t char_length;
     
    4747typedef struct {
    4848        char *in;
    49         off_t in_offset;
    50         off_t last_in_offset;
    51         off_t in_char_offset;
    52         off_t last_in_char_offset;
     49        unsigned int in_offset;
     50        unsigned int last_in_offset;
     51        unsigned int in_char_offset;
     52        unsigned int last_in_char_offset;
    5353       
    5454        char *outbuf;
     
    5858       
    5959        token_t *outtok;
     60        token_type_t current_type;
    6061        size_t outtok_offset;
    6162        size_t outtok_size;
     
    6465extern int tok_init(tokenizer_t *, char *, token_t *, size_t);
    6566extern void tok_fini(tokenizer_t *);
    66 extern int tok_tokenize(tokenizer_t *);
     67extern int tok_tokenize(tokenizer_t *, size_t *);
    6768
    6869#endif
Note: See TracChangeset for help on using the changeset viewer.