Debian Codegraph @

texk/upmendex/fread.c

raw
1 #include "mendex.h"
2
3 #include <kpathsea/tex-file.h>
4
5 #include "exkana.h"
6 #include "exvar.h"
7
8 #define BUFSIZE 65535
9
10 static int getestr(char *buff, char *estr);
11 static void chkpageattr(struct page *p);
12 static void copy_multibyte_char(char *buff1, char *buff2, int *i, int *j);
13
14 /* read idx file */
15 int idxread(char *filename, int start)
16 {
17 int i,j,k,l,m,n,cc,indent,wflg,flg=0,bflg=0,nest,esc,quo,eflg=0,pacc,preject;
18 char buff[BUFSIZE],wbuff[BUFSIZE],estr[256],table[BUFSIZE];
19 UChar utable[BUFSIZE],*tmp1,*tmp2;
20 FILE *fp;
21
22 pacc=acc;
23 preject=reject;
24
25 if (filename==NULL) {
26 fp=stdin;
27 #ifdef WIN32
28 setmode(fileno(fp), _O_BINARY);
29 #endif
30 verb_printf(efp, "Scanning input file stdin.");
31 }
32 else {
33 if(kpse_in_name_ok(filename))
34 fp=fopen(filename,"rb");
35 else
36 fp=NULL;
37 if (fp==NULL) {
38 sprintf(buff,"%s.idx",filename);
39 if(kpse_in_name_ok(buff))
40 fp=fopen(buff,"rb");
41 else
42 fp=NULL;
43 if (fp==NULL) {
44 warn_printf(efp,"Warning: Couldn't find input file %s.\n",filename);
45 return 1;
46 }
47 else strcpy(filename,buff);
48 }
49 verb_printf(efp,"Scanning input file %s.",filename);
50 }
51
52 for (i=start,n=1;;i++,n++) {
53 if (!(i%100))
54 ind=(struct index *)xrealloc(ind,sizeof(struct index)*(i+100));
55 LOOP:
56 ind[i].lnum=n;
57 if (fgets(buff,sizeof(buff)-1,fp)==NULL) break;
58 for (j=bflg=cc=0;j<strlen(buff);j++) {
59 if (buff[j]!=' ' && buff[j]!='\n') cc=1;
60 if (bflg==0) {
61 if (strncmp(&buff[j],keyword,strlen(keyword))==0) {
62 j+=strlen(keyword);
63 bflg=1;
64 }
65 }
66 if (bflg==1) {
67 if (buff[j]==arg_open) {
68 j++;
69 break;
70 }
71 }
72 }
73 if (j==strlen(buff)) {
74 if (cc) reject++;
75 i--;
76 continue;
77 }
78 indent=wflg=k=nest=esc=quo=0;
79
80 /* analyze words */
81
82 for (;;j++,k++) {
83 if (buff[j]=='\n' || buff[j]=='\0') {
84 verb_printf(efp,"\nWarning: Incomplete first argument in %s, line %d.",filename,ind[i].lnum);
85 warn++;
86 n++;
87 goto LOOP;
88 }
89
90 if (buff[j]==quote && esc==0 && quo==0) {
91 k--;
92 quo=1;
93 continue;
94 }
95
96 if (esc==1 && buff[j]==escape) {
97 esc=0;
98 } else if (quo==0 && buff[j]==escape) {
99 esc=1;
100 }
101
102 if (quo==0) {
103 if (esc==0 && buff[j]==arg_open) {
104 nest++;
105 wbuff[k]=buff[j];
106 continue;
107 }
108 else if (esc==0 && buff[j]==arg_close && nest>0) {
109 nest--;
110 wbuff[k]=buff[j];
111 continue;
112 }
113 if (nest==0) {
114 if (buff[j]==level) {
115 esc=0;
116 if (indent>=MAXDEPTH-1) {
117 fprintf(efp,"\nError: Extra `%c\' in %s, line %d.",level,filename,ind[i].lnum);
118 if (efp!=stderr) fprintf(stderr,"\nError: Extra `%c\' in %s, line %d.",level,filename,ind[i].lnum);
119 eflg++;
120 reject++;
121 n++;
122 goto LOOP;
123 }
124 wbuff[k]='\0';
125 multibyte_to_widechar(utable,BUFSIZE,wbuff);
126 ind[i].idx[indent]=u_xstrdup(utable);
127 if (!wflg) ind[i].org[indent]=NULL;
128 indent++;
129 wflg=0;
130 k= -1;
131 continue;
132 }
133 else if (buff[j]==actual) {
134 esc=0;
135 if (wflg) {
136 fprintf(efp,"\nError: Extra `%c\' in %s, line %d.",actual,filename,ind[i].lnum);
137 if (efp!=stderr) fprintf(stderr,"\nError: Extra `%c\' in %s, line %d.",actual,filename,ind[i].lnum);
138 eflg++;
139 reject++;
140 n++;
141 goto LOOP;
142 }
143 wbuff[k]='\0';
144 multibyte_to_widechar(utable,BUFSIZE,wbuff);
145 ind[i].org[indent]=u_xstrdup(utable);
146 wflg=1;
147 k= -1;
148 continue;
149 }
150 else if ((esc==0 && buff[j]==arg_close) || buff[j]==encap) {
151 esc=0;
152 if (buff[j]==encap) {
153 j++;
154 cc=getestr(&buff[j],estr);
155 if (cc<0) {
156 fprintf(efp,"\nError: Bad encap string in %s, line %d.",filename,ind[i].lnum);
157 if (efp!=stderr) fprintf(stderr,"\nError: Bad encap string in %s, line %d.",filename,ind[i].lnum);
158 eflg++;
159 reject++;
160 n++;
161 goto LOOP;
162 }
163 j+=cc;
164 }
165 else estr[0]='\0';
166
167 wbuff[k]='\0';
168 multibyte_to_widechar(utable,BUFSIZE,wbuff);
169 ind[i].idx[indent]=u_xstrdup(utable);
170 if (u_strlen(ind[i].idx[indent])==0) {
171 if (wflg) {
172 u_strcpy(ind[i].idx[indent],ind[i].org[indent]);
173 }
174 else if (indent>0) {
175 indent--;
176 }
177 else {
178 verb_printf(efp,"\nWarning: Illegal null field in %s, line %d.",filename,ind[i].lnum);
179 warn++;
180 n++;
181 goto LOOP;
182 }
183 }
184 if (!wflg) {
185 ind[i].org[indent]=NULL;
186 }
187 break;
188 }
189 }
190 if (bcomp==1) {
191 if (buff[j]==' ' || buff[j]=='\t') {
192 esc=0;
193 if (k==0) {
194 k--;
195 continue;
196 }
197 else if (buff[j+1]==' ' || buff[j+1]=='\t' || buff[j+1]==encap || buff[j+1]==arg_close || buff[j+1]==actual || buff[j+1]==level) {
198 k--;
199 continue;
200 }
201 else if (buff[j]=='\t') {
202 wbuff[k]=' ';
203 continue;
204 }
205 }
206 }
207 }
208 else quo=0;
209
210 if (buff[j]!=escape) esc=0;
211 copy_multibyte_char(buff, wbuff, &j, &k);
212 }
213 for (k=0; k<=indent; k++) {
214 if ((u_strlen(ind[i].idx[k])==0 && ind[i].org[k]==NULL) ||
215 (ind[i].org[k] && u_strlen(ind[i].org[k])==0)) {
216 fprintf(efp,"\nError: Illegal null field in %s, line %d.",filename,ind[i].lnum);
217 if (efp!=stderr) fprintf(stderr,"\nError: Illegal null field in %s, line %d.",filename,ind[i].lnum);
218 eflg++;
219 reject++;
220 n++;
221 goto LOOP;
222 }
223 }
224 ind[i].words=indent+1;
225
226 /* kana-convert */
227
228 for (k=0;k<ind[i].words;k++) {
229 if (ind[i].org[k]==NULL) {
230 cc=convert(ind[i].idx[k],utable);
231 if (cc==-1) {
232 fprintf(efp,"in %s, line %d.",filename,ind[i].lnum);
233 if (efp!=stderr) fprintf(stderr,"in %s, line %d.",filename,ind[i].lnum);
234 eflg++;
235 reject++;
236 n++;
237 goto LOOP;
238 }
239 ind[i].dic[k]=u_xstrdup(utable);
240 }
241 else {
242 cc=convert(ind[i].org[k],utable);
243 if (cc==-1) {
244 fprintf(efp,"in %s, line %d.",filename,ind[i].lnum);
245 if (efp!=stderr) fprintf(stderr,"in %s, line %d.",filename,ind[i].lnum);
246 eflg++;
247 reject++;
248 n++;
249 goto LOOP;
250 }
251 ind[i].dic[k]=u_xstrdup(utable);
252 }
253 }
254 acc++;
255
256 /* page edit */
257
258 if (i==0) {
259 ind[0].num=0;
260 ind[0].p=xmalloc(sizeof(struct page)*16);
261 for (;buff[j]!=arg_open && buff[j]!='\n' && buff[j]!='\0';j++);
262 if (buff[j]=='\n' || buff[j]=='\0') {
263 verb_printf(efp,"\nWarning: Missing second argument in %s, line %d.",filename,ind[i].lnum);
264 acc--;
265 reject++;
266 warn++;
267 n++;
268 goto LOOP;
269 }
270 j++;
271 for (k=nest=0;;j++,k++) {
272 if (buff[j]=='\n' || buff[j]=='\0') {
273 verb_printf(efp,"\nWarning: Incomplete second argument in %s, line %d.",filename,ind[i].lnum);
274 acc--;
275 reject++;
276 warn++;
277 n++;
278 goto LOOP;
279 }
280 if (buff[j]==arg_open)
281 nest++;
282 else if (buff[j]==arg_close) {
283 if (nest==0) {
284 table[k]='\0';
285 ind[0].p[0].page=xstrdup(table);
286 break;
287 }
288 else nest--;
289 }
290 copy_multibyte_char(buff, table, &j, &k);
291 }
292 ind[0].p[0].enc=xstrdup(estr);
293 chkpageattr(&ind[0].p[0]);
294 }
295 else {
296 for (l=0;l<i;l++) {
297 flg=0;
298 if (ind[i].words!=ind[l].words) continue;
299 for (flg=1,m=0;m<ind[i].words;m++) {
300 if (u_strcmp(ind[i].idx[m],ind[l].idx[m])!=0) {
301 flg=0;
302 break;
303 }
304 if (u_strcmp(ind[i].dic[m],ind[l].dic[m])!=0) {
305 if (ind[i].org[m]!=NULL) tmp1=ind[i].org[m];
306 else tmp1=ind[i].idx[m];
307
308 if (ind[l].org[m]!=NULL) tmp2=ind[l].org[m];
309 else tmp2=ind[l].idx[m];
310
311 widechar_to_multibyte(wbuff,BUFSIZE,tmp1);
312 verb_printf(efp,"\nWarning: Sort key \"%s\"",wbuff);
313 widechar_to_multibyte(wbuff,BUFSIZE,tmp2);
314 verb_printf(efp," is different from previous key \"%s\"",wbuff);
315 widechar_to_multibyte(wbuff,BUFSIZE,ind[i].idx[m]);
316 verb_printf(efp," for same index \"%s\" in %s, line %d.",wbuff,filename,ind[i].lnum);
317 warn++;
318 flg=0;
319 break;
320 }
321 }
322 if (flg==1) break;
323 }
324
325 if (flg==1) {
326 for (m=0;m<ind[i].words;m++) {
327 free(ind[i].idx[m]);
328 free(ind[i].dic[m]);
329 }
330
331 i--;
332 for (;buff[j]!=arg_open && buff[j]!='\n' && buff[j]!='\0';j++);
333 if (buff[j]=='\n' || buff[j]=='\0') {
334 verb_printf(efp,"\nWarning: Missing second argument in %s, line %d.",filename,ind[i].lnum);
335 acc--;
336 reject++;
337 warn++;
338 n++;
339 i++;
340 goto LOOP;
341 }
342 j++;
343 for (k=nest=0;;j++,k++) {
344 if (buff[j]=='\n' || buff[j]=='\0') {
345 verb_printf(efp,"\nWarning: Incomplete second argument in %s, line %d.",filename,ind[i].lnum);
346 warn++;
347 n++;
348 i++;
349 goto LOOP;
350 }
351 if (buff[j]==arg_open)
352 nest++;
353 else if (buff[j]==arg_close) {
354 if (nest==0) break;
355 else nest--;
356 }
357 copy_multibyte_char(buff, table, &j, &k);
358 }
359
360 table[k]='\0';
361
362 for (k=0;k<=ind[l].num;k++) {
363 if (strcmp(ind[l].p[k].page,table)==0) {
364 if (strcmp(ind[l].p[k].enc,estr)==0) break;
365 }
366 }
367
368 if (k>ind[l].num) {
369 ind[l].num++;
370 if (!((ind[l].num)%16)) ind[l].p=(struct page *)xrealloc(ind[l].p,sizeof(struct page)*((int)((ind[l].num)/16)+1)*16);
371
372 ind[l].p[ind[l].num].page=xstrdup(table);
373
374 ind[l].p[ind[l].num].enc=xstrdup(estr);
375 chkpageattr(&ind[l].p[ind[l].num]);
376 }
377 }
378 else {
379 ind[i].num=0;
380 ind[i].p=xmalloc(sizeof(struct page)*16);
381 for (;buff[j]!=arg_open && buff[j]!='\n' && buff[j]!='\0';j++);
382 if (buff[j]=='\n' || buff[j]=='\0') {
383 verb_printf(efp,"\nWarning: Missing second argument in %s, line %d.",filename,ind[i].lnum);
384 acc--;
385 reject++;
386 warn++;
387 n++;
388 goto LOOP;
389 }
390 j++;
391 for (k=nest=0;;j++,k++) {
392 if (buff[j]=='\n' || buff[j]=='\0') {
393 verb_printf(efp,"\nWarning: Incomplete second argument in %s, line %d.",filename,ind[i].lnum);
394 acc--;
395 reject++;
396 warn++;
397 n++;
398 goto LOOP;
399 }
400 if (buff[j]==arg_open)
401 nest++;
402 if (buff[j]==arg_close) {
403 if (nest==0) {
404 table[k]='\0';
405 ind[i].p[0].page=xstrdup(table);
406 break;
407 }
408 else nest--;
409 }
410 copy_multibyte_char(buff, table, &j, &k);
411 }
412 ind[l].p[0].enc=xstrdup(estr);
413 chkpageattr(&ind[i].p[0]);
414 }
415 }
416 }
417 lines=i;
418 if (filename != NULL) fclose(fp);
419
420 verb_printf(efp,"...done (%d entries accepted, %d rejected).\n",acc-pacc, reject-preject);
421 return eflg;
422 }
423
424 /* pic up encap string */
425 static int getestr(char *buff, char *estr)
426 {
427 int i,j,nest=0,esc=0,quo=0;
428
429 for (i=0,j=0;i<strlen(buff);i++,j++) {
430 /* If a "quote" character is found, it is removed and the
431 following character is not treated as a special character.
432 If a "quote" character follows an odd number of
433 consecutive "escape" characters, it is not treated as a
434 special character and is left as is.
435 Note that the "escape" characters are not removed. */
436 esc=0; quo=0;
437 if (buff[i]==escape) {
438 estr[j]=buff[i];
439 i++; j++;
440 esc=1;
441 } else if (buff[i]==quote) {
442 i++;
443 quo=1;
444 }
445 if (quo==0) {
446 if (nest==0 && esc==0 && buff[i]==arg_close) {
447 estr[j]='\0';
448 return i;
449 }
450 if (esc==0 && buff[i]==arg_open) nest++;
451 else if (esc==0 && buff[i]==arg_close) nest--;
452 else if (buff[i]==level || buff[i]==actual || buff[i]==encap) {
453 fprintf(efp, "\nError: Extra `%c\' at position %d in encap string.",buff[i],i);
454 return -1;
455 }
456 }
457 copy_multibyte_char(buff, estr, &i, &j);
458 }
459
460 return -1;
461 }
462
463 static void copy_multibyte_char(char *buff1, char *buff2, int *i, int *j)
464 {
465 int len;
466
467 if ((unsigned char)buff1[*i]<0x80) {
468 buff2[j ? *j : *i] = buff1[*i];
469 return;
470 }
471 len = multibytelen((unsigned char)buff1[*i]);
472 if (len<0) {
473 verb_printf(efp,"\nWarning: Illegal input of lead byte 0x%x in UTF-8.",(unsigned char)buff1[*i]);
474 len=1; /* copy one byte when illegal lead byte */
475 }
476 while(len--) {
477 buff2[j ? (*j)++ : *i] = buff1[*i];
478 (*i)++;
479 }
480 if (j!=NULL) (*j)--;
481 (*i)--;
482 }
483
484 int multibyte_to_widechar(UChar *wcstr, int32_t size, char *mbstr)
485 {
486 int32_t len, olen, nsub;
487 UErrorCode err;
488
489 len=strlen(mbstr)+1;
490 do {
491 err = U_ZERO_ERROR;
492 len--;
493 u_strFromUTF8WithSub(NULL, 0, &olen, mbstr, len, 0xFFFD, &nsub, &err);
494 } while (olen>=size);
495 err = U_ZERO_ERROR;
496 u_strFromUTF8WithSub(wcstr, size, &olen, mbstr, len, 0xFFFD, &nsub, &err);
497 return olen;
498 }
499
500 int widechar_to_multibyte(char *mbstr, int32_t size, UChar *wcstr)
501 {
502 int32_t len, olen, nsub;
503 UErrorCode err;
504
505 len=u_strlen(wcstr)+1;
506 do {
507 err = U_ZERO_ERROR;
508 len--;
509 u_strToUTF8WithSub(NULL, 0, &olen, wcstr, len, 0xFFFD, &nsub, &err);
510 } while (olen>=size);
511 err = U_ZERO_ERROR;
512 u_strToUTF8WithSub(mbstr, size, &olen, wcstr, len, 0xFFFD, &nsub, &err);
513 return olen;
514 }
515
516 static void chkpageattr(struct page *p)
517 {
518 int i,j,cc=0,cnt,pplen,pclen;
519 char buff[16],*pcpos,*page0;
520 static char pattr_prev[PAGE_COMPOSIT_DEPTH] = {0};
521
522 pplen=strlen(page_precedence);
523 pclen=strlen(page_compositor);
524 for (i=0;i<strlen(p->page);i++) {
525 page0=&p->page[i];
526 if (strncmp(page_compositor,page0,pclen)==0) {
527 p->attr[cc]=pattr[cc];
528 cc++;
529 i+=pclen-1;
530 if (cc>=PAGE_COMPOSIT_DEPTH) {
531 if (pclen>0)
532 verb_printf(efp, "\nToo many fields of page number \"%s\".\n", p->page);
533 else
534 verb_printf(efp, "\nIllegular page_comositor specification.\n");
535 exit(253);
536 }
537 }
538 else {
539 cnt=0;
540 if (!((*page0>='0' && *page0<='9') || (*page0>='A' && *page0<='Z') || (*page0>='a' && *page0<='z'))) {
541 for (j=cc;j<PAGE_COMPOSIT_DEPTH;j++) p->attr[j]=-1;
542 return;
543 }
544 pcpos=strstr(page0,page_compositor);
545 j=pcpos ? pcpos-page0 : strlen(page0);
546 if (j>15) {
547 verb_printf(efp, "\nToo long page number string \"%s\".\n", page0);
548 exit(253);
549 }
550 strncpy(buff,page0,j);
551 buff[j]='\0';
552 ATTRLOOP:
553 cnt++;
554 if (cnt>pplen) {
555 verb_printf(efp, "\nFailed to find page type for page \"%s\" in page_precedence specification (%s).\n",
556 page0, page_precedence);
557 exit(253);
558 }
559
560 switch(page_precedence[pattr[cc]]) {
561 case 'r':
562 if (strchr("ivxlcdm",*page0)==NULL ||
563 (strchr("lcdm",*page0) && strchr(page_precedence,'a') && strlen(buff)==1 &&
564 pattr_prev[cc]!='r')) {
565 /* heuristic detection as alphabet since L=50, C=100, D=100, M=1000 are quite large */
566 if (pattr[cc]<pplen-1)
567 pattr[cc]++;
568 else pattr[cc]=0;
569 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) pattr[j]=0;
570 goto ATTRLOOP;
571 }
572 break;
573 case 'R':
574 if (strchr("IVXLCDM",*page0)==NULL ||
575 (strchr("LCDM",*page0) && strchr(page_precedence,'A') && strlen(buff)==1 &&
576 pattr_prev[cc]!='R')) {
577 /* heuristic detection as alphabet since L=50, C=100, D=100, M=1000 are quite large */
578 if (pattr[cc]<pplen-1)
579 pattr[cc]++;
580 else pattr[cc]=0;
581 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) pattr[j]=0;
582 goto ATTRLOOP;
583 }
584 break;
585 case 'n':
586 if (*page0<'0' || *page0>'9') {
587 if (pattr[cc]<pplen-1)
588 pattr[cc]++;
589 else pattr[cc]=0;
590 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) pattr[j]=0;
591 goto ATTRLOOP;
592 }
593 break;
594 case 'a':
595 if (*page0<'a' || *page0>'z' || strlen(buff)>1 ||
596 (strchr("ivx",*page0) && strchr(page_precedence,'r') &&
597 pattr_prev[cc]!='a')) {
598 /* heuristic detection as roman number since I=1, V=5, X=10 are quite small */
599 if (pattr[cc]<pplen-1)
600 pattr[cc]++;
601 else pattr[cc]=0;
602 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) pattr[j]=0;
603 goto ATTRLOOP;
604 }
605 break;
606 case 'A':
607 if (*page0<'A' || *page0>'Z' || strlen(buff)>1 ||
608 (strchr("IVX",*page0) && strchr(page_precedence,'R') &&
609 pattr_prev[cc]!='A')) {
610 /* heuristic detection as roman number since I=1, V=5, X=10 are quite small */
611 if (pattr[cc]<pplen-1)
612 pattr[cc]++;
613 else pattr[cc]=0;
614 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) pattr[j]=0;
615 goto ATTRLOOP;
616 }
617 break;
618 default:
619 verb_printf(efp, "\nUnknown page type '%c' in page_precedence specification (%s).\n",
620 page_precedence[pattr[cc]], page_precedence);
621 exit(253);
622 }
623 pattr_prev[cc] = page_precedence[pattr[cc]];
624 }
625 }
626 p->attr[cc]=pattr[cc];
627 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) p->attr[j]=-1;
628 }