/*! * \param hashname name of the hash file (dictionary) * * \return */ int ISpellChecker::linit(char *hashname) { FILE *fpHash; register int i; register struct dent *dp; struct flagent *entry; struct flagptr *ind; int nextchar, x; int viazero; register ichar_t *cp; if((fpHash = fopen(hashname, "rb")) == NULL) { return (-1); } m_hashsize = fread(reinterpret_cast< char * >(&m_hashheader), 1, sizeof m_hashheader, fpHash); if(m_hashsize < static_cast< int >(sizeof(m_hashheader))) { if(m_hashsize < 0) fprintf(stderr, LOOKUP_C_CANT_READ, hashname); else if(m_hashsize == 0) fprintf(stderr, LOOKUP_C_NULL_HASH, hashname); else fprintf(stderr, LOOKUP_C_SHORT_HASH(m_hashname, m_hashsize, static_cast< int >(sizeof m_hashheader))); return (-1); } else if(m_hashheader.magic != MAGIC) { fprintf(stderr, LOOKUP_C_BAD_MAGIC(hashname, static_cast< unsigned int >(MAGIC), static_cast< unsigned int >(m_hashheader.magic))); return (-1); } else if(m_hashheader.magic2 != MAGIC) { fprintf(stderr, LOOKUP_C_BAD_MAGIC2(hashname, static_cast< unsigned int >(MAGIC), static_cast< unsigned int >(m_hashheader.magic2))); return (-1); } /* else if (hashheader.compileoptions != COMPILEOPTIONS*/ else if(1 != 1 || m_hashheader.maxstringchars != MAXSTRINGCHARS || m_hashheader.maxstringcharlen != MAXSTRINGCHARLEN) { fprintf(stderr, LOOKUP_C_BAD_OPTIONS(static_cast< unsigned int >(m_hashheader.compileoptions), m_hashheader.maxstringchars, m_hashheader.maxstringcharlen, static_cast< unsigned int >(COMPILEOPTIONS), MAXSTRINGCHARS, MAXSTRINGCHARLEN)); return (-1); } { m_hashtbl = (struct dent *)calloc(static_cast< unsigned >(m_hashheader.tblsize), sizeof(struct dent)); m_hashsize = m_hashheader.tblsize; m_hashstrings = static_cast< char * >(malloc(static_cast< unsigned >(m_hashheader.stringsize))); } m_numsflags = m_hashheader.stblsize; m_numpflags = m_hashheader.ptblsize; m_sflaglist = (struct flagent *)malloc((m_numsflags + m_numpflags) * sizeof(struct flagent)); if(m_hashtbl == NULL || m_hashstrings == NULL || m_sflaglist == NULL) { fprintf(stderr, LOOKUP_C_NO_HASH_SPACE); return (-1); } m_pflaglist = m_sflaglist + m_numsflags; { if(fread(m_hashstrings, 1, static_cast< unsigned >(m_hashheader.stringsize), fpHash) != static_cast< size_t >(m_hashheader.stringsize)) { fprintf(stderr, LOOKUP_C_BAD_FORMAT); fprintf(stderr, "stringsize err\n"); return (-1); } if(m_hashheader.compileoptions & 0x04) { if(fread(reinterpret_cast< char * >(m_hashtbl), 1, static_cast< unsigned >(m_hashheader.tblsize) * sizeof(struct dent), fpHash) != (static_cast< size_t >(m_hashheader.tblsize * sizeof(struct dent)))) { fprintf(stderr, LOOKUP_C_BAD_FORMAT); return (-1); } } else { for(x = 0; x < m_hashheader.tblsize; x++) { if(fread(reinterpret_cast< char * >(m_hashtbl + x), sizeof(struct dent) - sizeof(MASKTYPE), 1, fpHash) != 1) { fprintf(stderr, LOOKUP_C_BAD_FORMAT); return (-1); } } /*for*/ } /*else*/ } if(fread(reinterpret_cast< char * >(m_sflaglist), 1, static_cast< unsigned >(m_numsflags + m_numpflags) * sizeof(struct flagent), fpHash) != (m_numsflags + m_numpflags) * sizeof(struct flagent)) { fprintf(stderr, LOOKUP_C_BAD_FORMAT); return (-1); } fclose(fpHash); { for(i = m_hashsize, dp = m_hashtbl; --i >= 0; dp++) { if(dp->word == (char *)-1) dp->word = NULL; else dp->word = &m_hashstrings[reinterpret_cast< size_t >(dp->word)]; if(dp->next == (struct dent *)-1) dp->next = NULL; else dp->next = &m_hashtbl[reinterpret_cast< size_t >(dp->next)]; } } for(i = m_numsflags + m_numpflags, entry = m_sflaglist; --i >= 0; entry++) { if(entry->stripl) entry->strip = reinterpret_cast< ichar_t * >(&m_hashstrings[reinterpret_cast< size_t >(entry->strip)]); else entry->strip = NULL; if(entry->affl) entry->affix = reinterpret_cast< ichar_t * >(&m_hashstrings[reinterpret_cast< size_t >(entry->affix)]); else entry->affix = NULL; } /* ** Warning - 'entry' and 'i' are reset in the body of the loop ** below. Don't try to optimize it by (e.g.) moving the decrement ** of i into the loop condition. */ for(i = m_numsflags, entry = m_sflaglist; i > 0; i--, entry++) { if(entry->affl == 0) { cp = NULL; ind = &m_sflagindex[0]; viazero = 1; } else { cp = entry->affix + entry->affl - 1; ind = &m_sflagindex[*cp]; viazero = 0; while(ind->numents == 0 && ind->pu.fp != NULL) { if(cp == entry->affix) { ind = &ind->pu.fp[0]; viazero = 1; } else { ind = &ind->pu.fp[*--cp]; viazero = 0; } } } if(ind->numents == 0) ind->pu.ent = entry; ind->numents++; /* ** If this index entry has more than MAXSEARCH flags in ** it, we will split it into subentries to reduce the ** searching. However, the split doesn't make sense in ** two cases: (a) if we are already at the end of the ** current affix, or (b) if all the entries in the list ** have identical affixes. Since the list is sorted, (b) ** is true if the first and last affixes in the list ** are identical. */ if(!viazero && ind->numents >= MAXSEARCH && icharcmp(entry->affix, ind->pu.ent->affix) != 0) { /* Sneaky trick: back up and reprocess */ entry = ind->pu.ent - 1; /* -1 is for entry++ in loop */ i = m_numsflags - (entry - m_sflaglist); ind->pu.fp = (struct flagptr *)calloc(static_cast< unsigned >(SET_SIZE + m_hashheader.nstrchars), sizeof(struct flagptr)); if(ind->pu.fp == NULL) { fprintf(stderr, LOOKUP_C_NO_LANG_SPACE); return (-1); } ind->numents = 0; } } /* ** Warning - 'entry' and 'i' are reset in the body of the loop ** below. Don't try to optimize it by (e.g.) moving the decrement ** of i into the loop condition. */ for(i = m_numpflags, entry = m_pflaglist; i > 0; i--, entry++) { if(entry->affl == 0) { cp = NULL; ind = &m_pflagindex[0]; viazero = 1; } else { cp = entry->affix; ind = &m_pflagindex[*cp++]; viazero = 0; while(ind->numents == 0 && ind->pu.fp != NULL) { if(*cp == 0) { ind = &ind->pu.fp[0]; viazero = 1; } else { ind = &ind->pu.fp[*cp++]; viazero = 0; } } } if(ind->numents == 0) ind->pu.ent = entry; ind->numents++; /* ** If this index entry has more than MAXSEARCH flags in ** it, we will split it into subentries to reduce the ** searching. However, the split doesn't make sense in ** two cases: (a) if we are already at the end of the ** current affix, or (b) if all the entries in the list ** have identical affixes. Since the list is sorted, (b) ** is true if the first and last affixes in the list ** are identical. */ if(!viazero && ind->numents >= MAXSEARCH && icharcmp(entry->affix, ind->pu.ent->affix) != 0) { /* Sneaky trick: back up and reprocess */ entry = ind->pu.ent - 1; /* -1 is for entry++ in loop */ i = m_numpflags - (entry - m_pflaglist); ind->pu.fp = static_cast< struct flagptr * >(calloc(SET_SIZE + m_hashheader.nstrchars, sizeof(struct flagptr))); if(ind->pu.fp == NULL) { fprintf(stderr, LOOKUP_C_NO_LANG_SPACE); return (-1); } ind->numents = 0; } } #ifdef INDEXDUMP fprintf(stderr, "Prefix index table:\n"); dumpindex(m_pflagindex, 0); fprintf(stderr, "Suffix index table:\n"); dumpindex(m_sflagindex, 0); #endif if(m_hashheader.nstrchartype == 0) m_chartypes = NULL; else { m_chartypes = (struct strchartype *)malloc(m_hashheader.nstrchartype * sizeof(struct strchartype)); if(m_chartypes == NULL) { fprintf(stderr, LOOKUP_C_NO_LANG_SPACE); return (-1); } for(i = 0, nextchar = m_hashheader.strtypestart; i < m_hashheader.nstrchartype; i++) { m_chartypes[i].name = &m_hashstrings[nextchar]; nextchar += strlen(m_chartypes[i].name) + 1; m_chartypes[i].deformatter = &m_hashstrings[nextchar]; nextchar += strlen(m_chartypes[i].deformatter) + 1; m_chartypes[i].suffixes = &m_hashstrings[nextchar]; while(m_hashstrings[nextchar] != '\0') nextchar += strlen(&m_hashstrings[nextchar]) + 1; nextchar++; } } initckch(NULL); return (0); }
/** * @brief ... * @param argc * @param argv * @param lib */ int my_main(int argc, char *argv[], char lib) { char *wchars = NULL; char *preftype = NULL; static char outbuf[BUFSIZ]; int arglen, old_argc; char *cpd = NULL; char *Cmd = *argv; /* Pointer to name of $(LIBDIR)/dict */ char *LibDict = NULL; islib = lib; old_argc = argc; Trynum = 0; sprintf(hashname, "%s/%s", LIBDIR, DEFHASH); strcpy(signs, DEFAULT_SIGNS); argv++; argc--; while (argc && **argv == '-') { /* * Trying to add a new flag? Can't remember what's been used? * Here's a handy guide: * * Used: * ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 * ^^^^ ^^^ ^ ^^ ^^ * abcdefghijklmnopqrstuvwxyz * ^^^^^^* ^^^*^ ^^*^^^* */ arglen = strlen(*argv); add_inc = 0; switch ((*argv)[1]) { case 'v': option_v(Cmd, argc, argv, arglen); break; case 'n': preftype = option_n(Cmd, preftype, arglen); break; case 't': /* TeX mode */ preftype = option_t(Cmd, preftype, arglen); break; case 'T': /* Set preferred file type */ preftype = option_T(Cmd, argc, argv); break; case 'A': option_A(Cmd, arglen); break; case 'a': option_a(Cmd, arglen); break; case 'D': option_D(Cmd, arglen); break; case 'J': option_J(Cmd, arglen); break; case 'e': option_e(Cmd, arglen, argv); break; case 'c': option_c(Cmd, arglen, argv); break; case 'b': option_b(Cmd, arglen); break; case 'x': option_x(Cmd, arglen); break; case 'f': option_f(Cmd, argc, argv); break; case 'L': option_L(Cmd, argc, argv); break; case 'l': option_l(Cmd, arglen); break; case 'S': option_S(Cmd, arglen); break; case 'B': /* -B: report missing blanks */ option_B(Cmd, arglen); break; case 'C': /* -C: compound words are acceptable */ option_C(Cmd, arglen); break; case 'P': /* -P: don't gen non-dict poss's */ option_P(Cmd, arglen); break; case 'm': /* -m: make all poss affix combos*/ option_m(Cmd, arglen); break; case 'N': /* -N: suppress minimenu */ option_N(Cmd, arglen); break; case 'M': option_M(Cmd, arglen); break; /* -M: force minimenu */ case 'p': option_p(LibDict, &cpd, Cmd, argc, argv); break; case 'd': option_d(LibDict, cpd, Cmd, argc, argv); break; case 'V': /* Display 8-bit characters as M-xxx */ option_V(Cmd, arglen); break; case 'w': wchars = (*argv)+2; if (*wchars == '\0') { argv++; argc--; if (argc == 0) usage(Cmd); wchars = *argv; } break; case 'W': option_W(Cmd, argc, argv); break; case 'o': option_o(Cmd, argc, argv); break; case 'g': option_g(Cmd, arglen); break; case 'u': option_u(Cmd, arglen); break; case 'y': option_y(Cmd, arglen); break; case 'z': option_z(Cmd, arglen); break; default: usage(Cmd); } /* end switch */ if (add_inc) { argv++; argc--; } argv++; argc--; } if (!argc && !lflag && !aflag && !eflag && !dumpflag) usage(Cmd); verify_files(argc, argv); if (!oflag) strcpy(o_form, DEF_OUT); if (linit() < 0) exit(1); /* Force an error */ det_prefstringchar(preftype); if (prefstringchar < 0) defdupchar = 0; else defdupchar = prefstringchar; if (missingspaceflag < 0) missingspaceflag = hashheader.defspaceflag; if (tryhardflag < 0) tryhardflag = hashheader.defhardflag; initckch(wchars); process_LibDict(LibDict, cpd); if (process_a_e_and_d_flags() == 0) return 0; if (!islib) setbuf(stdout, outbuf); /* process lflag (also used with the -c option) */ if (lflag) { infile = stdin; outfile = stdout; if (!islib) checkfile(); return 0; } /* n. of parameters advanced */ return old_argc - argc; }