# include # include # include # include # include char *default_dict; int max_recursion=20; void usage(void); void format( char *); void downcase( char *); void searchDictionary( char *); const char *DICT_PATH[]={ "./", "/usr/share/dict/", "/usr/local/share/dict", "/usr/dict/", "/usr/local/mova/", "\0" }; const char *DICT_FILE[]={ "Mueller7accentGPL.koi", "Mueller7GPL.koi", "Mueller24.koi", "Apresyan.koi", "\0" }; FILE *fdict; typedef struct { char ch[2]; unsigned long pos; } HashData; #define MAXWORDS 4096 HashData *hList[MAXWORDS]; unsigned int hLines=0; int noformat=0; int nodowncase=0; void downcase( char *verb ) { while ( *verb != '\0' ) { *verb = tolower( *verb ); verb++; } } /* simple line formating */ void format( char *s ) { char *ptr=s; char *p; while( (p=strstr(ptr," _I")) || (p=strstr(ptr," _V")) || (p=strstr(ptr," _X")) ) { *p='\n'; *(p+1)=' '; ptr=p+1; } printf( "%s", s ); } void searchDictionary( char *arg ) { char *line; unsigned int i; char *ptr; int cur_recursion=0; int cur_buffer; int is_found=0; unsigned int max_printwidth=74; int max_linebuffer=16384; int recurse_mode=0; cur_buffer=max_linebuffer; if( !nodowncase ) downcase( arg ); for(i=0;ich[0])==tolower(arg[0]) && tolower(hList[i]->ch[1])==tolower(arg[1]) ) { while( *arg ) { fseek( fdict, i?hList[i-1]->pos:0, SEEK_SET ); line=malloc( cur_buffer ); fgets(line,cur_buffer,fdict); ptr=line; while( *ptr && !(*ptr == ' ' && *(ptr+1) == ' ') ) ptr++; *ptr=0; if( !nodowncase ) downcase( line ); while( !feof(fdict) && tolower(hList[i]->ch[0])==tolower(line[0]) && tolower(hList[i]->ch[1])==tolower(line[1]) && cur_recursion=max_printwidth-1 ) { line[max_printwidth-1]='\0'; strcat(line," ...\n"); printf( line ); } else printf( line ); cur_recursion++; } fgets(line,cur_buffer,fdict); ptr=line; while( *ptr && !(*ptr == ' ' && *(ptr+1) == ' ') ) ptr++; *ptr=0; if( !nodowncase ) downcase( line ); } recurse_mode=1; arg[strlen(arg)-1]=0; free( line ); if( is_found || cur_recursion==max_recursion ) break; } break; } } } void usage() { printf( "Dictionary search tool, 2000 Dmitry Yusupov \n" ); fprintf( stderr,"Usage: mufinder [options] [word]\n"\ "Short anotation:\n"\ " Usefull utility for advanced searching and formating\n"\ " in Mueller dictionary (www.chat.ru/~mueller_dic).\n"\ "\tword\t\t- the word for search like word or 'word1 word2 ...'\n"\ "Some usefull option:\n"\ "\t-hash\t\t- creating hash file\n"\ "\t-d \t- preffered dictionary file\n"\ "\t-c \t- max recursions level\n"\ "\t-n\t\t- for flat line printing\n"\ "\t-y\t\t- no case sentience\n"\ "\t-h\t\t- this help\n" ); exit(0); } int main( int argc, char *argv[] ) { char *dictfn; char *idxName; char *line; HashData *hd; char hLine[16]; unsigned long pos; int i,j; FILE *fhash; int ishash=0; char *word; setlocale( LC_ALL, "ru_RU.KOI8-R" ); /* because it's only russian! */ if ( argc == 1 ) usage(); if( argc > 1 ) { for(i=1;i 2 ) word = argv[argc-1]; else word = argv[1]; } } setbuf( stdin, NULL ); dictfn = malloc( 512 ); if( default_dict ) { strcpy(dictfn,default_dict); fdict = fopen( dictfn, "r" ); if ( !fdict ) { fprintf( stderr, "Error opening given dictionary file %s\n", dictfn ); exit( -1 ); } } else { for( i=0; DICT_PATH[i][0]!='\0'; i++ ) { for( j=0; DICT_FILE[j][0]!='\0'; j++ ) { strcpy(dictfn, DICT_PATH[i]); strcat(dictfn, DICT_FILE[j]); fdict = fopen( dictfn, "r" ); if ( fdict ) break; } if ( fdict ) break; } } if ( !fdict ) { fprintf( stderr, "Error opening dictionary file\n" ); exit( -1 ); } idxName = malloc( strlen(dictfn) + 6 ); strcpy( idxName, dictfn ); strcat( idxName, ".h" ); if( !ishash ) { fhash = fopen( idxName, "r" ); if ( !fhash ) { fprintf( stderr, "Error opening hash file\n" ); fprintf( stderr, "Try to use: su - -c \"%s -hash\"\n", argv[0] ); exit( -1 ); } } else { fprintf(stderr, "Use dict file %s...\n", dictfn); fprintf(stderr, "Try to create hash file %s...\n", idxName); fhash = fopen( idxName, "w+" ); if ( !fhash ) { fprintf( stderr, "Error creating hash file\n" ); exit( -1 ); } i=0;j=0; line=malloc( 16384 ); fgets(line,16384,fdict); while( !feof(fdict) ) { downcase( line ); hLine[0]=line[0];hLine[1]=line[1]; pos=ftell(fdict); fgets(line,16384,fdict); downcase( line ); if( hLine[0]!=line[0] || hLine[1]!=line[1] || feof(fdict) ) { fprintf(fhash,"%c%c%ld\n",hLine[0],hLine[1],pos); fprintf(stderr,"\rProcessed... %d keys",i); i++; } } free( line ); fclose(fhash); fprintf(stderr,"\rProcessed... Completed.\n"); exit(0); } fgets(hLine,16,fhash); while( !feof(fhash) ) { hd=malloc( sizeof(HashData) ); hd->ch[0]=hLine[0]; hd->ch[1]=hLine[1]; hd->pos=atoi(hLine+2); hList[hLines]=hd; fgets(hLine,16,fhash); hLines++; if( hLines>MAXWORDS ) { fprintf( stderr, "Error loading hash file\n" ); exit( -1 ); } } fclose( fhash ); if( word != '\0' ) { searchDictionary(word); } fclose( fdict ); return 0; }