Changeset 0662451 in mainline
- Timestamp:
- 2011-08-19T14:44:49Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5992e0e
- Parents:
- 89660f2
- Location:
- uspace/app/bdsh
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/input.c
r89660f2 r0662451 67 67 { 68 68 char *cmd[WORD_MAX]; 69 token_t tokens_space[WORD_MAX]; 70 token_t *tokens = tokens_space; 69 71 int rc = 0; 70 72 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]; 74 75 char *redir_from = NULL; 75 76 char *redir_to = NULL; … … 78 79 return CL_EFAIL; 79 80 80 rc = tok_init(&tok, usr->line, cmd, WORD_MAX);81 rc = tok_init(&tok, usr->line, tokens, WORD_MAX); 81 82 if (rc != EOK) { 82 83 goto finit; 83 84 } 84 85 85 rc = tok_tokenize(&tok); 86 size_t tokens_length; 87 rc = tok_tokenize(&tok, &tokens_length); 86 88 if (rc != EOK) { 87 89 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--; 88 99 } 89 100 … … 93 104 * First find the pipes and check that there are no more 94 105 */ 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) { 98 108 if (pipe_count >= 2) { 99 109 print_pipe_usage(); … … 106 116 } 107 117 108 actual_cmd = cmd; 118 unsigned int cmd_token_start = 0; 119 unsigned int cmd_token_end = tokens_length; 120 109 121 processed_pipes = 0; 110 122 111 123 /* 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) { 113 125 /* 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); 116 129 processed_pipes++; 117 130 } … … 119 132 /* Check if the second part (| to <file>) is present */ 120 133 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) { 123 138 /* 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); 127 142 processed_pipes++; 128 143 } … … 134 149 } 135 150 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) { 137 162 print_pipe_usage(); 138 163 rc = ENOTSUP; … … 170 195 } 171 196 172 rc = run_command( actual_cmd, usr, &new_iostate);197 rc = run_command(cmd, usr, &new_iostate); 173 198 174 199 finit_with_files: -
uspace/app/bdsh/tok.c
r89660f2 r0662451 42 42 static bool tok_pending_chars(tokenizer_t *); 43 43 static int tok_finish_string(tokenizer_t *); 44 static void tok_start_token(tokenizer_t *, token_type_t); 44 45 45 46 /** Initialize the token parser … … 61 62 tok->outtok = out_tokens; 62 63 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; 66 65 67 66 /* Prepare a buffer where all the token strings will be stored */ … … 90 89 91 90 /** Tokenize the input string into the tokens */ 92 int tok_tokenize(tokenizer_t *tok )91 int tok_tokenize(tokenizer_t *tok, size_t *tokens_length) 93 92 { 94 93 int rc; … … 98 97 while ((cur_char = tok_get_char(tok)) != 0) { 99 98 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. 102 100 * There may not be any pending char for a token in case 103 101 * there are several spaces in the input. … … 109 107 } 110 108 } 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 111 116 } 112 117 else if (cur_char == '|') { … … 121 126 } 122 127 128 tok_start_token(tok, TOKTYPE_PIPE); 129 123 130 rc = tok_push_char(tok, '|'); 124 131 if (rc != EOK) { … … 135 142 * A literal quote is written as '' 136 143 */ 144 tok_start_token(tok, TOKTYPE_TEXT); 137 145 rc = tok_finish_string(tok); 138 146 if (rc != EOK) { … … 141 149 } 142 150 else { 151 if (!tok_pending_chars(tok)) { 152 tok_start_token(tok, TOKTYPE_TEXT); 153 } 143 154 /* If we are handling any other character, just append it to 144 155 * the current token. … … 159 170 } 160 171 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; 164 173 165 174 return EOK; … … 211 220 wchar_t tok_look_char(tokenizer_t *tok) 212 221 { 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; 215 224 wchar_t ret = tok_get_char(tok); 216 225 tok->in_offset = old_offset; … … 225 234 } 226 235 236 void tok_start_token(tokenizer_t *tok, token_type_t type) 237 { 238 tok->current_type = type; 239 } 240 227 241 /** Push the current token to the output array */ 228 242 int tok_push_token(tokenizer_t *tok) … … 237 251 238 252 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 245 260 - 1; 246 tok->outtok[tok->outtok_offset]247 261 tok->outbuf_last_start = tok->outbuf_offset; 248 262 -
uspace/app/bdsh/tok.h
r89660f2 r0662451 38 38 typedef struct { 39 39 char *text; 40 off_t byte_start;41 off_t char_start;40 unsigned int byte_start; 41 unsigned int char_start; 42 42 size_t byte_length; 43 43 size_t char_length; … … 47 47 typedef struct { 48 48 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; 53 53 54 54 char *outbuf; … … 58 58 59 59 token_t *outtok; 60 token_type_t current_type; 60 61 size_t outtok_offset; 61 62 size_t outtok_size; … … 64 65 extern int tok_init(tokenizer_t *, char *, token_t *, size_t); 65 66 extern void tok_fini(tokenizer_t *); 66 extern int tok_tokenize(tokenizer_t * );67 extern int tok_tokenize(tokenizer_t *, size_t *); 67 68 68 69 #endif
Note:
See TracChangeset
for help on using the changeset viewer.