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