00001 #include <stdio.h>
00002 #include <inttypes.h>
00003 #include <ctype.h>
00004 #include <string.h>
00005
00006 #include "parse.h"
00007 #include "parse_string.h"
00008
00009 struct token_ops token_string_ops = {
00010 .parse = parse_string,
00011 .complete_get_nb = complete_get_nb_string,
00012 .complete_get_elt = complete_get_elt_string,
00013 .get_help = get_help_string,
00014 };
00015
00016 #define MULTISTRING_HELP PSTR("Mul-choice STRING")
00017 #define ANYSTRING_HELP PSTR("Any STRING")
00018 #define FIXEDSTRING_HELP PSTR("Fixed STRING")
00019
00020 static uint8_t
00021 get_token_len(const prog_char * s)
00022 {
00023 prog_char c;
00024 uint8_t i=0;
00025
00026 c = pgm_read_byte(s+i);
00027 while (c!='#' && c!='\0') {
00028 i++;
00029 c = pgm_read_byte(s+i);
00030 }
00031 return i;
00032 }
00033
00034 static const prog_char *
00035 get_next_token(const prog_char * s)
00036 {
00037 uint8_t i;
00038 i = get_token_len(s);
00039 if (pgm_read_byte(s+i) == '#')
00040 return s+i+1;
00041 return NULL;
00042 }
00043
00044 int8_t
00045 parse_string(parse_pgm_token_hdr_t * tk, const char * buf, void * res)
00046 {
00047 struct token_string_data sd;
00048 uint8_t token_len;
00049 const prog_char * str;
00050
00051 if (! *buf)
00052 return -1;
00053
00054 memcpy_P(&sd, &((struct token_string *)tk)->string_data, sizeof(sd));
00055
00056
00057 if (sd.str) {
00058 str = sd.str;
00059 do {
00060 token_len = get_token_len(str);
00061
00062
00063 if (token_len >= STR_TOKEN_SIZE - 1) {
00064 continue;
00065 }
00066
00067 if ( strncmp_P(buf, str, token_len) ) {
00068 continue;
00069 }
00070
00071 if ( !isendoftoken(*(buf+token_len)) ) {
00072 continue;
00073 }
00074
00075 break;
00076 } while ( (str = get_next_token(str)) != NULL );
00077
00078 if (!str)
00079 return -1;
00080 }
00081
00082 else {
00083 token_len=0;
00084 while(!isendoftoken(buf[token_len]) &&
00085 token_len < (STR_TOKEN_SIZE-1))
00086 token_len++;
00087
00088
00089 if (token_len >= STR_TOKEN_SIZE - 1) {
00090 return -1;
00091 }
00092 }
00093
00094 if (res) {
00095
00096 strncpy(res, buf, token_len);
00097 *((char *)res + token_len) = 0;
00098 }
00099
00100 return token_len;
00101 }
00102
00103 int8_t complete_get_nb_string(parse_pgm_token_hdr_t * tk)
00104 {
00105 struct token_string_data sd;
00106 int8_t ret=1;
00107
00108 memcpy_P(&sd, &((struct token_string *)tk)->string_data, sizeof(sd));
00109
00110 if (!sd.str)
00111 return 0;
00112
00113 while( (sd.str = get_next_token(sd.str)) != NULL ) {
00114 ret++;
00115 }
00116 return ret;
00117 }
00118
00119 int8_t complete_get_elt_string(parse_pgm_token_hdr_t * tk, int8_t idx,
00120 char * dstbuf, uint8_t size)
00121 {
00122 struct token_string_data sd;
00123 const prog_char * s;
00124 uint8_t len;
00125
00126 memcpy_P(&sd, &((struct token_string *)tk)->string_data, sizeof(sd));
00127 s = sd.str;
00128
00129 while (idx-- && s)
00130 s = get_next_token(s);
00131
00132 if (!s)
00133 return -1;
00134
00135 len = get_token_len(s);
00136 if (len > size - 1)
00137 return -1;
00138
00139 memcpy_P(dstbuf, s, len);
00140 dstbuf[len] = '\0';
00141
00142 return 0;
00143 }
00144
00145
00146 int8_t get_help_string(parse_pgm_token_hdr_t * tk, char * dstbuf, uint8_t size)
00147 {
00148 struct token_string_data sd;
00149 const prog_char * s;
00150
00151 memcpy_P(&sd, &((struct token_string *)tk)->string_data, sizeof(sd));
00152 s = sd.str;
00153
00154 if (s) {
00155 if (get_next_token(s)) {
00156 strncpy_P(dstbuf, MULTISTRING_HELP, size);
00157 }
00158 else {
00159 strncpy_P(dstbuf, FIXEDSTRING_HELP, size);
00160 }
00161 }
00162 else {
00163 strncpy_P(dstbuf, ANYSTRING_HELP, size);
00164 }
00165
00166 dstbuf[size-1] = '\0';
00167
00168 return 0;
00169 }