Break print_pending() and check_for_mismatched_quotes() out
authorali <ali@juiblex.co.uk>
Sun May 26 22:30:38 2013 +0100 (2013-05-26)
changeset 651b0e1aaf1800
parent 64 a83ca5ff8511
child 66 a5ef278feb34
Break print_pending() and check_for_mismatched_quotes() out
bookloupe/bookloupe.c
     1.1 --- a/bookloupe/bookloupe.c	Sun May 26 21:39:07 2013 +0100
     1.2 +++ b/bookloupe/bookloupe.c	Sun May 26 22:30:38 2013 +0100
     1.3 @@ -2339,6 +2339,147 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +struct pending {
     1.8 +    char dquote[80],squote[80],rbrack[80],sbrack[80],cbrack[80],unders[80];
     1.9 +    long squot;
    1.10 +};
    1.11 +
    1.12 +/*
    1.13 + * print_pending:
    1.14 + *
    1.15 + * If we are in a state of unbalanced quotes, and this line
    1.16 + * doesn't begin with a quote, output the stored error message.
    1.17 + * If the -P switch was used, print the warning even if the
    1.18 + * new para starts with quotes.
    1.19 + */
    1.20 +void print_pending(const char *aline,const char *parastart,
    1.21 +  struct pending *pending)
    1.22 +{
    1.23 +    const char *s;
    1.24 +    s=aline;
    1.25 +    while (*s==' ')
    1.26 +	s++;
    1.27 +    if (*pending->dquote)
    1.28 +	if (*s!=CHAR_DQUOTE || pswit[QPARA_SWITCH])
    1.29 +	{
    1.30 +	    if (!pswit[OVERVIEW_SWITCH])
    1.31 +	    {
    1.32 +		if (pswit[ECHO_SWITCH])
    1.33 +		    printf("\n%s\n",parastart);
    1.34 +		puts(pending->dquote);
    1.35 +	    }
    1.36 +	    else
    1.37 +		cnt_dquot++;
    1.38 +	}
    1.39 +    if (*pending->squote)
    1.40 +    {
    1.41 +	if (*s!=CHAR_SQUOTE && *s!=CHAR_OPEN_SQUOTE || pswit[QPARA_SWITCH] ||
    1.42 +	  pending->squot)
    1.43 +	{
    1.44 +	    if (!pswit[OVERVIEW_SWITCH])
    1.45 +	    {
    1.46 +		if (pswit[ECHO_SWITCH])
    1.47 +		    printf("\n%s\n",parastart);
    1.48 +		puts(pending->squote);
    1.49 +	    }
    1.50 +	    else
    1.51 +		cnt_squot++;
    1.52 +	}
    1.53 +    }
    1.54 +    if (*pending->rbrack)
    1.55 +    {
    1.56 +	if (!pswit[OVERVIEW_SWITCH])
    1.57 +	{
    1.58 +	    if (pswit[ECHO_SWITCH])
    1.59 +		printf("\n%s\n",parastart);
    1.60 +	    puts(pending->rbrack);
    1.61 +	}
    1.62 +	else
    1.63 +	    cnt_brack++;
    1.64 +    }
    1.65 +    if (*pending->sbrack)
    1.66 +    {
    1.67 +	if (!pswit[OVERVIEW_SWITCH])
    1.68 +	{
    1.69 +	    if (pswit[ECHO_SWITCH])
    1.70 +		printf("\n%s\n",parastart);
    1.71 +	    puts(pending->sbrack);
    1.72 +	}
    1.73 +	else
    1.74 +	    cnt_brack++;
    1.75 +    }
    1.76 +    if (*pending->cbrack)
    1.77 +    {
    1.78 +	if (!pswit[OVERVIEW_SWITCH])
    1.79 +	{
    1.80 +	    if (pswit[ECHO_SWITCH])
    1.81 +		printf("\n%s\n",parastart);
    1.82 +	    puts(pending->cbrack);
    1.83 +	}
    1.84 +	else
    1.85 +	    cnt_brack++;
    1.86 +    }
    1.87 +    if (*pending->unders)
    1.88 +    {
    1.89 +	if (!pswit[OVERVIEW_SWITCH])
    1.90 +	{
    1.91 +	    if (pswit[ECHO_SWITCH])
    1.92 +		printf("\n%s\n",parastart);
    1.93 +	    puts(pending->unders);
    1.94 +	}
    1.95 +	else
    1.96 +	    cnt_brack++;
    1.97 +    }
    1.98 +}
    1.99 +
   1.100 +/*
   1.101 + * check_for_mismatched_quotes:
   1.102 + *
   1.103 + * At end of paragraph, check for mismatched quotes.
   1.104 + *
   1.105 + * We don't want to report an error immediately, since it is a
   1.106 + * common convention to omit the quotes at end of paragraph if
   1.107 + * the next paragraph is a continuation of the same speaker.
   1.108 + * Where this is the case, the next para should begin with a
   1.109 + * quote, so we store the warning message and only display it
   1.110 + * at the top of the next iteration if the new para doesn't
   1.111 + * start with a quote.
   1.112 + * The -p switch overrides this default, and warns of unclosed
   1.113 + * quotes on _every_ paragraph, whether the next begins with a
   1.114 + * quote or not.
   1.115 + */
   1.116 +void check_for_mismatched_quotes(const struct counters *counters,
   1.117 +  struct pending *pending)
   1.118 +{
   1.119 +    if (counters->quot%2)
   1.120 +	sprintf(pending->dquote,"    Line %ld - Mismatched quotes",
   1.121 +	  linecnt);
   1.122 +    if (pswit[SQUOTE_SWITCH] && counters->open_single_quote &&
   1.123 +      counters->open_single_quote!=counters->close_single_quote)
   1.124 +	sprintf(pending->squote,"    Line %ld - Mismatched singlequotes?",
   1.125 +	  linecnt);
   1.126 +    if (pswit[SQUOTE_SWITCH] && counters->open_single_quote &&
   1.127 +      counters->open_single_quote!=counters->close_single_quote &&
   1.128 +      counters->open_single_quote!=counters->close_single_quote+1)
   1.129 +	/*
   1.130 +	 * Flag it to be noted regardless of the
   1.131 +	 * first char of the next para.
   1.132 +	 */
   1.133 +	pending->squot=1;
   1.134 +    if (counters->r_brack)
   1.135 +	sprintf(pending->rbrack,"    Line %ld - Mismatched round brackets?",
   1.136 +	  linecnt);
   1.137 +    if (counters->s_brack)
   1.138 +	sprintf(pending->sbrack,"    Line %ld - Mismatched square brackets?",
   1.139 +	  linecnt);
   1.140 +    if (counters->c_brack)
   1.141 +	sprintf(pending->cbrack,"    Line %ld - Mismatched curly brackets?",
   1.142 +	  linecnt);
   1.143 +    if (counters->c_unders%2)
   1.144 +	sprintf(pending->unders,"    Line %ld - Mismatched underscores?",
   1.145 +	  linecnt);
   1.146 +}
   1.147 +
   1.148  /*
   1.149   * procfile:
   1.150   *
   1.151 @@ -2346,7 +2487,7 @@
   1.152   */
   1.153  void procfile(char *filename)
   1.154  {
   1.155 -    const char *s,*t;
   1.156 +    const char *s;
   1.157      char parastart[81];     /* first line of current para */
   1.158      FILE *infile;
   1.159      struct first_pass_results *first_pass_results;
   1.160 @@ -2354,18 +2495,15 @@
   1.161      struct counters counters={0};
   1.162      struct line_properties last={0};
   1.163      struct parities parities={0};
   1.164 +    struct pending pending={{0},};
   1.165      int isemptyline;
   1.166 -    long squot,start_para_line;
   1.167 +    long start_para_line;
   1.168      signed int i,llen,isacro,isellipsis;
   1.169      signed int isnewpara;
   1.170 -    char dquote_err[80],squote_err[80],rbrack_err[80],sbrack_err[80],
   1.171 -      cbrack_err[80],unders_err[80];
   1.172      signed int enddash;
   1.173      last.start=CHAR_SPACE;
   1.174 -    *dquote_err=*squote_err=*rbrack_err=*cbrack_err=*sbrack_err=
   1.175 -      *unders_err=*prevline=0;
   1.176 +    *prevline=0;
   1.177      linecnt=checked_linecnt=start_para_line=0;
   1.178 -    squot=0;
   1.179      i=llen=isacro=isellipsis=0;
   1.180      isnewpara=enddash=0;
   1.181      infile=fopen(filename,"rb");
   1.182 @@ -2380,12 +2518,11 @@
   1.183      fprintf(stdout,"\n\nFile: %s\n\n",filename);
   1.184      first_pass_results=first_pass(infile);
   1.185      warnings=report_first_pass(first_pass_results);
   1.186 -    rewind(infile);
   1.187      /*
   1.188       * Here we go with the main pass. Hold onto yer hat!
   1.189 -     * Re-init some variables we've dirtied.
   1.190       */
   1.191 -    squot=linecnt=0;
   1.192 +    rewind(infile);
   1.193 +    linecnt=0;
   1.194      while (flgets(aline,LINEBUFSIZE-1,infile,linecnt+1))
   1.195      {
   1.196          linecnt++;
   1.197 @@ -2411,90 +2548,8 @@
   1.198              continue;                /* skip through the header */
   1.199  	}
   1.200          checked_linecnt++;
   1.201 -        s=aline;
   1.202 -        /*
   1.203 -	 * If we are in a state of unbalanced quotes, and this line
   1.204 -         * doesn't begin with a quote, output the stored error message.
   1.205 -         * If the -P switch was used, print the warning even if the
   1.206 -         * new para starts with quotes.
   1.207 -	 */
   1.208 -        t=s;
   1.209 -        while (*t==' ')
   1.210 -	    t++;
   1.211 -        if (*dquote_err)
   1.212 -            if (*t!=CHAR_DQUOTE || pswit[QPARA_SWITCH])
   1.213 -	    {
   1.214 -                if (!pswit[OVERVIEW_SWITCH])
   1.215 -		{
   1.216 -                    if (pswit[ECHO_SWITCH])
   1.217 -			printf("\n%s\n",parastart);
   1.218 -                    printf(dquote_err);
   1.219 -		}
   1.220 -                else
   1.221 -                    cnt_dquot++;
   1.222 -            }
   1.223 -        if (*squote_err)
   1.224 -	{
   1.225 -            if (*t!=CHAR_SQUOTE && *t!=CHAR_OPEN_SQUOTE ||
   1.226 -	      pswit[QPARA_SWITCH] || squot)
   1.227 -	    {
   1.228 -                if (!pswit[OVERVIEW_SWITCH])
   1.229 -		{
   1.230 -                    if (pswit[ECHO_SWITCH])
   1.231 -			printf("\n%s\n",parastart);
   1.232 -                    printf(squote_err);
   1.233 -		}
   1.234 -                else
   1.235 -                    cnt_squot++;
   1.236 -	    }
   1.237 -            squot=0;
   1.238 -	}
   1.239 -        if (*rbrack_err)
   1.240 -	{
   1.241 -            if (!pswit[OVERVIEW_SWITCH])
   1.242 -	    {
   1.243 -                if (pswit[ECHO_SWITCH])
   1.244 -		    printf("\n%s\n",parastart);
   1.245 -                printf(rbrack_err);
   1.246 -	    }
   1.247 -            else
   1.248 -                cnt_brack++;
   1.249 -	}
   1.250 -        if (*sbrack_err)
   1.251 -	{
   1.252 -            if (!pswit[OVERVIEW_SWITCH])
   1.253 -	    {
   1.254 -                if (pswit[ECHO_SWITCH])
   1.255 -		    printf("\n%s\n",parastart);
   1.256 -                printf(sbrack_err);
   1.257 -	    }
   1.258 -            else
   1.259 -                cnt_brack++;
   1.260 -	}
   1.261 -        if (*cbrack_err)
   1.262 -	{
   1.263 -            if (!pswit[OVERVIEW_SWITCH])
   1.264 -	    {
   1.265 -                if (pswit[ECHO_SWITCH])
   1.266 -		    printf("\n%s\n",parastart);
   1.267 -                printf(cbrack_err);
   1.268 -	    }
   1.269 -            else
   1.270 -                cnt_brack++;
   1.271 -	}
   1.272 -        if (*unders_err)
   1.273 -	{
   1.274 -            if (!pswit[OVERVIEW_SWITCH])
   1.275 -	    {
   1.276 -                if (pswit[ECHO_SWITCH])
   1.277 -		    printf("\n%s\n",parastart);
   1.278 -                printf(unders_err);
   1.279 -	    }
   1.280 -            else
   1.281 -                cnt_brack++;
   1.282 -	}
   1.283 -        *dquote_err=*squote_err=*rbrack_err=*cbrack_err= 
   1.284 -	  *sbrack_err=*unders_err=0;
   1.285 +	print_pending(aline,parastart,&pending);
   1.286 +	memset(&pending,0,sizeof(pending));
   1.287  	isemptyline=analyse_quotes(aline,&counters);
   1.288          if (isnewpara && !isemptyline)
   1.289  	{
   1.290 @@ -2589,49 +2644,9 @@
   1.291  	    check_for_unpunctuated_endquote(aline);
   1.292  	check_for_html_tag(aline);
   1.293  	check_for_html_entity(aline);
   1.294 -        /*
   1.295 -	 * At end of paragraph, check for mismatched quotes.
   1.296 -         * We don't want to report an error immediately, since it is a
   1.297 -         * common convention to omit the quotes at end of paragraph if
   1.298 -         * the next paragraph is a continuation of the same speaker.
   1.299 -         * Where this is the case, the next para should begin with a
   1.300 -         * quote, so we store the warning message and only display it
   1.301 -         * at the top of the next iteration if the new para doesn't
   1.302 -         * start with a quote.
   1.303 -         * The -p switch overrides this default, and warns of unclosed
   1.304 -         * quotes on _every_ paragraph, whether the next begins with a
   1.305 -         * quote or not.
   1.306 -	 */
   1.307          if (isemptyline)
   1.308  	{
   1.309 -	    /* end of para - add up the totals */
   1.310 -            if (counters.quot%2)
   1.311 -                sprintf(dquote_err,"    Line %ld - Mismatched quotes\n",
   1.312 -		  linecnt);
   1.313 -            if (pswit[SQUOTE_SWITCH] && counters.open_single_quote &&
   1.314 -	      counters.open_single_quote!=counters.close_single_quote)
   1.315 -                sprintf(squote_err,"    Line %ld - Mismatched singlequotes?\n",
   1.316 -		  linecnt);
   1.317 -            if (pswit[SQUOTE_SWITCH] && counters.open_single_quote &&
   1.318 -	      counters.open_single_quote!=counters.close_single_quote &&
   1.319 -	      counters.open_single_quote!=counters.close_single_quote+1)
   1.320 -		/*
   1.321 -		 * Flag it to be noted regardless of the
   1.322 -		 * first char of the next para.
   1.323 -		 */
   1.324 -                squot=1;
   1.325 -            if (counters.r_brack)
   1.326 -                sprintf(rbrack_err,"    Line %ld - "
   1.327 -		  "Mismatched round brackets?\n",linecnt);
   1.328 -            if (counters.s_brack)
   1.329 -                sprintf(sbrack_err,"    Line %ld - "
   1.330 -		  "Mismatched square brackets?\n",linecnt);
   1.331 -            if (counters.c_brack)
   1.332 -                sprintf(cbrack_err,"    Line %ld - "
   1.333 -		  "Mismatched curly brackets?\n",linecnt);
   1.334 -            if (counters.c_unders%2)
   1.335 -                sprintf(unders_err,"    Line %ld - Mismatched underscores?\n",
   1.336 -		  linecnt);
   1.337 +	    check_for_mismatched_quotes(&counters,&pending);
   1.338  	    memset(&counters,0,sizeof(counters));
   1.339  	    /* let the next iteration know that it's starting a new para */
   1.340              isnewpara=1;