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