| 1 |
#include "mendex.h" |
| 2 |
|
| 3 |
#include <kpathsea/tex-file.h> |
| 4 |
#include <ptexenc/ptexenc.h> |
| 5 |
|
| 6 |
#include "exvar.h" |
| 7 |
|
| 8 |
#include "kp.h" |
| 9 |
|
| 10 |
FILE *fp; |
| 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 |
|
| 20 |
static char * |
| 21 |
bfgets (char *buf, int size, FILE *fp) |
| 22 |
{ |
| 23 |
char *p, *q; |
| 24 |
p = mfgets (buf, size, fp); |
| 25 |
for (q = buf; *q; q++) { |
| 26 |
if(*q == '\r') { |
| 27 |
*q++ = '\n'; |
| 28 |
*q = '\0'; |
| 29 |
break; |
| 30 |
} |
| 31 |
} |
| 32 |
return p; |
| 33 |
} |
| 34 |
#define fgets bfgets |
| 35 |
|
| 36 |
/* read style file */ |
| 37 |
void styread(const char *filename) |
| 38 |
{ |
| 39 |
int i,cc; |
| 40 |
char buff[4096]; |
| 41 |
|
| 42 |
filename = KP_find_ist_file(filename); |
| 43 |
if(kpse_in_name_ok(filename)) |
| 44 |
fp=nkf_open(filename,"rb"); |
| 45 |
else |
| 46 |
fp=NULL; |
| 47 |
if (fp==NULL) { |
| 48 |
fprintf(stderr,"%s does not exist.\n",filename); |
| 49 |
exit(255); |
| 50 |
} |
| 51 |
verb_printf(efp,"Scanning style file %s.",filename); |
| 52 |
|
| 53 |
for (i=0;;i++) { |
| 54 |
if (fgets(buff,4095,fp)==NULL) break; |
| 55 |
if (getparam(buff,"keyword",keyword)) continue; |
| 56 |
if (getparachar(buff,"arg_open",&arg_open)) continue; |
| 57 |
if (getparachar(buff,"arg_close",&arg_close)) continue; |
| 58 |
if (getparachar(buff,"range_open",&range_open)) continue; |
| 59 |
if (getparachar(buff,"range_close",&range_close)) continue; |
| 60 |
if (getparachar(buff,"level",&level)) continue; |
| 61 |
if (getparachar(buff,"actual",&actual)) continue; |
| 62 |
if (getparachar(buff,"encap",&encap)) continue; |
| 63 |
if (getparachar(buff,"quote","e)) continue; |
| 64 |
if (getparachar(buff,"escape",&escape)) continue; |
| 65 |
if (getparam(buff,"preamble",preamble)) continue; |
| 66 |
if (getparam(buff,"postamble",postamble)) continue; |
| 67 |
if (getparam(buff,"setpage_prefix",setpage_prefix)) continue; |
| 68 |
if (getparam(buff,"setpage_suffix",setpage_suffix)) continue; |
| 69 |
if (getparam(buff,"group_skip",group_skip)) continue; |
| 70 |
if (getparam(buff,"lethead_prefix",lethead_prefix)) continue; |
| 71 |
if (getparam(buff,"heading_prefix",lethead_prefix)) continue; |
| 72 |
if (getparam(buff,"lethead_suffix",lethead_suffix)) continue; |
| 73 |
if (getparam(buff,"heading_suffix",lethead_suffix)) continue; |
| 74 |
if (getparam(buff,"symhead_positive",symhead_positive)) continue; |
| 75 |
if (getparam(buff,"symhead_negative",symhead_negative)) continue; |
| 76 |
if (getparam(buff,"numhead_positive",numhead_positive)) continue; |
| 77 |
if (getparam(buff,"numhead_negative",numhead_negative)) continue; |
| 78 |
if ( (cc=scompare(buff,"lethead_flag")) != -1 || |
| 79 |
(cc=scompare(buff,"heading_flag")) != -1 || |
| 80 |
(cc=scompare(buff,"headings_flag")) != -1 ) { |
| 81 |
lethead_flag=atoi(&buff[cc]); |
| 82 |
continue; |
| 83 |
} |
| 84 |
cc=scompare(buff,"priority"); |
| 85 |
if (cc!= -1) { |
| 86 |
priority=atoi(&buff[cc]); |
| 87 |
continue; |
| 88 |
} |
| 89 |
if (getparam(buff,"item_0",item_0)) continue; |
| 90 |
if (getparam(buff,"item_1",item_1)) continue; |
| 91 |
if (getparam(buff,"item_2",item_2)) continue; |
| 92 |
if (getparam(buff,"item_01",item_01)) continue; |
| 93 |
if (getparam(buff,"item_x1",item_x1)) continue; |
| 94 |
if (getparam(buff,"item_12",item_12)) continue; |
| 95 |
if (getparam(buff,"item_x2",item_x2)) continue; |
| 96 |
if (getparam(buff,"delim_0",delim_0)) continue; |
| 97 |
if (getparam(buff,"delim_1",delim_1)) continue; |
| 98 |
if (getparam(buff,"delim_2",delim_2)) continue; |
| 99 |
if (getparam(buff,"delim_n",delim_n)) continue; |
| 100 |
if (getparam(buff,"delim_r",delim_r)) continue; |
| 101 |
if (getparam(buff,"delim_t",delim_t)) continue; |
| 102 |
if (getparam(buff,"suffix_2p",suffix_2p)) continue; |
| 103 |
if (getparam(buff,"suffix_3p",suffix_3p)) continue; |
| 104 |
if (getparam(buff,"suffix_mp",suffix_mp)) continue; |
| 105 |
if (getparam(buff,"encap_prefix",encap_prefix)) continue; |
| 106 |
if (getparam(buff,"encap_infix",encap_infix)) continue; |
| 107 |
if (getparam(buff,"encap_suffix",encap_suffix)) continue; |
| 108 |
cc=scompare(buff,"line_max"); |
| 109 |
if (cc!= -1) { |
| 110 |
line_max=atoi(&buff[cc]); |
| 111 |
continue; |
| 112 |
} |
| 113 |
if (getparam(buff,"indent_space",indent_space)) continue; |
| 114 |
cc=scompare(buff,"indent_length"); |
| 115 |
if (cc!= -1) { |
| 116 |
indent_length=atoi(&buff[cc]); |
| 117 |
continue; |
| 118 |
} |
| 119 |
if (getparam(buff,"symbol",symhead)) continue; |
| 120 |
cc=scompare(buff,"symbol_flag"); |
| 121 |
if (cc!= -1) { |
| 122 |
symbol_flag=atoi(&buff[cc]); |
| 123 |
continue; |
| 124 |
} |
| 125 |
cc=scompare(buff,"letter_head"); |
| 126 |
if (cc!= -1) { |
| 127 |
letter_head=atoi(&buff[cc]); |
| 128 |
continue; |
| 129 |
} |
| 130 |
if (getparam(buff,"page_compositor",page_compositor)) continue; |
| 131 |
if (getparam(buff,"page_precedence",page_precedence)) continue; |
| 132 |
if (getparam(buff,"character_order",character_order)) continue; |
| 133 |
|
| 134 |
cc=strcspn(buff," \t\r\n"); |
| 135 |
if (cc>0) buff[cc]='\0'; |
| 136 |
if (buff[0]=='%' || buff[0]=='\n') continue; |
| 137 |
if (strlen(buff)>0) { |
| 138 |
verb_printf(efp,"\nWarning: Unknown specifier (%s).", buff); |
| 139 |
} |
| 140 |
} |
| 141 |
nkf_close(fp); |
| 142 |
|
| 143 |
verb_printf(efp,"...done.\n"); |
| 144 |
} |
| 145 |
|
| 146 |
/* analyze string parameter of style file */ |
| 147 |
static void convline(char *buff1, int start, char *buff2) |
| 148 |
{ |
| 149 |
int i,j,cc; |
| 150 |
|
| 151 |
for (i=start,j=cc=0;;i++) { |
| 152 |
if (j==STYBUFSIZE-1) { |
| 153 |
buff2[j]='\0'; |
| 154 |
break; |
| 155 |
} |
| 156 |
if (buff1[i]=='\"') { |
| 157 |
if (cc==0) { |
| 158 |
cc=1; |
| 159 |
} |
| 160 |
else { |
| 161 |
buff2[j]='\0'; |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
else if ((buff1[i]=='\n')||(buff1[i]=='\0')) { |
| 166 |
|
| 167 |
if (cc==1) { |
| 168 |
buff2[j++]='\n'; |
| 169 |
} |
| 170 |
if (fgets(buff1,4095,fp)==NULL) { |
| 171 |
buff2[j]='\0'; |
| 172 |
break; |
| 173 |
} |
| 174 |
i= -1; |
| 175 |
} |
| 176 |
else if (cc==1) { |
| 177 |
if ((unsigned char)buff1[i]>0x80) { |
| 178 |
int len = multibytelen((unsigned char)buff1[i]); |
| 179 |
if (len<0) { |
| 180 |
verb_printf(efp,"\nWarning: Illegal input of lead byte 0x%x in UTF-8.", (unsigned char)buff1[i]); |
| 181 |
continue; |
| 182 |
} |
| 183 |
else if (j+len>STYBUFSIZE-1) { |
| 184 |
buff2[j]='\0'; |
| 185 |
break; |
| 186 |
} |
| 187 |
while(len--) { |
| 188 |
buff2[j++]=buff1[i++]; |
| 189 |
} |
| 190 |
i--; |
| 191 |
continue; |
| 192 |
} |
| 193 |
else if (buff1[i]=='\\') { |
| 194 |
i++; |
| 195 |
if (buff1[i]=='\\') buff2[j]='\\'; |
| 196 |
else if (buff1[i]=='n') buff2[j]='\n'; |
| 197 |
else if (buff1[i]=='t') buff2[j]='\t'; |
| 198 |
else if (buff1[i]=='r') buff2[j]='\r'; |
| 199 |
else if (buff1[i]=='\"') buff2[j]='\"'; |
| 200 |
else { |
| 201 |
/* Otherwise, the '\' is simply ignored here and the |
| 202 |
following character is copied as is in the next loop. */ |
| 203 |
i--; |
| 204 |
continue; |
| 205 |
} |
| 206 |
} |
| 207 |
else buff2[j]=buff1[i]; |
| 208 |
j++; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/* compare strings */ |
| 214 |
static int scompare(char *buff1, const char *buff2) |
| 215 |
{ |
| 216 |
int i; |
| 217 |
|
| 218 |
if (sstrlen(buff1) == sstrlen(buff2)) { |
| 219 |
if (sstrcmp(buff1,buff2) == 0) { |
| 220 |
if (fgets(buff1,4095,fp)==NULL) return -1; |
| 221 |
return 0; |
| 222 |
} |
| 223 |
return -1; |
| 224 |
} |
| 225 |
for (i=0;i<sstrlen(buff1)-sstrlen(buff2);i++) { |
| 226 |
if (buff1[i]==' ' || buff1[i]=='\t') { |
| 227 |
continue; |
| 228 |
} |
| 229 |
else if ((sstrncmp(&buff1[i],buff2,sstrlen(buff2))==0) |
| 230 |
&&((buff1[i+strlen(buff2)]==' ')||(buff1[i+strlen(buff2)]=='\t')||(buff1[i+strlen(buff2)]=='\n'))) { |
| 231 |
return i+strlen(buff2); |
| 232 |
} |
| 233 |
else return -1; |
| 234 |
} |
| 235 |
return -1; |
| 236 |
} |
| 237 |
|
| 238 |
/* get string of style patameter */ |
| 239 |
static int getparam(char *buff, const char *paraname, char *param) |
| 240 |
{ |
| 241 |
int cc; |
| 242 |
|
| 243 |
cc=scompare(buff,paraname); |
| 244 |
if (cc!=-1) { |
| 245 |
convline(buff,cc,param); |
| 246 |
return 1; |
| 247 |
} |
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
/* get character of style parameter */ |
| 252 |
static int getparachar(char *buff, const char *paraname, char *param) |
| 253 |
{ |
| 254 |
int j,cc; |
| 255 |
|
| 256 |
cc=scompare(buff,paraname); |
| 257 |
if (cc!=-1) { |
| 258 |
for (j=cc;j<strlen(buff);j++) { |
| 259 |
if (buff[j]=='\'') { |
| 260 |
(*param)=buff[j+1]; |
| 261 |
break; |
| 262 |
} |
| 263 |
else if (buff[j]=='\n') { |
| 264 |
if (fgets(buff,4095,fp)==NULL) { |
| 265 |
break; |
| 266 |
} |
| 267 |
else j= -1; |
| 268 |
} |
| 269 |
} |
| 270 |
return 1; |
| 271 |
} |
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
static size_t sstrlen(const char *s) |
| 276 |
{ |
| 277 |
if (s == NULL) return 0; |
| 278 |
return strlen(s); |
| 279 |
} |
| 280 |
|
| 281 |
static int sstrcmp(const char *s1, const char *s2) |
| 282 |
{ |
| 283 |
if (s1 == NULL) return -1; |
| 284 |
if (s2 == NULL) return 1; |
| 285 |
return strcmp(s1, s2); |
| 286 |
} |
| 287 |
|
| 288 |
static int sstrncmp(const char *s1, const char *s2, size_t len) |
| 289 |
{ |
| 290 |
if (s1 == NULL) return -1; |
| 291 |
if (s2 == NULL) return 1; |
| 292 |
return strncmp(s1, s2, len); |
| 293 |
} |