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