# HG changeset patch # User ali # Date 1369603838 -3600 # Node ID 1b0e1aaf180067ab4afbd21700cfd1269c114be0 # Parent a83ca5ff8511da53db11b78635cca0abc6bfe21e Break print_pending() and check_for_mismatched_quotes() out diff -r a83ca5ff8511 -r 1b0e1aaf1800 bookloupe/bookloupe.c --- a/bookloupe/bookloupe.c Sun May 26 21:39:07 2013 +0100 +++ b/bookloupe/bookloupe.c Sun May 26 22:30:38 2013 +0100 @@ -2339,6 +2339,147 @@ } } +struct pending { + char dquote[80],squote[80],rbrack[80],sbrack[80],cbrack[80],unders[80]; + long squot; +}; + +/* + * print_pending: + * + * If we are in a state of unbalanced quotes, and this line + * doesn't begin with a quote, output the stored error message. + * If the -P switch was used, print the warning even if the + * new para starts with quotes. + */ +void print_pending(const char *aline,const char *parastart, + struct pending *pending) +{ + const char *s; + s=aline; + while (*s==' ') + s++; + if (*pending->dquote) + if (*s!=CHAR_DQUOTE || pswit[QPARA_SWITCH]) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + printf("\n%s\n",parastart); + puts(pending->dquote); + } + else + cnt_dquot++; + } + if (*pending->squote) + { + if (*s!=CHAR_SQUOTE && *s!=CHAR_OPEN_SQUOTE || pswit[QPARA_SWITCH] || + pending->squot) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + printf("\n%s\n",parastart); + puts(pending->squote); + } + else + cnt_squot++; + } + } + if (*pending->rbrack) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + printf("\n%s\n",parastart); + puts(pending->rbrack); + } + else + cnt_brack++; + } + if (*pending->sbrack) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + printf("\n%s\n",parastart); + puts(pending->sbrack); + } + else + cnt_brack++; + } + if (*pending->cbrack) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + printf("\n%s\n",parastart); + puts(pending->cbrack); + } + else + cnt_brack++; + } + if (*pending->unders) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + printf("\n%s\n",parastart); + puts(pending->unders); + } + else + cnt_brack++; + } +} + +/* + * check_for_mismatched_quotes: + * + * At end of paragraph, check for mismatched quotes. + * + * We don't want to report an error immediately, since it is a + * common convention to omit the quotes at end of paragraph if + * the next paragraph is a continuation of the same speaker. + * Where this is the case, the next para should begin with a + * quote, so we store the warning message and only display it + * at the top of the next iteration if the new para doesn't + * start with a quote. + * The -p switch overrides this default, and warns of unclosed + * quotes on _every_ paragraph, whether the next begins with a + * quote or not. + */ +void check_for_mismatched_quotes(const struct counters *counters, + struct pending *pending) +{ + if (counters->quot%2) + sprintf(pending->dquote," Line %ld - Mismatched quotes", + linecnt); + if (pswit[SQUOTE_SWITCH] && counters->open_single_quote && + counters->open_single_quote!=counters->close_single_quote) + sprintf(pending->squote," Line %ld - Mismatched singlequotes?", + linecnt); + if (pswit[SQUOTE_SWITCH] && counters->open_single_quote && + counters->open_single_quote!=counters->close_single_quote && + counters->open_single_quote!=counters->close_single_quote+1) + /* + * Flag it to be noted regardless of the + * first char of the next para. + */ + pending->squot=1; + if (counters->r_brack) + sprintf(pending->rbrack," Line %ld - Mismatched round brackets?", + linecnt); + if (counters->s_brack) + sprintf(pending->sbrack," Line %ld - Mismatched square brackets?", + linecnt); + if (counters->c_brack) + sprintf(pending->cbrack," Line %ld - Mismatched curly brackets?", + linecnt); + if (counters->c_unders%2) + sprintf(pending->unders," Line %ld - Mismatched underscores?", + linecnt); +} + /* * procfile: * @@ -2346,7 +2487,7 @@ */ void procfile(char *filename) { - const char *s,*t; + const char *s; char parastart[81]; /* first line of current para */ FILE *infile; struct first_pass_results *first_pass_results; @@ -2354,18 +2495,15 @@ struct counters counters={0}; struct line_properties last={0}; struct parities parities={0}; + struct pending pending={{0},}; int isemptyline; - long squot,start_para_line; + long start_para_line; signed int i,llen,isacro,isellipsis; signed int isnewpara; - char dquote_err[80],squote_err[80],rbrack_err[80],sbrack_err[80], - cbrack_err[80],unders_err[80]; signed int enddash; last.start=CHAR_SPACE; - *dquote_err=*squote_err=*rbrack_err=*cbrack_err=*sbrack_err= - *unders_err=*prevline=0; + *prevline=0; linecnt=checked_linecnt=start_para_line=0; - squot=0; i=llen=isacro=isellipsis=0; isnewpara=enddash=0; infile=fopen(filename,"rb"); @@ -2380,12 +2518,11 @@ fprintf(stdout,"\n\nFile: %s\n\n",filename); first_pass_results=first_pass(infile); warnings=report_first_pass(first_pass_results); - rewind(infile); /* * Here we go with the main pass. Hold onto yer hat! - * Re-init some variables we've dirtied. */ - squot=linecnt=0; + rewind(infile); + linecnt=0; while (flgets(aline,LINEBUFSIZE-1,infile,linecnt+1)) { linecnt++; @@ -2411,90 +2548,8 @@ continue; /* skip through the header */ } checked_linecnt++; - s=aline; - /* - * If we are in a state of unbalanced quotes, and this line - * doesn't begin with a quote, output the stored error message. - * If the -P switch was used, print the warning even if the - * new para starts with quotes. - */ - t=s; - while (*t==' ') - t++; - if (*dquote_err) - if (*t!=CHAR_DQUOTE || pswit[QPARA_SWITCH]) - { - if (!pswit[OVERVIEW_SWITCH]) - { - if (pswit[ECHO_SWITCH]) - printf("\n%s\n",parastart); - printf(dquote_err); - } - else - cnt_dquot++; - } - if (*squote_err) - { - if (*t!=CHAR_SQUOTE && *t!=CHAR_OPEN_SQUOTE || - pswit[QPARA_SWITCH] || squot) - { - if (!pswit[OVERVIEW_SWITCH]) - { - if (pswit[ECHO_SWITCH]) - printf("\n%s\n",parastart); - printf(squote_err); - } - else - cnt_squot++; - } - squot=0; - } - if (*rbrack_err) - { - if (!pswit[OVERVIEW_SWITCH]) - { - if (pswit[ECHO_SWITCH]) - printf("\n%s\n",parastart); - printf(rbrack_err); - } - else - cnt_brack++; - } - if (*sbrack_err) - { - if (!pswit[OVERVIEW_SWITCH]) - { - if (pswit[ECHO_SWITCH]) - printf("\n%s\n",parastart); - printf(sbrack_err); - } - else - cnt_brack++; - } - if (*cbrack_err) - { - if (!pswit[OVERVIEW_SWITCH]) - { - if (pswit[ECHO_SWITCH]) - printf("\n%s\n",parastart); - printf(cbrack_err); - } - else - cnt_brack++; - } - if (*unders_err) - { - if (!pswit[OVERVIEW_SWITCH]) - { - if (pswit[ECHO_SWITCH]) - printf("\n%s\n",parastart); - printf(unders_err); - } - else - cnt_brack++; - } - *dquote_err=*squote_err=*rbrack_err=*cbrack_err= - *sbrack_err=*unders_err=0; + print_pending(aline,parastart,&pending); + memset(&pending,0,sizeof(pending)); isemptyline=analyse_quotes(aline,&counters); if (isnewpara && !isemptyline) { @@ -2589,49 +2644,9 @@ check_for_unpunctuated_endquote(aline); check_for_html_tag(aline); check_for_html_entity(aline); - /* - * At end of paragraph, check for mismatched quotes. - * We don't want to report an error immediately, since it is a - * common convention to omit the quotes at end of paragraph if - * the next paragraph is a continuation of the same speaker. - * Where this is the case, the next para should begin with a - * quote, so we store the warning message and only display it - * at the top of the next iteration if the new para doesn't - * start with a quote. - * The -p switch overrides this default, and warns of unclosed - * quotes on _every_ paragraph, whether the next begins with a - * quote or not. - */ if (isemptyline) { - /* end of para - add up the totals */ - if (counters.quot%2) - sprintf(dquote_err," Line %ld - Mismatched quotes\n", - linecnt); - if (pswit[SQUOTE_SWITCH] && counters.open_single_quote && - counters.open_single_quote!=counters.close_single_quote) - sprintf(squote_err," Line %ld - Mismatched singlequotes?\n", - linecnt); - if (pswit[SQUOTE_SWITCH] && counters.open_single_quote && - counters.open_single_quote!=counters.close_single_quote && - counters.open_single_quote!=counters.close_single_quote+1) - /* - * Flag it to be noted regardless of the - * first char of the next para. - */ - squot=1; - if (counters.r_brack) - sprintf(rbrack_err," Line %ld - " - "Mismatched round brackets?\n",linecnt); - if (counters.s_brack) - sprintf(sbrack_err," Line %ld - " - "Mismatched square brackets?\n",linecnt); - if (counters.c_brack) - sprintf(cbrack_err," Line %ld - " - "Mismatched curly brackets?\n",linecnt); - if (counters.c_unders%2) - sprintf(unders_err," Line %ld - Mismatched underscores?\n", - linecnt); + check_for_mismatched_quotes(&counters,&pending); memset(&counters,0,sizeof(counters)); /* let the next iteration know that it's starting a new para */ isnewpara=1;