ali@103: #include ali@103: #include ali@103: #include ali@123: #include ali@103: #include "bookloupe.h" ali@103: #include "pending.h" ali@103: ali@103: /* ali@103: * print_pending: ali@103: * ali@103: * If we are in a state of unbalanced quotes, and this line ali@103: * doesn't begin with a quote, output the stored error message. ali@103: * If the -p switch was used, print the warning even if the ali@103: * new para starts with quotes. ali@103: */ ali@103: void print_pending(const char *aline,const char *parastart, ali@103: struct pending *pending) ali@103: { ali@103: if (aline) ali@123: while (g_unichar_isspace(g_utf8_get_char(aline))) ali@123: aline=g_utf8_next_char(aline); ali@103: if (pending->illustration.warning_text) ali@103: { ali@103: if (aline) ali@103: { ali@103: if (pswit[ECHO_SWITCH] && !pending->illustration.queried_line) ali@103: pending->illustration.queried_line=g_strdup(parastart); ali@103: } ali@103: else ali@103: { ali@103: if (!pswit[OVERVIEW_SWITCH]) ali@103: { ali@103: if (pending->illustration.queried_line) ali@103: g_print("\n%s\n",pending->illustration.queried_line); ali@103: g_print("%s\n",pending->illustration.warning_text); ali@103: } ali@103: else ali@103: cnt_brack++; ali@103: g_free(pending->illustration.warning_text); ali@103: pending->illustration.warning_text=NULL; ali@103: g_free(pending->illustration.queried_line); ali@103: pending->illustration.queried_line=NULL; ali@103: } ali@103: } ali@123: if (pending->quote) ali@103: { ali@123: if (!pending->continuing_quote || !aline || ali@123: !g_str_has_prefix(aline,pending->continuing_quote)) ali@103: { ali@103: if (!pswit[OVERVIEW_SWITCH]) ali@103: { ali@103: if (pswit[ECHO_SWITCH]) ali@103: g_print("\n%s\n",parastart); ali@123: g_print("%s\n",pending->quote); ali@103: } ali@103: else ali@123: cnt_quote++; ali@103: } ali@123: g_free(pending->quote); ali@123: pending->quote=NULL; ali@103: } ali@123: g_free(pending->continuing_quote); ali@123: pending->continuing_quote=NULL; ali@103: if (pending->rbrack) ali@103: { ali@103: if (!pswit[OVERVIEW_SWITCH]) ali@103: { ali@103: if (pswit[ECHO_SWITCH]) ali@103: g_print("\n%s\n",parastart); ali@103: g_print("%s\n",pending->rbrack); ali@103: } ali@103: else ali@103: cnt_brack++; ali@103: g_free(pending->rbrack); ali@103: pending->rbrack=NULL; ali@103: } ali@103: if (pending->sbrack) ali@103: { ali@103: if (!pswit[OVERVIEW_SWITCH]) ali@103: { ali@103: if (pswit[ECHO_SWITCH]) ali@103: g_print("\n%s\n",parastart); ali@103: g_print("%s\n",pending->sbrack); ali@103: } ali@103: else ali@103: cnt_brack++; ali@103: g_free(pending->sbrack); ali@103: pending->sbrack=NULL; ali@103: } ali@103: if (pending->cbrack) ali@103: { ali@103: if (!pswit[OVERVIEW_SWITCH]) ali@103: { ali@103: if (pswit[ECHO_SWITCH]) ali@103: g_print("\n%s\n",parastart); ali@103: g_print("%s\n",pending->cbrack); ali@103: } ali@103: else ali@103: cnt_brack++; ali@103: g_free(pending->cbrack); ali@103: pending->cbrack=NULL; ali@103: } ali@103: if (pending->unders) ali@103: { ali@103: if (!pswit[OVERVIEW_SWITCH]) ali@103: { ali@103: if (pswit[ECHO_SWITCH]) ali@103: g_print("\n%s\n",parastart); ali@103: g_print("%s\n",pending->unders); ali@103: } ali@103: else ali@103: cnt_brack++; ali@103: g_free(pending->unders); ali@103: pending->unders=NULL; ali@103: } ali@103: } ali@103: ali@103: void reset_pending(struct pending *pending) ali@103: { ali@103: memset(pending,0,sizeof(*pending)); ali@103: } ali@103: ali@103: /* ali@103: * check_for_mismatched_quotes: ali@103: * ali@103: * At end of paragraph, check for mismatched quotes. ali@103: * ali@103: * We don't want to report an error immediately, since it is a ali@103: * common convention to omit the quotes at end of paragraph if ali@103: * the next paragraph is a continuation of the same speaker. ali@103: * Where this is the case, the next para should begin with a ali@103: * quote, so we store the warning message and only display it ali@103: * at the top of the next iteration if the new para doesn't ali@103: * start with a quote. ali@103: * The -p switch overrides this default, and warns of unclosed ali@103: * quotes on _every_ paragraph, whether the next begins with a ali@103: * quote or not. ali@103: */ ali@123: void check_for_mismatched_quotes(struct counters *counters, ali@103: struct pending *pending) ali@103: { ali@123: gboolean all_single; ali@123: gunichar c; ali@123: int difference; ali@123: const char *quote_type; ali@123: GString *str; ali@123: if (counters->open_quotes) ali@103: { ali@123: str=g_string_new(NULL); ali@123: counters->open_quotes=g_slist_reverse(counters->open_quotes); ali@123: all_single=TRUE; ali@123: while(counters->open_quotes) ali@123: { ali@123: c=GPOINTER_TO_INT(counters->open_quotes->data); ali@123: if (!CHAR_IS_SQUOTE(c)) ali@123: all_single=FALSE; ali@123: g_string_append_unichar(str,c); ali@123: counters->open_quotes=g_slist_delete_link(counters->open_quotes, ali@123: counters->open_quotes); ali@123: } ali@123: pending->continuing_quote=g_string_free(str,FALSE); ali@123: if (all_single) ali@123: quote_type="singlequotes?"; ali@103: else ali@123: quote_type="quotes"; ali@123: pending->quote=g_strdup_printf(" Line %ld - Mismatched %s",linecnt, ali@123: quote_type); ali@103: } ali@103: difference=matching_difference(counters,COUNTER_ILLUSTRATION); ali@103: if (difference) ali@103: { ali@103: if (difference<0 && pending->illustration.warning_text) ali@103: { ali@103: difference++; ali@103: g_free(pending->illustration.queried_line); ali@103: pending->illustration.queried_line=NULL; ali@103: g_free(pending->illustration.warning_text); ali@103: pending->illustration.warning_text=NULL; ali@103: } ali@103: if (difference<0) ali@103: { ali@103: increment_matching(counters,CHAR_OPEN_SBRACK,FALSE); ali@103: difference++; ali@103: } ali@103: if (difference) ali@103: { ali@103: if (pending->illustration.warning_text) ali@103: { ali@103: if (!pswit[OVERVIEW_SWITCH]) ali@103: { ali@103: if (pending->illustration.queried_line) ali@103: g_print("\n%s\n",pending->illustration.queried_line); ali@103: g_print("%s\n",pending->illustration.warning_text); ali@103: } ali@103: else ali@103: cnt_brack++; ali@103: g_free(pending->illustration.warning_text); ali@103: } ali@103: pending->illustration.warning_text=g_strdup_printf( ali@103: " Line %ld - Mismatched illustration tag?",linecnt); ali@103: g_free(pending->illustration.queried_line); ali@103: pending->illustration.queried_line=NULL; ali@103: } ali@103: } ali@103: if (matching_difference(counters,CHAR_OPEN_RBRACK)) ali@103: pending->rbrack= ali@103: g_strdup_printf(" Line %ld - Mismatched round brackets?",linecnt); ali@103: if (matching_difference(counters,CHAR_OPEN_SBRACK)) ali@103: pending->sbrack= ali@103: g_strdup_printf(" Line %ld - Mismatched square brackets?",linecnt); ali@103: if (matching_difference(counters,CHAR_OPEN_CBRACK)) ali@103: pending->cbrack= ali@103: g_strdup_printf(" Line %ld - Mismatched curly brackets?",linecnt); ali@103: if (counters->c_unders%2) ali@103: pending->unders= ali@103: g_strdup_printf(" Line %ld - Mismatched underscores?",linecnt); ali@103: }