Debian Codegraph @

texk/mendexk/main.c

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