Implemented texi-to-man translation rule about {...} recursion

This commit is contained in:
Thomas Schmitt 2011-05-17 09:18:18 +00:00
parent be0b15a8e7
commit 3f4936519d

View File

@ -38,6 +38,10 @@ struct Mx1 {
}; };
int Mx1_substitute(struct Mx1 *m, char line_in[256], char line_out[256],
int raw, int upto, int flag);
int Mx1_init(struct Mx1 *m, char *prog, int flag) int Mx1_init(struct Mx1 *m, char *prog, int flag)
{ {
@ -78,81 +82,116 @@ int Mx1__get_word(char *line, char word[256], char **remainder, int flag)
} }
int Mx1_substitute(struct Mx1 *m, char line_in[256], char line_out[256], /* @param flag bit0= recursion
int raw, int flag) */
int Mx1_rewrap(struct Mx1 *m, char **read_pt, char **write_pt,
char *write_base, char *envelope,
char *front, char *back,
int flag)
{ {
char *rpt, *wpt, *ept; char *rpt, *wpt, *ept, content[256], msg[256];
int l; int l, ret;
rpt= *read_pt;
wpt= *write_pt;
ept= strchr(rpt, '}');
if(ept == NULL) {
sprintf(msg, "No closing bracket found for '%s'", envelope);
Mx1_report_error(m, msg, 0);
return(-1);
}
/* Mapped {...} content is subject to the rules except {...} mapping. */
l= ept - rpt;
ret= Mx1_substitute(m, rpt, content, 0, l, 1);
if(ret <= 0)
return(ret);
l= strlen(content);
if((wpt - write_base) + l + strlen(front) + strlen(back) > 255) {
Mx1_report_error(m, "Line length overflow while text substitution", 0);
return(-1);
}
strcpy(wpt, front);
wpt+= strlen(front);
strncpy(wpt, content, l);
wpt+= l;
strcpy(wpt, back);
wpt+= strlen(back);
(*read_pt)+= ept - rpt;
(*write_pt)= wpt;
return(1);
}
/* @param flag bit0= recursion
*/
int Mx1_substitute(struct Mx1 *m, char line_in[256], char line_out[256],
int raw, int upto, int flag)
{
char *rpt, *wpt;
int ret;
wpt= line_out; wpt= line_out;
for(rpt= line_in; *rpt != 0; rpt++) { for(rpt= line_in; rpt - line_in < upto && *rpt != 0; rpt++) {
if(rpt - line_in < raw) { if(rpt - line_in < raw) {
*(wpt++)= *rpt; *(wpt++)= *rpt;
continue; continue;
} }
if(*rpt == '@') { if(*rpt == '@') {
if(strncmp(rpt, "@strong{", 8) == 0) { if(strncmp(rpt, "@strong{", 8) == 0 && !(flag & 1)) {
/* @strong{-...} gets mapped to \fB\-...\fR . */;
/* @strong{...} gets mapped to \fB...\fR . */ /* @strong{...} gets mapped to \fB...\fR . */
ept= strchr(rpt, '}'); rpt+= 8;
if(ept == NULL) { ret= Mx1_rewrap(m, &rpt, &wpt, line_out,
Mx1_report_error(m, "No closing bracket found for '@strong{'", 0); "@strong{" , "\\fB", "\\fR", 0);
return(-1); if(ret <= 0)
} return(ret);
l= ept - rpt - 8;
if((wpt - line_out) + l + 6 + (rpt[8] == '-') > 255) } else if(strncmp(rpt, "@command{", 9) == 0 && !(flag & 1)) {
goto overflow;
strcpy(wpt, "\\fB");
wpt+= 3;
if(rpt[8] == '-')
*(wpt++)= '\\';
strncpy(wpt, rpt + 8, l);
wpt+= l;
strcpy(wpt, "\\fR");
wpt+= 3;
rpt+= ept - rpt;
} else if(strncmp(rpt, "@command{}", 9) == 0) {
/* @command{...} gets mapped to \fB...\fR . */ /* @command{...} gets mapped to \fB...\fR . */
ept= strchr(rpt, '}'); rpt+= 9;
if(ept == NULL) { ret= Mx1_rewrap(m, &rpt, &wpt, line_out,
Mx1_report_error(m, "No closing bracket found for '@command{'", 0); "@command{", "\\fB", "\\fR", 0);
return(-1); if(ret <= 0)
} return(ret);
l= ept - rpt - 9;
if((wpt - line_out) + l + 6 + (rpt[9] == '-') > 255) } else if(strncmp(rpt, "@email{", 7) == 0 && !(flag & 1)) {
goto overflow; /* @email{...} gets mapped to <...> . */
strcpy(wpt, "\\fB"); rpt+= 7;
wpt+= 3; ret= Mx1_rewrap(m, &rpt, &wpt, line_out, "@email{", "<", ">", 0);
strncpy(wpt, rpt + 9, l); if(ret <= 0)
wpt+= l; return(ret);
strcpy(wpt, "\\fR");
wpt+= 3;
rpt+= ept - rpt;
} else if(strncmp(rpt, "@minus{}", 8) == 0) { } else if(strncmp(rpt, "@minus{}", 8) == 0) {
/* @minus{} will become "-". */ /* @minus{} will become "-". */
if((wpt - line_out) + 1 > 255) if((wpt - line_out) + 1 > 255)
goto overflow; goto overflow;
*(wpt++)= '-'; *(wpt++)= '-';
rpt+= 7; rpt+= 7;
} else if(strncmp(rpt, "@@", 2) == 0 || } else if(strncmp(rpt, "@@", 2) == 0 ||
strncmp(rpt, "@{", 2) == 0 || strncmp(rpt, "@{", 2) == 0 ||
strncmp(rpt, "@}", 2) == 0) { strncmp(rpt, "@}", 2) == 0) {
/* @@ , @{, @} will get stripped of their first @. */ /* @@ , @{, @} will get stripped of their first @. */
*(wpt++)= *(rpt + 1); *(wpt++)= *(rpt + 1);
rpt++; rpt++;
} }
} else if(*rpt == '\\') { } else if(*rpt == '\\') {
/* "\" becomes "\\" */ /* "\" becomes "\\" */
if((wpt - line_out) + 2 > 255) if((wpt - line_out) + 2 > 255)
goto overflow; goto overflow;
*(wpt++)= '\\'; *(wpt++)= '\\';
*(wpt++)= '\\'; *(wpt++)= '\\';
} else if((wpt - line_out) + 1 > 255) { } else if((wpt - line_out) + 1 > 255) {
overflow:; overflow:;
Mx1_report_error(m, "Line length overflow while text substitution", 0); Mx1_report_error(m, "Line length overflow while text substitution", 0);
return(-1); return(-1);
} else } else
*(wpt++)= *rpt; *(wpt++)= *rpt;
} }
*wpt= 0; *wpt= 0;
return(1); return(1);
@ -250,7 +289,7 @@ int Mx1_convert(struct Mx1 *m, char line_in[256], char line_out[256], int flag)
/* Substitute option text */ /* Substitute option text */
raw= strlen(buf); raw= strlen(buf);
strcat(buf, word); strcat(buf, word);
ret= Mx1_substitute(m, buf, line_out, raw, 0); ret= Mx1_substitute(m, buf, line_out, raw, strlen(buf), 0);
if(ret <= 0) if(ret <= 0)
return(-1); return(-1);
strcpy(buf, line_out); strcpy(buf, line_out);
@ -260,28 +299,30 @@ int Mx1_convert(struct Mx1 *m, char line_in[256], char line_out[256], int flag)
strcat(buf, remainder); strcat(buf, remainder);
/* Substitute arguments text */ /* Substitute arguments text */
ret= Mx1_substitute(m, buf, line_out, raw, 0); ret= Mx1_substitute(m, buf, line_out, raw, strlen(buf), 0);
if(ret <= 0) if(ret <= 0)
return(-1); return(-1);
m->count_out++; m->count_out++;
return(1); return(1);
} }
/* @strong{-...} gets mapped to \fB\-...\fR . */
/* @strong{... } gets mapped to \fB...\fR . */ /* @strong{... } gets mapped to \fB...\fR . */
/* @command{... } gets mapped to \fB...\fR . */ /* @command{... } gets mapped to \fB...\fR . */
/* @minus{} will become "-". */ /* @minus{} will become "-". */
/* Mapped {...} content is subject to the rules except {...} mapping. */
/* @@ , @{, @} will get stripped of their first @. */ /* @@ , @{, @} will get stripped of their first @. */
/* "\" becomes "\\" */ /* "\" becomes "\\" */
if(line_in[0] != '@' || if(line_in[0] != '@' ||
strncmp(line_in, "@strong{", 8) == 0 || strncmp(line_in, "@strong{", 8) == 0 ||
strncmp(line_in, "@command{", 9) == 0 || strncmp(line_in, "@command{", 9) == 0 ||
strncmp(line_in, "@minus{}", 8) == 0 || strncmp(line_in, "@minus{}", 8) == 0 ||
strncmp(line_in, "@email{", 7) == 0 ||
strncmp(line_in, "@@", 2) == 0 || strncmp(line_in, "@@", 2) == 0 ||
strncmp(line_in, "@{", 2) == 0 || strncmp(line_in, "@{", 2) == 0 ||
strncmp(line_in, "@}", 2) == 0 ) { strncmp(line_in, "@}", 2) == 0 ) {
keep= 1; keep= 1;
ret= Mx1_substitute(m, line_in, line_out, 0, 0); ret= Mx1_substitute(m, line_in, line_out, 0, strlen(line_in), 0);
if(ret <= 0) if(ret <= 0)
return(-1); return(-1);
} }
@ -330,7 +371,7 @@ int main(int argc, char **argv)
if(argc < 2) { if(argc < 2) {
usage:; usage:;
fprintf(stderr, "usage: %s -auto|-filter|[-xorrisofs]\n", argv[0]); fprintf(stderr, "usage: %s -auto|-filter [-xorrisofs]\n", argv[0]);
fprintf(stderr, " -auto xorriso/xorriso.texi -> xorriso/xorriso.1\n"); fprintf(stderr, " -auto xorriso/xorriso.texi -> xorriso/xorriso.1\n");
fprintf(stderr, " -filter stdin -> stdout\n"); fprintf(stderr, " -filter stdin -> stdout\n");
fprintf(stderr, " -xorrisofs process xorriso/xorrisofs.texi\n"); fprintf(stderr, " -xorrisofs process xorriso/xorrisofs.texi\n");