diff options
author | Nicolas Pennequin <nicolas.pennequin@free.fr> | 2007-04-24 00:57:04 +0000 |
---|---|---|
committer | Nicolas Pennequin <nicolas.pennequin@free.fr> | 2007-04-24 00:57:04 +0000 |
commit | d7fd98931940ac84a6d21f3ce380101d7e8fd96f (patch) | |
tree | 94575ec217b527fbac786db1b18fe0c9f69069b1 /apps/gui | |
parent | 7027c6a0f8ed85c176e77ceb019a282b6b3d69ac (diff) | |
download | rockbox-d7fd98931940ac84a6d21f3ce380101d7e8fd96f.tar.gz rockbox-d7fd98931940ac84a6d21f3ce380101d7e8fd96f.zip |
WPS Parser: On parse failure, print a (hopefully) useful error message with line number (sim only).
Also the debug code is still disabled but now in a nicer way.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13251 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/gui')
-rw-r--r-- | apps/gui/wps_parser.c | 89 |
1 files changed, 69 insertions, 20 deletions
diff --git a/apps/gui/wps_parser.c b/apps/gui/wps_parser.c index daf318bbcd..d3e8454737 100644 --- a/apps/gui/wps_parser.c +++ b/apps/gui/wps_parser.c | |||
@@ -38,6 +38,10 @@ | |||
38 | #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps" | 38 | #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps" |
39 | #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps" | 39 | #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps" |
40 | 40 | ||
41 | #define PARSE_FAIL_UNCLOSED_COND 1 | ||
42 | #define PARSE_FAIL_INVALID_CHAR 2 | ||
43 | #define PARSE_FAIL_COND_SYNTAX_ERROR 3 | ||
44 | |||
41 | /* level of current conditional. | 45 | /* level of current conditional. |
42 | -1 means we're not in a conditional. */ | 46 | -1 means we're not in a conditional. */ |
43 | static int level = -1; | 47 | static int level = -1; |
@@ -52,6 +56,9 @@ static int condindex[WPS_MAX_COND_LEVEL]; | |||
52 | /* number of condtional options in current level */ | 56 | /* number of condtional options in current level */ |
53 | static int numoptions[WPS_MAX_COND_LEVEL]; | 57 | static int numoptions[WPS_MAX_COND_LEVEL]; |
54 | 58 | ||
59 | /* the current line in the file */ | ||
60 | static int line; | ||
61 | |||
55 | #ifdef HAVE_LCD_BITMAP | 62 | #ifdef HAVE_LCD_BITMAP |
56 | /* pointers to the bitmap filenames in the WPS source */ | 63 | /* pointers to the bitmap filenames in the WPS source */ |
57 | static const char *bmp_names[MAX_IMAGES]; | 64 | static const char *bmp_names[MAX_IMAGES]; |
@@ -69,6 +76,8 @@ extern void print_img_cond_indexes(struct wps_data *data); | |||
69 | extern void print_wps_strings(struct wps_data *data); | 76 | extern void print_wps_strings(struct wps_data *data); |
70 | #endif | 77 | #endif |
71 | 78 | ||
79 | static void wps_reset(struct wps_data *data); | ||
80 | |||
72 | /* Function for parsing of details for a token. At the moment the | 81 | /* Function for parsing of details for a token. At the moment the |
73 | function is called, the token type has already been set. The | 82 | function is called, the token type has already been set. The |
74 | function must fill in the details and possibly add more tokens | 83 | function must fill in the details and possibly add more tokens |
@@ -264,6 +273,7 @@ static const struct wps_tag all_tags[] = { | |||
264 | immediately after the first eol, i.e. to the start of the next line */ | 273 | immediately after the first eol, i.e. to the start of the next line */ |
265 | static int skip_end_of_line(const char *wps_bufptr) | 274 | static int skip_end_of_line(const char *wps_bufptr) |
266 | { | 275 | { |
276 | line++; | ||
267 | int skip = 0; | 277 | int skip = 0; |
268 | while(*(wps_bufptr + skip) != '\n') | 278 | while(*(wps_bufptr + skip) != '\n') |
269 | skip++; | 279 | skip++; |
@@ -639,9 +649,11 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) | |||
639 | 649 | ||
640 | char *current_string = data->string_buffer; | 650 | char *current_string = data->string_buffer; |
641 | int stringbuf_used = 0; | 651 | int stringbuf_used = 0; |
652 | int fail = 0; | ||
653 | line = 1; | ||
642 | level = -1; | 654 | level = -1; |
643 | 655 | ||
644 | while(*wps_bufptr && data->num_tokens < WPS_MAX_TOKENS - 1 | 656 | while(*wps_bufptr && !fail && data->num_tokens < WPS_MAX_TOKENS - 1 |
645 | && data->num_lines < WPS_MAX_LINES) | 657 | && data->num_lines < WPS_MAX_LINES) |
646 | { | 658 | { |
647 | switch(*wps_bufptr++) | 659 | switch(*wps_bufptr++) |
@@ -655,7 +667,10 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) | |||
655 | /* Alternating sublines separator */ | 667 | /* Alternating sublines separator */ |
656 | case ';': | 668 | case ';': |
657 | if (level >= 0) /* there are unclosed conditionals */ | 669 | if (level >= 0) /* there are unclosed conditionals */ |
658 | return false; | 670 | { |
671 | fail = PARSE_FAIL_UNCLOSED_COND; | ||
672 | break; | ||
673 | } | ||
659 | 674 | ||
660 | if (data->num_sublines+1 < WPS_MAX_SUBLINES) | 675 | if (data->num_sublines+1 < WPS_MAX_SUBLINES) |
661 | wps_start_new_subline(data); | 676 | wps_start_new_subline(data); |
@@ -667,7 +682,10 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) | |||
667 | /* Conditional list start */ | 682 | /* Conditional list start */ |
668 | case '<': | 683 | case '<': |
669 | if (data->tokens[data->num_tokens-2].type != WPS_TOKEN_CONDITIONAL) | 684 | if (data->tokens[data->num_tokens-2].type != WPS_TOKEN_CONDITIONAL) |
670 | return false; | 685 | { |
686 | fail = PARSE_FAIL_COND_SYNTAX_ERROR; | ||
687 | break; | ||
688 | } | ||
671 | 689 | ||
672 | data->tokens[data->num_tokens].type = WPS_TOKEN_CONDITIONAL_START; | 690 | data->tokens[data->num_tokens].type = WPS_TOKEN_CONDITIONAL_START; |
673 | lastcond[level] = data->num_tokens++; | 691 | lastcond[level] = data->num_tokens++; |
@@ -676,7 +694,10 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) | |||
676 | /* Conditional list end */ | 694 | /* Conditional list end */ |
677 | case '>': | 695 | case '>': |
678 | if (level < 0) /* not in a conditional, invalid char */ | 696 | if (level < 0) /* not in a conditional, invalid char */ |
679 | return false; | 697 | { |
698 | fail = PARSE_FAIL_INVALID_CHAR; | ||
699 | break; | ||
700 | } | ||
680 | 701 | ||
681 | data->tokens[data->num_tokens].type = WPS_TOKEN_CONDITIONAL_END; | 702 | data->tokens[data->num_tokens].type = WPS_TOKEN_CONDITIONAL_END; |
682 | if (lastcond[level]) | 703 | if (lastcond[level]) |
@@ -691,7 +712,10 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) | |||
691 | /* Conditional list option */ | 712 | /* Conditional list option */ |
692 | case '|': | 713 | case '|': |
693 | if (level < 0) /* not in a conditional, invalid char */ | 714 | if (level < 0) /* not in a conditional, invalid char */ |
694 | return false; | 715 | { |
716 | fail = PARSE_FAIL_INVALID_CHAR; | ||
717 | break; | ||
718 | } | ||
695 | 719 | ||
696 | data->tokens[data->num_tokens].type = WPS_TOKEN_CONDITIONAL_OPTION; | 720 | data->tokens[data->num_tokens].type = WPS_TOKEN_CONDITIONAL_OPTION; |
697 | if (lastcond[level]) | 721 | if (lastcond[level]) |
@@ -705,7 +729,10 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) | |||
705 | /* Comment */ | 729 | /* Comment */ |
706 | case '#': | 730 | case '#': |
707 | if (level >= 0) /* there are unclosed conditionals */ | 731 | if (level >= 0) /* there are unclosed conditionals */ |
708 | return false; | 732 | { |
733 | fail = PARSE_FAIL_UNCLOSED_COND; | ||
734 | break; | ||
735 | } | ||
709 | 736 | ||
710 | wps_bufptr += skip_end_of_line(wps_bufptr); | 737 | wps_bufptr += skip_end_of_line(wps_bufptr); |
711 | break; | 738 | break; |
@@ -713,8 +740,12 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) | |||
713 | /* End of this line */ | 740 | /* End of this line */ |
714 | case '\n': | 741 | case '\n': |
715 | if (level >= 0) /* there are unclosed conditionals */ | 742 | if (level >= 0) /* there are unclosed conditionals */ |
716 | return false; | 743 | { |
744 | fail = PARSE_FAIL_UNCLOSED_COND; | ||
745 | break; | ||
746 | } | ||
717 | 747 | ||
748 | line++; | ||
718 | wps_start_new_subline(data); | 749 | wps_start_new_subline(data); |
719 | data->num_lines++; /* Start a new line */ | 750 | data->num_lines++; /* Start a new line */ |
720 | 751 | ||
@@ -764,19 +795,41 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) | |||
764 | } | 795 | } |
765 | 796 | ||
766 | #ifdef DEBUG | 797 | #ifdef DEBUG |
767 | /* debugging code */ | 798 | |
768 | if (false) | 799 | #if 0 /* optional debugging code */ |
769 | { | 800 | dump_wps_tokens(data); |
770 | dump_wps_tokens(data); | 801 | print_line_info(data); |
771 | print_line_info(data); | 802 | print_wps_strings(data); |
772 | print_wps_strings(data); | ||
773 | #ifdef HAVE_LCD_BITMAP | 803 | #ifdef HAVE_LCD_BITMAP |
774 | print_img_cond_indexes(data); | 804 | print_img_cond_indexes(data); |
775 | #endif | 805 | #endif |
776 | } | ||
777 | #endif | 806 | #endif |
778 | 807 | ||
779 | return true; | 808 | if (fail) |
809 | { | ||
810 | DEBUGF("Failed parsing on line %d : ", line); | ||
811 | switch (fail) | ||
812 | { | ||
813 | case PARSE_FAIL_UNCLOSED_COND: | ||
814 | DEBUGF("Unclosed conditional"); | ||
815 | break; | ||
816 | |||
817 | case PARSE_FAIL_INVALID_CHAR: | ||
818 | DEBUGF("Invalid conditional char (not in an open conditional)"); | ||
819 | break; | ||
820 | |||
821 | case PARSE_FAIL_COND_SYNTAX_ERROR: | ||
822 | DEBUGF("Conditional syntax error"); | ||
823 | break; | ||
824 | } | ||
825 | DEBUGF("\n"); | ||
826 | } | ||
827 | #endif /* DEBUG */ | ||
828 | |||
829 | if (fail) | ||
830 | wps_reset(data); | ||
831 | |||
832 | return (fail == 0); | ||
780 | } | 833 | } |
781 | 834 | ||
782 | #ifdef HAVE_LCD_BITMAP | 835 | #ifdef HAVE_LCD_BITMAP |
@@ -972,11 +1025,7 @@ bool wps_data_load(struct wps_data *wps_data, | |||
972 | 1025 | ||
973 | /* parse the WPS source */ | 1026 | /* parse the WPS source */ |
974 | if (!wps_parse(wps_data, wps_buffer)) | 1027 | if (!wps_parse(wps_data, wps_buffer)) |
975 | { | ||
976 | DEBUGF("Failed parsing of %s\n", buf); | ||
977 | wps_reset(wps_data); | ||
978 | return false; | 1028 | return false; |
979 | } | ||
980 | 1029 | ||
981 | wps_data->wps_loaded = true; | 1030 | wps_data->wps_loaded = true; |
982 | 1031 | ||