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