00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <inttypes.h>
00027 #include <ctype.h>
00028
00029 #include <aversive/pgmspace.h>
00030
00031 #include "parse.h"
00032
00033
00034
00035 #define debug_printf(args...) do {} while(0)
00036
00037
00038 static int
00039 isendofline(char c)
00040 {
00041 if (c == '\n' ||
00042 c == '\r' )
00043 return 1;
00044 return 0;
00045 }
00046
00047 static int
00048 iscomment(char c)
00049 {
00050 if (c == '#')
00051 return 1;
00052 return 0;
00053 }
00054
00055 int
00056 isendoftoken(char c)
00057 {
00058 if (!c || iscomment(c) || isblank(c) || isendofline(c))
00059 return 1;
00060 return 0;
00061 }
00062
00063 static uint8_t
00064 nb_common_chars(const char * s1, const char * s2)
00065 {
00066 uint8_t i=0;
00067
00068 while (*s1==*s2 && *s1 && *s2) {
00069 s1++;
00070 s2++;
00071 i++;
00072 }
00073 return i;
00074 }
00075
00081 static int8_t
00082 match_inst(parse_pgm_inst_t *inst, const char * buf, uint8_t nb_match_token,
00083 void * result_buf)
00084 {
00085 uint8_t token_num=0;
00086 parse_pgm_token_hdr_t * token_p;
00087 uint8_t i=0;
00088 int8_t n = 0;
00089 struct token_hdr token_hdr;
00090
00091 token_p = (parse_pgm_token_hdr_t *)pgm_read_word(&inst->tokens[token_num]);
00092 if (token_p)
00093 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
00094
00095
00096 while (token_p && (!nb_match_token || i<nb_match_token)) {
00097 debug_printf("TK\n");
00098
00099 while (isblank(*buf)) {
00100 buf++;
00101 }
00102
00103
00104 if ( isendofline(*buf) || iscomment(*buf) )
00105 break;
00106
00107 n = token_hdr.ops->parse(token_p, buf, (result_buf ? result_buf+token_hdr.offset : NULL));
00108 if ( n < 0 )
00109 break;
00110 debug_printf("TK parsed (len=%d)\n", n);
00111 i++;
00112 buf += n;
00113
00114 token_num ++;
00115 token_p = (parse_pgm_token_hdr_t *)pgm_read_word(&inst->tokens[token_num]);
00116 if (token_p)
00117 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
00118 }
00119
00120
00121 if (i==0)
00122 return -1;
00123
00124
00125 if (nb_match_token) {
00126 if (i == nb_match_token) {
00127 return 0;
00128 }
00129 return i;
00130 }
00131
00132
00133 if (token_p) {
00134 return i;
00135 }
00136
00137
00138 while (isblank(*buf)) {
00139 buf++;
00140 }
00141
00142
00143 if ( isendofline(*buf) || iscomment(*buf) )
00144 return 0;
00145
00146
00147 return i;
00148 }
00149
00150
00151 int8_t
00152 parse(parse_pgm_ctx_t ctx[], const char * buf)
00153 {
00154 uint8_t inst_num=0;
00155 parse_pgm_inst_t * inst;
00156 const char * curbuf;
00157 char result_buf[256];
00158 void (*f)(void *, void *) = NULL;
00159 void * data = NULL;
00160 int comment = 0;
00161 int linelen = 0;
00162 int parse_it = 0;
00163 int8_t err = PARSE_NOMATCH;
00164 int8_t tok;
00165 #ifdef CMDLINE_DEBUG
00166 char debug_buf[64];
00167 #endif
00168
00169
00170
00171
00172
00173
00174 curbuf = buf;
00175 while (! isendofline(*curbuf)) {
00176 if ( *curbuf == '\0' ) {
00177 debug_printf("Incomplete buf (len=%d)\n", linelen);
00178 return 0;
00179 }
00180 if ( iscomment(*curbuf) ) {
00181 comment = 1;
00182 }
00183 if ( ! isblank(*curbuf) && ! comment) {
00184 parse_it = 1;
00185 }
00186 curbuf++;
00187 linelen++;
00188 }
00189
00190
00191 while (isendofline(buf[linelen])) {
00192 linelen++;
00193 }
00194
00195
00196 if ( parse_it == 0 ) {
00197 debug_printf("Empty line (len=%d)\n", linelen);
00198 return linelen;
00199 }
00200
00201 #ifdef CMDLINE_DEBUG
00202 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
00203 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
00204 #endif
00205
00206
00207 inst = (parse_pgm_inst_t *)pgm_read_word(ctx+inst_num);
00208 while (inst) {
00209 debug_printf("INST\n");
00210
00211
00212 tok = match_inst(inst, buf, 0, result_buf);
00213
00214 if (tok > 0)
00215 err = PARSE_BAD_ARGS;
00216
00217 else if (!tok) {
00218 debug_printf("INST fully parsed\n");
00219
00220 while (isblank(*curbuf)) {
00221 curbuf++;
00222 }
00223
00224
00225 if (isendofline(*curbuf) || iscomment(*curbuf)) {
00226 if (!f) {
00227 memcpy_P(&f, &inst->f, sizeof(f));
00228 memcpy_P(&data, &inst->data, sizeof(data));
00229 }
00230 else {
00231
00232 err = PARSE_AMBIGUOUS;
00233 f=NULL;
00234 debug_printf("Ambiguous cmd\n");
00235 break;
00236 }
00237 }
00238 }
00239
00240 inst_num ++;
00241 inst = (parse_pgm_inst_t *)pgm_read_word(ctx+inst_num);
00242 }
00243
00244
00245 if (f) {
00246 f(result_buf, data);
00247 }
00248
00249
00250 else {
00251 debug_printf("No match err=%d\n", err);
00252 return err;
00253 }
00254
00255 return linelen;
00256 }
00257
00258 int8_t
00259 complete(parse_pgm_ctx_t ctx[], const char *buf, int16_t *state,
00260 char *dst, uint8_t size)
00261 {
00262 const char * incomplete_token = buf;
00263 uint8_t inst_num = 0;
00264 parse_pgm_inst_t *inst;
00265 parse_pgm_token_hdr_t *token_p;
00266 struct token_hdr token_hdr;
00267 char tmpbuf[64], completion_buf[64];
00268 uint8_t incomplete_token_len;
00269 int8_t completion_len = -1;
00270 int8_t nb_token = 0;
00271 uint8_t i, n;
00272 int8_t l;
00273 uint8_t nb_completable;
00274 uint8_t nb_non_completable;
00275 uint16_t local_state=0;
00276 prog_char *help_str;
00277
00278 debug_printf("%s called\n", __FUNCTION__);
00279
00280 for (i=0 ; buf[i] ; i++) {
00281 if (!isblank(buf[i]) && isblank(buf[i+1]))
00282 nb_token++;
00283 if (isblank(buf[i]) && !isblank(buf[i+1]))
00284 incomplete_token = buf+i+1;
00285 }
00286 incomplete_token_len = strlen(incomplete_token);
00287
00288
00289 if (*state <= 0) {
00290 debug_printf("try complete <%s>\n", buf);
00291 debug_printf("there is %d complete tokens, <%s> is incomplete\n", nb_token, incomplete_token);
00292
00293 nb_completable = 0;
00294 nb_non_completable = 0;
00295
00296 inst = (parse_pgm_inst_t *)pgm_read_word(ctx+inst_num);
00297 while (inst) {
00298
00299 if (nb_token && match_inst(inst, buf, nb_token, NULL))
00300 goto next;
00301
00302 debug_printf("instruction match \n");
00303 token_p = (parse_pgm_token_hdr_t *) pgm_read_word(&inst->tokens[nb_token]);
00304 if (token_p)
00305 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
00306
00307
00308 if (!token_p ||
00309 !token_hdr.ops->complete_get_nb ||
00310 !token_hdr.ops->complete_get_elt ||
00311 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
00312 nb_non_completable++;
00313 goto next;
00314 }
00315
00316 debug_printf("%d choices for this token\n", n);
00317 for (i=0 ; i<n ; i++) {
00318 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
00319 continue;
00320 strcat_P(tmpbuf, PSTR(" "));
00321 debug_printf(" choice <%s>\n", tmpbuf);
00322
00323 if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
00324 if (completion_len == -1) {
00325 strcpy(completion_buf, tmpbuf+incomplete_token_len);
00326 completion_len = strlen(tmpbuf+incomplete_token_len);
00327
00328 }
00329 else {
00330 completion_len = nb_common_chars(completion_buf,
00331 tmpbuf+incomplete_token_len);
00332 completion_buf[completion_len] = 0;
00333 }
00334 nb_completable++;
00335 }
00336 }
00337 next:
00338 inst_num ++;
00339 inst = (parse_pgm_inst_t *)pgm_read_word(ctx+inst_num);
00340 }
00341
00342 debug_printf("total choices %d for this completion\n", nb_completable);
00343
00344
00345 if (nb_completable == 0 && nb_non_completable == 0)
00346 return 0;
00347
00348
00349 if (*state == 0 && incomplete_token_len > 0) {
00350
00351
00352 if (completion_len > 0) {
00353 if (completion_len + 1 > size)
00354 return 0;
00355
00356 strcpy(dst, completion_buf);
00357 return 2;
00358 }
00359 }
00360 }
00361
00362
00363 if (*state == -1)
00364 *state = 0;
00365
00366 debug_printf("Multiple choice STATE=%d\n", *state);
00367
00368 inst_num = 0;
00369 inst = (parse_pgm_inst_t *)pgm_read_word(ctx+inst_num);
00370 while (inst) {
00371
00372 inst = (parse_pgm_inst_t *)pgm_read_word(ctx+inst_num);
00373
00374 if (nb_token && match_inst(inst, buf, nb_token, NULL))
00375 goto next2;
00376
00377 token_p = (parse_pgm_token_hdr_t *)pgm_read_word(&inst->tokens[nb_token]);
00378 if (token_p)
00379 memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
00380
00381
00382 if (!token_p ||
00383 !token_hdr.ops->complete_get_nb ||
00384 !token_hdr.ops->complete_get_elt ||
00385 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
00386 if (local_state < *state) {
00387 local_state++;
00388 goto next2;
00389 }
00390 (*state)++;
00391 if (token_p && token_hdr.ops->get_help) {
00392 token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
00393 help_str = (prog_char *) pgm_read_word(&inst->help_str);
00394 if (help_str)
00395 snprintf_P(dst, size, PSTR("[%s]: %S"), tmpbuf, help_str);
00396 else
00397 snprintf_P(dst, size, PSTR("[%s]: No help"), tmpbuf);
00398 }
00399 else {
00400 snprintf_P(dst, size, PSTR("[RETURN]"));
00401 }
00402 return 1;
00403 }
00404
00405
00406 for (i=0 ; i<n ; i++) {
00407 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
00408 continue;
00409 strcat_P(tmpbuf, PSTR(" "));
00410 debug_printf(" choice <%s>\n", tmpbuf);
00411
00412 if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
00413 if (local_state < *state) {
00414 local_state++;
00415 continue;
00416 }
00417 (*state)++;
00418 l=snprintf(dst, size, "%s", tmpbuf);
00419 if (l>=0 && token_hdr.ops->get_help) {
00420 token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
00421 help_str = (prog_char *) pgm_read_word(&inst->help_str);
00422 if (help_str)
00423 snprintf_P(dst+l, size-l, PSTR("[%s]: %S"), tmpbuf, help_str);
00424 else
00425 snprintf_P(dst+l, size-l, PSTR("[%s]: No help"), tmpbuf);
00426 }
00427
00428 return 1;
00429 }
00430 }
00431 next2:
00432 inst_num ++;
00433 inst = (parse_pgm_inst_t *)pgm_read_word(ctx+inst_num);
00434 }
00435 return 0;
00436 }
00437