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