Debian Codegraph @

texk/mendexk/fread.c

raw
1 #include "mendex.h"
2
3 #include <kpathsea/tex-file.h>
4 #include <ptexenc/ptexenc.h>
5
6 #include "exkana.h"
7 #include "exvar.h"
8
9 #define BUFSIZE 65535
10
11 static int getestr(char *buff, char *estr);
12 static void chkpageattr(struct page *p);
13 static void copy_multibyte_char(char *buff1, char *buff2, int *i, int *j);
14
15 /* read idx file */
16 int idxread(char *filename, int start)
17 {
18 int i,j,k,l,m,n,cc,indent,wflg,flg=0,bflg=0,nest,esc,quo,eflg=0,pacc,preject;
19 char buff[BUFSIZE],wbuff[BUFSIZE],estr[256],table[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=nkf_open(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=nkf_open(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 (mfgets(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>=2) {
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 ind[i].idx[indent]=xmalloc(k+1);
125 strncpy(ind[i].idx[indent],wbuff,k);
126 ind[i].idx[indent][k]='\0';
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 ind[i].org[indent]=xmalloc(k+1);
144 strncpy(ind[i].org[indent],wbuff,k);
145 ind[i].org[indent][k]='\0';
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 ind[i].idx[indent]=xmalloc(k+1);
168 strncpy(ind[i].idx[indent],wbuff,k);
169 ind[i].idx[indent][k]='\0';
170 if (strlen(ind[i].idx[indent])==0) {
171 if (wflg) {
172 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 ((strlen(ind[i].idx[k])==0 && ind[i].org[k]==NULL) ||
215 (ind[i].org[k] && 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],table);
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]=xstrdup(table);
240 }
241 else {
242 cc=convert(ind[i].org[k],table);
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]=xstrdup(table);
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 (strcmp(ind[i].idx[m],ind[l].idx[m])!=0) {
301 flg=0;
302 break;
303 }
304 if (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 verb_printf(efp,"\nWarning: Sort key \"%s\" is different from previous key \"%s\" for same index \"%s\" in %s, line %d.",tmp1, tmp2, ind[i].idx[m], filename,ind[i].lnum);
312 warn++;
313 flg=0;
314 break;
315 }
316 }
317 if (flg==1) break;
318 }
319
320 if (flg==1) {
321 for (m=0;m<ind[i].words;m++) {
322 free(ind[i].idx[m]);
323 free(ind[i].dic[m]);
324 }
325
326 i--;
327 for (;buff[j]!=arg_open && buff[j]!='\n' && buff[j]!='\0';j++);
328 if (buff[j]=='\n' || buff[j]=='\0') {
329 verb_printf(efp,"\nWarning: Missing second argument in %s, line %d.",filename,ind[i].lnum);
330 acc--;
331 reject++;
332 warn++;
333 n++;
334 i++;
335 goto LOOP;
336 }
337 j++;
338 for (k=nest=0;;j++,k++) {
339 if (buff[j]=='\n' || buff[j]=='\0') {
340 verb_printf(efp,"\nWarning: Incomplete second argument in %s, line %d.",filename,ind[i].lnum);
341 warn++;
342 n++;
343 i++;
344 goto LOOP;
345 }
346 if (buff[j]==arg_open)
347 nest++;
348 else if (buff[j]==arg_close) {
349 if (nest==0) break;
350 else nest--;
351 }
352 copy_multibyte_char(buff, table, &j, &k);
353 }
354
355 table[k]='\0';
356
357 for (k=0;k<=ind[l].num;k++) {
358 if (strcmp(ind[l].p[k].page,table)==0) {
359 if (strcmp(ind[l].p[k].enc,estr)==0) break;
360 }
361 }
362
363 if (k>ind[l].num) {
364 ind[l].num++;
365 if (!((ind[l].num)%16)) ind[l].p=(struct page *)xrealloc(ind[l].p,sizeof(struct page)*((int)((ind[l].num)/16)+1)*16);
366
367 ind[l].p[ind[l].num].page=xstrdup(table);
368
369 ind[l].p[ind[l].num].enc=xstrdup(estr);
370 chkpageattr(&ind[l].p[ind[l].num]);
371 }
372 }
373 else {
374 ind[i].num=0;
375 ind[i].p=xmalloc(sizeof(struct page)*16);
376 for (;buff[j]!=arg_open && buff[j]!='\n' && buff[j]!='\0';j++);
377 if (buff[j]=='\n' || buff[j]=='\0') {
378 verb_printf(efp,"\nWarning: Missing second argument in %s, line %d.",filename,ind[i].lnum);
379 acc--;
380 reject++;
381 warn++;
382 n++;
383 goto LOOP;
384 }
385 j++;
386 for (k=nest=0;;j++,k++) {
387 if (buff[j]=='\n' || buff[j]=='\0') {
388 verb_printf(efp,"\nWarning: Incomplete second argument in %s, line %d.",filename,ind[i].lnum);
389 acc--;
390 reject++;
391 warn++;
392 n++;
393 goto LOOP;
394 }
395 if (buff[j]==arg_open)
396 nest++;
397 if (buff[j]==arg_close) {
398 if (nest==0) {
399 table[k]='\0';
400 ind[i].p[0].page=xstrdup(table);
401 break;
402 }
403 else nest--;
404 }
405 copy_multibyte_char(buff, table, &j, &k);
406 }
407 ind[l].p[0].enc=xstrdup(estr);
408 chkpageattr(&ind[i].p[0]);
409 }
410 }
411 }
412 lines=i;
413 if (filename != NULL) nkf_close(fp);
414
415 verb_printf(efp,"...done (%d entries accepted, %d rejected).\n",acc-pacc, reject-preject);
416 return eflg;
417 }
418
419 /* pic up encap string */
420 static int getestr(char *buff, char *estr)
421 {
422 int i,j,nest=0,esc=0,quo=0;
423
424 for (i=0,j=0;i<strlen(buff);i++,j++) {
425 /* If a "quote" character is found, it is removed and the
426 following character is not treated as a special character.
427 If a "quote" character follows an odd number of
428 consecutive "escape" characters, it is not treated as a
429 special character and is left as is.
430 Note that the "escape" characters are not removed. */
431 esc=0; quo=0;
432 if (buff[i]==escape) {
433 estr[j]=buff[i];
434 i++; j++;
435 esc=1;
436 } else if (buff[i]==quote) {
437 i++;
438 quo=1;
439 }
440 if (quo==0) {
441 if (nest==0 && esc==0 && buff[i]==arg_close) {
442 estr[j]='\0';
443 return i;
444 }
445 if (esc==0 && buff[i]==arg_open) nest++;
446 else if (esc==0 && buff[i]==arg_close) nest--;
447 else if (buff[i]==level || buff[i]==actual || buff[i]==encap) {
448 fprintf(efp, "\nError: Extra `%c\' at position %d in encap string.",buff[i],i);
449 return -1;
450 }
451 }
452 copy_multibyte_char(buff, estr, &i, &j);
453 }
454
455 return -1;
456 }
457
458 static void copy_multibyte_char(char *buff1, char *buff2, int *i, int *j)
459 {
460 int len;
461
462 if ((unsigned char)buff1[*i]<0x80) {
463 buff2[j ? *j : *i] = buff1[*i];
464 return;
465 }
466 len = multibytelen((unsigned char)buff1[*i]);
467 if (len<0) {
468 verb_printf(efp,"\nWarning: Illegal input of lead byte 0x%x in UTF-8.",(unsigned char)buff1[*i]);
469 len=1; /* copy one byte when illegal lead byte */
470 }
471 while(len--) {
472 buff2[j ? (*j)++ : *i] = buff1[*i];
473 (*i)++;
474 }
475 if (j!=NULL) (*j)--;
476 (*i)--;
477 }
478
479 static void chkpageattr(struct page *p)
480 {
481 int i,j,cc=0,cnt,pplen,pclen;
482 char buff[16],*pcpos,*page0;
483 static char pattr_prev[PAGE_COMPOSIT_DEPTH] = {0};
484
485 pplen=strlen(page_precedence);
486 pclen=strlen(page_compositor);
487 for (i=0;i<strlen(p->page);i++) {
488 page0=&p->page[i];
489 if (strncmp(page_compositor,page0,pclen)==0) {
490 p->attr[cc]=pattr[cc];
491 cc++;
492 i+=pclen-1;
493 if (cc>=PAGE_COMPOSIT_DEPTH) {
494 if (pclen>0)
495 verb_printf(efp, "\nToo many fields of page number \"%s\".\n", p->page);
496 else
497 verb_printf(efp, "\nIllegular page_comositor specification.\n");
498 exit(253);
499 }
500 }
501 else {
502 cnt=0;
503 if (!((*page0>='0' && *page0<='9') || (*page0>='A' && *page0<='Z') || (*page0>='a' && *page0<='z'))) {
504 for (j=cc;j<PAGE_COMPOSIT_DEPTH;j++) p->attr[j]=-1;
505 return;
506 }
507 pcpos=strstr(page0,page_compositor);
508 j=pcpos ? pcpos-page0 : strlen(page0);
509 if (j>15) {
510 verb_printf(efp, "\nToo long page number string \"%s\".\n", page0);
511 exit(253);
512 }
513 strncpy(buff,page0,j);
514 buff[j]='\0';
515 ATTRLOOP:
516 cnt++;
517 if (cnt>pplen) {
518 verb_printf(efp, "\nFailed to find page type for page \"%s\" in page_precedence specification (%s).\n",
519 page0, page_precedence);
520 exit(253);
521 }
522
523 switch(page_precedence[pattr[cc]]) {
524 case 'r':
525 if (strchr("ivxlcdm",*page0)==NULL ||
526 (strchr("lcdm",*page0) && strchr(page_precedence,'a') && strlen(buff)==1 &&
527 pattr_prev[cc]!='r')) {
528 /* heuristic detection as alphabet since L=50, C=100, D=100, M=1000 are quite large */
529 if (pattr[cc]<pplen-1)
530 pattr[cc]++;
531 else pattr[cc]=0;
532 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) pattr[j]=0;
533 goto ATTRLOOP;
534 }
535 break;
536 case 'R':
537 if (strchr("IVXLCDM",*page0)==NULL ||
538 (strchr("LCDM",*page0) && strchr(page_precedence,'A') && strlen(buff)==1 &&
539 pattr_prev[cc]!='R')) {
540 /* heuristic detection as alphabet since L=50, C=100, D=100, M=1000 are quite large */
541 if (pattr[cc]<pplen-1)
542 pattr[cc]++;
543 else pattr[cc]=0;
544 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) pattr[j]=0;
545 goto ATTRLOOP;
546 }
547 break;
548 case 'n':
549 if (*page0<'0' || *page0>'9') {
550 if (pattr[cc]<pplen-1)
551 pattr[cc]++;
552 else pattr[cc]=0;
553 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) pattr[j]=0;
554 goto ATTRLOOP;
555 }
556 break;
557 case 'a':
558 if (*page0<'a' || *page0>'z' || strlen(buff)>1 ||
559 (strchr("ivx",*page0) && strchr(page_precedence,'r') &&
560 pattr_prev[cc]!='a')) {
561 /* heuristic detection as roman number since I=1, V=5, X=10 are quite small */
562 if (pattr[cc]<pplen-1)
563 pattr[cc]++;
564 else pattr[cc]=0;
565 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) pattr[j]=0;
566 goto ATTRLOOP;
567 }
568 break;
569 case 'A':
570 if (*page0<'A' || *page0>'Z' || strlen(buff)>1 ||
571 (strchr("IVX",*page0) && strchr(page_precedence,'R') &&
572 pattr_prev[cc]!='A')) {
573 /* heuristic detection as roman number since I=1, V=5, X=10 are quite small */
574 if (pattr[cc]<pplen-1)
575 pattr[cc]++;
576 else pattr[cc]=0;
577 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) pattr[j]=0;
578 goto ATTRLOOP;
579 }
580 break;
581 default:
582 verb_printf(efp, "\nUnknown page type '%c' in page_precedence specification (%s).\n",
583 page_precedence[pattr[cc]], page_precedence);
584 exit(253);
585 }
586 pattr_prev[cc] = page_precedence[pattr[cc]];
587 }
588 }
589 p->attr[cc]=pattr[cc];
590 for (j=cc+1;j<PAGE_COMPOSIT_DEPTH;j++) p->attr[j]=-1;
591 }
592
593 char *mfgets(char *buf, int size, FILE *fp)
594 {
595 int c, len;
596
597 if ((len = input_line2(fp, (unsigned char *) buf, NULL, 0, size, &c)) == 0
598 && c != '\r' && c != '\n') return NULL;
599 if (c == '\n' || c == '\r') {
600 if (len+1 < size) strcat(buf+len, "\n");
601 else ungetc(c, fp);
602 }
603 if (c == EOF) return NULL;
604 return buf;
605 }