config: update test config files to current aversive config.in
[aversive.git] / modules / ihm / parse / parse.h
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.h,v 1.1.2.9 2009-01-03 16:25:13 zer0 Exp $
20  *
21  *
22  */
23
24 #ifndef _PARSE_H_
25 #define _PARSE_H_
26
27 #include <aversive/pgmspace.h>
28 #include <aversive/types.h>
29
30 #ifndef offsetof
31 #define offsetof(type, field)  ((size_t) &( ((type *)0)->field) )
32 #endif
33
34 #define PARSE_SUCCESS        0
35 #define PARSE_AMBIGUOUS     -1
36 #define PARSE_NOMATCH       -2
37 #define PARSE_BAD_ARGS      -3
38
39 /**
40  * Stores a pointer to the ops struct, and the offset: the place to
41  * write the parsed result in the destination structure.
42  */
43 struct token_hdr {
44         struct token_ops *ops;
45         uint8_t offset;
46 };
47 typedef struct token_hdr parse_token_hdr_t;
48
49 /**
50  * A token is defined by this structure.
51  *
52  * parse() takes the token as first argument, then the source buffer
53  * starting at the token we want to parse. The 3rd arg is a pointer
54  * where we store the parsed data (as binary). It returns the number of
55  * parsed chars on success and a negative value on error.
56  *
57  * complete_get_nb() returns the number of possible values for this
58  * token if completion is possible. If it is NULL or if it returns 0, 
59  * no completion is possible.
60  *
61  * complete_get_elt() copy in dstbuf (the size is specified in the
62  * parameter) the i-th possible completion for this token.  returns 0
63  * on success or and a negative value on error.
64  *
65  * get_help() fills the dstbuf with the help for the token. It returns
66  * -1 on error and 0 on success.
67  */
68 struct token_ops {
69         /** parse(token ptr, buf, res pts) */
70         int8_t (*parse)(PGM_P, const char *, void *);
71         /** return the num of possible choices for this token */
72         int8_t (*complete_get_nb)(PGM_P);
73         /** return the elt x for this token (token, idx, dstbuf, size) */
74         int8_t (*complete_get_elt)(PGM_P, int8_t, char *, uint8_t);
75         /** get help for this token (token, dstbuf, size) */
76         int8_t (*get_help)(PGM_P, char *, uint8_t);
77 };      
78
79 /**
80  * Store a instruction, which is a pointer to a callback function and
81  * its parameter that is called when the instruction is parsed, a help
82  * string, and a list of token composing this instruction.
83  */
84 struct inst {
85         /* f(parsed_struct, data) */
86         void (*f)(void *, void *);
87         void * data;
88         const char * help_str;
89         PGM_P tokens[];
90 };
91 typedef struct inst parse_inst_t;
92
93 /**
94  * A context is identified by its name, and contains a list of
95  * instruction 
96  *
97  */
98 typedef const parse_inst_t * parse_ctx_t;
99
100 /**
101  * Try to parse a buffer according to the specified context. The
102  * argument buf must ends with "\n\0". The function returns
103  * PARSE_AMBIGUOUS, PARSE_NOMATCH or PARSE_BAD_ARGS on error. Else it
104  * calls the associated function (defined in the context) and returns
105  * 0 (PARSE_SUCCESS).
106  */
107 int8_t parse(PGM_P ctx, const char * buf);
108
109 /**
110  * complete() must be called with *state==0.
111  * It returns < 0 on error. 
112  *
113  * Else it returns:
114  * 2 on completion (one possible choice). In this case, the chars
115  *   are appended in dst buffer.
116  * 1 if there is several possible choices. In this case, you must 
117  *   call the function again, keeping the value of state intact.
118  * 0 when the iteration is finished. The dst is not valid for this 
119  *   last call.
120  *
121  * The returned dst buf ends with \0.
122  * 
123  */
124 int8_t complete(PGM_P ctx, const char *buf, int16_t *state, 
125                 char *dst, uint8_t size);
126
127
128 /* true if(!c || iscomment(c) || isblank(c) || isendofline(c)) */
129 int isendoftoken(char c);
130
131 #endif /* _PARSE_H_ */