[go: up one dir, main page]

File: diction.c

package info (click to toggle)
diction 1.02-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 440 kB
  • ctags: 246
  • sloc: sh: 2,495; ansic: 2,173; makefile: 95
file content (417 lines) | stat: -rw-r--r-- 12,000 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* Notes */ /*{{{C}}}*//*{{{*/
/*

This program is GNU software, copyright 1997, 1998, 1999, 2000, 2001
Michael Haardt <michael@moria.de>.

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program.  If not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/
/*}}}*/
/* #includes */ /*{{{*/
#undef  _POSIX_SOURCE
#define _POSIX_SOURCE   1
#undef  _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 2

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <locale.h>
#ifdef HAVE_NL_TYPES_H
#include <nl_types.h>
#else
typedef long nl_catd;
static nl_catd catopen(const char *name, int oflag) { return 0; }
static const char *catgets(nl_catd catd, int set_id, int msg_id, const char *msg) { return msg; }
static void catclose(nl_catd catd) { }
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "getopt.h"
#include "config.h"

#include "misc.h"
#include "sentence.h"
/*}}}*/
/* #defines */ /*{{{*/
#define USAGE1     catgets(catd,1, 1,"Usage: diction [-f file [-n|-L language]] [file ...]\n")
#define USAGE2     catgets(catd,1, 2,"       diction [--file [--no-default-file|--language]] [file ...]\n")
#define USAGE3     catgets(catd,1, 3,"       diction --version\n")
#define MORE       catgets(catd,1, 4,"Try diction -h or diction --help for more information.\n")
#define HELP1      catgets(catd,1, 5,"Print wordy and commonly misused phrases in sentences.\n")
#define HELP2      catgets(catd,1, 6,"-f, --file             also read the specified database\n")
#define HELP3      catgets(catd,1, 7,"-n, --no-default-file  do not read the default phrase file\n")
#define HELP4      catgets(catd,1, 8,"-L, --language         set document language\n")
#define HELP5      catgets(catd,1, 9,"-h, --help             print this message\n")
#define HELP6      catgets(catd,1,10,"    --version          print the version\n")
#define BUGS       catgets(catd,1,11,"Report bugs to <michael@moria.de>.\n")
#define OPENFAILED catgets(catd,1,12,"diction: can not open %s: %s\n")
#define NOMEM      catgets(catd,1,13,"diction: out of memory.\n")
#define DOUBLE     catgets(catd,1,14,"Double word.")
#define NOSENT     catgets(catd,1,15,"No sentences found.\n")
#define NOPHRASES  catgets(catd,1,16,"No phrases ")
#define ONEPHRASE  catgets(catd,1,17,"1 phrase ")
#define NPHRASES   catgets(catd,1,18,"%d phrases ")
#define ONESENT    catgets(catd,1,19,"in 1 sentence found.\n")
#define NSENT      catgets(catd,1,20,"in %d sentences found.\n")
/*}}}*/

/* types */ /*{{{*/
struct word
{
  char *word;
  size_t size;
};

struct badPhrase
{
  char *phrase;
  char *suggest;
  int next; /* index of next entry with different first letter */
};
/*}}}*/

static struct badPhrase *badPhrases=(struct badPhrase *)0;
static int badPhraseCapacity=0;
static int badPhraseSize=0;
static struct word *wordQueue;
static int wordQueueLength;
static int wordQueueStart;
static int sentences,hits;
static nl_catd catd;
static int phraseStart[256];

static int phrasecmp(const void *a, const void *b) /*{{{*/
{
  const struct badPhrase *aa=a,*bb=b;

  return strcmp(bb->phrase,aa->phrase);
}
/*}}}*/
static void loadPhrases(const char *file) /*{{{*/
{
  FILE *fp;
  char ln[1024];
  const char *tab;
  size_t l;
  int fix,j;

  if ((fp=fopen(file,"r"))==(FILE*)0)
  {
    fprintf(stderr,OPENFAILED,file,strerror(errno));
    exit(1);
  }
  while (fgets(ln,sizeof(ln),fp))
  {
    char c;

    c=(ln[0]==' '?ln[1]:ln[0]);
    if (c && c!='\n')
    {
      phraseStart[(unsigned int)((unsigned char)c)]=1;
    }
    if (badPhraseSize==badPhraseCapacity) /* enlarge capacity */ /*{{{*/
    {
      if ((badPhrases=realloc(badPhrases,(badPhraseCapacity=3*(badPhraseCapacity+32))*sizeof(struct badPhrase)))==(struct badPhrase*)0)
      {
        fprintf(stderr,NOMEM);
        exit(2);
      }
    }
    /*}}}*/
    if ((tab=strchr(ln,'\t'))) /* load phrase and suggestion */ /*{{{*/
    {
      if ((badPhrases[badPhraseSize].phrase=malloc(tab-ln+1))==(char*)0)
      {
        fprintf(stderr,NOMEM);
        exit(2);
      }
      strncpy(badPhrases[badPhraseSize].phrase,ln,tab-ln);
      badPhrases[badPhraseSize].phrase[tab-ln]='\0';
      if ((badPhrases[badPhraseSize].suggest=malloc(l=strlen(tab+1)))==(char*)0)
      {
        fprintf(stderr,NOMEM);
        exit(2);
      }
      strncpy(badPhrases[badPhraseSize].suggest,tab+1,l);
      if (badPhrases[badPhraseSize].suggest[l-1]=='\n') badPhrases[badPhraseSize].suggest[l-1]='\0';
    }
    /*}}}*/
    else if (ln[0]!='\n') /* load superfluous phrase */ /*{{{*/
    {
      if ((badPhrases[badPhraseSize].phrase=malloc(l=strlen(ln)))==(char*)0)
      {
        fprintf(stderr,NOMEM);
        exit(2);
      }
      strncpy(badPhrases[badPhraseSize].phrase,ln,l);
      if (badPhrases[badPhraseSize].phrase[l-1]=='\n') badPhrases[badPhraseSize].phrase[l-1]='\0';
      badPhrases[badPhraseSize].suggest=(char*)0;
    }
    /*}}}*/
    ++badPhraseSize;
  }
  /* resolve =phrase explainations */ /*{{{*/
  for (fix=0; fix<badPhraseSize; ++fix)
  {
    if (badPhrases[fix].suggest && *badPhrases[fix].suggest=='=')
    {
      for (j=0; j<badPhraseSize; ++j)
      {
        if (j!=fix && strcmp(badPhrases[j].phrase,badPhrases[fix].suggest+1)==0)
        {
          free(badPhrases[fix].suggest);
          badPhrases[fix].suggest=badPhrases[j].suggest;
          break;
        }
        if (j==badPhraseSize)
        {
          fprintf(stderr,"diction: Warning: Unable to resolve %s.\n",badPhrases[fix].suggest);
        }
      }
    }
  }
  /*}}}*/
  /* sort phrases in reverse order and build skip array */ /*{{{*/
  qsort(badPhrases,badPhraseSize,sizeof(badPhrases[0]),phrasecmp);
  /*}}}*/
}
/*}}}*/

static void diction(const char *s, size_t length, const char *file, int line) /*{{{*/
{
  const char *lastout=s;
  size_t i;
  int j;
  int inword=0;
  int wordLength=-1;

  if (length==0) return;
  for (i=0; i<length; ++i)
  {
    const struct badPhrase *bp;
    const char *badword,*str;

    /* check for bad phrase */ /*{{{*/
    if (phraseStart[(unsigned int)((unsigned char)s[i])] || phraseStart[(unsigned int)((unsigned char)tolower(s[i]))])
    {
      for (j=0; j<badPhraseSize; ++j)
      {
        bp=&badPhrases[j];
        badword=bp->phrase;
        if (*badword==' ')
        {
          if (i && isalpha(s[i-1])) continue;
          ++badword;
        }
        str=&s[i];
        while ((*badword==tolower(*str) || *badword==*str) && *badword && *str) { ++badword; ++str; }
        if (*badword=='\0' && !isalpha(*str))
        {
          if (bp->suggest && *bp->suggest!='!')
          {
            ++hits;
            if (lastout==s) printf("%s:%d: ",file,line);
            while (lastout<s+i) putc(*lastout++,stdout);
            putc('[',stdout);
            while (lastout<str) { putc(*lastout++,stdout); ++i; }
            if (bp->suggest)
            {
              putc(' ',stdout);
              putc('-',stdout);
              putc('>',stdout);
              putc(' ',stdout);
              fputs(bp->suggest,stdout);
            }
            putc(']',stdout);
          }
          else
          {
            while (*badword) ++i;
          }
          break;
        }
      }
    }
    /*}}}*/
    /* check for double words */ /*{{{*/
    if (inword)
    {
      if (isalpha(s[i])) ++wordLength;
      else
      {
        const char *t;
        char *w;
        int last;

        last=wordQueueStart;
        if (++wordQueueStart>=wordQueueLength) wordQueueStart=0;
        if ((wordLength+1)>wordQueue[wordQueueStart].size)
        {
          wordQueue[wordQueueStart].word=realloc(wordQueue[wordQueueStart].word,wordQueue[wordQueueStart].size=wordLength+1);
        }
        w=wordQueue[wordQueueStart].word;
        for (t=s+i-wordLength; t<s+i; ++t,++w) *w=tolower(*t);
        *w++='\0';
        if (wordQueue[last].word && strcmp(wordQueue[last].word,wordQueue[wordQueueStart].word)==0)
        {
          if (lastout==s) printf("%s:%d: ",file,line);
          while (lastout<s+i-wordLength) putc(*lastout++,stdout);
          putc('[',stdout);
          while (lastout<s+i) { putc(*lastout++,stdout); }
          putc(' ',stdout);
          putc('-',stdout);
          putc('>',stdout);
          putc(' ',stdout);
          fputs(DOUBLE,stdout);
          putc(']',stdout);
        }
        inword=0;
      }
    }
    else if (isalpha(s[i]))
    {
      wordLength=1;
      inword=1;
    }
    /*}}}*/
  }
  ++sentences;
  if (lastout!=s)
  {
    while (lastout<s+length) putc(*lastout++,stdout);
    putc('\n',stdout);
    putc('\n',stdout);
  }
}
/*}}}*/

int main(int argc, char *argv[]) /*{{{*/
{
  int usage=0,c;
  const char *phraseLanguage=(const char*)0;
  char *userPhrases=(char*)0;
  char defaultPhrases[_POSIX_PATH_MAX];
  static struct option lopts[]=
  {
    { "file", required_argument, 0, 'f' },
    { "help", no_argument, 0, 'h' },
    { "version", no_argument, 0, 'v' },
    { "language", required_argument, 0, 'L' },
    { "no-default-file", no_argument, 0, 'n' },
    { (const char*)0, 0, 0, '\0' }
  };

  /* init locale */ /*{{{*/
  setlocale(LC_ALL,"");
#if 0
  phraseLanguage=setlocale(LC_MESSAGES,(char*)0);
#else
  if (getenv("LC_MESSAGES")) phraseLanguage=getenv("LC_MESSAGES");
  else phraseLanguage="C";
#endif
  catd=catopen("diction",0);
  /*}}}*/
  /* parse options */ /*{{{*/
  strcpy(defaultPhrases,SHAREDIR "/diction/");
  while ((c=getopt_long(argc,argv,"f:nL:h",lopts,(int*)0))!=EOF) switch(c)
  {
    case 'f': userPhrases=optarg; break;
    case 'n': defaultPhrases[0]='\0'; break;
    case 'L': phraseLanguage=optarg; break;
    case 'v': printf("GNU diction " VERSION "\n"); exit(0);
    case 'h': usage=2; break;
    default: usage=1; break;
  }
  if (defaultPhrases[0]) strcat(defaultPhrases,phraseLanguage);
  if (usage==1 || (userPhrases==(char*)0 && defaultPhrases[0]=='\0'))
  {
    fputs(USAGE1,stderr);
    fputs(USAGE2,stderr);
    fputs(USAGE3,stderr);
    fputs("\n",stderr);
    fputs(MORE,stderr);
    exit(1);
  }
  if (usage==2)
  {
    fputs(USAGE1,stdout);
    fputs(USAGE2,stdout);
    fputs(USAGE3,stdout);
    fputs("\n",stdout);
    fputs(HELP1,stdout);
    fputs("\n",stdout);
    fputs(HELP2,stdout);
    fputs(HELP3,stdout);
    fputs(HELP4,stdout);
    fputs(HELP5,stdout);
    fputs(HELP6,stdout);
    fputs("\n",stdout);
    fputs(BUGS,stdout);
    exit(0);
  }
  /*}}}*/
  /* allocate word queue */ /*{{{*/
  if ((wordQueue=malloc(sizeof(struct word)*(wordQueueLength=30)))==(struct word*)0)
  {
    fprintf(stderr,NOMEM);
    exit(2);
  }
  else
  {
    int i;

    for (i=0; i<wordQueueLength; ++i)
    {
      wordQueue[i].word=(char*)0;
      wordQueue[i].size=0;
    }
  }
  /*}}}*/
  if (defaultPhrases[0]) loadPhrases(defaultPhrases);
  if (userPhrases) loadPhrases(userPhrases);
  sentences=0;
  hits=0;
  if (optind==argc) sentence("diction",catd,stdin,"(stdin)",diction,phraseLanguage);
  else while (optind<argc)
  {
    FILE *fp;
    if ((fp=fopen(argv[optind],"r"))==(FILE*)0) fprintf(stderr,OPENFAILED,argv[optind],strerror(errno));
    else
    {
      sentence("diction",catd,fp,argv[optind],diction,phraseLanguage);
      fclose(fp);
    }
    ++optind;
  }
  if (sentences==0)
  {
    printf(NOSENT);
  }
  else
  {
    if (hits==0) printf(NOPHRASES);
    else if (hits==1) printf(ONEPHRASE);
    else printf(NPHRASES,hits);
    if (sentences==1) printf(ONESENT);
    else printf(NSENT,sentences);
  }
  exit(0);
}
/*}}}*/