| 1 |
#include "mendex.h" |
| 2 |
#include "version.h" |
| 3 |
#include <kpathsea/tex-file.h> |
| 4 |
#include <kpathsea/variable.h> |
| 5 |
|
| 6 |
#include "kana.h" |
| 7 |
#include "hanzi.h" |
| 8 |
#include "var.h" |
| 9 |
|
| 10 |
#include "kp.h" |
| 11 |
|
| 12 |
char *styfile[64],*idxfile[256],*indfile,*dicfile,*logfile; |
| 13 |
|
| 14 |
/* default paths */ |
| 15 |
#ifndef DEFAULT_INDEXSTYLES |
| 16 |
#define DEFAULT_INDEXSTYLES "." |
| 17 |
#endif |
| 18 |
#ifndef DEFAULT_INDEXDICTS |
| 19 |
#define DEFAULT_INDEXDICTS "." |
| 20 |
#endif |
| 21 |
KpathseaSupportInfo kp_ist,kp_dict; |
| 22 |
|
| 23 |
int main(int argc, char **argv) |
| 24 |
{ |
| 25 |
int i,j,k,cc=0,startpagenum=-1,ecount=0,chkopt=1,version=0; |
| 26 |
const char *envbuff; |
| 27 |
UVersionInfo icuVersion; |
| 28 |
char icu_version[U_MAX_VERSION_STRING_LENGTH] = ""; |
| 29 |
|
| 30 |
#ifdef WIN32 |
| 31 |
char **av; |
| 32 |
int ac; |
| 33 |
_setmaxstdio(2048); |
| 34 |
#endif |
| 35 |
kpse_set_program_name(argv[0], "upmendex"); |
| 36 |
u_getVersion(icuVersion); |
| 37 |
u_versionToString(icuVersion, icu_version); |
| 38 |
|
| 39 |
#ifdef WIN32 |
| 40 |
file_system_codepage = CP_UTF8; |
| 41 |
is_cp932_system = 0; |
| 42 |
if (get_command_line_args_utf8("utf-8", &ac, &av)) { |
| 43 |
argv = av; |
| 44 |
argc = ac; |
| 45 |
} |
| 46 |
#endif |
| 47 |
|
| 48 |
/* check options */ |
| 49 |
|
| 50 |
for (i=1,j=k=0;i<argc && j<256;i++) { |
| 51 |
if ((argv[i][0]=='-')&&(strlen(argv[i])>=2)&&chkopt) { |
| 52 |
switch (argv[i][1]) { |
| 53 |
case 'c': |
| 54 |
bcomp=1; |
| 55 |
break; |
| 56 |
|
| 57 |
case 'd': |
| 58 |
if ((argv[i][2]=='\0')&&(i+1<argc)) { |
| 59 |
dicfile=xstrdup(argv[++i]); |
| 60 |
} |
| 61 |
else { |
| 62 |
dicfile=xstrdup(&argv[i][2]); |
| 63 |
} |
| 64 |
break; |
| 65 |
|
| 66 |
case 'f': |
| 67 |
force=1; |
| 68 |
break; |
| 69 |
|
| 70 |
case 'g': |
| 71 |
gflg=1; |
| 72 |
break; |
| 73 |
|
| 74 |
case 'i': |
| 75 |
fsti=1; |
| 76 |
break; |
| 77 |
|
| 78 |
case 'l': |
| 79 |
lorder=1; |
| 80 |
break; |
| 81 |
|
| 82 |
case 'o': |
| 83 |
if ((argv[i][2]=='\0')&&(i+1<argc)) { |
| 84 |
indfile=xstrdup(argv[++i]); |
| 85 |
} |
| 86 |
else { |
| 87 |
indfile=xstrdup(&argv[i][2]); |
| 88 |
} |
| 89 |
break; |
| 90 |
|
| 91 |
case 'p': |
| 92 |
if ((argv[i][2]=='\0')&&(i+1<argc)) { |
| 93 |
i++; |
| 94 |
if (strcmp(argv[i],"any")==0) fpage=2; |
| 95 |
else if (strcmp(argv[i],"odd")==0) fpage=3; |
| 96 |
else if (strcmp(argv[i],"even")==0) fpage=4; |
| 97 |
else { |
| 98 |
fpage=1; |
| 99 |
startpagenum=atoi(argv[i]); |
| 100 |
} |
| 101 |
} |
| 102 |
else { |
| 103 |
if (strcmp(&argv[i][2],"any")==0) fpage=2; |
| 104 |
else if (strcmp(&argv[i][2],"odd")==0) fpage=3; |
| 105 |
else if (strcmp(&argv[i][2],"even")==0) fpage=4; |
| 106 |
else { |
| 107 |
fpage=1; |
| 108 |
startpagenum=atoi(&argv[i][2]); |
| 109 |
} |
| 110 |
} |
| 111 |
break; |
| 112 |
|
| 113 |
case 'q': |
| 114 |
verb=0; |
| 115 |
break; |
| 116 |
|
| 117 |
case 't': |
| 118 |
if ((argv[i][2]=='\0')&&(i+1<argc)) { |
| 119 |
logfile=xstrdup(argv[++i]); |
| 120 |
} |
| 121 |
else { |
| 122 |
logfile=xstrdup(&argv[i][2]); |
| 123 |
} |
| 124 |
break; |
| 125 |
|
| 126 |
case 'r': |
| 127 |
prange=0; |
| 128 |
break; |
| 129 |
|
| 130 |
case 's': |
| 131 |
if (k==64) { |
| 132 |
fprintf (stderr, "Too many style files.\n"); |
| 133 |
exit(255); |
| 134 |
} |
| 135 |
if ((argv[i][2]=='\0')&&(i+1<argc)) { |
| 136 |
styfile[k]=xstrdup(argv[++i]); |
| 137 |
} |
| 138 |
else { |
| 139 |
styfile[k]=xstrdup(&argv[i][2]); |
| 140 |
} |
| 141 |
k++; |
| 142 |
break; |
| 143 |
|
| 144 |
case 'v': |
| 145 |
debug=1; |
| 146 |
break; |
| 147 |
|
| 148 |
case '-': |
| 149 |
if (strlen(argv[i])==2) chkopt=0; |
| 150 |
if (strcmp(argv[i],"--version")==0) version=1; |
| 151 |
if (strcmp(argv[i],"--help")!=0 && strcmp(argv[i],"--version")!=0) break; |
| 152 |
|
| 153 |
default: |
| 154 |
fprintf(stderr,"upmendex - index processor, %s (%s).\n",VERSION, TL_VERSION); |
| 155 |
fprintf(stderr," Copyright 2009 ASCII MEDIA WORKS, 2015-2026 TANAKA Takuji\n"); |
| 156 |
fprintf(stderr," using ICU version %s\n",icu_version); |
| 157 |
if (version) exit(0); |
| 158 |
fprintf(stderr,"usage:\n"); |
| 159 |
fprintf(stderr,"%% upmendex [-ilqrcgf] [-s sty] [-d dic] [-o ind] [-t log] [-p no] [--] [idx0 idx1 ...]\n"); |
| 160 |
fprintf(stderr,"options:\n"); |
| 161 |
fprintf(stderr,"-i use stdin as the input file.\n"); |
| 162 |
fprintf(stderr,"-l use letter ordering.\n"); |
| 163 |
fprintf(stderr,"-q quiet mode.\n"); |
| 164 |
fprintf(stderr,"-r disable implicit page formation.\n"); |
| 165 |
fprintf(stderr,"-c compress blanks. (ignore leading and trailing blanks.)\n"); |
| 166 |
fprintf(stderr,"-g make Japanese index head <%s>.\n", AKASATANAutf8); |
| 167 |
fprintf(stderr,"-f force to output unknown scripts.\n"); |
| 168 |
fprintf(stderr,"-s sty take sty as style file.\n"); |
| 169 |
fprintf(stderr,"-d dic take dic as dictionary file.\n"); |
| 170 |
fprintf(stderr,"-o ind take ind as the output index file.\n"); |
| 171 |
fprintf(stderr,"-t log take log as the error log file.\n"); |
| 172 |
fprintf(stderr,"-p no set the starting page number of index.\n"); |
| 173 |
fprintf(stderr,"idx... input files.\n"); |
| 174 |
fprintf(stderr,"\nEmail bug reports to %s.\n", BUG_ADDRESS); |
| 175 |
exit(0); |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
else { |
| 180 |
cc=strlen(argv[i])+6; |
| 181 |
idxfile[j]=xmalloc(cc); |
| 182 |
strcpy(idxfile[j++],argv[i]); |
| 183 |
} |
| 184 |
} |
| 185 |
idxcount=j+fsti; |
| 186 |
|
| 187 |
kp_ist.var_name = "INDEXSTYLE"; |
| 188 |
kp_ist.path = DEFAULT_INDEXSTYLES; /* default path. */ |
| 189 |
kp_ist.suffix = "ist"; |
| 190 |
KP_entry_filetype(&kp_ist); |
| 191 |
kp_dict.var_name = "INDEXDICTIONARY"; |
| 192 |
kp_dict.path = DEFAULT_INDEXDICTS; /* default path */ |
| 193 |
kp_dict.suffix = "dict"; |
| 194 |
KP_entry_filetype(&kp_dict); |
| 195 |
|
| 196 |
/* check option errors */ |
| 197 |
|
| 198 |
if (idxcount==0) idxcount=fsti=1; |
| 199 |
|
| 200 |
if (styfile[0]==NULL) { |
| 201 |
envbuff=kpse_var_value("INDEXDEFAULTSTYLE"); |
| 202 |
if (envbuff!=NULL) { |
| 203 |
styfile[0]=xstrdup(envbuff); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if (!indfile &&(idxcount-fsti>0)) { |
| 208 |
indfile=xmalloc(strlen(idxfile[0])+6); |
| 209 |
for (i=strlen(idxfile[0]);i>=0;i--) { |
| 210 |
if (idxfile[0][i]=='.') { |
| 211 |
strncpy(indfile,idxfile[0],i); |
| 212 |
sprintf(&indfile[i],".ind"); |
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
if (i==-1) sprintf(indfile,"%s.ind",idxfile[0]); |
| 217 |
} |
| 218 |
|
| 219 |
if (!logfile && (idxcount-fsti > 0)) { |
| 220 |
logfile=xmalloc(strlen(idxfile[0])+6); |
| 221 |
for (i=strlen(idxfile[0]);i>=0;i--) { |
| 222 |
if (idxfile[0][i]=='.') { |
| 223 |
strncpy(logfile,idxfile[0],i); |
| 224 |
sprintf(&logfile[i],".ilg"); |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
if (i==-1) sprintf(logfile,"%s.ilg",idxfile[0]); |
| 229 |
} |
| 230 |
if (logfile && kpse_out_name_ok(logfile)) |
| 231 |
efp=fopen(logfile,"wb"); |
| 232 |
if(efp == NULL) { |
| 233 |
efp=stderr; |
| 234 |
logfile=xstrdup("stderr"); |
| 235 |
} |
| 236 |
|
| 237 |
if (strcmp(argv[0],"makeindex")==0) { |
| 238 |
verb_printf(efp,"This is Not `MAKEINDEX\', But `UPMENDEX\' %s [ICU %s] (%s).\n", |
| 239 |
VERSION, icu_version, TL_VERSION); |
| 240 |
} |
| 241 |
else { |
| 242 |
verb_printf(efp,"This is upmendex %s [ICU %s] (%s).\n", |
| 243 |
VERSION, icu_version, TL_VERSION); |
| 244 |
} |
| 245 |
|
| 246 |
/* init hangul,devanagari,thai *_head table */ |
| 247 |
u_strcpy(hangul_head,GANADA); |
| 248 |
u_strcpy(brahmic_head[BR_DEVA],DEVA_HEAD); |
| 249 |
u_strcpy(brahmic_head[BR_BENG],BENG_HEAD); |
| 250 |
u_strcpy(brahmic_head[BR_GURU],GURU_HEAD); |
| 251 |
u_strcpy(brahmic_head[BR_GUJR],GUJR_HEAD); |
| 252 |
u_strcpy(brahmic_head[BR_ORYA],ORYA_HEAD); |
| 253 |
u_strcpy(brahmic_head[BR_TAML],TAML_HEAD); |
| 254 |
u_strcpy(brahmic_head[BR_TELU],TELU_HEAD); |
| 255 |
u_strcpy(brahmic_head[BR_KNDA],KNDA_HEAD); |
| 256 |
u_strcpy(brahmic_head[BR_MLYM],MLYM_HEAD); |
| 257 |
u_strcpy(brahmic_head[BR_SINH],SINH_HEAD); |
| 258 |
u_strcpy(brahmic_head[BR_THAI],THAI_HEAD); |
| 259 |
u_strcpy(brahmic_head[BR_LAO ],LAO_HEAD ); |
| 260 |
|
| 261 |
for (k=0;styfile[k]!=NULL;k++) { |
| 262 |
styread(styfile[k]); |
| 263 |
} |
| 264 |
|
| 265 |
set_icu_attributes(); |
| 266 |
init_icu_collator(); |
| 267 |
|
| 268 |
/* init kanatable */ |
| 269 |
|
| 270 |
initkanatable(); |
| 271 |
|
| 272 |
/* read dictionary */ |
| 273 |
|
| 274 |
ecount+=dicread(dicfile); |
| 275 |
|
| 276 |
switch (letter_head) { |
| 277 |
case 0: |
| 278 |
case 1: |
| 279 |
if (gflg==1) { |
| 280 |
u_strcpy(atama,akasatana); |
| 281 |
} |
| 282 |
else { |
| 283 |
u_strcpy(atama,aiueo); |
| 284 |
} |
| 285 |
break; |
| 286 |
|
| 287 |
case 2: |
| 288 |
if (gflg==1) { |
| 289 |
u_strcpy(atama,AKASATANA); |
| 290 |
} |
| 291 |
else { |
| 292 |
u_strcpy(atama,AIUEO); |
| 293 |
} |
| 294 |
break; |
| 295 |
|
| 296 |
default: |
| 297 |
break; |
| 298 |
} |
| 299 |
if (u_strlen(kana_head)==0) u_strcpy(kana_head,atama); |
| 300 |
|
| 301 |
/* read idx file */ |
| 302 |
|
| 303 |
lines=0; |
| 304 |
ecount=0; |
| 305 |
ind=xmalloc(sizeof(struct index)); |
| 306 |
|
| 307 |
for (i=0;i<idxcount-fsti;i++) { |
| 308 |
ecount+=idxread(idxfile[i],lines); |
| 309 |
} |
| 310 |
if (fsti==1) { |
| 311 |
ecount+=idxread(NULL,lines); |
| 312 |
} |
| 313 |
verb_printf(efp,"%d entries accepted, %d rejected.\n",acc,reject); |
| 314 |
|
| 315 |
if (ecount!=0) { |
| 316 |
verb_printf(efp,"%d errors, written in %s.\n",ecount,logfile); |
| 317 |
lines=0; |
| 318 |
} |
| 319 |
if (lines==0) { |
| 320 |
verb_printf(efp,"Nothing written in output file.\n"); |
| 321 |
if (efp!=stderr) fclose(efp); |
| 322 |
exit(255); |
| 323 |
} |
| 324 |
|
| 325 |
/* sort index */ |
| 326 |
|
| 327 |
verb_printf(efp,"Sorting index."); |
| 328 |
|
| 329 |
scount=0; |
| 330 |
wsort(ind,lines); |
| 331 |
|
| 332 |
verb_printf(efp,"...done(%d comparisons).\n",scount); |
| 333 |
|
| 334 |
/* sort pages */ |
| 335 |
|
| 336 |
verb_printf(efp,"Sorting pages."); |
| 337 |
|
| 338 |
scount=0; |
| 339 |
pagesort(ind,lines); |
| 340 |
|
| 341 |
verb_printf(efp,"...done(%d comparisons).\n",scount); |
| 342 |
|
| 343 |
/* get last page */ |
| 344 |
|
| 345 |
if ((fpage>1)&&(idxcount-fsti>0)) cc=lastpage(idxfile[0]); |
| 346 |
|
| 347 |
switch (fpage) { |
| 348 |
case 2: |
| 349 |
startpagenum=cc+1; |
| 350 |
break; |
| 351 |
|
| 352 |
case 3: |
| 353 |
if ((cc+1)%2==0) startpagenum=cc+2; |
| 354 |
else startpagenum=cc+1; |
| 355 |
break; |
| 356 |
|
| 357 |
case 4: |
| 358 |
if ((cc+1)%2==1) startpagenum=cc+2; |
| 359 |
else startpagenum=cc+1; |
| 360 |
break; |
| 361 |
|
| 362 |
default: |
| 363 |
break; |
| 364 |
} |
| 365 |
|
| 366 |
/* write indfile */ |
| 367 |
|
| 368 |
verb_printf(efp,"Making index file."); |
| 369 |
|
| 370 |
indwrite(indfile,ind,startpagenum); |
| 371 |
|
| 372 |
verb_printf(efp,"...done.\n"); |
| 373 |
|
| 374 |
if (idxcount-fsti==0) indfile=xstrdup("stdout"); |
| 375 |
|
| 376 |
verb_printf(efp,"%d warnings, written in %s.\n",warn,logfile); |
| 377 |
verb_printf(efp,"Output written in %s.\n",indfile); |
| 378 |
if (efp!=stderr) fclose(efp); |
| 379 |
|
| 380 |
return 0; |
| 381 |
} |