Debian Codegraph @

texk/mendexk/fwrite.c

raw
1 #include "mendex.h"
2
3 #include <stdarg.h>
4
5 #include <kpathsea/tex-file.h>
6 #include <ptexenc/ptexenc.h>
7 #include <ptexenc/unicode.h>
8 #include <ptexenc/unicode-jp.h>
9
10 #include "exkana.h"
11 #include "exvar.h"
12
13 int line_length=0;
14
15 static void printpage(struct index *ind, FILE *fp, int num, char *lbuff);
16 static int range_check(struct index ind, int count, char *lbuff, FILE *fp);
17 static void linecheck(char *lbuff, char *tmpbuff, FILE *fp, int force);
18 static void crcheck(char *lbuff, FILE *fp);
19
20 /* All buffers have size BUFFERLEN. */
21 #define BUFFERLEN 4096
22
23 #ifdef HAVE___VA_ARGS__
24 /* Use C99 variadic macros if they are supported. */
25 /* We would like to use sizeof(buf) instead of BUFFERLEN but that fails
26 for, e.g., gcc-4.8.3 on Cygwin and gcc-4.5.3 on NetBSD. */
27 #define SPRINTF(buf, ...) \
28 snprintf(buf, BUFFERLEN, __VA_ARGS__)
29 #define SAPPENDF(buf, ...) \
30 snprintf(buf + strlen(buf), BUFFERLEN - strlen(buf), __VA_ARGS__)
31 #else
32 /* Alternatively use static inline functions. */
33 static inline int SPRINTF(char *buf, const char *format, ...)
34 {
35 va_list argptr;
36 int n;
37
38 va_start(argptr, format);
39 n = vsnprintf(buf, BUFFERLEN, format, argptr);
40 va_end(argptr);
41
42 return n;
43 }
44 static inline int SAPPENDF(char *buf, const char *format, ...)
45 {
46 va_list argptr;
47 int n;
48
49 va_start(argptr, format);
50 n = vsnprintf(buf + strlen(buf), BUFFERLEN - strlen(buf), format, argptr);
51 va_end(argptr);
52
53 return n;
54 }
55 #endif
56
57 static void fprint_euc_char(FILE *fp, const char a, const char b)
58 {
59 if (is_internalUPTEX()) { /* convert a character from EUC to UTF8 */
60 int k = 0;
61 unsigned char str[5];
62 int chr = (unsigned char)a<<8 | (unsigned char)b;
63 chr = (chr==0xffff) ? U_REPLACEMENT_CHARACTER : JIStoUCS2(chr & 0x7f7f);
64 chr = UCStoUTF8(chr);
65 /* if (BYTE1(chr) != 0){ str[k++] = BYTE1(chr); } */ /* do not happen */
66 if (BYTE2(chr) != 0){ str[k++] = BYTE2(chr); }
67 if (BYTE3(chr) != 0){ str[k++] = BYTE3(chr); }
68 str[k++] = BYTE4(chr);
69 str[k++] = '\0';
70 fprintf(fp,"%s",str);
71 }
72 else
73 fprintf(fp,"%c%c",a,b);
74 }
75
76 /* fprintf with convert kanji code */
77 int fprintf2(FILE *fp, const char *format, ...)
78 {
79 char print_buff[8000];
80 va_list argptr;
81 int n;
82
83 va_start(argptr, format);
84 n = vsnprintf(print_buff, sizeof print_buff, format, argptr);
85 va_end(argptr);
86
87 fputs(print_buff, fp);
88 return n;
89 }
90
91 void warn_printf(FILE *fp, const char *format, ...)
92 {
93 char print_buff[8000];
94 va_list argptr;
95
96 va_start(argptr, format);
97 vsnprintf(print_buff, sizeof print_buff, format, argptr);
98 va_end(argptr);
99
100 warn++;
101 fputs(print_buff, stderr);
102 if (fp!=stderr) fputs(print_buff, fp);
103 }
104
105 void verb_printf(FILE *fp, const char *format, ...)
106 {
107 char print_buff[8000];
108 va_list argptr;
109
110 va_start(argptr, format);
111 vsnprintf(print_buff, sizeof print_buff, format, argptr);
112 va_end(argptr);
113
114 if (verb!=0) fputs(print_buff, stderr);
115 if (fp!=stderr) fputs(print_buff, fp);
116 }
117
118 static int pnumconv2(struct page *p)
119 {
120 int j,k,cc,pclen;
121
122 pclen=strlen(page_compositor);
123 for (j=k=cc=0;j<strlen(p->page);j++) {
124 if (strncmp(p->page+j,page_compositor,pclen)==0) {
125 j+=pclen;
126 k=j;
127 cc++;
128 continue;
129 }
130 }
131 return pnumconv(p->page+k,p->attr[cc]);
132 }
133
134
135 /* write ind file */
136 void indwrite(char *filename, struct index *ind, int pagenum)
137 {
138 int i,j,hpoint=0;
139 char datama[2048],lbuff[BUFFERLEN];
140 FILE *fp=NULL;
141 int conv_euc_to_euc;
142 char *init, *init_prev;
143
144 if (filename && kpse_out_name_ok(filename))
145 fp=fopen(filename,"wb");
146 if (fp == NULL) {
147 fp=stdout;
148 #ifdef WIN32
149 setmode(fileno(fp), _O_BINARY);
150 #endif
151 }
152
153 conv_euc_to_euc = is_internalUPTEX() ? 1 : 0;
154 if (conv_euc_to_euc) set_enc_string(NULL, "euc");
155 convert(atama,datama);
156 if (conv_euc_to_euc) set_enc_string(NULL, "uptex");
157 fputs(preamble,fp);
158
159 if (fpage>0) {
160 fprintf(fp,"%s%d%s",setpage_prefix,pagenum,setpage_suffix);
161 }
162
163 if (strlen(symhead)==0) {
164 if (lethead_flag>0) {
165 strcpy(symhead, symhead_positive);
166 }
167 else if (lethead_flag<0) {
168 strcpy(symhead, symhead_negative);
169 }
170 }
171 {
172 if (lethead_flag>0) {
173 strcpy(numhead, numhead_positive);
174 }
175 else if (lethead_flag<0) {
176 strcpy(numhead, numhead_negative);
177 }
178 }
179
180 for (i=line_length=0;i<lines;i++) {
181 init = ind[i].dic[0];
182 if (i==0) {
183 if (alphabet(init)) {
184 if (lethead_flag>0) {
185 fprintf(fp,"%s%c%s",lethead_prefix,init[0],lethead_suffix);
186 }
187 else if (lethead_flag<0) {
188 fprintf(fp,"%s%c%s",lethead_prefix,init[0]+32,lethead_suffix);
189 }
190 SPRINTF(lbuff,"%s%s",item_0,ind[i].idx[0]);
191 }
192 else if (japanese(init)) {
193 if (lethead_flag) {
194 fputs(lethead_prefix,fp);
195 for (j=hpoint;j<(strlen(datama)/2);j++) {
196 if ((unsigned char)init[1]<(unsigned char)datama[j*2+1]) {
197 fprint_euc_char(fp,atama[(j-1)*2],atama[(j-1)*2+1]);
198 hpoint=j;
199 break;
200 }
201 }
202 if (j==(strlen(datama)/2)) {
203 fprint_euc_char(fp,atama[(j-1)*2],atama[(j-1)*2+1]);
204 }
205 fputs(lethead_suffix,fp);
206 }
207 SPRINTF(lbuff,"%s%s",item_0,ind[i].idx[0]);
208 for (hpoint=0;hpoint<(strlen(datama)/2);hpoint++) {
209 if ((unsigned char)init[1]<(unsigned char)datama[hpoint*2+1]) {
210 break;
211 }
212 }
213 }
214 else {
215 if (lethead_flag!=0 && symbol_flag==2 && numeric(init)) {
216 fprintf(fp,"%s%s%s",lethead_prefix,numhead,lethead_suffix);
217 }
218 if (lethead_flag!=0 && (symbol_flag==1 || (symbol_flag==2 && !numeric(init)))) {
219 fprintf(fp,"%s%s%s",lethead_prefix,symhead,lethead_suffix);
220 }
221 SPRINTF(lbuff,"%s%s",item_0,ind[i].idx[0]);
222 }
223 switch (ind[i].words) {
224 case 1:
225 SAPPENDF(lbuff,"%s",delim_0);
226 break;
227
228 case 2:
229 SAPPENDF(lbuff,"%s",item_x1);
230 SAPPENDF(lbuff,"%s",ind[i].idx[1]);
231 SAPPENDF(lbuff,"%s",delim_1);
232 break;
233
234 case 3:
235 SAPPENDF(lbuff,"%s",item_x1);
236 SAPPENDF(lbuff,"%s",ind[i].idx[1]);
237 SAPPENDF(lbuff,"%s",item_x2);
238 SAPPENDF(lbuff,"%s",ind[i].idx[2]);
239 SAPPENDF(lbuff,"%s",delim_2);
240 break;
241
242 default:
243 break;
244 }
245 printpage(ind,fp,i,lbuff);
246 }
247 else {
248 init_prev = ind[i-1].dic[0];
249 if (alphabet(init)) {
250 if (init[0]!=init_prev[0]) {
251 fputs(group_skip,fp);
252 if (lethead_flag>0) {
253 fprintf(fp,"%s%c%s",lethead_prefix,init[0],lethead_suffix);
254 }
255 else if (lethead_flag<0) {
256 fprintf(fp,"%s%c%s",lethead_prefix,init[0]+32,lethead_suffix);
257 }
258 }
259 }
260 else if (japanese(init)) {
261 for (j=hpoint;j<(strlen(datama)/2);j++) {
262 if (((unsigned char)init[0]<=(unsigned char)datama[j*2])&&((unsigned char)init[1]<(unsigned char)datama[j*2+1])) {
263 break;
264 }
265 }
266 if ((j!=hpoint)||(j==0)) {
267 hpoint=j;
268 fputs(group_skip,fp);
269 if (lethead_flag!=0) {
270 fputs(lethead_prefix,fp);
271 fprint_euc_char(fp,atama[(j-1)*2],atama[(j-1)*2+1]);
272 fputs(lethead_suffix,fp);
273 }
274 }
275 }
276 else {
277 if ( alphabet(init_prev) || japanese(init_prev) ||
278 (!numeric(init_prev)&&numeric(init)) || (numeric(init_prev)&&!numeric(init)) ) {
279 if (alphabet(init_prev) || japanese(init_prev) || symbol_flag==2)
280 fputs(group_skip,fp);
281 if (lethead_flag!=0 && symbol_flag==2 && numeric(init)) {
282 fprintf(fp,"%s%s%s",lethead_prefix,numhead,lethead_suffix);
283 }
284 if (lethead_flag!=0 && (symbol_flag==1 && (alphabet(init_prev)||japanese(init_prev)) ||
285 symbol_flag==2 && !numeric(init)) ) {
286 fprintf(fp,"%s%s%s",lethead_prefix,symhead,lethead_suffix);
287 }
288 }
289 }
290
291 switch (ind[i].words) {
292 case 1:
293 SAPPENDF(lbuff,"%s",item_0);
294 SAPPENDF(lbuff,"%s",ind[i].idx[0]);
295 SAPPENDF(lbuff,"%s",delim_0);
296 break;
297
298 case 2:
299 if (strcmp(ind[i-1].idx[0],ind[i].idx[0])!=0 || strcmp(ind[i-1].dic[0],ind[i].dic[0])!=0) {
300 SAPPENDF(lbuff,"%s",item_0);
301 SAPPENDF(lbuff,"%s",ind[i].idx[0]);
302 SAPPENDF(lbuff,"%s",item_x1);
303 }
304 else {
305 if (ind[i-1].words==1) {
306 SAPPENDF(lbuff,"%s",item_01);
307 }
308 else {
309 SAPPENDF(lbuff,"%s",item_1);
310 }
311 }
312 SAPPENDF(lbuff,"%s",ind[i].idx[1]);
313 SAPPENDF(lbuff,"%s",delim_1);
314 break;
315
316 case 3:
317 if (strcmp(ind[i-1].idx[0],ind[i].idx[0])!=0 || strcmp(ind[i-1].dic[0],ind[i].dic[0])!=0) {
318 SAPPENDF(lbuff,"%s",item_0);
319 SAPPENDF(lbuff,"%s",ind[i].idx[0]);
320 SAPPENDF(lbuff,"%s",item_x1);
321 SAPPENDF(lbuff,"%s",ind[i].idx[1]);
322 SAPPENDF(lbuff,"%s",item_x2);
323 }
324 else if (ind[i-1].words==1) {
325 SAPPENDF(lbuff,"%s",item_01);
326 SAPPENDF(lbuff,"%s",ind[i].idx[1]);
327 SAPPENDF(lbuff,"%s",item_x2);
328 }
329 else if (strcmp(ind[i-1].idx[1],ind[i].idx[1])!=0 || strcmp(ind[i-1].dic[1],ind[i].dic[1])!=0) {
330 SAPPENDF(lbuff,"%s",item_1);
331 SAPPENDF(lbuff,"%s",ind[i].idx[1]);
332 if (ind[i-1].words==2) SAPPENDF(lbuff,"%s",item_12);
333 else SAPPENDF(lbuff,"%s",item_x2);
334 }
335 else {
336 if (ind[i-1].words==2) SAPPENDF(lbuff,"%s",item_12);
337 else SAPPENDF(lbuff,"%s",item_2);
338 }
339 SAPPENDF(lbuff,"%s",ind[i].idx[2]);
340 SAPPENDF(lbuff,"%s",delim_2);
341 break;
342
343 default:
344 break;
345 }
346 printpage(ind,fp,i,lbuff);
347 }
348 }
349 fputs(postamble,fp);
350
351 if (fp!=stdout) fclose(fp);
352 }
353
354 /* write page block */
355 static void printpage(struct index *ind, FILE *fp, int num, char *lbuff)
356 {
357 int i,j,k,cc;
358 char buff[BUFFERLEN],tmpbuff[BUFFERLEN],errbuff[BUFFERLEN];
359
360 buff[0]=tmpbuff[0]='\0';
361
362 crcheck(lbuff,fp);
363 line_length=strlen(lbuff);
364
365 for(j=0;j<ind[num].num;j++) {
366 cc=range_check(ind[num],j,lbuff,fp);
367 if (cc>j) {
368 int epage = pnumconv2(&ind[num].p[cc]);
369 int bpage = pnumconv2(&ind[num].p[j]);
370 /* range process */
371 if (ind[num].p[j].enc[0]==range_open
372 || ind[num].p[j].enc[0]==range_close)
373 ind[num].p[j].enc++;
374 if (strlen(ind[num].p[j].enc)>0) {
375 SPRINTF(buff,"%s%s%s",encap_prefix,ind[num].p[j].enc,encap_infix);
376 }
377 /* print beginning of range */
378 SAPPENDF(buff,"%s",ind[num].p[j].page);
379 if (strlen(suffix_3p)>0 && epage-bpage==2) {
380 SAPPENDF(buff,"%s",suffix_3p);
381 }
382 else if (strlen(suffix_mp)>0 && epage-bpage>=2) {
383 SAPPENDF(buff,"%s",suffix_mp);
384 }
385 else if (strlen(suffix_2p)>0 && epage-bpage==1) {
386 SAPPENDF(buff,"%s",suffix_2p);
387 }
388 else if (epage-bpage==0) {
389 }
390 else {
391 SAPPENDF(buff,"%s",delim_r);
392 SAPPENDF(buff,"%s",ind[num].p[cc].page);
393 }
394 SAPPENDF(tmpbuff,"%s",buff);
395 buff[0]='\0';
396 if (strlen(ind[num].p[j].enc)>0) {
397 SAPPENDF(tmpbuff,"%s",encap_suffix);
398 }
399 linecheck(lbuff,tmpbuff,fp, FALSE);
400 j=cc;
401 if (j==ind[num].num) {
402 goto PRINT;
403 }
404 else {
405 SAPPENDF(tmpbuff,"%s",delim_n);
406 linecheck(lbuff,tmpbuff,fp, TRUE);
407 }
408 }
409 else if (strlen(ind[num].p[j].enc)>0) {
410 /* normal encap */
411 if (ind[num].p[j].enc[0]==range_close) {
412 SPRINTF(errbuff,"Warning: Unmatched range closing operator \'%c\',",range_close);
413 for (i=0;i<ind[num].words;i++) {
414 SAPPENDF(errbuff,"%s",ind[num].idx[i]);
415 SAPPENDF(errbuff,".");
416 }
417 warn_printf(efp, "%s\n", errbuff);
418 ind[num].p[j].enc++;
419 }
420 if (strlen(ind[num].p[j].enc)>0) {
421 SAPPENDF(tmpbuff,"%s",encap_prefix);
422 SAPPENDF(tmpbuff,"%s",ind[num].p[j].enc);
423 SAPPENDF(tmpbuff,"%s",encap_infix);
424 SAPPENDF(tmpbuff,"%s",ind[num].p[j].page);
425 SAPPENDF(tmpbuff,"%s",encap_suffix);
426 SAPPENDF(tmpbuff,"%s",delim_n);
427 linecheck(lbuff,tmpbuff,fp, FALSE);
428 }
429 else {
430 SAPPENDF(tmpbuff,"%s",ind[num].p[j].page);
431 SAPPENDF(tmpbuff,"%s",delim_n);
432 linecheck(lbuff,tmpbuff,fp, FALSE);
433 }
434 }
435 else {
436 /* no encap */
437 SAPPENDF(tmpbuff,"%s",ind[num].p[j].page);
438 SAPPENDF(tmpbuff,"%s",delim_n);
439 linecheck(lbuff,tmpbuff,fp, FALSE);
440 }
441 }
442
443 if (ind[num].p[j].enc[0]==range_open) {
444 SPRINTF(errbuff,"Warning: Unmatched range opening operator \'%c\',",range_open);
445 for (k=0;k<ind[num].words;k++) {
446 SAPPENDF(errbuff,"%s",ind[num].idx[k]);
447 SAPPENDF(errbuff,".");
448 }
449 warn_printf(efp, "%s\n", errbuff);
450 ind[num].p[j].enc++;
451 }
452 else if (ind[num].p[j].enc[0]==range_close) {
453 SPRINTF(errbuff,"Warning: Unmatched range closing operator \'%c\',",range_close);
454 for (k=0;k<ind[num].words;k++) {
455 SAPPENDF(errbuff,"%s",ind[num].idx[k]);
456 SAPPENDF(errbuff,".");
457 }
458 warn_printf(efp, "%s\n", errbuff);
459 ind[num].p[j].enc++;
460 }
461 if (strlen(ind[num].p[j].enc)>0) {
462 SAPPENDF(tmpbuff,"%s",encap_prefix);
463 SAPPENDF(tmpbuff,"%s",ind[num].p[j].enc);
464 SAPPENDF(tmpbuff,"%s",encap_infix);
465 SAPPENDF(tmpbuff,"%s",ind[num].p[j].page);
466 SAPPENDF(tmpbuff,"%s",encap_suffix);
467 }
468 else {
469 SAPPENDF(tmpbuff,"%s",ind[num].p[j].page);
470 }
471 linecheck(lbuff,tmpbuff,fp, FALSE);
472
473 PRINT:
474 fputs(lbuff,fp);
475 fputs(delim_t,fp);
476 lbuff[0]='\0';
477 }
478
479 static int range_check(struct index ind, int count, char *lbuff, FILE *fp)
480 {
481 int i,j,k,cc1,cc2,start,force=0;
482 char tmpbuff[BUFFERLEN],errbuff[BUFFERLEN];
483
484 for (i=count;i<ind.num+1;i++) {
485 if (ind.p[i].enc[0]==range_close) {
486 SPRINTF(errbuff,"Warning: Unmatched range closing operator \'%c\',",range_close);
487 SAPPENDF(errbuff,"%s",ind.idx[0]);
488 SAPPENDF(errbuff,".");
489 warn_printf(efp, "%s\n", errbuff);
490 ind.p[i].enc++;
491 }
492 if (ind.p[i].enc[0]==range_open) {
493 start=i;
494 ind.p[i].enc++;
495 for (j=i;j<ind.num+1;j++) {
496 if (strcmp(ind.p[start].enc,ind.p[j].enc)) {
497 if (ind.p[j].enc[0]==range_close) {
498 ind.p[j].enc++;
499 ind.p[j].enc[0]='\0';
500 force=1;
501 break;
502 }
503 else if (j!=i && ind.p[j].enc[0]==range_open) {
504 SPRINTF(errbuff,"Warning: Unmatched range opening operator \'%c\',",range_open);
505 for (k=0;k<ind.words;k++) {
506 SAPPENDF(errbuff,"%s",ind.idx[k]);
507 SAPPENDF(errbuff,".");
508 }
509 warn_printf(efp, "%s\n", errbuff);
510 ind.p[j].enc++;
511 }
512 if (strlen(ind.p[j].enc)>0) {
513 SPRINTF(tmpbuff,"%s%s%s%s%s%s",encap_prefix,ind.p[j].enc,encap_infix
514 ,ind.p[j].page,encap_suffix,delim_n);
515 linecheck(lbuff,tmpbuff,fp, FALSE);
516 }
517 }
518 }
519 if (j==ind.num+1) {
520 SPRINTF(errbuff,"Warning: Unmatched range opening operator \'%c\',",range_open);
521 for (k=0;k<ind.words;k++) {
522 SAPPENDF(errbuff,"%s",ind.idx[k]);
523 SAPPENDF(errbuff,".");
524 }
525 warn_printf(efp, "%s\n", errbuff);
526 }
527 i=j-1;
528 }
529 else if (prange && i<ind.num) {
530 if (chkcontinue(ind.p,i)
531 && (!strcmp(ind.p[i].enc,ind.p[i+1].enc)
532 || ind.p[i+1].enc[0]==range_open))
533 continue;
534 else {
535 i++;
536 break;
537 }
538 }
539 else {
540 i++;
541 break;
542 }
543 }
544 cc1=pnumconv2(&ind.p[i-1]);
545 cc2=pnumconv2(&ind.p[count]);
546 if (cc1>=cc2+2 || (cc1>=cc2+1 && strlen(suffix_2p)) || force) {
547 return i-1;
548 }
549 else return count;
550 }
551
552 /* check line length */
553 static void linecheck(char *lbuff, char *tmpbuff, FILE *fp, int force)
554 {
555 if (line_length+strlen(tmpbuff)>line_max && !force) {
556 SAPPENDF(lbuff,"\n");
557 fputs(lbuff,fp);
558 lbuff[0]='\0';
559 SAPPENDF(lbuff,"%s",indent_space);
560 SAPPENDF(lbuff,"%s",tmpbuff);
561 line_length=indent_length+strlen(tmpbuff);
562 tmpbuff[0]='\0';
563 }
564 else {
565 SAPPENDF(lbuff,"%s",tmpbuff);
566 line_length+=strlen(tmpbuff);
567 tmpbuff[0]='\0';
568 }
569 }
570
571 static void crcheck(char *lbuff, FILE *fp)
572 {
573 int i;
574 char buff[BUFFERLEN];
575
576 for (i=strlen(lbuff);i>=0;i--) {
577 if (lbuff[i]=='\n') {
578 strncpy(buff,lbuff,i+1);
579 buff[i+1]='\0';
580 fputs(buff,fp);
581 strncpy(buff,&lbuff[i+1],BUFFERLEN-1);
582 strcpy(lbuff,buff);
583 break;
584 }
585 }
586 }