diff options
author | Peter Breitenlohner <peb@mppmu.mpg.de> | 2012-07-28 10:09:46 +0000 |
---|---|---|
committer | Peter Breitenlohner <peb@mppmu.mpg.de> | 2012-07-28 10:09:46 +0000 |
commit | 2c71c9d92117f7f1fe0d5bd6fe74922e50be0c86 (patch) | |
tree | 6581f8e2c781693a75e6eabb32df88f8299fe1a2 | |
parent | bb3913a1553c1213f93e6f10185f770cde5ae901 (diff) |
mendexk: Use Nelson Beebe's qqsort() instead of qsort()
Handle input files with Dos/Windows (CRLF) line endings (from W32TeX)
Always use KPATHSEA. Use xmalloc() and xstrdup()
git-svn-id: svn://tug.org/texlive/trunk@27216 c570f23f-e606-0410-a88d-b1316a301751
-rw-r--r-- | Build/source/texk/mendexk/ChangeLog | 22 | ||||
-rw-r--r-- | Build/source/texk/mendexk/Makefile.am | 6 | ||||
-rw-r--r-- | Build/source/texk/mendexk/Makefile.in | 8 | ||||
-rw-r--r-- | Build/source/texk/mendexk/convert.c | 29 | ||||
-rw-r--r-- | Build/source/texk/mendexk/fread.c | 68 | ||||
-rw-r--r-- | Build/source/texk/mendexk/fwrite.c | 12 | ||||
-rw-r--r-- | Build/source/texk/mendexk/kp.c | 26 | ||||
-rw-r--r-- | Build/source/texk/mendexk/kp.h | 6 | ||||
-rw-r--r-- | Build/source/texk/mendexk/main.c | 36 | ||||
-rw-r--r-- | Build/source/texk/mendexk/mendex.h | 10 | ||||
-rw-r--r-- | Build/source/texk/mendexk/pageread.c | 15 | ||||
-rw-r--r-- | Build/source/texk/mendexk/qsort.c | 206 | ||||
-rw-r--r-- | Build/source/texk/mendexk/qsort.h | 16 | ||||
-rw-r--r-- | Build/source/texk/mendexk/sort.c | 4 | ||||
-rw-r--r-- | Build/source/texk/mendexk/styfile.c | 20 |
15 files changed, 328 insertions, 156 deletions
diff --git a/Build/source/texk/mendexk/ChangeLog b/Build/source/texk/mendexk/ChangeLog index 120556fb31a..b48370dab59 100644 --- a/Build/source/texk/mendexk/ChangeLog +++ b/Build/source/texk/mendexk/ChangeLog @@ -1,3 +1,22 @@ +2012-07-28 Peter Breitenlohner <peb@mppmu.mpg.de> + + * convert.c, pageread.c, styfile.c: Handle input files with + Dos/Windows (CRLF) line endings (from W32TeX). + + Use Nelson Beebe's qqsort() instead of qsort() (from W32TeX). + * convert.c, sort.c: Add #include "qsort.h". + * qsort.[ch] (new): Copied from ../makeindexk. + * Makefile.am (mendex_SOURCES): Add the new files. + +2012-07-27 Peter Breitenlohner <peb@mppmu.mpg.de> + + * convert.c, kp.[ch], main.c, styfile.c: Always use KPATHSEA. + * Makefile.am (AM_CPPFLAGS): Remove '-DKPATHSEA'. + * convert.c, fread.c, main.c, sort.c: Use xmalloc(). + * fread.c, main.c: Use xstrdup() instead of malloc()+strcpy(). + * fwrite.c, mendex.h, kp.[ch]: Change {verb,warn}_printf() and + KP_entry_filetype() return type from 'int' into 'void'. + 2012-07-26 Peter Breitenlohner <peb@mppmu.mpg.de> * fwrite.c, main.c: Use binary mode for input and output @@ -59,9 +78,6 @@ always cast string bytes to 'unsigned char' when tested for '<', '<=', '>', or '>='. - * convert.c, fread.c, main.c, sort.c: Do not typecast the - result of malloc(). - 2010-04-17 Peter Breitenlohner <peb@mppmu.mpg.de> * convert.c (dicread, dicvalread), fread.c (idxread), diff --git a/Build/source/texk/mendexk/Makefile.am b/Build/source/texk/mendexk/Makefile.am index 7696f057b4b..68e25da35d7 100644 --- a/Build/source/texk/mendexk/Makefile.am +++ b/Build/source/texk/mendexk/Makefile.am @@ -1,12 +1,11 @@ ## Makefile.am for the TeX Live subdirectory texk/mendexk/ ## -## Copyright (C) 2010, 2011 Peter Breitenlohner <tex-live@tug.org> +## Copyright (C) 2010-2012 Peter Breitenlohner <tex-live@tug.org> ## You may freely use, modify and/or distribute this file. ## ACLOCAL_AMFLAGS = -I ../../m4 INCLUDES = $(PTEXENC_INCLUDES) $(KPATHSEA_INCLUDES) -AM_CPPFLAGS = -DKPATHSEA AM_CFLAGS = $(WARNING_CFLAGS) bin_PROGRAMS = mendex @@ -24,11 +23,12 @@ mendex_SOURCES = \ main.c \ mendex.h \ pageread.c \ + qsort.c \ + qsort.h \ sort.c \ styfile.c \ var.h - $(mendex_OBJECTS): $(KPATHSEA_DEPEND) $(PTEXENC_DEPEND) LDADD = $(PTEXENC_LIBS) $(KPATHSEA_LIBS) diff --git a/Build/source/texk/mendexk/Makefile.in b/Build/source/texk/mendexk/Makefile.in index a5c5a0ccabe..0ab67c9532f 100644 --- a/Build/source/texk/mendexk/Makefile.in +++ b/Build/source/texk/mendexk/Makefile.in @@ -86,8 +86,8 @@ CONFIG_CLEAN_VPATH_FILES = am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)" PROGRAMS = $(bin_PROGRAMS) am_mendex_OBJECTS = convert.$(OBJEXT) fread.$(OBJEXT) fwrite.$(OBJEXT) \ - kp.$(OBJEXT) main.$(OBJEXT) pageread.$(OBJEXT) sort.$(OBJEXT) \ - styfile.$(OBJEXT) + kp.$(OBJEXT) main.$(OBJEXT) pageread.$(OBJEXT) qsort.$(OBJEXT) \ + sort.$(OBJEXT) styfile.$(OBJEXT) mendex_OBJECTS = $(am_mendex_OBJECTS) mendex_LDADD = $(LDADD) am__DEPENDENCIES_1 = @@ -287,7 +287,6 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ ACLOCAL_AMFLAGS = -I ../../m4 INCLUDES = $(PTEXENC_INCLUDES) $(KPATHSEA_INCLUDES) -AM_CPPFLAGS = -DKPATHSEA AM_CFLAGS = $(WARNING_CFLAGS) mendex_SOURCES = \ convert.c \ @@ -302,6 +301,8 @@ mendex_SOURCES = \ main.c \ mendex.h \ pageread.c \ + qsort.c \ + qsort.h \ sort.c \ styfile.c \ var.h @@ -425,6 +426,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/kp.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pageread.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/qsort.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sort.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/styfile.Po@am__quote@ diff --git a/Build/source/texk/mendexk/convert.c b/Build/source/texk/mendexk/convert.c index be0441b4cdb..55f81af0768 100644 --- a/Build/source/texk/mendexk/convert.c +++ b/Build/source/texk/mendexk/convert.c @@ -3,18 +3,17 @@ #include <string.h> #include <kpathsea/config.h> -#include <kpathsea/lib.h> #include <kpathsea/tex-file.h> +#include <kpathsea/variable.h> #include <ptexenc/ptexenc.h> #include "mendex.h" +#include "qsort.h" #include "exkana.h" #include "ktable.h" #include "exvar.h" -#ifdef KPATHSEA #include "kp.h" -#endif struct dictionary{ char dic[2][50]; @@ -68,9 +67,7 @@ int dicread(const char *filename) FILE *fp; if (filename!=NULL) { -#ifdef KPATHSEA filename = KP_find_file(&kp_dict,filename); -#endif if(kpse_in_name_ok(filename)) fp=nkf_open(filename,"rb"); else @@ -83,11 +80,11 @@ int dicread(const char *filename) for (i=0;;i++) { if (mfgets(buff,4095,fp)==NULL) break; - if ((buff[0]=='\n')||(buff[0]=='\0')) i--; + if ((buff[0]=='\r')||(buff[0]=='\n')||(buff[0]=='\0')) i--; } nkf_close(fp); - dictable=malloc(sizeof(struct dictionary)*i); + dictable=xmalloc(sizeof(struct dictionary)*i); dlines=dicvalread(filename,dictable,i); @@ -95,15 +92,9 @@ int dicread(const char *filename) } ENV: -#ifdef KPATHSEA - envfile=KP_get_value("INDEXDEFAULTDICTIONARY",NULL); -#else - envfile=getenv("INDEXDEFAULTDICTIONARY"); -#endif + envfile=kpse_var_value("INDEXDEFAULTDICTIONARY"); if ((envfile!=NULL)&&(strlen(envfile)!=0)) { -#ifdef KPATHSEA envfile = KP_find_file(&kp_dict,envfile); -#endif if(kpse_in_name_ok(envfile)) fp=nkf_open(envfile,"rb"); else @@ -116,11 +107,11 @@ ENV: for (i=0;;i++) { if (mfgets(buff,255,fp)==NULL) break; - if ((buff[0]=='\n')||(buff[0]=='\0')) i--; + if ((buff[0]=='\r')||(buff[0]=='\n')||(buff[0]=='\0')) i--; } nkf_close(fp); - envdic=malloc(sizeof(struct dictionary)*i); + envdic=xmalloc(sizeof(struct dictionary)*i); elines=dicvalread(envfile,envdic,i); @@ -147,12 +138,12 @@ static int dicvalread(const char *filename, struct dictionary *dicval, int line) } for (i=0;i<line;i++) { if (mfgets(buff,255,fp)==NULL) break; - if ((buff[0]=='\n')||(buff[0]=='\0')) { + if ((buff[0]=='\r')||(buff[0]=='\n')||(buff[0]=='\0')) { i--; continue; } for (j=0;((buff[j]==' ')||(buff[j]=='\t'));j++); - for (k=0;((buff[j]!='\n')&&(buff[j]!=' ')&&(buff[j]!='\t'));j++,k++) { + for (k=0;((buff[j]!='\r')&&(buff[j]!='\n')&&(buff[j]!=' ')&&(buff[j]!='\t'));j++,k++) { dicval[i].dic[0][k]=buff[j]; } dicval[i].dic[0][k]='\0'; @@ -161,7 +152,7 @@ static int dicvalread(const char *filename, struct dictionary *dicval, int line) continue; } for (;((buff[j]==' ')||(buff[j]=='\t'));j++); - for (k=0;((buff[j]!='\n')&&(buff[j]!=' ')&&(buff[j]!='\t'));j++,k++) { + for (k=0;((buff[j]!='\r')&&(buff[j]!='\n')&&(buff[j]!=' ')&&(buff[j]!='\t'));j++,k++) { dicval[i].dic[1][k]=buff[j]; } dicval[i].dic[1][k]='\0'; diff --git a/Build/source/texk/mendexk/fread.c b/Build/source/texk/mendexk/fread.c index e6fd3804a60..0b6778ff6ef 100644 --- a/Build/source/texk/mendexk/fread.c +++ b/Build/source/texk/mendexk/fread.c @@ -122,11 +122,7 @@ LOOP: n++; goto LOOP; } - ind[i].idx[indent]=malloc(k+1); - if (ind[i].idx[indent]==NULL) { - fprintf(stderr,"Malloc error.(ind[%d].idx[%d])\n",i,indent); - exit(255); - } + ind[i].idx[indent]=xmalloc(k+1); strncpy(ind[i].idx[indent],wbuff,k); ind[i].idx[indent][k]='\0'; if (!wflg) ind[i].org[indent]=NULL; @@ -145,11 +141,7 @@ LOOP: n++; goto LOOP; } - ind[i].org[indent]=malloc(k+1); - if (ind[i].org[indent]==NULL) { - fprintf(stderr,"Malloc error.(ind[%d].org[%d])\n",i,indent); - exit(255); - } + ind[i].org[indent]=xmalloc(k+1); strncpy(ind[i].org[indent],wbuff,k); ind[i].org[indent][k]='\0'; wflg=1; @@ -173,11 +165,7 @@ LOOP: } else estr[0]='\0'; - ind[i].idx[indent]=malloc(k+1); - if (ind[i].idx[indent]==NULL) { - fprintf(stderr,"Malloc error.(ind[%d].idx[%d])\n",i,indent); - exit(255); - } + ind[i].idx[indent]=xmalloc(k+1); strncpy(ind[i].idx[indent],wbuff,k); ind[i].idx[indent][k]='\0'; if (strlen(ind[i].idx[indent])==0) { @@ -243,12 +231,7 @@ LOOP: n++; goto LOOP; } - ind[i].dic[k]=malloc(strlen(table)+1); - if (ind[i].dic[k]==NULL) { - fprintf(stderr,"Malloc error.(ind[%d].dic[%d])\n",i,k); - exit(255); - } - strcpy(ind[i].dic[k],table); + ind[i].dic[k]=xstrdup(table); } else { cc=convert(ind[i].org[k],table); @@ -260,12 +243,7 @@ LOOP: n++; goto LOOP; } - ind[i].dic[k]=malloc(strlen(table)+1); - if (ind[i].dic[k]==NULL) { - fprintf(stderr,"Malloc error.(ind[%d].dic[%d])\n",i,k); - exit(255); - } - strcpy(ind[i].dic[k],table); + ind[i].dic[k]=xstrdup(table); } } acc++; @@ -274,7 +252,7 @@ LOOP: if (i==0) { ind[0].num=0; - ind[0].p=malloc(sizeof(struct page)*16); + ind[0].p=xmalloc(sizeof(struct page)*16); for (;buff[j]!=arg_open && buff[j]!='\n' && buff[j]!='\0';j++); if (buff[j]=='\n' || buff[j]=='\0') { verb_printf(efp,"\nWarning: Missing second argument in %s, line %d.",filename,ind[i].lnum); @@ -299,12 +277,7 @@ LOOP: else if (buff[j]==arg_close) { if (nest==0) { table[k]='\0'; - ind[0].p[0].page=malloc(strlen(table)+1); - if (ind[0].p[0].page==NULL) { - fprintf(stderr,"Malloc error.(ind[0].p[0].page)\n"); - exit(255); - } - strcpy(ind[0].p[0].page,table); + ind[0].p[0].page=xstrdup(table); break; } else nest--; @@ -316,12 +289,7 @@ LOOP: } table[k]=buff[j]; } - ind[0].p[0].enc=malloc(strlen(estr)+1); - if (ind[0].p[0].enc==NULL) { - fprintf(stderr,"Malloc error.(ind[0].p[0].enc)\n"); - exit(255); - } - strcpy(ind[0].p[0].enc,estr); + ind[0].p[0].enc=xstrdup(estr); chkpageattr(&ind[0].p[0]); } else { @@ -401,17 +369,15 @@ LOOP: ind[l].num++; if (!((ind[l].num)%16)) ind[l].p=(struct page *)realloc(ind[l].p,sizeof(struct page)*((int)((ind[l].num)/16)+1)*16); - ind[l].p[ind[l].num].page=malloc(strlen(table)+1); - strcpy(ind[l].p[ind[l].num].page,table); + ind[l].p[ind[l].num].page=xstrdup(table); - ind[l].p[ind[l].num].enc=malloc(strlen(estr)+1); - strcpy(ind[l].p[ind[l].num].enc,estr); + ind[l].p[ind[l].num].enc=xstrdup(estr); chkpageattr(&ind[l].p[ind[l].num]); } } else { ind[i].num=0; - ind[i].p=malloc(sizeof(struct page)*16); + ind[i].p=xmalloc(sizeof(struct page)*16); for (;buff[j]!=arg_open && buff[j]!='\n' && buff[j]!='\0';j++); if (buff[j]=='\n' || buff[j]=='\0') { verb_printf(efp,"\nWarning: Missing second argument in %s, line %d.",filename,ind[i].lnum); @@ -436,8 +402,7 @@ LOOP: if (buff[j]==arg_close) { if (nest==0) { table[k]='\0'; - ind[i].p[0].page=malloc(strlen(table)+1); - strcpy(ind[i].p[0].page,table); + ind[i].p[0].page=xstrdup(table); break; } else nest--; @@ -449,8 +414,7 @@ LOOP: } table[k]=buff[j]; } - ind[l].p[0].enc=malloc(strlen(estr)+1); - strcpy(ind[i].p[0].enc,estr); + ind[l].p[0].enc=xstrdup(estr); chkpageattr(&ind[i].p[0]); } } @@ -558,13 +522,13 @@ ATTRLOOP: if (cc<2) p->attr[++cc]= -1; } -char *mfgets(char *buf, int byte, FILE *fp) +char *mfgets(char *buf, int size, FILE *fp) { int c, len; - if ((len = input_line2(fp, (unsigned char *) buf, 0, byte, &c)) == 0) return NULL; + if ((len = input_line2(fp, (unsigned char *) buf, 0, size, &c)) == 0) return NULL; if (c == '\n' || c == '\r') { - if (len+1 < byte) strcat(buf+len, "\n"); + if (len+1 < size) strcat(buf+len, "\n"); else ungetc(c, fp); } if (c == EOF) return NULL; diff --git a/Build/source/texk/mendexk/fwrite.c b/Build/source/texk/mendexk/fwrite.c index 10eb46c9b93..5fb4c56fa36 100644 --- a/Build/source/texk/mendexk/fwrite.c +++ b/Build/source/texk/mendexk/fwrite.c @@ -38,35 +38,31 @@ int fprintf2(FILE *fp, const char *format, ...) return n; } -int warn_printf(FILE *fp, const char *format, ...) +void warn_printf(FILE *fp, const char *format, ...) { char print_buff[8000]; va_list argptr; - int n; va_start(argptr, format); - n = vsnprintf(print_buff, sizeof print_buff, format, argptr); + vsnprintf(print_buff, sizeof print_buff, format, argptr); va_end(argptr); warn++; fputs(print_buff, stderr); if (fp!=stderr) fputs(print_buff, fp); - return n; } -int verb_printf(FILE *fp, const char *format, ...) +void verb_printf(FILE *fp, const char *format, ...) { char print_buff[8000]; va_list argptr; - int n; va_start(argptr, format); - n = vsnprintf(print_buff, sizeof print_buff, format, argptr); + vsnprintf(print_buff, sizeof print_buff, format, argptr); va_end(argptr); if (verb!=0) fputs(print_buff, stderr); if (fp!=stderr) fputs(print_buff, fp); - return n; } diff --git a/Build/source/texk/mendexk/kp.c b/Build/source/texk/mendexk/kp.c index b319faa9c65..63d4bd6163d 100644 --- a/Build/source/texk/mendexk/kp.c +++ b/Build/source/texk/mendexk/kp.c @@ -1,28 +1,9 @@ /* Written by K.Asayayam Sep. 1995 */ -#ifdef KPATHSEA #include <kpathsea/kpathsea.h> #include <string.h> #include "kp.h" -int -KP_init(char *prog) -{ - kpse_set_program_name(prog, "mendex"); - return 0; -} - -/* KP_get_value(char *var,char *def_val) - ARGUMENTS: - char *var: name of variable. - char *def_val: default value. - */ -const char *KP_get_value(const char *var, const char *def_val) -{ - const char *p = kpse_var_value(var); - return p ? p : def_val; -} - /* KP_get_path(char *var, char *def_val) ARGUMENTS: char *var: name of variable. @@ -41,10 +22,9 @@ static const char *KP_get_path(const char *var, const char *def_val) /* */ -int KP_entry_filetype(KpathseaSupportInfo *info) +void KP_entry_filetype(KpathseaSupportInfo *info) { info->path = KP_get_path(info->var_name,info->path); - return 0; } /* KP_find_file(KpathseaSupportInfo *info, char *name) @@ -64,7 +44,3 @@ const char *KP_find_file(KpathseaSupportInfo *info, const char *name) } return ret ? ret : name; } - -#else -int KP_dummy_variable; -#endif diff --git a/Build/source/texk/mendexk/kp.h b/Build/source/texk/mendexk/kp.h index 69ae4fa8df5..2b6b83f9d98 100644 --- a/Build/source/texk/mendexk/kp.h +++ b/Build/source/texk/mendexk/kp.h @@ -1,7 +1,6 @@ /* Written by K.Asayayam Sep. 1995 */ #ifndef KSUPPORT_H_INCLUDED #define KSUPPORT_H_INCLUDED -#ifdef KPATHSEA typedef struct { const char *var_name; const char *path; @@ -10,9 +9,6 @@ typedef struct { extern KpathseaSupportInfo kp_ist, kp_dict; -extern int KP_init(char *); -extern int KP_entry_filetype(KpathseaSupportInfo *); +extern void KP_entry_filetype(KpathseaSupportInfo *); extern const char *KP_find_file(KpathseaSupportInfo *, const char *); -extern const char *KP_get_value(const char *, const char *); -#endif /* KPATHSEA */ #endif /* ! KSUPPORT_H_INCLUDED */ diff --git a/Build/source/texk/mendexk/main.c b/Build/source/texk/mendexk/main.c index e418c709960..3afd43ce5b1 100644 --- a/Build/source/texk/mendexk/main.c +++ b/Build/source/texk/mendexk/main.c @@ -4,19 +4,17 @@ #include <kpathsea/config.h> #include <kpathsea/tex-file.h> +#include <kpathsea/variable.h> #include <ptexenc/ptexenc.h> #include "mendex.h" #include "kana.h" #include "var.h" -#ifdef KPATHSEA #include "kp.h" -#endif char *styfile,*idxfile[256],indfile[256],*dicfile,logfile[256]; -#ifdef KPATHSEA /* default paths */ #ifndef DEFAULT_INDEXSTYLES #define DEFAULT_INDEXSTYLES "." @@ -25,7 +23,6 @@ char *styfile,*idxfile[256],indfile[256],*dicfile,logfile[256]; #define DEFAULT_INDEXDICTS "." #endif KpathseaSupportInfo kp_ist,kp_dict; -#endif #define VERSION "version 2.6f [14-Aug-2009]" @@ -38,8 +35,7 @@ int main(int argc, char **argv) #ifdef WIN32 _setmaxstdio(2048); #endif -#ifdef KPATHSEA - KP_init(argv[0]); + kpse_set_program_name(argv[0], "mendex"); kp_ist.var_name = "INDEXSTYLE"; kp_ist.path = DEFAULT_INDEXSTYLES; /* default path. */ kp_ist.suffix = "ist"; @@ -48,7 +44,6 @@ int main(int argc, char **argv) kp_dict.path = DEFAULT_INDEXDICTS; /* default path */ kp_dict.suffix = "dict"; KP_entry_filetype(&kp_dict); -#endif /* check options */ @@ -61,13 +56,10 @@ int main(int argc, char **argv) case 'd': if ((argv[i][2]=='\0')&&(i+1<argc)) { - i++; - dicfile=malloc(strlen(argv[i])+1); - strcpy(dicfile,argv[i]); + dicfile=xstrdup(argv[++i]); } else { - dicfile=malloc(strlen(&argv[i][2])+1); - strcpy(dicfile,&argv[i][2]); + dicfile=xstrdup(&argv[i][2]); } break; @@ -137,13 +129,10 @@ int main(int argc, char **argv) case 's': if ((argv[i][2]=='\0')&&(i+1<argc)) { - i++; - styfile=malloc(strlen(argv[i])+1); - strcpy(styfile,argv[i]); + styfile=xstrdup(argv[++i]); } else { - styfile=malloc(strlen(&argv[i][2])+1); - strcpy(styfile,&argv[i][2]); + styfile=xstrdup(&argv[i][2]); } break; @@ -198,7 +187,7 @@ int main(int argc, char **argv) cc=strlen(argv[i]); if (cc<4) cc+=4; else if (strcmp(&argv[i][cc-4],".idx")) cc+=4; - idxfile[j]=malloc(cc+1); + idxfile[j]=xmalloc(cc+1); strcpy(idxfile[j++],argv[i]); } } @@ -209,14 +198,9 @@ int main(int argc, char **argv) if (idxcount==0) idxcount=fsti=1; if (styfile==NULL) { -#ifdef KPATHSEA - envbuff=KP_get_value("INDEXDEFAULTSTYLE",NULL); -#else - envbuff=getenv("INDEXDEFAULTSTYLE"); -#endif + envbuff=kpse_var_value("INDEXDEFAULTSTYLE"); if (envbuff!=NULL) { - styfile=malloc(strlen(envbuff)+1); - strcpy(styfile,envbuff); + styfile=xstrdup(envbuff); } } @@ -295,7 +279,7 @@ int main(int argc, char **argv) lines=0; ecount=0; - ind=malloc(sizeof(struct index)); + ind=xmalloc(sizeof(struct index)); for (i=0;i<idxcount-fsti;i++) { ecount+=idxread(idxfile[i],lines); diff --git a/Build/source/texk/mendexk/mendex.h b/Build/source/texk/mendexk/mendex.h index f8a48758cb0..c071733b482 100644 --- a/Build/source/texk/mendexk/mendex.h +++ b/Build/source/texk/mendexk/mendex.h @@ -41,23 +41,19 @@ int chkcontinue(struct page *p, int num); void styread(const char *filename); /* fread.c */ -char *mfgets(char *buf, int byte, FILE *fp); +char *mfgets(char *buf, int size, FILE *fp); int idxread(char *filename, int start); /* fwrite.c */ int fprintf2 (FILE *fp, const char *format, ...); -int warn_printf(FILE *fp, const char *format, ...); -int verb_printf(FILE *fp, const char *format, ...); +void warn_printf(FILE *fp, const char *format, ...); +void verb_printf(FILE *fp, const char *format, ...); struct index; void indwrite(char *filename, struct index *ind, int pagenum); -#ifdef fprintf #undef fprintf -#endif #define fprintf fprintf2 -#ifdef fputs #undef fputs -#endif #define fputs fputs2 diff --git a/Build/source/texk/mendexk/pageread.c b/Build/source/texk/mendexk/pageread.c index 8b16e892d7a..1d6f574395b 100644 --- a/Build/source/texk/mendexk/pageread.c +++ b/Build/source/texk/mendexk/pageread.c @@ -5,6 +5,21 @@ #include <kpathsea/tex-file.h> #include "mendex.h" +static inline int +bfgetc (FILE *stream) +{ + int cc = getc (stream); + if (cc == '\r') { + cc = getc (stream); + if (cc != '\n') { + ungetc (cc, stream); + cc = '\r'; + } + } + return cc; +} +#define fgetc bfgetc + /* checking last page */ int lastpage(const char *filename) { diff --git a/Build/source/texk/mendexk/qsort.c b/Build/source/texk/mendexk/qsort.c new file mode 100644 index 00000000000..df95890e68b --- /dev/null +++ b/Build/source/texk/mendexk/qsort.c @@ -0,0 +1,206 @@ +/* + * + * This file is part of + * MakeIndex - A formatter and format independent index processor + * + * This file is public domain software donated by + * Nelson Beebe (beebe@science.utah.edu). + * + */ + +/* + * qsort.c: Our own version of the system qsort routine which is faster by an + * average of 25%, with lows and highs of 10% and 50%. The THRESHold below is + * the insertion sort threshold, and has been adjusted for records of size 48 + * bytes. The MTHREShold is where we stop finding a better median. + */ +#include "qsort.h" /* qq_compar_fn_t type and qqsort() prototype */ + +#define THRESH 4 /* threshold for insertion */ +#define MTHRESH 6 /* threshold for median */ + +static int qsz; /* size of each record */ +static int thresh; /* THRESHold in chars */ +static int mthresh; /* MTHRESHold in chars */ + +static qq_compar_fn_t qcmp; /* the comparison routine */ +static void qst(char *base, char *max); +/* + * qqsort: First, set up some global parameters for qst to share. Then, + * quicksort with qst(), and then a cleanup insertion sort ourselves. Sound + * simple? It's not... + */ + +void +qqsort(void *base, size_t n, size_t size, qq_compar_fn_t compar) +{ + register char *i; + register char *j; + register char *lo; + register char *hi; + register char *min; + register char c; + char *max; + + if (n <= 1) + return; + qsz = size; + qcmp = compar; + thresh = qsz * THRESH; + mthresh = qsz * MTHRESH; + max = (char *)base + n * qsz; + if (n >= THRESH) { + qst(base, max); + hi = (char *)base + thresh; + } else { + hi = max; + } + /* + * First put smallest element, which must be in the first THRESH, in the + * first position as a sentinel. This is done just by searching the + * first THRESH elements (or the first n if n < THRESH), finding the min, + * and swapping it into the first position. + */ + for (j = lo = base; (lo += qsz) < hi;) { + if ((*qcmp) (j, lo) > 0) + j = lo; + } + if (j != base) { /* swap j into place */ + for (i = base, hi = i + qsz; i < hi;) { + c = *j; + *j++ = *i; + *i++ = c; + } + } + /* + * With our sentinel in place, we now run the following hyper-fast + * insertion sort. For each remaining element, min, from [1] to [n-1], + * set hi to the index of the element AFTER which this one goes. Then, do + * the standard insertion sort shift on a character at a time basis for + * each element in the frob. + */ + for (min = base; (hi = min += qsz) < max;) { + while ((*qcmp) (hi -= qsz, min) > 0); + if ((hi += qsz) != min) { + for (lo = min + qsz; --lo >= min;) { + c = *lo; + for (i = j = lo; (j -= qsz) >= hi; i = j) + *i = *j; + *i = c; + } + } + } +} + + + +/* + * qst: Do a quicksort. First, find the median element, and put that one in + * the first place as the discriminator. (This "median" is just the median + * of the first, last and middle elements). (Using this median instead of + * the first element is a big win). Then, the usual partitioning/swapping, + * followed by moving the discriminator into the right place. Then, figure + * out the sizes of the two partions, do the smaller one recursively and the + * larger one via a repeat of this code. Stopping when there are less than + * THRESH elements in a partition and cleaning up with an insertion sort (in + * our caller) is a huge win. All data swaps are done in-line, which is + * space-losing but time-saving. (And there are only three places where this + * is done). + */ + +static void +qst(char *base, char *max) +{ + register char *i; + register char *j; + register char *jj; + register char *mid; + register int ii; + register char c; + void *tmp; + int lo; + int hi; + + lo = max - base; /* number of elements as chars */ + do { + /* + * At the top here, lo is the number of characters of elements in the + * current partition. (Which should be max - base). Find the median + * of the first, last, and middle element and make that the middle + * element. Set j to largest of first and middle. If max is larger + * than that guy, then it's that guy, else compare max with loser of + * first and take larger. Things are set up to prefer the middle, + * then the first in case of ties. + */ + mid = i = base + qsz * ((unsigned) (lo / qsz) >> 1); + if (lo >= mthresh) { + j = ((*qcmp) ((jj = base), i) > 0 ? jj : i); + if ((*qcmp) (j, (tmp = max - qsz)) > 0) { + /* switch to first loser */ + j = (j == jj ? i : jj); + if ((*qcmp) (j, tmp) < 0) + j = tmp; + } + if (j != i) { + ii = qsz; + do { + c = *i; + *i++ = *j; + *j++ = c; + } while (--ii); + } + } + /* Semi-standard quicksort partitioning/swapping */ + for (i = base, j = max - qsz;;) { + while (i < mid && (*qcmp) (i, mid) <= 0) + i += qsz; + while (j > mid) { + if ((*qcmp) (mid, j) <= 0) { + j -= qsz; + continue; + } + tmp = i + qsz; /* value of i after swap */ + if (i == mid) { /* j <-> mid, new mid is j */ + mid = jj = j; + } else { /* i <-> j */ + jj = j; + j -= qsz; + } + goto swap; + } + if (i == mid) { + break; + } else { /* i <-> mid, new mid is i */ + jj = mid; + tmp = mid = i; /* value of i after swap */ + j -= qsz; + } + swap: + ii = qsz; + do { + c = *i; + *i++ = *jj; + *jj++ = c; + } while (--ii); + i = tmp; + } + /* + * Look at sizes of the two partitions, do the smaller one first by + * recursion, then do the larger one by making sure lo is its size, + * base and max are updated correctly, and branching back. But only + * repeat (recursively or by branching) if the partition is of at + * least size THRESH. + */ + i = (j = mid) + qsz; + if ((lo = j - base) <= (hi = max - i)) { + if (lo >= thresh) + qst(base, j); + base = i; + lo = hi; + } else { + if (hi >= thresh) + qst(i, max); + max = j; + } + } while (lo >= thresh); +} diff --git a/Build/source/texk/mendexk/qsort.h b/Build/source/texk/mendexk/qsort.h new file mode 100644 index 00000000000..13e4972fec3 --- /dev/null +++ b/Build/source/texk/mendexk/qsort.h @@ -0,0 +1,16 @@ +/* + * + * This file is part of + * MakeIndex - A formatter and format independent index processor + * + * This file is public domain software donated by + * Nelson Beebe (beebe@science.utah.edu). + * + */ +#include <string.h> /* for size_t */ + +typedef int (*qq_compar_fn_t)(const void *, const void *); + +extern void qqsort (void *base, size_t n, size_t size, qq_compar_fn_t compar); + +#define qsort qqsort diff --git a/Build/source/texk/mendexk/sort.c b/Build/source/texk/mendexk/sort.c index f6eccbde304..7ec44785f20 100644 --- a/Build/source/texk/mendexk/sort.c +++ b/Build/source/texk/mendexk/sort.c @@ -2,7 +2,9 @@ #include <stdlib.h> #include <string.h> +#include <kpathsea/config.h> #include "mendex.h" +#include "qsort.h" #include "exkana.h" #include "exvar.h" @@ -155,7 +157,7 @@ void pagesort(struct index *ind, int num) for (i=0;i<num;i++) { if (ind[i].num==0) continue; - buff=malloc(sizeof(struct page)*(ind[i].num+1)); + buff=xmalloc(sizeof(struct page)*(ind[i].num+1)); for (j=0;j<=ind[i].num;j++) { buff[j]=ind[i].p[j]; } diff --git a/Build/source/texk/mendexk/styfile.c b/Build/source/texk/mendexk/styfile.c index 07f3f4601a7..f56a3f139d4 100644 --- a/Build/source/texk/mendexk/styfile.c +++ b/Build/source/texk/mendexk/styfile.c @@ -9,9 +9,7 @@ #include "exvar.h" -#ifdef KPATHSEA #include "kp.h" -#endif FILE *fp; @@ -23,15 +21,29 @@ static size_t sstrlen(const char *buff); static int sstrcmp(const char *s1, const char *s2); static int sstrncmp(const char *s1, const char *s2, size_t len); +static char * +bfgets (char *buf, int size, FILE *fp) +{ + char *p, *q; + p = fgets (buf, size, fp); + for (q = buf; *q; q++) { + if(*q == '\r') { + *q++ = '\n'; + *q = '\0'; + break; + } + } + return p; +} +#define fgets bfgets + /* read style file */ void styread(const char *filename) { int i,cc; char buff[4096]; -#ifdef KPATHSEA filename = KP_find_file(&kp_ist,filename); -#endif if(kpse_in_name_ok(filename)) fp=nkf_open(filename,"rb"); else |