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