rc_proto: packed struct
[protos/xbee-avr.git] / 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 struct token_hdr_pgm {
50         struct token_ops *ops;
51         uint8_t offset;
52 } PROGMEM;
53 typedef struct token_hdr_pgm parse_pgm_token_hdr_t;
54
55 /**
56  * A token is defined by this structure.
57  *
58  * parse() takes the token as first argument, then the source buffer
59  * starting at the token we want to parse. The 3rd arg is a pointer
60  * where we store the parsed data (as binary). It returns the number of
61  * parsed chars on success and a negative value on error.
62  *
63  * complete_get_nb() returns the number of possible values for this
64  * token if completion is possible. If it is NULL or if it returns 0, 
65  * no completion is possible.
66  *
67  * complete_get_elt() copy in dstbuf (the size is specified in the
68  * parameter) the i-th possible completion for this token.  returns 0
69  * on success or and a negative value on error.
70  *
71  * get_help() fills the dstbuf with the help for the token. It returns
72  * -1 on error and 0 on success.
73  */
74 struct token_ops {
75         /** parse(token ptr, buf, res pts) */
76         int8_t (*parse)(parse_pgm_token_hdr_t *, const char *, void *);
77         /** return the num of possible choices for this token */
78         int8_t (*complete_get_nb)(parse_pgm_token_hdr_t *);
79         /** return the elt x for this token (token, idx, dstbuf, size) */
80         int8_t (*complete_get_elt)(parse_pgm_token_hdr_t *, int8_t, char *, uint8_t);
81         /** get help for this token (token, dstbuf, size) */
82         int8_t (*get_help)(parse_pgm_token_hdr_t *, char *, uint8_t);
83 };      
84
85 /**
86  * Store a instruction, which is a pointer to a callback function and
87  * its parameter that is called when the instruction is parsed, a help
88  * string, and a list of token composing this instruction.
89  */
90 struct inst {
91         /* f(parsed_struct, data) */
92         void (*f)(void *, void *);
93         void * data;
94         char * help_str;
95         prog_void * tokens[];
96 };
97 typedef struct inst parse_inst_t;
98 struct inst_pgm {
99         /* f(parsed_struct, data) */
100         void (*f)(void *, void *);
101         void * data;
102         char * help_str;
103         prog_void * tokens[];
104 } PROGMEM;
105 typedef struct inst_pgm parse_pgm_inst_t;
106
107 /**
108  * A context is identified by its name, and contains a list of
109  * instruction 
110  *
111  */
112 typedef parse_pgm_inst_t * parse_ctx_t;
113 typedef PROGMEM parse_ctx_t parse_pgm_ctx_t;
114
115 /**
116  * Try to parse a buffer according to the specified context. The
117  * argument buf must ends with "\n\0". The function returns
118  * PARSE_AMBIGUOUS, PARSE_NOMATCH or PARSE_BAD_ARGS on error. Else it
119  * calls the associated function (defined in the context) and returns
120  * 0 (PARSE_SUCCESS).
121  */
122 int8_t parse(parse_pgm_ctx_t ctx[], const char * buf);
123
124 /**
125  * complete() must be called with *state==0.
126  * It returns < 0 on error. 
127  *
128  * Else it returns:
129  * 2 on completion (one possible choice). In this case, the chars
130  *   are appended in dst buffer.
131  * 1 if there is several possible choices. In this case, you must 
132  *   call the function again, keeping the value of state intact.
133  * 0 when the iteration is finished. The dst is not valid for this 
134  *   last call.
135  *
136  * The returned dst buf ends with \0.
137  * 
138  */
139 int8_t complete(parse_pgm_ctx_t ctx[], const char *buf, int16_t *state, 
140                 char *dst, uint8_t size);
141
142
143 /* true if(!c || iscomment(c) || isblank(c) || isendofline(c)) */
144 int isendoftoken(char c);
145
146 #endif /* _PARSE_H_ */