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