bookloupe/bookloupe.c
author ali <ali@juiblex.co.uk>
Tue Sep 24 07:22:18 2013 +0100 (2013-09-24)
changeset 109 f39394c9d41f
parent 99 7a62c77a0dbe
child 111 f805130deb6f
child 115 df21841a2b64
child 123 ddb5ddba6ef3
child 127 badcc3b340ab
child 131 2ff298db529e
child 136 2f3762ff90d8
child 142 466f43a12118
permissions -rw-r--r--
Update documentation for 2.0.53
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@43
   804
	    counters->quot++;
ali@99
   805
	if (CHAR_IS_SQUOTE(c))
ali@43
   806
	{
ali@43
   807
	    if (s==aline)
ali@43
   808
	    {
ali@43
   809
		/*
ali@43
   810
		 * At start of line, it can only be an openquote.
ali@43
   811
		 * Hardcode a very common exception!
ali@43
   812
		 */
ali@70
   813
		if (!g_str_has_prefix(snext,"tis") &&
ali@70
   814
		  !g_str_has_prefix(snext,"Tis"))
ali@99
   815
		    increment_matching(counters,c,TRUE);
ali@43
   816
	    }
ali@70
   817
	    else if (g_unichar_isalpha(g_utf8_get_char(sprev)) &&
ali@70
   818
	      g_unichar_isalpha(g_utf8_get_char(snext)))
ali@43
   819
		/* Do nothing! it's definitely an apostrophe, not a quote */
ali@43
   820
		;
ali@43
   821
	    /* it's outside a word - let's check it out */
ali@99
   822
	    else if (c==CHAR_OPEN_SQUOTE || c==CHAR_LS_QUOTE ||
ali@70
   823
	      g_unichar_isalpha(g_utf8_get_char(snext)))
ali@43
   824
	    {
ali@43
   825
		/* it damwell better BE an openquote */
ali@70
   826
		if (!g_str_has_prefix(snext,"tis") &&
ali@70
   827
		  !g_str_has_prefix(snext,"Tis"))
ali@43
   828
		    /* hardcode a very common exception! */
ali@99
   829
		    increment_matching(counters,c,TRUE);
ali@43
   830
	    }
ali@43
   831
	    else
ali@43
   832
	    {
ali@43
   833
		/* now - is it a closequote? */
ali@43
   834
		guessquote=0;   /* accumulate clues */
ali@70
   835
		if (g_unichar_isalpha(g_utf8_get_char(sprev)))
ali@43
   836
		{
ali@43
   837
		    /* it follows a letter - could be either */
ali@43
   838
		    guessquote++;
ali@70
   839
		    if (g_utf8_get_char(sprev)=='s')
ali@43
   840
		    {
ali@43
   841
			/* looks like a plural apostrophe */
ali@43
   842
			guessquote-=3;
ali@70
   843
			if (g_utf8_get_char(snext)==CHAR_SPACE)
ali@70
   844
			    /* bonus marks! */
ali@43
   845
			    guessquote-=2;
ali@43
   846
		    }
ali@43
   847
		}
ali@43
   848
		/* it doesn't have a letter either side */
ali@70
   849
		else if (strchr(".?!,;:",g_utf8_get_char(sprev)) &&
ali@70
   850
		  strchr(".?!,;: ",g_utf8_get_char(snext)))
ali@43
   851
		    guessquote+=8; /* looks like a closequote */
ali@43
   852
		else
ali@43
   853
		    guessquote++;
ali@99
   854
		if (matching_difference(counters,CHAR_SQUOTE)>0)
ali@43
   855
		    /*
ali@43
   856
		     * Give it the benefit of some doubt,
ali@43
   857
		     * if a squote is already open.
ali@43
   858
		     */
ali@43
   859
		    guessquote++;
ali@43
   860
		else
ali@43
   861
		    guessquote--;
ali@43
   862
		if (guessquote>=0)
ali@99
   863
		    increment_matching(counters,c,FALSE);
ali@43
   864
	    }
ali@43
   865
	}
ali@70
   866
	if (c!=CHAR_SPACE && c!='-' && c!='.' && c!=CHAR_ASTERISK &&
ali@70
   867
	  c!='\r' && c!='\n')
ali@69
   868
	    isemptyline=FALSE;  /* ignore lines like  *  *  *  as spacers */
ali@70
   869
	if (c==CHAR_UNDERSCORE)
ali@43
   870
	    counters->c_unders++;
ali@103
   871
	if (c==CHAR_OPEN_SBRACK)
ali@103
   872
	{
ali@103
   873
	    if (!matching_difference(counters,COUNTER_ILLUSTRATION) &&
ali@103
   874
	      !matching_difference(counters,c) && s==aline &&
ali@103
   875
	      g_str_has_prefix(s,"[Illustration:"))
ali@103
   876
		increment_matching(counters,COUNTER_ILLUSTRATION,TRUE);
ali@103
   877
	    else
ali@103
   878
		increment_matching(counters,c,TRUE);
ali@103
   879
	}
ali@103
   880
	else if (c==CHAR_OPEN_CBRACK || c==CHAR_OPEN_RBRACK)
ali@99
   881
	    increment_matching(counters,c,TRUE);
ali@103
   882
	if (c==CHAR_CLOSE_SBRACK)
ali@103
   883
	{
ali@103
   884
	    if (!matching_count(counters,COUNTER_ILLUSTRATION,FALSE) &&
ali@103
   885
	      !matching_difference(counters,c) && !*snext)
ali@103
   886
		increment_matching(counters,COUNTER_ILLUSTRATION,FALSE);
ali@103
   887
	    else
ali@103
   888
		increment_matching(counters,c,FALSE);
ali@103
   889
	}
ali@103
   890
	else if (c==CHAR_CLOSE_CBRACK || c==CHAR_CLOSE_RBRACK)
ali@99
   891
	    increment_matching(counters,c,FALSE);
ali@70
   892
	sprev=s;
ali@70
   893
	s=snext;
ali@43
   894
    }
ali@43
   895
    return isemptyline;
ali@43
   896
}
ali@43
   897
ali@41
   898
/*
ali@67
   899
 * check_for_control_characters:
ali@67
   900
 *
ali@67
   901
 * Check for invalid or questionable characters in the line
ali@67
   902
 * Anything above 127 is invalid for plain ASCII, and
ali@67
   903
 * non-printable control characters should also be flagged.
ali@67
   904
 * Tabs should generally not be there.
ali@67
   905
 */
ali@67
   906
void check_for_control_characters(const char *aline)
ali@67
   907
{
ali@70
   908
    gunichar c;
ali@67
   909
    const char *s;
ali@70
   910
    for (s=aline;*s;s=g_utf8_next_char(s))
ali@67
   911
    {
ali@70
   912
	c=g_utf8_get_char(s);
ali@67
   913
	if (c<CHAR_SPACE && c!=CHAR_LF && c!=CHAR_CR && c!=CHAR_TAB)
ali@67
   914
	{
ali@67
   915
	    if (pswit[ECHO_SWITCH])
ali@70
   916
		g_print("\n%s\n",aline);
ali@67
   917
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
   918
		g_print("    Line %ld column %ld - Control character %u\n",
ali@70
   919
		  linecnt,g_utf8_pointer_to_offset(s,aline)+1,c);
ali@67
   920
	    else
ali@67
   921
		cnt_bin++;
ali@67
   922
	}
ali@67
   923
    }
ali@67
   924
}
ali@67
   925
ali@67
   926
/*
ali@44
   927
 * check_for_odd_characters:
ali@44
   928
 *
ali@44
   929
 * Check for binary and other odd characters.
ali@44
   930
 */
ali@44
   931
void check_for_odd_characters(const char *aline,const struct warnings *warnings,
ali@69
   932
  gboolean isemptyline)
ali@44
   933
{
ali@44
   934
    /* Don't repeat multiple warnings on one line. */
ali@70
   935
    gboolean eNon_A=FALSE,eTab=FALSE,eTilde=FALSE;
ali@70
   936
    gboolean eCarat=FALSE,eFSlash=FALSE,eAst=FALSE;
ali@44
   937
    const char *s;
ali@70
   938
    gunichar c;
ali@70
   939
    for (s=aline;*s;s=g_utf8_next_char(s))
ali@44
   940
    {
ali@70
   941
	c=g_utf8_get_char(s);
ali@70
   942
	if (!eNon_A && (c<CHAR_SPACE && c!='\t' && c!='\n' || c>127))
ali@44
   943
	{
ali@44
   944
	    if (pswit[ECHO_SWITCH])
ali@70
   945
		g_print("\n%s\n",aline);
ali@44
   946
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
   947
		if (c>127 && c<160 || c>255)
ali@70
   948
		    g_print("    Line %ld column %ld - "
ali@70
   949
		      "Non-ISO-8859 character %u\n",
ali@70
   950
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1,c);
ali@44
   951
		else
ali@70
   952
		    g_print("    Line %ld column %ld - "
ali@70
   953
		      "Non-ASCII character %u\n",
ali@70
   954
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1,c);
ali@44
   955
	    else
ali@44
   956
		cnt_bin++;
ali@70
   957
	    eNon_A=TRUE;
ali@44
   958
	}
ali@70
   959
	if (!eTab && c==CHAR_TAB)
ali@44
   960
	{
ali@44
   961
	    if (pswit[ECHO_SWITCH])
ali@70
   962
		g_print("\n%s\n",aline);
ali@44
   963
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
   964
		g_print("    Line %ld column %ld - Tab character?\n",
ali@70
   965
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@44
   966
	    else
ali@44
   967
		cnt_odd++;
ali@70
   968
	    eTab=TRUE;
ali@44
   969
	}
ali@70
   970
	if (!eTilde && c==CHAR_TILDE)
ali@44
   971
	{
ali@44
   972
	    /*
ali@44
   973
	     * Often used by OCR software to indicate an
ali@44
   974
	     * unrecognizable character.
ali@44
   975
	     */
ali@44
   976
	    if (pswit[ECHO_SWITCH])
ali@70
   977
		g_print("\n%s\n",aline);
ali@44
   978
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
   979
		g_print("    Line %ld column %ld - Tilde character?\n",
ali@70
   980
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@44
   981
	    else
ali@44
   982
		cnt_odd++;
ali@70
   983
	    eTilde=TRUE;
ali@44
   984
	}
ali@70
   985
	if (!eCarat && c==CHAR_CARAT)
ali@44
   986
	{  
ali@44
   987
	    if (pswit[ECHO_SWITCH])
ali@70
   988
		g_print("\n%s\n",aline);
ali@44
   989
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
   990
		g_print("    Line %ld column %ld - Carat character?\n",
ali@70
   991
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@44
   992
	    else
ali@44
   993
		cnt_odd++;
ali@70
   994
	    eCarat=TRUE;
ali@44
   995
	}
ali@70
   996
	if (!eFSlash && c==CHAR_FORESLASH && warnings->fslash)
ali@44
   997
	{  
ali@44
   998
	    if (pswit[ECHO_SWITCH])
ali@70
   999
		g_print("\n%s\n",aline);
ali@44
  1000
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1001
		g_print("    Line %ld column %ld - Forward slash?\n",
ali@70
  1002
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@44
  1003
	    else
ali@44
  1004
		cnt_odd++;
ali@70
  1005
	    eFSlash=TRUE;
ali@44
  1006
	}
ali@44
  1007
	/*
ali@44
  1008
	 * Report asterisks only in paranoid mode,
ali@44
  1009
	 * since they're often deliberate.
ali@44
  1010
	 */
ali@44
  1011
	if (!eAst && pswit[PARANOID_SWITCH] && warnings->ast && !isemptyline &&
ali@70
  1012
	  c==CHAR_ASTERISK)
ali@44
  1013
	{
ali@44
  1014
	    if (pswit[ECHO_SWITCH])
ali@70
  1015
		g_print("\n%s\n",aline);
ali@44
  1016
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1017
		g_print("    Line %ld column %ld - Asterisk?\n",
ali@70
  1018
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@44
  1019
	    else
ali@44
  1020
		cnt_odd++;
ali@70
  1021
	    eAst=TRUE;
ali@44
  1022
	}
ali@44
  1023
    }
ali@44
  1024
}
ali@44
  1025
ali@44
  1026
/*
ali@45
  1027
 * check_for_long_line:
ali@45
  1028
 *
ali@45
  1029
 * Check for line too long.
ali@45
  1030
 */
ali@45
  1031
void check_for_long_line(const char *aline)
ali@45
  1032
{
ali@70
  1033
    if (g_utf8_strlen(aline,-1)>LONGEST_PG_LINE)
ali@45
  1034
    {
ali@45
  1035
	if (pswit[ECHO_SWITCH])
ali@70
  1036
	    g_print("\n%s\n",aline);
ali@45
  1037
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1038
	    g_print("    Line %ld column %ld - Long line %ld\n",
ali@70
  1039
	      linecnt,g_utf8_strlen(aline,-1),g_utf8_strlen(aline,-1));
ali@45
  1040
	else
ali@45
  1041
	    cnt_long++;
ali@45
  1042
    }
ali@45
  1043
}
ali@45
  1044
ali@45
  1045
/*
ali@45
  1046
 * check_for_short_line:
ali@45
  1047
 *
ali@45
  1048
 * Check for line too short.
ali@45
  1049
 *
ali@45
  1050
 * This one is a bit trickier to implement: we don't want to
ali@45
  1051
 * flag the last line of a paragraph for being short, so we
ali@45
  1052
 * have to wait until we know that our current line is a
ali@45
  1053
 * "normal" line, then report the _previous_ line if it was too
ali@45
  1054
 * short. We also don't want to report indented lines like
ali@45
  1055
 * chapter heads or formatted quotations. We therefore keep
ali@45
  1056
 * last->len as the length of the last line examined, and
ali@45
  1057
 * last->blen as the length of the last but one, and try to
ali@45
  1058
 * suppress unnecessary warnings by checking that both were of
ali@45
  1059
 * "normal" length. We keep the first character of the last
ali@45
  1060
 * line in last->start, and if it was a space, we assume that
ali@45
  1061
 * the formatting is deliberate. I can't figure out a way to
ali@45
  1062
 * distinguish something like a quoted verse left-aligned or
ali@45
  1063
 * the header or footer of a letter from a paragraph of short
ali@45
  1064
 * lines - maybe if I examined the whole paragraph, and if the
ali@45
  1065
 * para has less than, say, 8 lines and if all lines are short,
ali@45
  1066
 * then just assume it's OK? Need to look at some texts to see
ali@45
  1067
 * how often a formula like this would get the right result.
ali@45
  1068
 */
ali@45
  1069
void check_for_short_line(const char *aline,const struct line_properties *last)
ali@45
  1070
{
ali@70
  1071
    if (g_utf8_strlen(aline,-1)>1 && last->len>1 &&
ali@70
  1072
      last->len<SHORTEST_PG_LINE && last->blen>1 &&
ali@70
  1073
      last->blen>SHORTEST_PG_LINE && last->start!=CHAR_SPACE)
ali@45
  1074
    {
ali@45
  1075
	if (pswit[ECHO_SWITCH])
ali@70
  1076
	    g_print("\n%s\n",prevline);
ali@45
  1077
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1078
	    g_print("    Line %ld column %ld - Short line %ld?\n",
ali@70
  1079
	      linecnt-1,g_utf8_strlen(prevline,-1),g_utf8_strlen(prevline,-1));
ali@45
  1080
	else
ali@45
  1081
	    cnt_short++;
ali@45
  1082
    }
ali@45
  1083
}
ali@45
  1084
ali@45
  1085
/*
ali@46
  1086
 * check_for_starting_punctuation:
ali@46
  1087
 *
ali@46
  1088
 * Look for punctuation other than full ellipses at start of line.
ali@46
  1089
 */
ali@46
  1090
void check_for_starting_punctuation(const char *aline)
ali@46
  1091
{
ali@70
  1092
    if (*aline && g_utf8_strchr(".?!,;:",-1,g_utf8_get_char(aline)) &&
ali@70
  1093
      !g_str_has_prefix(aline,". . ."))
ali@46
  1094
    {
ali@46
  1095
	if (pswit[ECHO_SWITCH])
ali@70
  1096
	    g_print("\n%s\n",aline);
ali@46
  1097
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1098
	    g_print("    Line %ld column 1 - Begins with punctuation?\n",
ali@46
  1099
	      linecnt);
ali@46
  1100
	else
ali@46
  1101
	    cnt_punct++;
ali@46
  1102
    }
ali@46
  1103
}
ali@46
  1104
ali@46
  1105
/*
ali@47
  1106
 * check_for_spaced_emdash:
ali@47
  1107
 *
ali@47
  1108
 * Check for spaced em-dashes.
ali@47
  1109
 *
ali@47
  1110
 * We must check _all_ occurrences of "--" on the line
ali@47
  1111
 * hence the loop - even if the first double-dash is OK
ali@47
  1112
 * there may be another that's wrong later on.
ali@47
  1113
 */
ali@47
  1114
void check_for_spaced_emdash(const char *aline)
ali@47
  1115
{
ali@70
  1116
    const char *s,*t,*next;
ali@70
  1117
    for (s=aline;t=strstr(s,"--");s=next)
ali@47
  1118
    {
ali@70
  1119
	next=g_utf8_next_char(g_utf8_next_char(t));
ali@70
  1120
	if (t>aline && g_utf8_get_char(g_utf8_prev_char(t))==CHAR_SPACE ||
ali@70
  1121
	  g_utf8_get_char(next)==CHAR_SPACE)
ali@47
  1122
	{
ali@47
  1123
	    if (pswit[ECHO_SWITCH])
ali@70
  1124
		g_print("\n%s\n",aline);
ali@47
  1125
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1126
		g_print("    Line %ld column %ld - Spaced em-dash?\n",
ali@70
  1127
		  linecnt,g_utf8_pointer_to_offset(aline,t)+1);
ali@47
  1128
	    else
ali@47
  1129
		cnt_dash++;
ali@47
  1130
	}
ali@47
  1131
    }
ali@47
  1132
}
ali@47
  1133
ali@47
  1134
/*
ali@47
  1135
 * check_for_spaced_dash:
ali@47
  1136
 *
ali@47
  1137
 * Check for spaced dashes.
ali@47
  1138
 */
ali@47
  1139
void check_for_spaced_dash(const char *aline)
ali@47
  1140
{
ali@47
  1141
    const char *s;
ali@47
  1142
    if ((s=strstr(aline," -")))
ali@47
  1143
    {
ali@70
  1144
	if (g_utf8_get_char(g_utf8_next_char(g_utf8_next_char(s)))!='-')
ali@47
  1145
	{
ali@47
  1146
	    if (pswit[ECHO_SWITCH])
ali@70
  1147
		g_print("\n%s\n",aline);
ali@47
  1148
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1149
		g_print("    Line %ld column %ld - Spaced dash?\n",
ali@70
  1150
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@47
  1151
	    else
ali@47
  1152
		cnt_dash++;
ali@47
  1153
	}
ali@47
  1154
    }
ali@47
  1155
    else if ((s=strstr(aline,"- ")))
ali@47
  1156
    {
ali@70
  1157
	if (s==aline || g_utf8_get_char(g_utf8_prev_char(s))!='-')
ali@47
  1158
	{
ali@47
  1159
	    if (pswit[ECHO_SWITCH])
ali@70
  1160
		g_print("\n%s\n",aline);
ali@47
  1161
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1162
		g_print("    Line %ld column %ld - Spaced dash?\n",
ali@70
  1163
		  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@47
  1164
	    else
ali@47
  1165
		cnt_dash++;
ali@47
  1166
	}
ali@47
  1167
    }
ali@47
  1168
}
ali@47
  1169
ali@47
  1170
/*
ali@48
  1171
 * check_for_unmarked_paragraphs:
ali@48
  1172
 *
ali@48
  1173
 * Check for unmarked paragraphs indicated by separate speakers.
ali@48
  1174
 *
ali@48
  1175
 * May well be false positive:
ali@48
  1176
 * "Bravo!" "Wonderful!" called the crowd.
ali@48
  1177
 * but useful all the same.
ali@48
  1178
 */
ali@48
  1179
void check_for_unmarked_paragraphs(const char *aline)
ali@48
  1180
{
ali@48
  1181
    const char *s;
ali@48
  1182
    s=strstr(aline,"\"  \"");
ali@48
  1183
    if (!s)
ali@48
  1184
	s=strstr(aline,"\" \"");
ali@48
  1185
    if (s)
ali@48
  1186
    {
ali@48
  1187
	if (pswit[ECHO_SWITCH])
ali@70
  1188
	    g_print("\n%s\n",aline);
ali@48
  1189
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1190
	    g_print("    Line %ld column %ld - "
ali@70
  1191
	      "Query missing paragraph break?\n",
ali@70
  1192
	      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@48
  1193
	else
ali@48
  1194
	    cnt_punct++;
ali@48
  1195
    }
ali@48
  1196
}
ali@48
  1197
ali@48
  1198
/*
ali@49
  1199
 * check_for_jeebies:
ali@49
  1200
 *
ali@49
  1201
 * Check for "to he" and other easy h/b errors.
ali@49
  1202
 *
ali@49
  1203
 * This is a very inadequate effort on the h/b problem,
ali@49
  1204
 * but the phrase "to he" is always an error, whereas "to
ali@49
  1205
 * be" is quite common.
ali@49
  1206
 * Similarly, '"Quiet!", be said.' is a non-be error
ali@49
  1207
 * "to he" is _not_ always an error!:
ali@49
  1208
 *       "Where they went to he couldn't say."
ali@49
  1209
 * Another false positive:
ali@49
  1210
 *       What would "Cinderella" be without the . . .
ali@49
  1211
 * and another: "If he wants to he can see for himself."
ali@49
  1212
 */
ali@49
  1213
void check_for_jeebies(const char *aline)
ali@49
  1214
{
ali@49
  1215
    const char *s;
ali@49
  1216
    s=strstr(aline," be could ");
ali@49
  1217
    if (!s)
ali@49
  1218
	s=strstr(aline," be would ");
ali@49
  1219
    if (!s)
ali@49
  1220
	s=strstr(aline," was be ");
ali@49
  1221
    if (!s)
ali@49
  1222
	s=strstr(aline," be is ");
ali@49
  1223
    if (!s)
ali@49
  1224
	s=strstr(aline," is be ");
ali@49
  1225
    if (!s)
ali@49
  1226
	s=strstr(aline,"\", 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," to he ");
ali@49
  1233
    if (s)
ali@49
  1234
    {
ali@49
  1235
	if (pswit[ECHO_SWITCH])
ali@70
  1236
	    g_print("\n%s\n",aline);
ali@49
  1237
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1238
	    g_print("    Line %ld column %ld - Query he/be error?\n",
ali@70
  1239
	      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@49
  1240
	else
ali@49
  1241
	    cnt_word++;
ali@49
  1242
    }
ali@49
  1243
    s=strstr(aline," the had ");
ali@49
  1244
    if (!s)
ali@49
  1245
	s=strstr(aline," a had ");
ali@49
  1246
    if (!s)
ali@49
  1247
	s=strstr(aline," they bad ");
ali@49
  1248
    if (!s)
ali@49
  1249
	s=strstr(aline," she bad ");
ali@49
  1250
    if (!s)
ali@49
  1251
	s=strstr(aline," he bad ");
ali@49
  1252
    if (!s)
ali@49
  1253
	s=strstr(aline," you bad ");
ali@49
  1254
    if (!s)
ali@49
  1255
	s=strstr(aline," i bad ");
ali@49
  1256
    if (s)
ali@49
  1257
    {
ali@49
  1258
	if (pswit[ECHO_SWITCH])
ali@70
  1259
	    g_print("\n%s\n",aline);
ali@49
  1260
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1261
	    g_print("    Line %ld column %ld - Query had/bad error?\n",
ali@70
  1262
	      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@49
  1263
	else
ali@49
  1264
	    cnt_word++;
ali@49
  1265
    }
ali@49
  1266
    s=strstr(aline,"; hut ");
ali@49
  1267
    if (!s)
ali@49
  1268
	s=strstr(aline,", hut ");
ali@49
  1269
    if (s)
ali@49
  1270
    {
ali@49
  1271
	if (pswit[ECHO_SWITCH])
ali@70
  1272
	    g_print("\n%s\n",aline);
ali@49
  1273
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1274
	    g_print("    Line %ld column %ld - Query hut/but error?\n",
ali@70
  1275
	      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@49
  1276
	else
ali@49
  1277
	    cnt_word++;
ali@49
  1278
    }
ali@49
  1279
}
ali@49
  1280
ali@49
  1281
/*
ali@50
  1282
 * check_for_mta_from:
ali@50
  1283
 *
ali@50
  1284
 * Special case - angled bracket in front of "From" placed there by an
ali@50
  1285
 * MTA when sending an e-mail.
ali@50
  1286
 */
ali@50
  1287
void check_for_mta_from(const char *aline)
ali@50
  1288
{
ali@50
  1289
    const char *s;
ali@50
  1290
    s=strstr(aline,">From");
ali@50
  1291
    if (s)
ali@50
  1292
    {
ali@50
  1293
	if (pswit[ECHO_SWITCH])
ali@70
  1294
	    g_print("\n%s\n",aline);
ali@50
  1295
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1296
	    g_print("    Line %ld column %ld - "
ali@70
  1297
	      "Query angled bracket with From\n",
ali@70
  1298
	      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@50
  1299
	else
ali@50
  1300
	    cnt_punct++;
ali@50
  1301
    }
ali@50
  1302
}
ali@50
  1303
ali@50
  1304
/*
ali@51
  1305
 * check_for_orphan_character:
ali@51
  1306
 *
ali@51
  1307
 * Check for a single character line -
ali@51
  1308
 * often an overflow from bad wrapping.
ali@51
  1309
 */
ali@51
  1310
void check_for_orphan_character(const char *aline)
ali@51
  1311
{
ali@70
  1312
    gunichar c;
ali@70
  1313
    c=g_utf8_get_char(aline);
ali@70
  1314
    if (c && !*g_utf8_next_char(aline))
ali@51
  1315
    {
ali@70
  1316
	if (c=='I' || c=='V' || c=='X' || c=='L' || g_unichar_isdigit(c))
ali@51
  1317
	    ; /* Nothing - ignore numerals alone on a line. */
ali@51
  1318
	else
ali@51
  1319
	{
ali@51
  1320
	    if (pswit[ECHO_SWITCH])
ali@70
  1321
		g_print("\n%s\n",aline);
ali@51
  1322
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1323
		g_print("    Line %ld column 1 - Query single character line\n",
ali@51
  1324
		  linecnt);
ali@51
  1325
	    else
ali@51
  1326
		cnt_punct++;
ali@51
  1327
	}
ali@51
  1328
    }
ali@51
  1329
}
ali@51
  1330
ali@51
  1331
/*
ali@52
  1332
 * check_for_pling_scanno:
ali@52
  1333
 *
ali@52
  1334
 * Check for I" - often should be !
ali@52
  1335
 */
ali@52
  1336
void check_for_pling_scanno(const char *aline)
ali@52
  1337
{
ali@52
  1338
    const char *s;
ali@52
  1339
    s=strstr(aline," I\"");
ali@52
  1340
    if (s)
ali@52
  1341
    {
ali@52
  1342
	if (pswit[ECHO_SWITCH])
ali@70
  1343
	    g_print("\n%s\n",aline);
ali@52
  1344
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  1345
	    g_print("    Line %ld column %ld - Query I=exclamation mark?\n",
ali@70
  1346
	      linecnt,g_utf8_pointer_to_offset(aline,s));
ali@52
  1347
	else
ali@52
  1348
	    cnt_punct++;
ali@52
  1349
    }
ali@52
  1350
}
ali@52
  1351
ali@52
  1352
/*
ali@53
  1353
 * check_for_extra_period:
ali@53
  1354
 *
ali@53
  1355
 * Check for period without a capital letter. Cut-down from gutspell.
ali@53
  1356
 * Only works when it happens on a single line.
ali@53
  1357
 */
ali@53
  1358
void check_for_extra_period(const char *aline,const struct warnings *warnings)
ali@53
  1359
{
ali@99
  1360
    const char *s,*t,*s1,*sprev;
ali@69
  1361
    int i;
ali@70
  1362
    gsize len;
ali@69
  1363
    gboolean istypo;
ali@69
  1364
    gchar *testword;
ali@99
  1365
    gunichar c,nc,pc,*decomposition;
ali@53
  1366
    if (pswit[PARANOID_SWITCH])
ali@53
  1367
    {
ali@70
  1368
	for (t=aline;t=strstr(t,". ");)
ali@53
  1369
	{
ali@69
  1370
	    if (t==aline)
ali@53
  1371
	    {
ali@70
  1372
		t=g_utf8_next_char(t);
ali@53
  1373
		/* start of line punctuation is handled elsewhere */
ali@53
  1374
		continue;
ali@53
  1375
	    }
ali@70
  1376
	    if (!g_unichar_isalpha(g_utf8_get_char(g_utf8_prev_char(t))))
ali@53
  1377
	    {
ali@70
  1378
		t=g_utf8_next_char(t);
ali@53
  1379
		continue;
ali@53
  1380
	    }
ali@53
  1381
	    if (warnings->isDutch)
ali@53
  1382
	    {
ali@53
  1383
		/* For Frank & Jeroen -- 's Middags case */
ali@70
  1384
		gunichar c2,c3,c4,c5;
ali@70
  1385
		c2=g_utf8_get_char(g_utf8_offset_to_pointer(t,2));
ali@70
  1386
		c3=g_utf8_get_char(g_utf8_offset_to_pointer(t,3));
ali@70
  1387
		c4=g_utf8_get_char(g_utf8_offset_to_pointer(t,4));
ali@70
  1388
		c5=g_utf8_get_char(g_utf8_offset_to_pointer(t,5));
ali@99
  1389
		if (CHAR_IS_APOSTROPHE(c2) &&
ali@99
  1390
		  g_unichar_islower(c3) && c4==CHAR_SPACE &&
ali@99
  1391
		  g_unichar_isupper(c5))
ali@53
  1392
		{
ali@70
  1393
		    t=g_utf8_next_char(t);
ali@53
  1394
		    continue;
ali@53
  1395
		}
ali@53
  1396
	    }
ali@70
  1397
	    s1=g_utf8_next_char(g_utf8_next_char(t));
ali@70
  1398
	    while (*s1 && !g_unichar_isalpha(g_utf8_get_char(s1)) &&
ali@70
  1399
	      !isdigit(g_utf8_get_char(s1)))
ali@70
  1400
		s1=g_utf8_next_char(s1);
ali@70
  1401
	    if (g_unichar_islower(g_utf8_get_char(s1)))
ali@53
  1402
	    {
ali@53
  1403
		/* we have something to investigate */
ali@69
  1404
		istypo=TRUE;
ali@53
  1405
		/* so let's go back and find out */
ali@99
  1406
		nc=g_utf8_get_char(t);
ali@99
  1407
		s1=g_utf8_prev_char(t);
ali@99
  1408
		c=g_utf8_get_char(s1);
ali@99
  1409
		sprev=g_utf8_prev_char(s1);
ali@99
  1410
		pc=g_utf8_get_char(sprev);
ali@99
  1411
		while (s1>=aline &&
ali@99
  1412
		  (g_unichar_isalpha(c) || g_unichar_isdigit(c) ||
ali@99
  1413
		  g_unichar_isalpha(pc) && CHAR_IS_APOSTROPHE(c) &&
ali@99
  1414
		  g_unichar_isalpha(nc)))
ali@99
  1415
		{
ali@99
  1416
		    nc=c;
ali@99
  1417
		    s1=sprev;
ali@99
  1418
		    c=pc;
ali@99
  1419
		    sprev=g_utf8_prev_char(s1);
ali@99
  1420
		    pc=g_utf8_get_char(sprev);
ali@99
  1421
		}
ali@70
  1422
		s1=g_utf8_next_char(s1);
ali@69
  1423
		s=strchr(s1,'.');
ali@69
  1424
		if (s)
ali@69
  1425
		    testword=g_strndup(s1,s-s1);
ali@69
  1426
		else
ali@69
  1427
		    testword=g_strdup(s1);
ali@53
  1428
		for (i=0;*abbrev[i];i++)
ali@53
  1429
		    if (!strcmp(testword,abbrev[i]))
ali@69
  1430
			istypo=FALSE;
ali@70
  1431
		if (g_unichar_isdigit(g_utf8_get_char(testword)))
ali@69
  1432
		    istypo=FALSE;
ali@70
  1433
		if (!*g_utf8_next_char(testword))
ali@69
  1434
		    istypo=FALSE;
ali@53
  1435
		if (isroman(testword))
ali@69
  1436
		    istypo=FALSE;
ali@53
  1437
		if (istypo)
ali@53
  1438
		{
ali@69
  1439
		    istypo=FALSE;
ali@70
  1440
		    for (s=testword;*s;s=g_utf8_next_char(s))
ali@70
  1441
		    {
ali@70
  1442
			decomposition=g_unicode_canonical_decomposition(
ali@70
  1443
			  g_utf8_get_char(s),&len);
ali@70
  1444
			if (g_utf8_strchr("aeiou",-1,decomposition[0]))
ali@69
  1445
			    istypo=TRUE;
ali@70
  1446
			g_free(decomposition);
ali@70
  1447
		    }
ali@53
  1448
		}
ali@69
  1449
		if (istypo &&
ali@69
  1450
		  (pswit[VERBOSE_SWITCH] || !g_tree_lookup(qperiod,testword)))
ali@53
  1451
		{
ali@69
  1452
		    g_tree_insert(qperiod,g_strdup(testword),
ali@69
  1453
		      GINT_TO_POINTER(1));
ali@69
  1454
		    if (pswit[ECHO_SWITCH])
ali@70
  1455
			g_print("\n%s\n",aline);
ali@69
  1456
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1457
			g_print("    Line %ld column %ld - Extra period?\n",
ali@70
  1458
			  linecnt,g_utf8_pointer_to_offset(aline,t)+1);
ali@69
  1459
		    else
ali@69
  1460
			cnt_punct++;
ali@53
  1461
		}
ali@69
  1462
		g_free(testword);
ali@53
  1463
	    }
ali@70
  1464
	    t=g_utf8_next_char(t);
ali@53
  1465
	}
ali@53
  1466
    }
ali@53
  1467
}
ali@53
  1468
ali@53
  1469
/*
ali@54
  1470
 * check_for_following_punctuation:
ali@54
  1471
 *
ali@54
  1472
 * Check for words usually not followed by punctuation.
ali@54
  1473
 */
ali@54
  1474
void check_for_following_punctuation(const char *aline)
ali@54
  1475
{
ali@54
  1476
    int i;
ali@54
  1477
    const char *s,*wordstart;
ali@70
  1478
    gunichar c;
ali@69
  1479
    gchar *inword,*t;
ali@54
  1480
    if (pswit[TYPO_SWITCH])
ali@54
  1481
    {
ali@54
  1482
	for (s=aline;*s;)
ali@54
  1483
	{
ali@54
  1484
	    wordstart=s;
ali@69
  1485
	    t=getaword(&s);
ali@69
  1486
	    if (!*t)
ali@69
  1487
	    {
ali@69
  1488
		g_free(t);
ali@54
  1489
		continue;
ali@69
  1490
	    }
ali@70
  1491
	    inword=g_utf8_strdown(t,-1);
ali@69
  1492
	    g_free(t);
ali@54
  1493
	    for (i=0;*nocomma[i];i++)
ali@54
  1494
		if (!strcmp(inword,nocomma[i]))
ali@54
  1495
		{
ali@70
  1496
		    c=g_utf8_get_char(s);
ali@70
  1497
		    if (c==',' || c==';' || c==':')
ali@54
  1498
		    {
ali@54
  1499
			if (pswit[ECHO_SWITCH])
ali@70
  1500
			    g_print("\n%s\n",aline);
ali@54
  1501
			if (!pswit[OVERVIEW_SWITCH])
ali@70
  1502
			    g_print("    Line %ld column %ld - "
ali@54
  1503
			      "Query punctuation after %s?\n",
ali@70
  1504
			      linecnt,g_utf8_pointer_to_offset(aline,s)+1,
ali@70
  1505
			      inword);
ali@54
  1506
			else
ali@54
  1507
			    cnt_punct++;
ali@54
  1508
		    }
ali@54
  1509
		}
ali@54
  1510
	    for (i=0;*noperiod[i];i++)
ali@54
  1511
		if (!strcmp(inword,noperiod[i]))
ali@54
  1512
		{
ali@70
  1513
		    c=g_utf8_get_char(s);
ali@70
  1514
		    if (c=='.' || c=='!')
ali@54
  1515
		    {
ali@54
  1516
			if (pswit[ECHO_SWITCH])
ali@70
  1517
			    g_print("\n%s\n",aline);
ali@54
  1518
			if (!pswit[OVERVIEW_SWITCH])
ali@70
  1519
			    g_print("    Line %ld column %ld - "
ali@54
  1520
			      "Query punctuation after %s?\n",
ali@70
  1521
			      linecnt,g_utf8_pointer_to_offset(aline,s)+1,
ali@70
  1522
			      inword);
ali@54
  1523
			else
ali@54
  1524
			    cnt_punct++;
ali@54
  1525
		    }
ali@54
  1526
		}
ali@69
  1527
	    g_free(inword);
ali@54
  1528
	}
ali@54
  1529
    }
ali@54
  1530
}
ali@54
  1531
ali@54
  1532
/*
ali@55
  1533
 * check_for_typos:
ali@55
  1534
 *
ali@55
  1535
 * Check for commonly mistyped words,
ali@55
  1536
 * and digits like 0 for O in a word.
ali@55
  1537
 */
ali@55
  1538
void check_for_typos(const char *aline,struct warnings *warnings)
ali@55
  1539
{
ali@70
  1540
    const char *s,*t,*nt,*wordstart;
ali@70
  1541
    gchar *inword;
ali@70
  1542
    gunichar *decomposition;
ali@70
  1543
    gchar *testword;
ali@70
  1544
    int i,vowel,consonant,*dupcnt;
ali@70
  1545
    gboolean isdup,istypo,alower;
ali@99
  1546
    gunichar c,pc;
ali@70
  1547
    long offset,len;
ali@70
  1548
    gsize decomposition_len;
ali@55
  1549
    for (s=aline;*s;)
ali@55
  1550
    {
ali@55
  1551
	wordstart=s;
ali@69
  1552
	inword=getaword(&s);
ali@55
  1553
	if (!*inword)
ali@69
  1554
	{
ali@69
  1555
	    g_free(inword);
ali@55
  1556
	    continue; /* don't bother with empty lines */
ali@69
  1557
	}
ali@55
  1558
	if (mixdigit(inword))
ali@55
  1559
	{
ali@55
  1560
	    if (pswit[ECHO_SWITCH])
ali@70
  1561
		g_print("\n%s\n",aline);
ali@55
  1562
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1563
		g_print("    Line %ld column %ld - Query digit in %s\n",
ali@70
  1564
		  linecnt,g_utf8_pointer_to_offset(aline,wordstart)+1,inword);
ali@55
  1565
	    else
ali@55
  1566
		cnt_word++;
ali@55
  1567
	}
ali@55
  1568
	/*
ali@55
  1569
	 * Put the word through a series of tests for likely typos and OCR
ali@55
  1570
	 * errors.
ali@55
  1571
	 */
ali@69
  1572
	if (pswit[TYPO_SWITCH] || pswit[USERTYPO_SWITCH])
ali@55
  1573
	{
ali@69
  1574
	    istypo=FALSE;
ali@70
  1575
	    alower=FALSE;
ali@70
  1576
	    for (t=inword;*t;t=g_utf8_next_char(t))
ali@55
  1577
	    {
ali@70
  1578
		c=g_utf8_get_char(t);
ali@70
  1579
		nt=g_utf8_next_char(t);
ali@55
  1580
		/* lowercase for testing */
ali@70
  1581
		if (g_unichar_islower(c))
ali@70
  1582
		    alower=TRUE;
ali@70
  1583
		if (alower && (g_unichar_isupper(c) || g_unichar_istitle(c)))
ali@55
  1584
		{
ali@55
  1585
		    /*
ali@55
  1586
		     * We have an uppercase mid-word. However, there are
ali@55
  1587
		     * common cases:
ali@55
  1588
		     *   Mac and Mc like McGill
ali@55
  1589
		     *   French contractions like l'Abbe
ali@55
  1590
		     */
ali@70
  1591
		    offset=g_utf8_pointer_to_offset(inword,t);
ali@99
  1592
		    if (offset>0)
ali@99
  1593
			pc=g_utf8_get_char(g_utf8_prev_char(t));
ali@99
  1594
		    else
ali@99
  1595
			pc='\0';
ali@70
  1596
		    if (offset==2 && c=='m' && g_utf8_get_char(nt)=='c' ||
ali@70
  1597
		      offset==3 && c=='m' && g_utf8_get_char(nt)=='a' &&
ali@70
  1598
		      g_utf8_get_char(g_utf8_next_char(nt))=='c' ||
ali@99
  1599
		      CHAR_IS_APOSTROPHE(pc))
ali@55
  1600
			; /* do nothing! */
ali@55
  1601
		    else
ali@69
  1602
			istypo=TRUE;
ali@55
  1603
		}
ali@55
  1604
	    }
ali@70
  1605
	    testword=g_utf8_casefold(inword,-1);
ali@69
  1606
	}
ali@69
  1607
	if (pswit[TYPO_SWITCH])
ali@69
  1608
	{
ali@55
  1609
	    /*
ali@55
  1610
	     * Check for certain unlikely two-letter combinations at word
ali@55
  1611
	     * start and end.
ali@55
  1612
	     */
ali@70
  1613
	    len=g_utf8_strlen(testword,-1);
ali@70
  1614
	    if (len>1)
ali@55
  1615
	    {
ali@55
  1616
		for (i=0;*nostart[i];i++)
ali@70
  1617
		    if (g_str_has_prefix(testword,nostart[i]))
ali@69
  1618
			istypo=TRUE;
ali@55
  1619
		for (i=0;*noend[i];i++)
ali@70
  1620
		    if (g_str_has_suffix(testword,noend[i]))
ali@69
  1621
			istypo=TRUE;
ali@55
  1622
	    }
ali@55
  1623
	    /* ght is common, gbt never. Like that. */
ali@55
  1624
	    if (strstr(testword,"cb"))
ali@69
  1625
		istypo=TRUE;
ali@55
  1626
	    if (strstr(testword,"gbt"))
ali@69
  1627
		istypo=TRUE;
ali@55
  1628
	    if (strstr(testword,"pbt"))
ali@69
  1629
		istypo=TRUE;
ali@55
  1630
	    if (strstr(testword,"tbs"))
ali@69
  1631
		istypo=TRUE;
ali@55
  1632
	    if (strstr(testword,"mrn"))
ali@69
  1633
		istypo=TRUE;
ali@55
  1634
	    if (strstr(testword,"ahle"))
ali@69
  1635
		istypo=TRUE;
ali@55
  1636
	    if (strstr(testword,"ihle"))
ali@69
  1637
		istypo=TRUE;
ali@55
  1638
	    /*
ali@55
  1639
	     * "TBE" does happen - like HEARTBEAT - but uncommon.
ali@55
  1640
	     * Also "TBI" - frostbite, outbid - but uncommon.
ali@55
  1641
	     * Similarly "ii" like Hawaii, or Pompeii, and in Roman
ali@55
  1642
	     * numerals, but "ii" is a common scanno.
ali@55
  1643
	     */
ali@55
  1644
	    if (strstr(testword,"tbi"))
ali@69
  1645
		istypo=TRUE;
ali@55
  1646
	    if (strstr(testword,"tbe"))
ali@69
  1647
		istypo=TRUE;
ali@55
  1648
	    if (strstr(testword,"ii"))
ali@69
  1649
		istypo=TRUE;
ali@55
  1650
	    /*
ali@55
  1651
	     * Check for no vowels or no consonants.
ali@55
  1652
	     * If none, flag a typo.
ali@55
  1653
	     */
ali@70
  1654
	    if (!istypo && len>1)
ali@55
  1655
	    {
ali@55
  1656
		vowel=consonant=0;
ali@70
  1657
		for (t=testword;*t;t=g_utf8_next_char(t))
ali@55
  1658
		{
ali@70
  1659
		    c=g_utf8_get_char(t);
ali@70
  1660
		    decomposition=
ali@70
  1661
		      g_unicode_canonical_decomposition(c,&decomposition_len);
ali@70
  1662
		    if (c=='y' || g_unichar_isdigit(c))
ali@55
  1663
		    {
ali@55
  1664
			/* Yah, this is loose. */
ali@55
  1665
			vowel++;
ali@55
  1666
			consonant++;
ali@55
  1667
		    }
ali@70
  1668
		    else if (g_utf8_strchr("aeiou",-1,decomposition[0]))
ali@55
  1669
			vowel++;
ali@55
  1670
		    else
ali@55
  1671
			consonant++;
ali@70
  1672
		    g_free(decomposition);
ali@55
  1673
		}
ali@55
  1674
		if (!vowel || !consonant)
ali@69
  1675
		    istypo=TRUE;
ali@55
  1676
	    }
ali@55
  1677
	    /*
ali@55
  1678
	     * Now exclude the word from being reported if it's in
ali@55
  1679
	     * the okword list.
ali@55
  1680
	     */
ali@55
  1681
	    for (i=0;*okword[i];i++)
ali@55
  1682
		if (!strcmp(testword,okword[i]))
ali@69
  1683
		    istypo=FALSE;
ali@55
  1684
	    /*
ali@55
  1685
	     * What looks like a typo may be a Roman numeral.
ali@55
  1686
	     * Exclude these.
ali@55
  1687
	     */
ali@55
  1688
	    if (istypo && isroman(testword))
ali@69
  1689
		istypo=FALSE;
ali@55
  1690
	    /* Check the manual list of typos. */
ali@55
  1691
	    if (!istypo)
ali@55
  1692
		for (i=0;*typo[i];i++)
ali@55
  1693
		    if (!strcmp(testword,typo[i]))
ali@69
  1694
			istypo=TRUE;
ali@55
  1695
	    /*
ali@55
  1696
	     * Check lowercase s, l, i and m - special cases.
ali@55
  1697
	     *   "j" - often a semi-colon gone wrong.
ali@55
  1698
	     *   "d" for a missing apostrophe - he d
ali@55
  1699
	     *   "n" for "in"
ali@55
  1700
	     */
ali@70
  1701
	    if (!istypo && len==1 &&
ali@70
  1702
	      g_utf8_strchr("slmijdn",-1,g_utf8_get_char(inword)))
ali@69
  1703
		istypo=TRUE;
ali@55
  1704
	    if (istypo)
ali@55
  1705
	    {
ali@69
  1706
		dupcnt=g_tree_lookup(qword,testword);
ali@69
  1707
		if (dupcnt)
ali@69
  1708
		{
ali@69
  1709
		    (*dupcnt)++;
ali@69
  1710
		    isdup=!pswit[VERBOSE_SWITCH];
ali@69
  1711
		}
ali@69
  1712
		else
ali@69
  1713
		{
ali@69
  1714
		    dupcnt=g_new0(int,1);
ali@69
  1715
		    g_tree_insert(qword,g_strdup(testword),dupcnt);
ali@69
  1716
		    isdup=FALSE;
ali@69
  1717
		}
ali@55
  1718
		if (!isdup)
ali@55
  1719
		{
ali@55
  1720
		    if (pswit[ECHO_SWITCH])
ali@70
  1721
			g_print("\n%s\n",aline);
ali@55
  1722
		    if (!pswit[OVERVIEW_SWITCH])
ali@55
  1723
		    {
ali@70
  1724
			g_print("    Line %ld column %ld - Query word %s",
ali@70
  1725
			  linecnt,g_utf8_pointer_to_offset(aline,wordstart)+1,
ali@70
  1726
			  inword);
ali@69
  1727
			if (!pswit[VERBOSE_SWITCH])
ali@70
  1728
			    g_print(" - not reporting duplicates");
ali@70
  1729
			g_print("\n");
ali@55
  1730
		    }
ali@55
  1731
		    else
ali@55
  1732
			cnt_word++;
ali@55
  1733
		}
ali@55
  1734
	    }
ali@55
  1735
	}
ali@55
  1736
	/* check the user's list of typos */
ali@69
  1737
	if (!istypo && usertypo && g_tree_lookup(usertypo,testword))
ali@69
  1738
	{
ali@69
  1739
	    if (pswit[ECHO_SWITCH])
ali@70
  1740
		g_print("\n%s\n",aline);
ali@69
  1741
	    if (!pswit[OVERVIEW_SWITCH])  
ali@70
  1742
		g_print("    Line %ld column %ld - Query possible scanno %s\n",
ali@70
  1743
		  linecnt,g_utf8_pointer_to_offset(aline,wordstart)+2,inword);
ali@69
  1744
	}
ali@69
  1745
	if (pswit[TYPO_SWITCH] || pswit[USERTYPO_SWITCH])
ali@69
  1746
	    g_free(testword);
ali@55
  1747
	if (pswit[PARANOID_SWITCH] && warnings->digit)
ali@55
  1748
	{
ali@55
  1749
	    /* In paranoid mode, query all 0 and 1 standing alone. */
ali@55
  1750
	    if (!strcmp(inword,"0") || !strcmp(inword,"1"))
ali@55
  1751
	    {
ali@55
  1752
		if (pswit[ECHO_SWITCH])
ali@70
  1753
		    g_print("\n%s\n",aline);
ali@55
  1754
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  1755
		    g_print("    Line %ld column %ld - Query standalone %s\n",
ali@70
  1756
		      linecnt,g_utf8_pointer_to_offset(aline,wordstart)+2,
ali@70
  1757
		      inword);
ali@55
  1758
		else
ali@55
  1759
		    cnt_word++;
ali@55
  1760
	    }
ali@55
  1761
	}
ali@69
  1762
	g_free(inword);
ali@55
  1763
    }
ali@55
  1764
}
ali@55
  1765
ali@56
  1766
/*
ali@56
  1767
 * check_for_misspaced_punctuation:
ali@56
  1768
 *
ali@56
  1769
 * Look for added or missing spaces around punctuation and quotes.
ali@56
  1770
 * If there is a punctuation character like ! with no space on
ali@56
  1771
 * either side, suspect a missing!space. If there are spaces on
ali@56
  1772
 * both sides , assume a typo. If we see a double quote with no
ali@56
  1773
 * space or punctuation on either side of it, assume unspaced
ali@56
  1774
 * quotes "like"this.
ali@56
  1775
 */
ali@56
  1776
void check_for_misspaced_punctuation(const char *aline,
ali@69
  1777
  struct parities *parities,gboolean isemptyline)
ali@56
  1778
{
ali@69
  1779
    gboolean isacro,isellipsis;
ali@56
  1780
    const char *s;
ali@70
  1781
    gunichar c,nc,pc,n2c;
ali@70
  1782
    c=g_utf8_get_char(aline);
ali@70
  1783
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  1784
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@56
  1785
    {
ali@70
  1786
	pc=c;
ali@70
  1787
	c=nc;
ali@70
  1788
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@56
  1789
	/* For each character in the line after the first. */
ali@70
  1790
	if (g_utf8_strchr(".?!,;:_",-1,c))  /* if it's punctuation */
ali@56
  1791
	{
ali@56
  1792
	    /* we need to suppress warnings for acronyms like M.D. */
ali@69
  1793
	    isacro=FALSE;
ali@56
  1794
	    /* we need to suppress warnings for ellipsis . . . */
ali@69
  1795
	    isellipsis=FALSE;
ali@70
  1796
	    /*
ali@70
  1797
	     * If there are letters on both sides of it or
ali@70
  1798
	     * if it's strict punctuation followed by an alpha.
ali@70
  1799
	     */
ali@70
  1800
	    if (g_unichar_isalpha(nc) && (g_unichar_isalpha(pc) ||
ali@70
  1801
	      g_utf8_strchr("?!,;:",-1,c)))
ali@56
  1802
	    {
ali@70
  1803
		if (c=='.')
ali@56
  1804
		{
ali@70
  1805
		    if (g_utf8_pointer_to_offset(aline,s)>2 &&
ali@70
  1806
		      g_utf8_get_char(g_utf8_offset_to_pointer(s,-2))=='.')
ali@69
  1807
			isacro=TRUE;
ali@70
  1808
		    n2c=g_utf8_get_char(g_utf8_next_char(g_utf8_next_char(s)));
ali@70
  1809
		    if (nc && n2c=='.')
ali@69
  1810
			isacro=TRUE;
ali@56
  1811
		}
ali@56
  1812
		if (!isacro)
ali@56
  1813
		{
ali@56
  1814
		    if (pswit[ECHO_SWITCH])
ali@70
  1815
			g_print("\n%s\n",aline);
ali@56
  1816
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1817
			g_print("    Line %ld column %ld - Missing space?\n",
ali@70
  1818
			  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1819
		    else
ali@56
  1820
			cnt_punct++;
ali@56
  1821
		}
ali@56
  1822
	    }
ali@70
  1823
	    if (pc==CHAR_SPACE && (nc==CHAR_SPACE || !nc))
ali@56
  1824
	    {
ali@56
  1825
		/*
ali@56
  1826
		 * If there are spaces on both sides,
ali@56
  1827
		 * or space before and end of line.
ali@56
  1828
		 */
ali@70
  1829
		if (c=='.')
ali@56
  1830
		{
ali@70
  1831
		    if (g_utf8_pointer_to_offset(aline,s)>2 &&
ali@70
  1832
		      g_utf8_get_char(g_utf8_offset_to_pointer(s,-2))=='.')
ali@69
  1833
			isellipsis=TRUE;
ali@70
  1834
		    n2c=g_utf8_get_char(g_utf8_next_char(g_utf8_next_char(s)));
ali@70
  1835
		    if (nc && n2c=='.')
ali@69
  1836
			isellipsis=TRUE;
ali@56
  1837
		}
ali@56
  1838
		if (!isemptyline && !isellipsis)
ali@56
  1839
		{
ali@56
  1840
		    if (pswit[ECHO_SWITCH])
ali@70
  1841
			g_print("\n%s\n",aline);
ali@56
  1842
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1843
			g_print("    Line %ld column %ld - "
ali@70
  1844
			  "Spaced punctuation?\n",linecnt,
ali@70
  1845
			  g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1846
		    else
ali@56
  1847
			cnt_punct++;
ali@56
  1848
		}
ali@56
  1849
	    }
ali@56
  1850
	}
ali@56
  1851
    }
ali@56
  1852
    /* Split out the characters that CANNOT be preceded by space. */
ali@70
  1853
    c=g_utf8_get_char(aline);
ali@70
  1854
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  1855
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@56
  1856
    {
ali@70
  1857
	pc=c;
ali@70
  1858
	c=nc;
ali@70
  1859
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@56
  1860
	/* for each character in the line after the first */
ali@70
  1861
	if (g_utf8_strchr("?!,;:",-1,c))
ali@56
  1862
	{
ali@56
  1863
	    /* if it's punctuation that _cannot_ have a space before it */
ali@70
  1864
	    if (pc==CHAR_SPACE && !isemptyline && nc!=CHAR_SPACE)
ali@56
  1865
	    {
ali@56
  1866
		/*
ali@70
  1867
		 * If nc DOES == space,
ali@56
  1868
		 * it was already reported just above.
ali@56
  1869
		 */
ali@56
  1870
		if (pswit[ECHO_SWITCH])
ali@70
  1871
		    g_print("\n%s\n",aline);
ali@56
  1872
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  1873
		    g_print("    Line %ld column %ld - Spaced punctuation?\n",
ali@70
  1874
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1875
		else
ali@56
  1876
		    cnt_punct++;
ali@56
  1877
	    }
ali@56
  1878
	}
ali@56
  1879
    }
ali@56
  1880
    /*
ali@56
  1881
     * Special case " .X" where X is any alpha.
ali@56
  1882
     * This plugs a hole in the acronym code above.
ali@56
  1883
     * Inelegant, but maintainable.
ali@56
  1884
     */
ali@70
  1885
    c=g_utf8_get_char(aline);
ali@70
  1886
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  1887
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@56
  1888
    {
ali@70
  1889
	pc=c;
ali@70
  1890
	c=nc;
ali@70
  1891
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@56
  1892
	/* for each character in the line after the first */
ali@70
  1893
	if (c=='.')
ali@56
  1894
	{
ali@56
  1895
	    /* if it's a period */
ali@70
  1896
	    if (pc==CHAR_SPACE && g_unichar_isalpha(nc))
ali@56
  1897
	    {
ali@56
  1898
		/*
ali@56
  1899
		 * If the period follows a space and
ali@56
  1900
		 * is followed by a letter.
ali@56
  1901
		 */
ali@56
  1902
		if (pswit[ECHO_SWITCH])
ali@70
  1903
		    g_print("\n%s\n",aline);
ali@56
  1904
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  1905
		    g_print("    Line %ld column %ld - Spaced punctuation?\n",
ali@70
  1906
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1907
		else
ali@56
  1908
		    cnt_punct++;
ali@56
  1909
	    }
ali@56
  1910
	}
ali@56
  1911
    }
ali@70
  1912
    c=g_utf8_get_char(aline);
ali@70
  1913
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  1914
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@56
  1915
    {
ali@70
  1916
	pc=c;
ali@70
  1917
	c=nc;
ali@70
  1918
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@56
  1919
	/* for each character in the line after the first */
ali@70
  1920
	if (c==CHAR_DQUOTE)
ali@56
  1921
	{
ali@70
  1922
	    if (!g_utf8_strchr(" _-.'`,;:!/([{?}])",-1,pc) &&
ali@70
  1923
	      !g_utf8_strchr(" _-.'`,;:!/([{?}])",-1,nc) && nc ||
ali@70
  1924
	      !g_utf8_strchr(" _-([{'`",-1,pc) && g_unichar_isalpha(nc))
ali@56
  1925
	    {
ali@56
  1926
		if (pswit[ECHO_SWITCH])
ali@70
  1927
		    g_print("\n%s\n",aline);
ali@56
  1928
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  1929
		    g_print("    Line %ld column %ld - Unspaced quotes?\n",
ali@70
  1930
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1931
		else
ali@56
  1932
		    cnt_punct++;
ali@56
  1933
	    }
ali@56
  1934
	}
ali@56
  1935
    }
ali@56
  1936
    /* Check parity of quotes. */
ali@70
  1937
    nc=g_utf8_get_char(aline);
ali@70
  1938
    for (s=aline;*s;s=g_utf8_next_char(s))
ali@56
  1939
    {
ali@70
  1940
	c=nc;
ali@70
  1941
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@70
  1942
	if (c==CHAR_DQUOTE)
ali@56
  1943
	{
ali@56
  1944
	    parities->dquote=!parities->dquote;
ali@56
  1945
	    if (!parities->dquote)
ali@56
  1946
	    {
ali@56
  1947
		/* parity even */
ali@70
  1948
		if (!g_utf8_strchr("_-.'`/,;:!?)]} ",-1,nc))
ali@56
  1949
		{
ali@56
  1950
		    if (pswit[ECHO_SWITCH])
ali@70
  1951
			g_print("\n%s\n",aline);
ali@56
  1952
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1953
			g_print("    Line %ld column %ld - "
ali@70
  1954
			  "Wrongspaced quotes?\n",
ali@70
  1955
			  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1956
		    else
ali@56
  1957
			cnt_punct++;
ali@56
  1958
		}
ali@56
  1959
	    }
ali@56
  1960
	    else
ali@56
  1961
	    {
ali@56
  1962
		/* parity odd */
ali@70
  1963
		if (!g_unichar_isalpha(nc) && !isdigit(nc) &&
ali@70
  1964
		  !g_utf8_strchr("_-/.'`([{$",-1,nc) || !nc)
ali@56
  1965
		{
ali@56
  1966
		    if (pswit[ECHO_SWITCH])
ali@70
  1967
			g_print("\n%s\n",aline);
ali@56
  1968
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1969
			g_print("    Line %ld column %ld - "
ali@70
  1970
			  "Wrongspaced quotes?\n",
ali@70
  1971
			  linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  1972
		    else
ali@56
  1973
			cnt_punct++;
ali@56
  1974
		}
ali@56
  1975
	    }
ali@56
  1976
	}
ali@56
  1977
    }
ali@70
  1978
    if (g_utf8_get_char(aline)==CHAR_DQUOTE)
ali@56
  1979
    {
ali@70
  1980
	if (g_utf8_strchr(",;:!?)]} ",-1,
ali@70
  1981
	  g_utf8_get_char(g_utf8_next_char(aline))))
ali@56
  1982
	{
ali@56
  1983
	    if (pswit[ECHO_SWITCH])
ali@70
  1984
		g_print("\n%s\n",aline);
ali@56
  1985
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  1986
		g_print("    Line %ld column 1 - Wrongspaced quotes?\n",
ali@56
  1987
		  linecnt);
ali@56
  1988
	    else
ali@56
  1989
		cnt_punct++;
ali@56
  1990
	}
ali@56
  1991
    }
ali@56
  1992
    if (pswit[SQUOTE_SWITCH])
ali@56
  1993
    {
ali@70
  1994
	nc=g_utf8_get_char(aline);
ali@70
  1995
	for (s=aline;*s;s=g_utf8_next_char(s))
ali@56
  1996
	{
ali@70
  1997
	    c=nc;
ali@70
  1998
	    nc=g_utf8_get_char(g_utf8_next_char(s));
ali@99
  1999
	    if (CHAR_IS_SQUOTE(c) && (s==aline || s>aline &&
ali@70
  2000
	      !g_unichar_isalpha(g_utf8_get_char(g_utf8_prev_char(s))) ||
ali@70
  2001
	      !g_unichar_isalpha(nc)))
ali@56
  2002
	    {
ali@56
  2003
		parities->squote=!parities->squote;
ali@56
  2004
		if (!parities->squote)
ali@56
  2005
		{
ali@56
  2006
		    /* parity even */
ali@70
  2007
		    if (!g_utf8_strchr("_-.'`/\",;:!?)]} ",-1,nc))
ali@56
  2008
		    {
ali@56
  2009
			if (pswit[ECHO_SWITCH])
ali@70
  2010
			    g_print("\n%s\n",aline);
ali@56
  2011
			if (!pswit[OVERVIEW_SWITCH])
ali@70
  2012
			    g_print("    Line %ld column %ld - "
ali@56
  2013
			      "Wrongspaced singlequotes?\n",
ali@70
  2014
			      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  2015
			else
ali@56
  2016
			    cnt_punct++;
ali@56
  2017
		    }
ali@56
  2018
		}
ali@56
  2019
		else
ali@56
  2020
		{
ali@56
  2021
		    /* parity odd */
ali@70
  2022
		    if (!g_unichar_isalpha(nc) && !isdigit(nc) &&
ali@70
  2023
		      !g_utf8_strchr("_-/\".'`",-1,nc) || !nc)
ali@56
  2024
		    {
ali@56
  2025
			if (pswit[ECHO_SWITCH])
ali@70
  2026
			    g_print("\n%s\n",aline);
ali@56
  2027
			if (!pswit[OVERVIEW_SWITCH])
ali@70
  2028
			    g_print("    Line %ld column %ld - "
ali@56
  2029
			      "Wrongspaced singlequotes?\n",
ali@70
  2030
			      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@56
  2031
			else
ali@56
  2032
			    cnt_punct++;
ali@56
  2033
		    }
ali@56
  2034
		}
ali@56
  2035
	    }
ali@56
  2036
	}
ali@56
  2037
    }
ali@56
  2038
}
ali@56
  2039
ali@55
  2040
/*
ali@57
  2041
 * check_for_double_punctuation:
ali@57
  2042
 *
ali@57
  2043
 * Look for double punctuation like ,. or ,,
ali@57
  2044
 * Thanks to DW for the suggestion!
ali@57
  2045
 * In books with references, ".," and ".;" are common
ali@57
  2046
 * e.g. "etc., etc.," and vol. 1.; vol 3.;
ali@57
  2047
 * OTOH, from my initial tests, there are also fairly
ali@57
  2048
 * common errors. What to do? Make these cases paranoid?
ali@57
  2049
 * ".," is the most common, so warnings->dotcomma is used
ali@57
  2050
 * to suppress detailed reporting if it occurs often.
ali@57
  2051
 */
ali@57
  2052
void check_for_double_punctuation(const char *aline,struct warnings *warnings)
ali@57
  2053
{
ali@70
  2054
    const char *s;
ali@70
  2055
    gunichar c,nc;
ali@70
  2056
    nc=g_utf8_get_char(aline);
ali@70
  2057
    for (s=aline;*s;s=g_utf8_next_char(s))
ali@57
  2058
    {
ali@70
  2059
	c=nc;
ali@70
  2060
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@57
  2061
	/* for each punctuation character in the line */
ali@70
  2062
	if (c && nc && g_utf8_strchr(".?!,;:",-1,c) &&
ali@70
  2063
	  g_utf8_strchr(".?!,;:",-1,nc))
ali@57
  2064
	{
ali@57
  2065
	    /* followed by punctuation, it's a query, unless . . . */
ali@70
  2066
	    if (c==nc && (c=='.' || c=='?' || c=='!') ||
ali@70
  2067
	      !warnings->dotcomma && c=='.' && nc==',' ||
ali@70
  2068
	      warnings->isFrench && g_str_has_prefix(s,",...") ||
ali@70
  2069
	      warnings->isFrench && g_str_has_prefix(s,"...,") ||
ali@70
  2070
	      warnings->isFrench && g_str_has_prefix(s,";...") ||
ali@70
  2071
	      warnings->isFrench && g_str_has_prefix(s,"...;") ||
ali@70
  2072
	      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@57
  2078
	    {
ali@70
  2079
		if (warnings->isFrench && g_str_has_prefix(s,",...") ||
ali@70
  2080
		  warnings->isFrench && g_str_has_prefix(s,"...,") ||
ali@70
  2081
		  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
		{
ali@70
  2090
		    s+=4;
ali@70
  2091
		    nc=g_utf8_get_char(g_utf8_next_char(s));
ali@70
  2092
		}
ali@57
  2093
		; /* do nothing for .. !! and ?? which can be legit */
ali@57
  2094
	    }
ali@57
  2095
	    else
ali@57
  2096
	    {
ali@57
  2097
		if (pswit[ECHO_SWITCH])
ali@70
  2098
		    g_print("\n%s\n",aline);
ali@57
  2099
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2100
		    g_print("    Line %ld column %ld - Double punctuation?\n",
ali@70
  2101
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@57
  2102
		else
ali@57
  2103
		    cnt_punct++;
ali@57
  2104
	    }
ali@57
  2105
	}
ali@57
  2106
    }
ali@57
  2107
}
ali@57
  2108
ali@57
  2109
/*
ali@58
  2110
 * check_for_spaced_quotes:
ali@58
  2111
 */
ali@58
  2112
void check_for_spaced_quotes(const char *aline)
ali@58
  2113
{
ali@99
  2114
    int i;
ali@58
  2115
    const char *s,*t;
ali@99
  2116
    const gunichar single_quotes[]={CHAR_SQUOTE,CHAR_OPEN_SQUOTE,CHAR_LS_QUOTE,
ali@99
  2117
      CHAR_RS_QUOTE};
ali@99
  2118
    GString *pattern;
ali@58
  2119
    s=aline;
ali@58
  2120
    while ((t=strstr(s," \" ")))
ali@58
  2121
    {
ali@58
  2122
	if (pswit[ECHO_SWITCH])
ali@70
  2123
	    g_print("\n%s\n",aline);
ali@58
  2124
	if (!pswit[OVERVIEW_SWITCH])
ali@70
  2125
	    g_print("    Line %ld column %ld - Spaced doublequote?\n",
ali@70
  2126
	      linecnt,g_utf8_pointer_to_offset(aline,t)+1);
ali@58
  2127
	else
ali@58
  2128
	    cnt_punct++;
ali@70
  2129
	s=g_utf8_next_char(g_utf8_next_char(t));
ali@58
  2130
    }
ali@99
  2131
    pattern=g_string_new(NULL);
ali@99
  2132
    for(i=0;i<G_N_ELEMENTS(single_quotes);i++)
ali@58
  2133
    {
ali@99
  2134
	g_string_assign(pattern," ");
ali@99
  2135
	g_string_append_unichar(pattern,single_quotes[i]);
ali@99
  2136
	g_string_append_c(pattern,' ');
ali@99
  2137
	s=aline;
ali@99
  2138
	while ((t=strstr(s,pattern->str)))
ali@99
  2139
	{
ali@99
  2140
	    if (pswit[ECHO_SWITCH])
ali@99
  2141
		g_print("\n%s\n",aline);
ali@99
  2142
	    if (!pswit[OVERVIEW_SWITCH])
ali@99
  2143
		g_print("    Line %ld column %ld - Spaced singlequote?\n",
ali@99
  2144
		  linecnt,g_utf8_pointer_to_offset(aline,t)+1);
ali@99
  2145
	    else
ali@99
  2146
		cnt_punct++;
ali@99
  2147
	    s=g_utf8_next_char(g_utf8_next_char(t));
ali@99
  2148
	}
ali@58
  2149
    }
ali@99
  2150
    g_string_free(pattern,TRUE);
ali@58
  2151
}
ali@58
  2152
ali@58
  2153
/*
ali@59
  2154
 * check_for_miscased_genative:
ali@59
  2155
 *
ali@59
  2156
 * Check special case of 'S instead of 's at end of word.
ali@59
  2157
 */
ali@59
  2158
void check_for_miscased_genative(const char *aline)
ali@59
  2159
{
ali@59
  2160
    const char *s;
ali@70
  2161
    gunichar c,nc,pc;
ali@69
  2162
    if (!*aline)
ali@69
  2163
	return;
ali@70
  2164
    c=g_utf8_get_char(aline);
ali@70
  2165
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  2166
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@59
  2167
    {
ali@70
  2168
	pc=c;
ali@70
  2169
	c=nc;
ali@70
  2170
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@99
  2171
	if (CHAR_IS_APOSTROPHE(c) && nc=='S' && g_unichar_islower(pc))
ali@59
  2172
	{
ali@59
  2173
	    if (pswit[ECHO_SWITCH])
ali@70
  2174
		g_print("\n%s\n",aline);
ali@59
  2175
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2176
		g_print("    Line %ld column %ld - Capital \"S\"?\n",
ali@70
  2177
		  linecnt,g_utf8_pointer_to_offset(aline,s)+2);
ali@59
  2178
	    else
ali@59
  2179
		cnt_punct++;
ali@59
  2180
	}
ali@59
  2181
    }
ali@59
  2182
}
ali@59
  2183
ali@59
  2184
/*
ali@60
  2185
 * check_end_of_line:
ali@60
  2186
 *
ali@60
  2187
 * Now check special cases - start and end of line -
ali@60
  2188
 * for single and double quotes. Start is sometimes [sic]
ali@60
  2189
 * but better to query it anyway.
ali@60
  2190
 * While we're here, check for dash at end of line.
ali@60
  2191
 */
ali@60
  2192
void check_end_of_line(const char *aline,struct warnings *warnings)
ali@60
  2193
{
ali@70
  2194
    int lbytes;
ali@70
  2195
    const char *s;
ali@70
  2196
    gunichar c1,c2;
ali@70
  2197
    lbytes=strlen(aline);
ali@70
  2198
    if (g_utf8_strlen(aline,lbytes)>1)
ali@60
  2199
    {
ali@70
  2200
	s=g_utf8_prev_char(aline+lbytes);
ali@70
  2201
	c1=g_utf8_get_char(s);
ali@70
  2202
	c2=g_utf8_get_char(g_utf8_prev_char(s));
ali@99
  2203
	if ((c1==CHAR_DQUOTE || CHAR_IS_SQUOTE(c1)) && c2==CHAR_SPACE)
ali@60
  2204
	{
ali@60
  2205
	    if (pswit[ECHO_SWITCH])
ali@70
  2206
		g_print("\n%s\n",aline);
ali@60
  2207
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2208
		g_print("    Line %ld column %ld - Spaced quote?\n",linecnt,
ali@70
  2209
		  g_utf8_strlen(aline,lbytes));
ali@70
  2210
	    else
ali@70
  2211
		cnt_punct++;
ali@70
  2212
	}
ali@70
  2213
	c1=g_utf8_get_char(aline);
ali@70
  2214
	c2=g_utf8_get_char(g_utf8_next_char(aline));
ali@99
  2215
	if (CHAR_IS_SQUOTE(c1) && c2==CHAR_SPACE)
ali@70
  2216
	{
ali@70
  2217
	    if (pswit[ECHO_SWITCH])
ali@70
  2218
		g_print("\n%s\n",aline);
ali@70
  2219
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2220
		g_print("    Line %ld column 1 - Spaced quote?\n",linecnt);
ali@60
  2221
	    else
ali@60
  2222
		cnt_punct++;
ali@60
  2223
	}
ali@60
  2224
	/*
ali@60
  2225
	 * Dash at end of line may well be legit - paranoid mode only
ali@60
  2226
	 * and don't report em-dash at line-end.
ali@60
  2227
	 */
ali@60
  2228
	if (pswit[PARANOID_SWITCH] && warnings->hyphen)
ali@60
  2229
	{
ali@70
  2230
	    for (s=g_utf8_prev_char(aline+lbytes);
ali@70
  2231
	      s>aline && g_utf8_get_char(s)<=CHAR_SPACE;s=g_utf8_prev_char(s))
ali@60
  2232
		;
ali@70
  2233
	    if (g_utf8_get_char(s)=='-' &&
ali@70
  2234
	      g_utf8_get_char(g_utf8_prev_char(s))!='-')
ali@60
  2235
	    {
ali@60
  2236
		if (pswit[ECHO_SWITCH])
ali@70
  2237
		    g_print("\n%s\n",aline);
ali@60
  2238
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2239
		    g_print("    Line %ld column %ld - "
ali@70
  2240
		      "Hyphen at end of line?\n",
ali@70
  2241
		      linecnt,g_utf8_pointer_to_offset(aline,s));
ali@60
  2242
	    }
ali@60
  2243
	}
ali@60
  2244
    }
ali@60
  2245
}
ali@60
  2246
ali@60
  2247
/*
ali@61
  2248
 * check_for_unspaced_bracket:
ali@61
  2249
 *
ali@61
  2250
 * Brackets are often unspaced, but shouldn't be surrounded by alpha.
ali@61
  2251
 * If so, suspect a scanno like "a]most".
ali@61
  2252
 */
ali@61
  2253
void check_for_unspaced_bracket(const char *aline)
ali@61
  2254
{
ali@70
  2255
    const char *s;
ali@70
  2256
    gunichar c,nc,pc;
ali@70
  2257
    c=g_utf8_get_char(aline);
ali@70
  2258
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  2259
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@61
  2260
    {
ali@70
  2261
	pc=c;
ali@70
  2262
	c=nc;
ali@70
  2263
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@70
  2264
	if (!nc)
ali@70
  2265
	    break;
ali@61
  2266
	/* for each bracket character in the line except 1st & last */
ali@70
  2267
	if (g_utf8_strchr("{[()]}",-1,c) &&
ali@70
  2268
	  g_unichar_isalpha(pc) && g_unichar_isalpha(nc))
ali@61
  2269
	{
ali@61
  2270
	    if (pswit[ECHO_SWITCH])
ali@70
  2271
		g_print("\n%s\n",aline);
ali@61
  2272
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2273
		g_print("    Line %ld column %ld - Unspaced bracket?\n",
ali@70
  2274
		  linecnt,g_utf8_pointer_to_offset(aline,s));
ali@61
  2275
	    else
ali@61
  2276
		cnt_punct++;
ali@61
  2277
	}
ali@61
  2278
    }
ali@61
  2279
}
ali@61
  2280
ali@61
  2281
/*
ali@62
  2282
 * check_for_unpunctuated_endquote:
ali@62
  2283
 */
ali@62
  2284
void check_for_unpunctuated_endquote(const char *aline)
ali@62
  2285
{
ali@70
  2286
    const char *s;
ali@70
  2287
    gunichar c,nc,pc;
ali@70
  2288
    c=g_utf8_get_char(aline);
ali@70
  2289
    nc=c?g_utf8_get_char(g_utf8_next_char(aline)):0;
ali@70
  2290
    for (s=g_utf8_next_char(aline);nc;s=g_utf8_next_char(s))
ali@62
  2291
    {
ali@70
  2292
	pc=c;
ali@70
  2293
	c=nc;
ali@70
  2294
	nc=g_utf8_get_char(g_utf8_next_char(s));
ali@62
  2295
	/* for each character in the line except 1st */
ali@70
  2296
	if (c==CHAR_DQUOTE && isalpha(pc))
ali@62
  2297
	{
ali@62
  2298
	    if (pswit[ECHO_SWITCH])
ali@70
  2299
		g_print("\n%s\n",aline);
ali@62
  2300
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2301
		g_print("    Line %ld column %ld - "
ali@70
  2302
		  "endquote missing punctuation?\n",
ali@70
  2303
		  linecnt,g_utf8_pointer_to_offset(aline,s));
ali@62
  2304
	    else
ali@62
  2305
		cnt_punct++;
ali@62
  2306
	}
ali@62
  2307
    }
ali@62
  2308
}
ali@62
  2309
ali@62
  2310
/*
ali@63
  2311
 * check_for_html_tag:
ali@63
  2312
 *
ali@63
  2313
 * Check for <HTML TAG>.
ali@63
  2314
 *
ali@63
  2315
 * If there is a < in the line, followed at some point
ali@63
  2316
 * by a > then we suspect HTML.
ali@63
  2317
 */
ali@63
  2318
void check_for_html_tag(const char *aline)
ali@63
  2319
{
ali@63
  2320
    const char *open,*close;
ali@70
  2321
    gchar *tag;
ali@70
  2322
    open=strchr(aline,'<');
ali@63
  2323
    if (open)
ali@63
  2324
    {
ali@70
  2325
	close=strchr(g_utf8_next_char(open),'>');
ali@63
  2326
	if (close)
ali@63
  2327
	{
ali@70
  2328
	    if (pswit[ECHO_SWITCH])
ali@70
  2329
		g_print("\n%s\n",aline);
ali@70
  2330
	    if (!pswit[OVERVIEW_SWITCH])
ali@63
  2331
	    {
ali@70
  2332
		tag=g_strndup(open,close-open+1);
ali@70
  2333
		g_print("    Line %ld column %ld - HTML Tag? %s \n",
ali@70
  2334
		  linecnt,g_utf8_pointer_to_offset(aline,open)+1,tag);
ali@70
  2335
		g_free(tag);
ali@63
  2336
	    }
ali@70
  2337
	    else
ali@70
  2338
		cnt_html++;
ali@63
  2339
	}
ali@63
  2340
    }
ali@63
  2341
}
ali@63
  2342
ali@63
  2343
/*
ali@64
  2344
 * check_for_html_entity:
ali@64
  2345
 *
ali@64
  2346
 * Check for &symbol; HTML.
ali@64
  2347
 *
ali@64
  2348
 * If there is a & in the line, followed at
ali@64
  2349
 * some point by a ; then we suspect HTML.
ali@64
  2350
 */
ali@64
  2351
void check_for_html_entity(const char *aline)
ali@64
  2352
{
ali@64
  2353
    const char *s,*amp,*scolon;
ali@70
  2354
    gchar *entity;
ali@70
  2355
    amp=strchr(aline,'&');
ali@64
  2356
    if (amp)
ali@64
  2357
    {
ali@70
  2358
	scolon=strchr(amp,';');
ali@64
  2359
	if (scolon)
ali@64
  2360
	{
ali@70
  2361
	    for (s=amp;s<scolon;s=g_utf8_next_char(s))   
ali@70
  2362
		if (g_utf8_get_char(s)==CHAR_SPACE)
ali@70
  2363
		    break;		/* Don't report "Jones & Son;" */
ali@70
  2364
	    if (s>=scolon)
ali@64
  2365
	    {
ali@64
  2366
		if (pswit[ECHO_SWITCH])
ali@70
  2367
		    g_print("\n%s\n",aline);
ali@64
  2368
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2369
		{
ali@70
  2370
		    entity=g_strndup(amp,scolon-amp+1);
ali@70
  2371
		    g_print("    Line %ld column %d - HTML symbol? %s \n",
ali@70
  2372
		      linecnt,(int)(amp-aline)+1,entity);
ali@70
  2373
		    g_free(entity);
ali@70
  2374
		}
ali@64
  2375
		else
ali@64
  2376
		    cnt_html++;
ali@64
  2377
	    }
ali@64
  2378
	}
ali@64
  2379
    }
ali@64
  2380
}
ali@64
  2381
ali@65
  2382
/*
ali@66
  2383
 * check_for_omitted_punctuation:
ali@66
  2384
 *
ali@66
  2385
 * Check for omitted punctuation at end of paragraph by working back
ali@66
  2386
 * through prevline. DW.
ali@66
  2387
 * Need to check this only for "normal" paras.
ali@66
  2388
 * So what is a "normal" para?
ali@66
  2389
 *    Not normal if one-liner (chapter headings, etc.)
ali@66
  2390
 *    Not normal if doesn't contain at least one locase letter
ali@66
  2391
 *    Not normal if starts with space
ali@66
  2392
 */
ali@66
  2393
void check_for_omitted_punctuation(const char *prevline,
ali@66
  2394
  struct line_properties *last,int start_para_line)
ali@66
  2395
{
ali@70
  2396
    gboolean letter_on_line=FALSE;
ali@66
  2397
    const char *s;
ali@99
  2398
    gunichar c;
ali@70
  2399
    for (s=prevline;*s;s=g_utf8_next_char(s))
ali@70
  2400
	if (g_unichar_isalpha(g_utf8_get_char(s)))
ali@70
  2401
	{
ali@70
  2402
	    letter_on_line=TRUE;
ali@70
  2403
	    break;
ali@70
  2404
	}
ali@66
  2405
    /*
ali@66
  2406
     * This next "if" is a problem.
ali@66
  2407
     * If we say "start_para_line <= linecnt - 1", that includes
ali@66
  2408
     * one-line "paragraphs" like chapter heads. Lotsa false positives.
ali@66
  2409
     * If we say "start_para_line < linecnt - 1" it doesn't, but then it
ali@66
  2410
     * misses genuine one-line paragraphs.
ali@66
  2411
     */
ali@70
  2412
    if (letter_on_line && last->blen>2 && start_para_line<linecnt-1 &&
ali@70
  2413
      g_utf8_get_char(prevline)>CHAR_SPACE)
ali@66
  2414
    {
ali@99
  2415
	s=prevline+strlen(prevline);
ali@99
  2416
	do
ali@99
  2417
	{
ali@99
  2418
	    s=g_utf8_prev_char(s);
ali@99
  2419
	    c=g_utf8_get_char(s);
ali@99
  2420
	} while (CHAR_IS_CLOSING_QUOTE(c) && c>CHAR_SPACE && s>prevline);
ali@70
  2421
	for (;s>prevline;s=g_utf8_prev_char(s))
ali@66
  2422
	{
ali@70
  2423
	    if (g_unichar_isalpha(g_utf8_get_char(s)))
ali@66
  2424
	    {
ali@66
  2425
		if (pswit[ECHO_SWITCH])
ali@70
  2426
		    g_print("\n%s\n",prevline);
ali@66
  2427
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2428
		    g_print("    Line %ld column %ld - "
ali@66
  2429
		      "No punctuation at para end?\n",
ali@70
  2430
		      linecnt-1,g_utf8_strlen(prevline,-1));
ali@66
  2431
		else
ali@66
  2432
		    cnt_punct++;
ali@66
  2433
		break;
ali@66
  2434
	    }
ali@70
  2435
	    if (g_utf8_strchr("-.:!([{?}])",-1,g_utf8_get_char(s)))
ali@66
  2436
		break;
ali@66
  2437
	}
ali@66
  2438
    }
ali@66
  2439
}
ali@66
  2440
ali@69
  2441
gboolean report_duplicate_queries(gpointer key,gpointer value,gpointer data)
ali@69
  2442
{
ali@69
  2443
    const char *word=key;
ali@69
  2444
    int *dupcnt=value;
ali@69
  2445
    if (*dupcnt)
ali@70
  2446
	g_print("\nNote: Queried word %s was duplicated %d times\n",
ali@69
  2447
	  word,*dupcnt);
ali@69
  2448
    return FALSE;
ali@69
  2449
}
ali@69
  2450
ali@70
  2451
void print_as_windows_1252(const char *string)
ali@70
  2452
{
ali@70
  2453
    gsize inbytes,outbytes;
ali@70
  2454
    gchar *buf,*bp;
ali@86
  2455
    static GIConv converter=(GIConv)-1;
ali@70
  2456
    if (!string)
ali@70
  2457
    {
ali@70
  2458
	if (converter!=(GIConv)-1)
ali@70
  2459
	    g_iconv_close(converter);
ali@70
  2460
	converter=(GIConv)-1;
ali@70
  2461
	return;
ali@70
  2462
    }
ali@86
  2463
    if (converter==(GIConv)-1)
ali@70
  2464
	converter=g_iconv_open("WINDOWS-1252","UTF-8");
ali@70
  2465
    if (converter!=(GIConv)-1)
ali@70
  2466
    {
ali@70
  2467
	inbytes=outbytes=strlen(string);
ali@70
  2468
	bp=buf=g_malloc(outbytes+1);
ali@70
  2469
	g_iconv(converter,(char **)&string,&inbytes,&bp,&outbytes);
ali@70
  2470
	*bp='\0';
ali@70
  2471
	fputs(buf,stdout);
ali@70
  2472
	g_free(buf);
ali@70
  2473
    }
ali@70
  2474
    else
ali@70
  2475
	fputs(string,stdout);
ali@70
  2476
}
ali@70
  2477
ali@72
  2478
void print_as_utf_8(const char *string)
ali@72
  2479
{
ali@72
  2480
    fputs(string,stdout);
ali@72
  2481
}
ali@72
  2482
ali@66
  2483
/*
ali@41
  2484
 * procfile:
ali@41
  2485
 *
ali@41
  2486
 * Process one file.
ali@41
  2487
 */
ali@69
  2488
void procfile(const char *filename)
ali@41
  2489
{
ali@65
  2490
    const char *s;
ali@69
  2491
    gchar *parastart=NULL;	/* first line of current para */
ali@69
  2492
    gchar *etext,*aline;
ali@69
  2493
    gchar *etext_ptr;
ali@69
  2494
    GError *err=NULL;
ali@41
  2495
    struct first_pass_results *first_pass_results;
ali@42
  2496
    struct warnings *warnings;
ali@43
  2497
    struct counters counters={0};
ali@45
  2498
    struct line_properties last={0};
ali@56
  2499
    struct parities parities={0};
ali@69
  2500
    struct pending pending={0};
ali@69
  2501
    gboolean isemptyline;
ali@68
  2502
    long start_para_line=0;
ali@69
  2503
    gboolean isnewpara=FALSE,enddash=FALSE;
ali@45
  2504
    last.start=CHAR_SPACE;
ali@68
  2505
    linecnt=checked_linecnt=0;
ali@69
  2506
    etext=read_etext(filename,&err);
ali@69
  2507
    if (!etext)
ali@41
  2508
    {
ali@68
  2509
	if (pswit[STDOUT_SWITCH])
ali@69
  2510
	    fprintf(stdout,"bookloupe: %s: %s\n",filename,err->message);
ali@68
  2511
	else
ali@69
  2512
	    fprintf(stderr,"bookloupe: %s: %s\n",filename,err->message);
ali@41
  2513
	exit(1);
ali@41
  2514
    }
ali@70
  2515
    g_print("\n\nFile: %s\n\n",filename);
ali@69
  2516
    first_pass_results=first_pass(etext);
ali@42
  2517
    warnings=report_first_pass(first_pass_results);
ali@69
  2518
    qword=g_tree_new_full((GCompareDataFunc)strcmp,NULL,g_free,g_free);
ali@69
  2519
    qperiod=g_tree_new_full((GCompareDataFunc)strcmp,NULL,g_free,NULL);
ali@40
  2520
    /*
ali@40
  2521
     * Here we go with the main pass. Hold onto yer hat!
ali@40
  2522
     */
ali@65
  2523
    linecnt=0;
ali@69
  2524
    etext_ptr=etext;
ali@69
  2525
    while ((aline=flgets(&etext_ptr,linecnt+1)))
ali@40
  2526
    {
ali@68
  2527
	linecnt++;
ali@68
  2528
	if (linecnt==1)
ali@69
  2529
	    isnewpara=TRUE;
ali@70
  2530
	if (pswit[DP_SWITCH] && g_str_has_prefix(aline,"-----File: "))
ali@40
  2531
	    continue;    // skip DP page separators completely
ali@68
  2532
	if (linecnt<first_pass_results->firstline ||
ali@41
  2533
	  (first_pass_results->footerline>0 &&
ali@41
  2534
	  linecnt>first_pass_results->footerline))
ali@40
  2535
	{
ali@68
  2536
	    if (pswit[HEADER_SWITCH])
ali@40
  2537
	    {
ali@70
  2538
		if (g_str_has_prefix(aline,"Title:"))
ali@70
  2539
		    g_print("    %s\n",aline);
ali@70
  2540
		if (g_str_has_prefix(aline,"Author:"))
ali@70
  2541
		    g_print("    %s\n",aline);
ali@70
  2542
		if (g_str_has_prefix(aline,"Release Date:"))
ali@70
  2543
		    g_print("    %s\n",aline);
ali@70
  2544
		if (g_str_has_prefix(aline,"Edition:"))
ali@70
  2545
		    g_print("    %s\n\n",aline);
ali@40
  2546
	    }
ali@68
  2547
	    continue;		/* skip through the header */
ali@40
  2548
	}
ali@68
  2549
	checked_linecnt++;
ali@65
  2550
	print_pending(aline,parastart,&pending);
ali@43
  2551
	isemptyline=analyse_quotes(aline,&counters);
ali@68
  2552
	if (isnewpara && !isemptyline)
ali@40
  2553
	{
ali@40
  2554
	    /* This line is the start of a new paragraph. */
ali@68
  2555
	    start_para_line=linecnt;
ali@40
  2556
	    /* Capture its first line in case we want to report it later. */
ali@69
  2557
	    g_free(parastart);
ali@69
  2558
	    parastart=g_strdup(aline);
ali@56
  2559
	    memset(&parities,0,sizeof(parities));  /* restart the quote count */
ali@68
  2560
	    s=aline;
ali@70
  2561
	    while (*s && !g_unichar_isalpha(g_utf8_get_char(s)) &&
ali@70
  2562
	      !g_unichar_isdigit(g_utf8_get_char(s)))
ali@70
  2563
		s=g_utf8_next_char(s);
ali@70
  2564
	    if (g_unichar_islower(g_utf8_get_char(s)))
ali@40
  2565
	    {
ali@40
  2566
		/* and its first letter is lowercase */
ali@68
  2567
		if (pswit[ECHO_SWITCH])
ali@70
  2568
		    g_print("\n%s\n",aline);
ali@68
  2569
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2570
		    g_print("    Line %ld column %ld - "
ali@40
  2571
		      "Paragraph starts with lower-case\n",
ali@70
  2572
		      linecnt,g_utf8_pointer_to_offset(aline,s)+1);
ali@68
  2573
		else
ali@68
  2574
		    cnt_punct++;
ali@40
  2575
	    }
ali@69
  2576
	    isnewpara=FALSE; /* Signal the end of new para processing. */
ali@40
  2577
	}
ali@68
  2578
	/* Check for an em-dash broken at line end. */
ali@70
  2579
	if (enddash && g_utf8_get_char(aline)=='-')
ali@40
  2580
	{
ali@68
  2581
	    if (pswit[ECHO_SWITCH])
ali@70
  2582
		g_print("\n%s\n",aline);
ali@68
  2583
	    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2584
		g_print("    Line %ld column 1 - Broken em-dash?\n",linecnt);
ali@68
  2585
	    else
ali@68
  2586
		cnt_punct++;
ali@40
  2587
	}
ali@69
  2588
	enddash=FALSE;
ali@70
  2589
	for (s=g_utf8_prev_char(aline+strlen(aline));
ali@70
  2590
	  g_utf8_get_char(s)==' ' && s>aline;s=g_utf8_prev_char(s))
ali@40
  2591
	    ;
ali@70
  2592
	if (s>=aline && g_utf8_get_char(s)=='-')
ali@69
  2593
	    enddash=TRUE;
ali@67
  2594
	check_for_control_characters(aline);
ali@68
  2595
	if (warnings->bin)
ali@44
  2596
	    check_for_odd_characters(aline,warnings,isemptyline);
ali@68
  2597
	if (warnings->longline)
ali@45
  2598
	    check_for_long_line(aline);
ali@68
  2599
	if (warnings->shortline)
ali@45
  2600
	    check_for_short_line(aline,&last);
ali@68
  2601
	last.blen=last.len;
ali@70
  2602
	last.len=g_utf8_strlen(aline,-1);
ali@70
  2603
	last.start=g_utf8_get_char(aline);
ali@46
  2604
	check_for_starting_punctuation(aline);
ali@68
  2605
	if (warnings->dash)
ali@40
  2606
	{
ali@47
  2607
	    check_for_spaced_emdash(aline);
ali@47
  2608
	    check_for_spaced_dash(aline);
ali@40
  2609
	}
ali@48
  2610
	check_for_unmarked_paragraphs(aline);
ali@49
  2611
	check_for_jeebies(aline);
ali@50
  2612
	check_for_mta_from(aline);
ali@51
  2613
	check_for_orphan_character(aline);
ali@52
  2614
	check_for_pling_scanno(aline);
ali@53
  2615
	check_for_extra_period(aline,warnings);
ali@54
  2616
	check_for_following_punctuation(aline);
ali@55
  2617
	check_for_typos(aline,warnings);
ali@56
  2618
	check_for_misspaced_punctuation(aline,&parities,isemptyline);
ali@57
  2619
	check_for_double_punctuation(aline,warnings);
ali@58
  2620
	check_for_spaced_quotes(aline);
ali@59
  2621
	check_for_miscased_genative(aline);
ali@60
  2622
	check_end_of_line(aline,warnings);
ali@61
  2623
	check_for_unspaced_bracket(aline);
ali@68
  2624
	if (warnings->endquote)
ali@62
  2625
	    check_for_unpunctuated_endquote(aline);
ali@63
  2626
	check_for_html_tag(aline);
ali@64
  2627
	check_for_html_entity(aline);
ali@68
  2628
	if (isemptyline)
ali@40
  2629
	{
ali@65
  2630
	    check_for_mismatched_quotes(&counters,&pending);
ali@103
  2631
	    counters_reset(&counters);
ali@40
  2632
	    /* let the next iteration know that it's starting a new para */
ali@69
  2633
	    isnewpara=TRUE;
ali@69
  2634
	    if (prevline)
ali@69
  2635
		check_for_omitted_punctuation(prevline,&last,start_para_line);
ali@40
  2636
	}
ali@69
  2637
	g_free(prevline);
ali@69
  2638
	prevline=g_strdup(aline);
ali@0
  2639
    }
ali@103
  2640
    linecnt++;
ali@103
  2641
    check_for_mismatched_quotes(&counters,&pending);
ali@103
  2642
    print_pending(NULL,parastart,&pending);
ali@103
  2643
    reset_pending(&pending);
ali@69
  2644
    if (prevline)
ali@69
  2645
    {
ali@69
  2646
	g_free(prevline);
ali@69
  2647
	prevline=NULL;
ali@69
  2648
    }
ali@69
  2649
    g_free(parastart);
ali@69
  2650
    g_free(prevline);
ali@69
  2651
    g_free(etext);
ali@79
  2652
    if (!pswit[OVERVIEW_SWITCH] && !pswit[VERBOSE_SWITCH])
ali@69
  2653
	g_tree_foreach(qword,report_duplicate_queries,NULL);
ali@69
  2654
    g_tree_unref(qword);
ali@69
  2655
    g_tree_unref(qperiod);
ali@99
  2656
    counters_destroy(&counters);
ali@70
  2657
    g_set_print_handler(NULL);
ali@70
  2658
    print_as_windows_1252(NULL);
ali@71
  2659
    if (pswit[MARKUP_SWITCH])  
ali@71
  2660
	loseentities(NULL);
ali@0
  2661
}
ali@0
  2662
ali@40
  2663
/*
ali@40
  2664
 * flgets:
ali@40
  2665
 *
ali@69
  2666
 * Get one line from the input text, checking for
ali@40
  2667
 * the existence of exactly one CR/LF line-end per line.
ali@40
  2668
 *
ali@40
  2669
 * Returns: a pointer to the line.
ali@40
  2670
 */
ali@69
  2671
char *flgets(char **etext,long lcnt)
ali@0
  2672
{
ali@70
  2673
    gunichar c;
ali@69
  2674
    gboolean isCR=FALSE;
ali@69
  2675
    char *theline=*etext;
ali@70
  2676
    char *eos=theline;
ali@70
  2677
    gchar *s;
ali@70
  2678
    for (;;)
ali@40
  2679
    {
ali@70
  2680
	c=g_utf8_get_char(*etext);
ali@70
  2681
	*etext=g_utf8_next_char(*etext);
ali@69
  2682
	if (!c)
ali@68
  2683
	    return NULL;
ali@40
  2684
	/* either way, it's end of line */
ali@69
  2685
	if (c=='\n')
ali@40
  2686
	{
ali@68
  2687
	    if (isCR)
ali@68
  2688
		break;
ali@68
  2689
	    else
ali@40
  2690
	    {
ali@40
  2691
		/* Error - a LF without a preceding CR */
ali@68
  2692
		if (pswit[LINE_END_SWITCH])
ali@40
  2693
		{
ali@68
  2694
		    if (pswit[ECHO_SWITCH])
ali@70
  2695
		    {
ali@70
  2696
			s=g_strndup(theline,eos-theline);
ali@70
  2697
			g_print("\n%s\n",s);
ali@70
  2698
			g_free(s);
ali@70
  2699
		    }
ali@68
  2700
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2701
			g_print("    Line %ld - No CR?\n",lcnt);
ali@68
  2702
		    else
ali@68
  2703
			cnt_lineend++;
ali@40
  2704
		}
ali@68
  2705
		break;
ali@40
  2706
	    }
ali@40
  2707
	}
ali@69
  2708
	if (c=='\r')
ali@40
  2709
	{
ali@68
  2710
	    if (isCR)
ali@40
  2711
	    {
ali@40
  2712
		/* Error - two successive CRs */
ali@68
  2713
		if (pswit[LINE_END_SWITCH])
ali@40
  2714
		{
ali@68
  2715
		    if (pswit[ECHO_SWITCH])
ali@70
  2716
		    {
ali@70
  2717
			s=g_strndup(theline,eos-theline);
ali@70
  2718
			g_print("\n%s\n",s);
ali@70
  2719
			g_free(s);
ali@70
  2720
		    }
ali@68
  2721
		    if (!pswit[OVERVIEW_SWITCH])
ali@70
  2722
			g_print("    Line %ld - Two successive CRs?\n",lcnt);
ali@68
  2723
		    else
ali@68
  2724
			cnt_lineend++;
ali@40
  2725
		}
ali@40
  2726
	    }
ali@69
  2727
	    isCR=TRUE;
ali@40
  2728
	}
ali@68
  2729
	else
ali@40
  2730
	{
ali@68
  2731
	    if (pswit[LINE_END_SWITCH] && isCR)
ali@40
  2732
	    {
ali@68
  2733
		if (pswit[ECHO_SWITCH])
ali@70
  2734
		{
ali@70
  2735
		    s=g_strndup(theline,eos-theline);
ali@70
  2736
		    g_print("\n%s\n",s);
ali@70
  2737
		    g_free(s);
ali@70
  2738
		}
ali@68
  2739
		if (!pswit[OVERVIEW_SWITCH])
ali@70
  2740
		    g_print("    Line %ld column %ld - CR without LF?\n",
ali@70
  2741
		      lcnt,g_utf8_pointer_to_offset(theline,eos)+1);
ali@68
  2742
		else
ali@68
  2743
		    cnt_lineend++;
ali@70
  2744
		*eos=' ';
ali@40
  2745
	    }
ali@69
  2746
	    isCR=FALSE;
ali@70
  2747
	    eos=g_utf8_next_char(eos);
ali@40
  2748
	}
ali@69
  2749
    }
ali@70
  2750
    *eos='\0';
ali@0
  2751
    if (pswit[MARKUP_SWITCH])  
ali@68
  2752
	postprocess_for_HTML(theline);
ali@0
  2753
    if (pswit[DP_SWITCH])  
ali@68
  2754
	postprocess_for_DP(theline);
ali@40
  2755
    return theline;
ali@0
  2756
}
ali@0
  2757
ali@40
  2758
/*
ali@40
  2759
 * mixdigit:
ali@40
  2760
 *
ali@40
  2761
 * Takes a "word" as a parameter, and checks whether it
ali@40
  2762
 * contains a mixture of alpha and digits. Generally, this is an
ali@40
  2763
 * error, but may not be for cases like 4th or L5 12s. 3d.
ali@40
  2764
 *
ali@70
  2765
 * Returns: TRUE iff an is error found.
ali@40
  2766
 */
ali@70
  2767
gboolean mixdigit(const char *checkword)
ali@0
  2768
{
ali@70
  2769
    gboolean wehaveadigit,wehavealetter,query;
ali@70
  2770
    const char *s,*nondigit;
ali@70
  2771
    wehaveadigit=wehavealetter=query=FALSE;
ali@70
  2772
    for (s=checkword;*s;s=g_utf8_next_char(s))
ali@70
  2773
	if (g_unichar_isalpha(g_utf8_get_char(s)))
ali@70
  2774
	    wehavealetter=TRUE;
ali@70
  2775
	else if (g_unichar_isdigit(g_utf8_get_char(s)))
ali@70
  2776
	    wehaveadigit=TRUE;
ali@40
  2777
    if (wehaveadigit && wehavealetter)
ali@40
  2778
    {
ali@40
  2779
	/* Now exclude common legit cases, like "21st" and "12l. 3s. 11d." */
ali@70
  2780
	query=TRUE;
ali@70
  2781
	for (nondigit=checkword;g_unichar_isdigit(g_utf8_get_char(nondigit));
ali@70
  2782
	  nondigit=g_utf8_next_char(nondigit))
ali@68
  2783
	    ;
ali@68
  2784
	/* digits, ending in st, rd, nd, th of either case */
ali@70
  2785
	if (!g_ascii_strcasecmp(nondigit,"st") ||
ali@70
  2786
	  !g_ascii_strcasecmp(nondigit,"rd") ||
ali@70
  2787
	  !g_ascii_strcasecmp(nondigit,"nd") ||
ali@70
  2788
	  !g_ascii_strcasecmp(nondigit,"th"))
ali@70
  2789
	    query=FALSE;
ali@70
  2790
	if (!g_ascii_strcasecmp(nondigit,"sts") ||
ali@70
  2791
	  !g_ascii_strcasecmp(nondigit,"rds") ||
ali@70
  2792
	  !g_ascii_strcasecmp(nondigit,"nds") ||
ali@70
  2793
	  !g_ascii_strcasecmp(nondigit,"ths"))
ali@70
  2794
	    query=FALSE;
ali@70
  2795
	if (!g_ascii_strcasecmp(nondigit,"stly") ||
ali@70
  2796
	  !g_ascii_strcasecmp(nondigit,"rdly") ||
ali@70
  2797
	  !g_ascii_strcasecmp(nondigit,"ndly") ||
ali@70
  2798
	  !g_ascii_strcasecmp(nondigit,"thly"))
ali@70
  2799
	    query=FALSE;
ali@68
  2800
	/* digits, ending in l, L, s or d */
ali@70
  2801
	if (!g_ascii_strcasecmp(nondigit,"l") || !strcmp(nondigit,"s") ||
ali@70
  2802
	  !strcmp(nondigit,"d"))
ali@70
  2803
	    query=FALSE;
ali@68
  2804
	/*
ali@40
  2805
	 * L at the start of a number, representing Britsh pounds, like L500.
ali@70
  2806
	 * This is cute. We know the current word is mixed digit. If the first
ali@68
  2807
	 * letter is L, there must be at least one digit following. If both
ali@68
  2808
	 * digits and letters follow, we have a genuine error, else we have a
ali@68
  2809
	 * capital L followed by digits, and we accept that as a non-error.
ali@40
  2810
	 */
ali@70
  2811
	if (g_utf8_get_char(checkword)=='L' &&
ali@70
  2812
	  !mixdigit(g_utf8_next_char(checkword)))
ali@70
  2813
	    query=FALSE;
ali@40
  2814
    }
ali@40
  2815
    return query;
ali@0
  2816
}
ali@0
  2817
ali@40
  2818
/*
ali@40
  2819
 * getaword:
ali@40
  2820
 *
ali@69
  2821
 * Extracts the first/next "word" from the line, and returns it.
ali@69
  2822
 * A word is defined as one English word unit--or at least that's the aim.
ali@69
  2823
 * "ptr" is advanced to the position in the line where we will start
ali@69
  2824
 * looking for the next word.
ali@40
  2825
 *
ali@69
  2826
 * Returns: A newly-allocated string.
ali@40
  2827
 */
ali@69
  2828
gchar *getaword(const char **ptr)
ali@0
  2829
{
ali@70
  2830
    const char *s,*t;
ali@69
  2831
    GString *word;
ali@70
  2832
    gunichar c,pc;
ali@69
  2833
    word=g_string_new(NULL);
ali@70
  2834
    for (;!g_unichar_isdigit(g_utf8_get_char(*ptr)) &&
ali@70
  2835
      !g_unichar_isalpha(g_utf8_get_char(*ptr)) &&
ali@70
  2836
      **ptr;*ptr=g_utf8_next_char(*ptr))
ali@40
  2837
	;
ali@40
  2838
    /*
ali@40
  2839
     * Use a look-ahead to handle exceptions for numbers like 1,000 and 1.35.
ali@40
  2840
     * Especially yucky is the case of L1,000
ali@40
  2841
     * This section looks for a pattern of characters including a digit
ali@40
  2842
     * followed by a comma or period followed by one or more digits.
ali@40
  2843
     * If found, it returns this whole pattern as a word; otherwise we discard
ali@40
  2844
     * the results and resume our normal programming.
ali@40
  2845
     */
ali@69
  2846
    s=*ptr;
ali@70
  2847
    for (;g_unichar_isdigit(g_utf8_get_char(s)) ||
ali@70
  2848
      g_unichar_isalpha(g_utf8_get_char(s)) ||
ali@70
  2849
      g_utf8_get_char(s)==',' || g_utf8_get_char(s)=='.';s=g_utf8_next_char(s))
ali@70
  2850
	g_string_append_unichar(word,g_utf8_get_char(s));
ali@82
  2851
    if (word->len)
ali@40
  2852
    {
ali@82
  2853
	for (t=g_utf8_next_char(word->str);*t;t=g_utf8_next_char(t))
ali@40
  2854
	{
ali@82
  2855
	    c=g_utf8_get_char(t);
ali@82
  2856
	    pc=g_utf8_get_char(g_utf8_prev_char(t));
ali@82
  2857
	    if ((c=='.' || c==',') && g_unichar_isdigit(pc))
ali@82
  2858
	    {
ali@82
  2859
		*ptr=s;
ali@82
  2860
		return g_string_free(word,FALSE);
ali@82
  2861
	    }
ali@40
  2862
	}
ali@40
  2863
    }
ali@0
  2864
    /* we didn't find a punctuated number - do the regular getword thing */
ali@69
  2865
    g_string_truncate(word,0);
ali@99
  2866
    c=g_utf8_get_char(*ptr);
ali@99
  2867
    for (;g_unichar_isdigit(c) || g_unichar_isalpha(c) || CHAR_IS_APOSTROPHE(c);
ali@99
  2868
      *ptr=g_utf8_next_char(*ptr),c=g_utf8_get_char(*ptr))
ali@99
  2869
	g_string_append_unichar(word,c);
ali@69
  2870
    return g_string_free(word,FALSE);
ali@0
  2871
}
ali@0
  2872
ali@40
  2873
/*
ali@40
  2874
 * isroman:
ali@40
  2875
 *
ali@40
  2876
 * Is this word a Roman Numeral?
ali@40
  2877
 *
ali@40
  2878
 * It doesn't actually validate that the number is a valid Roman Numeral--for
ali@40
  2879
 * example it will pass MXXXXXXXXXX as a valid Roman Numeral, but that's not
ali@40
  2880
 * what we're here to do. If it passes this, it LOOKS like a Roman numeral.
ali@40
  2881
 * Anyway, the actual Romans were pretty tolerant of bad arithmetic, or
ali@40
  2882
 * expressions thereof, except when it came to taxes. Allow any number of M,
ali@40
  2883
 * an optional D, an optional CM or CD, any number of optional Cs, an optional
ali@40
  2884
 * XL or an optional XC, an optional IX or IV, an optional V and any number
ali@40
  2885
 * of optional Is.
ali@40
  2886
 */
ali@69
  2887
gboolean isroman(const char *t)
ali@0
  2888
{
ali@69
  2889
    const char *s;
ali@40
  2890
    if (!t || !*t)
ali@69
  2891
	return FALSE;
ali@40
  2892
    s=t;
ali@70
  2893
    while (g_utf8_get_char(t)=='m' && *t)
ali@40
  2894
	t++;
ali@70
  2895
    if (g_utf8_get_char(t)=='d')
ali@40
  2896
	t++;
ali@70
  2897
    if (g_str_has_prefix(t,"cm"))
ali@40
  2898
	t+=2;
ali@70
  2899
    if (g_str_has_prefix(t,"cd"))
ali@40
  2900
	t+=2;
ali@70
  2901
    while (g_utf8_get_char(t)=='c' && *t)
ali@40
  2902
	t++;
ali@70
  2903
    if (g_str_has_prefix(t,"xl"))
ali@40
  2904
	t+=2;
ali@70
  2905
    if (g_str_has_prefix(t,"xc"))
ali@40
  2906
	t+=2;
ali@70
  2907
    if (g_utf8_get_char(t)=='l')
ali@40
  2908
	t++;
ali@70
  2909
    while (g_utf8_get_char(t)=='x' && *t)
ali@40
  2910
	t++;
ali@70
  2911
    if (g_str_has_prefix(t,"ix"))
ali@40
  2912
	t+=2;
ali@70
  2913
    if (g_str_has_prefix(t,"iv"))
ali@40
  2914
	t+=2;
ali@70
  2915
    if (g_utf8_get_char(t)=='v')
ali@40
  2916
	t++;
ali@70
  2917
    while (g_utf8_get_char(t)=='i' && *t)
ali@40
  2918
	t++;
ali@40
  2919
    return !*t;
ali@0
  2920
}
ali@0
  2921
ali@40
  2922
/*
ali@40
  2923
 * postprocess_for_DP:
ali@40
  2924
 *
ali@40
  2925
 * Invoked with the -d switch from flgets().
ali@40
  2926
 * It simply "removes" from the line a hard-coded set of common
ali@40
  2927
 * DP-specific tags, so that the line passed to the main routine has
ali@40
  2928
 * been pre-cleaned of DP markup.
ali@40
  2929
 */
ali@0
  2930
void postprocess_for_DP(char *theline)
ali@0
  2931
{
ali@40
  2932
    char *s,*t;
ali@0
  2933
    int i;
ali@0
  2934
    if (!*theline) 
ali@68
  2935
	return;
ali@40
  2936
    for (i=0;*DPmarkup[i];i++)
ali@70
  2937
	while ((s=strstr(theline,DPmarkup[i])))
ali@40
  2938
	{
ali@68
  2939
	    t=s+strlen(DPmarkup[i]);
ali@70
  2940
	    memmove(s,t,strlen(t)+1);
ali@40
  2941
	}
ali@0
  2942
}
ali@0
  2943
ali@40
  2944
/*
ali@40
  2945
 * postprocess_for_HTML:
ali@40
  2946
 *
ali@40
  2947
 * Invoked with the -m switch from flgets().
ali@40
  2948
 * It simply "removes" from the line a hard-coded set of common
ali@40
  2949
 * HTML tags and "replaces" a hard-coded set of common HTML
ali@40
  2950
 * entities, so that the line passed to the main routine has
ali@40
  2951
 * been pre-cleaned of HTML.
ali@40
  2952
 */
ali@0
  2953
void postprocess_for_HTML(char *theline)
ali@0
  2954
{
ali@70
  2955
    while (losemarkup(theline))
ali@70
  2956
	;
ali@71
  2957
    loseentities(theline);
ali@0
  2958
}
ali@0
  2959
ali@0
  2960
char *losemarkup(char *theline)
ali@0
  2961
{
ali@40
  2962
    char *s,*t;
ali@0
  2963
    int i;
ali@70
  2964
    s=strchr(theline,'<');
ali@70
  2965
    t=s?strchr(s,'>'):NULL;
ali@40
  2966
    if (!s || !t)
ali@40
  2967
	return NULL;
ali@40
  2968
    for (i=0;*markup[i];i++)
ali@70
  2969
	if (tagcomp(g_utf8_next_char(s),markup[i]))
ali@40
  2970
	{
ali@70
  2971
	    t=g_utf8_next_char(t);
ali@70
  2972
	    memmove(s,t,strlen(t)+1);
ali@70
  2973
	    return s;
ali@68
  2974
	}
ali@40
  2975
    /* It's an unrecognized <xxx>. */
ali@40
  2976
    return NULL;
ali@0
  2977
}
ali@0
  2978
ali@71
  2979
void loseentities(char *theline)
ali@0
  2980
{
ali@0
  2981
    int i;
ali@71
  2982
    gsize nb;
ali@71
  2983
    char *amp,*scolon;
ali@71
  2984
    gchar *s,*t;
ali@71
  2985
    gunichar c;
ali@71
  2986
    GTree *entities=NULL;
ali@86
  2987
    static GIConv translit=(GIConv)-1,to_utf8=(GIConv)-1;
ali@71
  2988
    if (!theline)
ali@40
  2989
    {
ali@71
  2990
	if (entities)
ali@71
  2991
	    g_tree_destroy(entities);
ali@71
  2992
	entities=NULL;
ali@86
  2993
	if (translit!=(GIConv)-1)
ali@71
  2994
	    g_iconv_close(translit);
ali@71
  2995
	translit=(GIConv)-1;
ali@86
  2996
	if (to_utf8!=(GIConv)-1)
ali@71
  2997
	    g_iconv_close(to_utf8);
ali@71
  2998
	to_utf8=(GIConv)-1;
ali@71
  2999
	return;
ali@71
  3000
    }
ali@71
  3001
    if (!*theline)
ali@71
  3002
	return;
ali@71
  3003
    if (!entities)
ali@71
  3004
    {
ali@71
  3005
	entities=g_tree_new((GCompareFunc)strcmp);
ali@71
  3006
	for(i=0;i<G_N_ELEMENTS(HTMLentities);i++)
ali@71
  3007
	    g_tree_insert(entities,HTMLentities[i].name,
ali@71
  3008
	      GUINT_TO_POINTER(HTMLentities[i].c));
ali@71
  3009
    }
ali@71
  3010
    if (translit==(GIConv)-1)
ali@71
  3011
	translit=g_iconv_open("ISO_8859-1//TRANSLIT","UTF-8");
ali@71
  3012
    if (to_utf8==(GIConv)-1)
ali@71
  3013
	to_utf8=g_iconv_open("UTF-8","ISO_8859-1");
ali@71
  3014
    while((amp=strchr(theline,'&')))
ali@71
  3015
    {
ali@71
  3016
	scolon=strchr(amp,';');
ali@71
  3017
	if (scolon)
ali@40
  3018
	{
ali@71
  3019
	    if (amp[1]=='#')
ali@71
  3020
	    {
ali@71
  3021
		if (amp+2+strspn(amp+2,"0123456789")==scolon)
ali@71
  3022
		    c=strtol(amp+2,NULL,10);
ali@71
  3023
		else if (amp[2]=='x' &&
ali@71
  3024
		  amp+3+strspn(amp+3,"0123456789abcdefABCDEF")==scolon)
ali@71
  3025
		    c=strtol(amp+3,NULL,16);
ali@71
  3026
	    }
ali@71
  3027
	    else
ali@71
  3028
	    {
ali@71
  3029
		s=g_strndup(amp+1,scolon-(amp+1));
ali@71
  3030
	        c=GPOINTER_TO_UINT(g_tree_lookup(entities,s));
ali@71
  3031
		g_free(s);
ali@71
  3032
	    }
ali@40
  3033
	}
ali@71
  3034
	else
ali@71
  3035
	    c=0;
ali@71
  3036
	if (c)
ali@71
  3037
	{
ali@71
  3038
	    theline=amp;
ali@71
  3039
	    if (c<128 || c>=192 && c<=255)	/* An ISO-8859-1 character */
ali@71
  3040
		theline+=g_unichar_to_utf8(c,theline);
ali@71
  3041
	    else
ali@71
  3042
	    {
ali@71
  3043
		s=g_malloc(6);
ali@71
  3044
		nb=g_unichar_to_utf8(c,s);
ali@71
  3045
		t=g_convert_with_iconv(s,nb,translit,NULL,&nb,NULL);
ali@71
  3046
		g_free(s);
ali@71
  3047
		s=g_convert_with_iconv(t,nb,to_utf8,NULL,&nb,NULL);
ali@71
  3048
		g_free(t);
ali@71
  3049
		memcpy(theline,s,nb);
ali@71
  3050
		g_free(s);
ali@71
  3051
		theline+=nb;
ali@71
  3052
	    }
ali@71
  3053
	    memmove(theline,g_utf8_next_char(scolon),
ali@71
  3054
	      strlen(g_utf8_next_char(scolon))+1);
ali@71
  3055
	}
ali@71
  3056
	else
ali@71
  3057
	    theline=g_utf8_next_char(amp);
ali@40
  3058
    }
ali@0
  3059
}
ali@0
  3060
ali@70
  3061
gboolean tagcomp(const char *strin,const char *basetag)
ali@0
  3062
{
ali@70
  3063
    gboolean retval;
ali@70
  3064
    gchar *s,*t;
ali@70
  3065
    if (g_utf8_get_char(strin)=='/')
ali@70
  3066
	t=g_utf8_casefold(g_utf8_next_char(strin),-1); /* ignore a slash */
ali@70
  3067
    else
ali@70
  3068
	t=g_utf8_casefold(strin,-1);
ali@70
  3069
    s=g_utf8_casefold(basetag,-1);
ali@70
  3070
    retval=g_str_has_prefix(t,s);
ali@70
  3071
    g_free(s);
ali@70
  3072
    g_free(t);
ali@70
  3073
    return retval;
ali@0
  3074
}
ali@0
  3075
ali@69
  3076
void proghelp(GOptionContext *context)
ali@0
  3077
{
ali@69
  3078
    gchar *help;
ali@40
  3079
    fputs("Bookloupe version " PACKAGE_VERSION ".\n",stderr);
ali@40
  3080
    fputs("Copyright 2000-2005 Jim Tinsley <jtinsley@pobox.com>.\n",stderr);
ali@40
  3081
    fputs("Copyright 2012- J. Ali Harlow <ali@juiblex.co.uk>.\n",stderr);
ali@40
  3082
    fputs("Bookloupe comes wih ABSOLUTELY NO WARRANTY. "
ali@40
  3083
      "For details, read the file COPYING.\n",stderr);
ali@40
  3084
    fputs("This is Free Software; "
ali@40
  3085
      "you may redistribute it under certain conditions (GPL);\n",stderr);
ali@40
  3086
    fputs("read the file COPYING for details.\n\n",stderr);
ali@69
  3087
    help=g_option_context_get_help(context,TRUE,NULL);
ali@69
  3088
    fputs(help,stderr);
ali@69
  3089
    g_free(help);
ali@69
  3090
    fputs("Sample usage: bookloupe warpeace.txt\n\n",stderr);
ali@40
  3091
    fputs("Bookloupe queries anything it thinks shouldn't be in a PG text; "
ali@40
  3092
      "non-ASCII\n",stderr);
ali@40
  3093
    fputs("characters like accented letters, "
ali@40
  3094
      "lines longer than 75 or shorter than 55,\n",stderr);
ali@40
  3095
    fputs("unbalanced quotes or brackets, "
ali@40
  3096
      "a variety of badly formatted punctuation, \n",stderr);
ali@40
  3097
    fputs("HTML tags, some likely typos. "
ali@40
  3098
      "It is NOT a substitute for human judgement.\n",stderr);
ali@0
  3099
    fputs("\n",stderr);
ali@0
  3100
}