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