better compilation on host
[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(parse_pgm_inst_t *inst, const char * buf, uint8_t nb_match_token, 
89            void * result_buf)
90 {
91         uint8_t token_num=0;
92         parse_pgm_token_hdr_t * token_p;
93         uint8_t i=0;
94         int8_t n = 0;
95         struct token_hdr token_hdr;
96
97         token_p = (parse_pgm_token_hdr_t *)pgm_read_pgmptr(&inst->tokens[token_num]);
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 = (parse_pgm_token_hdr_t *)pgm_read_pgmptr(&inst->tokens[token_num]);
122                 if (token_p)
123                         memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
124         }
125         
126         /* does not match */
127         if (i==0)
128                 return -1;
129         
130         /* in case we want to match a specific num of token */
131         if (nb_match_token) {
132                 if (i == nb_match_token) {
133                         return 0;
134                 }
135                 return i;
136         }
137
138         /* we don't match all the tokens */
139         if (token_p) {
140                 return i;
141         }
142
143         /* are there are some tokens more */
144         while (isblank(*buf)) {
145                 buf++;
146         }
147         
148         /* end of buf */
149         if ( isendofline(*buf) || iscomment(*buf) )
150                 return 0;
151
152         /* garbage after inst */
153         return i;
154 }
155
156
157 int8_t
158 parse(parse_pgm_ctx_t ctx[], const char * buf)
159 {
160         uint8_t inst_num=0;
161         parse_pgm_inst_t * inst;
162         const char * curbuf;
163         char result_buf[256]; /* XXX align, size zé in broblém */
164         void (*f)(void *, void *) = NULL;
165         void * data = NULL;
166         int comment = 0;
167         int linelen = 0;
168         int parse_it = 0;
169         int8_t err = PARSE_NOMATCH;
170         int8_t tok;
171 #ifdef CMDLINE_DEBUG
172         char debug_buf[64];
173 #endif
174
175         /* 
176          * - look if the buffer contains at least one line
177          * - look if line contains only spaces or comments 
178          * - count line length
179          */
180         curbuf = buf;
181         while (! isendofline(*curbuf)) {
182                 if ( *curbuf == '\0' ) {
183                         debug_printf("Incomplete buf (len=%d)\n", linelen);
184                         return 0;
185                 }
186                 if ( iscomment(*curbuf) ) {
187                         comment = 1;
188                 }
189                 if ( ! isblank(*curbuf) && ! comment) {
190                         parse_it = 1;
191                 }
192                 curbuf++;
193                 linelen++;
194         }
195
196         /* skip all endofline chars */
197         while (isendofline(buf[linelen])) {
198                 linelen++;
199         }
200
201         /* empty line */
202         if ( parse_it == 0 ) {
203                 debug_printf("Empty line (len=%d)\n", linelen);
204                 return linelen;
205         }
206
207 #ifdef CMDLINE_DEBUG
208         snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
209         debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
210 #endif
211
212         /* parse it !! */
213         inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
214         while (inst) {
215                 debug_printf("INST\n");
216
217                 /* fully parsed */
218                 tok = match_inst(inst, buf, 0, result_buf);
219
220                 if (tok > 0) /* we matched at least one token */
221                         err = PARSE_BAD_ARGS;
222
223                 else if (!tok) {
224                         debug_printf("INST fully parsed\n");
225                         /* skip spaces */
226                         while (isblank(*curbuf)) {
227                                 curbuf++;
228                         }
229                         
230                         /* if end of buf -> there is no garbage after inst */
231                         if (isendofline(*curbuf) || iscomment(*curbuf)) {
232                                 if (!f) {
233                                         memcpy_P(&f, &inst->f, sizeof(f));
234                                         memcpy_P(&data, &inst->data, sizeof(data));
235                                 }
236                                 else {
237                                         /* more than 1 inst matches */
238                                         err = PARSE_AMBIGUOUS;
239                                         f=NULL;
240                                         debug_printf("Ambiguous cmd\n");
241                                         break;
242                                 }
243                         }
244                 }
245                         
246                 inst_num ++;
247                 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
248         }
249         
250         /* call func */
251         if (f) {
252                 f(result_buf, data);
253         }
254
255         /* no match */
256         else {
257                 debug_printf("No match err=%d\n", err);
258                 return err;
259         }
260         
261         return linelen;
262 }
263
264 int8_t 
265 complete(parse_pgm_ctx_t ctx[], const char *buf, int16_t *state, 
266          char *dst, uint8_t size)
267 {
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;
276         int8_t nb_token = 0;
277         uint8_t i, n;
278         int8_t l;
279         uint8_t nb_completable;
280         uint8_t nb_non_completable;
281         int16_t local_state=0;
282         prog_char *help_str;
283
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]))
288                         nb_token++;
289                 if (isblank(buf[i]) && !isblank(buf[i+1]))
290                         incomplete_token = buf+i+1;
291         }
292         incomplete_token_len = strlen(incomplete_token);
293
294         /* first call -> do a first pass */
295         if (*state <= 0) {
296                 debug_printf("try complete <%s>\n", buf);
297                 debug_printf("there is %d complete tokens, <%s> is incomplete\n", nb_token, incomplete_token);
298
299                 nb_completable = 0;
300                 nb_non_completable = 0;
301                 
302                 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
303                 while (inst) {
304                         /* parse the first tokens of the inst */
305                         if (nb_token && match_inst(inst, buf, nb_token, NULL))
306                                 goto next;
307                         
308                         debug_printf("instruction match \n");
309                         token_p = (parse_pgm_token_hdr_t *) pgm_read_pgmptr(&inst->tokens[nb_token]);
310                         if (token_p)
311                                 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
312
313                         /* non completable */
314                         if (!token_p || 
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++;
319                                 goto next;
320                         }
321
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)
325                                         continue;
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);
333                                                 
334                                         }
335                                         else {
336                                                 completion_len = nb_common_chars(completion_buf, 
337                                                                                  tmpbuf+incomplete_token_len);
338                                                 completion_buf[completion_len] = 0;
339                                         }
340                                         nb_completable++;
341                                 }
342                         }               
343                 next:
344                         inst_num ++;
345                         inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
346                 }
347
348                 debug_printf("total choices %d for this completion\n", nb_completable);
349
350                 /* no possible completion */
351                 if (nb_completable == 0 && nb_non_completable == 0)
352                         return 0;
353                 
354                 /* if multichoice is not required */
355                 if (*state == 0 && incomplete_token_len > 0) {
356                         /* one or several choices starting with the
357                            same chars */
358                         if (completion_len > 0) { 
359                                 if (completion_len + 1 > size)
360                                         return 0;
361                                 
362                                 strcpy(dst, completion_buf);
363                                 return 2;
364                         }
365                 }
366         }
367
368         /* init state correctly */
369         if (*state == -1)
370                 *state = 0;
371
372         debug_printf("Multiple choice STATE=%d\n", *state);
373
374         inst_num = 0;
375         inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
376         while (inst) {
377                 /* we need to redo it */
378                 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
379                 
380                 if (nb_token && match_inst(inst, buf, nb_token, NULL))
381                         goto next2;
382                 
383                 token_p = (parse_pgm_token_hdr_t *)pgm_read_pgmptr(&inst->tokens[nb_token]);
384                 if (token_p)
385                         memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
386
387                 /* one choice for this token */
388                 if (!token_p || 
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) {
393                                 local_state++;
394                                 goto next2;
395                         }
396                         (*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);
400                                 if (help_str)
401                                         snprintf_P(dst, size, PSTR("[%s]: "PGMS_FMT""), tmpbuf, help_str);
402                                 else
403                                         snprintf_P(dst, size, PSTR("[%s]: No help"), tmpbuf);
404                         }
405                         else {
406                                 snprintf_P(dst, size, PSTR("[RETURN]"));
407                         }
408                         return 1;
409                 }
410
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)
414                                 continue;
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) {
420                                         local_state++;
421                                         continue;
422                                 }
423                                 (*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);
428                                         if (help_str)
429                                                 snprintf_P(dst+l, size-l, PSTR("[%s]: "PGMS_FMT), tmpbuf, help_str);
430                                         else
431                                                 snprintf_P(dst+l, size-l, PSTR("[%s]: No help"), tmpbuf);
432                                 }
433
434                                 return 1;
435                         }
436                 }
437         next2:
438                 inst_num ++;
439                 inst = (parse_pgm_inst_t *)pgm_read_pgmptr(ctx+inst_num);
440         }
441         return 0;
442 }
443