1 /*************************************************************************/
2 /* bookloupe--check for assorted weirdnesses in a PG candidate text file */
4 /* Copyright 2000-2005 Jim Tinsley <jtinsley@pobox.com> */
5 /* Copyright 2012- J. Ali Harlow <ali@juiblex.co.uk> */
7 /* This program is free software; you can redistribute it and/or modify */
8 /* it under the terms of the GNU General Public License as published by */
9 /* the Free Software Foundation; either version 2 of the License, or */
10 /* (at your option) any later version. */
12 /* This program is distributed in the hope that it will be useful, */
13 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
15 /* GNU General Public License for more details. */
17 /* You should have received a copy of the GNU General Public License */
18 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /*************************************************************************/
26 #define MAXWORDLEN 80 /* max length of one word */
27 #define LINEBUFSIZE 2048 /* buffer size for an input line */
29 #define MAX_USER_TYPOS 1000
30 #define USERTYPO_FILE "gutcheck.typ"
33 #define MAX_PATH 16384
36 char aline[LINEBUFSIZE];
37 char prevline[LINEBUFSIZE];
41 "teh", "th", "og", "fi", "ro", "adn", "yuo", "ot", "fo", "thet", "ane",
42 "nad", "te", "ig", "acn", "ahve", "alot", "anbd", "andt", "awya", "aywa",
43 "bakc", "om", "btu", "byt", "cna", "cxan", "coudl", "dont", "didnt",
44 "couldnt", "wouldnt", "doesnt", "shouldnt", "doign", "ehr", "hmi", "hse",
45 "esle", "eyt", "fitrs", "firts", "foudn", "frmo", "fromt", "fwe", "gaurd",
46 "gerat", "goign", "gruop", "haev", "hda", "hearign", "seeign", "sayign",
47 "herat", "hge", "hsa", "hsi", "hte", "htere", "htese", "htey", "htis",
48 "hvae", "hwich", "idae", "ihs", "iits", "int", "iwll", "iwth", "jsut",
49 "loev", "sefl", "myu", "nkow", "nver", "nwe", "nwo", "ocur", "ohter",
50 "omre", "onyl", "otehr", "otu", "owrk", "owuld", "peice", "peices",
51 "peolpe", "peopel", "perhasp", "perhpas", "pleasent", "poeple", "porblem",
52 "porblems", "rwite", "saidt", "saidh", "saids", "seh", "smae", "smoe",
53 "sohw", "stnad", "stopry", "stoyr", "stpo", "tahn", "taht", "tath",
54 "tehy", "tghe", "tghis", "theri", "theyll", "thgat", "thge", "thier",
55 "thna", "thne", "thnig", "thnigs", "thsi", "thsoe", "thta", "timne",
56 "tirne", "tkae", "tthe", "tyhat", "tyhe", "veyr", "vou", "vour", "vrey",
57 "waht", "wasnt", "awtn", "watn", "wehn", "whic", "whcih", "whihc", "whta",
58 "wihch", "wief", "wiht", "witha", "wiull", "wnat", "wnated", "wnats",
59 "woh", "wohle", "wokr", "woudl", "wriet", "wrod", "wroet", "wroking",
60 "wtih", "wuould", "wya", "yera", "yeras", "yersa", "yoiu", "youve",
61 "ytou", "yuor", "abead", "ahle", "ahout", "ahove", "altbough", "balf",
62 "bardly", "bas", "bave", "baving", "bebind", "beld", "belp", "belped",
63 "ber", "bere", "bim", "bis", "bome", "bouse", "bowever", "buge",
64 "dehates", "deht", "han", "hecause", "hecome", "heen", "hefore", "hegan",
65 "hegin", "heing", "helieve", "henefit", "hetter", "hetween", "heyond",
66 "hig", "higber", "huild", "huy", "hy", "jobn", "joh", "meanwbile",
67 "memher", "memhers", "numher", "numhers", "perbaps", "prohlem", "puhlic",
68 "witbout", "arn", "hin", "hirn", "wrok", "wroked", "amd", "aud",
69 "prornise", "prornised", "modem", "bo", "heside", "chapteb", "chaptee",
73 char *usertypo[MAX_USER_TYPOS];
75 /* Common abbreviations and other OK words not to query as typos. */
77 "mr", "mrs", "mss", "mssrs", "ft", "pm", "st", "dr", "hmm", "h'm", "hmmm",
78 "rd", "sh", "br", "pp", "hm", "cf", "jr", "sr", "vs", "lb", "lbs", "ltd",
79 "pompeii","hawaii","hawaiian", "hotbed", "heartbeat", "heartbeats",
80 "outbid", "outbids", "frostbite", "frostbitten", ""
83 /* Common abbreviations that cause otherwise unexplained periods. */
85 "cent", "cents", "viz", "vol", "vols", "vid", "ed", "al", "etc", "op",
86 "cit", "deg", "min", "chap", "oz", "mme", "mlle", "mssrs", ""
90 * Two-Letter combinations that rarely if ever start words,
91 * but are common scannos or otherwise common letter combinations.
94 "hr", "hl", "cb", "sb", "tb", "wb", "tl", "tn", "rn", "lt", "tj", ""
98 * Two-Letter combinations that rarely if ever end words,
99 * but are common scannos or otherwise common letter combinations.
102 "cb", "gb", "pb", "sb", "tb", "wh", "fr", "br", "qu", "tw", "gl", "fl",
103 "sw", "gr", "sl", "cl", "iy", ""
107 "a", "b", "big", "blockquote", "body", "br", "center", "col", "div", "em",
108 "font", "h1", "h2", "h3", "h4", "h5", "h6", "head", "hr", "html", "i",
109 "img", "li", "meta", "ol", "p", "pre", "small", "span", "strong", "sub",
110 "sup", "table", "td", "tfoot", "thead", "title", "tr", "tt", "u", "ul", ""
114 "<sc>", "</sc>", "/*", "*/", "/#", "#/", "/$", "$/", "<tb>", ""
118 "the", "it's", "their", "an", "mrs", "a", "our", "that's", "its", "whose",
119 "every", "i'll", "your", "my", "mr", "mrs", "mss", "mssrs", "ft", "pm",
120 "st", "dr", "rd", "pp", "cf", "jr", "sr", "vs", "lb", "lbs", "ltd", "i'm",
121 "during", "let", "toward", "among", ""
125 "every", "i'm", "during", "that's", "their", "your", "our", "my", "or",
126 "and", "but", "as", "if", "the", "its", "it's", "until", "than", "whether",
127 "i'll", "whose", "who", "because", "when", "let", "till", "very", "an",
128 "among", "those", "into", "whom", "having", "thence", ""
131 char vowels[] = "aeiouàáâãäæèéêëìíîïòóôõöùúûü";
138 "&", "&", "&",
139 "<", "<", "<",
140 ">", ">", ">",
141 "°", "°", " degrees",
142 "£", "£", "L",
143 """, """, "\"", /* quotation mark = APL quote */
144 "Œ", "Œ", "OE", /* latin capital ligature OE */
145 "œ", "œ", "oe", /* latin small ligature oe */
146 "Š", "Š", "S", /* latin capital letter S with caron */
147 "š", "š", "s", /* latin small letter s with caron */
148 "Ÿ", "Ÿ", "Y", /* latin capital letter Y with diaeresis */
149 "ˆ", "ˆ", "", /* modifier letter circumflex accent */
150 "˜", "˜", "~", /* small tilde, U+02DC ISOdia */
151 " ", " ", " ", /* en space, U+2002 ISOpub */
152 " ", " ", " ", /* em space, U+2003 ISOpub */
153 " ", " ", " ", /* thin space, U+2009 ISOpub */
154 "–", "–", "-", /* en dash, U+2013 ISOpub */
155 "—", "—", "--", /* em dash, U+2014 ISOpub */
156 "’", "’", "'", /* right single quotation mark */
157 "‚", "‚", "'", /* single low-9 quotation mark */
158 "“", "“", "\"", /* left double quotation mark */
159 "”", "”", "\"", /* right double quotation mark */
160 "„", "„", "\"", /* double low-9 quotation mark */
161 "‹", "‹", "\"", /* single left-pointing angle quotation mark */
162 "›", "›", "\"", /* single right-pointing angle quotation mark */
163 " ", " ", " ", /* no-break space = non-breaking space, */
164 "¡", "¡", "!", /* inverted exclamation mark */
165 "¢", "¢", "c", /* cent sign */
166 "£", "£", "L", /* pound sign */
167 "¤", "¤", "$", /* currency sign */
168 "¥", "¥", "Y", /* yen sign = yuan sign */
169 "§", "§", "--", /* section sign */
170 "¨", "¨", " ", /* diaeresis = spacing diaeresis */
171 "©", "©", "(C) ", /* copyright sign */
172 "ª", "ª", " ", /* feminine ordinal indicator */
173 "«", "«", "\"", /* left-pointing double angle quotation mark */
174 "­", "­", "-", /* soft hyphen = discretionary hyphen */
175 "®", "®", "(R) ", /* registered sign = registered trade mark sign */
176 "¯", "¯", " ", /* macron = spacing macron = overline */
177 "°", "°", " degrees", /* degree sign */
178 "±", "±", "+-", /* plus-minus sign = plus-or-minus sign */
179 "²", "²", "2", /* superscript two = superscript digit two */
180 "³", "³", "3", /* superscript three = superscript digit three */
181 "´", "´", " ", /* acute accent = spacing acute */
182 "µ", "µ", "m", /* micro sign */
183 "¶", "¶", "--", /* pilcrow sign = paragraph sign */
184 "¸", "¸", " ", /* cedilla = spacing cedilla */
185 "¹", "¹", "1", /* superscript one = superscript digit one */
186 "º", "º", " ", /* masculine ordinal indicator */
187 "»", "»", "\"", /* right-pointing double angle quotation mark */
188 "¼", "¼", "1/4", /* vulgar fraction one quarter */
189 "½", "½", "1/2", /* vulgar fraction one half */
190 "¾", "¾", "3/4", /* vulgar fraction three quarters */
191 "¿", "¿", "?", /* inverted question mark */
192 "À", "À", "A", /* latin capital letter A with grave */
193 "Á", "Á", "A", /* latin capital letter A with acute */
194 "Â", "Â", "A", /* latin capital letter A with circumflex */
195 "Ã", "Ã", "A", /* latin capital letter A with tilde */
196 "Ä", "Ä", "A", /* latin capital letter A with diaeresis */
197 "Å", "Å", "A", /* latin capital letter A with ring above */
198 "Æ", "Æ", "AE", /* latin capital letter AE */
199 "Ç", "Ç", "C", /* latin capital letter C with cedilla */
200 "È", "È", "E", /* latin capital letter E with grave */
201 "É", "É", "E", /* latin capital letter E with acute */
202 "Ê", "Ê", "E", /* latin capital letter E with circumflex */
203 "Ë", "Ë", "E", /* latin capital letter E with diaeresis */
204 "Ì", "Ì", "I", /* latin capital letter I with grave */
205 "Í", "Í", "I", /* latin capital letter I with acute */
206 "Î", "Î", "I", /* latin capital letter I with circumflex */
207 "Ï", "Ï", "I", /* latin capital letter I with diaeresis */
208 "Ð", "Ð", "E", /* latin capital letter ETH */
209 "Ñ", "Ñ", "N", /* latin capital letter N with tilde */
210 "Ò", "Ò", "O", /* latin capital letter O with grave */
211 "Ó", "Ó", "O", /* latin capital letter O with acute */
212 "Ô", "Ô", "O", /* latin capital letter O with circumflex */
213 "Õ", "Õ", "O", /* latin capital letter O with tilde */
214 "Ö", "Ö", "O", /* latin capital letter O with diaeresis */
215 "×", "×", "*", /* multiplication sign */
216 "Ø", "Ø", "O", /* latin capital letter O with stroke */
217 "Ù", "Ù", "U", /* latin capital letter U with grave */
218 "Ú", "Ú", "U", /* latin capital letter U with acute */
219 "Û", "Û", "U", /* latin capital letter U with circumflex */
220 "Ü", "Ü", "U", /* latin capital letter U with diaeresis */
221 "Ý", "Ý", "Y", /* latin capital letter Y with acute */
222 "Þ", "Þ", "TH", /* latin capital letter THORN */
223 "ß", "ß", "sz", /* latin small letter sharp s = ess-zed */
224 "à", "à", "a", /* latin small letter a with grave */
225 "á", "á", "a", /* latin small letter a with acute */
226 "â", "â", "a", /* latin small letter a with circumflex */
227 "ã", "ã", "a", /* latin small letter a with tilde */
228 "ä", "ä", "a", /* latin small letter a with diaeresis */
229 "å", "å", "a", /* latin small letter a with ring above */
230 "æ", "æ", "ae", /* latin small letter ae */
231 "ç", "ç", "c", /* latin small letter c with cedilla */
232 "è", "è", "e", /* latin small letter e with grave */
233 "é", "é", "e", /* latin small letter e with acute */
234 "ê", "ê", "e", /* latin small letter e with circumflex */
235 "ë", "ë", "e", /* latin small letter e with diaeresis */
236 "ì", "ì", "i", /* latin small letter i with grave */
237 "í", "í", "i", /* latin small letter i with acute */
238 "î", "î", "i", /* latin small letter i with circumflex */
239 "ï", "ï", "i", /* latin small letter i with diaeresis */
240 "ð", "ð", "eth", /* latin small letter eth */
241 "ñ", "ñ", "n", /* latin small letter n with tilde */
242 "ò", "ò", "o", /* latin small letter o with grave */
243 "ó", "ó", "o", /* latin small letter o with acute */
244 "ô", "ô", "o", /* latin small letter o with circumflex */
245 "õ", "õ", "o", /* latin small letter o with tilde */
246 "ö", "ö", "o", /* latin small letter o with diaeresis */
247 "÷", "÷", "/", /* division sign */
248 "ø", "ø", "o", /* latin small letter o with stroke */
249 "ù", "ù", "u", /* latin small letter u with grave */
250 "ú", "ú", "u", /* latin small letter u with acute */
251 "û", "û", "u", /* latin small letter u with circumflex */
252 "ü", "ü", "u", /* latin small letter u with diaeresis */
253 "ý", "ý", "y", /* latin small letter y with acute */
254 "þ", "þ", "th", /* latin small letter thorn */
255 "ÿ", "ÿ", "y", /* latin small letter y with diaeresis */
259 /* special characters */
260 #define CHAR_SPACE 32
264 #define CHAR_DQUOTE 34
265 #define CHAR_SQUOTE 39
266 #define CHAR_OPEN_SQUOTE 96
267 #define CHAR_TILDE 126
268 #define CHAR_ASTERISK 42
269 #define CHAR_FORESLASH 47
270 #define CHAR_CARAT 94
272 #define CHAR_UNDERSCORE '_'
273 #define CHAR_OPEN_CBRACK '{'
274 #define CHAR_CLOSE_CBRACK '}'
275 #define CHAR_OPEN_RBRACK '('
276 #define CHAR_CLOSE_RBRACK ')'
277 #define CHAR_OPEN_SBRACK '['
278 #define CHAR_CLOSE_SBRACK ']'
280 /* longest and shortest normal PG line lengths */
281 #define LONGEST_PG_LINE 75
282 #define WAY_TOO_LONG 80
283 #define SHORTEST_PG_LINE 55
285 #define SWITCHES "ESTPXLOYHWVMUD" /* switches:- */
286 /* D - ignore DP-specific markup */
287 /* E - echo queried line */
288 /* S - check single quotes */
289 /* T - check common typos */
290 /* P - require closure of quotes on */
291 /* every paragraph */
292 /* X - "Trust no one" :-) Paranoid! */
293 /* Queries everything */
294 /* L - line end checking defaults on */
295 /* -L turns it off */
296 /* O - overview. Just shows counts. */
297 /* Y - puts errors to stdout */
298 /* instead of stderr */
299 /* H - Echoes header fields */
300 /* M - Ignore markup in < > */
301 /* U - Use file of User-defined Typos*/
302 /* W - Defaults for use on Web upload*/
303 /* V - Verbose - list EVERYTHING! */
304 #define SWITNO 14 /* max number of switch parms */
305 /* - used for defining array-size */
306 #define MINARGS 1 /* minimum no of args excl switches */
307 #define MAXARGS 1 /* maximum no of args excl switches */
309 int pswit[SWITNO]; /* program switches set by SWITCHES */
311 #define ECHO_SWITCH 0
312 #define SQUOTE_SWITCH 1
313 #define TYPO_SWITCH 2
314 #define QPARA_SWITCH 3
315 #define PARANOID_SWITCH 4
316 #define LINE_END_SWITCH 5
317 #define OVERVIEW_SWITCH 6
318 #define STDOUT_SWITCH 7
319 #define HEADER_SWITCH 8
321 #define VERBOSE_SWITCH 10
322 #define MARKUP_SWITCH 11
323 #define USERTYPO_SWITCH 12
326 long cnt_dquot; /* for overview mode, count of doublequote queries */
327 long cnt_squot; /* for overview mode, count of singlequote queries */
328 long cnt_brack; /* for overview mode, count of brackets queries */
329 long cnt_bin; /* for overview mode, count of non-ASCII queries */
330 long cnt_odd; /* for overview mode, count of odd character queries */
331 long cnt_long; /* for overview mode, count of long line errors */
332 long cnt_short; /* for overview mode, count of short line queries */
333 long cnt_punct; /* for overview mode, count of punctuation and spacing queries */
334 long cnt_dash; /* for overview mode, count of dash-related queries */
335 long cnt_word; /* for overview mode, count of word queries */
336 long cnt_html; /* for overview mode, count of html queries */
337 long cnt_lineend; /* for overview mode, count of line-end queries */
338 long cnt_spacend; /* count of lines with space at end */
339 long linecnt; /* count of total lines in the file */
340 long checked_linecnt; /* count of lines actually checked */
343 void procfile(char *);
345 #define LOW_THRESHOLD 0
346 #define HIGH_THRESHOLD 1
352 #define FIRST_OF_PAIR 0
353 #define SECOND_OF_PAIR 1
355 #define MAX_WORDPAIR 1000
357 char running_from[MAX_PATH];
359 int mixdigit(char *);
360 char *getaword(char *,char *);
361 int matchword(char *,char *);
362 char *flgets(char *,int,FILE *,long);
363 void lowerit(char *);
364 int gcisalpha(unsigned char);
365 int gcisdigit(unsigned char);
366 int gcisletter(unsigned char);
367 char *gcstrchr(char *s,char c);
368 void postprocess_for_HTML(char *);
369 char *linehasmarkup(char *);
370 char *losemarkup(char *);
371 int tagcomp(char *,char *);
372 char *loseentities(char *);
375 void postprocess_for_DP(char *);
377 char wrk[LINEBUFSIZE];
380 #define MAX_QWORD_LENGTH 40
381 char qword[MAX_QWORD][MAX_QWORD_LENGTH];
382 char qperiod[MAX_QWORD][MAX_QWORD_LENGTH];
383 signed int dupcnt[MAX_QWORD];
385 int main(int argc,char **argv)
389 char usertypo_file[MAX_PATH];
391 if (strlen(argv[0])<sizeof(running_from))
392 /* save the path to the executable */
393 strcpy(running_from,argv[0]);
394 /* find out what directory we're running from */
395 s=running_from+strlen(running_from);
396 for (;*s!='/' && *s!='\\' && s>=running_from;s--)
398 switno=strlen(SWITCHES);
399 for (i=switno;--i>0;)
400 pswit[i]=0; /* initialise switches */
402 * Standard loop to extract switches.
403 * When we come out of this loop, the arguments will be
404 * in argv[0] upwards and the switches used will be
405 * represented by their equivalent elements in pswit[]
407 while (--argc>0 && **++argv=='-')
408 for (argsw=argv[0]+1;*argsw!='\0';argsw++)
409 for (i=switno,invarg=1;(--i>=0) && invarg==1;)
410 if ((toupper(*argsw))==SWITCHES[i])
415 /* Paranoid checking is turned OFF, not on, by its switch */
416 pswit[PARANOID_SWITCH]^=1;
417 if (pswit[PARANOID_SWITCH])
418 /* if running in paranoid mode force typo checks as well */
419 pswit[TYPO_SWITCH]=pswit[TYPO_SWITCH]^1;
420 /* Line-end checking is turned OFF, not on, by its switch */
421 pswit[LINE_END_SWITCH]^=1;
422 /* Echoing is turned OFF, not on, by its switch */
423 pswit[ECHO_SWITCH]^=1;
424 if (pswit[OVERVIEW_SWITCH])
425 /* just print summary; don't echo */
426 pswit[ECHO_SWITCH]=0;
428 * Web uploads - for the moment, this is really just a placeholder
429 * until we decide what processing we really want to do on web uploads
431 if (pswit[WEB_SWITCH])
433 /* specific override for web uploads */
434 pswit[ECHO_SWITCH]=1;
435 pswit[SQUOTE_SWITCH]=0;
436 pswit[TYPO_SWITCH]=1;
437 pswit[QPARA_SWITCH]=0;
438 pswit[PARANOID_SWITCH]=1;
439 pswit[LINE_END_SWITCH]=0;
440 pswit[OVERVIEW_SWITCH]=0;
441 pswit[STDOUT_SWITCH]=0;
442 pswit[HEADER_SWITCH]=1;
443 pswit[VERBOSE_SWITCH]=0;
444 pswit[MARKUP_SWITCH]=0;
445 pswit[USERTYPO_SWITCH]=0;
448 if (argc<MINARGS || argc>MAXARGS)
450 /* check number of args */
454 /* read in the user-defined stealth scanno list */
455 if (pswit[USERTYPO_SWITCH])
457 /* ... we were told we had one! */
458 usertypofile=fopen(USERTYPO_FILE,"rb");
461 /* not in cwd. try excuteable directory. */
462 strcpy(usertypo_file,running_from);
463 strcat(usertypo_file,USERTYPO_FILE);
464 usertypofile=fopen(usertypo_file,"rb");
466 /* we ain't got no user typo file! */
467 printf(" --> I couldn't find gutcheck.typ "
468 "-- proceeding without user typos.\n");
474 /* we managed to open a User Typo File! */
475 if (pswit[USERTYPO_SWITCH])
477 while (flgets(aline,LINEBUFSIZE-1,usertypofile,
478 (long)usertypo_count))
484 s=malloc(strlen(aline)+1);
487 fprintf(stderr,"bookloupe: cannot get enough "
488 "memory for user typo file!\n");
492 usertypo[usertypo_count]=s;
494 if (usertypo_count>=MAX_USER_TYPOS)
496 printf(" --> Only %d user-defined typos "
497 "allowed: ignoring the rest\n",
505 fclose(usertypofile);
508 fprintf(stderr,"bookloupe: Check and report on an e-text\n");
509 cnt_dquot=cnt_squot=cnt_brack=cnt_bin=cnt_odd=cnt_long=
510 cnt_short=cnt_punct=cnt_dash=cnt_word=cnt_html=cnt_lineend=
513 if (pswit[OVERVIEW_SWITCH])
515 printf(" Checked %ld lines of %ld (head+foot = %ld)\n\n",
516 checked_linecnt,linecnt,linecnt-checked_linecnt);
517 printf(" --------------- Queries found --------------\n");
519 printf(" Long lines: %14ld\n",cnt_long);
521 printf(" Short lines: %14ld\n",cnt_short);
523 printf(" Line-end problems: %14ld\n",cnt_lineend);
525 printf(" Common typos: %14ld\n",cnt_word);
527 printf(" Unmatched quotes: %14ld\n",cnt_dquot);
529 printf(" Unmatched SingleQuotes: %14ld\n",cnt_squot);
531 printf(" Unmatched brackets: %14ld\n",cnt_brack);
533 printf(" Non-ASCII characters: %14ld\n",cnt_bin);
535 printf(" Proofing characters: %14ld\n",cnt_odd);
537 printf(" Punctuation & spacing queries: %14ld\n",cnt_punct);
539 printf(" Non-standard dashes: %14ld\n",cnt_dash);
541 printf(" Possible HTML tags: %14ld\n",cnt_html);
543 printf(" TOTAL QUERIES %14ld\n",
544 cnt_dquot+cnt_squot+cnt_brack+cnt_bin+cnt_odd+cnt_long+
545 cnt_short+cnt_punct+cnt_dash+cnt_word+cnt_html+cnt_lineend);
550 struct first_pass_results {
551 long firstline,astline;
552 long footerline,totlen,binlen,alphalen,endquote_count,shortline,dotcomma;
553 long fslashline,hyphens,longline,verylongline,htmcount,standalone_digit;
554 long spacedash,emdash,space_emdash,non_PG_space_emdash,PG_space_emdash;
555 signed int Dutchcount,Frenchcount;
561 * Run a first pass - verify that it's a valid PG
562 * file, decide whether to report some things that
563 * occur many times in the text like long or short
564 * lines, non-standard dashes, etc.
566 struct first_pass_results *first_pass(FILE *infile)
568 char laststart=CHAR_SPACE,*s;
570 unsigned int lastlen=0,lastblen=0;
571 long spline=0,nspline=0;
572 static struct first_pass_results results={0};
573 char inword[MAXWORDLEN]="";
574 while (fgets(aline,LINEBUFSIZE-1,infile))
576 while (aline[strlen(aline)-1]==10 || aline[strlen(aline)-1]==13)
577 aline[strlen(aline)-1]=0;
579 if (strstr(aline,"*END") && strstr(aline,"SMALL PRINT") &&
580 (strstr(aline,"PUBLIC DOMAIN") || strstr(aline,"COPYRIGHT")))
583 printf(" --> Duplicate header?\n");
584 spline=linecnt+1; /* first line of non-header text, that is */
586 if (!strncmp(aline,"*** START",9) && strstr(aline,"PROJECT GUTENBERG"))
589 printf(" --> Duplicate header?\n");
590 nspline=linecnt+1; /* first line of non-header text, that is */
592 if (spline || nspline)
595 if (strstr(aline,"end") && strstr(aline,"project gutenberg"))
597 if (strstr(aline,"end")<strstr(aline,"project gutenberg"))
599 if (results.footerline)
601 /* it's an old-form header - we can detect duplicates */
603 printf(" --> Duplicate footer?\n");
606 results.footerline=linecnt;
611 results.firstline=spline;
613 results.firstline=nspline; /* override with new */
614 if (results.footerline)
615 continue; /* don't count the boilerplate in the footer */
617 results.totlen+=llen;
620 if ((unsigned char)aline[i]>127)
622 if (gcisalpha(aline[i]))
624 if (i>0 && aline[i]==CHAR_DQUOTE && isalpha(aline[i-1]))
625 results.endquote_count++;
627 if (strlen(aline)>2 && lastlen>2 && lastlen<SHORTEST_PG_LINE &&
628 lastblen>2 && lastblen>SHORTEST_PG_LINE && laststart!=CHAR_SPACE)
630 if (*aline && (unsigned char)aline[strlen(aline)-1]<=CHAR_SPACE)
632 if (strstr(aline,".,"))
634 /* only count ast lines for ignoring purposes where there is */
635 /* locase text on the line */
636 if (strstr(aline,"*"))
639 if (*s>='a' && *s<='z')
644 if (strstr(aline,"/"))
645 results.fslashline++;
646 for (i=llen-1;i>0 && (unsigned char)aline[i]<=CHAR_SPACE;i--)
648 if (aline[i]=='-' && aline[i-1]!='-')
650 if (llen>LONGEST_PG_LINE)
652 if (llen>WAY_TOO_LONG)
653 results.verylongline++;
654 if (strstr(aline,"<") && strstr(aline,">"))
656 i=(signed int)(strstr(aline,">")-strstr(aline,"<")+1);
659 if (strstr(aline,"<i>"))
660 results.htmcount+=4; /* bonus marks! */
662 /* Check for spaced em-dashes */
663 if (strstr(aline,"--"))
666 if (*(strstr(aline,"--")-1)==CHAR_SPACE ||
667 (*(strstr(aline,"--")+2)==CHAR_SPACE))
668 results.space_emdash++;
669 if (*(strstr(aline,"--")-1)==CHAR_SPACE &&
670 (*(strstr(aline,"--")+2)==CHAR_SPACE))
671 /* count of em-dashes with spaces both sides */
672 results.non_PG_space_emdash++;
673 if (*(strstr(aline,"--")-1)!=CHAR_SPACE &&
674 (*(strstr(aline,"--")+2)!=CHAR_SPACE))
675 /* count of PG-type em-dashes with no spaces */
676 results.PG_space_emdash++;
680 s=getaword(s,inword);
681 if (!strcmp(inword,"hij") || !strcmp(inword,"niet"))
682 results.Dutchcount++;
683 if (!strcmp(inword,"dans") || !strcmp(inword,"avec"))
684 results.Frenchcount++;
685 if (!strcmp(inword,"0") || !strcmp(inword,"1"))
686 results.standalone_digit++;
688 /* Check for spaced dashes */
689 if (strstr(aline," -") && *(strstr(aline," -")+2)!='-')
692 lastlen=strlen(aline);
699 signed int shortline,longline,bin,dash,dotcomma,ast,fslash,digit,hyphen;
700 signed int endquote,isDutch,isFrench;
706 * Make some snap decisions based on the first pass results.
708 struct warnings *report_first_pass(struct first_pass_results *results)
710 static struct warnings warnings={0};
712 printf(" --> %ld lines in this file have white space at end\n",
715 if (results->dotcomma>5)
718 printf(" --> %ld lines in this file contain '.,'. "
719 "Not reporting them.\n",results->dotcomma);
722 * If more than 50 lines, or one-tenth, are short,
723 * don't bother reporting them.
725 warnings.shortline=1;
726 if (results->shortline>50 || results->shortline*10>linecnt)
728 warnings.shortline=0;
729 printf(" --> %ld lines in this file are short. "
730 "Not reporting short lines.\n",results->shortline);
733 * If more than 50 lines, or one-tenth, are long,
734 * don't bother reporting them.
737 if (results->longline>50 || results->longline*10>linecnt)
740 printf(" --> %ld lines in this file are long. "
741 "Not reporting long lines.\n",results->longline);
743 /* If more than 10 lines contain asterisks, don't bother reporting them. */
745 if (results->astline>10)
748 printf(" --> %ld lines in this file contain asterisks. "
749 "Not reporting them.\n",results->astline);
752 * If more than 10 lines contain forward slashes,
753 * don't bother reporting them.
756 if (results->fslashline>10)
759 printf(" --> %ld lines in this file contain forward slashes. "
760 "Not reporting them.\n",results->fslashline);
763 * If more than 20 lines contain unpunctuated endquotes,
764 * don't bother reporting them.
767 if (results->endquote_count>20)
770 printf(" --> %ld lines in this file contain unpunctuated endquotes. "
771 "Not reporting them.\n",results->endquote_count);
774 * If more than 15 lines contain standalone digits,
775 * don't bother reporting them.
778 if (results->standalone_digit>10)
781 printf(" --> %ld lines in this file contain standalone 0s and 1s. "
782 "Not reporting them.\n",results->standalone_digit);
785 * If more than 20 lines contain hyphens at end,
786 * don't bother reporting them.
789 if (results->hyphens>20)
792 printf(" --> %ld lines in this file have hyphens at end. "
793 "Not reporting them.\n",results->hyphens);
795 if (results->htmcount>20 && !pswit[MARKUP_SWITCH])
797 printf(" --> Looks like this is HTML. Switching HTML mode ON.\n");
798 pswit[MARKUP_SWITCH]=1;
800 if (results->verylongline>0)
801 printf(" --> %ld lines in this file are VERY long!\n",
802 results->verylongline);
804 * If there are more non-PG spaced dashes than PG em-dashes,
805 * assume it's deliberate.
806 * Current PG guidelines say don't use them, but older texts do,
807 * and some people insist on them whatever the guidelines say.
810 if (results->spacedash+results->non_PG_space_emdash>
811 results->PG_space_emdash)
814 printf(" --> There are %ld spaced dashes and em-dashes. "
815 "Not reporting them.\n",
816 results->spacedash+results->non_PG_space_emdash);
818 /* If more than a quarter of characters are hi-bit, bug out. */
820 if (results->binlen*4>results->totlen)
822 printf(" --> This file does not appear to be ASCII. "
823 "Terminating. Best of luck with it!\n");
826 if (results->alphalen*4<results->totlen)
828 printf(" --> This file does not appear to be text. "
829 "Terminating. Best of luck with it!\n");
832 if (results->binlen*100>results->totlen || results->binlen>100)
834 printf(" --> There are a lot of foreign letters here. "
835 "Not reporting them.\n");
839 if (results->Dutchcount>50)
842 printf(" --> This looks like Dutch - "
843 "switching off dashes and warnings for 's Middags case.\n");
846 if (results->Frenchcount>50)
849 printf(" --> This looks like French - "
850 "switching off some doublepunct.\n");
852 if (results->firstline && results->footerline)
853 printf(" The PG header and footer appear to be already on.\n");
856 if (results->firstline)
857 printf(" The PG header is on - no footer.\n");
858 if (results->footerline)
859 printf(" The PG footer is on - no header.\n");
862 if (pswit[VERBOSE_SWITCH])
865 warnings.shortline=1;
874 printf(" *** Verbose output is ON -- you asked for it! ***\n");
876 if (warnings.isDutch)
878 if (results->footerline>0 && results->firstline>0 &&
879 results->footerline>results->firstline &&
880 results->footerline-results->firstline<100)
882 printf(" --> I don't really know where this text starts. \n");
883 printf(" There are no reference points.\n");
884 printf(" I'm going to have to report the header and footer "
886 results->firstline=0;
893 signed int c_unders,c_brack,s_brack,r_brack;
894 signed int open_single_quote,close_single_quote;
900 * Look along the line, accumulate the count of quotes, and see
901 * if this is an empty line - i.e. a line with nothing on it
903 * If line has just spaces, period, * and/or - on it, don't
904 * count it, since empty lines with asterisks or dashes to
905 * separate sections are common.
907 * Returns: Non-zero if the line is empty.
909 int analyse_quotes(const char *s,struct counters *counters)
911 signed int guessquote=0;
912 int isemptyline=1; /* assume the line is empty until proven otherwise */
917 if (*s==CHAR_SQUOTE || *s==CHAR_OPEN_SQUOTE)
922 * At start of line, it can only be an openquote.
923 * Hardcode a very common exception!
925 if (strncmp(s+2,"tis",3) && strncmp(s+2,"Tis",3))
926 counters->open_single_quote++;
928 else if (gcisalpha(s[-1]) && gcisalpha(s[1]))
929 /* Do nothing! it's definitely an apostrophe, not a quote */
931 /* it's outside a word - let's check it out */
932 else if (*s==CHAR_OPEN_SQUOTE || gcisalpha(s[1]))
934 /* it damwell better BE an openquote */
935 if (strncmp(s+1,"tis",3) && strncmp(s+1,"Tis",3))
936 /* hardcode a very common exception! */
937 counters->open_single_quote++;
941 /* now - is it a closequote? */
942 guessquote=0; /* accumulate clues */
943 if (gcisalpha(s[-1]))
945 /* it follows a letter - could be either */
949 /* looks like a plural apostrophe */
951 if (s[1]==CHAR_SPACE) /* bonus marks! */
955 /* it doesn't have a letter either side */
956 else if (strchr(".?!,;:",s[-1]) && strchr(".?!,;: ",s[1]))
957 guessquote+=8; /* looks like a closequote */
960 if (counters->open_single_quote>counters->close_single_quote)
962 * Give it the benefit of some doubt,
963 * if a squote is already open.
969 counters->close_single_quote++;
972 if (*s!=CHAR_SPACE && *s!='-' && *s!='.' && *s!=CHAR_ASTERISK &&
974 isemptyline=0; /* ignore lines like * * * as spacers */
975 if (*s==CHAR_UNDERSCORE)
976 counters->c_unders++;
977 if (*s==CHAR_OPEN_CBRACK)
979 if (*s==CHAR_CLOSE_CBRACK)
981 if (*s==CHAR_OPEN_RBRACK)
983 if (*s==CHAR_CLOSE_RBRACK)
985 if (*s==CHAR_OPEN_SBRACK)
987 if (*s==CHAR_CLOSE_SBRACK)
995 * check_for_odd_characters:
997 * Check for binary and other odd characters.
999 void check_for_odd_characters(const char *aline,const struct warnings *warnings,
1002 /* Don't repeat multiple warnings on one line. */
1003 signed int eNon_A=0,eTab=0,eTilde=0,eCarat=0,eFSlash=0,eAst=0;
1006 for (s=aline;*s;s++)
1008 c=*(unsigned char *)s;
1009 if (!eNon_A && (*s<CHAR_SPACE && *s!=9 && *s!='\n' || c>127))
1011 if (pswit[ECHO_SWITCH])
1012 printf("\n%s\n",aline);
1013 if (!pswit[OVERVIEW_SWITCH])
1015 printf(" Line %ld column %d - "
1016 "Non-ISO-8859 character %d\n",linecnt,(int)(s-aline)+1,c);
1018 printf(" Line %ld column %d - Non-ASCII character %d\n",
1019 linecnt,(int)(s-aline)+1,c);
1024 if (!eTab && *s==CHAR_TAB)
1026 if (pswit[ECHO_SWITCH])
1027 printf("\n%s\n",aline);
1028 if (!pswit[OVERVIEW_SWITCH])
1029 printf(" Line %ld column %d - Tab character?\n",
1030 linecnt,(int)(s-aline)+1);
1035 if (!eTilde && *s==CHAR_TILDE)
1038 * Often used by OCR software to indicate an
1039 * unrecognizable character.
1041 if (pswit[ECHO_SWITCH])
1042 printf("\n%s\n",aline);
1043 if (!pswit[OVERVIEW_SWITCH])
1044 printf(" Line %ld column %d - Tilde character?\n",
1045 linecnt,(int)(s-aline)+1);
1050 if (!eCarat && *s==CHAR_CARAT)
1052 if (pswit[ECHO_SWITCH])
1053 printf("\n%s\n",aline);
1054 if (!pswit[OVERVIEW_SWITCH])
1055 printf(" Line %ld column %d - Carat character?\n",
1056 linecnt,(int)(s-aline)+1);
1061 if (!eFSlash && *s==CHAR_FORESLASH && warnings->fslash)
1063 if (pswit[ECHO_SWITCH])
1064 printf("\n%s\n",aline);
1065 if (!pswit[OVERVIEW_SWITCH])
1066 printf(" Line %ld column %d - Forward slash?\n",
1067 linecnt,(int)(s-aline)+1);
1073 * Report asterisks only in paranoid mode,
1074 * since they're often deliberate.
1076 if (!eAst && pswit[PARANOID_SWITCH] && warnings->ast && !isemptyline &&
1079 if (pswit[ECHO_SWITCH])
1080 printf("\n%s\n",aline);
1081 if (!pswit[OVERVIEW_SWITCH])
1082 printf(" Line %ld column %d - Asterisk?\n",
1083 linecnt,(int)(s-aline)+1);
1092 * check_for_long_line:
1094 * Check for line too long.
1096 void check_for_long_line(const char *aline)
1098 if (strlen(aline)>LONGEST_PG_LINE)
1100 if (pswit[ECHO_SWITCH])
1101 printf("\n%s\n",aline);
1102 if (!pswit[OVERVIEW_SWITCH])
1103 printf(" Line %ld column %d - Long line %d\n",
1104 linecnt,strlen(aline),strlen(aline));
1110 struct line_properties {
1111 unsigned int len,blen;
1116 * check_for_short_line:
1118 * Check for line too short.
1120 * This one is a bit trickier to implement: we don't want to
1121 * flag the last line of a paragraph for being short, so we
1122 * have to wait until we know that our current line is a
1123 * "normal" line, then report the _previous_ line if it was too
1124 * short. We also don't want to report indented lines like
1125 * chapter heads or formatted quotations. We therefore keep
1126 * last->len as the length of the last line examined, and
1127 * last->blen as the length of the last but one, and try to
1128 * suppress unnecessary warnings by checking that both were of
1129 * "normal" length. We keep the first character of the last
1130 * line in last->start, and if it was a space, we assume that
1131 * the formatting is deliberate. I can't figure out a way to
1132 * distinguish something like a quoted verse left-aligned or
1133 * the header or footer of a letter from a paragraph of short
1134 * lines - maybe if I examined the whole paragraph, and if the
1135 * para has less than, say, 8 lines and if all lines are short,
1136 * then just assume it's OK? Need to look at some texts to see
1137 * how often a formula like this would get the right result.
1139 void check_for_short_line(const char *aline,const struct line_properties *last)
1141 if (strlen(aline)>1 && last->len>1 && last->len<SHORTEST_PG_LINE &&
1142 last->blen>1 && last->blen>SHORTEST_PG_LINE && last->start!=CHAR_SPACE)
1144 if (pswit[ECHO_SWITCH])
1145 printf("\n%s\n",prevline);
1146 if (!pswit[OVERVIEW_SWITCH])
1147 printf(" Line %ld column %d - Short line %d?\n",
1148 linecnt-1,strlen(prevline),strlen(prevline));
1155 * check_for_starting_punctuation:
1157 * Look for punctuation other than full ellipses at start of line.
1159 void check_for_starting_punctuation(const char *aline)
1161 if (*aline && strchr(".?!,;:",aline[0]) && strncmp(". . .",aline,5))
1163 if (pswit[ECHO_SWITCH])
1164 printf("\n%s\n",aline);
1165 if (!pswit[OVERVIEW_SWITCH])
1166 printf(" Line %ld column 1 - Begins with punctuation?\n",
1178 void procfile(char *filename)
1180 char *s,*t,*s1,*wordstart;
1181 char inword[MAXWORDLEN],testword[MAXWORDLEN];
1182 char parastart[81]; /* first line of current para */
1184 struct first_pass_results *first_pass_results;
1185 struct warnings *warnings;
1186 struct counters counters={0};
1187 struct line_properties last={0};
1189 long squot,start_para_line;
1190 signed int i,j,llen,isacro,isellipsis,istypo,alower;
1191 signed int dquotepar,squotepar;
1192 signed int isnewpara,vowel,consonant;
1193 char dquote_err[80],squote_err[80],rbrack_err[80],sbrack_err[80],
1194 cbrack_err[80],unders_err[80];
1195 signed int qword_index,qperiod_index,isdup;
1197 last.start=CHAR_SPACE;
1198 *dquote_err=*squote_err=*rbrack_err=*cbrack_err=*sbrack_err=
1199 *unders_err=*prevline=0;
1200 linecnt=checked_linecnt=start_para_line=0;
1202 i=llen=isacro=isellipsis=istypo=0;
1203 isnewpara=vowel=consonant=enddash=0;
1204 qword_index=qperiod_index=isdup=0;
1205 *inword=*testword=0;
1206 dquotepar=squotepar=0;
1207 for (j=0;j<MAX_QWORD;j++)
1210 for (i=0;i<MAX_QWORD_LENGTH;i++)
1216 infile=fopen(filename,"rb");
1219 if (pswit[STDOUT_SWITCH])
1220 fprintf(stdout,"bookloupe: cannot open %s\n",filename);
1222 fprintf(stderr,"bookloupe: cannot open %s\n",filename);
1225 fprintf(stdout,"\n\nFile: %s\n\n",filename);
1226 first_pass_results=first_pass(infile);
1227 warnings=report_first_pass(first_pass_results);
1230 * Here we go with the main pass. Hold onto yer hat!
1231 * Re-init some variables we've dirtied.
1234 while (flgets(aline,LINEBUFSIZE-1,infile,linecnt+1))
1239 if (pswit[DP_SWITCH] && !strncmp(aline,"-----File: ",11))
1240 continue; // skip DP page separators completely
1241 if (linecnt<first_pass_results->firstline ||
1242 (first_pass_results->footerline>0 &&
1243 linecnt>first_pass_results->footerline))
1245 if (pswit[HEADER_SWITCH])
1247 if (!strncmp(aline,"Title:",6))
1248 printf(" %s\n",aline);
1249 if (!strncmp(aline,"Author:",7))
1250 printf(" %s\n",aline);
1251 if (!strncmp(aline,"Release Date:",13))
1252 printf(" %s\n",aline);
1253 if (!strncmp(aline,"Edition:",8))
1254 printf(" %s\n\n",aline);
1256 continue; /* skip through the header */
1261 * If we are in a state of unbalanced quotes, and this line
1262 * doesn't begin with a quote, output the stored error message.
1263 * If the -P switch was used, print the warning even if the
1264 * new para starts with quotes.
1270 if (*t!=CHAR_DQUOTE || pswit[QPARA_SWITCH])
1272 if (!pswit[OVERVIEW_SWITCH])
1274 if (pswit[ECHO_SWITCH])
1275 printf("\n%s\n",parastart);
1283 if (*t!=CHAR_SQUOTE && *t!=CHAR_OPEN_SQUOTE ||
1284 pswit[QPARA_SWITCH] || squot)
1286 if (!pswit[OVERVIEW_SWITCH])
1288 if (pswit[ECHO_SWITCH])
1289 printf("\n%s\n",parastart);
1299 if (!pswit[OVERVIEW_SWITCH])
1301 if (pswit[ECHO_SWITCH])
1302 printf("\n%s\n",parastart);
1310 if (!pswit[OVERVIEW_SWITCH])
1312 if (pswit[ECHO_SWITCH])
1313 printf("\n%s\n",parastart);
1321 if (!pswit[OVERVIEW_SWITCH])
1323 if (pswit[ECHO_SWITCH])
1324 printf("\n%s\n",parastart);
1332 if (!pswit[OVERVIEW_SWITCH])
1334 if (pswit[ECHO_SWITCH])
1335 printf("\n%s\n",parastart);
1341 *dquote_err=*squote_err=*rbrack_err=*cbrack_err=
1342 *sbrack_err=*unders_err=0;
1343 isemptyline=analyse_quotes(aline,&counters);
1344 if (isnewpara && !isemptyline)
1346 /* This line is the start of a new paragraph. */
1347 start_para_line=linecnt;
1348 /* Capture its first line in case we want to report it later. */
1349 strncpy(parastart,aline,80);
1351 dquotepar=squotepar=0; /* restart the quote count */
1353 while (!gcisalpha(*s) && !gcisdigit(*s) && *s)
1355 if (*s>='a' && *s<='z')
1357 /* and its first letter is lowercase */
1358 if (pswit[ECHO_SWITCH])
1359 printf("\n%s\n",aline);
1360 if (!pswit[OVERVIEW_SWITCH])
1361 printf(" Line %ld column %d - "
1362 "Paragraph starts with lower-case\n",
1363 linecnt,(int)(s-aline)+1);
1367 isnewpara=0; /* Signal the end of new para processing. */
1369 /* Check for an em-dash broken at line end. */
1370 if (enddash && *aline=='-')
1372 if (pswit[ECHO_SWITCH])
1373 printf("\n%s\n",aline);
1374 if (!pswit[OVERVIEW_SWITCH])
1375 printf(" Line %ld column 1 - Broken em-dash?\n",linecnt);
1380 for (s=aline+strlen(aline)-1;*s==' ' && s>aline;s--)
1382 if (s>=aline && *s=='-')
1385 * Check for invalid or questionable characters in the line
1386 * Anything above 127 is invalid for plain ASCII, and
1387 * non-printable control characters should also be flagged.
1388 * Tabs should generally not be there.
1390 for (s=aline;*s;s++)
1392 i=(unsigned char)*s;
1393 if (i<CHAR_SPACE && i!=CHAR_LF && i!=CHAR_CR && i!=CHAR_TAB)
1395 if (pswit[ECHO_SWITCH])
1396 printf("\n%s\n",aline);
1397 if (!pswit[OVERVIEW_SWITCH])
1398 printf(" Line %ld column %d - Control character %d\n",
1399 linecnt,(int)(s-aline)+1,i);
1405 check_for_odd_characters(aline,warnings,isemptyline);
1406 if (warnings->longline)
1407 check_for_long_line(aline);
1408 if (warnings->shortline)
1409 check_for_short_line(aline,&last);
1411 last.len=strlen(aline);
1412 last.start=aline[0];
1413 check_for_starting_punctuation(aline);
1415 * Check for spaced em-dashes.
1416 * We must check _all_ occurrences of "--" on the line
1417 * hence the loop - even if the first double-dash is OK
1418 * there may be another that's wrong later on.
1423 while (strstr(s,"--"))
1425 if (*(strstr(s,"--")-1)==CHAR_SPACE ||
1426 (*(strstr(s,"--")+2)==CHAR_SPACE))
1428 if (pswit[ECHO_SWITCH])
1429 printf("\n%s\n",aline);
1430 if (!pswit[OVERVIEW_SWITCH])
1431 printf(" Line %ld column %d - Spaced em-dash?\n",
1432 linecnt,(int)(strstr(s,"--")-aline)+1);
1439 /* Check for spaced dashes. */
1442 if (strstr(aline," -"))
1444 if (*(strstr(aline," -")+2)!='-')
1446 if (pswit[ECHO_SWITCH])
1447 printf("\n%s\n",aline);
1448 if (!pswit[OVERVIEW_SWITCH])
1449 printf(" Line %ld column %d - Spaced dash?\n",
1450 linecnt,(int)(strstr(aline," -")-aline)+1);
1455 else if (strstr(aline,"- "))
1457 if (*(strstr(aline,"- ")-1)!='-')
1459 if (pswit[ECHO_SWITCH])
1460 printf("\n%s\n",aline);
1461 if (!pswit[OVERVIEW_SWITCH])
1462 printf(" Line %ld column %d - Spaced dash?\n",
1463 linecnt,(int)(strstr(aline,"- ")-aline)+1);
1470 * Check for unmarked paragraphs indicated by separate speakers.
1471 * May well be false positive:
1472 * "Bravo!" "Wonderful!" called the crowd.
1473 * but useful all the same.
1477 if (strstr(aline,"\" \""))
1478 s=strstr(aline,"\" \"");
1479 if (strstr(aline,"\" \""))
1480 s=strstr(aline,"\" \"");
1483 if (pswit[ECHO_SWITCH])
1484 printf("\n%s\n",aline);
1485 if (!pswit[OVERVIEW_SWITCH])
1486 printf(" Line %ld column %d - "
1487 "Query missing paragraph break?\n",
1488 linecnt,(int)(s-aline)+1);
1493 * Check for "to he" and other easy he/be errors.
1494 * This is a very inadequate effort on the he/be problem,
1495 * but the phrase "to he" is always an error, whereas "to
1496 * be" is quite common.
1497 * Similarly, '"Quiet!", be said.' is a non-be error
1498 * "to he" is _not_ always an error!:
1499 * "Where they went to he couldn't say."
1500 * Another false positive:
1501 * What would "Cinderella" be without the . . .
1502 * and another: "If he wants to he can see for himself."
1506 if (strstr(aline," to he "))
1507 s=strstr(aline," to he ");
1508 if (strstr(aline,"\" be "))
1509 s=strstr(aline,"\" be ");
1510 if (strstr(aline,"\", be "))
1511 s=strstr(aline,"\", be ");
1512 if (strstr(aline," is be "))
1513 s=strstr(aline," is be ");
1514 if (strstr(aline," be is "))
1515 s=strstr(aline," be is ");
1516 if (strstr(aline," was be "))
1517 s=strstr(aline," was be ");
1518 if (strstr(aline," be would "))
1519 s=strstr(aline," be would ");
1520 if (strstr(aline," be could "))
1521 s=strstr(aline," be could ");
1524 if (pswit[ECHO_SWITCH])
1525 printf("\n%s\n",aline);
1526 if (!pswit[OVERVIEW_SWITCH])
1527 printf(" Line %ld column %d - Query he/be error?\n",
1528 linecnt,(int)(s-aline)+1);
1534 if (strstr(aline," i bad "))
1535 s=strstr(aline," i bad ");
1536 if (strstr(aline," you bad "))
1537 s=strstr(aline," you bad ");
1538 if (strstr(aline," he bad "))
1539 s=strstr(aline," he bad ");
1540 if (strstr(aline," she bad "))
1541 s=strstr(aline," she bad ");
1542 if (strstr(aline," they bad "))
1543 s=strstr(aline," they bad ");
1544 if (strstr(aline," a had "))
1545 s=strstr(aline," a had ");
1546 if (strstr(aline," the had "))
1547 s=strstr(aline," the had ");
1550 if (pswit[ECHO_SWITCH])
1551 printf("\n%s\n",aline);
1552 if (!pswit[OVERVIEW_SWITCH])
1553 printf(" Line %ld column %d - Query had/bad error?\n",
1554 linecnt,(int)(s-aline)+1);
1560 if (strstr(aline,", hut "))
1561 s=strstr(aline,", hut ");
1562 if (strstr(aline,"; hut "))
1563 s=strstr(aline,"; hut ");
1566 if (pswit[ECHO_SWITCH])
1567 printf("\n%s\n",aline);
1568 if (!pswit[OVERVIEW_SWITCH])
1569 printf(" Line %ld column %d - Query hut/but error?\n",
1570 linecnt,(int)(s-aline)+1);
1575 * Special case - angled bracket in front of "From" placed there by an
1576 * MTA when sending an e-mail.
1578 if (strstr(aline,">From"))
1580 if (pswit[ECHO_SWITCH])
1581 printf("\n%s\n",aline);
1582 if (!pswit[OVERVIEW_SWITCH])
1583 printf(" Line %ld column %d - "
1584 "Query angled bracket with From\n",
1585 linecnt,(int)(strstr(aline,">From")-aline)+1);
1590 * Check for a single character line -
1591 * often an overflow from bad wrapping.
1593 if (*aline && !aline[1])
1595 if (*aline=='I' || *aline=='V' || *aline=='X' || *aline=='L' ||
1597 ; /* Nothing - ignore numerals alone on a line. */
1600 if (pswit[ECHO_SWITCH])
1601 printf("\n%s\n",aline);
1602 if (!pswit[OVERVIEW_SWITCH])
1603 printf(" Line %ld column 1 - "
1604 "Query single character line\n",linecnt);
1609 /* Check for I" - often should be ! */
1610 if (strstr(aline," I\""))
1612 if (pswit[ECHO_SWITCH])
1613 printf("\n%s\n",aline);
1614 if (!pswit[OVERVIEW_SWITCH])
1615 printf(" Line %ld column %ld - Query I=exclamation mark?\n",
1616 linecnt,strstr(aline," I\"")-aline);
1621 * Check for period without a capital letter. Cut-down from gutspell.
1622 * Only works when it happens on a single line.
1624 if (pswit[PARANOID_SWITCH])
1626 for (t=s=aline;strstr(t,". ");)
1632 /* start of line punctuation is handled elsewhere */
1635 if (!gcisalpha(t[-1]))
1640 if (warnings->isDutch)
1642 /* For Frank & Jeroen -- 's Middags case */
1643 if (t[2]==CHAR_SQUOTE && t[3]>='a' && t[3]<='z' &&
1644 t[4]==CHAR_SPACE && t[5]>='A' && t[5]<='Z')
1651 while (*s1 && !gcisalpha(*s1) && !isdigit(*s1))
1653 if (*s1>='a' && *s1<='z')
1655 /* we have something to investigate */
1657 /* so let's go back and find out */
1658 for (s1=t-1;s1>=s &&
1659 (gcisalpha(*s1) || gcisdigit(*s1) || *s1==CHAR_SQUOTE &&
1660 gcisalpha(s1[1]) && gcisalpha(s1[-1]));s1--)
1663 for (i=0;*s1 && *s1!='.';s1++,i++)
1666 for (i=0;*abbrev[i];i++)
1667 if (!strcmp(testword,abbrev[i]))
1669 if (gcisdigit(*testword))
1673 if (isroman(testword))
1678 for (i=0;testword[i];i++)
1679 if (strchr(vowels,testword[i]))
1685 if (strlen(testword)<MAX_QWORD_LENGTH &&
1686 !pswit[VERBOSE_SWITCH])
1687 for (i=0;i<qperiod_index;i++)
1688 if (!strcmp(testword,qperiod[i]))
1692 if (qperiod_index<MAX_QWORD &&
1693 strlen(testword)<MAX_QWORD_LENGTH)
1695 strcpy(qperiod[qperiod_index],testword);
1698 if (pswit[ECHO_SWITCH])
1699 printf("\n%s\n",aline);
1700 if (!pswit[OVERVIEW_SWITCH])
1701 printf(" Line %ld column %d - "
1702 "Extra period?\n",linecnt,(int)(t-aline)+1);
1711 if (pswit[TYPO_SWITCH])
1713 /* Check for words usually not followed by punctuation. */
1717 s=getaword(s,inword);
1721 for (i=0;*nocomma[i];i++)
1722 if (!strcmp(inword,nocomma[i]))
1724 if (*s==',' || *s==';' || *s==':')
1726 if (pswit[ECHO_SWITCH])
1727 printf("\n%s\n",aline);
1728 if (!pswit[OVERVIEW_SWITCH])
1729 printf(" Line %ld column %d - "
1730 "Query punctuation after %s?\n",
1731 linecnt,(int)(s-aline)+1,inword);
1736 for (i=0;*noperiod[i];i++)
1737 if (!strcmp(inword,noperiod[i]))
1739 if (*s=='.' || *s=='!')
1741 if (pswit[ECHO_SWITCH])
1742 printf("\n%s\n",aline);
1743 if (!pswit[OVERVIEW_SWITCH])
1744 printf(" Line %ld column %d - "
1745 "Query punctuation after %s?\n",
1746 linecnt,(int)(s-aline)+1,inword);
1754 * Check for commonly mistyped words,
1755 * and digits like 0 for O in a word.
1760 s=getaword(s,inword);
1762 continue; /* don't bother with empty lines */
1763 if (mixdigit(inword))
1765 if (pswit[ECHO_SWITCH])
1766 printf("\n%s\n",aline);
1767 if (!pswit[OVERVIEW_SWITCH])
1768 printf(" Line %ld column %d - Query digit in %s\n",
1769 linecnt,(int)(wordstart-aline)+1,inword);
1774 * Put the word through a series of tests for likely typos and OCR
1777 if (pswit[TYPO_SWITCH])
1780 strcpy(testword,inword);
1782 for (i=0;i<(signed int)strlen(testword);i++)
1784 /* lowercase for testing */
1785 if (testword[i]>='a' && testword[i]<='z')
1787 if (alower && testword[i]>='A' && testword[i]<='Z')
1790 * We have an uppercase mid-word. However, there are
1792 * Mac and Mc like McGill
1793 * French contractions like l'Abbe
1795 if (i==2 && testword[0]=='m' && testword[1]=='c' ||
1796 i==3 && testword[0]=='m' && testword[1]=='a' &&
1797 testword[2]=='c' || i>0 && testword[i-1]==CHAR_SQUOTE)
1802 testword[i]=(char)tolower(testword[i]);
1805 * Check for certain unlikely two-letter combinations at word
1808 if (strlen(testword)>1)
1810 for (i=0;*nostart[i];i++)
1811 if (!strncmp(testword,nostart[i],2))
1813 for (i=0;*noend[i];i++)
1814 if (!strncmp(testword+strlen(testword)-2,noend[i],2))
1817 /* ght is common, gbt never. Like that. */
1818 if (strstr(testword,"cb"))
1820 if (strstr(testword,"gbt"))
1822 if (strstr(testword,"pbt"))
1824 if (strstr(testword,"tbs"))
1826 if (strstr(testword,"mrn"))
1828 if (strstr(testword,"ahle"))
1830 if (strstr(testword,"ihle"))
1833 * "TBE" does happen - like HEARTBEAT - but uncommon.
1834 * Also "TBI" - frostbite, outbid - but uncommon.
1835 * Similarly "ii" like Hawaii, or Pompeii, and in Roman
1836 * numerals, but "ii" is a common scanno.
1838 if (strstr(testword,"tbi"))
1840 if (strstr(testword,"tbe"))
1842 if (strstr(testword,"ii"))
1845 * Check for no vowels or no consonants.
1846 * If none, flag a typo.
1848 if (!istypo && strlen(testword)>1)
1851 for (i=0;testword[i];i++)
1853 if (testword[i]=='y' || gcisdigit(testword[i]))
1855 /* Yah, this is loose. */
1859 else if (strchr(vowels,testword[i]))
1864 if (!vowel || !consonant)
1868 * Now exclude the word from being reported if it's in
1871 for (i=0;*okword[i];i++)
1872 if (!strcmp(testword,okword[i]))
1875 * What looks like a typo may be a Roman numeral.
1878 if (istypo && isroman(testword))
1880 /* Check the manual list of typos. */
1882 for (i=0;*typo[i];i++)
1883 if (!strcmp(testword,typo[i]))
1886 * Check lowercase s, l, i and m - special cases.
1887 * "j" - often a semi-colon gone wrong.
1888 * "d" for a missing apostrophe - he d
1891 if (!istypo && strlen(testword)==1 && strchr("slmijdn",*inword))
1896 if (strlen(testword)<MAX_QWORD_LENGTH &&
1897 !pswit[VERBOSE_SWITCH])
1898 for (i=0;i<qword_index;i++)
1899 if (!strcmp(testword,qword[i]))
1906 if (qword_index<MAX_QWORD &&
1907 strlen(testword)<MAX_QWORD_LENGTH)
1909 strcpy(qword[qword_index],testword);
1912 if (pswit[ECHO_SWITCH])
1913 printf("\n%s\n",aline);
1914 if (!pswit[OVERVIEW_SWITCH])
1916 printf(" Line %ld column %d - Query word %s",
1917 linecnt,(int)(wordstart-aline)+1,inword);
1918 if (strlen(testword)<MAX_QWORD_LENGTH &&
1919 !pswit[VERBOSE_SWITCH])
1920 printf(" - not reporting duplicates");
1928 /* check the user's list of typos */
1929 if (!istypo && usertypo_count)
1930 for (i=0;i<usertypo_count;i++)
1931 if (!strcmp(testword,usertypo[i]))
1933 if (pswit[ECHO_SWITCH])
1934 printf("\n%s\n",aline);
1935 if (!pswit[OVERVIEW_SWITCH])
1936 printf(" Line %ld column %d - "
1937 "Query possible scanno %s\n",
1938 linecnt,(int)(wordstart-aline)+2,inword);
1940 if (pswit[PARANOID_SWITCH] && warnings->digit)
1942 /* In paranoid mode, query all 0 and 1 standing alone. */
1943 if (!strcmp(inword,"0") || !strcmp(inword,"1"))
1945 if (pswit[ECHO_SWITCH])
1946 printf("\n%s\n",aline);
1947 if (!pswit[OVERVIEW_SWITCH])
1948 printf(" Line %ld column %d - Query standalone %s\n",
1949 linecnt,(int)(wordstart-aline)+2,inword);
1956 * Look for added or missing spaces around punctuation and quotes.
1957 * If there is a punctuation character like ! with no space on
1958 * either side, suspect a missing!space. If there are spaces on
1959 * both sides , assume a typo. If we see a double quote with no
1960 * space or punctuation on either side of it, assume unspaced
1961 * quotes "like"this.
1964 for (i=1;i<llen;i++)
1966 /* For each character in the line after the first. */
1967 if (strchr(".?!,;:_",aline[i])) /* if it's punctuation */
1969 /* we need to suppress warnings for acronyms like M.D. */
1971 /* we need to suppress warnings for ellipsis . . . */
1973 /* if there are letters on both sides of it or ... */
1974 if (gcisalpha(aline[i-1]) && gcisalpha(aline[i+1]) ||
1975 gcisalpha(aline[i+1]) && strchr("?!,;:",aline[i]))
1977 /* ...if it's strict punctuation followed by an alpha */
1980 if (i>2 && aline[i-2]=='.')
1982 if (i+2<llen && aline[i+2]=='.')
1987 if (pswit[ECHO_SWITCH])
1988 printf("\n%s\n",aline);
1989 if (!pswit[OVERVIEW_SWITCH])
1990 printf(" Line %ld column %d - Missing space?\n",
1996 if (aline[i-1]==CHAR_SPACE &&
1997 (aline[i+1]==CHAR_SPACE || aline[i+1]==0))
2000 * If there are spaces on both sides,
2001 * or space before and end of line.
2005 if (i>2 && aline[i-2]=='.')
2007 if (i+2<llen && aline[i+2]=='.')
2010 if (!isemptyline && !isellipsis)
2012 if (pswit[ECHO_SWITCH])
2013 printf("\n%s\n",aline);
2014 if (!pswit[OVERVIEW_SWITCH])
2015 printf(" Line %ld column %d - "
2016 "Spaced punctuation?\n",linecnt,i+1);
2023 /* Split out the characters that CANNOT be preceded by space. */
2025 for (i=1;i<llen;i++)
2027 /* for each character in the line after the first */
2028 if (strchr("?!,;:",aline[i]))
2030 /* if it's punctuation that _cannot_ have a space before it */
2031 if (aline[i-1]==CHAR_SPACE && !isemptyline &&
2032 aline[i+1]!=CHAR_SPACE)
2035 * If aline[i+1) DOES == space,
2036 * it was already reported just above.
2038 if (pswit[ECHO_SWITCH])
2039 printf("\n%s\n",aline);
2040 if (!pswit[OVERVIEW_SWITCH])
2041 printf(" Line %ld column %d - Spaced punctuation?\n",
2049 * Special case " .X" where X is any alpha.
2050 * This plugs a hole in the acronym code above.
2051 * Inelegant, but maintainable.
2054 for (i=1;i<llen;i++)
2056 /* for each character in the line after the first */
2059 /* if it's a period */
2060 if (aline[i-1]==CHAR_SPACE && gcisalpha(aline[i+1]))
2063 * If the period follows a space and
2064 * is followed by a letter.
2066 if (pswit[ECHO_SWITCH])
2067 printf("\n%s\n",aline);
2068 if (!pswit[OVERVIEW_SWITCH])
2069 printf(" Line %ld column %d - Spaced punctuation?\n",
2076 for (i=1;i<llen;i++)
2078 /* for each character in the line after the first */
2079 if (aline[i]==CHAR_DQUOTE)
2081 if (!strchr(" _-.'`,;:!/([{?}])",aline[i-1]) &&
2082 !strchr(" _-.'`,;:!/([{?}])",aline[i+1]) && aline[i+1] ||
2083 !strchr(" _-([{'`",aline[i-1]) && gcisalpha(aline[i+1]))
2085 if (pswit[ECHO_SWITCH])
2086 printf("\n%s\n",aline);
2087 if (!pswit[OVERVIEW_SWITCH])
2088 printf(" Line %ld column %d - Unspaced quotes?\n",
2095 /* Check parity of quotes. */
2096 for (s=aline;*s;s++)
2098 if (*s==CHAR_DQUOTE)
2100 if (!(dquotepar=!dquotepar))
2103 if (!strchr("_-.'`/,;:!?)]} ",s[1]))
2105 if (pswit[ECHO_SWITCH])
2106 printf("\n%s\n",aline);
2107 if (!pswit[OVERVIEW_SWITCH])
2108 printf(" Line %ld column %d - "
2109 "Wrongspaced quotes?\n",linecnt,(int)(s-aline)+1);
2117 if (!gcisalpha(s[1]) && !isdigit(s[1]) &&
2118 !strchr("_-/.'`([{$",s[1]) || !s[1])
2120 if (pswit[ECHO_SWITCH])
2121 printf("\n%s\n",aline);
2122 if (!pswit[OVERVIEW_SWITCH])
2123 printf(" Line %ld column %d - "
2124 "Wrongspaced quotes?\n",linecnt,(int)(s-aline)+1);
2131 if (*aline==CHAR_DQUOTE)
2133 if (strchr(",;:!?)]} ",aline[1]))
2135 if (pswit[ECHO_SWITCH])
2136 printf("\n%s\n",aline);
2137 if (!pswit[OVERVIEW_SWITCH])
2138 printf(" Line %ld column 1 - Wrongspaced quotes?\n",
2144 if (pswit[SQUOTE_SWITCH])
2146 for (s=aline;*s;s++)
2148 if ((*s==CHAR_SQUOTE || *s==CHAR_OPEN_SQUOTE) &&
2149 (s==aline || s>aline && !gcisalpha(s[-1]) ||
2152 if (!(squotepar=!squotepar))
2155 if (!strchr("_-.'`/\",;:!?)]} ",s[1]))
2157 if (pswit[ECHO_SWITCH])
2158 printf("\n%s\n",aline);
2159 if (!pswit[OVERVIEW_SWITCH])
2160 printf(" Line %ld column %d - "
2161 "Wrongspaced singlequotes?\n",
2162 linecnt,(int)(s-aline)+1);
2170 if (!gcisalpha(s[1]) && !isdigit(s[1]) &&
2171 !strchr("_-/\".'`",s[1]) || !s[1])
2173 if (pswit[ECHO_SWITCH])
2174 printf("\n%s\n",aline);
2175 if (!pswit[OVERVIEW_SWITCH])
2176 printf(" Line %ld column %d - "
2177 "Wrongspaced singlequotes?\n",
2178 linecnt,(int)(s-aline)+1);
2187 * Look for double punctuation like ,. or ,,
2188 * Thanks to DW for the suggestion!
2189 * In books with references, ".," and ".;" are common
2190 * e.g. "etc., etc.," and vol. 1.; vol 3.;
2191 * OTOH, from my initial tests, there are also fairly
2192 * common errors. What to do? Make these cases paranoid?
2193 * ".," is the most common, so warnings->dotcomma is used
2194 * to suppress detailed reporting if it occurs often.
2197 for (i=0;i<llen;i++)
2199 /* for each punctuation character in the line */
2200 if (strchr(".?!,;:",aline[i]) && (strchr(".?!,;:",aline[i+1])) &&
2201 aline[i] && aline[i+1])
2203 /* followed by punctuation, it's a query, unless . . . */
2204 if (aline[i]==aline[i+1] && (aline[i]=='.' || aline[i]=='?' ||
2206 !warnings->dotcomma && aline[i]=='.' && aline[i+1]==',' ||
2207 warnings->isFrench && !strncmp(aline+i,",...",4) ||
2208 warnings->isFrench && !strncmp(aline+i,"...,",4) ||
2209 warnings->isFrench && !strncmp(aline+i,";...",4) ||
2210 warnings->isFrench && !strncmp(aline+i,"...;",4) ||
2211 warnings->isFrench && !strncmp(aline+i,":...",4) ||
2212 warnings->isFrench && !strncmp(aline+i,"...:",4) ||
2213 warnings->isFrench && !strncmp(aline+i,"!...",4) ||
2214 warnings->isFrench && !strncmp(aline+i,"...!",4) ||
2215 warnings->isFrench && !strncmp(aline+i,"?...",4) ||
2216 warnings->isFrench && !strncmp(aline+i,"...?",4))
2218 if (warnings->isFrench && !strncmp(aline+i,",...",4) ||
2219 warnings->isFrench && !strncmp(aline+i,"...,",4) ||
2220 warnings->isFrench && !strncmp(aline+i,";...",4) ||
2221 warnings->isFrench && !strncmp(aline+i,"...;",4) ||
2222 warnings->isFrench && !strncmp(aline+i,":...",4) ||
2223 warnings->isFrench && !strncmp(aline+i,"...:",4) ||
2224 warnings->isFrench && !strncmp(aline+i,"!...",4) ||
2225 warnings->isFrench && !strncmp(aline+i,"...!",4) ||
2226 warnings->isFrench && !strncmp(aline+i,"?...",4) ||
2227 warnings->isFrench && !strncmp(aline+i,"...?",4))
2229 ; /* do nothing for .. !! and ?? which can be legit */
2233 if (pswit[ECHO_SWITCH])
2234 printf("\n%s\n",aline);
2235 if (!pswit[OVERVIEW_SWITCH])
2236 printf(" Line %ld column %d - Double punctuation?\n",
2244 while (strstr(s," \" "))
2246 if (pswit[ECHO_SWITCH])
2247 printf("\n%s\n",aline);
2248 if (!pswit[OVERVIEW_SWITCH])
2249 printf(" Line %ld column %d - Spaced doublequote?\n",
2250 linecnt,(int)(strstr(s," \" ")-aline+1));
2253 s=strstr(s," \" ")+2;
2256 while (strstr(s," ' "))
2258 if (pswit[ECHO_SWITCH])
2259 printf("\n%s\n",aline);
2260 if (!pswit[OVERVIEW_SWITCH])
2261 printf(" Line %ld column %d - Spaced singlequote?\n",
2262 linecnt,(int)(strstr(s," ' ")-aline+1));
2265 s=strstr(s," ' ")+2;
2268 while (strstr(s," ` "))
2270 if (pswit[ECHO_SWITCH])
2271 printf("\n%s\n",aline);
2272 if (!pswit[OVERVIEW_SWITCH])
2273 printf(" Line %ld column %d - Spaced singlequote?\n",
2274 linecnt,(int)(strstr(s," ` ")-aline+1));
2277 s=strstr(s," ` ")+2;
2279 /* check special case of 'S instead of 's at end of word */
2283 if (*s==CHAR_SQUOTE && s[1]=='S' && s[-1]>='a' && s[-1]<='z')
2285 if (pswit[ECHO_SWITCH])
2286 printf("\n%s\n",aline);
2287 if (!pswit[OVERVIEW_SWITCH])
2288 printf(" Line %ld column %d - Capital \"S\"?\n",
2289 linecnt,(int)(s-aline+2));
2296 * Now check special cases - start and end of line -
2297 * for single and double quotes. Start is sometimes [sic]
2298 * but better to query it anyway.
2299 * While we're here, check for dash at end of line.
2304 if (aline[llen-1]==CHAR_DQUOTE || aline[llen-1]==CHAR_SQUOTE ||
2305 aline[llen-1]==CHAR_OPEN_SQUOTE)
2306 if (aline[llen-2]==CHAR_SPACE)
2308 if (pswit[ECHO_SWITCH])
2309 printf("\n%s\n",aline);
2310 if (!pswit[OVERVIEW_SWITCH])
2311 printf(" Line %ld column %d - Spaced quote?\n",
2316 if ((aline[0]==CHAR_SQUOTE || aline[0]==CHAR_OPEN_SQUOTE) &&
2317 aline[1]==CHAR_SPACE)
2319 if (pswit[ECHO_SWITCH])
2320 printf("\n%s\n",aline);
2321 if (!pswit[OVERVIEW_SWITCH])
2322 printf(" Line %ld column 1 - Spaced quote?\n",linecnt);
2327 * Dash at end of line may well be legit - paranoid mode only
2328 * and don't report em-dash at line-end.
2330 if (pswit[PARANOID_SWITCH] && warnings->hyphen)
2332 for (i=llen-1;i>0 && (unsigned char)aline[i]<=CHAR_SPACE;i--)
2334 if (aline[i]=='-' && aline[i-1]!='-')
2336 if (pswit[ECHO_SWITCH])
2337 printf("\n%s\n",aline);
2338 if (!pswit[OVERVIEW_SWITCH])
2339 printf(" Line %ld column %d - "
2340 "Hyphen at end of line?\n",linecnt,i);
2345 * Brackets are often unspaced, but shouldn't be surrounded by alpha.
2346 * If so, suspect a scanno like "a]most".
2349 for (i=1;i<llen-1;i++)
2351 /* for each bracket character in the line except 1st & last */
2352 if (strchr("{[()]}",aline[i]) && gcisalpha(aline[i-1]) &&
2353 gcisalpha(aline[i+1]))
2355 if (pswit[ECHO_SWITCH])
2356 printf("\n%s\n",aline);
2357 if (!pswit[OVERVIEW_SWITCH])
2358 printf(" Line %ld column %d - Unspaced bracket?\n",
2365 if (warnings->endquote)
2367 for (i=1;i<llen;i++)
2369 /* for each character in the line except 1st */
2370 if (aline[i]==CHAR_DQUOTE && isalpha(aline[i-1]))
2372 if (pswit[ECHO_SWITCH])
2373 printf("\n%s\n",aline);
2374 if (!pswit[OVERVIEW_SWITCH])
2375 printf(" Line %ld column %d - "
2376 "endquote missing punctuation?\n",linecnt,i);
2383 * Check for <HTML TAG>.
2384 * If there is a < in the line, followed at some point
2385 * by a > then we suspect HTML.
2387 if (strstr(aline,"<") && strstr(aline,">"))
2389 i=(signed int)(strstr(aline,">")-strstr(aline,"<")+1);
2392 strncpy(wrk,strstr(aline,"<"),i);
2394 if (pswit[ECHO_SWITCH])
2395 printf("\n%s\n",aline);
2396 if (!pswit[OVERVIEW_SWITCH])
2397 printf(" Line %ld column %d - HTML Tag? %s \n",
2398 linecnt,(int)(strstr(aline,"<")-aline)+1,wrk);
2404 * Check for &symbol; HTML.
2405 * If there is a & in the line, followed at
2406 * some point by a ; then we suspect HTML.
2408 if (strstr(aline,"&") && strstr(aline,";"))
2410 i=(int)(strstr(aline,";")-strstr(aline,"&")+1);
2411 for (s=strstr(aline,"&");s<strstr(aline,";");s++)
2413 i=0; /* Don't report "Jones & Son;" */
2416 strncpy(wrk,strstr(aline,"&"),i);
2418 if (pswit[ECHO_SWITCH])
2419 printf("\n%s\n",aline);
2420 if (!pswit[OVERVIEW_SWITCH])
2421 printf(" Line %ld column %d - HTML symbol? %s \n",
2422 linecnt,(int)(strstr(aline,"&")-aline)+1,wrk);
2428 * At end of paragraph, check for mismatched quotes.
2429 * We don't want to report an error immediately, since it is a
2430 * common convention to omit the quotes at end of paragraph if
2431 * the next paragraph is a continuation of the same speaker.
2432 * Where this is the case, the next para should begin with a
2433 * quote, so we store the warning message and only display it
2434 * at the top of the next iteration if the new para doesn't
2435 * start with a quote.
2436 * The -p switch overrides this default, and warns of unclosed
2437 * quotes on _every_ paragraph, whether the next begins with a
2442 /* end of para - add up the totals */
2443 if (counters.quot%2)
2444 sprintf(dquote_err," Line %ld - Mismatched quotes\n",
2446 if (pswit[SQUOTE_SWITCH] && counters.open_single_quote &&
2447 counters.open_single_quote!=counters.close_single_quote)
2448 sprintf(squote_err," Line %ld - Mismatched singlequotes?\n",
2450 if (pswit[SQUOTE_SWITCH] && counters.open_single_quote &&
2451 counters.open_single_quote!=counters.close_single_quote &&
2452 counters.open_single_quote!=counters.close_single_quote+1)
2454 * Flag it to be noted regardless of the
2455 * first char of the next para.
2458 if (counters.r_brack)
2459 sprintf(rbrack_err," Line %ld - "
2460 "Mismatched round brackets?\n",linecnt);
2461 if (counters.s_brack)
2462 sprintf(sbrack_err," Line %ld - "
2463 "Mismatched square brackets?\n",linecnt);
2464 if (counters.c_brack)
2465 sprintf(cbrack_err," Line %ld - "
2466 "Mismatched curly brackets?\n",linecnt);
2467 if (counters.c_unders%2)
2468 sprintf(unders_err," Line %ld - Mismatched underscores?\n",
2470 memset(&counters,0,sizeof(counters));
2471 /* let the next iteration know that it's starting a new para */
2475 * Check for omitted punctuation at end of paragraph by working back
2476 * through prevline. DW.
2477 * Need to check this only for "normal" paras.
2478 * So what is a "normal" para?
2479 * Not normal if one-liner (chapter headings, etc.)
2480 * Not normal if doesn't contain at least one locase letter
2481 * Not normal if starts with space
2486 for (s=prevline,i=0;*s && !i;s++)
2488 /* use i to indicate the presence of a letter on the line */
2491 * This next "if" is a problem.
2492 * If we say "start_para_line <= linecnt - 1", that includes
2493 * one-line "paragraphs" like chapter heads. Lotsa false positives.
2494 * If we say "start_para_line < linecnt - 1" it doesn't, but then it
2495 * misses genuine one-line paragraphs.
2497 if (i && last.blen>2 && start_para_line<linecnt-1 &&
2498 *prevline>CHAR_SPACE)
2500 for (i=strlen(prevline)-1;
2501 (prevline[i]==CHAR_DQUOTE || prevline[i]==CHAR_SQUOTE) &&
2502 prevline[i]>CHAR_SPACE && i>0;
2507 if (gcisalpha(prevline[i]))
2509 if (pswit[ECHO_SWITCH])
2510 printf("\n%s\n",prevline);
2511 if (!pswit[OVERVIEW_SWITCH])
2512 printf(" Line %ld column %d - "
2513 "No punctuation at para end?\n",
2514 linecnt-1,strlen(prevline));
2519 if (strchr("-.:!([{?}])",prevline[i]))
2524 strcpy(prevline,aline);
2527 if (!pswit[OVERVIEW_SWITCH])
2528 for (i=0;i<MAX_QWORD;i++)
2530 printf("\nNote: Queried word %s was duplicated %d time%s\n",
2531 qword[i],dupcnt[i],"s");
2537 * Get one line from the input stream, checking for
2538 * the existence of exactly one CR/LF line-end per line.
2540 * Returns: a pointer to the line.
2542 char *flgets(char *theline,int maxlen,FILE *thefile,long lcnt)
2548 c=cint=fgetc(thefile);
2553 /* either way, it's end of line */
2560 /* Error - a LF without a preceding CR */
2561 if (pswit[LINE_END_SWITCH])
2563 if (pswit[ECHO_SWITCH])
2564 printf("\n%s\n",theline);
2565 if (!pswit[OVERVIEW_SWITCH])
2566 printf(" Line %ld - No CR?\n",lcnt);
2577 /* Error - two successive CRs */
2578 if (pswit[LINE_END_SWITCH])
2580 if (pswit[ECHO_SWITCH])
2581 printf("\n%s\n",theline);
2582 if (!pswit[OVERVIEW_SWITCH])
2583 printf(" Line %ld - Two successive CRs?\n",lcnt);
2592 if (pswit[LINE_END_SWITCH] && isCR)
2594 if (pswit[ECHO_SWITCH])
2595 printf("\n%s\n",theline);
2596 if (!pswit[OVERVIEW_SWITCH])
2597 printf(" Line %ld column %d - CR without LF?\n",
2607 c=cint=fgetc(thefile);
2608 } while(len<maxlen);
2609 if (pswit[MARKUP_SWITCH])
2610 postprocess_for_HTML(theline);
2611 if (pswit[DP_SWITCH])
2612 postprocess_for_DP(theline);
2619 * Takes a "word" as a parameter, and checks whether it
2620 * contains a mixture of alpha and digits. Generally, this is an
2621 * error, but may not be for cases like 4th or L5 12s. 3d.
2623 * Returns: 0 if no error found, 1 if error.
2625 int mixdigit(char *checkword)
2627 int wehaveadigit,wehavealetter,firstdigits,query,wl;
2629 wehaveadigit=wehavealetter=query=0;
2630 for (s=checkword;*s;s++)
2636 if (wehaveadigit && wehavealetter)
2638 /* Now exclude common legit cases, like "21st" and "12l. 3s. 11d." */
2640 wl=strlen(checkword);
2641 for (firstdigits=0;gcisdigit(checkword[firstdigits]);firstdigits++)
2643 /* digits, ending in st, rd, nd, th of either case */
2644 if (firstdigits+2==wl && (matchword(checkword+wl-2,"st") ||
2645 matchword(checkword+wl-2,"rd") || matchword(checkword+wl-2,"nd") ||
2646 matchword(checkword+wl-2,"th")))
2648 if (firstdigits+3==wl && (matchword(checkword+wl-3,"sts") ||
2649 matchword(checkword+wl-3,"rds") || matchword(checkword+wl-3,"nds") ||
2650 matchword(checkword+wl-3,"ths")))
2652 if (firstdigits+3==wl && (matchword(checkword+wl-4,"stly") ||
2653 matchword(checkword+wl-4,"rdly") ||
2654 matchword(checkword+wl-4,"ndly") || matchword(checkword+wl-4,"thly")))
2656 /* digits, ending in l, L, s or d */
2657 if (firstdigits+1==wl && (checkword[wl-1]=='l' ||
2658 checkword[wl-1]=='L' || checkword[wl-1]=='s' || checkword[wl-1]=='d'))
2661 * L at the start of a number, representing Britsh pounds, like L500.
2662 * This is cute. We know the current word is mixeddigit. If the first
2663 * letter is L, there must be at least one digit following. If both
2664 * digits and letters follow, we have a genuine error, else we have a
2665 * capital L followed by digits, and we accept that as a non-error.
2667 if (checkword[0]=='L' && !mixdigit(checkword+1))
2676 * Extracts the first/next "word" from the line, and puts
2677 * it into "thisword". A word is defined as one English word unit--or
2678 * at least that's the aim.
2680 * Returns: a pointer to the position in the line where we will start
2681 * looking for the next word.
2683 char *getaword(char *fromline,char *thisword)
2688 for (;!gcisdigit(*fromline) && !gcisalpha(*fromline) && *fromline;
2692 * Use a look-ahead to handle exceptions for numbers like 1,000 and 1.35.
2693 * Especially yucky is the case of L1,000
2694 * This section looks for a pattern of characters including a digit
2695 * followed by a comma or period followed by one or more digits.
2696 * If found, it returns this whole pattern as a word; otherwise we discard
2697 * the results and resume our normal programming.
2700 for (;(gcisdigit(*s) || gcisalpha(*s) || *s==',' || *s=='.') &&
2701 wordlen<MAXWORDLEN;s++)
2703 thisword[wordlen]=*s;
2706 thisword[wordlen]=0;
2707 for (i=1;i<wordlen-1;i++)
2709 if (thisword[i]=='.' || thisword[i]==',')
2711 if (gcisdigit(thisword[i-1]) && gcisdigit(thisword[i-1]))
2718 /* we didn't find a punctuated number - do the regular getword thing */
2720 for (;(gcisdigit(*fromline) || gcisalpha(*fromline) || *fromline=='\'') &&
2721 wordlen<MAXWORDLEN;fromline++)
2723 thisword[wordlen]=*fromline;
2726 thisword[wordlen]=0;
2733 * A case-insensitive string matcher.
2735 int matchword(char *checkfor,char *thisword)
2737 unsigned int ismatch,i;
2738 if (strlen(checkfor)!=strlen(thisword))
2740 ismatch=1; /* assume a match until we find a difference */
2741 for (i=0;i<strlen(checkfor);i++)
2742 if (toupper(checkfor[i])!=toupper(thisword[i]))
2750 * Lowercase the line.
2753 void lowerit(char *theline)
2755 for (;*theline;theline++)
2756 if (*theline>='A' && *theline<='Z')
2763 * Is this word a Roman Numeral?
2765 * It doesn't actually validate that the number is a valid Roman Numeral--for
2766 * example it will pass MXXXXXXXXXX as a valid Roman Numeral, but that's not
2767 * what we're here to do. If it passes this, it LOOKS like a Roman numeral.
2768 * Anyway, the actual Romans were pretty tolerant of bad arithmetic, or
2769 * expressions thereof, except when it came to taxes. Allow any number of M,
2770 * an optional D, an optional CM or CD, any number of optional Cs, an optional
2771 * XL or an optional XC, an optional IX or IV, an optional V and any number
2774 int isroman(char *t)
2780 while (*t=='m' && *t)
2784 if (*t=='c' && t[1]=='m')
2786 if (*t=='c' && t[1]=='d')
2788 while (*t=='c' && *t)
2790 if (*t=='x' && t[1]=='l')
2792 if (*t=='x' && t[1]=='c')
2796 while (*t=='x' && *t)
2798 if (*t=='i' && t[1]=='x')
2800 if (*t=='i' && t[1]=='v')
2804 while (*t=='i' && *t)
2812 * A version of isalpha() that is somewhat lenient on 8-bit texts.
2813 * If we use the standard function, 8-bit accented characters break
2814 * words, so that tete with accented characters appears to be two words, "t"
2815 * and "t", with 8-bit characters between them. This causes over-reporting of
2816 * errors. gcisalpha() recognizes accented letters from the CP1252 (Windows)
2817 * and ISO-8859-1 character sets, which are the most common PG 8-bit types.
2819 int gcisalpha(unsigned char c)
2821 if (c>='a' && c<='z')
2823 if (c>='A' && c<='Z')
2827 if (c>=192 && c!=208 && c!=215 && c!=222 && c!=240 && c!=247 && c!=254)
2829 if (c==140 || c==142 || c==156 || c==158 || c==159)
2837 * A version of isdigit() that doesn't get confused in 8-bit texts.
2839 int gcisdigit(unsigned char c)
2841 return c>='0' && c<='9';
2847 * A version of isletter() that doesn't get confused in 8-bit texts.
2848 * NB: this is ISO-8891-1-specific.
2850 int gcisletter(unsigned char c)
2852 return c>='A' && c<='Z' || c>='a' && c<='z' || c>=192;
2858 * Wraps strchr to return NULL if the character being searched for is zero.
2860 char *gcstrchr(char *s,char c)
2868 * postprocess_for_DP:
2870 * Invoked with the -d switch from flgets().
2871 * It simply "removes" from the line a hard-coded set of common
2872 * DP-specific tags, so that the line passed to the main routine has
2873 * been pre-cleaned of DP markup.
2875 void postprocess_for_DP(char *theline)
2881 for (i=0;*DPmarkup[i];i++)
2883 s=strstr(theline,DPmarkup[i]);
2886 t=s+strlen(DPmarkup[i]);
2894 s=strstr(theline,DPmarkup[i]);
2900 * postprocess_for_HTML:
2902 * Invoked with the -m switch from flgets().
2903 * It simply "removes" from the line a hard-coded set of common
2904 * HTML tags and "replaces" a hard-coded set of common HTML
2905 * entities, so that the line passed to the main routine has
2906 * been pre-cleaned of HTML.
2908 void postprocess_for_HTML(char *theline)
2910 if (strstr(theline,"<") && strstr(theline,">"))
2911 while (losemarkup(theline))
2913 while (loseentities(theline))
2917 char *losemarkup(char *theline)
2923 s=strstr(theline,"<");
2924 t=strstr(theline,">");
2927 for (i=0;*markup[i];i++)
2928 if (!tagcomp(s+1,markup[i]))
2941 /* It's an unrecognized <xxx>. */
2945 char *loseentities(char *theline)
2951 for (i=0;*entities[i].htmlent;i++)
2953 s=strstr(theline,entities[i].htmlent);
2956 t=malloc((size_t)strlen(s));
2959 strcpy(t,s+strlen(entities[i].htmlent));
2960 strcpy(s,entities[i].textent);
2966 for (i=0;*entities[i].htmlnum;i++)
2968 s=strstr(theline,entities[i].htmlnum);
2971 t=malloc((size_t)strlen(s));
2974 strcpy(t,s+strlen(entities[i].htmlnum));
2975 strcpy(s,entities[i].textent);
2984 int tagcomp(char *strin,char *basetag)
2990 t++; /* ignore a slash */
2993 if (tolower(*s)!=tolower(*t))
3003 fputs("Bookloupe version " PACKAGE_VERSION ".\n",stderr);
3004 fputs("Copyright 2000-2005 Jim Tinsley <jtinsley@pobox.com>.\n",stderr);
3005 fputs("Copyright 2012- J. Ali Harlow <ali@juiblex.co.uk>.\n",stderr);
3006 fputs("Bookloupe comes wih ABSOLUTELY NO WARRANTY. "
3007 "For details, read the file COPYING.\n",stderr);
3008 fputs("This is Free Software; "
3009 "you may redistribute it under certain conditions (GPL);\n",stderr);
3010 fputs("read the file COPYING for details.\n\n",stderr);
3011 fputs("Usage is: bookloupe [-setpxloyhud] filename\n",stderr);
3012 fputs(" where -s checks single quotes, -e suppresses echoing lines, "
3013 "-t checks typos\n",stderr);
3014 fputs(" -x (paranoid) switches OFF -t and extra checks, "
3015 "-l turns OFF line-end checks\n",stderr);
3016 fputs(" -o just displays overview without detail, "
3017 "-h echoes header fields\n",stderr);
3018 fputs(" -v (verbose) unsuppresses duplicate reporting, "
3019 "-m suppresses markup\n",stderr);
3020 fputs(" -d ignores DP-specific markup,\n",stderr);
3021 fputs(" -u uses a file gutcheck.typ to query user-defined "
3022 "possible typos\n",stderr);
3023 fputs("Sample usage: bookloupe warpeace.txt \n",stderr);
3025 fputs("Bookloupe looks for errors in Project Gutenberg(TM) etexts.\n",
3027 fputs("Bookloupe queries anything it thinks shouldn't be in a PG text; "
3028 "non-ASCII\n",stderr);
3029 fputs("characters like accented letters, "
3030 "lines longer than 75 or shorter than 55,\n",stderr);
3031 fputs("unbalanced quotes or brackets, "
3032 "a variety of badly formatted punctuation, \n",stderr);
3033 fputs("HTML tags, some likely typos. "
3034 "It is NOT a substitute for human judgement.\n",stderr);