Commit from zer0 on branch b_zer0 (2007-09-22 16:27 CEST)
=================================

enhance parse lib

  aversive  modules/ihm/parse/parse.c               1.1.2.2
  aversive  modules/ihm/parse/parse.h               1.1.2.2
  aversive  modules/ihm/parse/parse_num.h           1.1.2.2
  aversive  modules/ihm/parse/parse_string.c        1.1.2.2
  aversive  modules/ihm/parse/parse_string.h        1.1.2.2
+ aversive  modules/ihm/parse/test/.config          1.1.2.1
  aversive  modules/ihm/parse/test/main.c           1.1.2.2
+ aversive  modules/ihm/parse/test/rdline_config.h  1.1.2.1


==================================
aversive/modules/ihm/parse/parse.c  (1.1.2.1 -> 1.1.2.2)
==================================

@@ -9,6 +9,9 @@
 #define BUFSIZ 128
 #endif
 
+#define CMDLINE_DEBUG
+#define debug_printf printf
+
 static int
 isendofline(char c)
 {
@@ -47,7 +50,7 @@
        char result_buf[BUFSIZ]; /* XXX align, size */
        void (*f)(void *, void *) = NULL;
        void * data = NULL;
-       int8_t n;
+       int8_t n = 0;
        int comment = 0;
        int linelen = 0;
        int parse_it = 0;
@@ -63,7 +66,7 @@
        curbuf = buf;
        while (! isendofline(*curbuf)) {
                if ( *curbuf == '\0' ) {
-                       dprintf("Incomplete buf (len=%d)\n", linelen);
+                       debug_printf("Incomplete buf (len=%d)\n", linelen);
                        return 0;
                }
                if ( iscomment(*curbuf) ) {
@@ -83,18 +86,18 @@
 
        /* empty line */
        if ( parse_it == 0 ) {
-               dprintf("Empty line (len=%d)\n", linelen);
+               debug_printf("Empty line (len=%d)\n", linelen);
                return linelen;
        }
 
 #ifdef CMDLINE_DEBUG
-       snprintf(debug_buf, (linelen>BUFSIZ-n ? BUFSIZ-n : linelen), "%s", buf);
-       dprintf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
+       snprintf(debug_buf, (linelen>BUFSIZ ? BUFSIZ : linelen), "%s", buf);
+       debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
 #endif
 
        /* parse it !! */
        while (inst) {
-               dprintf("INST\n");
+               debug_printf("INST\n");
                inst = *inst_list;
                token_list = (struct token_hdr **) inst->tokens;
                token = *token_list;
@@ -102,7 +105,7 @@
 
                /* check if we match all tokens of inst */
                while (token) {
-                       dprintf("TK\n");
+                       debug_printf("TK\n");
                        /* skip spaces */
                        while (isblank(*curbuf)) {
                                curbuf++;
@@ -115,7 +118,7 @@
                        n = token->parse(token, curbuf, 
result_buf+token->offset);
                        if ( n < 0 )
                                break;
-                       dprintf("TK parsed (len=%d)\n", n);
+                       debug_printf("TK parsed (len=%d)\n", n);
                        curbuf += n;
 
                        token_list ++;
@@ -124,7 +127,7 @@
 
                /* fully parsed */
                if (!token) {
-                       dprintf("INST fully parsed\n");
+                       debug_printf("INST fully parsed\n");
                        /* skip spaces */
                        while (isblank(*curbuf)) {
                                curbuf++;
@@ -141,12 +144,12 @@
                                        /* return -1; */
                                        /* XXX */
                                        f=NULL;
-                                       dprintf("Ambiguous cmd\n");
+                                       debug_printf("Ambiguous cmd\n");
                                        break;
                                }
                        }
                        else {
-                               dprintf("Garbage after inst %x\n", *curbuf & 
0xff);
+                               debug_printf("Garbage after inst %x\n", *curbuf 
& 0xff);
                        }
                }
                        
@@ -163,7 +166,7 @@
        else {
                /* XXX */
                /* return mes couilles */
-               dprintf("No match\n");
+               debug_printf("No match\n");
        }
        
        return linelen;


==================================
aversive/modules/ihm/parse/parse.h  (1.1.2.1 -> 1.1.2.2)
==================================

@@ -7,10 +7,32 @@
 #define offsetof(type, field)  ((size_t) &( ((type *)0)->field) )
 #endif
 
+/**
+ * A token is defined by this structure.
+ *
+ * parse() takes the token as first argument, then the source buffer
+ * starting at the token we want to parse. The 3rd arg is a pointer
+ * where we store the parsed data (as binary). It returns the number of
+ * parsed chars on success and a negative value on error.
+ *
+ * complete_get_nb() returns the number of possible values for this
+ * token if completion is possible.
+ *
+ * complete_get_elt() XXX
+ *
+ * help_str is the string that is displayed when we ask for help for
+ * this token.
+ *
+ * offset stores the place to write the parsed result in the
+ * destination structure.
+ */
 struct token_hdr {
        /* parse(token ptr, buf, res pts) */
        int8_t (*parse)(void *, char *, void *);
-       int8_t (*complete)(void *, char *, char *, uint8_t);
+       /* return the num of possible choices for this token */
+       int8_t (*complete_get_nb)(void *);
+       /* return the elt x for this token */
+       int8_t (*complete_get_elt)(void *, int8_t, char *, uint8_t);
        char * help_str;
        uint8_t offset;
 };


======================================
aversive/modules/ihm/parse/parse_num.h  (1.1.2.1 -> 1.1.2.2)
======================================

@@ -24,7 +24,8 @@
 {                                                         \
        {                                                  \
                .parse = parse_num,                        \
-               .complete = NULL,                          \
+               .complete_get_nb = NULL,                   \
+               .complete_get_elt = NULL,                  \
                .help_str = NULL,                          \
                .offset = offsetof(structure, field),      \
        },                                                 \


=========================================
aversive/modules/ihm/parse/parse_string.c  (1.1.2.1 -> 1.1.2.2)
=========================================

@@ -6,47 +6,78 @@
 #include "parse.h"
 #include "parse_string.h"
 
+static uint8_t
+get_token_len(const char * s)
+{
+        const char *s2=s;
+        while (*(s2)!='#' && *(s2)!='\0')
+                s2++;
+        return s2-s;
+}
+
+static const char *
+get_next_token(const char * s)
+{
+        while ( (*s != '#') && (*s != '\0') )
+                s++;
+       if (*s == '#')
+               return s+1;
+       return NULL;
+}
+
+
+
 int8_t 
 parse_string(void * data, char * buf, void * res)
 {
        struct token_string * token = data;
        uint8_t token_len;
+       const char * str;
 
        if (! *buf)
                return -1;
 
-       /* fixed string */
-       if (token->str) {
-               token_len = strlen(token->str);
-               
-               /* if token is too big... */
-               if (token_len >= STR_TOKEN_SIZE - 1) {
-                       return -1;
-               }
-               
-               if ( strncmp(buf, token->str, token_len) ) {
-                       return -1;
-               }
+        /* fixed string */
+        if (token->str) {
+               str = token->str;
+                do {
+                        token_len = get_token_len(token->str);
+                        
+                        /* if token is too big... */
+                        if (token_len >= STR_TOKEN_SIZE - 1) {
+                               continue;
+                        }
+                        
+                        if ( strncmp(buf, str, token_len) ) {
+                               continue;
+                        }
+                        
+                        if ( !isendoftoken(*(buf+token_len)) ) {
+                               continue;
+                        }
 
-               if ( !isendoftoken(*(buf+token_len)) )
-                       return -1;
-       }
-       /* unspecified string */
-       else {
-               token_len=0;
-               while(!isendoftoken(buf[token_len]) && 
-                     token_len < (STR_TOKEN_SIZE-1))
-                       token_len++;
+                        break;
+                } while ( (str = get_next_token(str)) != NULL );
 
-               /* return if token too long */
-               if (token_len >= STR_TOKEN_SIZE - 1) {
+               if (!str)
                        return -1;
-               }
-       }
-       
-       /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
-       strncpy(res, buf, token_len);
-       *((char *)res + token_len) = 0;
+        }
+        /* unspecified string */
+        else {
+                token_len=0;
+                while(!isendoftoken(buf[token_len]) && 
+                      token_len < (STR_TOKEN_SIZE-1))
+                        token_len++;
+
+                /* return if token too long */
+                if (token_len >= STR_TOKEN_SIZE - 1) {
+                        return -1;
+                }
+        }
+        
+        /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
+        strncpy(res, buf, token_len);
+        *((char *)res + token_len) = 0;
 
-       return token_len;
+        return token_len;
 }


=========================================
aversive/modules/ihm/parse/parse_string.h  (1.1.2.1 -> 1.1.2.2)
=========================================

@@ -18,7 +18,8 @@
 {                                                           \
        {                                                    \
                .parse = parse_string,                       \
-               .complete = NULL,                            \
+               .complete_get_nb = NULL,                     \
+               .complete_get_elt = NULL,                    \
                .help_str = NULL,                            \
                .offset = offsetof(structure, field),        \
        },                                                   \


=======================================
aversive/modules/ihm/parse/test/.config  (1.1.2.1)
=======================================

@@ -0,0 +1,217 @@
+#
+# Automatically generated by make menuconfig: don't edit
+#
+
+#
+# Hardware
+#
+# CONFIG_MCU_AT90S2313 is not set
+# CONFIG_MCU_AT90S2323 is not set
+# CONFIG_MCU_AT90S3333 is not set
+# CONFIG_MCU_AT90S2343 is not set
+# CONFIG_MCU_ATTINY22 is not set
+# CONFIG_MCU_ATTINY26 is not set
+# CONFIG_MCU_AT90S4414 is not set
+# CONFIG_MCU_AT90S4433 is not set
+# CONFIG_MCU_AT90S4434 is not set
+# CONFIG_MCU_AT90S8515 is not set
+# CONFIG_MCU_AT90S8534 is not set
+# CONFIG_MCU_AT90S8535 is not set
+# CONFIG_MCU_AT86RF401 is not set
+# CONFIG_MCU_ATMEGA103 is not set
+# CONFIG_MCU_ATMEGA603 is not set
+# CONFIG_MCU_AT43USB320 is not set
+# CONFIG_MCU_AT43USB355 is not set
+# CONFIG_MCU_AT76C711 is not set
+# CONFIG_MCU_ATMEGA8 is not set
+# CONFIG_MCU_ATMEGA48 is not set
+# CONFIG_MCU_ATMEGA88 is not set
+# CONFIG_MCU_ATMEGA8515 is not set
+# CONFIG_MCU_ATMEGA8535 is not set
+# CONFIG_MCU_ATTINY13 is not set
+# CONFIG_MCU_ATTINY2313 is not set
+# CONFIG_MCU_ATMEGA16 is not set
+# CONFIG_MCU_ATMEGA161 is not set
+# CONFIG_MCU_ATMEGA162 is not set
+# CONFIG_MCU_ATMEGA163 is not set
+# CONFIG_MCU_ATMEGA165 is not set
+# CONFIG_MCU_ATMEGA168 is not set
+# CONFIG_MCU_ATMEGA169 is not set
+# CONFIG_MCU_ATMEGA32 is not set
+# CONFIG_MCU_ATMEGA323 is not set
+# CONFIG_MCU_ATMEGA325 is not set
+# CONFIG_MCU_ATMEGA3250 is not set
+# CONFIG_MCU_ATMEGA64 is not set
+# CONFIG_MCU_ATMEGA645 is not set
+# CONFIG_MCU_ATMEGA6450 is not set
+CONFIG_MCU_ATMEGA128=y
+# CONFIG_MCU_AT90CAN128 is not set
+# CONFIG_MCU_AT94K is not set
+# CONFIG_MCU_AT90S1200 is not set
+CONFIG_QUARTZ=16000000
+
+#
+# Generation options
+#
+CONFIG_OPTM_0=y
+# CONFIG_OPTM_1 is not set
+# CONFIG_OPTM_2 is not set
+# CONFIG_OPTM_3 is not set
+# CONFIG_OPTM_S is not set
+CONFIG_MATH_LIB=y
+# CONFIG_FDEVOPEN_COMPAT is not set
+# CONFIG_MINIMAL_PRINTF is not set
+CONFIG_STANDARD_PRINTF=y
+# CONFIG_ADVANCED_PRINTF is not set
+CONFIG_FORMAT_IHEX=y
+# CONFIG_FORMAT_SREC is not set
+# CONFIG_FORMAT_BINARY is not set
+
+#
+# Base modules
+#
+CONFIG_MODULE_CIRBUF=y
+# CONFIG_MODULE_CIRBUF_LARGE is not set
+# CONFIG_MODULE_FIXED_POINT is not set
+# CONFIG_MODULE_VECT2 is not set
+# CONFIG_MODULE_SCHEDULER is not set
+# CONFIG_MODULE_SCHEDULER_CREATE_CONFIG is not set
+# CONFIG_MODULE_SCHEDULER_USE_TIMERS is not set
+# CONFIG_MODULE_TIME is not set
+# CONFIG_MODULE_TIME_CREATE_CONFIG is not set
+
+#
+# Communication modules
+#
+CONFIG_MODULE_UART=y
+CONFIG_MODULE_UART_CREATE_CONFIG=y
+# CONFIG_MODULE_I2C is not set
+# CONFIG_MODULE_I2C_MASTER is not set
+# CONFIG_MODULE_I2C_MULTIMASTER is not set
+# CONFIG_MODULE_I2C_CREATE_CONFIG is not set
+# CONFIG_MODULE_MF2_CLIENT is not set
+# CONFIG_MODULE_MF2_CLIENT_USE_SCHEDULER is not set
+# CONFIG_MODULE_MF2_CLIENT_CREATE_CONFIG is not set
+# CONFIG_MODULE_MF2_SERVER is not set
+# CONFIG_MODULE_MF2_SERVER_CREATE_CONFIG is not set
+
+#
+# Hardware modules
+#
+# CONFIG_MODULE_TIMER is not set
+# CONFIG_MODULE_TIMER_CREATE_CONFIG is not set
+# CONFIG_MODULE_TIMER_DYNAMIC is not set
+# CONFIG_MODULE_PWM is not set
+# CONFIG_MODULE_PWM_CREATE_CONFIG is not set
+# CONFIG_MODULE_ADC is not set
+# CONFIG_MODULE_ADC_CREATE_CONFIG is not set
+
+#
+# IHM modules
+#
+# CONFIG_MODULE_MENU is not set
+CONFIG_MODULE_RDLINE=y
+CONFIG_MODULE_RDLINE_CREATE_CONFIG=y
+CONFIG_MODULE_RDLINE_KILL_BUF=y
+CONFIG_MODULE_RDLINE_HISTORY=y
+CONFIG_MODULE_PARSE=y
+
+#
+# External devices modules
+#
+# CONFIG_MODULE_LCD is not set
+# CONFIG_MODULE_LCD_CREATE_CONFIG is not set
+# CONFIG_MODULE_MULTISERVO is not set
+# CONFIG_MODULE_MULTISERVO_CREATE_CONFIG is not set
+
+#
+# Brushless motor drivers (you should enable pwm modules to see all)
+#
+# CONFIG_MODULE_BRUSHLESS_3PHASE_DIGITAL_HALL is not set
+# CONFIG_MODULE_BRUSHLESS_3PHASE_DIGITAL_HALL_CREATE_CONFIG is not set
+# CONFIG_MODULE_BRUSHLESS_3PHASE_DIGITAL_HALL_DOUBLE is not set
+# CONFIG_MODULE_BRUSHLESS_3PHASE_DIGITAL_HALL_DOUBLE_CREATE_CONFIG is not set
+
+#
+# Encoders
+#
+# CONFIG_MODULE_ENCODERS_MICROB is not set
+# CONFIG_MODULE_ENCODERS_MICROB_CREATE_CONFIG is not set
+# CONFIG_MODULE_ENCODERS_EIRBOT is not set
+# CONFIG_MODULE_ENCODERS_EIRBOT_CREATE_CONFIG is not set
+
+#
+# Robot specific modules
+#
+# CONFIG_MODULE_ROBOT_SYSTEM is not set
+# CONFIG_MODULE_POSITION_MANAGER is not set
+# CONFIG_MODULE_TRAJECTORY_MANAGER is not set
+
+#
+# Control system modules
+#
+# CONFIG_MODULE_CONTROL_SYSTEM_MANAGER is not set
+# CONFIG_MODULE_PID is not set
+# CONFIG_MODULE_RAMP is not set
+# CONFIG_MODULE_QUADRAMP is not set
+# CONFIG_MODULE_QUADRAMP_DERIVATE is not set
+# CONFIG_MODULE_BIQUAD is not set
+
+#
+# Crypto modules
+#
+# CONFIG_MODULE_AES is not set
+# CONFIG_MODULE_AES_CTR is not set
+# CONFIG_MODULE_MD5 is not set
+# CONFIG_MODULE_MD5_HMAC is not set
+# CONFIG_MODULE_RC4 is not set
+
+#
+# Encodings modules
+#
+# CONFIG_MODULE_BASE64 is not set
+# CONFIG_MODULE_HAMMING is not set
+
+#
+# Debug modules
+#
+# CONFIG_MODULE_DIAGNOSTIC is not set
+# CONFIG_MODULE_DIAGNOSTIC_CREATE_CONFIG is not set
+CONFIG_MODULE_ERROR=y
+CONFIG_MODULE_ERROR_CREATE_CONFIG=y
+
+#
+# Programmer options
+#
+CONFIG_AVRDUDE=y
+# CONFIG_AVARICE is not set
+
+#
+# Avrdude
+#
+# CONFIG_AVRDUDE_PROG_FUTURELEC is not set
+# CONFIG_AVRDUDE_PROG_ABCMINI is not set
+# CONFIG_AVRDUDE_PROG_PICOWEB is not set
+# CONFIG_AVRDUDE_PROG_SP12 is not set
+# CONFIG_AVRDUDE_PROG_ALF is not set
+# CONFIG_AVRDUDE_PROG_BASCOM is not set
+# CONFIG_AVRDUDE_PROG_DT006 is not set
+# CONFIG_AVRDUDE_PROG_PONY_STK200 is not set
+CONFIG_AVRDUDE_PROG_STK200=y
+# CONFIG_AVRDUDE_PROG_PAVR is not set
+# CONFIG_AVRDUDE_PROG_BUTTERFLY is not set
+# CONFIG_AVRDUDE_PROG_AVR910 is not set
+# CONFIG_AVRDUDE_PROG_STK500 is not set
+# CONFIG_AVRDUDE_PROG_AVRISP is not set
+# CONFIG_AVRDUDE_PROG_BSD is not set
+# CONFIG_AVRDUDE_PROG_DAPA is not set
+# CONFIG_AVRDUDE_PROG_JTAG1 is not set
+CONFIG_AVRDUDE_PORT="/dev/parport0"
+
+#
+# Avarice
+#
+CONFIG_AVARICE_PORT="/dev/ttyS0"
+CONFIG_AVARICE_DEBUG_PORT=1234
+CONFIG_AVARICE_PROG_MKI=y
+# CONFIG_AVARICE_PROG_MKII is not set


======================================
aversive/modules/ihm/parse/test/main.c  (1.1.2.1 -> 1.1.2.2)
======================================

@@ -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: main.c,v 1.1.2.1 2007-09-17 20:53:28 zer0 Exp $
+ *  Revision : $Id: main.c,v 1.1.2.2 2007-09-22 14:27:41 zer0 Exp $
  *
  *
  */
@@ -46,7 +46,9 @@
 
 #include <aversive/wait.h>
 #include <uart.h>
-#include "rdline.h"
+#include <rdline.h>
+#include <parse.h>
+#include <parse_string.h>
 
 /* globals */
 struct rdline rdl;
@@ -161,6 +163,10 @@
 #endif
 }
 
+struct cmd_result {
+       fixed_string_t arg0;
+} result;
+struct token_string tok = TOKEN_STRING_INITIALIZER(struct cmd_result, arg0, 
"test1#test2#dd");
 
 
 int main(void)
@@ -171,6 +177,8 @@
        int n, i;
        int8_t ret;
 #endif
+       int err;
+
 #ifdef DEBUG_SOCKET
        struct sockaddr_in sin_ci;
 
@@ -205,12 +213,27 @@
        fdevopen(uart0_dev_send, uart0_dev_recv);
 
        wait_ms(5000);
-       printf("Start\n");
        uart0_register_rx_event(rx);
        
        sei();
 #endif
+       printf("Start\n");
 
+       err = parse_string(&tok, "test1", &result);
+       if (err >= 0)
+               printf("Match : <%s> (consumed %d)\n", result.arg0, err);
+
+       err = parse_string(&tok, "test2", &result);
+       if (err >= 0)
+               printf("Match : <%s> (consumed %d)\n", result.arg0, err);
+
+       err = parse_string(&tok, "test3", &result);
+       if (err >= 0)
+               printf("Match : <%s> (consumed %d)\n", result.arg0, err);
+
+       err = parse_string(&tok, "test", &result);
+       if (err >= 0)
+               printf("Match : <%s> (consumed %d)\n", result.arg0, err);
 
        /* common init */
        rdline_init(&rdl, write_char, display_buffer, complete_buffer);


===============================================
aversive/modules/ihm/parse/test/rdline_config.h  (1.1.2.1)
===============================================



Commit from zer0 on branch b_zer0 (2007-09-22 20:15 CEST)
=================================

prepare completion

  aversive  modules/ihm/parse/parse.c         1.1.2.3
  aversive  modules/ihm/parse/parse.h         1.1.2.3
  aversive  modules/ihm/parse/parse_num.c     1.1.2.2
  aversive  modules/ihm/parse/parse_num.h     1.1.2.3
  aversive  modules/ihm/parse/parse_string.c  1.1.2.3
  aversive  modules/ihm/parse/parse_string.h  1.1.2.3
  aversive  modules/ihm/parse/test/.config    1.1.2.2
  aversive  modules/ihm/parse/test/Makefile   1.1.2.2
  aversive  modules/ihm/parse/test/main.c     1.1.2.3


==================================
aversive/modules/ihm/parse/parse.c  (1.1.2.2 -> 1.1.2.3)
==================================

@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <string.h>
 #include <inttypes.h>
 #include <ctype.h>
 
@@ -6,7 +7,7 @@
 
 /* XXX */
 #ifndef BUFSIZ
-#define BUFSIZ 128
+#define BUFSIZ 64
 #endif
 
 #define CMDLINE_DEBUG
@@ -38,24 +39,71 @@
 }
 
 
+/** 
+ * try to match the buffer with an instruction (only the first
+ * nb_match_token if != 0). Return 0 on success or -1.
+ */
+static int8_t
+match_inst(struct inst * inst, const char * buf, uint8_t nb_match_token, 
+          void * result_buf)
+{
+       struct token_hdr ** token_list;
+       struct token_hdr * token;
+       uint8_t i=0;
+       int8_t n = 0;
+
+       token_list = (struct token_hdr **) inst->tokens;
+       token = *token_list;
+       
+       /* check if we match all tokens of inst */
+       while (token && (!nb_match_token || i<nb_match_token)) {
+               debug_printf("TK\n");
+               /* skip spaces */
+               while (isblank(*buf)) {
+                       buf++;
+               }
+               
+               /* end of buf */
+               if ( isendofline(*buf) || iscomment(*buf) )
+                       break;
+               
+               n = token->parse(token, buf, (result_buf ? 
result_buf+token->offset : NULL));
+               if ( n < 0 )
+                       break;
+               debug_printf("TK parsed (len=%d)\n", n);
+               i++;
+               buf += n;
+               
+               token_list ++;
+               token = *token_list;
+       }
+       
+       /* match the first token */
+       if (nb_match_token && (i == nb_match_token))
+               return 0;
+
+       /* does not match */
+       if (token)
+               return -1;
+       
+       /* match all tokens */
+       return 0;
+}
 
 int8_t
-parse_buf(struct ctx * ctx, char * buf)
+parse(struct ctx * ctx, const char * buf)
 {
        struct inst ** inst_list = (struct inst **) ctx->insts;
        struct inst * inst = *inst_list;
-       struct token_hdr ** token_list;
-       struct token_hdr * token;
-       char *curbuf;
-       char result_buf[BUFSIZ]; /* XXX align, size */
+       const char * curbuf;
+       char result_buf[64]; /* XXX align, size */
        void (*f)(void *, void *) = NULL;
        void * data = NULL;
-       int8_t n = 0;
        int comment = 0;
        int linelen = 0;
        int parse_it = 0;
 #ifdef CMDLINE_DEBUG
-       char debug_buf[BUFSIZ];
+       char debug_buf[64];
 #endif
 
        /* 
@@ -91,7 +139,7 @@
        }
 
 #ifdef CMDLINE_DEBUG
-       snprintf(debug_buf, (linelen>BUFSIZ ? BUFSIZ : linelen), "%s", buf);
+       snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
        debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
 #endif
 
@@ -99,34 +147,9 @@
        while (inst) {
                debug_printf("INST\n");
                inst = *inst_list;
-               token_list = (struct token_hdr **) inst->tokens;
-               token = *token_list;
-               curbuf = buf;
-
-               /* check if we match all tokens of inst */
-               while (token) {
-                       debug_printf("TK\n");
-                       /* skip spaces */
-                       while (isblank(*curbuf)) {
-                               curbuf++;
-                       }
-                       
-                       /* end of buf */
-                       if ( isendofline(*curbuf) || iscomment(*curbuf) )
-                               break;
-
-                       n = token->parse(token, curbuf, 
result_buf+token->offset);
-                       if ( n < 0 )
-                               break;
-                       debug_printf("TK parsed (len=%d)\n", n);
-                       curbuf += n;
-
-                       token_list ++;
-                       token = *token_list;
-               }
 
                /* fully parsed */
-               if (!token) {
+               if (! match_inst(inst, buf, 0, result_buf)) {
                        debug_printf("INST fully parsed\n");
                        /* skip spaces */
                        while (isblank(*curbuf)) {
@@ -171,3 +194,92 @@
        
        return linelen;
 }
+
+int8_t 
+complete(struct ctx * ctx, const char *buf, uint16_t *state, 
+        char *dst, uint8_t size)
+{
+       const char * incomplete_token = NULL;
+       struct inst ** inst_list = (struct inst **) ctx->insts;
+       struct inst * inst = *inst_list;
+       struct token_hdr * token;
+       char tmpbuf[64];
+       int8_t nb_token = -1;
+       uint8_t buf_len = strlen(buf);
+       uint8_t i;
+       uint8_t total_choices, nb_choice;
+
+       printf("try complete <%s>\n", buf);
+       /* count the number of complete token to parse */
+       for (i=0 ; i<buf_len ; i++) {
+               if (nb_token == -1) {
+                       if (!isblank(buf[i])) {
+                               nb_token = 0;
+                       }
+               }
+               else {
+                       if (isblank(buf[i])) {
+                               incomplete_token = buf+i+1;
+                               if (!isblank(buf[i-1])) {
+                                       nb_token++;
+                               }
+                       }
+               }
+       }
+
+       if (! incomplete_token)
+               return -1;
+       printf("%d %s\n", nb_token, incomplete_token);
+
+       /* first call -> do a first pass */
+       if (*state == 0) {
+               total_choices = 0;
+
+               /* parse the first tokens */
+               while (inst) {
+                       inst = *inst_list;
+
+                       /* fully parsed */
+                       if (match_inst(inst, buf, nb_token, NULL))
+                               goto next;
+                       
+                       printf("match \n");
+                       token = inst->tokens[nb_token];
+                       if (!token)
+                               goto next;
+                       if (!token->complete_get_nb || !token->complete_get_elt)
+                               goto next;
+
+                       nb_choice = token->complete_get_nb(token);
+                       printf("%d choices\n", nb_choice);
+                       for (i=0 ; i<nb_choice ; i++) {
+                               if (token->complete_get_elt(token, i, tmpbuf, 
sizeof(tmpbuf)) < 0)
+                                       continue;
+                               printf("choice <%s>\n", tmpbuf);
+                               if (!strncmp(incomplete_token, tmpbuf, 
+                                            strlen(incomplete_token))) {
+                                       total_choices++;
+                               }
+                       }               
+               next:
+                       inst_list ++;
+                       inst = *inst_list;
+               }
+
+               printf("total choices %d\n", total_choices);
+       }
+
+       if (total_choices == 0)
+               return 0;
+       
+       if (total_choices == 1) {
+               if (strlen(tmpbuf+strlen(incomplete_token)) > size)
+                       return 0;
+               
+               strcpy(dst, tmpbuf+strlen(incomplete_token));
+               return 1;
+       }
+
+       return 0;
+
+}


==================================
aversive/modules/ihm/parse/parse.h  (1.1.2.2 -> 1.1.2.3)
==================================

@@ -28,10 +28,10 @@
  */
 struct token_hdr {
        /* parse(token ptr, buf, res pts) */
-       int8_t (*parse)(void *, char *, void *);
+       int8_t (*parse)(void *, const char *, void *);
        /* return the num of possible choices for this token */
        int8_t (*complete_get_nb)(void *);
-       /* return the elt x for this token */
+       /* return the elt x for this token (token, idx, dstbuf, size) */
        int8_t (*complete_get_elt)(void *, int8_t, char *, uint8_t);
        char * help_str;
        uint8_t offset;
@@ -51,7 +51,9 @@
 };
 
 
-int8_t parse_buf(struct ctx * ctx, char * buf);
+int8_t parse(struct ctx * ctx, const char * buf);
+int8_t complete(struct ctx * ctx, const char *buf, uint16_t *state, 
+               char *dst, uint8_t size);
 int isendoftoken(char c);
 
 #endif /* _PARSE_H_ */


======================================
aversive/modules/ihm/parse/parse_num.c  (1.1.2.1 -> 1.1.2.2)
======================================

@@ -62,7 +62,7 @@
 
 /* parse an int or a float */
 int8_t 
-parse_num(void * data, char * srcbuf, void * res)
+parse_num(void * data, const char * srcbuf, void * res)
 {
        struct token_num * token = data;
        enum num_parse_state_t st = START;
@@ -278,30 +278,37 @@
        case OCTAL_OK:
        case BIN_OK:
                if ( type == INT8 && res1 <= S08_MAX ) {
-                       *(int8_t *)res = (int8_t) res1;
+                       if (res)
+                               *(int8_t *)res = (int8_t) res1;
                        return (buf-srcbuf);
                }
                else if ( type == INT16 && res1 <= S16_MAX ) {
-                       *(int16_t *)res = (int16_t) res1;
+                       if (res)
+                               *(int16_t *)res = (int16_t) res1;
                        return (buf-srcbuf);
                }
                else if ( type == INT32 && res1 <= S32_MAX ) {
-                       *(int32_t *)res = (int32_t) res1;
+                       if (res)
+                               *(int32_t *)res = (int32_t) res1;
                }
                else if ( type == UINT8 && res1 <= U08_MAX ) {
-                       *(uint8_t *)res = (uint8_t) res1;
+                       if (res)
+                               *(uint8_t *)res = (uint8_t) res1;
                        return (buf-srcbuf);
                }
                else if (type == UINT16  && res1 <= U16_MAX ) {
-                       *(uint16_t *)res = (uint16_t) res1;
+                       if (res)
+                               *(uint16_t *)res = (uint16_t) res1;
                        return (buf-srcbuf);
                }
                else if ( type == UINT32 ) {
-                       *(uint32_t *)res = (uint32_t) res1;
+                       if (res)
+                               *(uint32_t *)res = (uint32_t) res1;
                        return (buf-srcbuf);
                }
                else if ( type == FLOAT ) {
-                       *(float *)res = (float)res1;
+                       if (res)
+                               *(float *)res = (float)res1;
                        return (buf-srcbuf);
                }
                else {
@@ -311,19 +318,23 @@
 
        case DEC_NEG_OK:
                if ( type == INT8 && res1 <= S08_MAX + 1 ) {
-                       *(int8_t *)res = (int8_t) res1;
+                       if (res)
+                               *(int8_t *)res = (int8_t) res1;
                        return (buf-srcbuf);
                }
                else if ( type == INT16 && res1 <= S16_MAX + 1 ) {
-                       *(int16_t *)res = (int16_t) res1;
+                       if (res)
+                               *(int16_t *)res = (int16_t) res1;
                        return (buf-srcbuf);
                }
                else if ( type == INT32 && res1 <= (uint32_t)S32_MAX + 1 ) {
-                       *(int32_t *)res = (int32_t) res1;
+                       if (res)
+                               *(int32_t *)res = (int32_t) res1;
                        return (buf-srcbuf);
                }
                else if ( type == FLOAT ) {
-                       *(float *)res = - (float)res1;
+                       if (res)
+                               *(float *)res = - (float)res1;
                        return (buf-srcbuf);
                }
                else {
@@ -334,7 +345,8 @@
        case FLOAT_POS:
        case FLOAT_POS_OK:
                if ( type == FLOAT ) {
-                       *(float *)res = (float)res1 + ((float)res2 / 
(float)res3);
+                       if (res)
+                               *(float *)res = (float)res1 + ((float)res2 / 
(float)res3);
                        return (buf-srcbuf);
                        
                }
@@ -346,7 +358,8 @@
        case FLOAT_NEG:
        case FLOAT_NEG_OK:
                if ( type == FLOAT ) {
-                       *(float *)res = - ((float)res1 + ((float)res2 / 
(float)res3));
+                       if (res)
+                               *(float *)res = - ((float)res1 + ((float)res2 / 
(float)res3));
                        return (buf-srcbuf);
                        
                }


======================================
aversive/modules/ihm/parse/parse_num.h  (1.1.2.2 -> 1.1.2.3)
======================================

@@ -18,7 +18,7 @@
        enum numtype type;
 };
 
-int8_t parse_num(void * token_ptr, char * srcbuf, void * res);
+int8_t parse_num(void * token_ptr, const char * srcbuf, void * res);
 
 #define TOKEN_NUM_INITIALIZER(structure, field, numtype)   \
 {                                                         \


=========================================
aversive/modules/ihm/parse/parse_string.c  (1.1.2.2 -> 1.1.2.3)
=========================================

@@ -18,17 +18,14 @@
 static const char *
 get_next_token(const char * s)
 {
-        while ( (*s != '#') && (*s != '\0') )
-                s++;
-       if (*s == '#')
+       s = strchr(s, '#');
+       if (s)
                return s+1;
        return NULL;
 }
 
-
-
 int8_t 
-parse_string(void * data, char * buf, void * res)
+parse_string(void * data, const char * buf, void * res)
 {
        struct token_string * token = data;
        uint8_t token_len;
@@ -75,9 +72,47 @@
                 }
         }
         
-        /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
-        strncpy(res, buf, token_len);
-        *((char *)res + token_len) = 0;
+       if (res) {
+               /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
+               strncpy(res, buf, token_len);
+               *((char *)res + token_len) = 0;
+       }
 
         return token_len;
 }
+
+int8_t complete_get_nb_string(void * data)
+{
+       struct token_string * token = data;
+       const char * str = token->str;
+       int8_t ret=1;
+
+       while( (str = strchr(str, '#')) != NULL ) {
+               str++;
+               ret++;
+       }
+       return ret;
+}
+
+int8_t complete_get_elt_string(void * data, int8_t idx, 
+                             char * dstbuf, uint8_t size)
+{
+       struct token_string * token = data;
+       const char * s = token->str;
+       uint8_t len;
+
+       while (idx-- && s)
+               s = get_next_token(s);
+
+       if (!s)
+               return -1;
+
+       len = get_token_len(s);
+       if (len > size)
+               return -1;
+
+       memcpy(dstbuf, s, len);
+       dstbuf[len] = '\0';
+
+       return 0;
+}


=========================================
aversive/modules/ihm/parse/parse_string.h  (1.1.2.2 -> 1.1.2.3)
=========================================

@@ -12,14 +12,17 @@
        const char * str;
 };
 
-int8_t parse_string(void * token_ptr, char * srcbuf, void * res);
+int8_t parse_string(void * token_ptr, const char * srcbuf, void * res);
+int8_t complete_get_nb_string(void * token_ptr);
+int8_t complete_get_elt_string(void * token_ptr, int8_t idx, 
+                              char * dstbuf, uint8_t size);
 
 #define TOKEN_STRING_INITIALIZER(structure, field, string)   \
 {                                                           \
        {                                                    \
                .parse = parse_string,                       \
-               .complete_get_nb = NULL,                     \
-               .complete_get_elt = NULL,                    \
+               .complete_get_nb = complete_get_nb_string,   \
+               .complete_get_elt = complete_get_elt_string, \
                .help_str = NULL,                            \
                .offset = offsetof(structure, field),        \
        },                                                   \


=======================================
aversive/modules/ihm/parse/test/.config  (1.1.2.1 -> 1.1.2.2)
=======================================

@@ -1,5 +1,5 @@
 #
-# Automatically generated by make menuconfig: don't edit
+# Automatically generated make config: don't edit
 #
 
 #
@@ -70,6 +70,10 @@
 #
 # Base modules
 #
+
+#
+# Enable math library in generation options to see all modules
+#
 CONFIG_MODULE_CIRBUF=y
 # CONFIG_MODULE_CIRBUF_LARGE is not set
 # CONFIG_MODULE_FIXED_POINT is not set
@@ -83,6 +87,10 @@
 #
 # Communication modules
 #
+
+#
+# uart needs circular buffer, mf2 client may need scheduler
+#
 CONFIG_MODULE_UART=y
 CONFIG_MODULE_UART_CREATE_CONFIG=y
 # CONFIG_MODULE_I2C is not set
@@ -151,6 +159,10 @@
 # Control system modules
 #
 # CONFIG_MODULE_CONTROL_SYSTEM_MANAGER is not set
+
+#
+# Filters
+#
 # CONFIG_MODULE_PID is not set
 # CONFIG_MODULE_RAMP is not set
 # CONFIG_MODULE_QUADRAMP is not set
@@ -160,6 +172,10 @@
 #
 # Crypto modules
 #
+
+#
+# Crypto modules depend on utils module
+#
 # CONFIG_MODULE_AES is not set
 # CONFIG_MODULE_AES_CTR is not set
 # CONFIG_MODULE_MD5 is not set
@@ -169,12 +185,20 @@
 #
 # Encodings modules
 #
+
+#
+# Encoding modules depend on utils module
+#
 # CONFIG_MODULE_BASE64 is not set
 # CONFIG_MODULE_HAMMING is not set
 
 #
 # Debug modules
 #
+
+#
+# Debug modules depend on utils module
+#
 # CONFIG_MODULE_DIAGNOSTIC is not set
 # CONFIG_MODULE_DIAGNOSTIC_CREATE_CONFIG is not set
 CONFIG_MODULE_ERROR=y


========================================
aversive/modules/ihm/parse/test/Makefile  (1.1.2.1 -> 1.1.2.2)
========================================

@@ -4,7 +4,7 @@
 AVERSIVE_DIR = ../../../..
 
 # List C source files here. (C dependencies are automatically generated.)
-SRC = $(TARGET).c
+SRC = $(TARGET).c commands.c
 
 # List Assembler source files here.
 # Make them always end in a capital .S.  Files ending in a lowercase .s


======================================
aversive/modules/ihm/parse/test/main.c  (1.1.2.2 -> 1.1.2.3)
======================================

@@ -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: main.c,v 1.1.2.2 2007-09-22 14:27:41 zer0 Exp $
+ *  Revision : $Id: main.c,v 1.1.2.3 2007-09-22 18:15:55 zer0 Exp $
  *
  *
  */
@@ -50,6 +50,8 @@
 #include <parse.h>
 #include <parse_string.h>
 
+extern struct ctx main_ctx;
+
 /* globals */
 struct rdline rdl;
 char prompt[RDLINE_PROMPT_SIZE];
@@ -173,11 +175,12 @@
 {
 #ifdef HOST_VERSION
        struct termios oldterm, term;
-       char buf[BUFSIZ];
+       char buf[64];
        int n, i;
        int8_t ret;
 #endif
        int err;
+       uint16_t state=0;
 
 #ifdef DEBUG_SOCKET
        struct sockaddr_in sin_ci;
@@ -202,12 +205,15 @@
        }
 #endif /* DEBUG_SOCKET */
 
+
 #ifdef HOST_VERSION
+#if 0
        tcgetattr(0, &oldterm);
        memcpy(&term, &oldterm, sizeof(term));
        term.c_lflag &= ~(ICANON | ECHO | ISIG);
        tcsetattr(0, TCSANOW, &term);
        setbuf(stdin, NULL);
+#endif
 #else
        uart_init();
        fdevopen(uart0_dev_send, uart0_dev_recv);
@@ -235,6 +241,55 @@
        if (err >= 0)
                printf("Match : <%s> (consumed %d)\n", result.arg0, err);
 
+       printf("complete nb = %d\n", complete_get_nb_string(&tok));
+
+       complete_get_elt_string(&tok, 0, buf, sizeof(buf));
+       printf("complete 0 : <%s>\n", buf);
+       complete_get_elt_string(&tok, 1, buf, sizeof(buf));
+       printf("complete 1 : <%s>\n", buf);
+       complete_get_elt_string(&tok, 2, buf, sizeof(buf));
+       printf("complete 2 : <%s>\n", buf);
+       if (complete_get_elt_string(&tok, 3, buf, sizeof(buf)) != -1)
+               printf("complete 3 ????? : <%s>\n", buf);
+
+       state=0;
+       while ( (err = complete(&main_ctx, "cmd1 mo", &state, buf, 
sizeof(buf))) == 1) {
+               printf("err = %d, buf = <%s>\n", err, buf);
+       }
+       if (err == 2)
+               printf("err = %d, buf = <%s>\n", err, buf);
+       
+       state=0;
+       while ( (err = complete(&main_ctx, "cmd1 p", &state, buf, sizeof(buf))) 
== 1) {
+               printf("err = %d, buf = <%s>\n", err, buf);
+       }
+       if (err == 2)
+               printf("err = %d, buf = <%s>\n", err, buf);
+       
+       state=0;
+       while ( (err = complete(&main_ctx, "cmd1 ", &state, buf, sizeof(buf))) 
== 1) {
+               printf("err = %d, buf = <%s>\n", err, buf);
+       }
+       if (err == 2)
+               printf("err = %d, buf = <%s>\n", err, buf);
+       
+       state=0;
+       while ( (err = complete(&main_ctx, "toto", &state, buf, sizeof(buf))) 
== 1) {
+               printf("err = %d, buf = <%s>\n", err, buf);
+       }
+       if (err == 2)
+               printf("err = %d, buf = <%s>\n", err, buf);
+       
+       state=0;
+       while ( (err = complete(&main_ctx, "csdcds", &state, buf, sizeof(buf))) 
== 1) {
+               printf("err = %d, buf = <%s>\n", err, buf);
+       }
+       if (err == 2)
+               printf("err = %d, buf = <%s>\n", err, buf);
+       
+
+       return 0;
+
        /* common init */
        rdline_init(&rdl, write_char, display_buffer, complete_buffer);
        snprintf(prompt, sizeof(prompt), "toto[%d] > ", cpt++); 
@@ -243,7 +298,7 @@
 
        /* loop to send chars on host */
 #ifdef HOST_VERSION
-       while ((n=read(0, buf, BUFSIZ-1)) > 0) {
+       while ((n=read(0, buf, sizeof(buf)-1)) > 0) {
                buf[n] = 0;
 
                for (i=0 ; i<n ; i++) {

_______________________________________________
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

Répondre à