/* Notes */ /*{{{C}}}*//*{{{*/ /* This program is GNU software, copyright 1997-2004 Michael Haardt . 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 "config.h" #include #include #include #include #include #include #ifdef HAVE_GETTEXT #include #define _(String) gettext(String) #else #define _(String) String #endif #include #include #include #include #include #include "getopt.h" #include "misc.h" #include "sentence.h" /*}}}*/ /* types */ /*{{{*/ struct badPhrase { char *phrase; regex_t phrase_r; char *suggest; }; /*}}}*/ static int doubleWords=1; static char phraseLanguage[32]; static struct badPhrase *badPhrases=(struct badPhrase *)0; static int badPhraseCapacity=0; static int badPhraseSize=0; static int sentences,hits; static void loadPhrases(const char *file) /*{{{*/ { FILE *fp; char ln[1024]; char *tab; size_t l; int fix,j; if ((fp=fopen(file,"r"))==(FILE*)0) { fprintf(stderr,_("diction: Opening `%s' failed (%s).\n"),file,strerror(errno)); exit(1); } while (fgets(ln,sizeof(ln),fp)) { l=strlen(ln); if (l && ln[l-1]=='\n') ln[--l]='\0'; if (ln[0]) { int err; if (badPhraseSize==badPhraseCapacity) /* enlarge capacity */ /*{{{*/ { if ((badPhrases=realloc(badPhrases,(badPhraseCapacity=3*(badPhraseCapacity+32))*sizeof(struct badPhrase)))==(struct badPhrase*)0) { fprintf(stderr,_("diction: out of memory.\n")); exit(2); } } /*}}}*/ if ((tab=strchr(ln,'\t'))) { *tab='\0'; ++tab; } if ((badPhrases[badPhraseSize].phrase=malloc(strlen(ln)+1))==(char*)0) { fprintf(stderr,_("diction: out of memory.\n")); exit(2); } strcpy(badPhrases[badPhraseSize].phrase,ln); #if 0 if ((err=regcomp(&badPhrases[badPhraseSize].phrase_r,ln,REG_EXTENDED))!=0) { char errmsg[1024]; regerror(err,&badPhrases[badPhraseSize].phrase_r,errmsg,sizeof(errmsg)); fprintf(stderr,_("diction: Compiling regular expression `%s' failed (%s).\n"),ln,errmsg); exit(2); } #endif if (tab) { if ((badPhrases[badPhraseSize].suggest=malloc(strlen(tab)+1))==(char*)0) { fprintf(stderr,_("diction: out of memory.\n")); exit(2); } strcpy(badPhrases[badPhraseSize].suggest,tab); } else badPhrases[badPhraseSize].suggest=(char*)0; ++badPhraseSize; } } /* resolve =phrase explainations */ /*{{{*/ for (fix=0; fixphrase; if (*badword==' ') /* beginning of sentence or word */ { if (s>sent && isalpha(*(s-1))) continue; ++badword; } str=s; while ((*badword==tolower(*str) || *badword==*str) && *badword && *str) { ++badword; ++str; } if ((*badword=='\0' && !isalpha(*str)) || (*badword=='~' && isalpha(*str))) { if (bp->suggest && *bp->suggest!='!') { ++hits; if (lastout==sent) printf("%s:%d: ",file,line); while (lastoutsuggest) { putc(' ',stdout); putc('-',stdout); putc('>',stdout); putc(' ',stdout); fputs(bp->suggest,stdout); } putc(']',stdout); } s=str-1; break; } } /*}}}*/ /* check for double words */ /*{{{*/ if (doubleWords) { const char *badword,*str; if (s>sent && !isalpha(*(s-1))) { /* move back to end of last word */ badword=s-1; while (badword>=sent && !isalpha(*badword)) --badword; if (badword>sent) { /* move back to begin of last word */ while (badword>=sent && isalpha(*badword)) --badword; if (!isalpha(*badword)) ++badword; str=s; while (*badword==*str && badword',stdout); putc(' ',stdout); fputs(_("Double word."),stdout); putc(']',stdout); lastWord=s; s=str-1; } } } } /*}}}*/ ++s; } ++sentences; if (lastout!=sent) { while (lastout.\n"),stdout); exit(0); } /*}}}*/ if (defaultPhrases[0]) loadPhrases(defaultPhrases); if (userPhrases) loadPhrases(userPhrases); sentences=0; hits=0; if (optind==argc) sentence("diction",stdin,"(stdin)",diction,phraseLanguage); else while (optind