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