diff options
-rw-r--r-- | fref.lex | 72 |
1 files changed, 39 insertions, 33 deletions
@@ -39,7 +39,7 @@ void entry(void); void field(); struct trans *gettrans(char *); - int pf(char *, ...); + int printif(char *, ...); struct trans { char *and; @@ -107,7 +107,7 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - tglob = gettrans(lang ? lang : "en"); + tglob = gettrans(lang? lang: "en"); yylex(); } @@ -210,69 +210,76 @@ field() exit(1); } -/* Print formatted entry. */ +/* + * This function prints a formatted entry with the hitherto gathered + * fields. It uses the special printif formatting function (see below). + */ void entry() { int i; struct trans *t; - #define C(a,b) (punct ? b : a) - - t = *e.lc ? gettrans(e.lc) : tglob; + t = *e.lc? gettrans(e.lc): tglob; *e.lc = 0; /* Print label. */ - pf("% = ", e.la); + printif("% = ", e.la); /* Print authors. */ for(i = 0; i < e.a-2; i++) - pf("*, ", e.au[i]); + printif("*, ", e.au[i]); if(i == e.a-2) - pf("* * ", e.au[i++], t->and); + printif("* * ", e.au[i++], t->and); if(i == e.a-1) - pf("%", e.au[i]); + printif("%", e.au[i]); /* Print date. */ - (pf(" (%).\n", e.da) || pf(".\n")); + printif(" (%)", e.da); + printif(".\n"); /* Print title, book/journal. */ if(*e.bo || *e.jo){ - pf("%.\n", e.ti); - pf("\\fI%\\fP", e.bo ? e.bo : e.jo); - pf(" (*%)", t->ed, e.ed); + printif("%.\n", e.ti); + printif("\\fI%\\fP", e.bo? e.bo: e.jo); + printif(" (*%)", t->ed, e.ed); }else{ - pf("\\fI%\\fP", e.ti); + printif("\\fI%\\fP", e.ti); } /* Print volume. */ - pf(",\n*%", C(t->vo,t->Vo), e.vo); + printif(",\n*%", punct? t->Vo: t->vo, e.vo); /* Print pages, converting hyphens to en dashes. */ if(*e.pg){ - pf(",\n*", strpbrk(e.pg, ",-") ? C(t->pp,t->Pp) : C(t->p,t->P)); + if(strpbrk(e.pg, ",-")) + printif(",\n*", punct? t->Pp: t->pp); + else + printif(",\n*", punct? t->P: t->p); for(i = 0; i < strlen(e.pg); i++){ - if(e.pg[i] == '-') putchar('\\'); + if(e.pg[i] == '-') + putchar('\\'); putchar(e.pg[i]); } *e.pg = 0; } - pf(".\n"); + printif(".\n"); /* Print translator. */ - pf("*%.\n", t->tr, e.tr); + printif("*%.\n", t->tr, e.tr); /* Print city, issuer. */ - (pf("%: %.\n", e.ci, e.is) || pf("%.\n", e.is)); + printif("%*", e.ci, *e.is? ": ": ".\n"); + printif("%.\n", e.is); /* Print other information. */ - pf("%.\n", e.xx); + printif("%.\n", e.xx); /* Print hypertext reference. */ - pf("*%\n", t->avail, e.hr); + printif("*%\n", t->avail, e.hr); /* Print access date. */ - pf("[%]\n", e.ad); + printif("[%]\n", e.ad); /* Reset state. */ e.a = 0; @@ -286,15 +293,9 @@ entry() #undef DO } -/* - * This function interpolates any number of strings into a formatted - * string, which is printed only if all interpolated strings are non-empty. - * Interpolation points are marked by % or *. If a string is interpolated - * using %, the function empties it after printing it. Additionally, the - * function tries not to repeat punctuation after %. - */ +/* Print formatted string if no string (* or %) is empty. */ int -pf(char *fmt, ...) +printif(char *fmt, ...) { char *buf, *p, **fs; int i, n, sz; @@ -316,11 +317,16 @@ pf(char *fmt, ...) } va_end(ap); + /* + * After use, a %-string is cleared and punct set to true if it + * ended with a punctuation character (.?!). Punctuation in fmt + * (.,?!) is skipped if punct is true. + */ for(i = 0; *fmt; fmt++) switch(*fmt){ case '%': p = fs[i]+strlen(fs[i])-1; - punct = strpbrk(p, ".?!") ? 1 : 0; + punct = strpbrk(p, ".?!")? 1: 0; printf("%s", fs[i]); fs[i][0] = 0; i++; |