File: main.c

package info (click to toggle)
texlive-bin 2016.20160513.41080.dfsg-2+deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 155,060 kB
  • sloc: ansic: 702,862; cpp: 222,090; perl: 76,014; sh: 23,402; makefile: 8,078; lex: 4,710; pascal: 3,782; python: 3,614; java: 3,569; yacc: 2,905; exp: 2,031; xml: 800; tcl: 670; ruby: 620; lisp: 397; sed: 351; asm: 140; csh: 48; awk: 30
file content (409 lines) | stat: -rw-r--r-- 8,715 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "mendex.h"
#include <kpathsea/tex-file.h>
#include <kpathsea/variable.h>
#include <ptexenc/ptexenc.h>

#include "kana.h"
#include "var.h"

#include "kp.h"

char *styfile,*idxfile[256],*indfile,*dicfile,*logfile;

/* default paths */
#ifndef DEFAULT_INDEXSTYLES
#define DEFAULT_INDEXSTYLES "."
#endif
#ifndef DEFAULT_INDEXDICTS
#define DEFAULT_INDEXDICTS "."
#endif
KpathseaSupportInfo kp_ist,kp_dict;

#define VERSION "version 2.6f [14-Aug-2009]"

int main(int argc, char **argv)
{
	int i,j,cc=0,startpagenum=-1,ecount=0,chkopt=1;
	const char *envbuff;
	char *p;

	enable_UPTEX (true); /* enable */

#ifdef WIN32
	_setmaxstdio(2048);
        set_enc_string("sjis", "euc");
        sjisterminal = 0;
#else
        set_enc_string(NULL, "euc");
#endif
	kpse_set_program_name(argv[0], "mendex");

	p = getenv ("PTEX_KANJI_ENC");
	if (p) {
		if (!set_enc_string (p, NULL))
			fprintf (stderr, "Ignoring bad kanji encoding \"%s\".\n", p);
	}

#ifdef WIN32
	p = kpse_var_value ("guess_input_kanji_encoding");
	if (p) {
		if (*p == '1' || *p == 'y' || *p == 't')
			infile_enc_auto = 1;
		free(p);
	}
#endif

	kp_ist.var_name = "INDEXSTYLE";
	kp_ist.path = DEFAULT_INDEXSTYLES; /* default path. */
	kp_ist.suffix = "ist";
	KP_entry_filetype(&kp_ist);
	kp_dict.var_name = "INDEXDICTIONARY";
	kp_dict.path = DEFAULT_INDEXDICTS; /* default path */
	kp_dict.suffix = "dict";
	KP_entry_filetype(&kp_dict);

/*   check options   */

	for (i=1,j=0;i<argc && j<256;i++) {
		if ((argv[i][0]=='-')&&(strlen(argv[i])>=2)&&chkopt) {
			switch (argv[i][1]) {
			case 'c':
				bcomp=1;
				break;

			case 'd':
				if ((argv[i][2]=='\0')&&(i+1<argc)) {
					dicfile=xstrdup(argv[++i]);
				}
				else {
					dicfile=xstrdup(&argv[i][2]);
				}
				break;

			case 'f':
				force=1;
				break;

			case 'g':
				gflg=1;
				break;

			case 'i':
				fsti=1;
				break;

			case 'l':
				lorder=1;
				break;

			case 'o':
				if ((argv[i][2]=='\0')&&(i+1<argc)) {
					indfile=xstrdup(argv[++i]);
				}
				else {
					indfile=xstrdup(&argv[i][2]);
				}
				break;

			case 'p':
				if ((argv[i][2]=='\0')&&(i+1<argc)) {
					i++;
					if (strcmp(argv[i],"any")==0) fpage=2;
					else if (strcmp(argv[i],"odd")==0) fpage=3;
					else if (strcmp(argv[i],"even")==0) fpage=4;
					else {
						fpage=1;
						startpagenum=atoi(argv[i]);
					}
				}
				else {
					if (strcmp(&argv[i][2],"any")==0) fpage=2;
					else if (strcmp(&argv[i][2],"odd")==0) fpage=3;
					else if (strcmp(&argv[i][2],"even")==0) fpage=4;
					else {
						fpage=1;
						startpagenum=atoi(&argv[i][2]);
					}
				}
				break;

			case 'q':
				verb=0;
				break;

			case 't':
				if ((argv[i][2]=='\0')&&(i+1<argc)) {
					logfile=xstrdup(argv[++i]);
				}
				else {
					logfile=xstrdup(&argv[i][2]);
				}
				break;

			case 'r':
				prange=0;
				break;

			case 's':
				if ((argv[i][2]=='\0')&&(i+1<argc)) {
					styfile=xstrdup(argv[++i]);
				}
				else {
					styfile=xstrdup(&argv[i][2]);
				}
				break;

			case 'v':
				debug=1;
				break;

			case 'E':
				set_enc_string("EUC", NULL);
				break;

			case 'J':
				set_enc_string("JIS", NULL);
				break;

			case 'S':
				set_enc_string("SJIS", NULL);
				break;

#ifdef WIN32
			case 'T':
				sjisterminal = 1;
				break;
#endif

			case 'U':
				set_enc_string("UTF8", "uptex");
				break;


			case 'I':
				if ((argv[i][2]=='\0')&&(i+1<argc)) {
					i++;
					if (strcmp(argv[i],"euc")==0) set_enc_string(NULL, "euc");
					else if (strcmp(argv[i],"utf8")==0) set_enc_string(NULL, "uptex");
				}
				else {
					if (strcmp(&argv[i][2],"euc")==0) set_enc_string(NULL, "euc");
					else if (strcmp(&argv[i][2],"utf8")==0) set_enc_string(NULL, "uptex");
				}
				break;

			case '-':
				if (strlen(argv[i])==2) chkopt=0;
				if (strcmp(argv[i],"--help")!=0) break;

			default:
				fprintf(stderr,"mendex - Japanese index processor, %s (%s) (%s).\n",VERSION, get_enc_string(), TL_VERSION);
				fprintf(stderr," Copyright 2009 ASCII MEDIA WORKS.\n");
				fprintf(stderr,"usage:\n");
				fprintf(stderr,"%% mendex [-ilqrcgfEJS"
#ifdef WIN32
					       "T"
#endif
					       "U] [-s sty] [-d dic] [-o ind] [-t log] [-p no] [-I enc] [--] [idx0 idx1 ...]\n");
				fprintf(stderr,"options:\n");
				fprintf(stderr,"-i      use stdin as the input file.\n");
				fprintf(stderr,"-l      use letter ordering.\n");
				fprintf(stderr,"-q      quiet mode.\n");
				fprintf(stderr,"-r      disable implicit page formation.\n");
				fprintf(stderr,"-c      compress blanks. (ignore leading and trailing blanks.)\n");
				fprintf(stderr,"-g      make Japanese index head <%s>.\n",
							is_internalUPTEX() ? AKASATANAutf8 : AKASATANA);
				fprintf(stderr,"-f      force to output kanji.\n");
				fprintf(stderr,"-s sty  take sty as style file.\n");
				fprintf(stderr,"-d dic  take dic as dictionary file.\n");
				fprintf(stderr,"-o ind  take ind as the output index file.\n");
				fprintf(stderr,"-t log  take log as the error log file.\n");
				fprintf(stderr,"-p no   set the starting page number of index.\n");
				fprintf(stderr,"-E      EUC mode.\n");
				fprintf(stderr,"-J      JIS mode.\n");
				fprintf(stderr,"-S      ShiftJIS mode.\n");
#ifdef WIN32
				fprintf(stderr,"-T      ShiftJIS terminal.\n");
#endif
				fprintf(stderr,"-U      UTF-8 mode.\n");
				fprintf(stderr,"-I enc  internal encoding for keywords (enc: euc or utf8).\n");
				fprintf(stderr,"idx...  input files.\n");
				exit(0);
				break;
			}
		}
		else {
			cc=strlen(argv[i]);
			if (cc<4) cc+=4;
			else if (strcmp(&argv[i][cc-4],".idx")) cc+=4;
			idxfile[j]=xmalloc(cc+1);
			strcpy(idxfile[j++],argv[i]);
		}
	}
	idxcount=j+fsti;

/*   check option errors   */

	if (idxcount==0) idxcount=fsti=1;

	if (styfile==NULL) {
		envbuff=kpse_var_value("INDEXDEFAULTSTYLE");
		if (envbuff!=NULL) {
			styfile=xstrdup(envbuff);
		}
	}

	if (!indfile &&(idxcount-fsti>0)) {
		indfile=xmalloc(strlen(idxfile[0]+6));
		for (i=strlen(idxfile[0]);i>=0;i--) {
			if (idxfile[0][i]=='.') {
				strncpy(indfile,idxfile[0],i);
				sprintf(&indfile[i],".ind");
				break;
			}
		}
		if (i==-1) sprintf(indfile,"%s.ind",idxfile[0]);
	}

	if (!logfile && (idxcount-fsti > 0)) {
		logfile=xmalloc(strlen(idxfile[0]+6));
		for (i=strlen(idxfile[0]);i>=0;i--) {
			if (idxfile[0][i]=='.') {
				strncpy(logfile,idxfile[0],i);
				sprintf(&logfile[i],".ilg");
				break;
			}
		}
		if (i==-1) sprintf(logfile,"%s.ilg",idxfile[0]);
		}
	if (logfile && kpse_out_name_ok(logfile))
		efp=fopen(logfile,"wb");
	if(efp == NULL) {
		efp=stderr;
		logfile=xstrdup("stderr");
	}

	if (styfile!=NULL) styread(styfile);

	if (strcmp(argv[0],"makeindex")==0) {
		verb_printf(efp,"This is Not `MAKEINDEX\', But `MENDEX\' %s (%s) (%s).\n",
			    VERSION, get_enc_string(), TL_VERSION);
	}
	else {
		verb_printf(efp,"This is mendex %s (%s) (%s).\n",
			    VERSION, get_enc_string(), TL_VERSION);
	}

/*   init kanatable   */

	initkanatable();

/*   read dictionary   */

	ecount+=dicread(dicfile);

	switch (letter_head) {
	case 0:
	case 1:
		if (gflg==1) {
			strcpy(atama,akasatana);
		}
		else {
			strcpy(atama,aiueo);
		}
		break;

	case 2:
		if (gflg==1) {
			strcpy(atama,AKASATANA);
		}
		else {
			strcpy(atama,AIUEO);
		}
		break;

	default:
		break;
	}

/*   read idx file   */

	lines=0;
	ecount=0;
	ind=xmalloc(sizeof(struct index));

	for (i=0;i<idxcount-fsti;i++) {
		ecount+=idxread(idxfile[i],lines);
	}
	if (fsti==1) {
		ecount+=idxread(NULL,lines);
	}
	verb_printf(efp,"%d entries accepted, %d rejected.\n",acc,reject);

	if (ecount!=0) {
		verb_printf(efp,"%d errors, written in %s.\n",ecount,logfile);
		lines=0;
	}
	if (lines==0) {
		verb_printf(efp,"Nothing written in output file.\n");
		if (efp!=stderr) fclose(efp);
		exit(255);
	}

/*   sort index   */

	verb_printf(efp,"Sorting index.");

	scount=0;
	wsort(ind,lines);

	verb_printf(efp,"...done(%d comparisons).\n",scount);

/*   sort pages   */

	verb_printf(efp,"Sorting pages.");

	scount=0;
	pagesort(ind,lines);

	verb_printf(efp,"...done(%d comparisons).\n",scount);

/*   get last page   */

	if ((fpage>1)&&(idxcount-fsti>0)) cc=lastpage(idxfile[0]);

	switch (fpage) {
	case 2:
		startpagenum=cc+1;
		break;

	case 3:
		if ((cc+1)%2==0) startpagenum=cc+2;
		else startpagenum=cc+1;
		break;

	case 4:
		if ((cc+1)%2==1) startpagenum=cc+2;
		else startpagenum=cc+1;
		break;
		
	default:
		break;
	}

/*   write indfile   */

	verb_printf(efp,"Making index file.");

	indwrite(indfile,ind,startpagenum);

	verb_printf(efp,"...done.\n");

	if (idxcount-fsti==0) indfile=xstrdup("stdout");

	verb_printf(efp,"%d warnings, written in %s.\n",warn,logfile);
	verb_printf(efp,"Output written in %s.\n",indfile);
	if (efp!=stderr) fclose(efp);

	return 0;
}