2 * Copyright Droids Corporation (2007)
3 * Olivier MATZ <zer0@droids-corp.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Revision : $Id: parse.c,v 1.1.2.11 2009-04-07 20:00:46 zer0 Exp $
29 #include <aversive/pgmspace.h>
34 #define pgm_read_pgmptr(x) ((void *)(*(x)))
36 #define pgm_read_pgmptr(x) (void *)pgm_read_word(x)
39 //#define CMDLINE_DEBUG
40 //#define debug_printf printf
41 #define debug_printf(args...) do {} while(0)
64 if (!c || iscomment(c) || isblank(c) || isendofline(c))
70 nb_common_chars(const char * s1, const char * s2)
74 while (*s1==*s2 && *s1 && *s2) {
83 * try to match the buffer with an instruction (only the first
84 * nb_match_token tokens if != 0). Return 0 if we match all the
85 * tokens, else the number of matched tokens, else -1.
88 match_inst(parse_pgm_inst_t *inst, const char * buf, uint8_t nb_match_token,
92 parse_pgm_token_hdr_t * token_p;
95 struct token_hdr token_hdr;
97 token_p = (parse_pgm_token_hdr_t *)pgm_read_pgmptr(&inst->tokens[token_num]);
99 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
101 /* check if we match all tokens of inst */
102 while (token_p && (!nb_match_token || i<nb_match_token)) {
103 debug_printf("TK\n");
105 while (isblank(*buf)) {
110 if ( isendofline(*buf) || iscomment(*buf) )
113 n = token_hdr.ops->parse(token_p, buf, (result_buf ? result_buf+token_hdr.offset : NULL));
116 debug_printf("TK parsed (len=%d)\n", n);
121 token_p = (parse_pgm_token_hdr_t *)pgm_read_pgmptr(&inst->tokens[token_num]);
123 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
130 /* in case we want to match a specific num of token */
131 if (nb_match_token) {
132 if (i == nb_match_token) {
138 /* we don't match all the tokens */
143 /* are there are some tokens more */
144 while (isblank(*buf)) {
149 if ( isendofline(*buf) || iscomment(*buf) )
152 /* garbage after inst */
158 parse(parse_pgm_ctx_t ctx[], const char * buf)
161 parse_pgm_inst_t * inst;
163 char result_buf[256]; /* XXX align, size zé in broblém */
164 void (*f)(void *, void *) = NULL;
169 int8_t err = PARSE_NOMATCH;
176 * - look if the buffer contains at least one line
177 * - look if line contains only spaces or comments
178 * - count line length
181 while (! isendofline(*curbuf)) {
182 if ( *curbuf == '\0' ) {
183 debug_printf("Incomplete buf (len=%d)\n", linelen);
186 if ( iscomment(*curbuf) ) {
189 if ( ! isblank(*curbuf) && ! comment) {
196 /* skip all endofline chars */
197 while (isendofline(buf[linelen])) {
202 if ( parse_it == 0 ) {
203 debug_printf("Empty line (len=%d)\n", linelen);
208 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
209 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
213 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
215 debug_printf("INST\n");
218 tok = match_inst(inst, buf, 0, result_buf);
220 if (tok > 0) /* we matched at least one token */
221 err = PARSE_BAD_ARGS;
224 debug_printf("INST fully parsed\n");
226 while (isblank(*curbuf)) {
230 /* if end of buf -> there is no garbage after inst */
231 if (isendofline(*curbuf) || iscomment(*curbuf)) {
233 memcpy_P(&f, &inst->f, sizeof(f));
234 memcpy_P(&data, &inst->data, sizeof(data));
237 /* more than 1 inst matches */
238 err = PARSE_AMBIGUOUS;
240 debug_printf("Ambiguous cmd\n");
247 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
257 debug_printf("No match err=%d\n", err);
265 complete(parse_pgm_ctx_t ctx[], const char *buf, int16_t *state,
266 char *dst, uint8_t size)
268 const char * incomplete_token = buf;
269 uint8_t inst_num = 0;
270 parse_pgm_inst_t *inst;
271 parse_pgm_token_hdr_t *token_p;
272 struct token_hdr token_hdr;
273 char tmpbuf[64], completion_buf[64];
274 uint8_t incomplete_token_len;
275 int8_t completion_len = -1;
279 uint8_t nb_completable;
280 uint8_t nb_non_completable;
281 int16_t local_state=0;
284 debug_printf("%s called\n", __FUNCTION__);
285 /* count the number of complete token to parse */
286 for (i=0 ; buf[i] ; i++) {
287 if (!isblank(buf[i]) && isblank(buf[i+1]))
289 if (isblank(buf[i]) && !isblank(buf[i+1]))
290 incomplete_token = buf+i+1;
292 incomplete_token_len = strlen(incomplete_token);
294 /* first call -> do a first pass */
296 debug_printf("try complete <%s>\n", buf);
297 debug_printf("there is %d complete tokens, <%s> is incomplete\n", nb_token, incomplete_token);
300 nb_non_completable = 0;
302 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
304 /* parse the first tokens of the inst */
305 if (nb_token && match_inst(inst, buf, nb_token, NULL))
308 debug_printf("instruction match \n");
309 token_p = (parse_pgm_token_hdr_t *) pgm_read_pgmptr(&inst->tokens[nb_token]);
311 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
313 /* non completable */
315 !token_hdr.ops->complete_get_nb ||
316 !token_hdr.ops->complete_get_elt ||
317 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
318 nb_non_completable++;
322 debug_printf("%d choices for this token\n", n);
323 for (i=0 ; i<n ; i++) {
324 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
326 strcat_P(tmpbuf, PSTR(" ")); /* we have at least room for one char */
327 debug_printf(" choice <%s>\n", tmpbuf);
328 /* does the completion match the beginning of the word ? */
329 if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
330 if (completion_len == -1) {
331 strcpy(completion_buf, tmpbuf+incomplete_token_len);
332 completion_len = strlen(tmpbuf+incomplete_token_len);
336 completion_len = nb_common_chars(completion_buf,
337 tmpbuf+incomplete_token_len);
338 completion_buf[completion_len] = 0;
345 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
348 debug_printf("total choices %d for this completion\n", nb_completable);
350 /* no possible completion */
351 if (nb_completable == 0 && nb_non_completable == 0)
354 /* if multichoice is not required */
355 if (*state == 0 && incomplete_token_len > 0) {
356 /* one or several choices starting with the
358 if (completion_len > 0) {
359 if (completion_len + 1 > size)
362 strcpy(dst, completion_buf);
368 /* init state correctly */
372 debug_printf("Multiple choice STATE=%d\n", *state);
375 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
377 /* we need to redo it */
378 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
380 if (nb_token && match_inst(inst, buf, nb_token, NULL))
383 token_p = (parse_pgm_token_hdr_t *)pgm_read_pgmptr(&inst->tokens[nb_token]);
385 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
387 /* one choice for this token */
389 !token_hdr.ops->complete_get_nb ||
390 !token_hdr.ops->complete_get_elt ||
391 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
392 if (local_state < *state) {
397 if (token_p && token_hdr.ops->get_help) {
398 token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
399 help_str = (prog_char *) pgm_read_pgmptr(&inst->help_str);
401 snprintf_P(dst, size, PSTR("[%s]: "PGMS_FMT""), tmpbuf, help_str);
403 snprintf_P(dst, size, PSTR("[%s]: No help"), tmpbuf);
406 snprintf_P(dst, size, PSTR("[RETURN]"));
411 /* several choices */
412 for (i=0 ; i<n ; i++) {
413 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
415 strcat_P(tmpbuf, PSTR(" ")); /* we have at least room for one char */
416 debug_printf(" choice <%s>\n", tmpbuf);
417 /* does the completion match the beginning of the word ? */
418 if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
419 if (local_state < *state) {
424 l=snprintf(dst, size, "%s", tmpbuf);
425 if (l>=0 && token_hdr.ops->get_help) {
426 token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
427 help_str = (prog_char *) pgm_read_pgmptr(&inst->help_str);
429 snprintf_P(dst+l, size-l, PSTR("[%s]: "PGMS_FMT), tmpbuf, help_str);
431 snprintf_P(dst+l, size-l, PSTR("[%s]: No help"), tmpbuf);
439 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);