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