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