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(PGM_P inst, const char * buf, uint8_t nb_match_token,
95 struct token_hdr token_hdr;
97 token_p = pgm_read_pgmptr(inst + sizeof(struct inst));
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 = pgm_read_pgmptr(inst + sizeof(struct inst) +
122 token_num * sizeof(PGM_P));
124 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
131 /* in case we want to match a specific num of token */
132 if (nb_match_token) {
133 if (i == nb_match_token) {
139 /* we don't match all the tokens */
144 /* are there are some tokens more */
145 while (isblank(*buf)) {
150 if ( isendofline(*buf) || iscomment(*buf) )
153 /* garbage after inst */
159 parse(PGM_P ctx, const char * buf)
164 char result_buf[256]; /* XXX align, size zé in broblém */
165 void (*f)(void *, void *) = NULL;
170 int8_t err = PARSE_NOMATCH;
177 * - look if the buffer contains at least one line
178 * - look if line contains only spaces or comments
179 * - count line length
182 while (! isendofline(*curbuf)) {
183 if ( *curbuf == '\0' ) {
184 debug_printf("Incomplete buf (len=%d)\n", linelen);
187 if ( iscomment(*curbuf) ) {
190 if ( ! isblank(*curbuf) && ! comment) {
197 /* skip all endofline chars */
198 while (isendofline(buf[linelen])) {
203 if ( parse_it == 0 ) {
204 debug_printf("Empty line (len=%d)\n", linelen);
209 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
210 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
214 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
216 debug_printf("INST\n");
219 tok = match_inst(inst, buf, 0, result_buf);
221 if (tok > 0) /* we matched at least one token */
222 err = PARSE_BAD_ARGS;
225 debug_printf("INST fully parsed\n");
227 while (isblank(*curbuf)) {
231 /* if end of buf -> there is no garbage after inst */
232 if (isendofline(*curbuf) || iscomment(*curbuf)) {
234 memcpy_P(&f, inst + offsetof(parse_inst_t, f), sizeof(f));
235 memcpy_P(&data, inst + offsetof(parse_inst_t, data), sizeof(data));
238 /* more than 1 inst matches */
239 err = PARSE_AMBIGUOUS;
241 debug_printf("Ambiguous cmd\n");
248 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
258 debug_printf("No match err=%d\n", err);
266 complete(PGM_P ctx, const char *buf, int16_t *state,
267 char *dst, uint8_t size)
269 const char * incomplete_token = buf;
270 uint8_t inst_num = 0;
273 struct token_hdr token_hdr;
274 char tmpbuf[64], completion_buf[64];
275 uint8_t incomplete_token_len;
276 int8_t completion_len = -1;
280 uint8_t nb_completable;
281 uint8_t nb_non_completable;
282 int16_t local_state=0;
285 debug_printf("%s called\n", __FUNCTION__);
286 /* count the number of complete token to parse */
287 for (i=0 ; buf[i] ; i++) {
288 if (!isblank(buf[i]) && isblank(buf[i+1]))
290 if (isblank(buf[i]) && !isblank(buf[i+1]))
291 incomplete_token = buf+i+1;
293 incomplete_token_len = strlen(incomplete_token);
295 /* first call -> do a first pass */
297 debug_printf("try complete <%s>\n", buf);
298 debug_printf("there is %d complete tokens, <%s> is incomplete\n", nb_token, incomplete_token);
301 nb_non_completable = 0;
303 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
305 /* parse the first tokens of the inst */
306 if (nb_token && match_inst(inst, buf, nb_token, NULL))
309 debug_printf("instruction match \n");
310 token_p = pgm_read_pgmptr(inst + sizeof(struct inst) +
311 sizeof(PGM_P) * nb_token);
313 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
315 /* non completable */
317 !token_hdr.ops->complete_get_nb ||
318 !token_hdr.ops->complete_get_elt ||
319 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
320 nb_non_completable++;
324 debug_printf("%d choices for this token\n", n);
325 for (i=0 ; i<n ; i++) {
326 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
328 strcat_P(tmpbuf, PSTR(" ")); /* we have at least room for one char */
329 debug_printf(" choice <%s>\n", tmpbuf);
330 /* does the completion match the beginning of the word ? */
331 if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
332 if (completion_len == -1) {
333 strcpy(completion_buf, tmpbuf+incomplete_token_len);
334 completion_len = strlen(tmpbuf+incomplete_token_len);
338 completion_len = nb_common_chars(completion_buf,
339 tmpbuf+incomplete_token_len);
340 completion_buf[completion_len] = 0;
347 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
350 debug_printf("total choices %d for this completion\n", nb_completable);
352 /* no possible completion */
353 if (nb_completable == 0 && nb_non_completable == 0)
356 /* if multichoice is not required */
357 if (*state == 0 && incomplete_token_len > 0) {
358 /* one or several choices starting with the
360 if (completion_len > 0) {
361 if (completion_len + 1 > size)
364 strcpy(dst, completion_buf);
370 /* init state correctly */
374 debug_printf("Multiple choice STATE=%d\n", *state);
377 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
379 /* we need to redo it */
380 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
382 if (nb_token && match_inst(inst, buf, nb_token, NULL))
385 token_p = pgm_read_pgmptr(inst + sizeof(struct inst) +
386 sizeof(PGM_P) * nb_token);
388 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
390 /* one choice for this token */
392 !token_hdr.ops->complete_get_nb ||
393 !token_hdr.ops->complete_get_elt ||
394 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
395 if (local_state < *state) {
400 if (token_p && token_hdr.ops->get_help) {
401 token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
402 help_str = pgm_read_pgmptr(inst + offsetof(struct inst, help_str));
404 snprintf_P(dst, size, PSTR("[%s]: "PGMS_FMT""), tmpbuf, help_str);
406 snprintf_P(dst, size, PSTR("[%s]: No help"), tmpbuf);
409 snprintf_P(dst, size, PSTR("[RETURN]"));
414 /* several choices */
415 for (i=0 ; i<n ; i++) {
416 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
418 strcat_P(tmpbuf, PSTR(" ")); /* we have at least room for one char */
419 debug_printf(" choice <%s>\n", tmpbuf);
420 /* does the completion match the beginning of the word ? */
421 if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
422 if (local_state < *state) {
427 l=snprintf(dst, size, "%s", tmpbuf);
428 if (l>=0 && token_hdr.ops->get_help) {
429 token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
430 help_str = pgm_read_pgmptr(inst + offsetof(struct inst, help_str));
432 snprintf_P(dst+l, size-l, PSTR("[%s]: "PGMS_FMT), tmpbuf, help_str);
434 snprintf_P(dst+l, size-l, PSTR("[%s]: No help"), tmpbuf);
442 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));