bookloupe/bookloupe.c
author ali <ali@juiblex.co.uk>
Tue Sep 24 07:18:50 2013 +0100 (2013-09-24)
changeset 162 8c2d6a0cf717
parent 103 adc06e9e8470
child 144 d7a97f077f9e
child 147 bb31577536d1
child 150 fd584db1d305
child 151 a485f5dcc2de
child 158 408b56cff0bd
permissions -rw-r--r--
Fix bug #10: loupe-test should have an option to display bookloupe output
ali@0
     1
/*************************************************************************/
ali@40
     2
/* bookloupe--check for assorted weirdnesses in a PG candidate text file */
ali@68
     3
/*									 */
ali@68
     4
/* Copyright 2000-2005 Jim Tinsley <jtinsley@pobox.com>			 */
ali@68
     5
/* Copyright 2012- J. Ali Harlow <ali@juiblex.co.uk>			 */
ali@68
     6
/*									 */
ali@0
     7
/* This program is free software; you can redistribute it and/or modify  */
ali@0
     8
/* it under the terms of the GNU General Public License as published by  */
ali@0
     9
/* the Free Software Foundation; either version 2 of the License, or     */
ali@68
    10
/* (at your option) any later version.					 */
ali@68
    11
/*									 */
ali@0
    12
/* This program is distributed in the hope that it will be useful,       */
ali@68
    13
/* but WITHOUT ANY WARRANTY; without even the implied warranty of	 */
ali@68
    14
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the		 */
ali@68
    15
/* GNU General Public License for more details.				 */
ali@68
    16
/*									 */
ali@68
    17
/* You should have received a copy of the GNU General Public License	 */
ali@68
    18
/* along with this program. If not, see <http://www.gnu.org/licenses/>.	 */
ali@0
    19
/*************************************************************************/
ali@0
    20
ali@0
    21
#include <stdio.h>
ali@0
    22
#include <stdlib.h>
ali@0
    23
#include <string.h>
ali@0
    24
#include <ctype.h>
ali@73
    25
#ifdef __WIN32__
ali@73
    26
#include <windows.h>
ali@73
    27
#endif
ali@69
    28
#include <glib.h>
ali@69
    29
#include <bl/bl.h>
ali@99
    30
#include "bookloupe.h"
ali@99
    31
#include "counters.h"
ali@103
    32
#include "pending.h"
ali@71
    33
#include "HTMLentities.h"
ali@0
    34
ali@69
    35
gchar *prevline;
ali@0
    36
ali@40
    37
/* Common typos. */
ali@40
    38
char *typo[] = {
ali@40
    39
    "teh", "th", "og", "fi", "ro", "adn", "yuo", "ot", "fo", "thet", "ane",
ali@40
    40
    "nad", "te", "ig", "acn",  "ahve", "alot", "anbd", "andt", "awya", "aywa",
ali@40
    41
    "bakc", "om", "btu", "byt", "cna", "cxan", "coudl", "dont", "didnt",
ali@40
    42
    "couldnt", "wouldnt", "doesnt", "shouldnt", "doign", "ehr", "hmi", "hse",
ali@40
    43
    "esle", "eyt", "fitrs", "firts", "foudn", "frmo", "fromt", "fwe", "gaurd",
ali@40
    44
    "gerat", "goign", "gruop", "haev", "hda", "hearign", "seeign", "sayign",
ali@40
    45
    "herat", "hge", "hsa", "hsi", "hte", "htere", "htese", "htey", "htis",
ali@40
    46
    "hvae", "hwich", "idae", "ihs", "iits", "int", "iwll", "iwth", "jsut",
ali@40
    47
    "loev", "sefl", "myu", "nkow", "nver", "nwe", "nwo", "ocur", "ohter",
ali@40
    48
    "omre", "onyl", "otehr", "otu", "owrk", "owuld", "peice", "peices",
ali@40
    49
    "peolpe", "peopel", "perhasp", "perhpas", "pleasent", "poeple", "porblem",
ali@40
    50
    "porblems", "rwite", "saidt", "saidh", "saids", "seh", "smae", "smoe",
ali@40
    51
    "sohw", "stnad", "stopry", "stoyr", "stpo", "tahn", "taht", "tath",
ali@40
    52
    "tehy", "tghe", "tghis", "theri", "theyll", "thgat", "thge", "thier",
ali@40
    53
    "thna", "thne", "thnig", "thnigs", "thsi", "thsoe", "thta", "timne",
ali@40
    54
    "tirne", "tkae", "tthe", "tyhat", "tyhe", "veyr", "vou", "vour", "vrey",
ali@40
    55
    "waht", "wasnt", "awtn", "watn", "wehn", "whic", "whcih", "whihc", "whta",
ali@40
    56
    "wihch", "wief", "wiht", "witha", "wiull", "wnat", "wnated", "wnats",
ali@40
    57
    "woh", "wohle", "wokr", "woudl", "wriet", "wrod", "wroet", "wroking",
ali@40
    58
    "wtih", "wuould", "wya", "yera", "yeras", "yersa", "yoiu", "youve",
ali@40
    59
    "ytou", "yuor", "abead", "ahle", "ahout", "ahove", "altbough", "balf",
ali@40
    60
    "bardly", "bas", "bave", "baving", "bebind", "beld", "belp", "belped",
ali@40
    61
    "ber", "bere", "bim", "bis", "bome", "bouse", "bowever", "buge",
ali@40
    62
    "dehates", "deht", "han", "hecause", "hecome", "heen", "hefore", "hegan",
ali@40
    63
    "hegin", "heing", "helieve", "henefit", "hetter", "hetween", "heyond",
ali@40
    64
    "hig", "higber", "huild", "huy", "hy", "jobn", "joh", "meanwbile",
ali@40
    65
    "memher", "memhers", "numher", "numhers", "perbaps", "prohlem", "puhlic",
ali@40
    66
    "witbout", "arn", "hin", "hirn", "wrok", "wroked", "amd", "aud",
ali@40
    67
    "prornise", "prornised", "modem", "bo", "heside", "chapteb", "chaptee",
ali@40
    68
    "se", ""
ali@40
    69
};
ali@0
    70
ali@69
    71
GTree *usertypo;
ali@0
    72
ali@40
    73
/* Common abbreviations and other OK words not to query as typos. */
ali@40
    74
char *okword[] = {
ali@40
    75
    "mr", "mrs", "mss", "mssrs", "ft", "pm", "st", "dr", "hmm", "h'm", "hmmm",
ali@40
    76
    "rd", "sh", "br", "pp", "hm", "cf", "jr", "sr", "vs", "lb", "lbs", "ltd",
ali@40
    77
    "pompeii","hawaii","hawaiian", "hotbed", "heartbeat", "heartbeats",
ali@40
    78
    "outbid", "outbids", "frostbite", "frostbitten", ""
ali@40
    79
};
ali@0
    80
ali@40
    81
/* Common abbreviations that cause otherwise unexplained periods. */
ali@40
    82
char *abbrev[] = {
ali@40
    83
    "cent", "cents", "viz", "vol", "vols", "vid", "ed", "al", "etc", "op",
ali@40
    84
    "cit", "deg", "min", "chap", "oz", "mme", "mlle", "mssrs", ""
ali@40
    85
};
ali@0
    86
ali@40
    87
/*
ali@40
    88
 * Two-Letter combinations that rarely if ever start words,
ali@40
    89
 * but are common scannos or otherwise common letter combinations.
ali@40
    90
 */
ali@40
    91
char *nostart[] = {
ali@40
    92
    "hr", "hl", "cb", "sb", "tb", "wb", "tl", "tn", "rn", "lt", "tj", ""
ali@40
    93
};
ali@0
    94
ali@40
    95
/*
ali@40
    96
 * Two-Letter combinations that rarely if ever end words,
ali@40
    97
 * but are common scannos or otherwise common letter combinations.
ali@40
    98
 */
ali@40
    99
char *noend[] = {
ali@40
   100
    "cb", "gb", "pb", "sb", "tb", "wh", "fr", "br", "qu", "tw", "gl", "fl",
ali@40
   101
    "sw", "gr", "sl", "cl", "iy", ""
ali@40
   102
};
ali@0
   103
ali@40
   104
char *markup[] = {
ali@40
   105
    "a", "b", "big", "blockquote", "body", "br", "center", "col", "div", "em",
ali@40
   106
    "font", "h1", "h2", "h3", "h4", "h5", "h6", "head", "hr", "html", "i",
ali@40
   107
    "img", "li", "meta", "ol", "p", "pre", "small", "span", "strong", "sub",
ali@40
   108
    "sup", "table", "td", "tfoot", "thead", "title", "tr", "tt", "u", "ul", ""
ali@40
   109
};
ali@0
   110
ali@40
   111
char *DPmarkup[] = {
ali@40
   112
    "<sc>", "</sc>", "/*", "*/", "/#", "#/", "/$", "$/", "<tb>", ""
ali@40
   113
};
ali@0
   114
ali@40
   115
char *nocomma[] = {
ali@40
   116
    "the", "it's", "their", "an", "mrs", "a", "our", "that's", "its", "whose",
ali@40
   117
    "every", "i'll", "your", "my", "mr", "mrs", "mss", "mssrs", "ft", "pm",
ali@40
   118
    "st", "dr", "rd", "pp", "cf", "jr", "sr", "vs", "lb", "lbs", "ltd", "i'm",
ali@40
   119
    "during", "let", "toward", "among", ""
ali@40
   120
};
ali@0
   121
ali@40
   122
char *noperiod[] = {
ali@40
   123
    "every", "i'm", "during", "that's", "their", "your", "our", "my", "or",
ali@40
   124
    "and", "but", "as", "if", "the", "its", "it's", "until", "than", "whether",
ali@40
   125
    "i'll", "whose", "who", "because", "when", "let", "till", "very", "an",
ali@40
   126
    "among", "those", "into", "whom", "having", "thence", ""
ali@40
   127
}; 
ali@0
   128
ali@69
   129
gboolean pswit[SWITNO];  /* program switches */
ali@0
   130
ali@69
   131
static GOptionEntry options[]={
ali@69
   132
    { "dp", 'd', 0, G_OPTION_ARG_NONE, pswit+DP_SWITCH,
ali@69
   133
      "Ignore DP-specific markup", NULL },
ali@69
   134
    { "noecho", 'e', 0, G_OPTION_ARG_NONE, pswit+ECHO_SWITCH,
ali@69
   135
      "Don't echo queried line", NULL },
ali@69
   136
    { "squote", 's', 0, G_OPTION_ARG_NONE, pswit+SQUOTE_SWITCH,
ali@69
   137
      "Check single quotes", NULL },
ali@69
   138
    { "typo", 't', 0, G_OPTION_ARG_NONE, pswit+TYPO_SWITCH,
ali@69
   139
      "Check common typos", NULL },
ali@69
   140
    { "qpara", 'p', 0, G_OPTION_ARG_NONE, pswit+QPARA_SWITCH,
ali@69
   141
      "Require closure of quotes on every paragraph", NULL },
ali@69
   142
    { "relaxed", 'x', 0, G_OPTION_ARG_NONE, pswit+PARANOID_SWITCH,
ali@69
   143
      "Disable paranoid querying of everything", NULL },
ali@69
   144
    { "line-end", 'l', 0, G_OPTION_ARG_NONE, pswit+LINE_END_SWITCH,
ali@69
   145
      "Disable line end checking", NULL },
ali@69
   146
    { "overview", 'o', 0, G_OPTION_ARG_NONE, pswit+OVERVIEW_SWITCH,
ali@69
   147
      "Overview: just show counts", NULL },
ali@69
   148
    { "stdout", 'y', 0, G_OPTION_ARG_NONE, pswit+STDOUT_SWITCH,
ali@69
   149
      "Output errors to stdout instead of stderr", NULL },
ali@69
   150
    { "header", 'h', 0, G_OPTION_ARG_NONE, pswit+HEADER_SWITCH,
ali@69
   151
      "Echo header fields", NULL },
ali@69
   152
    { "markup", 'm', 0, G_OPTION_ARG_NONE, pswit+MARKUP_SWITCH,
ali@69
   153
      "Ignore markup in < >", NULL },
ali@69
   154
    { "usertypo", 'u', 0, G_OPTION_ARG_NONE, pswit+USERTYPO_SWITCH,
ali@69
   155
      "Use file of user-defined typos", NULL },
ali@69
   156
    { "web", 'w', 0, G_OPTION_ARG_NONE, pswit+WEB_SWITCH,
ali@69
   157
      "Defaults for use on www upload", NULL },
ali@69
   158
    { "verbose", 'v', 0, G_OPTION_ARG_NONE, pswit+VERBOSE_SWITCH,
ali@69
   159
      "Verbose - list everything", NULL },
ali@69
   160
    { NULL }
ali@69
   161
};
ali@0
   162
ali@142
   163
long cnt_quote;		/* for overview mode, count of quote queries */
ali@68
   164
long cnt_brack;		/* for overview mode, count of brackets queries */
ali@68
   165
long cnt_bin;		/* for overview mode, count of non-ASCII queries */
ali@68
   166
long cnt_odd;		/* for overview mode, count of odd character queries */
ali@68
   167
long cnt_long;		/* for overview mode, count of long line errors */
ali@68
   168
long cnt_short;		/* for overview mode, count of short line queries */
ali@68
   169
long cnt_punct;		/* for overview mode,
ali@68
   170
			   count of punctuation and spacing queries */
ali@68
   171
long cnt_dash;		/* for overview mode, count of dash-related queries */
ali@68
   172
long cnt_word;		/* for overview mode, count of word queries */
ali@68
   173
long cnt_html;		/* for overview mode, count of html queries */
ali@68
   174
long cnt_lineend;	/* for overview mode, count of line-end queries */
ali@68
   175
long cnt_spacend;	/* count of lines with space at end */
ali@68
   176
long linecnt;		/* count of total lines in the file */
ali@68
   177
long checked_linecnt;	/* count of lines actually checked */
ali@0
   178
ali@69
   179
void proghelp(GOptionContext *context);
ali@69
   180
void procfile(const char *);
ali@0
   181
ali@69
   182
gchar *running_from;
ali@0
   183
ali@70
   184
gboolean mixdigit(const char *);
ali@69
   185
gchar *getaword(const char **);
ali@69
   186
char *flgets(char **,long);
ali@0
   187
void postprocess_for_HTML(char *);
ali@0
   188
char *linehasmarkup(char *);
ali@0
   189
char *losemarkup(char *);
ali@70
   190
gboolean tagcomp(const char *,const char *);
ali@71
   191
void loseentities(char *);
ali@69
   192
gboolean isroman(const char *);
ali@0
   193
void postprocess_for_DP(char *);
ali@72
   194
void print_as_windows_1252(const char *string);
ali@72
   195
void print_as_utf_8(const char *string);
ali@0
   196
ali@69
   197
GTree *qword,*qperiod;
ali@68
   198
ali@73
   199
#ifdef __WIN32__
ali@73
   200
UINT saved_cp;
ali@73
   201
#endif
ali@73
   202
ali@69
   203
void parse_options(int *argc,char ***argv)
ali@0
   204
{
ali@69
   205
    GError *err=NULL;
ali@69
   206
    GOptionContext *context;
ali@69
   207
    context=g_option_context_new(
ali@69
   208
      "file - looks for errors in Project Gutenberg(TM) etexts");
ali@69
   209
    g_option_context_add_main_entries(context,options,NULL);
ali@69
   210
    if (!g_option_context_parse(context,argc,argv,&err))
ali@69
   211
    {
ali@69
   212
	g_printerr("Bookloupe: %s\n",err->message);
ali@69
   213
	g_printerr("Use \"%s --help\" for help\n",(*argv)[0]);
ali@69
   214
	exit(1);
ali@69
   215
    }
ali@40
   216
    /* Paranoid checking is turned OFF, not on, by its switch */
ali@69
   217
    pswit[PARANOID_SWITCH]=!pswit[PARANOID_SWITCH];
ali@40
   218
    if (pswit[PARANOID_SWITCH])
ali@69
   219
	/* if running in paranoid mode, typo checks default to enabled */
ali@69
   220
	pswit[TYPO_SWITCH]=!pswit[TYPO_SWITCH];
ali@40
   221
    /* Line-end checking is turned OFF, not on, by its switch */
ali@69
   222
    pswit[LINE_END_SWITCH]=!pswit[LINE_END_SWITCH];
ali@40
   223
    /* Echoing is turned OFF, not on, by its switch */
ali@69
   224
    pswit[ECHO_SWITCH]=!pswit[ECHO_SWITCH];
ali@40
   225
    if (pswit[OVERVIEW_SWITCH])
ali@40
   226
	/* just print summary; don't echo */
ali@69
   227
	pswit[ECHO_SWITCH]=FALSE;
ali@40
   228
    /*
ali@40
   229
     * Web uploads - for the moment, this is really just a placeholder
ali@40
   230
     * until we decide what processing we really want to do on web uploads
ali@40
   231
     */
ali@40
   232
    if (pswit[WEB_SWITCH])
ali@40
   233
    {
ali@40
   234
	/* specific override for web uploads */
ali@69
   235
	pswit[ECHO_SWITCH]=TRUE;
ali@69
   236
	pswit[SQUOTE_SWITCH]=FALSE;
ali@69
   237
	pswit[TYPO_SWITCH]=TRUE;
ali@69
   238
	pswit[QPARA_SWITCH]=FALSE;
ali@69
   239
	pswit[PARANOID_SWITCH]=TRUE;
ali@69
   240
	pswit[LINE_END_SWITCH]=FALSE;
ali@69
   241
	pswit[OVERVIEW_SWITCH]=FALSE;
ali@69
   242
	pswit[STDOUT_SWITCH]=FALSE;
ali@69
   243
	pswit[HEADER_SWITCH]=TRUE;
ali@69
   244
	pswit[VERBOSE_SWITCH]=FALSE;
ali@69
   245
	pswit[MARKUP_SWITCH]=FALSE;
ali@69
   246
	pswit[USERTYPO_SWITCH]=FALSE;
ali@69
   247
	pswit[DP_SWITCH]=FALSE;
ali@40
   248
    }
ali@69
   249
    if (*argc<2)
ali@40
   250
    {
ali@69
   251
	proghelp(context);
ali@69
   252
	exit(1);
ali@40
   253
    }
ali@69
   254
    g_option_context_free(context);
ali@69
   255
}
ali@69
   256
ali@69
   257
/*
ali@69
   258
 * read_user_scannos:
ali@69
   259
 *
ali@69
   260
 * Read in the user-defined stealth scanno list.
ali@69
   261
 */
ali@69
   262
void read_user_scannos(void)
ali@69
   263
{
ali@69
   264
    GError *err=NULL;
ali@69
   265
    gchar *usertypo_file;
ali@69
   266
    gboolean okay;
ali@69
   267
    int i;
ali@70
   268
    gsize len,nb;
ali@70
   269
    gchar *contents,*utf8,**lines;
ali@69
   270
    usertypo_file=g_strdup("bookloupe.typ");
ali@69
   271
    okay=file_get_contents_text(usertypo_file,&contents,&len,&err);
ali@69
   272
    if (g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
ali@69
   273
    {
ali@69
   274
	g_clear_error(&err);
ali@69
   275
	g_free(usertypo_file);
ali@69
   276
	usertypo_file=g_build_filename(running_from,"bookloupe.typ",NULL);
ali@69
   277
	okay=file_get_contents_text(usertypo_file,&contents,&len,&err);
ali@69
   278
    }
ali@69
   279
    if (g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
ali@69
   280
    {
ali@69
   281
	g_clear_error(&err);
ali@69
   282
	g_free(usertypo_file);
ali@69
   283
	usertypo_file=g_strdup("gutcheck.typ");
ali@69
   284
	okay=file_get_contents_text(usertypo_file,&contents,&len,&err);
ali@69
   285
    }
ali@69
   286
    if (g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
ali@69
   287
    {
ali@69
   288
	g_clear_error(&err);
ali@69
   289
	g_free(usertypo_file);
ali@69
   290
	usertypo_file=g_build_filename(running_from,"gutcheck.typ",NULL);
ali@69
   291
	okay=file_get_contents_text(usertypo_file,&contents,&len,&err);
ali@69
   292
    }
ali@69
   293
    if (g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
ali@69
   294
    {
ali@69
   295
	g_free(usertypo_file);
ali@70
   296
	g_print("   --> I couldn't find bookloupe.typ "
ali@69
   297
	  "-- proceeding without user typos.\n");
ali@69
   298
	return;
ali@69
   299
    }
ali@69
   300
    else if (!okay)
ali@69
   301
    {
ali@69
   302
	fprintf(stderr,"%s: %s\n",usertypo_file,err->message);
ali@69
   303
	g_free(usertypo_file);
ali@69
   304
	g_clear_error(&err);
ali@69
   305
	exit(1);
ali@69
   306
    }
ali@72
   307
    if (g_utf8_validate(contents,len,NULL))
ali@72
   308
	utf8=g_utf8_normalize(contents,len,G_NORMALIZE_DEFAULT_COMPOSE);
ali@72
   309
    else
ali@72
   310
	utf8=g_convert(contents,len,"UTF-8","WINDOWS-1252",NULL,&nb,NULL);
ali@70
   311
    g_free(contents);
ali@70
   312
    lines=g_strsplit_set(utf8,"\r\n",0);
ali@70
   313
    g_free(utf8);
ali@69
   314
    usertypo=g_tree_new_full((GCompareDataFunc)strcmp,NULL,g_free,NULL);
ali@69
   315
    for (i=0;lines[i];i++)
ali@69
   316
	if (*(unsigned char *)lines[i]>'!')
ali@69
   317
	    g_tree_insert(usertypo,lines[i],GINT_TO_POINTER(1));
ali@69
   318
	else
ali@69
   319
	    g_free(lines[i]);
ali@69
   320
    g_free(lines);
ali@69
   321
}
ali@69
   322
ali@69
   323
/*
ali@69
   324
 * read_etext:
ali@69
   325
 *
ali@69
   326
 * Read an etext returning a newly allocated string containing the file
ali@69
   327
 * contents or NULL on error.
ali@69
   328
 */
ali@69
   329
gchar *read_etext(const char *filename,GError **err)
ali@69
   330
{
ali@76
   331
    GError *tmp_err=NULL;
ali@70
   332
    gchar *contents,*utf8;
ali@76
   333
    gsize len,bytes_read,bytes_written;
ali@76
   334
    int i,line,col;
ali@69
   335
    if (!g_file_get_contents(filename,&contents,&len,err))
ali@69
   336
	return NULL;
ali@72
   337
    if (g_utf8_validate(contents,len,NULL))
ali@72
   338
    {
ali@72
   339
	utf8=g_utf8_normalize(contents,len,G_NORMALIZE_DEFAULT_COMPOSE);
ali@72
   340
	g_set_print_handler(print_as_utf_8);
ali@73
   341
#ifdef __WIN32__
ali@73
   342
	SetConsoleOutputCP(CP_UTF8);
ali@73
   343
#endif
ali@72
   344
    }
ali@72
   345
    else
ali@72
   346
    {
ali@76
   347
	utf8=g_convert(contents,len,"UTF-8","WINDOWS-1252",&bytes_read,
ali@76
   348
	  &bytes_written,&tmp_err);
ali@76
   349
	if (g_error_matches(tmp_err,G_CONVERT_ERROR,
ali@76
   350
	  G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
ali@76
   351
	{
ali@76
   352
	    line=col=1;
ali@76
   353
	    for(i=0;i<bytes_read;i++)
ali@76
   354
		if (contents[i]=='\n')
ali@76
   355
		{
ali@76
   356
		    line++;
ali@76
   357
		    col=1;
ali@76
   358
		}
ali@76
   359
		else if (contents[i]!='\r')
ali@76
   360
		    col++;
ali@76
   361
	    g_set_error(err,G_CONVERT_ERROR,G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
ali@76
   362
	      "Input conversion failed. Byte %d at line %d, column %d is not a "
ali@76
   363
	      "valid Windows-1252 character",
ali@76
   364
	      ((unsigned char *)contents)[bytes_read],line,col);
ali@76
   365
	}
ali@76
   366
	else if (tmp_err)
ali@76
   367
	    g_propagate_error(err,tmp_err);
ali@72
   368
	g_set_print_handler(print_as_windows_1252);
ali@73
   369
#ifdef __WIN32__
ali@73
   370
	SetConsoleOutputCP(1252);
ali@73
   371
#endif
ali@72
   372
    }
ali@70
   373
    g_free(contents);
ali@70
   374
    return utf8;
ali@69
   375
}
ali@69
   376
ali@73
   377
void cleanup_on_exit(void)
ali@73
   378
{
ali@73
   379
#ifdef __WIN32__
ali@73
   380
    SetConsoleOutputCP(saved_cp);
ali@73
   381
#endif
ali@73
   382
}
ali@73
   383
ali@69
   384
int main(int argc,char **argv)
ali@69
   385
{
ali@73
   386
#ifdef __WIN32__
ali@73
   387
    atexit(cleanup_on_exit);
ali@73
   388
    saved_cp=GetConsoleOutputCP();
ali@73
   389
#endif
ali@69
   390
    running_from=g_path_get_dirname(argv[0]);
ali@69
   391
    parse_options(&argc,&argv);
ali@40
   392
    if (pswit[USERTYPO_SWITCH])
ali@69
   393
	read_user_scannos();
ali@40
   394
    fprintf(stderr,"bookloupe: Check and report on an e-text\n");
ali@69
   395
    procfile(argv[1]);
ali@40
   396
    if (pswit[OVERVIEW_SWITCH])
ali@40
   397
    {
ali@70
   398
	g_print("    Checked %ld lines of %ld (head+foot = %ld)\n\n",
ali@40
   399
	  checked_linecnt,linecnt,linecnt-checked_linecnt);
ali@70
   400
	g_print("    --------------- Queries found --------------\n");
ali@68
   401
	if (cnt_long)
ali@70
   402
	    g_print("    Long lines:		    %14ld\n",cnt_long);
ali@68
   403
	if (cnt_short)
ali@70
   404
	    g_print("    Short lines:		   %14ld\n",cnt_short);
ali@68
   405
	if (cnt_lineend)
ali@70
   406
	    g_print("    Line-end problems:	     %14ld\n",cnt_lineend);
ali@68
   407
	if (cnt_word)
ali@70
   408
	    g_print("    Common typos:		  %14ld\n",cnt_word);
ali@142
   409
	if (cnt_quote)
ali@142
   410
	    g_print("    Unmatched quotes:	      %14ld\n",cnt_quote);
ali@68
   411
	if (cnt_brack)
ali@70
   412
	    g_print("    Unmatched brackets:	    %14ld\n",cnt_brack);
ali@68
   413
	if (cnt_bin)
ali@70
   414
	    g_print("    Non-ASCII characters:	  %14ld\n",cnt_bin);
ali@68
   415
	if (cnt_odd)
ali@70
   416
	    g_print("    Proofing characters:	   %14ld\n",cnt_odd);
ali@68
   417
	if (cnt_punct)
ali@70
   418
	    g_print("    Punctuation & spacing queries: %14ld\n",cnt_punct);
ali@68
   419
	if (cnt_dash)
ali@70
   420
	    g_print("    Non-standard dashes:	   %14ld\n",cnt_dash);
ali@68
   421
	if (cnt_html)
ali@70
   422
	    g_print("    Possible HTML tags:	    %14ld\n",cnt_html);
ali@70
   423
	g_print("\n");
ali@70
   424
	g_print("    TOTAL QUERIES		  %14ld\n",
ali@142
   425
	  cnt_quote+cnt_brack+cnt_bin+cnt_odd+cnt_long+cnt_short+cnt_punct+
ali@142
   426
	  cnt_dash+cnt_word+cnt_html+cnt_lineend);
ali@40
   427
    }
ali@69
   428
    g_free(running_from);
ali@69
   429
    if (usertypo)
ali@69
   430
	g_tree_unref(usertypo);
ali@40
   431
    return 0;
ali@0
   432
}
ali@0
   433
ali@40
   434
/*
ali@41
   435
 * first_pass:
ali@40
   436
 *
ali@41
   437
 * Run a first pass - verify that it's a valid PG
ali@41
   438
 * file, decide whether to report some things that
ali@41
   439
 * occur many times in the text like long or short
ali@41
   440
 * lines, non-standard dashes, etc.
ali@40
   441
 */
ali@69
   442
struct first_pass_results *first_pass(const char *etext)
ali@0
   443
{
ali@70
   444
    gunichar laststart=CHAR_SPACE;
ali@54
   445
    const char *s;
ali@69
   446
    gchar *lc_line;
ali@70
   447
    int i,j,lbytes,llen;
ali@69
   448
    gchar **lines;
ali@41
   449
    unsigned int lastlen=0,lastblen=0;
ali@41
   450
    long spline=0,nspline=0;
ali@41
   451
    static struct first_pass_results results={0};
ali@69
   452
    gchar *inword;
ali@142
   453
    QuoteClass qc;
ali@69
   454
    lines=g_strsplit(etext,"\n",0);
ali@69
   455
    for (j=0;lines[j];j++)
ali@40
   456
    {
ali@70
   457
	lbytes=strlen(lines[j]);
ali@82
   458
	while (lbytes>0 && lines[j][lbytes-1]=='\r')
ali@70
   459
	    lines[j][--lbytes]='\0';
ali@70
   460
	llen=g_utf8_strlen(lines[j],lbytes);
ali@68
   461
	linecnt++;
ali@69
   462
	if (strstr(lines[j],"*END") && strstr(lines[j],"SMALL PRINT") &&
ali@69
   463
	  (strstr(lines[j],"PUBLIC DOMAIN") || strstr(lines[j],"COPYRIGHT")))
ali@40
   464
	{
ali@68
   465
	    if (spline)
ali@70
   466
		g_print("   --> Duplicate header?\n");
ali@68
   467
	    spline=linecnt+1;   /* first line of non-header text, that is */
ali@40
   468
	}
ali@69
   469
	if (!strncmp(lines[j],"*** START",9) &&
ali@69
   470
	  strstr(lines[j],"PROJECT GUTENBERG"))
ali@40
   471
	{
ali@68
   472
	    if (nspline)
ali@70
   473
		g_print("   --> Duplicate header?\n");
ali@68
   474
	    nspline=linecnt+1;   /* first line of non-header text, that is */
ali@40
   475
	}
ali@68
   476
	if (spline || nspline)
ali@40
   477
	{
ali@70
   478
	    lc_line=g_utf8_strdown(lines[j],lbytes);
ali@69
   479
	    if (strstr(lc_line,"end") && strstr(lc_line,"project gutenberg"))
ali@40
   480
	    {
ali@69
   481
		if (strstr(lc_line,"end")<strstr(lc_line,"project gutenberg"))
ali@40
   482
		{
ali@68
   483
		    if (results.footerline)
ali@40
   484
		    {
ali@40
   485
			/* it's an old-form header - we can detect duplicates */
ali@68
   486
			if (!nspline)
ali@70
   487
			    g_print("   --> Duplicate footer?\n");
ali@40
   488
		    }
ali@68
   489
		    else
ali@68
   490
			results.footerline=linecnt;
ali@40
   491
		}
ali@40
   492
	    }
ali@69
   493
	    g_free(lc_line);
ali@40
   494
	}
ali@68
   495
	if (spline)
ali@41
   496
	    results.firstline=spline;
ali@68
   497
	if (nspline)
ali@41
   498
	    results.firstline=nspline;  /* override with new */
ali@68
   499
	if (results.footerline)
ali@40
   500
	    continue;    /* don't count the boilerplate in the footer */
ali@68
   501
	results.totlen+=llen;
ali@70
   502
	for (s=lines[j];*s;s=g_utf8_next_char(s))
ali@40
   503
	{
ali@70
   504
	    if (g_utf8_get_char(s)>127)
ali@41
   505
		results.binlen++;
ali@70
   506
	    if (g_unichar_isalpha(g_utf8_get_char(s)))
ali@41
   507
		results.alphalen++;
ali@142
   508
	    if (s>lines[j])
ali@142
   509
	    {
ali@142
   510
		if (CHAR_IS_DQUOTE(g_utf8_get_char(s)))
ali@142
   511
		    qc=QUOTE_CLASS(g_utf8_get_char(s));
ali@142
   512
		else
ali@142
   513
		    qc=INVALID_QUOTE;
ali@142
   514
		if ((qc==CLOSING_QUOTE || qc==NEUTRAL_QUOTE) &&
ali@142
   515
		  isalpha(g_utf8_get_char(g_utf8_prev_char(s))))
ali@142
   516
		    results.endquote_count++;
ali@142
   517
	    }
ali@40
   518
	}
ali@69
   519
	if (llen>2 && lastlen>2 && lastlen<SHORTEST_PG_LINE && lastblen>2 &&
ali@69
   520
	  lastblen>SHORTEST_PG_LINE && laststart!=CHAR_SPACE)
ali@41
   521
	    results.shortline++;
ali@70
   522
	if (lbytes>0 &&
ali@70
   523
	  g_utf8_get_char(g_utf8_prev_char(lines[j]+lbytes))<=CHAR_SPACE)
ali@40
   524
	    cnt_spacend++;
ali@69
   525
	if (strstr(lines[j],".,"))
ali@41
   526
	    results.dotcomma++;
ali@68
   527
	/* only count ast lines for ignoring purposes where there is */
ali@68
   528
	/* locase text on the line */
ali@69
   529
	if (strchr(lines[j],'*'))
ali@40
   530
	{
ali@70
   531
	    for (s=lines[j];*s;s=g_utf8_next_char(s))
ali@70
   532
		if (g_unichar_islower(g_utf8_get_char(s)))
ali@68
   533
		    break;
ali@70
   534
	    if (*s)
ali@41
   535
		results.astline++;
ali@40
   536
	}
ali@69
   537
	if (strchr(lines[j],'/'))
ali@68
   538
	    results.fslashline++;
ali@82
   539
	if (lbytes>0)
ali@82
   540
	{
ali@82
   541
	    for (s=g_utf8_prev_char(lines[j]+lbytes);
ali@82
   542
	      s>lines[j] && g_utf8_get_char(s)<=CHAR_SPACE;
ali@82
   543
	      s=g_utf8_prev_char(s))
ali@82
   544
		;
ali@82
   545
	    if (s>g_utf8_next_char(lines[j]) && g_utf8_get_char(s)=='-' &&
ali@82
   546
	      g_utf8_get_char(g_utf8_prev_char(s))!='-')
ali@82
   547
		results.hyphens++;
ali@82
   548
	}
ali@68
   549
	if (llen>LONGEST_PG_LINE)
ali@41
   550
	    results.longline++;
ali@68
   551
	if (llen>WAY_TOO_LONG)
ali@41
   552
	    results.verylongline++;
ali@69
   553
	if (strchr(lines[j],'<') && strchr(lines[j],'>'))
ali@40
   554
	{
ali@69
   555
	    i=(int)(strchr(lines[j],'>')-strchr(lines[j],'<')+1);
ali@68
   556
	    if (i>0)
ali@68
   557
		results.htmcount++;
ali@69
   558
	    if (strstr(lines[j],"<i>"))
ali@41
   559
		results.htmcount+=4; /* bonus marks! */
ali@40
   560
	}
ali@68
   561
	/* Check for spaced em-dashes */
ali@70
   562
	if (lines[j][0] && (s=strstr(g_utf8_next_char(lines[j]),"--")))
ali@40
   563
	{
ali@68
   564
	    results.emdash++;
ali@70
   565
	    if (s[-1]==CHAR_SPACE || s[2]==CHAR_SPACE)
ali@41
   566
		results.space_emdash++;
ali@70
   567
	    if (s[-1]==CHAR_SPACE && s[2]==CHAR_SPACE)
ali@40
   568
		/* count of em-dashes with spaces both sides */
ali@41
   569
		results.non_PG_space_emdash++;
ali@70
   570
	    if (s[-1]!=CHAR_SPACE && s[2]!=CHAR_SPACE)
ali@40
   571
		/* count of PG-type em-dashes with no spaces */
ali@41
   572
		results.PG_space_emdash++;
ali@40
   573
	}
ali@69
   574
	for (s=lines[j];*s;)
ali@40
   575
	{
ali@69
   576
	    inword=getaword(&s);
ali@68
   577
	    if (!strcmp(inword,"hij") || !strcmp(inword,"niet")) 
ali@68
   578
		results.Dutchcount++;
ali@68
   579
	    if (!strcmp(inword,"dans") || !strcmp(inword,"avec")) 
ali@68
   580
		results.Frenchcount++;
ali@68
   581
	    if (!strcmp(inword,"0") || !strcmp(inword,"1")) 
ali@68
   582
		results.standalone_digit++;
ali@69
   583
	    g_free(inword);
ali@40
   584
	}
ali@68
   585
	/* Check for spaced dashes */
ali@69
   586
	if (strstr(lines[j]," -") && *(strstr(lines[j]," -")+2)!='-')
ali@41
   587
	    results.spacedash++;
ali@68
   588
	lastblen=lastlen;
ali@69
   589
	lastlen=llen;
ali@69
   590
	laststart=lines[j][0];
ali@40
   591
    }
ali@69
   592
    g_strfreev(lines);
ali@41
   593
    return &results;
ali@41
   594
}
ali@41
   595
ali@42
   596
/*
ali@42
   597
 * report_first_pass:
ali@42
   598
 *
ali@42
   599
 * Make some snap decisions based on the first pass results.
ali@42
   600
 */
ali@42
   601
struct warnings *report_first_pass(struct first_pass_results *results)
ali@42
   602
{
ali@42
   603
    static struct warnings warnings={0};
ali@42
   604
    if (cnt_spacend>0)
ali@70
   605
	g_print("   --> %ld lines in this file have white space at end\n",
ali@42
   606
	  cnt_spacend);
ali@42
   607
    warnings.dotcomma=1;
ali@42
   608
    if (results->dotcomma>5)
ali@42
   609
    {
ali@68
   610
	warnings.dotcomma=0;
ali@70
   611
	g_print("   --> %ld lines in this file contain '.,'. "
ali@42
   612
	  "Not reporting them.\n",results->dotcomma);
ali@42
   613
    }
ali@42
   614
    /*
ali@42
   615
     * If more than 50 lines, or one-tenth, are short,
ali@42
   616
     * don't bother reporting them.
ali@42
   617
     */
ali@42
   618
    warnings.shortline=1;
ali@42
   619
    if (results->shortline>50 || results->shortline*10>linecnt)
ali@42
   620
    {
ali@68
   621
	warnings.shortline=0;
ali@70
   622
	g_print("   --> %ld lines in this file are short. "
ali@42
   623
	  "Not reporting short lines.\n",results->shortline);
ali@42
   624
    }
ali@42
   625
    /*
ali@42
   626
     * If more than 50 lines, or one-tenth, are long,
ali@42
   627
     * don't bother reporting them.
ali@42
   628
     */
ali@42
   629
    warnings.longline=1;
ali@42
   630
    if (results->longline>50 || results->longline*10>linecnt)
ali@42
   631
    {
ali@68
   632
	warnings.longline=0;
ali@70
   633
	g_print("   --> %ld lines in this file are long. "
ali@42
   634
	  "Not reporting long lines.\n",results->longline);
ali@42
   635
    }
ali@42
   636
    /* If more than 10 lines contain asterisks, don't bother reporting them. */
ali@42
   637
    warnings.ast=1;
ali@42
   638
    if (results->astline>10)
ali@42
   639
    {
ali@68
   640
	warnings.ast=0;
ali@70
   641
	g_print("   --> %ld lines in this file contain asterisks. "
ali@42
   642
	  "Not reporting them.\n",results->astline);
ali@42
   643
    }
ali@42
   644
    /*
ali@42
   645
     * If more than 10 lines contain forward slashes,
ali@42
   646
     * don't bother reporting them.
ali@42
   647
     */
ali@42
   648
    warnings.fslash=1;
ali@42
   649
    if (results->fslashline>10)
ali@42
   650
    {
ali@68
   651
	warnings.fslash=0;
ali@70
   652
	g_print("   --> %ld lines in this file contain forward slashes. "
ali@42
   653
	  "Not reporting them.\n",results->fslashline);
ali@42
   654
    }
ali@42
   655
    /*
ali@42
   656
     * If more than 20 lines contain unpunctuated endquotes,
ali@42
   657
     * don't bother reporting them.
ali@42
   658
     */
ali@42
   659
    warnings.endquote=1;
ali@42
   660
    if (results->endquote_count>20)
ali@42
   661
    {
ali@68
   662
	warnings.endquote=0;
ali@70
   663
	g_print("   --> %ld lines in this file contain unpunctuated endquotes. "
ali@42
   664
	  "Not reporting them.\n",results->endquote_count);
ali@42
   665
    }
ali@42
   666
    /*
ali@42
   667
     * If more than 15 lines contain standalone digits,
ali@42
   668
     * don't bother reporting them.
ali@42
   669
     */
ali@42
   670
    warnings.digit=1;
ali@42
   671
    if (results->standalone_digit>10)
ali@42
   672
    {
ali@68
   673
	warnings.digit=0;
ali@70
   674
	g_print("   --> %ld lines in this file contain standalone 0s and 1s. "
ali@42
   675
	  "Not reporting them.\n",results->standalone_digit);
ali@42
   676
    }
ali@42
   677
    /*
ali@42
   678
     * If more than 20 lines contain hyphens at end,
ali@42
   679
     * don't bother reporting them.
ali@42
   680
     */
ali@42
   681
    warnings.hyphen=1;
ali@42
   682
    if (results->hyphens>20)
ali@42
   683
    {
ali@68
   684
	warnings.hyphen=0;
ali@70
   685
	g_print("   --> %ld lines in this file have hyphens at end. "
ali@42
   686
	  "Not reporting them.\n",results->hyphens);
ali@42
   687
    }
ali@42
   688
    if (results->htmcount>20 && !pswit[MARKUP_SWITCH])
ali@42
   689
    {
ali@70
   690
	g_print("   --> Looks like this is HTML. Switching HTML mode ON.\n");
ali@68
   691
	pswit[MARKUP_SWITCH]=1;
ali@42
   692
    }
ali@42
   693
    if (results->verylongline>0)
ali@70
   694
	g_print("   --> %ld lines in this file are VERY long!\n",
ali@42
   695
	  results->verylongline);
ali@42
   696
    /*
ali@42
   697
     * If there are more non-PG spaced dashes than PG em-dashes,
ali@42
   698
     * assume it's deliberate.
ali@42
   699
     * Current PG guidelines say don't use them, but older texts do,
ali@42
   700
     * and some people insist on them whatever the guidelines say.
ali@42
   701
     */
ali@42
   702
    warnings.dash=1;
ali@42
   703
    if (results->spacedash+results->non_PG_space_emdash>
ali@42
   704
      results->PG_space_emdash)
ali@42
   705
    {
ali@68
   706
	warnings.dash=0;
ali@70
   707
	g_print("   --> There are %ld spaced dashes and em-dashes. "
ali@42
   708
	  "Not reporting them.\n",
ali@42
   709
	  results->spacedash+results->non_PG_space_emdash);
ali@42
   710
    }
ali@42
   711
    /* If more than a quarter of characters are hi-bit, bug out. */
ali@42
   712
    warnings.bin=1;
ali@42
   713
    if (results->binlen*4>results->totlen)
ali@42
   714
    {
ali@70
   715
	g_print("   --> This file does not appear to be ASCII. "
ali@42
   716
	  "Terminating. Best of luck with it!\n");
ali@68
   717
	exit(1);
ali@42
   718
    }
ali@42
   719
    if (results->alphalen*4<results->totlen)
ali@42
   720
    {
ali@70
   721
	g_print("   --> This file does not appear to be text. "
ali@42
   722
	  "Terminating. Best of luck with it!\n");
ali@68
   723
	exit(1);
ali@42
   724
    }
ali@42
   725
    if (results->binlen*100>results->totlen || results->binlen>100)
ali@42
   726
    {
ali@70
   727
	g_print("   --> There are a lot of foreign letters here. "
ali@42
   728
	  "Not reporting them.\n");
ali@68
   729
	warnings.bin=0;
ali@42
   730
    }
ali@69
   731
    warnings.isDutch=FALSE;
ali@42
   732
    if (results->Dutchcount>50)
ali@42
   733
    {
ali@69
   734
	warnings.isDutch=TRUE;
ali@70
   735
	g_print("   --> This looks like Dutch - "
ali@42
   736
	  "switching off dashes and warnings for 's Middags case.\n");
ali@42
   737
    }
ali@69
   738
    warnings.isFrench=FALSE;
ali@42
   739
    if (results->Frenchcount>50)
ali@42
   740
    {
ali@69
   741
	warnings.isFrench=TRUE;
ali@70
   742
	g_print("   --> This looks like French - "
ali@42
   743
	  "switching off some doublepunct.\n");
ali@42
   744
    }
ali@42
   745
    if (results->firstline && results->footerline)
ali@70
   746
	g_print("    The PG header and footer appear to be already on.\n");
ali@42
   747
    else
ali@42
   748
    {
ali@68
   749
	if (results->firstline)
ali@70
   750
	    g_print("    The PG header is on - no footer.\n");
ali@68
   751
	if (results->footerline)
ali@70
   752
	    g_print("    The PG footer is on - no header.\n");
ali@42
   753
    }
ali@70
   754
    g_print("\n");
ali@42
   755
    if (pswit[VERBOSE_SWITCH])
ali@42
   756
    {
ali@68
   757
	warnings.bin=1;
ali@68
   758
	warnings.shortline=1;
ali@68
   759
	warnings.dotcomma=1;
ali@68
   760
	warnings.longline=1;
ali@68
   761
	warnings.dash=1;
ali@68
   762
	warnings.digit=1;
ali@68
   763
	warnings.ast=1;
ali@68
   764
	warnings.fslash=1;
ali@68
   765
	warnings.hyphen=1;
ali@68
   766
	warnings.endquote=1;
ali@70
   767
	g_print("   *** Verbose output is ON -- you asked for it! ***\n");
ali@42
   768
    }
ali@42
   769
    if (warnings.isDutch)
ali@68
   770
	warnings.dash=0;
ali@42
   771
    if (results->footerline>0 && results->firstline>0 &&
ali@42
   772
      results->footerline>results->firstline &&
ali@42
   773
      results->footerline-results->firstline<100)
ali@42
   774
    {
ali@70
   775
	g_print("   --> I don't really know where this text starts. \n");
ali@70
   776
	g_print("       There are no reference points.\n");
ali@70
   777
	g_print("       I'm going to have to report the header and footer "
ali@42
   778
	  "as well.\n");
ali@68
   779
	results->firstline=0;
ali@42
   780
    }
ali@42
   781
    return &warnings;
ali@42
   782
}
ali@42
   783
ali@43
   784
/*
ali@43
   785
 * analyse_quotes:
ali@43
   786
 *
ali@43
   787
 * Look along the line, accumulate the count of quotes, and see
ali@43
   788
 * if this is an empty line - i.e. a line with nothing on it
ali@43
   789
 * but spaces.
ali@43
   790
 * If line has just spaces, period, * and/or - on it, don't
ali@43
   791
 * count it, since empty lines with asterisks or dashes to
ali@43
   792
 * separate sections are common.
ali@43
   793
 *
ali@69
   794
 * Returns: TRUE if the line is empty.
ali@43
   795
 */
ali@142
   796
gboolean analyse_quotes(const char *aline,int linecnt,struct counters *counters)
ali@43
   797
{
ali@68
   798
    int guessquote=0;
ali@69
   799
    /* assume the line is empty until proven otherwise */
ali@69
   800
    gboolean isemptyline=TRUE;
ali@70
   801
    const char *s=aline,*sprev,*snext;
ali@70
   802
    gunichar c;
ali@70
   803
    sprev=NULL;
ali@142
   804
    GError *tmp_err=NULL;
ali@43
   805
    while (*s)
ali@43
   806
    {
ali@70
   807
	snext=g_utf8_next_char(s);
ali@70
   808
	c=g_utf8_get_char(s);
ali@142
   809
	if (CHAR_IS_DQUOTE(c))
ali@142
   810
	    (void)count_quote(counters,c,QUOTE_CLASS(c),&tmp_err);
ali@142
   811
	else if (CHAR_IS_SQUOTE(c) && pswit[SQUOTE_SWITCH])
ali@43
   812
	{
ali@43
   813
	    if (s==aline)
ali@43
   814
	    {
ali@43
   815
		/*
ali@142
   816
		 * At start of line, it can only be a quotation mark.
ali@43
   817
		 * Hardcode a very common exception!
ali@43
   818
		 */
ali@70
   819
		if (!g_str_has_prefix(snext,"tis") &&
ali@70
   820
		  !g_str_has_prefix(snext,"Tis"))
ali@142
   821
		    (void)count_quote(counters,c,NEUTRAL_QUOTE,&tmp_err);
ali@43
   822
	    }
ali@70
   823
	    else if (g_unichar_isalpha(g_utf8_get_char(sprev)) &&
ali@70
   824
	      g_unichar_isalpha(g_utf8_get_char(snext)))
ali@43
   825
		/* Do nothing! it's definitely an apostrophe, not a quote */
ali@43
   826
		;
ali@43
   827
	    /* it's outside a word - let's check it out */
ali@99
   828
	    else if (c==CHAR_OPEN_SQUOTE || c==CHAR_LS_QUOTE ||
ali@70
   829
	      g_unichar_isalpha(g_utf8_get_char(snext)))
ali@43
   830
	    {
ali@142
   831
		/* certainly looks like a quotation mark */
ali@70
   832
		if (!g_str_has_prefix(snext,"tis") &&
ali@70
   833
		  !g_str_has_prefix(snext,"Tis"))
ali@43
   834
		    /* hardcode a very common exception! */
ali@142
   835
		{
ali@142
   836
		    if (strchr(".?!,;:",g_utf8_get_char(sprev)))
ali@142
   837
			(void)count_quote(counters,c,NEUTRAL_QUOTE,&tmp_err);
ali@142
   838
		    else
ali@142
   839
			(void)count_quote(counters,c,OPENING_QUOTE,&tmp_err);
ali@142
   840
		}
ali@43
   841
	    }
ali@43
   842
	    else
ali@43
   843
	    {
ali@142
   844
		/* now - is it a quotation mark? */
ali@43
   845
		guessquote=0;   /* accumulate clues */
ali@70
   846
		if (g_unichar_isalpha(g_utf8_get_char(sprev)))
ali@43
   847
		{
ali@43
   848
		    /* it follows a letter - could be either */
ali@43
   849
		    guessquote++;
ali@70
   850
		    if (g_utf8_get_char(sprev)=='s')
ali@43
   851
		    {
ali@43
   852
			/* looks like a plural apostrophe */
ali@43
   853
			guessquote-=3;
ali@70
   854
			if (g_utf8_get_char(snext)==CHAR_SPACE)
ali@70
   855
			    /* bonus marks! */
ali@43
   856
			    guessquote-=2;
ali@43
   857
		    }
ali@142
   858
		    if (innermost_quote_matches(counters,c))
ali@142
   859
			/*
ali@142
   860
			 * Give it the benefit of some doubt,
ali@142
   861
			 * if a squote is already open.
ali@142
   862
			 */
ali@142
   863
			guessquote++;
ali@142
   864
		    else
ali@142
   865
			guessquote--;
ali@142
   866
		    if (guessquote>=0)
ali@142
   867
			(void)count_quote(counters,c,CLOSING_QUOTE,&tmp_err);
ali@43
   868
		}
ali@43
   869
		else
ali@142
   870
		    /* no adjacent letter - it must be a quote of some kind */
ali@142
   871
		    (void)count_quote(counters,c,NEUTRAL_QUOTE,&tmp_err);
ali@43
   872
	    }
ali@43
   873
	}
ali@142
   874
	if (tmp_err)
ali@142
   875
	{
ali@142
   876
	    if (pswit[ECHO_SWITCH])
ali@142
   877
		g_print("\n%s\n",aline);
ali@142
   878
	    if (!pswit[OVERVIEW_SWITCH])
ali@142
   879
		g_print("    Line %ld column %ld - %s\n",
ali@142
   880
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1,tmp_err->message);
ali@142
   881
	    g_clear_error(&tmp_err);
ali@142
   882
	}
ali@70
   883
	if (c!=CHAR_SPACE && c!='-' && c!='.' && c!=CHAR_ASTERISK &&
ali@70
   884
	  c!='\r' && c!='\n')
ali@69
   885
	    isemptyline=FALSE;  /* ignore lines like  *  *  *  as spacers */
ali@70
   886
	if (c==CHAR_UNDERSCORE)
ali@43
   887
	    counters->c_unders++;
ali@103
   888
	if (c==CHAR_OPEN_SBRACK)
ali@103
   889
	{
ali@103
   890
	    if (!matching_difference(counters,COUNTER_ILLUSTRATION) &&
ali@103
   891
	      !matching_difference(counters,c) && s==aline &&
ali@103
   892
	      g_str_has_prefix(s,"[Illustration:"))
ali@103
   893
		increment_matching(counters,COUNTER_ILLUSTRATION,TRUE);
ali@103
   894
	    else
ali@103
   895
		increment_matching(counters,c,TRUE);
ali@103
   896
	}
ali@103
   897
	else if (c==CHAR_OPEN_CBRACK || c==CHAR_OPEN_RBRACK)
ali@99
   898
	    increment_matching(counters,c,TRUE);
ali@103
   899
	if (c==CHAR_CLOSE_SBRACK)
ali@103
   900
	{
ali@103
   901
	    if (!matching_count(counters,COUNTER_ILLUSTRATION,FALSE) &&
ali@103
   902
	      !matching_difference(counters,c) && !*snext)
ali@103
   903
		increment_matching(counters,COUNTER_ILLUSTRATION,FALSE);
ali@103
   904
	    else
ali@103
   905
		increment_matching(counters,c,FALSE);
ali@103
   906
	}
ali@103
   907
	else if (c==CHAR_CLOSE_CBRACK || c==CHAR_CLOSE_RBRACK)
ali@99
   908
	    increment_matching(counters,c,FALSE);
ali@70
   909
	sprev=s;
ali@70
   910
	s=snext;
ali@43
   911
    }
ali@43
   912
    return isemptyline;
ali@43
   913
}
ali@43
   914
ali@41
   915
/*
ali@67
   916
 * check_for_control_characters:
ali@67
   917
 *
ali@67
   918
 * Check for invalid or questionable characters in the line
ali@67
   919
 * Anything above 127 is invalid for plain ASCII, and
ali@67
   920
 * non-printable control characters should also be flagged.
ali@67
   921
 * Tabs should generally not be there.
ali@67
   922
 */
ali@67
   923
void check_for_control_characters(const char *aline)
ali@67
   924
{
ali@70
   925
    gunichar c;
ali@67
   926
    const char *s;
ali@70
   927
    for (s=aline;*s;s=g_utf8_next_char(s))
ali@67
   928
    {
ali@70
   929
	c=g_utf8_get_char(s);
ali@67
   930
	if (c<CHAR_SPACE && c!=CHAR_LF && c!=CHAR_CR && c!=CHAR_TAB)
ali@67
   931
	{
ali@67
   932
	    if (pswit[ECHO_SWITCH])
ali@70
   933
		g_print("\n%s\n",aline);
ali@67
   934
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
   935
		g_print("    Line %ld column %ld - Control character %u\n",
ali@70
   936
		  linecnt,g_utf8_pointer_to_offset(s,aline)+1,c);
ali@67
   937
	    else
ali@67
   938
		cnt_bin++;
ali@67
   939
	}
ali@67
   940
    }
ali@67
   941
}
ali@67
   942
ali@67
   943
/*
ali@44
   944
 * check_for_odd_characters:
ali@44
   945
 *
ali@44
   946
 * Check for binary and other odd characters.
ali@44
   947
 */
ali@44
   948
void check_for_odd_characters(const char *aline,const struct warnings *warnings,
ali@69
   949
  gboolean isemptyline)
ali@44
   950
{
ali@44
   951
    /* Don't repeat multiple warnings on one line. */
ali@70
   952
    gboolean eNon_A=FALSE,eTab=FALSE,eTilde=FALSE;
ali@70
   953
    gboolean eCarat=FALSE,eFSlash=FALSE,eAst=FALSE;
ali@44
   954
    const char *s;
ali@70
   955
    gunichar c;
ali@70
   956
    for (s=aline;*s;s=g_utf8_next_char(s))
ali@44
   957
    {
ali@70
   958
	c=g_utf8_get_char(s);
ali@70
   959
	if (!eNon_A && (c<CHAR_SPACE && c!='\t' && c!='\n' || c>127))
ali@44
   960
	{
ali@44
   961
	    if (pswit[ECHO_SWITCH])
ali@70
   962
		g_print("\n%s\n",aline);
ali@44
   963
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
   964
		if (c>127 && c<160 || c>255)
ali@70
   965
		    g_print("    Line %ld column %ld - "
ali@70
   966
		      "Non-ISO-8859 character %u\n",
ali@70
   967
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1,c);
ali@44
   968
		else
ali@70
   969
		    g_print("    Line %ld column %ld - "
ali@70
   970
		      "Non-ASCII character %u\n",
ali@70
   971
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1,c);
ali@44
   972
	    else
ali@44
   973
		cnt_bin++;
ali@70
   974
	    eNon_A=TRUE;
ali@44
   975
	}
ali@70
   976
	if (!eTab && c==CHAR_TAB)
ali@44
   977
	{
ali@44
   978
	    if (pswit[ECHO_SWITCH])
ali@70
   979
		g_print("\n%s\n",aline);
ali@44
   980
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
   981
		g_print("    Line %ld column %ld - Tab character?\n",
ali@70
   982
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@44
   983
	    else
ali@44
   984
		cnt_odd++;
ali@70
   985
	    eTab=TRUE;
ali@44
   986
	}
ali@70
   987
	if (!eTilde && c==CHAR_TILDE)
ali@44
   988
	{
ali@44
   989
	    /*
ali@44
   990
	     * Often used by OCR software to indicate an
ali@44
   991
	     * unrecognizable character.
ali@44
   992
	     */
ali@44
   993
	    if (pswit[ECHO_SWITCH])
ali@70
   994
		g_print("\n%s\n",aline);
ali@44
   995
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
   996
		g_print("    Line %ld column %ld - Tilde character?\n",
ali@70
   997
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@44
   998
	    else
ali@44
   999
		cnt_odd++;
ali@70
  1000
	    eTilde=TRUE;
ali@44
  1001
	}
ali@70
  1002
	if (!eCarat && c==CHAR_CARAT)
ali@44
  1003
	{  
ali@44
  1004
	    if (pswit[ECHO_SWITCH])
ali@70
  1005
		g_print("\n%s\n",aline);
ali@44
  1006
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1007
		g_print("    Line %ld column %ld - Carat character?\n",
ali@70
  1008
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@44
  1009
	    else
ali@44
  1010
		cnt_odd++;
ali@70
  1011
	    eCarat=TRUE;
ali@44
  1012
	}
ali@70
  1013
	if (!eFSlash && c==CHAR_FORESLASH && warnings->fslash)
ali@44
  1014
	{  
ali@44
  1015
	    if (pswit[ECHO_SWITCH])
ali@70
  1016
		g_print("\n%s\n",aline);
ali@44
  1017
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1018
		g_print("    Line %ld column %ld - Forward slash?\n",
ali@70
  1019
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@44
  1020
	    else
ali@44
  1021
		cnt_odd++;
ali@70
  1022
	    eFSlash=TRUE;
ali@44
  1023
	}
ali@44
  1024
	/*
ali@44
  1025
	 * Report asterisks only in paranoid mode,
ali@44
  1026
	 * since they're often deliberate.
ali@44
  1027
	 */
ali@44
  1028
	if (!eAst && pswit[PARANOID_SWITCH] && warnings->ast && !isemptyline &&
ali@70
  1029
	  c==CHAR_ASTERISK)
ali@44
  1030
	{
ali@44
  1031
	    if (pswit[ECHO_SWITCH])
ali@70
  1032
		g_print("\n%s\n",aline);
ali@44
  1033
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1034
		g_print("    Line %ld column %ld - Asterisk?\n",
ali@70
  1035
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@44
  1036
	    else
ali@44
  1037
		cnt_odd++;
ali@70
  1038
	    eAst=TRUE;
ali@44
  1039
	}
ali@44
  1040
    }
ali@44
  1041
}
ali@44
  1042
ali@44
  1043
/*
ali@45
  1044
 * check_for_long_line:
ali@45
  1045
 *
ali@45
  1046
 * Check for line too long.
ali@45
  1047
 */
ali@45
  1048
void check_for_long_line(const char *aline)
ali@45
  1049
{
ali@70
  1050
    if (g_utf8_strlen(aline,-1)>LONGEST_PG_LINE)
ali@45
  1051
    {
ali@45
  1052
	if (pswit[ECHO_SWITCH])
ali@70
  1053
	    g_print("\n%s\n",aline);
ali@45
  1054
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1055
	    g_print("    Line %ld column %ld - Long line %ld\n",
ali@70
  1056
	      linecnt,g_utf8_strlen(aline,-1),g_utf8_strlen(aline,-1));
ali@45
  1057
	else
ali@45
  1058
	    cnt_long++;
ali@45
  1059
    }
ali@45
  1060
}
ali@45
  1061
ali@45
  1062
/*
ali@45
  1063
 * check_for_short_line:
ali@45
  1064
 *
ali@45
  1065
 * Check for line too short.
ali@45
  1066
 *
ali@45
  1067
 * This one is a bit trickier to implement: we don't want to
ali@45
  1068
 * flag the last line of a paragraph for being short, so we
ali@45
  1069
 * have to wait until we know that our current line is a
ali@45
  1070
 * "normal" line, then report the _previous_ line if it was too
ali@45
  1071
 * short. We also don't want to report indented lines like
ali@45
  1072
 * chapter heads or formatted quotations. We therefore keep
ali@45
  1073
 * last->len as the length of the last line examined, and
ali@45
  1074
 * last->blen as the length of the last but one, and try to
ali@45
  1075
 * suppress unnecessary warnings by checking that both were of
ali@45
  1076
 * "normal" length. We keep the first character of the last
ali@45
  1077
 * line in last->start, and if it was a space, we assume that
ali@45
  1078
 * the formatting is deliberate. I can't figure out a way to
ali@45
  1079
 * distinguish something like a quoted verse left-aligned or
ali@45
  1080
 * the header or footer of a letter from a paragraph of short
ali@45
  1081
 * lines - maybe if I examined the whole paragraph, and if the
ali@45
  1082
 * para has less than, say, 8 lines and if all lines are short,
ali@45
  1083
 * then just assume it's OK? Need to look at some texts to see
ali@45
  1084
 * how often a formula like this would get the right result.
ali@45
  1085
 */
ali@45
  1086
void check_for_short_line(const char *aline,const struct line_properties *last)
ali@45
  1087
{
ali@70
  1088
    if (g_utf8_strlen(aline,-1)>1 && last->len>1 &&
ali@70
  1089
      last->len<SHORTEST_PG_LINE && last->blen>1 &&
ali@70
  1090
      last->blen>SHORTEST_PG_LINE && last->start!=CHAR_SPACE)
ali@45
  1091
    {
ali@45
  1092
	if (pswit[ECHO_SWITCH])
ali@70
  1093
	    g_print("\n%s\n",prevline);
ali@45
  1094
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1095
	    g_print("    Line %ld column %ld - Short line %ld?\n",
ali@70
  1096
	      linecnt-1,g_utf8_strlen(prevline,-1),g_utf8_strlen(prevline,-1));
ali@45
  1097
	else
ali@45
  1098
	    cnt_short++;
ali@45
  1099
    }
ali@45
  1100
}
ali@45
  1101
ali@45
  1102
/*
ali@46
  1103
 * check_for_starting_punctuation:
ali@46
  1104
 *
ali@46
  1105
 * Look for punctuation other than full ellipses at start of line.
ali@46
  1106
 */
ali@46
  1107
void check_for_starting_punctuation(const char *aline)
ali@46
  1108
{
ali@70
  1109
    if (*aline && g_utf8_strchr(".?!,;:",-1,g_utf8_get_char(aline)) &&
ali@70
  1110
      !g_str_has_prefix(aline,". . ."))
ali@46
  1111
    {
ali@46
  1112
	if (pswit[ECHO_SWITCH])
ali@70
  1113
	    g_print("\n%s\n",aline);
ali@46
  1114
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1115
	    g_print("    Line %ld column 1 - Begins with punctuation?\n",
ali@46
  1116
	      linecnt);
ali@46
  1117
	else
ali@46
  1118
	    cnt_punct++;
ali@46
  1119
    }
ali@46
  1120
}
ali@46
  1121
ali@46
  1122
/*
ali@47
  1123
 * check_for_spaced_emdash:
ali@47
  1124
 *
ali@47
  1125
 * Check for spaced em-dashes.
ali@47
  1126
 *
ali@47
  1127
 * We must check _all_ occurrences of "--" on the line
ali@47
  1128
 * hence the loop - even if the first double-dash is OK
ali@47
  1129
 * there may be another that's wrong later on.
ali@47
  1130
 */
ali@47
  1131
void check_for_spaced_emdash(const char *aline)
ali@47
  1132
{
ali@70
  1133
    const char *s,*t,*next;
ali@70
  1134
    for (s=aline;t=strstr(s,"--");s=next)
ali@47
  1135
    {
ali@70
  1136
	next=g_utf8_next_char(g_utf8_next_char(t));
ali@70
  1137
	if (t>aline && g_utf8_get_char(g_utf8_prev_char(t))==CHAR_SPACE ||
ali@70
  1138
	  g_utf8_get_char(next)==CHAR_SPACE)
ali@47
  1139
	{
ali@47
  1140
	    if (pswit[ECHO_SWITCH])
ali@70
  1141
		g_print("\n%s\n",aline);
ali@47
  1142
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1143
		g_print("    Line %ld column %ld - Spaced em-dash?\n",
ali@70
  1144
		  linecnt,g_utf8_pointer_to_offset(aline,t)+1);
ali@47
  1145
	    else
ali@47
  1146
		cnt_dash++;
ali@47
  1147
	}
ali@47
  1148
    }
ali@47
  1149
}
ali@47
  1150
ali@47
  1151
/*
ali@47
  1152
 * check_for_spaced_dash:
ali@47
  1153
 *
ali@47
  1154
 * Check for spaced dashes.
ali@47
  1155
 */
ali@47
  1156
void check_for_spaced_dash(const char *aline)
ali@47
  1157
{
ali@47
  1158
    const char *s;
ali@47
  1159
    if ((s=strstr(aline," -")))
ali@47
  1160
    {
ali@70
  1161
	if (g_utf8_get_char(g_utf8_next_char(g_utf8_next_char(s)))!='-')
ali@47
  1162
	{
ali@47
  1163
	    if (pswit[ECHO_SWITCH])
ali@70
  1164
		g_print("\n%s\n",aline);
ali@47
  1165
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1166
		g_print("    Line %ld column %ld - Spaced dash?\n",
ali@70
  1167
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@47
  1168
	    else
ali@47
  1169
		cnt_dash++;
ali@47
  1170
	}
ali@47
  1171
    }
ali@47
  1172
    else if ((s=strstr(aline,"- ")))
ali@47
  1173
    {
ali@70
  1174
	if (s==aline || g_utf8_get_char(g_utf8_prev_char(s))!='-')
ali@47
  1175
	{
ali@47
  1176
	    if (pswit[ECHO_SWITCH])
ali@70
  1177
		g_print("\n%s\n",aline);
ali@47
  1178
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1179
		g_print("    Line %ld column %ld - Spaced dash?\n",
ali@70
  1180
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@47
  1181
	    else
ali@47
  1182
		cnt_dash++;
ali@47
  1183
	}
ali@47
  1184
    }
ali@47
  1185
}
ali@47
  1186
ali@47
  1187
/*
ali@48
  1188
 * check_for_unmarked_paragraphs:
ali@48
  1189
 *
ali@48
  1190
 * Check for unmarked paragraphs indicated by separate speakers.
ali@48
  1191
 *
ali@48
  1192
 * May well be false positive:
ali@48
  1193
 * "Bravo!" "Wonderful!" called the crowd.
ali@48
  1194
 * but useful all the same.
ali@48
  1195
 */
ali@48
  1196
void check_for_unmarked_paragraphs(const char *aline)
ali@48
  1197
{
ali@48
  1198
    const char *s;
ali@48
  1199
    s=strstr(aline,"\"  \"");
ali@48
  1200
    if (!s)
ali@48
  1201
	s=strstr(aline,"\" \"");
ali@48
  1202
    if (s)
ali@48
  1203
    {
ali@48
  1204
	if (pswit[ECHO_SWITCH])
ali@70
  1205
	    g_print("\n%s\n",aline);
ali@48
  1206
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1207
	    g_print("    Line %ld column %ld - "
ali@70
  1208
	      "Query missing paragraph break?\n",
ali@70
  1209
	      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@48
  1210
	else
ali@48
  1211
	    cnt_punct++;
ali@48
  1212
    }
ali@48
  1213
}
ali@48
  1214
ali@48
  1215
/*
ali@49
  1216
 * check_for_jeebies:
ali@49
  1217
 *
ali@49
  1218
 * Check for "to he" and other easy h/b errors.
ali@49
  1219
 *
ali@49
  1220
 * This is a very inadequate effort on the h/b problem,
ali@49
  1221
 * but the phrase "to he" is always an error, whereas "to
ali@49
  1222
 * be" is quite common.
ali@49
  1223
 * Similarly, '"Quiet!", be said.' is a non-be error
ali@49
  1224
 * "to he" is _not_ always an error!:
ali@49
  1225
 *       "Where they went to he couldn't say."
ali@49
  1226
 * Another false positive:
ali@49
  1227
 *       What would "Cinderella" be without the . . .
ali@49
  1228
 * and another: "If he wants to he can see for himself."
ali@49
  1229
 */
ali@49
  1230
void check_for_jeebies(const char *aline)
ali@49
  1231
{
ali@49
  1232
    const char *s;
ali@49
  1233
    s=strstr(aline," be could ");
ali@49
  1234
    if (!s)
ali@49
  1235
	s=strstr(aline," be would ");
ali@49
  1236
    if (!s)
ali@49
  1237
	s=strstr(aline," was be ");
ali@49
  1238
    if (!s)
ali@49
  1239
	s=strstr(aline," be is ");
ali@49
  1240
    if (!s)
ali@49
  1241
	s=strstr(aline," is be ");
ali@49
  1242
    if (!s)
ali@49
  1243
	s=strstr(aline,"\", be ");
ali@49
  1244
    if (!s)
ali@49
  1245
	s=strstr(aline,"\" be ");
ali@49
  1246
    if (!s)
ali@49
  1247
	s=strstr(aline,"\" be ");
ali@49
  1248
    if (!s)
ali@49
  1249
	s=strstr(aline," to he ");
ali@49
  1250
    if (s)
ali@49
  1251
    {
ali@49
  1252
	if (pswit[ECHO_SWITCH])
ali@70
  1253
	    g_print("\n%s\n",aline);
ali@49
  1254
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1255
	    g_print("    Line %ld column %ld - Query he/be error?\n",
ali@70
  1256
	      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@49
  1257
	else
ali@49
  1258
	    cnt_word++;
ali@49
  1259
    }
ali@49
  1260
    s=strstr(aline," the had ");
ali@49
  1261
    if (!s)
ali@49
  1262
	s=strstr(aline," a had ");
ali@49
  1263
    if (!s)
ali@49
  1264
	s=strstr(aline," they bad ");
ali@49
  1265
    if (!s)
ali@49
  1266
	s=strstr(aline," she bad ");
ali@49
  1267
    if (!s)
ali@49
  1268
	s=strstr(aline," he bad ");
ali@49
  1269
    if (!s)
ali@49
  1270
	s=strstr(aline," you bad ");
ali@49
  1271
    if (!s)
ali@49
  1272
	s=strstr(aline," i bad ");
ali@49
  1273
    if (s)
ali@49
  1274
    {
ali@49
  1275
	if (pswit[ECHO_SWITCH])
ali@70
  1276
	    g_print("\n%s\n",aline);
ali@49
  1277
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1278
	    g_print("    Line %ld column %ld - Query had/bad error?\n",
ali@70
  1279
	      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@49
  1280
	else
ali@49
  1281
	    cnt_word++;
ali@49
  1282
    }
ali@49
  1283
    s=strstr(aline,"; hut ");
ali@49
  1284
    if (!s)
ali@49
  1285
	s=strstr(aline,", hut ");
ali@49
  1286
    if (s)
ali@49
  1287
    {
ali@49
  1288
	if (pswit[ECHO_SWITCH])
ali@70
  1289
	    g_print("\n%s\n",aline);
ali@49
  1290
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1291
	    g_print("    Line %ld column %ld - Query hut/but error?\n",
ali@70
  1292
	      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@49
  1293
	else
ali@49
  1294
	    cnt_word++;
ali@49
  1295
    }
ali@49
  1296
}
ali@49
  1297
ali@49
  1298
/*
ali@50
  1299
 * check_for_mta_from:
ali@50
  1300
 *
ali@50
  1301
 * Special case - angled bracket in front of "From" placed there by an
ali@50
  1302
 * MTA when sending an e-mail.
ali@50
  1303
 */
ali@50
  1304
void check_for_mta_from(const char *aline)
ali@50
  1305
{
ali@50
  1306
    const char *s;
ali@50
  1307
    s=strstr(aline,">From");
ali@50
  1308
    if (s)
ali@50
  1309
    {
ali@50
  1310
	if (pswit[ECHO_SWITCH])
ali@70
  1311
	    g_print("\n%s\n",aline);
ali@50
  1312
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1313
	    g_print("    Line %ld column %ld - "
ali@70
  1314
	      "Query angled bracket with From\n",
ali@70
  1315
	      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@50
  1316
	else
ali@50
  1317
	    cnt_punct++;
ali@50
  1318
    }
ali@50
  1319
}
ali@50
  1320
ali@50
  1321
/*
ali@51
  1322
 * check_for_orphan_character:
ali@51
  1323
 *
ali@51
  1324
 * Check for a single character line -
ali@51
  1325
 * often an overflow from bad wrapping.
ali@51
  1326
 */
ali@51
  1327
void check_for_orphan_character(const char *aline)
ali@51
  1328
{
ali@70
  1329
    gunichar c;
ali@70
  1330
    c=g_utf8_get_char(aline);
ali@70
  1331
    if (c && !*g_utf8_next_char(aline))
ali@51
  1332
    {
ali@70
  1333
	if (c=='I' || c=='V' || c=='X' || c=='L' || g_unichar_isdigit(c))
ali@51
  1334
	    ; /* Nothing - ignore numerals alone on a line. */
ali@51
  1335
	else
ali@51
  1336
	{
ali@51
  1337
	    if (pswit[ECHO_SWITCH])
ali@70
  1338
		g_print("\n%s\n",aline);
ali@51
  1339
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1340
		g_print("    Line %ld column 1 - Query single character line\n",
ali@51
  1341
		  linecnt);
ali@51
  1342
	    else
ali@51
  1343
		cnt_punct++;
ali@51
  1344
	}
ali@51
  1345
    }
ali@51
  1346
}
ali@51
  1347
ali@51
  1348
/*
ali@52
  1349
 * check_for_pling_scanno:
ali@52
  1350
 *
ali@52
  1351
 * Check for I" - often should be !
ali@52
  1352
 */
ali@52
  1353
void check_for_pling_scanno(const char *aline)
ali@52
  1354
{
ali@52
  1355
    const char *s;
ali@52
  1356
    s=strstr(aline," I\"");
ali@52
  1357
    if (s)
ali@52
  1358
    {
ali@52
  1359
	if (pswit[ECHO_SWITCH])
ali@70
  1360
	    g_print("\n%s\n",aline);
ali@52
  1361
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1362
	    g_print("    Line %ld column %ld - Query I=exclamation mark?\n",
ali@70
  1363
	      linecnt,g_utf8_pointer_to_offset(aline,s));
ali@52
  1364
	else
ali@52
  1365
	    cnt_punct++;
ali@52
  1366
    }
ali@52
  1367
}
ali@52
  1368
ali@52
  1369
/*
ali@53
  1370
 * check_for_extra_period:
ali@53
  1371
 *
ali@53
  1372
 * Check for period without a capital letter. Cut-down from gutspell.
ali@53
  1373
 * Only works when it happens on a single line.
ali@53
  1374
 */
ali@53
  1375
void check_for_extra_period(const char *aline,const struct warnings *warnings)
ali@53
  1376
{
ali@99
  1377
    const char *s,*t,*s1,*sprev;
ali@69
  1378
    int i;
ali@70
  1379
    gsize len;
ali@69
  1380
    gboolean istypo;
ali@69
  1381
    gchar *testword;
ali@99
  1382
    gunichar c,nc,pc,*decomposition;
ali@53
  1383
    if (pswit[PARANOID_SWITCH])
ali@53
  1384
    {
ali@70
  1385
	for (t=aline;t=strstr(t,". ");)
ali@53
  1386
	{
ali@69
  1387
	    if (t==aline)
ali@53
  1388
	    {
ali@70
  1389
		t=g_utf8_next_char(t);
ali@53
  1390
		/* start of line punctuation is handled elsewhere */
ali@53
  1391
		continue;
ali@53
  1392
	    }
ali@70
  1393
	    if (!g_unichar_isalpha(g_utf8_get_char(g_utf8_prev_char(t))))
ali@53
  1394
	    {
ali@70
  1395
		t=g_utf8_next_char(t);
ali@53
  1396
		continue;
ali@53
  1397
	    }
ali@53
  1398
	    if (warnings->isDutch)
ali@53
  1399
	    {
ali@53
  1400
		/* For Frank & Jeroen -- 's Middags case */
ali@70
  1401
		gunichar c2,c3,c4,c5;
ali@70
  1402
		c2=g_utf8_get_char(g_utf8_offset_to_pointer(t,2));
ali@70
  1403
		c3=g_utf8_get_char(g_utf8_offset_to_pointer(t,3));
ali@70
  1404
		c4=g_utf8_get_char(g_utf8_offset_to_pointer(t,4));
ali@70
  1405
		c5=g_utf8_get_char(g_utf8_offset_to_pointer(t,5));
ali@99
  1406
		if (CHAR_IS_APOSTROPHE(c2) &&
ali@99
  1407
		  g_unichar_islower(c3) && c4==CHAR_SPACE &&
ali@99
  1408
		  g_unichar_isupper(c5))
ali@53
  1409
		{
ali@70
  1410
		    t=g_utf8_next_char(t);
ali@53
  1411
		    continue;
ali@53
  1412
		}
ali@53
  1413
	    }
ali@70
  1414
	    s1=g_utf8_next_char(g_utf8_next_char(t));
ali@70
  1415
	    while (*s1 && !g_unichar_isalpha(g_utf8_get_char(s1)) &&
ali@70
  1416
	      !isdigit(g_utf8_get_char(s1)))
ali@70
  1417
		s1=g_utf8_next_char(s1);
ali@70
  1418
	    if (g_unichar_islower(g_utf8_get_char(s1)))
ali@53
  1419
	    {
ali@53
  1420
		/* we have something to investigate */
ali@69
  1421
		istypo=TRUE;
ali@53
  1422
		/* so let's go back and find out */
ali@99
  1423
		nc=g_utf8_get_char(t);
ali@99
  1424
		s1=g_utf8_prev_char(t);
ali@99
  1425
		c=g_utf8_get_char(s1);
ali@99
  1426
		sprev=g_utf8_prev_char(s1);
ali@99
  1427
		pc=g_utf8_get_char(sprev);
ali@99
  1428
		while (s1>=aline &&
ali@99
  1429
		  (g_unichar_isalpha(c) || g_unichar_isdigit(c) ||
ali@99
  1430
		  g_unichar_isalpha(pc) && CHAR_IS_APOSTROPHE(c) &&
ali@99
  1431
		  g_unichar_isalpha(nc)))
ali@99
  1432
		{
ali@99
  1433
		    nc=c;
ali@99
  1434
		    s1=sprev;
ali@99
  1435
		    c=pc;
ali@99
  1436
		    sprev=g_utf8_prev_char(s1);
ali@99
  1437
		    pc=g_utf8_get_char(sprev);
ali@99
  1438
		}
ali@70
  1439
		s1=g_utf8_next_char(s1);
ali@69
  1440
		s=strchr(s1,'.');
ali@69
  1441
		if (s)
ali@69
  1442
		    testword=g_strndup(s1,s-s1);
ali@69
  1443
		else
ali@69
  1444
		    testword=g_strdup(s1);
ali@53
  1445
		for (i=0;*abbrev[i];i++)
ali@53
  1446
		    if (!strcmp(testword,abbrev[i]))
ali@69
  1447
			istypo=FALSE;
ali@70
  1448
		if (g_unichar_isdigit(g_utf8_get_char(testword)))
ali@69
  1449
		    istypo=FALSE;
ali@70
  1450
		if (!*g_utf8_next_char(testword))
ali@69
  1451
		    istypo=FALSE;
ali@53
  1452
		if (isroman(testword))
ali@69
  1453
		    istypo=FALSE;
ali@53
  1454
		if (istypo)
ali@53
  1455
		{
ali@69
  1456
		    istypo=FALSE;
ali@70
  1457
		    for (s=testword;*s;s=g_utf8_next_char(s))
ali@70
  1458
		    {
ali@70
  1459
			decomposition=g_unicode_canonical_decomposition(
ali@70
  1460
			  g_utf8_get_char(s),&len);
ali@70
  1461
			if (g_utf8_strchr("aeiou",-1,decomposition[0]))
ali@69
  1462
			    istypo=TRUE;
ali@70
  1463
			g_free(decomposition);
ali@70
  1464
		    }
ali@53
  1465
		}
ali@69
  1466
		if (istypo &&
ali@69
  1467
		  (pswit[VERBOSE_SWITCH] || !g_tree_lookup(qperiod,testword)))
ali@53
  1468
		{
ali@69
  1469
		    g_tree_insert(qperiod,g_strdup(testword),
ali@69
  1470
		      GINT_TO_POINTER(1));
ali@69
  1471
		    if (pswit[ECHO_SWITCH])
ali@70
  1472
			g_print("\n%s\n",aline);
ali@69
  1473
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1474
			g_print("    Line %ld column %ld - Extra period?\n",
ali@70
  1475
			  linecnt,g_utf8_pointer_to_offset(aline,t)+1);
ali@69
  1476
		    else
ali@69
  1477
			cnt_punct++;
ali@53
  1478
		}
ali@69
  1479
		g_free(testword);
ali@53
  1480
	    }
ali@70
  1481
	    t=g_utf8_next_char(t);
ali@53
  1482
	}
ali@53
  1483
    }
ali@53
  1484
}
ali@53
  1485
ali@53
  1486
/*
ali@54
  1487
 * check_for_following_punctuation:
ali@54
  1488
 *
ali@54
  1489
 * Check for words usually not followed by punctuation.
ali@54
  1490
 */
ali@54
  1491
void check_for_following_punctuation(const char *aline)
ali@54
  1492
{
ali@54
  1493
    int i;
ali@54
  1494
    const char *s,*wordstart;
ali@70
  1495
    gunichar c;
ali@69
  1496
    gchar *inword,*t;
ali@54
  1497
    if (pswit[TYPO_SWITCH])
ali@54
  1498
    {
ali@54
  1499
	for (s=aline;*s;)
ali@54
  1500
	{
ali@54
  1501
	    wordstart=s;
ali@69
  1502
	    t=getaword(&s);
ali@69
  1503
	    if (!*t)
ali@69
  1504
	    {
ali@69
  1505
		g_free(t);
ali@54
  1506
		continue;
ali@69
  1507
	    }
ali@70
  1508
	    inword=g_utf8_strdown(t,-1);
ali@69
  1509
	    g_free(t);
ali@54
  1510
	    for (i=0;*nocomma[i];i++)
ali@54
  1511
		if (!strcmp(inword,nocomma[i]))
ali@54
  1512
		{
ali@70
  1513
		    c=g_utf8_get_char(s);
ali@70
  1514
		    if (c==',' || c==';' || c==':')
ali@54
  1515
		    {
ali@54
  1516
			if (pswit[ECHO_SWITCH])
ali@70
  1517
			    g_print("\n%s\n",aline);
ali@54
  1518
			if (!pswit[OVERVIEW_SWITCH])
ali@70
  1519
			    g_print("    Line %ld column %ld - "
ali@54
  1520
			      "Query punctuation after %s?\n",
ali@70
  1521
			      linecnt,g_utf8_pointer_to_offset(aline,s)+1,
ali@70
  1522
			      inword);
ali@54
  1523
			else
ali@54
  1524
			    cnt_punct++;
ali@54
  1525
		    }
ali@54
  1526
		}
ali@54
  1527
	    for (i=0;*noperiod[i];i++)
ali@54
  1528
		if (!strcmp(inword,noperiod[i]))
ali@54
  1529
		{
ali@70
  1530
		    c=g_utf8_get_char(s);
ali@70
  1531
		    if (c=='.' || c=='!')
ali@54
  1532
		    {
ali@54
  1533
			if (pswit[ECHO_SWITCH])
ali@70
  1534
			    g_print("\n%s\n",aline);
ali@54
  1535
			if (!pswit[OVERVIEW_SWITCH])
ali@70
  1536
			    g_print("    Line %ld column %ld - "
ali@54
  1537
			      "Query punctuation after %s?\n",
ali@70
  1538
			      linecnt,g_utf8_pointer_to_offset(aline,s)+1,
ali@70
  1539
			      inword);
ali@54
  1540
			else
ali@54
  1541
			    cnt_punct++;
ali@54
  1542
		    }
ali@54
  1543
		}
ali@69
  1544
	    g_free(inword);
ali@54
  1545
	}
ali@54
  1546
    }
ali@54
  1547
}
ali@54
  1548
ali@54
  1549
/*
ali@55
  1550
 * check_for_typos:
ali@55
  1551
 *
ali@55
  1552
 * Check for commonly mistyped words,
ali@55
  1553
 * and digits like 0 for O in a word.
ali@55
  1554
 */
ali@55
  1555
void check_for_typos(const char *aline,struct warnings *warnings)
ali@55
  1556
{
ali@70
  1557
    const char *s,*t,*nt,*wordstart;
ali@70
  1558
    gchar *inword;
ali@70
  1559
    gunichar *decomposition;
ali@70
  1560
    gchar *testword;
ali@70
  1561
    int i,vowel,consonant,*dupcnt;
ali@70
  1562
    gboolean isdup,istypo,alower;
ali@99
  1563
    gunichar c,pc;
ali@70
  1564
    long offset,len;
ali@70
  1565
    gsize decomposition_len;
ali@55
  1566
    for (s=aline;*s;)
ali@55
  1567
    {
ali@55
  1568
	wordstart=s;
ali@69
  1569
	inword=getaword(&s);
ali@55
  1570
	if (!*inword)
ali@69
  1571
	{
ali@69
  1572
	    g_free(inword);
ali@55
  1573
	    continue; /* don't bother with empty lines */
ali@69
  1574
	}
ali@55
  1575
	if (mixdigit(inword))
ali@55
  1576
	{
ali@55
  1577
	    if (pswit[ECHO_SWITCH])
ali@70
  1578
		g_print("\n%s\n",aline);
ali@55
  1579
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1580
		g_print("    Line %ld column %ld - Query digit in %s\n",
ali@70
  1581
		  linecnt,g_utf8_pointer_to_offset(aline,wordstart)+1,inword);
ali@55
  1582
	    else
ali@55
  1583
		cnt_word++;
ali@55
  1584
	}
ali@55
  1585
	/*
ali@55
  1586
	 * Put the word through a series of tests for likely typos and OCR
ali@55
  1587
	 * errors.
ali@55
  1588
	 */
ali@69
  1589
	if (pswit[TYPO_SWITCH] || pswit[USERTYPO_SWITCH])
ali@55
  1590
	{
ali@69
  1591
	    istypo=FALSE;
ali@70
  1592
	    alower=FALSE;
ali@70
  1593
	    for (t=inword;*t;t=g_utf8_next_char(t))
ali@55
  1594
	    {
ali@70
  1595
		c=g_utf8_get_char(t);
ali@70
  1596
		nt=g_utf8_next_char(t);
ali@55
  1597
		/* lowercase for testing */
ali@70
  1598
		if (g_unichar_islower(c))
ali@70
  1599
		    alower=TRUE;
ali@70
  1600
		if (alower && (g_unichar_isupper(c) || g_unichar_istitle(c)))
ali@55
  1601
		{
ali@55
  1602
		    /*
ali@55
  1603
		     * We have an uppercase mid-word. However, there are
ali@55
  1604
		     * common cases:
ali@55
  1605
		     *   Mac and Mc like McGill
ali@55
  1606
		     *   French contractions like l'Abbe
ali@55
  1607
		     */
ali@70
  1608
		    offset=g_utf8_pointer_to_offset(inword,t);
ali@99
  1609
		    if (offset>0)
ali@99
  1610
			pc=g_utf8_get_char(g_utf8_prev_char(t));
ali@99
  1611
		    else
ali@99
  1612
			pc='\0';
ali@70
  1613
		    if (offset==2 && c=='m' && g_utf8_get_char(nt)=='c' ||
ali@70
  1614
		      offset==3 && c=='m' && g_utf8_get_char(nt)=='a' &&
ali@70
  1615
		      g_utf8_get_char(g_utf8_next_char(nt))=='c' ||
ali@99
  1616
		      CHAR_IS_APOSTROPHE(pc))
ali@55
  1617
			; /* do nothing! */
ali@55
  1618
		    else
ali@69
  1619
			istypo=TRUE;
ali@55
  1620
		}
ali@55
  1621
	    }
ali@70
  1622
	    testword=g_utf8_casefold(inword,-1);
ali@69
  1623
	}
ali@69
  1624
	if (pswit[TYPO_SWITCH])
ali@69
  1625
	{
ali@55
  1626
	    /*
ali@55
  1627
	     * Check for certain unlikely two-letter combinations at word
ali@55
  1628
	     * start and end.
ali@55
  1629
	     */
ali@70
  1630
	    len=g_utf8_strlen(testword,-1);
ali@70
  1631
	    if (len>1)
ali@55
  1632
	    {
ali@55
  1633
		for (i=0;*nostart[i];i++)
ali@70
  1634
		    if (g_str_has_prefix(testword,nostart[i]))
ali@69
  1635
			istypo=TRUE;
ali@55
  1636
		for (i=0;*noend[i];i++)
ali@70
  1637
		    if (g_str_has_suffix(testword,noend[i]))
ali@69
  1638
			istypo=TRUE;
ali@55
  1639
	    }
ali@55
  1640
	    /* ght is common, gbt never. Like that. */
ali@55
  1641
	    if (strstr(testword,"cb"))
ali@69
  1642
		istypo=TRUE;
ali@55
  1643
	    if (strstr(testword,"gbt"))
ali@69
  1644
		istypo=TRUE;
ali@55
  1645
	    if (strstr(testword,"pbt"))
ali@69
  1646
		istypo=TRUE;
ali@55
  1647
	    if (strstr(testword,"tbs"))
ali@69
  1648
		istypo=TRUE;
ali@55
  1649
	    if (strstr(testword,"mrn"))
ali@69
  1650
		istypo=TRUE;
ali@55
  1651
	    if (strstr(testword,"ahle"))
ali@69
  1652
		istypo=TRUE;
ali@55
  1653
	    if (strstr(testword,"ihle"))
ali@69
  1654
		istypo=TRUE;
ali@55
  1655
	    /*
ali@55
  1656
	     * "TBE" does happen - like HEARTBEAT - but uncommon.
ali@55
  1657
	     * Also "TBI" - frostbite, outbid - but uncommon.
ali@55
  1658
	     * Similarly "ii" like Hawaii, or Pompeii, and in Roman
ali@55
  1659
	     * numerals, but "ii" is a common scanno.
ali@55
  1660
	     */
ali@55
  1661
	    if (strstr(testword,"tbi"))
ali@69
  1662
		istypo=TRUE;
ali@55
  1663
	    if (strstr(testword,"tbe"))
ali@69
  1664
		istypo=TRUE;
ali@55
  1665
	    if (strstr(testword,"ii"))
ali@69
  1666
		istypo=TRUE;
ali@55
  1667
	    /*
ali@55
  1668
	     * Check for no vowels or no consonants.
ali@55
  1669
	     * If none, flag a typo.
ali@55
  1670
	     */
ali@70
  1671
	    if (!istypo && len>1)
ali@55
  1672
	    {
ali@55
  1673
		vowel=consonant=0;
ali@70
  1674
		for (t=testword;*t;t=g_utf8_next_char(t))
ali@55
  1675
		{
ali@70
  1676
		    c=g_utf8_get_char(t);
ali@70
  1677
		    decomposition=
ali@70
  1678
		      g_unicode_canonical_decomposition(c,&decomposition_len);
ali@70
  1679
		    if (c=='y' || g_unichar_isdigit(c))
ali@55
  1680
		    {
ali@55
  1681
			/* Yah, this is loose. */
ali@55
  1682
			vowel++;
ali@55
  1683
			consonant++;
ali@55
  1684
		    }
ali@70
  1685
		    else if (g_utf8_strchr("aeiou",-1,decomposition[0]))
ali@55
  1686
			vowel++;
ali@55
  1687
		    else
ali@55
  1688
			consonant++;
ali@70
  1689
		    g_free(decomposition);
ali@55
  1690
		}
ali@55
  1691
		if (!vowel || !consonant)
ali@69
  1692
		    istypo=TRUE;
ali@55
  1693
	    }
ali@55
  1694
	    /*
ali@55
  1695
	     * Now exclude the word from being reported if it's in
ali@55
  1696
	     * the okword list.
ali@55
  1697
	     */
ali@55
  1698
	    for (i=0;*okword[i];i++)
ali@55
  1699
		if (!strcmp(testword,okword[i]))
ali@69
  1700
		    istypo=FALSE;
ali@55
  1701
	    /*
ali@55
  1702
	     * What looks like a typo may be a Roman numeral.
ali@55
  1703
	     * Exclude these.
ali@55
  1704
	     */
ali@55
  1705
	    if (istypo && isroman(testword))
ali@69
  1706
		istypo=FALSE;
ali@55
  1707
	    /* Check the manual list of typos. */
ali@55
  1708
	    if (!istypo)
ali@55
  1709
		for (i=0;*typo[i];i++)
ali@55
  1710
		    if (!strcmp(testword,typo[i]))
ali@69
  1711
			istypo=TRUE;
ali@55
  1712
	    /*
ali@55
  1713
	     * Check lowercase s, l, i and m - special cases.
ali@55
  1714
	     *   "j" - often a semi-colon gone wrong.
ali@55
  1715
	     *   "d" for a missing apostrophe - he d
ali@55
  1716
	     *   "n" for "in"
ali@55
  1717
	     */
ali@70
  1718
	    if (!istypo && len==1 &&
ali@70
  1719
	      g_utf8_strchr("slmijdn",-1,g_utf8_get_char(inword)))
ali@69
  1720
		istypo=TRUE;
ali@55
  1721
	    if (istypo)
ali@55
  1722
	    {
ali@69
  1723
		dupcnt=g_tree_lookup(qword,testword);
ali@69
  1724
		if (dupcnt)
ali@69
  1725
		{
ali@69
  1726
		    (*dupcnt)++;
ali@69
  1727
		    isdup=!pswit[VERBOSE_SWITCH];
ali@69
  1728
		}
ali@69
  1729
		else
ali@69
  1730
		{
ali@69
  1731
		    dupcnt=g_new0(int,1);
ali@69
  1732
		    g_tree_insert(qword,g_strdup(testword),dupcnt);
ali@69
  1733
		    isdup=FALSE;
ali@69
  1734
		}
ali@55
  1735
		if (!isdup)
ali@55
  1736
		{
ali@55
  1737
		    if (pswit[ECHO_SWITCH])
ali@70
  1738
			g_print("\n%s\n",aline);
ali@55
  1739
		    if (!pswit[OVERVIEW_SWITCH])
ali@55
  1740
		    {
ali@70
  1741
			g_print("    Line %ld column %ld - Query word %s",
ali@70
  1742
			  linecnt,g_utf8_pointer_to_offset(aline,wordstart)+1,
ali@70
  1743
			  inword);
ali@69
  1744
			if (!pswit[VERBOSE_SWITCH])
ali@70
  1745
			    g_print(" - not reporting duplicates");
ali@70
  1746
			g_print("\n");
ali@55
  1747
		    }
ali@55
  1748
		    else
ali@55
  1749
			cnt_word++;
ali@55
  1750
		}
ali@55
  1751
	    }
ali@55
  1752
	}
ali@55
  1753
	/* check the user's list of typos */
ali@69
  1754
	if (!istypo && usertypo && g_tree_lookup(usertypo,testword))
ali@69
  1755
	{
ali@69
  1756
	    if (pswit[ECHO_SWITCH])
ali@70
  1757
		g_print("\n%s\n",aline);
ali@69
  1758
	    if (!pswit[OVERVIEW_SWITCH])  
ali@70
  1759
		g_print("    Line %ld column %ld - Query possible scanno %s\n",
ali@70
  1760
		  linecnt,g_utf8_pointer_to_offset(aline,wordstart)+2,inword);
ali@69
  1761
	}
ali@69
  1762
	if (pswit[TYPO_SWITCH] || pswit[USERTYPO_SWITCH])
ali@69
  1763
	    g_free(testword);
ali@55
  1764
	if (pswit[PARANOID_SWITCH] && warnings->digit)
ali@55
  1765
	{
ali@55
  1766
	    /* In paranoid mode, query all 0 and 1 standing alone. */
ali@55
  1767
	    if (!strcmp(inword,"0") || !strcmp(inword,"1"))
ali@55
  1768
	    {
ali@55
  1769
		if (pswit[ECHO_SWITCH])
ali@70
  1770
		    g_print("\n%s\n",aline);
ali@55
  1771
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  1772
		    g_print("    Line %ld column %ld - Query standalone %s\n",
ali@70
  1773
		      linecnt,g_utf8_pointer_to_offset(aline,wordstart)+2,
ali@70
  1774
		      inword);
ali@55
  1775
		else
ali@55
  1776
		    cnt_word++;
ali@55
  1777
	    }
ali@55
  1778
	}
ali@69
  1779
	g_free(inword);
ali@55
  1780
    }
ali@55
  1781
}
ali@55
  1782
ali@56
  1783
/*
ali@56
  1784
 * check_for_misspaced_punctuation:
ali@56
  1785
 *
ali@56
  1786
 * Look for added or missing spaces around punctuation and quotes.
ali@56
  1787
 * If there is a punctuation character like ! with no space on
ali@56
  1788
 * either side, suspect a missing!space. If there are spaces on
ali@56
  1789
 * both sides , assume a typo. If we see a double quote with no
ali@56
  1790
 * space or punctuation on either side of it, assume unspaced
ali@56
  1791
 * quotes "like"this.
ali@56
  1792
 */
ali@56
  1793
void check_for_misspaced_punctuation(const char *aline,
ali@69
  1794
  struct parities *parities,gboolean isemptyline)
ali@56
  1795
{
ali@69
  1796
    gboolean isacro,isellipsis;
ali@56
  1797
    const char *s;
ali@70
  1798
    gunichar c,nc,pc,n2c;
ali@142
  1799
    int parity;
ali@70
  1800
    c=g_utf8_get_char(aline);
ali@70
  1801
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  1802
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@56
  1803
    {
ali@70
  1804
	pc=c;
ali@70
  1805
	c=nc;
ali@70
  1806
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@56
  1807
	/* For each character in the line after the first. */
ali@70
  1808
	if (g_utf8_strchr(".?!,;:_",-1,c))  /* if it's punctuation */
ali@56
  1809
	{
ali@56
  1810
	    /* we need to suppress warnings for acronyms like M.D. */
ali@69
  1811
	    isacro=FALSE;
ali@56
  1812
	    /* we need to suppress warnings for ellipsis . . . */
ali@69
  1813
	    isellipsis=FALSE;
ali@70
  1814
	    /*
ali@70
  1815
	     * If there are letters on both sides of it or
ali@70
  1816
	     * if it's strict punctuation followed by an alpha.
ali@70
  1817
	     */
ali@70
  1818
	    if (g_unichar_isalpha(nc) && (g_unichar_isalpha(pc) ||
ali@70
  1819
	      g_utf8_strchr("?!,;:",-1,c)))
ali@56
  1820
	    {
ali@70
  1821
		if (c=='.')
ali@56
  1822
		{
ali@70
  1823
		    if (g_utf8_pointer_to_offset(aline,s)>2 &&
ali@70
  1824
		      g_utf8_get_char(g_utf8_offset_to_pointer(s,-2))=='.')
ali@69
  1825
			isacro=TRUE;
ali@70
  1826
		    n2c=g_utf8_get_char(g_utf8_next_char(g_utf8_next_char(s)));
ali@70
  1827
		    if (nc && n2c=='.')
ali@69
  1828
			isacro=TRUE;
ali@56
  1829
		}
ali@56
  1830
		if (!isacro)
ali@56
  1831
		{
ali@56
  1832
		    if (pswit[ECHO_SWITCH])
ali@70
  1833
			g_print("\n%s\n",aline);
ali@56
  1834
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1835
			g_print("    Line %ld column %ld - Missing space?\n",
ali@70
  1836
			  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1837
		    else
ali@56
  1838
			cnt_punct++;
ali@56
  1839
		}
ali@56
  1840
	    }
ali@70
  1841
	    if (pc==CHAR_SPACE && (nc==CHAR_SPACE || !nc))
ali@56
  1842
	    {
ali@56
  1843
		/*
ali@56
  1844
		 * If there are spaces on both sides,
ali@56
  1845
		 * or space before and end of line.
ali@56
  1846
		 */
ali@70
  1847
		if (c=='.')
ali@56
  1848
		{
ali@70
  1849
		    if (g_utf8_pointer_to_offset(aline,s)>2 &&
ali@70
  1850
		      g_utf8_get_char(g_utf8_offset_to_pointer(s,-2))=='.')
ali@69
  1851
			isellipsis=TRUE;
ali@70
  1852
		    n2c=g_utf8_get_char(g_utf8_next_char(g_utf8_next_char(s)));
ali@70
  1853
		    if (nc && n2c=='.')
ali@69
  1854
			isellipsis=TRUE;
ali@56
  1855
		}
ali@56
  1856
		if (!isemptyline && !isellipsis)
ali@56
  1857
		{
ali@56
  1858
		    if (pswit[ECHO_SWITCH])
ali@70
  1859
			g_print("\n%s\n",aline);
ali@56
  1860
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1861
			g_print("    Line %ld column %ld - "
ali@70
  1862
			  "Spaced punctuation?\n",linecnt,
ali@70
  1863
			  g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1864
		    else
ali@56
  1865
			cnt_punct++;
ali@56
  1866
		}
ali@56
  1867
	    }
ali@56
  1868
	}
ali@56
  1869
    }
ali@56
  1870
    /* Split out the characters that CANNOT be preceded by space. */
ali@70
  1871
    c=g_utf8_get_char(aline);
ali@70
  1872
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  1873
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@56
  1874
    {
ali@70
  1875
	pc=c;
ali@70
  1876
	c=nc;
ali@70
  1877
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@56
  1878
	/* for each character in the line after the first */
ali@70
  1879
	if (g_utf8_strchr("?!,;:",-1,c))
ali@56
  1880
	{
ali@56
  1881
	    /* if it's punctuation that _cannot_ have a space before it */
ali@70
  1882
	    if (pc==CHAR_SPACE && !isemptyline && nc!=CHAR_SPACE)
ali@56
  1883
	    {
ali@56
  1884
		/*
ali@70
  1885
		 * If nc DOES == space,
ali@56
  1886
		 * it was already reported just above.
ali@56
  1887
		 */
ali@56
  1888
		if (pswit[ECHO_SWITCH])
ali@70
  1889
		    g_print("\n%s\n",aline);
ali@56
  1890
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  1891
		    g_print("    Line %ld column %ld - Spaced punctuation?\n",
ali@70
  1892
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1893
		else
ali@56
  1894
		    cnt_punct++;
ali@56
  1895
	    }
ali@56
  1896
	}
ali@56
  1897
    }
ali@56
  1898
    /*
ali@56
  1899
     * Special case " .X" where X is any alpha.
ali@56
  1900
     * This plugs a hole in the acronym code above.
ali@56
  1901
     * Inelegant, but maintainable.
ali@56
  1902
     */
ali@70
  1903
    c=g_utf8_get_char(aline);
ali@70
  1904
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  1905
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@56
  1906
    {
ali@70
  1907
	pc=c;
ali@70
  1908
	c=nc;
ali@70
  1909
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@56
  1910
	/* for each character in the line after the first */
ali@70
  1911
	if (c=='.')
ali@56
  1912
	{
ali@56
  1913
	    /* if it's a period */
ali@70
  1914
	    if (pc==CHAR_SPACE && g_unichar_isalpha(nc))
ali@56
  1915
	    {
ali@56
  1916
		/*
ali@56
  1917
		 * If the period follows a space and
ali@56
  1918
		 * is followed by a letter.
ali@56
  1919
		 */
ali@56
  1920
		if (pswit[ECHO_SWITCH])
ali@70
  1921
		    g_print("\n%s\n",aline);
ali@56
  1922
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  1923
		    g_print("    Line %ld column %ld - Spaced punctuation?\n",
ali@70
  1924
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1925
		else
ali@56
  1926
		    cnt_punct++;
ali@56
  1927
	    }
ali@56
  1928
	}
ali@56
  1929
    }
ali@70
  1930
    c=g_utf8_get_char(aline);
ali@70
  1931
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  1932
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@56
  1933
    {
ali@70
  1934
	pc=c;
ali@70
  1935
	c=nc;
ali@70
  1936
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@56
  1937
	/* for each character in the line after the first */
ali@142
  1938
	if (CHAR_IS_DQUOTE(c))
ali@56
  1939
	{
ali@70
  1940
	    if (!g_utf8_strchr(" _-.'`,;:!/([{?}])",-1,pc) &&
ali@70
  1941
	      !g_utf8_strchr(" _-.'`,;:!/([{?}])",-1,nc) && nc ||
ali@70
  1942
	      !g_utf8_strchr(" _-([{'`",-1,pc) && g_unichar_isalpha(nc))
ali@56
  1943
	    {
ali@56
  1944
		if (pswit[ECHO_SWITCH])
ali@70
  1945
		    g_print("\n%s\n",aline);
ali@56
  1946
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  1947
		    g_print("    Line %ld column %ld - Unspaced quotes?\n",
ali@70
  1948
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1949
		else
ali@56
  1950
		    cnt_punct++;
ali@56
  1951
	    }
ali@56
  1952
	}
ali@56
  1953
    }
ali@56
  1954
    /* Check parity of quotes. */
ali@70
  1955
    nc=g_utf8_get_char(aline);
ali@70
  1956
    for (s=aline;*s;s=g_utf8_next_char(s))
ali@56
  1957
    {
ali@70
  1958
	c=nc;
ali@70
  1959
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@142
  1960
	if (CHAR_IS_DQUOTE(c))
ali@56
  1961
	{
ali@142
  1962
	    if (c==CHAR_DQUOTE)
ali@142
  1963
	    {
ali@142
  1964
		parities->dquote=!parities->dquote;
ali@142
  1965
		parity=parities->dquote;
ali@142
  1966
	    }
ali@142
  1967
	    else if (c==CHAR_LD_QUOTE)
ali@142
  1968
		parity=1;
ali@142
  1969
	    else
ali@142
  1970
		parity=0;
ali@142
  1971
	    if (!parity)
ali@56
  1972
	    {
ali@56
  1973
		/* parity even */
ali@70
  1974
		if (!g_utf8_strchr("_-.'`/,;:!?)]} ",-1,nc))
ali@56
  1975
		{
ali@56
  1976
		    if (pswit[ECHO_SWITCH])
ali@70
  1977
			g_print("\n%s\n",aline);
ali@56
  1978
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1979
			g_print("    Line %ld column %ld - "
ali@70
  1980
			  "Wrongspaced quotes?\n",
ali@70
  1981
			  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1982
		    else
ali@56
  1983
			cnt_punct++;
ali@56
  1984
		}
ali@56
  1985
	    }
ali@56
  1986
	    else
ali@56
  1987
	    {
ali@56
  1988
		/* parity odd */
ali@70
  1989
		if (!g_unichar_isalpha(nc) && !isdigit(nc) &&
ali@70
  1990
		  !g_utf8_strchr("_-/.'`([{$",-1,nc) || !nc)
ali@56
  1991
		{
ali@56
  1992
		    if (pswit[ECHO_SWITCH])
ali@70
  1993
			g_print("\n%s\n",aline);
ali@56
  1994
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1995
			g_print("    Line %ld column %ld - "
ali@70
  1996
			  "Wrongspaced quotes?\n",
ali@70
  1997
			  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1998
		    else
ali@56
  1999
			cnt_punct++;
ali@56
  2000
		}
ali@56
  2001
	    }
ali@56
  2002
	}
ali@56
  2003
    }
ali@142
  2004
    c=g_utf8_get_char(aline);
ali@142
  2005
    if (CHAR_IS_DQUOTE(c))
ali@56
  2006
    {
ali@70
  2007
	if (g_utf8_strchr(",;:!?)]} ",-1,
ali@70
  2008
	  g_utf8_get_char(g_utf8_next_char(aline))))
ali@56
  2009
	{
ali@56
  2010
	    if (pswit[ECHO_SWITCH])
ali@70
  2011
		g_print("\n%s\n",aline);
ali@56
  2012
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2013
		g_print("    Line %ld column 1 - Wrongspaced quotes?\n",
ali@56
  2014
		  linecnt);
ali@56
  2015
	    else
ali@56
  2016
		cnt_punct++;
ali@56
  2017
	}
ali@56
  2018
    }
ali@56
  2019
    if (pswit[SQUOTE_SWITCH])
ali@56
  2020
    {
ali@70
  2021
	nc=g_utf8_get_char(aline);
ali@70
  2022
	for (s=aline;*s;s=g_utf8_next_char(s))
ali@56
  2023
	{
ali@70
  2024
	    c=nc;
ali@70
  2025
	    nc=g_utf8_get_char(g_utf8_next_char(s));
ali@99
  2026
	    if (CHAR_IS_SQUOTE(c) && (s==aline || s>aline &&
ali@70
  2027
	      !g_unichar_isalpha(g_utf8_get_char(g_utf8_prev_char(s))) ||
ali@70
  2028
	      !g_unichar_isalpha(nc)))
ali@56
  2029
	    {
ali@56
  2030
		parities->squote=!parities->squote;
ali@56
  2031
		if (!parities->squote)
ali@56
  2032
		{
ali@56
  2033
		    /* parity even */
ali@70
  2034
		    if (!g_utf8_strchr("_-.'`/\",;:!?)]} ",-1,nc))
ali@56
  2035
		    {
ali@56
  2036
			if (pswit[ECHO_SWITCH])
ali@70
  2037
			    g_print("\n%s\n",aline);
ali@56
  2038
			if (!pswit[OVERVIEW_SWITCH])
ali@70
  2039
			    g_print("    Line %ld column %ld - "
ali@56
  2040
			      "Wrongspaced singlequotes?\n",
ali@70
  2041
			      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  2042
			else
ali@56
  2043
			    cnt_punct++;
ali@56
  2044
		    }
ali@56
  2045
		}
ali@56
  2046
		else
ali@56
  2047
		{
ali@56
  2048
		    /* parity odd */
ali@70
  2049
		    if (!g_unichar_isalpha(nc) && !isdigit(nc) &&
ali@70
  2050
		      !g_utf8_strchr("_-/\".'`",-1,nc) || !nc)
ali@56
  2051
		    {
ali@56
  2052
			if (pswit[ECHO_SWITCH])
ali@70
  2053
			    g_print("\n%s\n",aline);
ali@56
  2054
			if (!pswit[OVERVIEW_SWITCH])
ali@70
  2055
			    g_print("    Line %ld column %ld - "
ali@56
  2056
			      "Wrongspaced singlequotes?\n",
ali@70
  2057
			      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  2058
			else
ali@56
  2059
			    cnt_punct++;
ali@56
  2060
		    }
ali@56
  2061
		}
ali@56
  2062
	    }
ali@56
  2063
	}
ali@56
  2064
    }
ali@56
  2065
}
ali@56
  2066
ali@55
  2067
/*
ali@57
  2068
 * check_for_double_punctuation:
ali@57
  2069
 *
ali@57
  2070
 * Look for double punctuation like ,. or ,,
ali@57
  2071
 * Thanks to DW for the suggestion!
ali@57
  2072
 * In books with references, ".," and ".;" are common
ali@57
  2073
 * e.g. "etc., etc.," and vol. 1.; vol 3.;
ali@57
  2074
 * OTOH, from my initial tests, there are also fairly
ali@57
  2075
 * common errors. What to do? Make these cases paranoid?
ali@57
  2076
 * ".," is the most common, so warnings->dotcomma is used
ali@57
  2077
 * to suppress detailed reporting if it occurs often.
ali@57
  2078
 */
ali@57
  2079
void check_for_double_punctuation(const char *aline,struct warnings *warnings)
ali@57
  2080
{
ali@70
  2081
    const char *s;
ali@70
  2082
    gunichar c,nc;
ali@70
  2083
    nc=g_utf8_get_char(aline);
ali@70
  2084
    for (s=aline;*s;s=g_utf8_next_char(s))
ali@57
  2085
    {
ali@70
  2086
	c=nc;
ali@70
  2087
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@57
  2088
	/* for each punctuation character in the line */
ali@70
  2089
	if (c && nc && g_utf8_strchr(".?!,;:",-1,c) &&
ali@70
  2090
	  g_utf8_strchr(".?!,;:",-1,nc))
ali@57
  2091
	{
ali@57
  2092
	    /* followed by punctuation, it's a query, unless . . . */
ali@70
  2093
	    if (c==nc && (c=='.' || c=='?' || c=='!') ||
ali@70
  2094
	      !warnings->dotcomma && c=='.' && nc==',' ||
ali@70
  2095
	      warnings->isFrench && g_str_has_prefix(s,",...") ||
ali@70
  2096
	      warnings->isFrench && g_str_has_prefix(s,"...,") ||
ali@70
  2097
	      warnings->isFrench && g_str_has_prefix(s,";...") ||
ali@70
  2098
	      warnings->isFrench && g_str_has_prefix(s,"...;") ||
ali@70
  2099
	      warnings->isFrench && g_str_has_prefix(s,":...") ||
ali@70
  2100
	      warnings->isFrench && g_str_has_prefix(s,"...:") ||
ali@70
  2101
	      warnings->isFrench && g_str_has_prefix(s,"!...") ||
ali@70
  2102
	      warnings->isFrench && g_str_has_prefix(s,"...!") ||
ali@70
  2103
	      warnings->isFrench && g_str_has_prefix(s,"?...") ||
ali@70
  2104
	      warnings->isFrench && g_str_has_prefix(s,"...?"))
ali@57
  2105
	    {
ali@70
  2106
		if (warnings->isFrench && g_str_has_prefix(s,",...") ||
ali@70
  2107
		  warnings->isFrench && g_str_has_prefix(s,"...,") ||
ali@70
  2108
		  warnings->isFrench && g_str_has_prefix(s,";...") ||
ali@70
  2109
		  warnings->isFrench && g_str_has_prefix(s,"...;") ||
ali@70
  2110
		  warnings->isFrench && g_str_has_prefix(s,":...") ||
ali@70
  2111
		  warnings->isFrench && g_str_has_prefix(s,"...:") ||
ali@70
  2112
		  warnings->isFrench && g_str_has_prefix(s,"!...") ||
ali@70
  2113
		  warnings->isFrench && g_str_has_prefix(s,"...!") ||
ali@70
  2114
		  warnings->isFrench && g_str_has_prefix(s,"?...") ||
ali@70
  2115
		  warnings->isFrench && g_str_has_prefix(s,"...?"))
ali@70
  2116
		{
ali@70
  2117
		    s+=4;
ali@70
  2118
		    nc=g_utf8_get_char(g_utf8_next_char(s));
ali@70
  2119
		}
ali@57
  2120
		; /* do nothing for .. !! and ?? which can be legit */
ali@57
  2121
	    }
ali@57
  2122
	    else
ali@57
  2123
	    {
ali@57
  2124
		if (pswit[ECHO_SWITCH])
ali@70
  2125
		    g_print("\n%s\n",aline);
ali@57
  2126
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2127
		    g_print("    Line %ld column %ld - Double punctuation?\n",
ali@70
  2128
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@57
  2129
		else
ali@57
  2130
		    cnt_punct++;
ali@57
  2131
	    }
ali@57
  2132
	}
ali@57
  2133
    }
ali@57
  2134
}
ali@57
  2135
ali@57
  2136
/*
ali@58
  2137
 * check_for_spaced_quotes:
ali@58
  2138
 */
ali@58
  2139
void check_for_spaced_quotes(const char *aline)
ali@58
  2140
{
ali@99
  2141
    int i;
ali@58
  2142
    const char *s,*t;
ali@99
  2143
    const gunichar single_quotes[]={CHAR_SQUOTE,CHAR_OPEN_SQUOTE,CHAR_LS_QUOTE,
ali@99
  2144
      CHAR_RS_QUOTE};
ali@99
  2145
    GString *pattern;
ali@58
  2146
    s=aline;
ali@58
  2147
    while ((t=strstr(s," \" ")))
ali@58
  2148
    {
ali@58
  2149
	if (pswit[ECHO_SWITCH])
ali@70
  2150
	    g_print("\n%s\n",aline);
ali@58
  2151
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  2152
	    g_print("    Line %ld column %ld - Spaced doublequote?\n",
ali@70
  2153
	      linecnt,g_utf8_pointer_to_offset(aline,t)+1);
ali@58
  2154
	else
ali@58
  2155
	    cnt_punct++;
ali@70
  2156
	s=g_utf8_next_char(g_utf8_next_char(t));
ali@58
  2157
    }
ali@99
  2158
    pattern=g_string_new(NULL);
ali@99
  2159
    for(i=0;i<G_N_ELEMENTS(single_quotes);i++)
ali@58
  2160
    {
ali@99
  2161
	g_string_assign(pattern," ");
ali@99
  2162
	g_string_append_unichar(pattern,single_quotes[i]);
ali@99
  2163
	g_string_append_c(pattern,' ');
ali@99
  2164
	s=aline;
ali@99
  2165
	while ((t=strstr(s,pattern->str)))
ali@99
  2166
	{
ali@99
  2167
	    if (pswit[ECHO_SWITCH])
ali@99
  2168
		g_print("\n%s\n",aline);
ali@99
  2169
	    if (!pswit[OVERVIEW_SWITCH])
ali@99
  2170
		g_print("    Line %ld column %ld - Spaced singlequote?\n",
ali@99
  2171
		  linecnt,g_utf8_pointer_to_offset(aline,t)+1);
ali@99
  2172
	    else
ali@99
  2173
		cnt_punct++;
ali@99
  2174
	    s=g_utf8_next_char(g_utf8_next_char(t));
ali@99
  2175
	}
ali@58
  2176
    }
ali@99
  2177
    g_string_free(pattern,TRUE);
ali@58
  2178
}
ali@58
  2179
ali@58
  2180
/*
ali@59
  2181
 * check_for_miscased_genative:
ali@59
  2182
 *
ali@59
  2183
 * Check special case of 'S instead of 's at end of word.
ali@59
  2184
 */
ali@59
  2185
void check_for_miscased_genative(const char *aline)
ali@59
  2186
{
ali@59
  2187
    const char *s;
ali@70
  2188
    gunichar c,nc,pc;
ali@69
  2189
    if (!*aline)
ali@69
  2190
	return;
ali@70
  2191
    c=g_utf8_get_char(aline);
ali@70
  2192
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  2193
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@59
  2194
    {
ali@70
  2195
	pc=c;
ali@70
  2196
	c=nc;
ali@70
  2197
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@99
  2198
	if (CHAR_IS_APOSTROPHE(c) && nc=='S' && g_unichar_islower(pc))
ali@59
  2199
	{
ali@59
  2200
	    if (pswit[ECHO_SWITCH])
ali@70
  2201
		g_print("\n%s\n",aline);
ali@59
  2202
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2203
		g_print("    Line %ld column %ld - Capital \"S\"?\n",
ali@70
  2204
		  linecnt,g_utf8_pointer_to_offset(aline,s)+2);
ali@59
  2205
	    else
ali@59
  2206
		cnt_punct++;
ali@59
  2207
	}
ali@59
  2208
    }
ali@59
  2209
}
ali@59
  2210
ali@59
  2211
/*
ali@60
  2212
 * check_end_of_line:
ali@60
  2213
 *
ali@60
  2214
 * Now check special cases - start and end of line -
ali@60
  2215
 * for single and double quotes. Start is sometimes [sic]
ali@60
  2216
 * but better to query it anyway.
ali@60
  2217
 * While we're here, check for dash at end of line.
ali@60
  2218
 */
ali@60
  2219
void check_end_of_line(const char *aline,struct warnings *warnings)
ali@60
  2220
{
ali@70
  2221
    int lbytes;
ali@70
  2222
    const char *s;
ali@70
  2223
    gunichar c1,c2;
ali@70
  2224
    lbytes=strlen(aline);
ali@70
  2225
    if (g_utf8_strlen(aline,lbytes)>1)
ali@60
  2226
    {
ali@70
  2227
	s=g_utf8_prev_char(aline+lbytes);
ali@70
  2228
	c1=g_utf8_get_char(s);
ali@70
  2229
	c2=g_utf8_get_char(g_utf8_prev_char(s));
ali@142
  2230
	if ((CHAR_IS_DQUOTE(c1) || CHAR_IS_SQUOTE(c1)) && c2==CHAR_SPACE)
ali@60
  2231
	{
ali@60
  2232
	    if (pswit[ECHO_SWITCH])
ali@70
  2233
		g_print("\n%s\n",aline);
ali@60
  2234
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2235
		g_print("    Line %ld column %ld - Spaced quote?\n",linecnt,
ali@70
  2236
		  g_utf8_strlen(aline,lbytes));
ali@70
  2237
	    else
ali@70
  2238
		cnt_punct++;
ali@70
  2239
	}
ali@70
  2240
	c1=g_utf8_get_char(aline);
ali@70
  2241
	c2=g_utf8_get_char(g_utf8_next_char(aline));
ali@99
  2242
	if (CHAR_IS_SQUOTE(c1) && c2==CHAR_SPACE)
ali@70
  2243
	{
ali@70
  2244
	    if (pswit[ECHO_SWITCH])
ali@70
  2245
		g_print("\n%s\n",aline);
ali@70
  2246
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2247
		g_print("    Line %ld column 1 - Spaced quote?\n",linecnt);
ali@60
  2248
	    else
ali@60
  2249
		cnt_punct++;
ali@60
  2250
	}
ali@60
  2251
	/*
ali@60
  2252
	 * Dash at end of line may well be legit - paranoid mode only
ali@60
  2253
	 * and don't report em-dash at line-end.
ali@60
  2254
	 */
ali@60
  2255
	if (pswit[PARANOID_SWITCH] && warnings->hyphen)
ali@60
  2256
	{
ali@70
  2257
	    for (s=g_utf8_prev_char(aline+lbytes);
ali@70
  2258
	      s>aline && g_utf8_get_char(s)<=CHAR_SPACE;s=g_utf8_prev_char(s))
ali@60
  2259
		;
ali@70
  2260
	    if (g_utf8_get_char(s)=='-' &&
ali@70
  2261
	      g_utf8_get_char(g_utf8_prev_char(s))!='-')
ali@60
  2262
	    {
ali@60
  2263
		if (pswit[ECHO_SWITCH])
ali@70
  2264
		    g_print("\n%s\n",aline);
ali@60
  2265
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2266
		    g_print("    Line %ld column %ld - "
ali@70
  2267
		      "Hyphen at end of line?\n",
ali@70
  2268
		      linecnt,g_utf8_pointer_to_offset(aline,s));
ali@60
  2269
	    }
ali@60
  2270
	}
ali@60
  2271
    }
ali@60
  2272
}
ali@60
  2273
ali@60
  2274
/*
ali@61
  2275
 * check_for_unspaced_bracket:
ali@61
  2276
 *
ali@61
  2277
 * Brackets are often unspaced, but shouldn't be surrounded by alpha.
ali@61
  2278
 * If so, suspect a scanno like "a]most".
ali@61
  2279
 */
ali@61
  2280
void check_for_unspaced_bracket(const char *aline)
ali@61
  2281
{
ali@70
  2282
    const char *s;
ali@70
  2283
    gunichar c,nc,pc;
ali@70
  2284
    c=g_utf8_get_char(aline);
ali@70
  2285
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  2286
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@61
  2287
    {
ali@70
  2288
	pc=c;
ali@70
  2289
	c=nc;
ali@70
  2290
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@70
  2291
	if (!nc)
ali@70
  2292
	    break;
ali@61
  2293
	/* for each bracket character in the line except 1st & last */
ali@70
  2294
	if (g_utf8_strchr("{[()]}",-1,c) &&
ali@70
  2295
	  g_unichar_isalpha(pc) && g_unichar_isalpha(nc))
ali@61
  2296
	{
ali@61
  2297
	    if (pswit[ECHO_SWITCH])
ali@70
  2298
		g_print("\n%s\n",aline);
ali@61
  2299
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2300
		g_print("    Line %ld column %ld - Unspaced bracket?\n",
ali@70
  2301
		  linecnt,g_utf8_pointer_to_offset(aline,s));
ali@61
  2302
	    else
ali@61
  2303
		cnt_punct++;
ali@61
  2304
	}
ali@61
  2305
    }
ali@61
  2306
}
ali@61
  2307
ali@61
  2308
/*
ali@62
  2309
 * check_for_unpunctuated_endquote:
ali@62
  2310
 */
ali@62
  2311
void check_for_unpunctuated_endquote(const char *aline)
ali@62
  2312
{
ali@70
  2313
    const char *s;
ali@70
  2314
    gunichar c,nc,pc;
ali@142
  2315
    QuoteClass qc;
ali@70
  2316
    c=g_utf8_get_char(aline);
ali@70
  2317
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  2318
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@62
  2319
    {
ali@70
  2320
	pc=c;
ali@70
  2321
	c=nc;
ali@142
  2322
	qc=CHAR_IS_DQUOTE(c)?QUOTE_CLASS(c):INVALID_QUOTE;
ali@70
  2323
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@62
  2324
	/* for each character in the line except 1st */
ali@142
  2325
	if ((qc==CLOSING_QUOTE || qc==NEUTRAL_QUOTE) && isalpha(pc))
ali@62
  2326
	{
ali@62
  2327
	    if (pswit[ECHO_SWITCH])
ali@70
  2328
		g_print("\n%s\n",aline);
ali@62
  2329
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2330
		g_print("    Line %ld column %ld - "
ali@70
  2331
		  "endquote missing punctuation?\n",
ali@70
  2332
		  linecnt,g_utf8_pointer_to_offset(aline,s));
ali@62
  2333
	    else
ali@62
  2334
		cnt_punct++;
ali@62
  2335
	}
ali@62
  2336
    }
ali@62
  2337
}
ali@62
  2338
ali@62
  2339
/*
ali@63
  2340
 * check_for_html_tag:
ali@63
  2341
 *
ali@63
  2342
 * Check for <HTML TAG>.
ali@63
  2343
 *
ali@63
  2344
 * If there is a < in the line, followed at some point
ali@63
  2345
 * by a > then we suspect HTML.
ali@63
  2346
 */
ali@63
  2347
void check_for_html_tag(const char *aline)
ali@63
  2348
{
ali@63
  2349
    const char *open,*close;
ali@70
  2350
    gchar *tag;
ali@70
  2351
    open=strchr(aline,'<');
ali@63
  2352
    if (open)
ali@63
  2353
    {
ali@70
  2354
	close=strchr(g_utf8_next_char(open),'>');
ali@63
  2355
	if (close)
ali@63
  2356
	{
ali@70
  2357
	    if (pswit[ECHO_SWITCH])
ali@70
  2358
		g_print("\n%s\n",aline);
ali@70
  2359
	    if (!pswit[OVERVIEW_SWITCH])
ali@63
  2360
	    {
ali@70
  2361
		tag=g_strndup(open,close-open+1);
ali@70
  2362
		g_print("    Line %ld column %ld - HTML Tag? %s \n",
ali@70
  2363
		  linecnt,g_utf8_pointer_to_offset(aline,open)+1,tag);
ali@70
  2364
		g_free(tag);
ali@63
  2365
	    }
ali@70
  2366
	    else
ali@70
  2367
		cnt_html++;
ali@63
  2368
	}
ali@63
  2369
    }
ali@63
  2370
}
ali@63
  2371
ali@63
  2372
/*
ali@64
  2373
 * check_for_html_entity:
ali@64
  2374
 *
ali@64
  2375
 * Check for &symbol; HTML.
ali@64
  2376
 *
ali@64
  2377
 * If there is a & in the line, followed at
ali@64
  2378
 * some point by a ; then we suspect HTML.
ali@64
  2379
 */
ali@64
  2380
void check_for_html_entity(const char *aline)
ali@64
  2381
{
ali@64
  2382
    const char *s,*amp,*scolon;
ali@70
  2383
    gchar *entity;
ali@70
  2384
    amp=strchr(aline,'&');
ali@64
  2385
    if (amp)
ali@64
  2386
    {
ali@70
  2387
	scolon=strchr(amp,';');
ali@64
  2388
	if (scolon)
ali@64
  2389
	{
ali@70
  2390
	    for (s=amp;s<scolon;s=g_utf8_next_char(s))   
ali@70
  2391
		if (g_utf8_get_char(s)==CHAR_SPACE)
ali@70
  2392
		    break;		/* Don't report "Jones & Son;" */
ali@70
  2393
	    if (s>=scolon)
ali@64
  2394
	    {
ali@64
  2395
		if (pswit[ECHO_SWITCH])
ali@70
  2396
		    g_print("\n%s\n",aline);
ali@64
  2397
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2398
		{
ali@70
  2399
		    entity=g_strndup(amp,scolon-amp+1);
ali@70
  2400
		    g_print("    Line %ld column %d - HTML symbol? %s \n",
ali@70
  2401
		      linecnt,(int)(amp-aline)+1,entity);
ali@70
  2402
		    g_free(entity);
ali@70
  2403
		}
ali@64
  2404
		else
ali@64
  2405
		    cnt_html++;
ali@64
  2406
	    }
ali@64
  2407
	}
ali@64
  2408
    }
ali@64
  2409
}
ali@64
  2410
ali@65
  2411
/*
ali@66
  2412
 * check_for_omitted_punctuation:
ali@66
  2413
 *
ali@66
  2414
 * Check for omitted punctuation at end of paragraph by working back
ali@66
  2415
 * through prevline. DW.
ali@66
  2416
 * Need to check this only for "normal" paras.
ali@66
  2417
 * So what is a "normal" para?
ali@66
  2418
 *    Not normal if one-liner (chapter headings, etc.)
ali@66
  2419
 *    Not normal if doesn't contain at least one locase letter
ali@66
  2420
 *    Not normal if starts with space
ali@66
  2421
 */
ali@66
  2422
void check_for_omitted_punctuation(const char *prevline,
ali@66
  2423
  struct line_properties *last,int start_para_line)
ali@66
  2424
{
ali@70
  2425
    gboolean letter_on_line=FALSE;
ali@66
  2426
    const char *s;
ali@99
  2427
    gunichar c;
ali@142
  2428
    gboolean closing_quote;
ali@70
  2429
    for (s=prevline;*s;s=g_utf8_next_char(s))
ali@70
  2430
	if (g_unichar_isalpha(g_utf8_get_char(s)))
ali@70
  2431
	{
ali@70
  2432
	    letter_on_line=TRUE;
ali@70
  2433
	    break;
ali@70
  2434
	}
ali@66
  2435
    /*
ali@66
  2436
     * This next "if" is a problem.
ali@66
  2437
     * If we say "start_para_line <= linecnt - 1", that includes
ali@66
  2438
     * one-line "paragraphs" like chapter heads. Lotsa false positives.
ali@66
  2439
     * If we say "start_para_line < linecnt - 1" it doesn't, but then it
ali@66
  2440
     * misses genuine one-line paragraphs.
ali@66
  2441
     */
ali@70
  2442
    if (letter_on_line && last->blen>2 && start_para_line<linecnt-1 &&
ali@70
  2443
      g_utf8_get_char(prevline)>CHAR_SPACE)
ali@66
  2444
    {
ali@99
  2445
	s=prevline+strlen(prevline);
ali@99
  2446
	do
ali@99
  2447
	{
ali@99
  2448
	    s=g_utf8_prev_char(s);
ali@99
  2449
	    c=g_utf8_get_char(s);
ali@142
  2450
	    if (QUOTE_CLASS(c)==CLOSING_QUOTE || QUOTE_CLASS(c)==NEUTRAL_QUOTE)
ali@142
  2451
		closing_quote=TRUE;
ali@142
  2452
	    else
ali@142
  2453
		closing_quote=FALSE;
ali@142
  2454
	} while (closing_quote && s>prevline);
ali@70
  2455
	for (;s>prevline;s=g_utf8_prev_char(s))
ali@66
  2456
	{
ali@70
  2457
	    if (g_unichar_isalpha(g_utf8_get_char(s)))
ali@66
  2458
	    {
ali@66
  2459
		if (pswit[ECHO_SWITCH])
ali@70
  2460
		    g_print("\n%s\n",prevline);
ali@66
  2461
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2462
		    g_print("    Line %ld column %ld - "
ali@66
  2463
		      "No punctuation at para end?\n",
ali@70
  2464
		      linecnt-1,g_utf8_strlen(prevline,-1));
ali@66
  2465
		else
ali@66
  2466
		    cnt_punct++;
ali@66
  2467
		break;
ali@66
  2468
	    }
ali@70
  2469
	    if (g_utf8_strchr("-.:!([{?}])",-1,g_utf8_get_char(s)))
ali@66
  2470
		break;
ali@66
  2471
	}
ali@66
  2472
    }
ali@66
  2473
}
ali@66
  2474
ali@69
  2475
gboolean report_duplicate_queries(gpointer key,gpointer value,gpointer data)
ali@69
  2476
{
ali@69
  2477
    const char *word=key;
ali@69
  2478
    int *dupcnt=value;
ali@69
  2479
    if (*dupcnt)
ali@70
  2480
	g_print("\nNote: Queried word %s was duplicated %d times\n",
ali@69
  2481
	  word,*dupcnt);
ali@69
  2482
    return FALSE;
ali@69
  2483
}
ali@69
  2484
ali@70
  2485
void print_as_windows_1252(const char *string)
ali@70
  2486
{
ali@70
  2487
    gsize inbytes,outbytes;
ali@70
  2488
    gchar *buf,*bp;
ali@86
  2489
    static GIConv converter=(GIConv)-1;
ali@70
  2490
    if (!string)
ali@70
  2491
    {
ali@70
  2492
	if (converter!=(GIConv)-1)
ali@70
  2493
	    g_iconv_close(converter);
ali@70
  2494
	converter=(GIConv)-1;
ali@70
  2495
	return;
ali@70
  2496
    }
ali@86
  2497
    if (converter==(GIConv)-1)
ali@70
  2498
	converter=g_iconv_open("WINDOWS-1252","UTF-8");
ali@70
  2499
    if (converter!=(GIConv)-1)
ali@70
  2500
    {
ali@70
  2501
	inbytes=outbytes=strlen(string);
ali@70
  2502
	bp=buf=g_malloc(outbytes+1);
ali@70
  2503
	g_iconv(converter,(char **)&string,&inbytes,&bp,&outbytes);
ali@70
  2504
	*bp='\0';
ali@70
  2505
	fputs(buf,stdout);
ali@70
  2506
	g_free(buf);
ali@70
  2507
    }
ali@70
  2508
    else
ali@70
  2509
	fputs(string,stdout);
ali@70
  2510
}
ali@70
  2511
ali@72
  2512
void print_as_utf_8(const char *string)
ali@72
  2513
{
ali@72
  2514
    fputs(string,stdout);
ali@72
  2515
}
ali@72
  2516
ali@66
  2517
/*
ali@41
  2518
 * procfile:
ali@41
  2519
 *
ali@41
  2520
 * Process one file.
ali@41
  2521
 */
ali@69
  2522
void procfile(const char *filename)
ali@41
  2523
{
ali@65
  2524
    const char *s;
ali@69
  2525
    gchar *parastart=NULL;	/* first line of current para */
ali@69
  2526
    gchar *etext,*aline;
ali@69
  2527
    gchar *etext_ptr;
ali@69
  2528
    GError *err=NULL;
ali@41
  2529
    struct first_pass_results *first_pass_results;
ali@42
  2530
    struct warnings *warnings;
ali@43
  2531
    struct counters counters={0};
ali@45
  2532
    struct line_properties last={0};
ali@56
  2533
    struct parities parities={0};
ali@69
  2534
    struct pending pending={0};
ali@69
  2535
    gboolean isemptyline;
ali@68
  2536
    long start_para_line=0;
ali@69
  2537
    gboolean isnewpara=FALSE,enddash=FALSE;
ali@45
  2538
    last.start=CHAR_SPACE;
ali@68
  2539
    linecnt=checked_linecnt=0;
ali@69
  2540
    etext=read_etext(filename,&err);
ali@69
  2541
    if (!etext)
ali@41
  2542
    {
ali@68
  2543
	if (pswit[STDOUT_SWITCH])
ali@69
  2544
	    fprintf(stdout,"bookloupe: %s: %s\n",filename,err->message);
ali@68
  2545
	else
ali@69
  2546
	    fprintf(stderr,"bookloupe: %s: %s\n",filename,err->message);
ali@41
  2547
	exit(1);
ali@41
  2548
    }
ali@70
  2549
    g_print("\n\nFile: %s\n\n",filename);
ali@69
  2550
    first_pass_results=first_pass(etext);
ali@42
  2551
    warnings=report_first_pass(first_pass_results);
ali@69
  2552
    qword=g_tree_new_full((GCompareDataFunc)strcmp,NULL,g_free,g_free);
ali@69
  2553
    qperiod=g_tree_new_full((GCompareDataFunc)strcmp,NULL,g_free,NULL);
ali@40
  2554
    /*
ali@40
  2555
     * Here we go with the main pass. Hold onto yer hat!
ali@40
  2556
     */
ali@65
  2557
    linecnt=0;
ali@69
  2558
    etext_ptr=etext;
ali@69
  2559
    while ((aline=flgets(&etext_ptr,linecnt+1)))
ali@40
  2560
    {
ali@68
  2561
	linecnt++;
ali@68
  2562
	if (linecnt==1)
ali@69
  2563
	    isnewpara=TRUE;
ali@70
  2564
	if (pswit[DP_SWITCH] && g_str_has_prefix(aline,"-----File: "))
ali@40
  2565
	    continue;    // skip DP page separators completely
ali@68
  2566
	if (linecnt<first_pass_results->firstline ||
ali@41
  2567
	  (first_pass_results->footerline>0 &&
ali@41
  2568
	  linecnt>first_pass_results->footerline))
ali@40
  2569
	{
ali@68
  2570
	    if (pswit[HEADER_SWITCH])
ali@40
  2571
	    {
ali@70
  2572
		if (g_str_has_prefix(aline,"Title:"))
ali@70
  2573
		    g_print("    %s\n",aline);
ali@70
  2574
		if (g_str_has_prefix(aline,"Author:"))
ali@70
  2575
		    g_print("    %s\n",aline);
ali@70
  2576
		if (g_str_has_prefix(aline,"Release Date:"))
ali@70
  2577
		    g_print("    %s\n",aline);
ali@70
  2578
		if (g_str_has_prefix(aline,"Edition:"))
ali@70
  2579
		    g_print("    %s\n\n",aline);
ali@40
  2580
	    }
ali@68
  2581
	    continue;		/* skip through the header */
ali@40
  2582
	}
ali@68
  2583
	checked_linecnt++;
ali@65
  2584
	print_pending(aline,parastart,&pending);
ali@142
  2585
	isemptyline=analyse_quotes(aline,linecnt,&counters);
ali@68
  2586
	if (isnewpara && !isemptyline)
ali@40
  2587
	{
ali@40
  2588
	    /* This line is the start of a new paragraph. */
ali@68
  2589
	    start_para_line=linecnt;
ali@40
  2590
	    /* Capture its first line in case we want to report it later. */
ali@69
  2591
	    g_free(parastart);
ali@69
  2592
	    parastart=g_strdup(aline);
ali@56
  2593
	    memset(&parities,0,sizeof(parities));  /* restart the quote count */
ali@68
  2594
	    s=aline;
ali@70
  2595
	    while (*s && !g_unichar_isalpha(g_utf8_get_char(s)) &&
ali@70
  2596
	      !g_unichar_isdigit(g_utf8_get_char(s)))
ali@70
  2597
		s=g_utf8_next_char(s);
ali@70
  2598
	    if (g_unichar_islower(g_utf8_get_char(s)))
ali@40
  2599
	    {
ali@40
  2600
		/* and its first letter is lowercase */
ali@68
  2601
		if (pswit[ECHO_SWITCH])
ali@70
  2602
		    g_print("\n%s\n",aline);
ali@68
  2603
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2604
		    g_print("    Line %ld column %ld - "
ali@40
  2605
		      "Paragraph starts with lower-case\n",
ali@70
  2606
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@68
  2607
		else
ali@68
  2608
		    cnt_punct++;
ali@40
  2609
	    }
ali@69
  2610
	    isnewpara=FALSE; /* Signal the end of new para processing. */
ali@40
  2611
	}
ali@68
  2612
	/* Check for an em-dash broken at line end. */
ali@70
  2613
	if (enddash && g_utf8_get_char(aline)=='-')
ali@40
  2614
	{
ali@68
  2615
	    if (pswit[ECHO_SWITCH])
ali@70
  2616
		g_print("\n%s\n",aline);
ali@68
  2617
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2618
		g_print("    Line %ld column 1 - Broken em-dash?\n",linecnt);
ali@68
  2619
	    else
ali@68
  2620
		cnt_punct++;
ali@40
  2621
	}
ali@69
  2622
	enddash=FALSE;
ali@70
  2623
	for (s=g_utf8_prev_char(aline+strlen(aline));
ali@70
  2624
	  g_utf8_get_char(s)==' ' && s>aline;s=g_utf8_prev_char(s))
ali@40
  2625
	    ;
ali@70
  2626
	if (s>=aline && g_utf8_get_char(s)=='-')
ali@69
  2627
	    enddash=TRUE;
ali@67
  2628
	check_for_control_characters(aline);
ali@68
  2629
	if (warnings->bin)
ali@44
  2630
	    check_for_odd_characters(aline,warnings,isemptyline);
ali@68
  2631
	if (warnings->longline)
ali@45
  2632
	    check_for_long_line(aline);
ali@68
  2633
	if (warnings->shortline)
ali@45
  2634
	    check_for_short_line(aline,&last);
ali@68
  2635
	last.blen=last.len;
ali@70
  2636
	last.len=g_utf8_strlen(aline,-1);
ali@70
  2637
	last.start=g_utf8_get_char(aline);
ali@46
  2638
	check_for_starting_punctuation(aline);
ali@68
  2639
	if (warnings->dash)
ali@40
  2640
	{
ali@47
  2641
	    check_for_spaced_emdash(aline);
ali@47
  2642
	    check_for_spaced_dash(aline);
ali@40
  2643
	}
ali@48
  2644
	check_for_unmarked_paragraphs(aline);
ali@49
  2645
	check_for_jeebies(aline);
ali@50
  2646
	check_for_mta_from(aline);
ali@51
  2647
	check_for_orphan_character(aline);
ali@52
  2648
	check_for_pling_scanno(aline);
ali@53
  2649
	check_for_extra_period(aline,warnings);
ali@54
  2650
	check_for_following_punctuation(aline);
ali@55
  2651
	check_for_typos(aline,warnings);
ali@56
  2652
	check_for_misspaced_punctuation(aline,&parities,isemptyline);
ali@57
  2653
	check_for_double_punctuation(aline,warnings);
ali@58
  2654
	check_for_spaced_quotes(aline);
ali@59
  2655
	check_for_miscased_genative(aline);
ali@60
  2656
	check_end_of_line(aline,warnings);
ali@61
  2657
	check_for_unspaced_bracket(aline);
ali@68
  2658
	if (warnings->endquote)
ali@62
  2659
	    check_for_unpunctuated_endquote(aline);
ali@63
  2660
	check_for_html_tag(aline);
ali@64
  2661
	check_for_html_entity(aline);
ali@68
  2662
	if (isemptyline)
ali@40
  2663
	{
ali@65
  2664
	    check_for_mismatched_quotes(&counters,&pending);
ali@103
  2665
	    counters_reset(&counters);
ali@40
  2666
	    /* let the next iteration know that it's starting a new para */
ali@69
  2667
	    isnewpara=TRUE;
ali@69
  2668
	    if (prevline)
ali@69
  2669
		check_for_omitted_punctuation(prevline,&last,start_para_line);
ali@40
  2670
	}
ali@69
  2671
	g_free(prevline);
ali@69
  2672
	prevline=g_strdup(aline);
ali@0
  2673
    }
ali@103
  2674
    linecnt++;
ali@103
  2675
    check_for_mismatched_quotes(&counters,&pending);
ali@103
  2676
    print_pending(NULL,parastart,&pending);
ali@103
  2677
    reset_pending(&pending);
ali@69
  2678
    if (prevline)
ali@69
  2679
    {
ali@69
  2680
	g_free(prevline);
ali@69
  2681
	prevline=NULL;
ali@69
  2682
    }
ali@69
  2683
    g_free(parastart);
ali@69
  2684
    g_free(prevline);
ali@69
  2685
    g_free(etext);
ali@79
  2686
    if (!pswit[OVERVIEW_SWITCH] && !pswit[VERBOSE_SWITCH])
ali@69
  2687
	g_tree_foreach(qword,report_duplicate_queries,NULL);
ali@69
  2688
    g_tree_unref(qword);
ali@69
  2689
    g_tree_unref(qperiod);
ali@99
  2690
    counters_destroy(&counters);
ali@70
  2691
    g_set_print_handler(NULL);
ali@70
  2692
    print_as_windows_1252(NULL);
ali@71
  2693
    if (pswit[MARKUP_SWITCH])  
ali@71
  2694
	loseentities(NULL);
ali@0
  2695
}
ali@0
  2696
ali@40
  2697
/*
ali@40
  2698
 * flgets:
ali@40
  2699
 *
ali@69
  2700
 * Get one line from the input text, checking for
ali@40
  2701
 * the existence of exactly one CR/LF line-end per line.
ali@40
  2702
 *
ali@40
  2703
 * Returns: a pointer to the line.
ali@40
  2704
 */
ali@69
  2705
char *flgets(char **etext,long lcnt)
ali@0
  2706
{
ali@70
  2707
    gunichar c;
ali@69
  2708
    gboolean isCR=FALSE;
ali@69
  2709
    char *theline=*etext;
ali@70
  2710
    char *eos=theline;
ali@70
  2711
    gchar *s;
ali@70
  2712
    for (;;)
ali@40
  2713
    {
ali@70
  2714
	c=g_utf8_get_char(*etext);
ali@70
  2715
	*etext=g_utf8_next_char(*etext);
ali@69
  2716
	if (!c)
ali@68
  2717
	    return NULL;
ali@40
  2718
	/* either way, it's end of line */
ali@69
  2719
	if (c=='\n')
ali@40
  2720
	{
ali@68
  2721
	    if (isCR)
ali@68
  2722
		break;
ali@68
  2723
	    else
ali@40
  2724
	    {
ali@40
  2725
		/* Error - a LF without a preceding CR */
ali@68
  2726
		if (pswit[LINE_END_SWITCH])
ali@40
  2727
		{
ali@68
  2728
		    if (pswit[ECHO_SWITCH])
ali@70
  2729
		    {
ali@70
  2730
			s=g_strndup(theline,eos-theline);
ali@70
  2731
			g_print("\n%s\n",s);
ali@70
  2732
			g_free(s);
ali@70
  2733
		    }
ali@68
  2734
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2735
			g_print("    Line %ld - No CR?\n",lcnt);
ali@68
  2736
		    else
ali@68
  2737
			cnt_lineend++;
ali@40
  2738
		}
ali@68
  2739
		break;
ali@40
  2740
	    }
ali@40
  2741
	}
ali@69
  2742
	if (c=='\r')
ali@40
  2743
	{
ali@68
  2744
	    if (isCR)
ali@40
  2745
	    {
ali@40
  2746
		/* Error - two successive CRs */
ali@68
  2747
		if (pswit[LINE_END_SWITCH])
ali@40
  2748
		{
ali@68
  2749
		    if (pswit[ECHO_SWITCH])
ali@70
  2750
		    {
ali@70
  2751
			s=g_strndup(theline,eos-theline);
ali@70
  2752
			g_print("\n%s\n",s);
ali@70
  2753
			g_free(s);
ali@70
  2754
		    }
ali@68
  2755
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2756
			g_print("    Line %ld - Two successive CRs?\n",lcnt);
ali@68
  2757
		    else
ali@68
  2758
			cnt_lineend++;
ali@40
  2759
		}
ali@40
  2760
	    }
ali@69
  2761
	    isCR=TRUE;
ali@40
  2762
	}
ali@68
  2763
	else
ali@40
  2764
	{
ali@68
  2765
	    if (pswit[LINE_END_SWITCH] && isCR)
ali@40
  2766
	    {
ali@68
  2767
		if (pswit[ECHO_SWITCH])
ali@70
  2768
		{
ali@70
  2769
		    s=g_strndup(theline,eos-theline);
ali@70
  2770
		    g_print("\n%s\n",s);
ali@70
  2771
		    g_free(s);
ali@70
  2772
		}
ali@68
  2773
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2774
		    g_print("    Line %ld column %ld - CR without LF?\n",
ali@70
  2775
		      lcnt,g_utf8_pointer_to_offset(theline,eos)+1);
ali@68
  2776
		else
ali@68
  2777
		    cnt_lineend++;
ali@70
  2778
		*eos=' ';
ali@40
  2779
	    }
ali@69
  2780
	    isCR=FALSE;
ali@70
  2781
	    eos=g_utf8_next_char(eos);
ali@40
  2782
	}
ali@69
  2783
    }
ali@70
  2784
    *eos='\0';
ali@0
  2785
    if (pswit[MARKUP_SWITCH])  
ali@68
  2786
	postprocess_for_HTML(theline);
ali@0
  2787
    if (pswit[DP_SWITCH])  
ali@68
  2788
	postprocess_for_DP(theline);
ali@40
  2789
    return theline;
ali@0
  2790
}
ali@0
  2791
ali@40
  2792
/*
ali@40
  2793
 * mixdigit:
ali@40
  2794
 *
ali@40
  2795
 * Takes a "word" as a parameter, and checks whether it
ali@40
  2796
 * contains a mixture of alpha and digits. Generally, this is an
ali@40
  2797
 * error, but may not be for cases like 4th or L5 12s. 3d.
ali@40
  2798
 *
ali@70
  2799
 * Returns: TRUE iff an is error found.
ali@40
  2800
 */
ali@70
  2801
gboolean mixdigit(const char *checkword)
ali@0
  2802
{
ali@70
  2803
    gboolean wehaveadigit,wehavealetter,query;
ali@70
  2804
    const char *s,*nondigit;
ali@70
  2805
    wehaveadigit=wehavealetter=query=FALSE;
ali@70
  2806
    for (s=checkword;*s;s=g_utf8_next_char(s))
ali@70
  2807
	if (g_unichar_isalpha(g_utf8_get_char(s)))
ali@70
  2808
	    wehavealetter=TRUE;
ali@70
  2809
	else if (g_unichar_isdigit(g_utf8_get_char(s)))
ali@70
  2810
	    wehaveadigit=TRUE;
ali@40
  2811
    if (wehaveadigit && wehavealetter)
ali@40
  2812
    {
ali@40
  2813
	/* Now exclude common legit cases, like "21st" and "12l. 3s. 11d." */
ali@70
  2814
	query=TRUE;
ali@70
  2815
	for (nondigit=checkword;g_unichar_isdigit(g_utf8_get_char(nondigit));
ali@70
  2816
	  nondigit=g_utf8_next_char(nondigit))
ali@68
  2817
	    ;
ali@68
  2818
	/* digits, ending in st, rd, nd, th of either case */
ali@70
  2819
	if (!g_ascii_strcasecmp(nondigit,"st") ||
ali@70
  2820
	  !g_ascii_strcasecmp(nondigit,"rd") ||
ali@70
  2821
	  !g_ascii_strcasecmp(nondigit,"nd") ||
ali@70
  2822
	  !g_ascii_strcasecmp(nondigit,"th"))
ali@70
  2823
	    query=FALSE;
ali@70
  2824
	if (!g_ascii_strcasecmp(nondigit,"sts") ||
ali@70
  2825
	  !g_ascii_strcasecmp(nondigit,"rds") ||
ali@70
  2826
	  !g_ascii_strcasecmp(nondigit,"nds") ||
ali@70
  2827
	  !g_ascii_strcasecmp(nondigit,"ths"))
ali@70
  2828
	    query=FALSE;
ali@70
  2829
	if (!g_ascii_strcasecmp(nondigit,"stly") ||
ali@70
  2830
	  !g_ascii_strcasecmp(nondigit,"rdly") ||
ali@70
  2831
	  !g_ascii_strcasecmp(nondigit,"ndly") ||
ali@70
  2832
	  !g_ascii_strcasecmp(nondigit,"thly"))
ali@70
  2833
	    query=FALSE;
ali@68
  2834
	/* digits, ending in l, L, s or d */
ali@70
  2835
	if (!g_ascii_strcasecmp(nondigit,"l") || !strcmp(nondigit,"s") ||
ali@70
  2836
	  !strcmp(nondigit,"d"))
ali@70
  2837
	    query=FALSE;
ali@68
  2838
	/*
ali@40
  2839
	 * L at the start of a number, representing Britsh pounds, like L500.
ali@70
  2840
	 * This is cute. We know the current word is mixed digit. If the first
ali@68
  2841
	 * letter is L, there must be at least one digit following. If both
ali@68
  2842
	 * digits and letters follow, we have a genuine error, else we have a
ali@68
  2843
	 * capital L followed by digits, and we accept that as a non-error.
ali@40
  2844
	 */
ali@70
  2845
	if (g_utf8_get_char(checkword)=='L' &&
ali@70
  2846
	  !mixdigit(g_utf8_next_char(checkword)))
ali@70
  2847
	    query=FALSE;
ali@40
  2848
    }
ali@40
  2849
    return query;
ali@0
  2850
}
ali@0
  2851
ali@40
  2852
/*
ali@40
  2853
 * getaword:
ali@40
  2854
 *
ali@69
  2855
 * Extracts the first/next "word" from the line, and returns it.
ali@69
  2856
 * A word is defined as one English word unit--or at least that's the aim.
ali@69
  2857
 * "ptr" is advanced to the position in the line where we will start
ali@69
  2858
 * looking for the next word.
ali@40
  2859
 *
ali@69
  2860
 * Returns: A newly-allocated string.
ali@40
  2861
 */
ali@69
  2862
gchar *getaword(const char **ptr)
ali@0
  2863
{
ali@70
  2864
    const char *s,*t;
ali@69
  2865
    GString *word;
ali@70
  2866
    gunichar c,pc;
ali@69
  2867
    word=g_string_new(NULL);
ali@70
  2868
    for (;!g_unichar_isdigit(g_utf8_get_char(*ptr)) &&
ali@70
  2869
      !g_unichar_isalpha(g_utf8_get_char(*ptr)) &&
ali@70
  2870
      **ptr;*ptr=g_utf8_next_char(*ptr))
ali@40
  2871
	;
ali@40
  2872
    /*
ali@40
  2873
     * Use a look-ahead to handle exceptions for numbers like 1,000 and 1.35.
ali@40
  2874
     * Especially yucky is the case of L1,000
ali@40
  2875
     * This section looks for a pattern of characters including a digit
ali@40
  2876
     * followed by a comma or period followed by one or more digits.
ali@40
  2877
     * If found, it returns this whole pattern as a word; otherwise we discard
ali@40
  2878
     * the results and resume our normal programming.
ali@40
  2879
     */
ali@69
  2880
    s=*ptr;
ali@70
  2881
    for (;g_unichar_isdigit(g_utf8_get_char(s)) ||
ali@70
  2882
      g_unichar_isalpha(g_utf8_get_char(s)) ||
ali@70
  2883
      g_utf8_get_char(s)==',' || g_utf8_get_char(s)=='.';s=g_utf8_next_char(s))
ali@70
  2884
	g_string_append_unichar(word,g_utf8_get_char(s));
ali@82
  2885
    if (word->len)
ali@40
  2886
    {
ali@82
  2887
	for (t=g_utf8_next_char(word->str);*t;t=g_utf8_next_char(t))
ali@40
  2888
	{
ali@82
  2889
	    c=g_utf8_get_char(t);
ali@82
  2890
	    pc=g_utf8_get_char(g_utf8_prev_char(t));
ali@82
  2891
	    if ((c=='.' || c==',') && g_unichar_isdigit(pc))
ali@82
  2892
	    {
ali@82
  2893
		*ptr=s;
ali@82
  2894
		return g_string_free(word,FALSE);
ali@82
  2895
	    }
ali@40
  2896
	}
ali@40
  2897
    }
ali@0
  2898
    /* we didn't find a punctuated number - do the regular getword thing */
ali@69
  2899
    g_string_truncate(word,0);
ali@99
  2900
    c=g_utf8_get_char(*ptr);
ali@99
  2901
    for (;g_unichar_isdigit(c) || g_unichar_isalpha(c) || CHAR_IS_APOSTROPHE(c);
ali@99
  2902
      *ptr=g_utf8_next_char(*ptr),c=g_utf8_get_char(*ptr))
ali@99
  2903
	g_string_append_unichar(word,c);
ali@69
  2904
    return g_string_free(word,FALSE);
ali@0
  2905
}
ali@0
  2906
ali@40
  2907
/*
ali@40
  2908
 * isroman:
ali@40
  2909
 *
ali@40
  2910
 * Is this word a Roman Numeral?
ali@40
  2911
 *
ali@40
  2912
 * It doesn't actually validate that the number is a valid Roman Numeral--for
ali@40
  2913
 * example it will pass MXXXXXXXXXX as a valid Roman Numeral, but that's not
ali@40
  2914
 * what we're here to do. If it passes this, it LOOKS like a Roman numeral.
ali@40
  2915
 * Anyway, the actual Romans were pretty tolerant of bad arithmetic, or
ali@40
  2916
 * expressions thereof, except when it came to taxes. Allow any number of M,
ali@40
  2917
 * an optional D, an optional CM or CD, any number of optional Cs, an optional
ali@40
  2918
 * XL or an optional XC, an optional IX or IV, an optional V and any number
ali@40
  2919
 * of optional Is.
ali@40
  2920
 */
ali@69
  2921
gboolean isroman(const char *t)
ali@0
  2922
{
ali@69
  2923
    const char *s;
ali@40
  2924
    if (!t || !*t)
ali@69
  2925
	return FALSE;
ali@40
  2926
    s=t;
ali@70
  2927
    while (g_utf8_get_char(t)=='m' && *t)
ali@40
  2928
	t++;
ali@70
  2929
    if (g_utf8_get_char(t)=='d')
ali@40
  2930
	t++;
ali@70
  2931
    if (g_str_has_prefix(t,"cm"))
ali@40
  2932
	t+=2;
ali@70
  2933
    if (g_str_has_prefix(t,"cd"))
ali@40
  2934
	t+=2;
ali@70
  2935
    while (g_utf8_get_char(t)=='c' && *t)
ali@40
  2936
	t++;
ali@70
  2937
    if (g_str_has_prefix(t,"xl"))
ali@40
  2938
	t+=2;
ali@70
  2939
    if (g_str_has_prefix(t,"xc"))
ali@40
  2940
	t+=2;
ali@70
  2941
    if (g_utf8_get_char(t)=='l')
ali@40
  2942
	t++;
ali@70
  2943
    while (g_utf8_get_char(t)=='x' && *t)
ali@40
  2944
	t++;
ali@70
  2945
    if (g_str_has_prefix(t,"ix"))
ali@40
  2946
	t+=2;
ali@70
  2947
    if (g_str_has_prefix(t,"iv"))
ali@40
  2948
	t+=2;
ali@70
  2949
    if (g_utf8_get_char(t)=='v')
ali@40
  2950
	t++;
ali@70
  2951
    while (g_utf8_get_char(t)=='i' && *t)
ali@40
  2952
	t++;
ali@40
  2953
    return !*t;
ali@0
  2954
}
ali@0
  2955
ali@40
  2956
/*
ali@40
  2957
 * postprocess_for_DP:
ali@40
  2958
 *
ali@40
  2959
 * Invoked with the -d switch from flgets().
ali@40
  2960
 * It simply "removes" from the line a hard-coded set of common
ali@40
  2961
 * DP-specific tags, so that the line passed to the main routine has
ali@40
  2962
 * been pre-cleaned of DP markup.
ali@40
  2963
 */
ali@0
  2964
void postprocess_for_DP(char *theline)
ali@0
  2965
{
ali@40
  2966
    char *s,*t;
ali@0
  2967
    int i;
ali@0
  2968
    if (!*theline) 
ali@68
  2969
	return;
ali@40
  2970
    for (i=0;*DPmarkup[i];i++)
ali@70
  2971
	while ((s=strstr(theline,DPmarkup[i])))
ali@40
  2972
	{
ali@68
  2973
	    t=s+strlen(DPmarkup[i]);
ali@70
  2974
	    memmove(s,t,strlen(t)+1);
ali@40
  2975
	}
ali@0
  2976
}
ali@0
  2977
ali@40
  2978
/*
ali@40
  2979
 * postprocess_for_HTML:
ali@40
  2980
 *
ali@40
  2981
 * Invoked with the -m switch from flgets().
ali@40
  2982
 * It simply "removes" from the line a hard-coded set of common
ali@40
  2983
 * HTML tags and "replaces" a hard-coded set of common HTML
ali@40
  2984
 * entities, so that the line passed to the main routine has
ali@40
  2985
 * been pre-cleaned of HTML.
ali@40
  2986
 */
ali@0
  2987
void postprocess_for_HTML(char *theline)
ali@0
  2988
{
ali@70
  2989
    while (losemarkup(theline))
ali@70
  2990
	;
ali@71
  2991
    loseentities(theline);
ali@0
  2992
}
ali@0
  2993
ali@0
  2994
char *losemarkup(char *theline)
ali@0
  2995
{
ali@40
  2996
    char *s,*t;
ali@0
  2997
    int i;
ali@70
  2998
    s=strchr(theline,'<');
ali@70
  2999
    t=s?strchr(s,'>'):NULL;
ali@40
  3000
    if (!s || !t)
ali@40
  3001
	return NULL;
ali@40
  3002
    for (i=0;*markup[i];i++)
ali@70
  3003
	if (tagcomp(g_utf8_next_char(s),markup[i]))
ali@40
  3004
	{
ali@70
  3005
	    t=g_utf8_next_char(t);
ali@70
  3006
	    memmove(s,t,strlen(t)+1);
ali@70
  3007
	    return s;
ali@68
  3008
	}
ali@40
  3009
    /* It's an unrecognized <xxx>. */
ali@40
  3010
    return NULL;
ali@0
  3011
}
ali@0
  3012
ali@71
  3013
void loseentities(char *theline)
ali@0
  3014
{
ali@0
  3015
    int i;
ali@71
  3016
    gsize nb;
ali@71
  3017
    char *amp,*scolon;
ali@71
  3018
    gchar *s,*t;
ali@71
  3019
    gunichar c;
ali@71
  3020
    GTree *entities=NULL;
ali@86
  3021
    static GIConv translit=(GIConv)-1,to_utf8=(GIConv)-1;
ali@71
  3022
    if (!theline)
ali@40
  3023
    {
ali@71
  3024
	if (entities)
ali@71
  3025
	    g_tree_destroy(entities);
ali@71
  3026
	entities=NULL;
ali@86
  3027
	if (translit!=(GIConv)-1)
ali@71
  3028
	    g_iconv_close(translit);
ali@71
  3029
	translit=(GIConv)-1;
ali@86
  3030
	if (to_utf8!=(GIConv)-1)
ali@71
  3031
	    g_iconv_close(to_utf8);
ali@71
  3032
	to_utf8=(GIConv)-1;
ali@71
  3033
	return;
ali@71
  3034
    }
ali@71
  3035
    if (!*theline)
ali@71
  3036
	return;
ali@71
  3037
    if (!entities)
ali@71
  3038
    {
ali@71
  3039
	entities=g_tree_new((GCompareFunc)strcmp);
ali@71
  3040
	for(i=0;i<G_N_ELEMENTS(HTMLentities);i++)
ali@71
  3041
	    g_tree_insert(entities,HTMLentities[i].name,
ali@71
  3042
	      GUINT_TO_POINTER(HTMLentities[i].c));
ali@71
  3043
    }
ali@71
  3044
    if (translit==(GIConv)-1)
ali@71
  3045
	translit=g_iconv_open("ISO_8859-1//TRANSLIT","UTF-8");
ali@71
  3046
    if (to_utf8==(GIConv)-1)
ali@71
  3047
	to_utf8=g_iconv_open("UTF-8","ISO_8859-1");
ali@71
  3048
    while((amp=strchr(theline,'&')))
ali@71
  3049
    {
ali@71
  3050
	scolon=strchr(amp,';');
ali@71
  3051
	if (scolon)
ali@40
  3052
	{
ali@71
  3053
	    if (amp[1]=='#')
ali@71
  3054
	    {
ali@71
  3055
		if (amp+2+strspn(amp+2,"0123456789")==scolon)
ali@71
  3056
		    c=strtol(amp+2,NULL,10);
ali@71
  3057
		else if (amp[2]=='x' &&
ali@71
  3058
		  amp+3+strspn(amp+3,"0123456789abcdefABCDEF")==scolon)
ali@71
  3059
		    c=strtol(amp+3,NULL,16);
ali@71
  3060
	    }
ali@71
  3061
	    else
ali@71
  3062
	    {
ali@71
  3063
		s=g_strndup(amp+1,scolon-(amp+1));
ali@71
  3064
	        c=GPOINTER_TO_UINT(g_tree_lookup(entities,s));
ali@71
  3065
		g_free(s);
ali@71
  3066
	    }
ali@40
  3067
	}
ali@71
  3068
	else
ali@71
  3069
	    c=0;
ali@71
  3070
	if (c)
ali@71
  3071
	{
ali@71
  3072
	    theline=amp;
ali@71
  3073
	    if (c<128 || c>=192 && c<=255)	/* An ISO-8859-1 character */
ali@71
  3074
		theline+=g_unichar_to_utf8(c,theline);
ali@71
  3075
	    else
ali@71
  3076
	    {
ali@71
  3077
		s=g_malloc(6);
ali@71
  3078
		nb=g_unichar_to_utf8(c,s);
ali@71
  3079
		t=g_convert_with_iconv(s,nb,translit,NULL,&nb,NULL);
ali@71
  3080
		g_free(s);
ali@71
  3081
		s=g_convert_with_iconv(t,nb,to_utf8,NULL,&nb,NULL);
ali@71
  3082
		g_free(t);
ali@71
  3083
		memcpy(theline,s,nb);
ali@71
  3084
		g_free(s);
ali@71
  3085
		theline+=nb;
ali@71
  3086
	    }
ali@71
  3087
	    memmove(theline,g_utf8_next_char(scolon),
ali@71
  3088
	      strlen(g_utf8_next_char(scolon))+1);
ali@71
  3089
	}
ali@71
  3090
	else
ali@71
  3091
	    theline=g_utf8_next_char(amp);
ali@40
  3092
    }
ali@0
  3093
}
ali@0
  3094
ali@70
  3095
gboolean tagcomp(const char *strin,const char *basetag)
ali@0
  3096
{
ali@70
  3097
    gboolean retval;
ali@70
  3098
    gchar *s,*t;
ali@70
  3099
    if (g_utf8_get_char(strin)=='/')
ali@70
  3100
	t=g_utf8_casefold(g_utf8_next_char(strin),-1); /* ignore a slash */
ali@70
  3101
    else
ali@70
  3102
	t=g_utf8_casefold(strin,-1);
ali@70
  3103
    s=g_utf8_casefold(basetag,-1);
ali@70
  3104
    retval=g_str_has_prefix(t,s);
ali@70
  3105
    g_free(s);
ali@70
  3106
    g_free(t);
ali@70
  3107
    return retval;
ali@0
  3108
}
ali@0
  3109
ali@69
  3110
void proghelp(GOptionContext *context)
ali@0
  3111
{
ali@69
  3112
    gchar *help;
ali@40
  3113
    fputs("Bookloupe version " PACKAGE_VERSION ".\n",stderr);
ali@40
  3114
    fputs("Copyright 2000-2005 Jim Tinsley <jtinsley@pobox.com>.\n",stderr);
ali@40
  3115
    fputs("Copyright 2012- J. Ali Harlow <ali@juiblex.co.uk>.\n",stderr);
ali@40
  3116
    fputs("Bookloupe comes wih ABSOLUTELY NO WARRANTY. "
ali@40
  3117
      "For details, read the file COPYING.\n",stderr);
ali@40
  3118
    fputs("This is Free Software; "
ali@40
  3119
      "you may redistribute it under certain conditions (GPL);\n",stderr);
ali@40
  3120
    fputs("read the file COPYING for details.\n\n",stderr);
ali@69
  3121
    help=g_option_context_get_help(context,TRUE,NULL);
ali@69
  3122
    fputs(help,stderr);
ali@69
  3123
    g_free(help);
ali@69
  3124
    fputs("Sample usage: bookloupe warpeace.txt\n\n",stderr);
ali@40
  3125
    fputs("Bookloupe queries anything it thinks shouldn't be in a PG text; "
ali@40
  3126
      "non-ASCII\n",stderr);
ali@40
  3127
    fputs("characters like accented letters, "
ali@40
  3128
      "lines longer than 75 or shorter than 55,\n",stderr);
ali@40
  3129
    fputs("unbalanced quotes or brackets, "
ali@40
  3130
      "a variety of badly formatted punctuation, \n",stderr);
ali@40
  3131
    fputs("HTML tags, some likely typos. "
ali@40
  3132
      "It is NOT a substitute for human judgement.\n",stderr);
ali@0
  3133
    fputs("\n",stderr);
ali@0
  3134
}