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