Commit from zer0 on branch b_zer0 (2007-11-22 22:40 CET) =================================
Port to progmem. Maybe it does not work on x86 anymore, but I'll fix it soon. The only missing thing is having struct token in pgmspace too. aversive modules/ihm/parse/parse.c 1.1.2.7 aversive modules/ihm/parse/parse.h 1.1.2.7 aversive modules/ihm/parse/parse_num.c 1.1.2.6 aversive modules/ihm/parse/parse_string.c 1.1.2.5 aversive modules/ihm/parse/parse_string.h 1.1.2.6 ================================== aversive/modules/ihm/parse/parse.c (1.1.2.6 -> 1.1.2.7) ================================== @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Revision : $Id: parse.c,v 1.1.2.6 2007-11-21 21:54:39 zer0 Exp $ + * Revision : $Id: parse.c,v 1.1.2.7 2007-11-22 21:40:17 zer0 Exp $ * * */ @@ -30,11 +30,6 @@ #include "parse.h" -/* XXX */ -#ifndef BUFSIZ -#define BUFSIZ 64 -#endif - //#define CMDLINE_DEBUG //#define debug_printf printf #define debug_printf(args...) do {} while(0) @@ -84,16 +79,15 @@ * tokens, else the number of matched tokens, else -1. */ static int8_t -match_inst(struct inst * inst, const char * buf, uint8_t nb_match_token, +match_inst(parse_pgm_inst_t *inst, const char * buf, uint8_t nb_match_token, void * result_buf) { - struct token_hdr ** token_list; + uint8_t token_num=0; struct token_hdr * token; uint8_t i=0; int8_t n = 0; - token_list = (struct token_hdr **) inst->tokens; - token = *token_list; + token = (struct token_hdr *)pgm_read_word(&inst->tokens[token_num]); /* check if we match all tokens of inst */ while (token && (!nb_match_token || i<nb_match_token)) { @@ -114,8 +108,8 @@ i++; buf += n; - token_list ++; - token = *token_list; + token_num ++; + token = (struct token_hdr *)pgm_read_word(&inst->tokens[token_num]); } /* does not match */ @@ -150,10 +144,10 @@ int8_t -parse(struct ctx * ctx, const char * buf) +parse(parse_pgm_ctx_t ctx[], const char * buf) { - struct inst ** inst_list = (struct inst **) ctx->insts; - struct inst * inst; + uint8_t inst_num=0; + parse_pgm_inst_t * inst; const char * curbuf; char result_buf[256]; /* XXX align, size zé in broblém */ void (*f)(void *, void *) = NULL; @@ -205,7 +199,7 @@ #endif /* parse it !! */ - inst = *inst_list; + inst = (struct inst *)pgm_read_word(ctx+inst_num); while (inst) { debug_printf("INST\n"); @@ -225,8 +219,8 @@ /* if end of buf -> there is no garbage after inst */ if (isendofline(*curbuf) || iscomment(*curbuf)) { if (!f) { - f = inst->f; - data = inst->data; + memcpy_P(&f, &inst->f, sizeof(f)); + memcpy_P(&data, &inst->data, sizeof(data)); } else { /* more than 1 inst matches */ @@ -238,8 +232,8 @@ } } - inst_list ++; - inst = *inst_list; + inst_num ++; + inst = (struct inst *)pgm_read_word(ctx+inst_num); } /* call func */ @@ -257,11 +251,11 @@ } int8_t -complete(struct ctx * ctx, const char *buf, int16_t *state, +complete(parse_pgm_ctx_t ctx[], const char *buf, int16_t *state, char *dst, uint8_t size) { const char * incomplete_token = buf; - struct inst ** inst_list = (struct inst **) ctx->insts; + uint8_t inst_num = 0; struct inst * inst; struct token_hdr * token; char tmpbuf[64], completion_buf[64]; @@ -273,6 +267,7 @@ uint8_t nb_completable; uint8_t nb_non_completable; uint16_t local_state=0; + prog_char * help_str; debug_printf("%s called\n", __FUNCTION__); /* count the number of complete token to parse */ @@ -291,15 +286,15 @@ nb_completable = 0; nb_non_completable = 0; - - inst = *inst_list; + + inst = (struct inst *)pgm_read_word(ctx+inst_num); while (inst) { /* parse the first tokens of the inst */ if (nb_token && match_inst(inst, buf, nb_token, NULL)) goto next; debug_printf("instruction match \n"); - token = inst->tokens[nb_token]; + token = (struct token_hdr *) pgm_read_word(&inst->tokens[nb_token]); /* non completable */ if (!token || @@ -332,8 +327,8 @@ } } next: - inst_list ++; - inst = *inst_list; + inst_num ++; + inst = (struct inst *)pgm_read_word(ctx+inst_num); } debug_printf("total choices %d for this completion\n", nb_completable); @@ -362,15 +357,18 @@ debug_printf("Multiple choice STATE=%d\n", *state); - inst_list = (struct inst **) ctx->insts; - inst = *inst_list; + inst_num = 0; + inst = (struct inst *)pgm_read_word(ctx+inst_num); while (inst) { - inst = *inst_list; + /* we need to redo it */ + inst = (struct inst *)pgm_read_word(ctx+inst_num); if (nb_token && match_inst(inst, buf, nb_token, NULL)) goto next2; - token = inst->tokens[nb_token]; + token = (struct token_hdr *)pgm_read_word(&inst->tokens[nb_token]); + + /* one choice for this token */ if (!token || !token->ops->complete_get_nb || !token->ops->complete_get_elt || @@ -382,8 +380,9 @@ (*state)++; if (token && token->ops->get_help) { token->ops->get_help(token, tmpbuf, sizeof(tmpbuf)); - if (inst->help_str) - snprintf_P(dst, size, PSTR("[%s]: %s"), tmpbuf, inst->help_str); + help_str = (prog_char *) pgm_read_word(&inst->help_str); + if (help_str) + snprintf_P(dst, size, PSTR("[%s]: %S"), tmpbuf, help_str); else snprintf_P(dst, size, PSTR("[%s]: No help"), tmpbuf); } @@ -393,6 +392,7 @@ return 1; } + /* several choices */ for (i=0 ; i<n ; i++) { if (token->ops->complete_get_elt(token, i, tmpbuf, sizeof(tmpbuf)-1) < 0) continue; @@ -408,17 +408,21 @@ l=snprintf(dst, size, "%s", tmpbuf); if (l>=0 && token->ops->get_help) { token->ops->get_help(token, tmpbuf, sizeof(tmpbuf)); - snprintf(dst+l, size-l, "[%s]: %s", tmpbuf, inst->help_str?:"No help"); + help_str = (prog_char *) pgm_read_word(&inst->help_str); + if (help_str) + snprintf_P(dst+l, size-l, PSTR("[%s]: %S"), tmpbuf, help_str); + else + snprintf_P(dst+l, size-l, PSTR("[%s]: No help"), tmpbuf); } return 1; } } next2: - inst_list ++; - inst = *inst_list; + inst_num ++; + inst = (struct inst *)pgm_read_word(ctx+inst_num); } return 0; - } + ================================== aversive/modules/ihm/parse/parse.h (1.1.2.6 -> 1.1.2.7) ================================== @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Revision : $Id: parse.h,v 1.1.2.6 2007-11-21 21:54:39 zer0 Exp $ + * Revision : $Id: parse.h,v 1.1.2.7 2007-11-22 21:40:17 zer0 Exp $ * * */ @@ -24,6 +24,7 @@ #ifndef _PARSE_H_ #define _PARSE_H_ +#include <aversive/pgmspace.h> #include <aversive/types.h> #ifndef offsetof @@ -86,15 +87,16 @@ char * help_str; void * tokens[]; }; +typedef struct inst parse_inst_t; +typedef PROGMEM parse_inst_t parse_pgm_inst_t; /** * A context is identified by its name, and contains a list of * instruction + * */ -struct ctx { - char * name; - struct inst * insts[]; -}; +typedef parse_pgm_inst_t * parse_ctx_t; +typedef PROGMEM parse_ctx_t parse_pgm_ctx_t; /** * Try to parse a buffer according to the specified context. The @@ -103,7 +105,7 @@ * calls the associated function (defined in the context) and returns * 0 (PARSE_SUCCESS). */ -int8_t parse(struct ctx * ctx, const char * buf); +int8_t parse(parse_pgm_ctx_t ctx[], const char * buf); /** * complete() must be called with *state==0. @@ -120,7 +122,7 @@ * The returned dst buf ends with \0. * */ -int8_t complete(struct ctx * ctx, const char *buf, int16_t *state, +int8_t complete(parse_pgm_ctx_t ctx[], const char *buf, int16_t *state, char *dst, uint8_t size); ====================================== aversive/modules/ihm/parse/parse_num.c (1.1.2.5 -> 1.1.2.6) ====================================== @@ -53,14 +53,17 @@ }; /* Keep it sync with enum in .h */ -static const char * num_help[] = { - "UINT8", - "UINT16", - "UINT32", - "INT8", - "INT16", - "INT32", - "FLOAT", +static const prog_char help1[] = "UINT8"; +static const prog_char help2[] = "UINT16"; +static const prog_char help3[] = "UINT32"; +static const prog_char help4[] = "INT8"; +static const prog_char help5[] = "INT16"; +static const prog_char help6[] = "INT32"; +static const prog_char help7[] = "FLOAT"; + +static const prog_char * num_help[] = { + help1, help2, help3, help4, + help5, help6, help7, }; static inline int8_t @@ -404,7 +407,7 @@ /* if (type >= (sizeof(num_help)/sizeof(const char *))) */ /* return -1; */ - strncpy(dstbuf, num_help[type], size); + strncpy_P(dstbuf, num_help[type], size); dstbuf[size-1] = '\0'; return 0; } ========================================= aversive/modules/ihm/parse/parse_string.c (1.1.2.4 -> 1.1.2.5) ========================================= @@ -13,25 +13,31 @@ .get_help = get_help_string, }; -#define MULTISTRING_HELP "Mul-choice STRING" -#define ANYSTRING_HELP "Any STRING" -#define FIXEDSTRING_HELP "Fixed STRING" +#define MULTISTRING_HELP PSTR("Mul-choice STRING") +#define ANYSTRING_HELP PSTR("Any STRING") +#define FIXEDSTRING_HELP PSTR("Fixed STRING") static uint8_t -get_token_len(const char * s) +get_token_len(const prog_char * s) { - const char *s2=s; - while (*(s2)!='#' && *(s2)!='\0') - s2++; - return s2-s; + prog_char c; + uint8_t i=0; + + c = pgm_read_byte(s+i); + while (c!='#' && c!='\0') { + i++; + c = pgm_read_byte(s+i); + } + return i; } -static const char * -get_next_token(const char * s) +static const prog_char * +get_next_token(const prog_char * s) { - s = strchr(s, '#'); - if (s) - return s+1; + uint8_t i; + i = get_token_len(s); + if (pgm_read_byte(s+i) == '#') + return s+i+1; return NULL; } @@ -40,7 +46,7 @@ { struct token_string * token = data; uint8_t token_len; - const char * str; + const prog_char * str; if (! *buf) return -1; @@ -56,7 +62,7 @@ continue; } - if ( strncmp(buf, str, token_len) ) { + if ( strncmp_P(buf, str, token_len) ) { continue; } @@ -95,14 +101,13 @@ int8_t complete_get_nb_string(void * data) { struct token_string * token = data; - const char * str = token->str; + const prog_char * str = token->str; int8_t ret=1; if (!str) return 0; - while( (str = strchr(str, '#')) != NULL ) { - str++; + while( (str = get_next_token(str)) != NULL ) { ret++; } return ret; @@ -112,7 +117,7 @@ char * dstbuf, uint8_t size) { struct token_string * token = data; - const char * s = token->str; + const prog_char * s = token->str; uint8_t len; while (idx-- && s) @@ -125,7 +130,7 @@ if (len > size) return -1; - memcpy(dstbuf, s, len); + memcpy_P(dstbuf, s, len); dstbuf[len] = '\0'; return 0; @@ -135,18 +140,18 @@ int8_t get_help_string(void * data, char * dstbuf, uint8_t size) { struct token_string * token = data; - const char * s = token->str; + const prog_char * s = token->str; - if (token->str) { - if (strchr(s, '#')) { - strncpy(dstbuf, MULTISTRING_HELP, size); + if (s) { + if (get_next_token(s)) { + strncpy_P(dstbuf, MULTISTRING_HELP, size); } else { - strncpy(dstbuf, FIXEDSTRING_HELP, size); + strncpy_P(dstbuf, FIXEDSTRING_HELP, size); } } else { - strncpy(dstbuf, ANYSTRING_HELP, size); + strncpy_P(dstbuf, ANYSTRING_HELP, size); } dstbuf[size-1] = '\0'; ========================================= aversive/modules/ihm/parse/parse_string.h (1.1.2.5 -> 1.1.2.6) ========================================= @@ -10,7 +10,7 @@ struct token_string { struct token_hdr hdr; - const char * str; + prog_char * str; }; extern struct token_ops token_string_ops;
_______________________________________________ Avr-list mailing list Avr-list@droids-corp.org CVSWEB : http://cvsweb.droids-corp.org/cgi-bin/viewcvs.cgi/aversive WIKI : http://wiki.droids-corp.org/index.php/Aversive DOXYGEN : http://zer0.droids-corp.org/doxygen_aversive/html/ BUGZILLA : http://bugzilla.droids-corp.org COMMIT LOGS : http://zer0.droids-corp.org/aversive_commitlog