Debian Codegraph @

texk/upmendex/styfile.c

raw
1 #include "mendex.h"
2
3 #include <kpathsea/tex-file.h>
4
5 #include "exvar.h"
6
7 #include "kp.h"
8
9 FILE *fp;
10 static char icu_attr_str[STYBUFSIZE];
11
12 static void convline(char *buff1, int start, char *buff2);
13 static int scompare(char *buff1, const char *buff2);
14 static int getparam(char *buff, const char *paraname, char *param);
15 static int getparachar(char *buff, const char *paraname, char *param);
16 static size_t sstrlen(const char *buff);
17 static int sstrcmp(const char *s1, const char *s2);
18 static int sstrncmp(const char *s1, const char *s2, size_t len);
19 static int escape_mode=0;
20
21 static char *
22 bfgets (char *buf, int size, FILE *fp)
23 {
24 char *p, *q;
25 p = fgets (buf, size, fp);
26 for (q = buf; *q; q++) {
27 if(*q == '\r') {
28 *q++ = '\n';
29 *q = '\0';
30 break;
31 }
32 }
33 return p;
34 }
35 #define fgets bfgets
36
37 /* read style file */
38 void styread(const char *filename)
39 {
40 int i,j,q,cc;
41 char buff[4096],tmp[4096],tmp2[4096];
42
43 filename = KP_find_ist_file(filename);
44 if(kpse_in_name_ok(filename))
45 fp=fopen(filename,"rb");
46 else
47 fp=NULL;
48 if (fp==NULL) {
49 fprintf(stderr,"%s does not exist.\n",filename);
50 exit(255);
51 }
52 verb_printf(efp,"Scanning style file %s.",filename);
53
54 for (i=0;;i++) {
55 if (fgets(buff,4095,fp)==NULL) break;
56 if (getparam(buff,"keyword",keyword)) continue;
57 if (getparachar(buff,"arg_open",&arg_open)) continue;
58 if (getparachar(buff,"arg_close",&arg_close)) continue;
59 if (getparachar(buff,"range_open",&range_open)) continue;
60 if (getparachar(buff,"range_close",&range_close)) continue;
61 if (getparachar(buff,"level",&level)) continue;
62 if (getparachar(buff,"actual",&actual)) continue;
63 if (getparachar(buff,"encap",&encap)) continue;
64 if (getparachar(buff,"quote",&quote)) continue;
65 if (getparachar(buff,"escape",&escape)) continue;
66 if (getparam(buff,"preamble",preamble)) continue;
67 if (getparam(buff,"postamble",postamble)) continue;
68 if (getparam(buff,"setpage_prefix",setpage_prefix)) continue;
69 if (getparam(buff,"setpage_suffix",setpage_suffix)) continue;
70 if (getparam(buff,"group_skip",group_skip)) continue;
71 if (getparam(buff,"lethead_prefix",lethead_prefix)) continue;
72 if (getparam(buff,"heading_prefix",lethead_prefix)) continue;
73 if (getparam(buff,"lethead_suffix",lethead_suffix)) continue;
74 if (getparam(buff,"heading_suffix",lethead_suffix)) continue;
75 if (getparam(buff,"symhead_positive",symhead_positive)) continue;
76 if (getparam(buff,"symhead_negative",symhead_negative)) continue;
77 if (getparam(buff,"numhead_positive",numhead_positive)) continue;
78 if (getparam(buff,"numhead_negative",numhead_negative)) continue;
79 if ( (cc=scompare(buff,"lethead_flag")) != -1 ||
80 (cc=scompare(buff,"heading_flag")) != -1 ||
81 (cc=scompare(buff,"headings_flag")) != -1 ) {
82 lethead_flag=atoi(&buff[cc]);
83 continue;
84 }
85 cc=scompare(buff,"priority");
86 if (cc!= -1) {
87 priority=atoi(&buff[cc]);
88 continue;
89 }
90 q=0;
91 for (j=0;;j++) {
92 sprintf(tmp,"item_%d",j);
93 if (getparam(buff,tmp,item_0[j])) {
94 q=1;
95 break;
96 }
97 sprintf(tmp,"delim_%d",j);
98 if (getparam(buff,tmp,delim_0[j])) {
99 q=1;
100 break;
101 }
102 if (j==MAXDEPTH-1) break;
103 sprintf(tmp,"item_%d%d",j,j+1);
104 if (getparam(buff,tmp,item_01[j])) {
105 q=1;
106 break;
107 }
108 sprintf(tmp,"item_x%d",j+1);
109 if (getparam(buff,tmp,item_x[j])) {
110 q=1;
111 break;
112 }
113 }
114 if (q==1) continue;
115 if (getparam(buff,"delim_n",delim_n)) continue;
116 if (getparam(buff,"delim_r",delim_r)) continue;
117 if (getparam(buff,"delim_t",delim_t)) continue;
118 if (getparam(buff,"suffix_2p",suffix_2p)) continue;
119 if (getparam(buff,"suffix_3p",suffix_3p)) continue;
120 if (getparam(buff,"suffix_mp",suffix_mp)) continue;
121 if (getparam(buff,"encap_prefix",encap_prefix)) continue;
122 if (getparam(buff,"encap_infix",encap_infix)) continue;
123 if (getparam(buff,"encap_suffix",encap_suffix)) continue;
124 cc=scompare(buff,"line_max");
125 if (cc!= -1) {
126 line_max=atoi(&buff[cc]);
127 continue;
128 }
129 if (getparam(buff,"indent_space",indent_space)) continue;
130 cc=scompare(buff,"indent_length");
131 if (cc!= -1) {
132 indent_length=atoi(&buff[cc]);
133 continue;
134 }
135 if (getparam(buff,"symbol",symhead)) continue;
136 cc=scompare(buff,"symbol_flag");
137 if (cc!= -1) {
138 symbol_flag=atoi(&buff[cc]);
139 continue;
140 }
141 cc=scompare(buff,"letter_head");
142 if (cc!= -1) {
143 letter_head=atoi(&buff[cc]);
144 continue;
145 }
146 if (getparam(buff,"kana_head",tmp)) {
147 multibyte_to_widechar(kana_head,STYBUFSIZE,tmp);
148 continue;
149 }
150 if (getparam(buff,"hangul_head",tmp)) {
151 multibyte_to_widechar(hangul_head,STYBUFSIZE,tmp);
152 continue;
153 }
154 if (getparam(buff,"hanzi_head",tmp)) {
155 multibyte_to_widechar(hanzi_head,STYBUFSIZE,tmp);
156 continue;
157 }
158 if (getparam(buff,"thai_head",tmp)) {
159 multibyte_to_widechar(brahmic_head[BR_THAI],STYBUFSIZE,tmp);
160 continue;
161 }
162 if (getparam(buff,"devanagari_head",tmp)) {
163 multibyte_to_widechar(brahmic_head[BR_DEVA],STYBUFSIZE,tmp);
164 continue;
165 }
166 if (getparam(buff,"page_compositor",page_compositor)) continue;
167 if (getparam(buff,"page_precedence",page_precedence)) continue;
168 if (getparam(buff,"character_order",character_order)) continue;
169 if (getparam(buff,"icu_locale", icu_locale )) continue;
170 cc=scompare(buff,"icu_rules");
171 if (cc!= -1) {
172 escape_mode=1;
173 getparam(buff,"icu_rules",icu_rules);
174 escape_mode=0;
175 continue;
176 }
177 if (getparam(buff,"icu_attributes", icu_attr_str )) continue;
178
179 cc=scompare(buff,"script_preamble");
180 if (cc!= -1) {
181 strcpy(tmp,buff+strlen("script_preamble"));
182 if (getparam(tmp,"latin", script_preamble[CH_LATIN] )) continue;
183 if (getparam(tmp,"cyrillic", script_preamble[CH_CYRILLIC] )) continue;
184 if (getparam(tmp,"greek", script_preamble[CH_GREEK] )) continue;
185 if (getparam(tmp,"kana", script_preamble[CH_KANA] )) continue;
186 if (getparam(tmp,"hangul", script_preamble[CH_HANGUL] )) continue;
187 if (getparam(tmp,"hanzi", script_preamble[CH_HANZI] )) continue;
188 if (getparam(tmp,"arabic", script_preamble[CH_ARABIC] )) continue;
189 if (getparam(tmp,"hebrew", script_preamble[CH_HEBREW] )) continue;
190 if (getparam(tmp,"devanagari",script_preamble[CH_DEVANAGARI] )) continue;
191 if (getparam(tmp,"bengali", script_preamble[CH_BENGALI] )) continue;
192 if (getparam(tmp,"gurmukhi", script_preamble[CH_GURMUKHI] )) continue;
193 if (getparam(tmp,"gujarati", script_preamble[CH_GUJARATI] )) continue;
194 if (getparam(tmp,"oriya", script_preamble[CH_ORIYA] )) continue;
195 if (getparam(tmp,"tamil", script_preamble[CH_TAMIL] )) continue;
196 if (getparam(tmp,"telugu", script_preamble[CH_TELUGU] )) continue;
197 if (getparam(tmp,"kannada", script_preamble[CH_KANNADA] )) continue;
198 if (getparam(tmp,"malayalam", script_preamble[CH_MALAYALAM] )) continue;
199 if (getparam(tmp,"sinhala", script_preamble[CH_SINHALA] )) continue;
200 if (getparam(tmp,"thai", script_preamble[CH_THAI] )) continue;
201 if (getparam(tmp,"lao", script_preamble[CH_LAO] )) continue;
202 if (strlen(tmp)>0) {
203 verb_printf(efp,"\nWarning: Unknown script for specifier \"script_preamble\" (%s).", tmp);
204 }
205 continue;
206 }
207 cc=scompare(buff,"script_postamble");
208 if (cc!= -1) {
209 strcpy(tmp,buff+strlen("script_postamble"));
210 if (getparam(tmp,"latin", script_postamble[CH_LATIN] )) continue;
211 if (getparam(tmp,"cyrillic", script_postamble[CH_CYRILLIC] )) continue;
212 if (getparam(tmp,"greek", script_postamble[CH_GREEK] )) continue;
213 if (getparam(tmp,"kana", script_postamble[CH_KANA] )) continue;
214 if (getparam(tmp,"hangul", script_postamble[CH_HANGUL] )) continue;
215 if (getparam(tmp,"hanzi", script_postamble[CH_HANZI] )) continue;
216 if (getparam(tmp,"arabic", script_postamble[CH_ARABIC] )) continue;
217 if (getparam(tmp,"hebrew", script_postamble[CH_HEBREW] )) continue;
218 if (getparam(tmp,"devanagari",script_postamble[CH_DEVANAGARI] )) continue;
219 if (getparam(tmp,"bengali", script_postamble[CH_BENGALI] )) continue;
220 if (getparam(tmp,"gurmukhi", script_postamble[CH_GURMUKHI] )) continue;
221 if (getparam(tmp,"gujarati", script_postamble[CH_GUJARATI] )) continue;
222 if (getparam(tmp,"oriya", script_postamble[CH_ORIYA] )) continue;
223 if (getparam(tmp,"tamil", script_postamble[CH_TAMIL] )) continue;
224 if (getparam(tmp,"telugu", script_postamble[CH_TELUGU] )) continue;
225 if (getparam(tmp,"kannada", script_postamble[CH_KANNADA] )) continue;
226 if (getparam(tmp,"malayalam", script_postamble[CH_MALAYALAM] )) continue;
227 if (getparam(tmp,"sinhala", script_postamble[CH_SINHALA] )) continue;
228 if (getparam(tmp,"thai", script_postamble[CH_THAI] )) continue;
229 if (getparam(tmp,"lao", script_postamble[CH_LAO] )) continue;
230 if (strlen(tmp)>0) {
231 verb_printf(efp,"\nWarning: Unknown script for specifier \"script_postamble\" (%s).", tmp);
232 }
233 continue;
234 }
235 cc=scompare(buff,"script_head");
236 if (cc!= -1) {
237 strcpy(tmp,buff+strlen("script_head"));
238 if (getparam(tmp,"kana", tmp2)){ multibyte_to_widechar(kana_head ,STYBUFSIZE,tmp2); continue; }
239 if (getparam(tmp,"hangul", tmp2)){ multibyte_to_widechar(hangul_head ,STYBUFSIZE,tmp2); continue; }
240 if (getparam(tmp,"hanzi", tmp2)){ multibyte_to_widechar(hanzi_head ,STYBUFSIZE,tmp2); continue; }
241 if (getparam(tmp,"devanagari",tmp2)){ multibyte_to_widechar(brahmic_head[BR_DEVA],STYBUFSIZE,tmp2); continue; }
242 if (getparam(tmp,"bengali", tmp2)){ multibyte_to_widechar(brahmic_head[BR_BENG],STYBUFSIZE,tmp2); continue; }
243 if (getparam(tmp,"gurmukhi", tmp2)){ multibyte_to_widechar(brahmic_head[BR_GURU],STYBUFSIZE,tmp2); continue; }
244 if (getparam(tmp,"gujarati", tmp2)){ multibyte_to_widechar(brahmic_head[BR_GUJR],STYBUFSIZE,tmp2); continue; }
245 if (getparam(tmp,"oriya", tmp2)){ multibyte_to_widechar(brahmic_head[BR_ORYA],STYBUFSIZE,tmp2); continue; }
246 if (getparam(tmp,"tamil", tmp2)){ multibyte_to_widechar(brahmic_head[BR_TAML],STYBUFSIZE,tmp2); continue; }
247 if (getparam(tmp,"telugu", tmp2)){ multibyte_to_widechar(brahmic_head[BR_TELU],STYBUFSIZE,tmp2); continue; }
248 if (getparam(tmp,"kannada", tmp2)){ multibyte_to_widechar(brahmic_head[BR_KNDA],STYBUFSIZE,tmp2); continue; }
249 if (getparam(tmp,"malayalam", tmp2)){ multibyte_to_widechar(brahmic_head[BR_MLYM],STYBUFSIZE,tmp2); continue; }
250 if (getparam(tmp,"sinhala", tmp2)){ multibyte_to_widechar(brahmic_head[BR_SINH],STYBUFSIZE,tmp2); continue; }
251 if (getparam(tmp,"thai", tmp2)){ multibyte_to_widechar(brahmic_head[BR_THAI],STYBUFSIZE,tmp2); continue; }
252 if (getparam(tmp,"lao", tmp2)){ multibyte_to_widechar(brahmic_head[BR_LAO ],STYBUFSIZE,tmp2); continue; }
253 if (strlen(tmp)>0) {
254 verb_printf(efp,"\nWarning: Unknown script for specifier \"script_head\" (%s).", tmp);
255 }
256 continue;
257 }
258
259 cc=strcspn(buff," \t\r\n");
260 if (cc>0) buff[cc]='\0';
261 if (buff[0]=='%' || buff[0]=='\n') continue;
262 if (strlen(buff)>0) {
263 verb_printf(efp,"\nWarning: Unknown specifier (%s).", buff);
264 }
265 }
266 fclose(fp);
267
268 verb_printf(efp,"...done.\n");
269 }
270
271 /* analyze string parameter of style file */
272 static void convline(char *buff1, int start, char *buff2)
273 {
274 int i,j,cc;
275
276 for (i=start,j=cc=0;;i++) {
277 if (j==STYBUFSIZE-1) {
278 buff2[j]='\0';
279 break;
280 }
281 if (buff1[i]=='\"') {
282 if (cc==0) {
283 cc=1;
284 }
285 else {
286 buff2[j]='\0';
287 break;
288 }
289 }
290 else if ((buff1[i]=='\n')||(buff1[i]=='\0')) {
291
292 if (cc==1) {
293 buff2[j++]='\n';
294 }
295 if (fgets(buff1,4095,fp)==NULL) {
296 buff2[j]='\0';
297 break;
298 }
299 i= -1;
300 }
301 else if (cc==1) {
302 if ((unsigned char)buff1[i]>0x80) {
303 int len = multibytelen((unsigned char)buff1[i]);
304 if (len<0) {
305 verb_printf(efp,"\nWarning: Illegal input of lead byte 0x%x in UTF-8.", (unsigned char)buff1[i]);
306 continue;
307 }
308 else if (j+len>STYBUFSIZE-1) {
309 buff2[j]='\0';
310 break;
311 }
312 while(len--) {
313 buff2[j++]=buff1[i++];
314 }
315 i--;
316 continue;
317 }
318 else if (buff1[i]=='\\' && !escape_mode) {
319 i++;
320 if (buff1[i]=='\\') buff2[j]='\\';
321 else if (buff1[i]=='n') buff2[j]='\n';
322 else if (buff1[i]=='t') buff2[j]='\t';
323 else if (buff1[i]=='r') buff2[j]='\r';
324 else if (buff1[i]=='\"') buff2[j]='\"';
325 else {
326 /* Otherwise, the '\' is simply ignored here and the
327 following character is copied as is in the next loop. */
328 i--;
329 continue;
330 }
331 }
332 else buff2[j]=buff1[i];
333 j++;
334 }
335 }
336 }
337
338 /* compare strings */
339 static int scompare(char *buff1, const char *buff2)
340 {
341 int i;
342
343 if (sstrlen(buff1) == sstrlen(buff2)) {
344 if (sstrcmp(buff1,buff2) == 0) {
345 if (fgets(buff1,4095,fp)==NULL) return -1;
346 return 0;
347 }
348 return -1;
349 }
350 for (i=0;i<sstrlen(buff1)-sstrlen(buff2);i++) {
351 if (buff1[i]==' ' || buff1[i]=='\t') {
352 continue;
353 }
354 else if ((sstrncmp(&buff1[i],buff2,sstrlen(buff2))==0)
355 &&((buff1[i+strlen(buff2)]==' ')||(buff1[i+strlen(buff2)]=='\t')||(buff1[i+strlen(buff2)]=='\n'))) {
356 return i+strlen(buff2);
357 }
358 else return -1;
359 }
360 return -1;
361 }
362
363 /* get string of style patameter */
364 static int getparam(char *buff, const char *paraname, char *param)
365 {
366 int cc;
367
368 cc=scompare(buff,paraname);
369 if (cc!=-1) {
370 convline(buff,cc,param);
371 return 1;
372 }
373 return 0;
374 }
375
376 /* get character of style parameter */
377 static int getparachar(char *buff, const char *paraname, char *param)
378 {
379 int j,cc;
380
381 cc=scompare(buff,paraname);
382 if (cc!=-1) {
383 for (j=cc;j<strlen(buff);j++) {
384 if (buff[j]=='\'') {
385 (*param)=buff[j+1];
386 break;
387 }
388 else if (buff[j]=='\n') {
389 if (fgets(buff,4095,fp)==NULL) {
390 break;
391 }
392 else j= -1;
393 }
394 }
395 return 1;
396 }
397 return 0;
398 }
399
400 static size_t sstrlen(const char *s)
401 {
402 if (s == NULL) return 0;
403 return strlen(s);
404 }
405
406 static int sstrcmp(const char *s1, const char *s2)
407 {
408 if (s1 == NULL) return -1;
409 if (s2 == NULL) return 1;
410 return strcmp(s1, s2);
411 }
412
413 static int sstrncmp(const char *s1, const char *s2, size_t len)
414 {
415 if (s1 == NULL) return -1;
416 if (s2 == NULL) return 1;
417 return strncmp(s1, s2, len);
418 }
419
420 void set_icu_attributes(void)
421 {
422 int i,attr;
423 char *pos, *head, *tmp;
424
425 for (i=0;i<UCOL_ATTRIBUTE_COUNT;i++) icu_attributes[i]=UCOL_DEFAULT;
426 tmp=icu_attr_str;
427 head=strtok(tmp, " \t\r\n");
428 while (head!=NULL) {
429 pos=head;
430 if (strncmp(pos,"alternate:",10)==0) {
431 pos+=10; attr=UCOL_ALTERNATE_HANDLING;
432 if (strcmp(pos,"shifted")==0) icu_attributes[attr]=UCOL_SHIFTED;
433 else if (strcmp(pos,"non-ignorable")==0) icu_attributes[attr]=UCOL_NON_IGNORABLE;
434 else goto illegal;
435 } else
436 if (strncmp(pos,"strength:",9)==0) {
437 pos+=9; attr=UCOL_STRENGTH;
438 if (strcmp(pos,"primary")==0) icu_attributes[attr]=UCOL_PRIMARY;
439 else if (strcmp(pos,"secondary")==0) icu_attributes[attr]=UCOL_SECONDARY;
440 else if (strcmp(pos,"tertiary")==0) icu_attributes[attr]=UCOL_TERTIARY;
441 else if (strcmp(pos,"quaternary")==0) icu_attributes[attr]=UCOL_QUATERNARY;
442 else if (strcmp(pos,"identical")==0) icu_attributes[attr]=UCOL_IDENTICAL;
443 else goto illegal;
444 } else
445 if (strncmp(pos,"french-collation:",17)==0) {
446 pos+=17; attr=UCOL_FRENCH_COLLATION;
447 if (strcmp(pos,"on")==0) icu_attributes[attr]=UCOL_ON;
448 else if (strcmp(pos,"off")==0) icu_attributes[attr]=UCOL_OFF;
449 else goto illegal;
450 } else
451 if (strncmp(pos,"case-first:",11)==0) {
452 pos+=11; attr=UCOL_CASE_FIRST;
453 if (strcmp(pos,"off")==0) icu_attributes[attr]=UCOL_OFF;
454 else if (strcmp(pos,"upper-first")==0) icu_attributes[attr]=UCOL_UPPER_FIRST;
455 else if (strcmp(pos,"lower-first")==0) icu_attributes[attr]=UCOL_LOWER_FIRST;
456 else goto illegal;
457 } else
458 if (strncmp(pos,"case-level:",11)==0) {
459 pos+=11; attr=UCOL_CASE_LEVEL;
460 if (strcmp(pos,"on")==0) icu_attributes[attr]=UCOL_ON;
461 else if (strcmp(pos,"off")==0) icu_attributes[attr]=UCOL_OFF;
462 else goto illegal;
463 } else
464 if (strncmp(pos,"normalization-mode:",19)==0) {
465 pos+=19; attr=UCOL_NORMALIZATION_MODE;
466 if (strcmp(pos,"on")==0) icu_attributes[attr]=UCOL_ON;
467 else if (strcmp(pos,"off")==0) icu_attributes[attr]=UCOL_OFF;
468 else goto illegal;
469 } else
470 if (strncmp(pos,"numeric-ordering:",17)==0) {
471 pos+=17; attr=UCOL_NUMERIC_COLLATION;
472 if (strcmp(pos,"on")==0) icu_attributes[attr]=UCOL_ON;
473 else if (strcmp(pos,"off")==0) icu_attributes[attr]=UCOL_OFF;
474 else goto illegal;
475 } else {
476 illegal:
477 verb_printf(efp,"\nWarning: Illegal input for icu_attributes (%s).",head);
478 }
479 head=strtok(NULL, " \t\r\n");
480 }
481 }