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