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