some pgmspace types are deprecated
[aversive.git] / modules / ihm / parse / parse.c
1 /*  
2  *  Copyright Droids Corporation (2007)
3  *  Olivier MATZ <zer0@droids-corp.org>
4  * 
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.
9  *
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.
14  *
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
18  *
19  *  Revision : $Id: parse.c,v 1.1.2.11 2009-04-07 20:00:46 zer0 Exp $
20  *
21  *
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <ctype.h>
28
29 #include <aversive/pgmspace.h>
30
31 #include "parse.h"
32
33 #ifdef HOST_VERSION
34 #define pgm_read_pgmptr(x) ((void *)(*(x)))
35 #else
36 #define pgm_read_pgmptr(x) (void *)pgm_read_word(x)
37 #endif
38
39 //#define CMDLINE_DEBUG
40 //#define debug_printf printf
41 #define debug_printf(args...) do {} while(0)
42
43
44 static int
45 isendofline(char c)
46 {
47         if (c == '\n' || 
48             c == '\r' )
49                 return 1;
50         return 0;
51 }
52
53 static int
54 iscomment(char c)
55 {
56         if (c == '#')
57                 return 1;
58         return 0;
59 }
60
61 int
62 isendoftoken(char c)
63 {
64         if (!c || iscomment(c) || isblank(c) || isendofline(c))
65                 return 1;
66         return 0;
67 }
68
69 static uint8_t
70 nb_common_chars(const char * s1, const char * s2)
71 {
72         uint8_t i=0;
73
74         while (*s1==*s2 && *s1 && *s2) {
75                 s1++;
76                 s2++;
77                 i++;
78         }
79         return i;
80 }
81
82 /** 
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.
86  */
87 static int8_t
88 match_inst(PGM_P inst, const char * buf, uint8_t nb_match_token, 
89            void * result_buf)
90 {
91         uint8_t token_num=0;
92         PGM_P token_p;
93         uint8_t i=0;
94         int8_t n = 0;
95         struct token_hdr token_hdr;
96
97         token_p = pgm_read_pgmptr(inst + sizeof(struct inst));
98         if (token_p)
99                 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
100         
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");
104                 /* skip spaces */
105                 while (isblank(*buf)) {
106                         buf++;
107                 }
108                 
109                 /* end of buf */
110                 if ( isendofline(*buf) || iscomment(*buf) )
111                         break;
112                 
113                 n = token_hdr.ops->parse(token_p, buf, (result_buf ? result_buf+token_hdr.offset : NULL));
114                 if ( n < 0 )
115                         break;
116                 debug_printf("TK parsed (len=%d)\n", n);
117                 i++;
118                 buf += n;
119                 
120                 token_num ++;
121                 token_p = pgm_read_pgmptr(inst + sizeof(struct inst) +
122                                           token_num * sizeof(PGM_P));
123                 if (token_p)
124                         memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
125         }
126         
127         /* does not match */
128         if (i==0)
129                 return -1;
130         
131         /* in case we want to match a specific num of token */
132         if (nb_match_token) {
133                 if (i == nb_match_token) {
134                         return 0;
135                 }
136                 return i;
137         }
138
139         /* we don't match all the tokens */
140         if (token_p) {
141                 return i;
142         }
143
144         /* are there are some tokens more */
145         while (isblank(*buf)) {
146                 buf++;
147         }
148         
149         /* end of buf */
150         if ( isendofline(*buf) || iscomment(*buf) )
151                 return 0;
152
153         /* garbage after inst */
154         return i;
155 }
156
157
158 int8_t
159 parse(PGM_P ctx, const char * buf)
160 {
161         uint8_t inst_num=0;
162         PGM_P inst;
163         const char * curbuf;
164         char result_buf[256]; /* XXX align, size zé in broblém */
165         void (*f)(void *, void *) = NULL;
166         void * data = NULL;
167         int comment = 0;
168         int linelen = 0;
169         int parse_it = 0;
170         int8_t err = PARSE_NOMATCH;
171         int8_t tok;
172 #ifdef CMDLINE_DEBUG
173         char debug_buf[64];
174 #endif
175
176         /* 
177          * - look if the buffer contains at least one line
178          * - look if line contains only spaces or comments 
179          * - count line length
180          */
181         curbuf = buf;
182         while (! isendofline(*curbuf)) {
183                 if ( *curbuf == '\0' ) {
184                         debug_printf("Incomplete buf (len=%d)\n", linelen);
185                         return 0;
186                 }
187                 if ( iscomment(*curbuf) ) {
188                         comment = 1;
189                 }
190                 if ( ! isblank(*curbuf) && ! comment) {
191                         parse_it = 1;
192                 }
193                 curbuf++;
194                 linelen++;
195         }
196
197         /* skip all endofline chars */
198         while (isendofline(buf[linelen])) {
199                 linelen++;
200         }
201
202         /* empty line */
203         if ( parse_it == 0 ) {
204                 debug_printf("Empty line (len=%d)\n", linelen);
205                 return linelen;
206         }
207
208 #ifdef CMDLINE_DEBUG
209         snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
210         debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
211 #endif
212
213         /* parse it !! */
214         inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
215         while (inst) {
216                 debug_printf("INST\n");
217
218                 /* fully parsed */
219                 tok = match_inst(inst, buf, 0, result_buf);
220
221                 if (tok > 0) /* we matched at least one token */
222                         err = PARSE_BAD_ARGS;
223
224                 else if (!tok) {
225                         debug_printf("INST fully parsed\n");
226                         /* skip spaces */
227                         while (isblank(*curbuf)) {
228                                 curbuf++;
229                         }
230                         
231                         /* if end of buf -> there is no garbage after inst */
232                         if (isendofline(*curbuf) || iscomment(*curbuf)) {
233                                 if (!f) {
234                                         memcpy_P(&f, inst + offsetof(parse_inst_t, f), sizeof(f));
235                                         memcpy_P(&data, inst + offsetof(parse_inst_t, data), sizeof(data));
236                                 }
237                                 else {
238                                         /* more than 1 inst matches */
239                                         err = PARSE_AMBIGUOUS;
240                                         f=NULL;
241                                         debug_printf("Ambiguous cmd\n");
242                                         break;
243                                 }
244                         }
245                 }
246                         
247                 inst_num ++;
248                 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
249         }
250         
251         /* call func */
252         if (f) {
253                 f(result_buf, data);
254         }
255
256         /* no match */
257         else {
258                 debug_printf("No match err=%d\n", err);
259                 return err;
260         }
261         
262         return linelen;
263 }
264
265 int8_t 
266 complete(PGM_P ctx, const char *buf, int16_t *state, 
267          char *dst, uint8_t size)
268 {
269         const char * incomplete_token = buf;
270         uint8_t inst_num = 0;
271         PGM_P inst;
272         PGM_P token_p;
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;
277         int8_t nb_token = 0;
278         uint8_t i, n;
279         int8_t l;
280         uint8_t nb_completable;
281         uint8_t nb_non_completable;
282         int16_t local_state=0;
283         PGM_P help_str;
284
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]))
289                         nb_token++;
290                 if (isblank(buf[i]) && !isblank(buf[i+1]))
291                         incomplete_token = buf+i+1;
292         }
293         incomplete_token_len = strlen(incomplete_token);
294
295         /* first call -> do a first pass */
296         if (*state <= 0) {
297                 debug_printf("try complete <%s>\n", buf);
298                 debug_printf("there is %d complete tokens, <%s> is incomplete\n", nb_token, incomplete_token);
299
300                 nb_completable = 0;
301                 nb_non_completable = 0;
302                 
303                 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
304                 while (inst) {
305                         /* parse the first tokens of the inst */
306                         if (nb_token && match_inst(inst, buf, nb_token, NULL))
307                                 goto next;
308                         
309                         debug_printf("instruction match \n");
310                         token_p = pgm_read_pgmptr(inst + sizeof(struct inst) +
311                                                   sizeof(PGM_P) * nb_token);
312                         if (token_p)
313                                 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
314
315                         /* non completable */
316                         if (!token_p || 
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++;
321                                 goto next;
322                         }
323
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)
327                                         continue;
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);
335                                                 
336                                         }
337                                         else {
338                                                 completion_len = nb_common_chars(completion_buf, 
339                                                                                  tmpbuf+incomplete_token_len);
340                                                 completion_buf[completion_len] = 0;
341                                         }
342                                         nb_completable++;
343                                 }
344                         }               
345                 next:
346                         inst_num ++;
347                         inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
348                 }
349
350                 debug_printf("total choices %d for this completion\n", nb_completable);
351
352                 /* no possible completion */
353                 if (nb_completable == 0 && nb_non_completable == 0)
354                         return 0;
355                 
356                 /* if multichoice is not required */
357                 if (*state == 0 && incomplete_token_len > 0) {
358                         /* one or several choices starting with the
359                            same chars */
360                         if (completion_len > 0) { 
361                                 if (completion_len + 1 > size)
362                                         return 0;
363                                 
364                                 strcpy(dst, completion_buf);
365                                 return 2;
366                         }
367                 }
368         }
369
370         /* init state correctly */
371         if (*state == -1)
372                 *state = 0;
373
374         debug_printf("Multiple choice STATE=%d\n", *state);
375
376         inst_num = 0;
377         inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
378         while (inst) {
379                 /* we need to redo it */
380                 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
381                 
382                 if (nb_token && match_inst(inst, buf, nb_token, NULL))
383                         goto next2;
384                 
385                 token_p = pgm_read_pgmptr(inst + sizeof(struct inst) +
386                                           sizeof(PGM_P) * nb_token);
387                 if (token_p)
388                         memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
389
390                 /* one choice for this token */
391                 if (!token_p || 
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) {
396                                 local_state++;
397                                 goto next2;
398                         }
399                         (*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));
403                                 if (help_str)
404                                         snprintf_P(dst, size, PSTR("[%s]: "PGMS_FMT""), tmpbuf, help_str);
405                                 else
406                                         snprintf_P(dst, size, PSTR("[%s]: No help"), tmpbuf);
407                         }
408                         else {
409                                 snprintf_P(dst, size, PSTR("[RETURN]"));
410                         }
411                         return 1;
412                 }
413
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)
417                                 continue;
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) {
423                                         local_state++;
424                                         continue;
425                                 }
426                                 (*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));
431                                         if (help_str)
432                                                 snprintf_P(dst+l, size-l, PSTR("[%s]: "PGMS_FMT), tmpbuf, help_str);
433                                         else
434                                                 snprintf_P(dst+l, size-l, PSTR("[%s]: No help"), tmpbuf);
435                                 }
436
437                                 return 1;
438                         }
439                 }
440         next2:
441                 inst_num ++;
442                 inst = pgm_read_pgmptr(ctx + inst_num * sizeof(PGM_P));
443         }
444         return 0;
445 }
446