summaryrefslogtreecommitdiff
path: root/Build/source/texk/mendexk/fwrite.c
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/texk/mendexk/fwrite.c')
-rw-r--r--Build/source/texk/mendexk/fwrite.c146
1 files changed, 86 insertions, 60 deletions
diff --git a/Build/source/texk/mendexk/fwrite.c b/Build/source/texk/mendexk/fwrite.c
index 80e7b77cbba..81030e7e94a 100644
--- a/Build/source/texk/mendexk/fwrite.c
+++ b/Build/source/texk/mendexk/fwrite.c
@@ -17,13 +17,39 @@ static int range_check(struct index ind, int count, char *lbuff);
static void linecheck(char *lbuff, char *tmpbuff);
static void crcheck(char *lbuff, FILE *fp);
-/* OS X defines snprintf() as macro that chokes on using TAIL_LEN */
-#ifdef __APPLE__
-#undef snprintf
-#endif
-
#define BUFFERLEN 4096
-#define TAIL_LEN(x) ((x)+strlen(x)), (BUFFERLEN-strlen(x))
+
+#ifdef HAVE___VA_ARGS__
+/* Use C99 variadic macros if they are supported. */
+#define SPRINTF(buf, ...) \
+ snprintf(buf, sizeof(buf), __VA_ARGS__)
+#define SAPPENDF(buf, ...) \
+ snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), __VA_ARGS__)
+#else
+/* Alternatively use static inline functions (all buffers have size BUFFERLEN). */
+static inline int SPRINTF(char *buf, const char *format, ...)
+{
+ va_list argptr;
+ int n;
+
+ va_start(argptr, format);
+ n = vsnprintf(buf, BUFFERLEN, format, argptr);
+ va_end(argptr);
+
+ return n;
+}
+static inline int SAPPENDF(char *buf, const char *format, ...)
+{
+ va_list argptr;
+ int n;
+
+ va_start(argptr, format);
+ n = vsnprintf(buf + strlen(buf), BUFFERLEN - strlen(buf), format, argptr);
+ va_end(argptr);
+
+ return n;
+}
+#endif
static void fprint_euc_char(FILE *fp, const char a, const char b)
{
@@ -127,7 +153,7 @@ void indwrite(char *filename, struct index *ind, int pagenum)
fprintf(fp,"%s%s%s",lethead_prefix,symhead_negative,lethead_suffix);
}
}
- snprintf(lbuff,BUFFERLEN,"%s%s",item_0,ind[i].idx[0]);
+ SPRINTF(lbuff,"%s%s",item_0,ind[i].idx[0]);
}
else if (alphabet(ind[i].dic[0][0])) {
if (lethead_flag>0) {
@@ -136,7 +162,7 @@ void indwrite(char *filename, struct index *ind, int pagenum)
else if (lethead_flag<0) {
fprintf(fp,"%s%c%s",lethead_prefix,ind[i].dic[0][0]+32,lethead_suffix);
}
- snprintf(lbuff,BUFFERLEN,"%s%s",item_0,ind[i].idx[0]);
+ SPRINTF(lbuff,"%s%s",item_0,ind[i].idx[0]);
}
else if (japanese(ind[i].dic[0])) {
if (lethead_flag) {
@@ -153,7 +179,7 @@ void indwrite(char *filename, struct index *ind, int pagenum)
}
fputs(lethead_suffix,fp);
}
- snprintf(lbuff,BUFFERLEN,"%s%s",item_0,ind[i].idx[0]);
+ SPRINTF(lbuff,"%s%s",item_0,ind[i].idx[0]);
for (hpoint=0;hpoint<(strlen(datama)/2);hpoint++) {
if ((unsigned char)ind[i].dic[0][1]<(unsigned char)datama[hpoint*2+1]) {
break;
@@ -162,18 +188,18 @@ void indwrite(char *filename, struct index *ind, int pagenum)
}
switch (ind[i].words) {
case 1:
- snprintf(TAIL_LEN(lbuff),"%s",delim_0);
+ SAPPENDF(lbuff,"%s",delim_0);
break;
case 2:
- snprintf(TAIL_LEN(lbuff),"%s%s",item_x1,ind[i].idx[1]);
- snprintf(TAIL_LEN(lbuff),"%s",delim_1);
+ SAPPENDF(lbuff,"%s%s",item_x1,ind[i].idx[1]);
+ SAPPENDF(lbuff,"%s",delim_1);
break;
case 3:
- snprintf(TAIL_LEN(lbuff),"%s%s",item_x1,ind[i].idx[1]);
- snprintf(TAIL_LEN(lbuff),"%s%s",item_x2,ind[i].idx[2]);
- snprintf(TAIL_LEN(lbuff),"%s",delim_2);
+ SAPPENDF(lbuff,"%s%s",item_x1,ind[i].idx[1]);
+ SAPPENDF(lbuff,"%s%s",item_x2,ind[i].idx[2]);
+ SAPPENDF(lbuff,"%s",delim_2);
break;
default:
@@ -229,41 +255,41 @@ void indwrite(char *filename, struct index *ind, int pagenum)
switch (ind[i].words) {
case 1:
- snprintf(TAIL_LEN(lbuff),"%s%s%s",item_0,ind[i].idx[0],delim_0);
+ SAPPENDF(lbuff,"%s%s%s",item_0,ind[i].idx[0],delim_0);
break;
case 2:
if (strcmp(ind[i-1].idx[0],ind[i].idx[0])!=0 || strcmp(ind[i-1].dic[0],ind[i].dic[0])!=0) {
- snprintf(TAIL_LEN(lbuff),"%s%s%s",item_0,ind[i].idx[0],item_x1);
+ SAPPENDF(lbuff,"%s%s%s",item_0,ind[i].idx[0],item_x1);
}
else {
if (ind[i-1].words==1) {
- snprintf(TAIL_LEN(lbuff),"%s",item_01);
+ SAPPENDF(lbuff,"%s",item_01);
}
else {
- snprintf(TAIL_LEN(lbuff),"%s",item_1);
+ SAPPENDF(lbuff,"%s",item_1);
}
}
- snprintf(TAIL_LEN(lbuff),"%s",ind[i].idx[1]);
- snprintf(TAIL_LEN(lbuff),"%s",delim_1);
+ SAPPENDF(lbuff,"%s",ind[i].idx[1]);
+ SAPPENDF(lbuff,"%s",delim_1);
break;
case 3:
if (strcmp(ind[i-1].idx[0],ind[i].idx[0])!=0 || strcmp(ind[i-1].dic[0],ind[i].dic[0])!=0) {
- snprintf(TAIL_LEN(lbuff),"%s%s",item_0,ind[i].idx[0]);
- snprintf(TAIL_LEN(lbuff),"%s%s%s",item_x1,ind[i].idx[1],item_x2);
+ SAPPENDF(lbuff,"%s%s",item_0,ind[i].idx[0]);
+ SAPPENDF(lbuff,"%s%s%s",item_x1,ind[i].idx[1],item_x2);
}
else if (ind[i-1].words==1) {
- snprintf(TAIL_LEN(lbuff),"%s%s%s",item_01,ind[i].idx[1],item_x2);
+ SAPPENDF(lbuff,"%s%s%s",item_01,ind[i].idx[1],item_x2);
}
else if (strcmp(ind[i-1].idx[1],ind[i].idx[1])!=0 || strcmp(ind[i-1].dic[1],ind[i].dic[1])!=0) {
- if (ind[i-1].words==2) snprintf(TAIL_LEN(lbuff),"%s%s%s",item_1,ind[i].idx[1],item_12);
- else snprintf(TAIL_LEN(lbuff),"%s%s%s",item_1,ind[i].idx[1],item_x2);
+ if (ind[i-1].words==2) SAPPENDF(lbuff,"%s%s%s",item_1,ind[i].idx[1],item_12);
+ else SAPPENDF(lbuff,"%s%s%s",item_1,ind[i].idx[1],item_x2);
}
else {
- snprintf(TAIL_LEN(lbuff),"%s",item_2);
+ SAPPENDF(lbuff,"%s",item_2);
}
- snprintf(TAIL_LEN(lbuff),"%s%s",ind[i].idx[2],delim_2);
+ SAPPENDF(lbuff,"%s%s",ind[i].idx[2],delim_2);
break;
default:
@@ -300,25 +326,25 @@ static void printpage(struct index *ind, FILE *fp, int num, char *lbuff)
|| ind[num].p[j].enc[0]==range_close)
ind[num].p[j].enc++;
if (strlen(ind[num].p[j].enc)>0) {
- snprintf(buff,BUFFERLEN,"%s%s%s",encap_prefix,ind[num].p[j].enc,encap_infix);
+ SPRINTF(buff,"%s%s%s",encap_prefix,ind[num].p[j].enc,encap_infix);
}
if (strlen(suffix_3p)>0 && (pnumconv(ind[num].p[cc].page,ind[num].p[cc].attr[0])-pnumconv(ind[num].p[j].page,ind[num].p[j].attr[0]))==2) {
- snprintf(TAIL_LEN(buff),"%s%s",ind[num].p[j].page,suffix_3p);
+ SAPPENDF(buff,"%s%s",ind[num].p[j].page,suffix_3p);
}
else if (strlen(suffix_mp)>0 && (pnumconv(ind[num].p[cc].page,ind[num].p[cc].attr[0])-pnumconv(ind[num].p[j].page,ind[num].p[j].attr[0]))>=2) {
- snprintf(TAIL_LEN(buff),"%s%s",ind[num].p[j].page,suffix_mp);
+ SAPPENDF(buff,"%s%s",ind[num].p[j].page,suffix_mp);
}
else if (strlen(suffix_2p)>0 && (pnumconv(ind[num].p[cc].page,ind[num].p[cc].attr[0])-pnumconv(ind[num].p[j].page,ind[num].p[j].attr[0]))==1) {
- snprintf(TAIL_LEN(buff),"%s%s",ind[num].p[j].page,suffix_2p);
+ SAPPENDF(buff,"%s%s",ind[num].p[j].page,suffix_2p);
}
else {
- snprintf(TAIL_LEN(buff),"%s%s",ind[num].p[j].page,delim_r);
- snprintf(TAIL_LEN(buff),"%s",ind[num].p[cc].page);
+ SAPPENDF(buff,"%s%s",ind[num].p[j].page,delim_r);
+ SAPPENDF(buff,"%s",ind[num].p[cc].page);
}
- snprintf(TAIL_LEN(tmpbuff),"%s",buff);
+ SAPPENDF(tmpbuff,"%s",buff);
buff[0]='\0';
if (strlen(ind[num].p[j].enc)>0) {
- snprintf(TAIL_LEN(tmpbuff),"%s",encap_suffix);
+ SAPPENDF(tmpbuff,"%s",encap_suffix);
}
linecheck(lbuff,tmpbuff);
j=cc;
@@ -326,53 +352,53 @@ static void printpage(struct index *ind, FILE *fp, int num, char *lbuff)
goto PRINT;
}
else {
- snprintf(TAIL_LEN(tmpbuff),"%s",delim_n);
+ SAPPENDF(tmpbuff,"%s",delim_n);
linecheck(lbuff,tmpbuff);
}
}
else if (strlen(ind[num].p[j].enc)>0) {
/* normal encap */
if (ind[num].p[j].enc[0]==range_close) {
- snprintf(errbuff,BUFFERLEN,"Warning: Unmatched range closing operator \'%c\',",range_close);
- for (i=0;i<ind[num].words;i++) snprintf(TAIL_LEN(errbuff),"%s.",ind[num].idx[i]);
+ SPRINTF(errbuff,"Warning: Unmatched range closing operator \'%c\',",range_close);
+ for (i=0;i<ind[num].words;i++) SAPPENDF(errbuff,"%s.",ind[num].idx[i]);
warn_printf(efp, "%s\n", errbuff);
ind[num].p[j].enc++;
}
if (strlen(ind[num].p[j].enc)>0) {
- snprintf(TAIL_LEN(tmpbuff),"%s%s%s",encap_prefix,ind[num].p[j].enc,encap_infix);
- snprintf(TAIL_LEN(tmpbuff),"%s%s%s",ind[num].p[j].page,encap_suffix,delim_n);
+ SAPPENDF(tmpbuff,"%s%s%s",encap_prefix,ind[num].p[j].enc,encap_infix);
+ SAPPENDF(tmpbuff,"%s%s%s",ind[num].p[j].page,encap_suffix,delim_n);
linecheck(lbuff,tmpbuff);
}
else {
- snprintf(TAIL_LEN(tmpbuff),"%s%s",ind[num].p[j].page,delim_n);
+ SAPPENDF(tmpbuff,"%s%s",ind[num].p[j].page,delim_n);
linecheck(lbuff,tmpbuff);
}
}
else {
/* no encap */
- snprintf(TAIL_LEN(tmpbuff),"%s%s",ind[num].p[j].page,delim_n);
+ SAPPENDF(tmpbuff,"%s%s",ind[num].p[j].page,delim_n);
linecheck(lbuff,tmpbuff);
}
}
if (ind[num].p[j].enc[0]==range_open) {
- snprintf(errbuff,BUFFERLEN,"Warning: Unmatched range opening operator \'%c\',",range_open);
- for (k=0;k<ind[num].words;k++) snprintf(TAIL_LEN(errbuff),"%s.",ind[num].idx[k]);
+ SPRINTF(errbuff,"Warning: Unmatched range opening operator \'%c\',",range_open);
+ for (k=0;k<ind[num].words;k++) SAPPENDF(errbuff,"%s.",ind[num].idx[k]);
warn_printf(efp, "%s\n", errbuff);
ind[num].p[j].enc++;
}
else if (ind[num].p[j].enc[0]==range_close) {
- snprintf(errbuff,BUFFERLEN,"Warning: Unmatched range closing operator \'%c\',",range_close);
- for (k=0;k<ind[num].words;k++) snprintf(TAIL_LEN(errbuff),"%s.",ind[num].idx[k]);
+ SPRINTF(errbuff,"Warning: Unmatched range closing operator \'%c\',",range_close);
+ for (k=0;k<ind[num].words;k++) SAPPENDF(errbuff,"%s.",ind[num].idx[k]);
warn_printf(efp, "%s\n", errbuff);
ind[num].p[j].enc++;
}
if (strlen(ind[num].p[j].enc)>0) {
- snprintf(TAIL_LEN(tmpbuff),"%s%s%s",encap_prefix,ind[num].p[j].enc,encap_infix);
- snprintf(TAIL_LEN(tmpbuff),"%s%s",ind[num].p[j].page,encap_suffix);
+ SAPPENDF(tmpbuff,"%s%s%s",encap_prefix,ind[num].p[j].enc,encap_infix);
+ SAPPENDF(tmpbuff,"%s%s",ind[num].p[j].page,encap_suffix);
}
else {
- snprintf(TAIL_LEN(tmpbuff),"%s",ind[num].p[j].page);
+ SAPPENDF(tmpbuff,"%s",ind[num].p[j].page);
}
linecheck(lbuff,tmpbuff);
@@ -389,8 +415,8 @@ static int range_check(struct index ind, int count, char *lbuff)
for (i=count;i<ind.num+1;i++) {
if (ind.p[i].enc[0]==range_close) {
- snprintf(errbuff,BUFFERLEN,"Warning: Unmatched range closing operator \'%c\',",range_close);
- snprintf(TAIL_LEN(errbuff),"%s.",ind.idx[0]);
+ SPRINTF(errbuff,"Warning: Unmatched range closing operator \'%c\',",range_close);
+ SAPPENDF(errbuff,"%s.",ind.idx[0]);
warn_printf(efp, "%s\n", errbuff);
ind.p[i].enc++;
}
@@ -406,21 +432,21 @@ static int range_check(struct index ind, int count, char *lbuff)
break;
}
else if (j!=i && ind.p[j].enc[0]==range_open) {
- snprintf(errbuff,BUFFERLEN,"Warning: Unmatched range opening operator \'%c\',",range_open);
- for (k=0;k<ind.words;k++) snprintf(TAIL_LEN(errbuff),"%s.",ind.idx[k]);
+ SPRINTF(errbuff,"Warning: Unmatched range opening operator \'%c\',",range_open);
+ for (k=0;k<ind.words;k++) SAPPENDF(errbuff,"%s.",ind.idx[k]);
warn_printf(efp, "%s\n", errbuff);
ind.p[j].enc++;
}
if (strlen(ind.p[j].enc)>0) {
- snprintf(tmpbuff,BUFFERLEN,"%s%s%s",encap_prefix,ind.p[j].enc,encap_infix);
- snprintf(TAIL_LEN(tmpbuff),"%s%s%s",ind.p[j].page,encap_suffix,delim_n);
+ SPRINTF(tmpbuff,"%s%s%s",encap_prefix,ind.p[j].enc,encap_infix);
+ SAPPENDF(tmpbuff,"%s%s%s",ind.p[j].page,encap_suffix,delim_n);
linecheck(lbuff,tmpbuff);
}
}
}
if (j==ind.num+1) {
- snprintf(errbuff,BUFFERLEN,"Warning: Unmatched range opening operator \'%c\',",range_open);
- for (k=0;k<ind.words;k++) snprintf(TAIL_LEN(errbuff),"%s.",ind.idx[k]);
+ SPRINTF(errbuff,"Warning: Unmatched range opening operator \'%c\',",range_open);
+ for (k=0;k<ind.words;k++) SAPPENDF(errbuff,"%s.",ind.idx[k]);
warn_printf(efp, "%s\n", errbuff);
}
i=j-1;
@@ -452,12 +478,12 @@ static int range_check(struct index ind, int count, char *lbuff)
static void linecheck(char *lbuff, char *tmpbuff)
{
if (line_length+strlen(tmpbuff)>line_max) {
- snprintf(TAIL_LEN(lbuff),"\n%s%s",indent_space,tmpbuff);
+ SAPPENDF(lbuff,"\n%s%s",indent_space,tmpbuff);
line_length=indent_length+strlen(tmpbuff);
tmpbuff[0]='\0';
}
else {
- snprintf(TAIL_LEN(lbuff),"%s",tmpbuff);
+ SAPPENDF(lbuff,"%s",tmpbuff);
line_length+=strlen(tmpbuff);
tmpbuff[0]='\0';
}