/* * fref creates troff-formatted references according to specifications * given on standard in. The specification syntax is similar to that of * refer, but differs in that all identifiers are two letters instead of * one (like troff itself). This allows for a larger number of fields. * * To build fref, run the following commands: * $ lex fref.lex * $ cc -std=c89 -O2 -o fref lex.yy.c -lfl */ %x ENTRY #include #include #define MAXFIELD 255 /* Max field length. */ #define FOR_FIELDS \ DO("ad",ad,e.ad) /* Access date. */ \ DO("bo",bo,e.bo) /* Book. */ \ DO("ci",ci,e.ci) /* City. */ \ DO("da",da,e.da) /* Date (year). */ \ DO("ed",ed,e.ed) /* Editor. */ \ DO("hr",hr,e.hr) /* Hypertext reference. */ \ DO("is",is,e.is) /* Issuer, publisher. */ \ DO("jo",jo,e.jo) /* Journal. */ \ DO("la",la,e.la) /* Label. */ \ DO("lc",lc,e.lc) /* Reference-specific language. */ \ DO("no",no,e.no) /* TODO: Issue number. */ \ DO("pg",pg,e.pg) /* Page number(s). */ \ DO("se",se,e.se) /* TODO: Series. */ \ DO("ti",ti,e.ti) /* Title. */ \ DO("tr",tr,e.tr) /* Translator. */ \ DO("vo",vo,e.vo) /* Volume. */ \ DO("xx",xx,e.xx) /* Extra information. */ \ /* END */ void entry(void); void field(); struct trans *gettrans(char *); int pf(char *, ...); /* Translation strings. */ struct trans { char *and; char *avail; char *ed; char *p; char *pp; char *tr; char *vo; }; /* Fields of current entry. */ struct entry { int a; /* Number of authors. */ char au[MAXFIELD][MAXFIELD]; /* Authors. */ #define DO(s,n,m) char n[MAXFIELD]; FOR_FIELDS #undef DO } e; extern char *optarg; extern int optind; char *name; /* Program name. */ char *lang; /* Main language (-l). */ int line; /* Line at which entry begins. */ int lines; /* Total number of lines. */ struct trans *len; /* English strings. */ struct trans *lru; /* Russian strings. */ struct trans *lsv; /* Swedish strings. */ struct trans *tglob; /* Global (default) strings. */ %% \n lines++; ECHO; ^%.*\n line = ++lines; field(); BEGIN(ENTRY); ^%.*\n lines++; field(); ^[^%] entry(); ECHO; BEGIN(0); <> entry(); yyterminate(); %% void main(int argc, char *argv[]) { int c; /* Initialize global variables. */ lines = 0; lang = NULL; len = lru = lsv = tglob = NULL; memset(&e, 0, sizeof(e)); /* Save program name. */ name = strrchr(argv[0], '/'); if(name) name++; else name = argv[0]; /* Parse command-line options. */ while((c = getopt(argc, argv, "l:")) != -1) switch(c){ case 'l': lang = optarg; break; default: fprintf(stderr, "usage: %s [-lLANG]\n", argv[0]); exit(1); } argc -= optind; argv += optind; /* Get global language strings. */ tglob = gettrans(lang ? lang : "en"); yylex(); } /* Save entry field. */ void field() { yytext++; yytext[strcspn(yytext, "\n")] = 0; /* Fill corresponding field. */ #define FI(s,m) if(strncmp(yytext,s,2) == 0){ \ /* Don't copy empty field. */ \ if(strlen(yytext) < 3) \ return; \ yytext += 3; \ strcpy(m, yytext); \ return; \ } FI("au",e.au[e.a++]); #define DO(s,n,m) FI(s,m) FOR_FIELDS #undef DO /* * As no field matched, an error is printed and the program exits * with a positive value. */ fprintf(stderr, "%s: unrecognized field %%%.2s at line %d\n", name, yytext, lines); exit(1); } /* Print formatted entry. */ void entry() { int i; struct trans *t; /* Get translation strings. */ t = *e.lc ? gettrans(e.lc) : tglob; *e.lc = 0; /* Print label. */ pf("% = ", e.la); /* Print authors. */ for(i = 0; i < e.a-2; i++) pf("*, ", e.au[i]); if(i == e.a-2) pf("* * ", e.au[i++], t->and); if(i == e.a-1) pf("*", e.au[i]); /* Print date. */ if(!pf(" (%).\n", e.da) && e.a){ if(!strpbrk(e.au[i]+strlen(e.au[i])-1, ".,?!")) pf("."); pf("\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); }else pf("\\fI%\\fP", e.ti); /* Print volume. */ pf(", *%", t->vo, e.vo); /* Print pages, converting hyphens to en dashes. */ if(*e.pg){ pf(", *", strpbrk(e.pg, ",-") ? t->pp : t->p); for(i = 0; i < strlen(e.pg); i++){ if(e.pg[i] == '-') putchar('\\'); putchar(e.pg[i]); } *e.pg = 0; } pf(".\n"); /* Print translator. */ pf("*%.\n", t->tr, e.tr); /* Print city, issuer. */ (pf("%: %.\n", e.ci, e.is) || pf("%.\n", e.is)); /* Print other information. */ pf("%.\n", e.xx); /* Print hypertext reference. */ pf("*%\n", t->avail, e.hr); /* Print access date. */ pf("[%]\n", e.ad); /* Reset state. */ e.a = 0; #define DO(s,n,m) if(*m){ \ fprintf(stderr, "%s: unused field %%" s \ " in reference at line %d\n", \ name, line); \ *m = 0; \ } FOR_FIELDS #undef DO } /* Return pointer to translation struct. */ struct trans * gettrans(char *lg) { if(strncmp(lg, "en", 2) == 0){ if(!len){ if(!(len = malloc(sizeof(struct trans)))) err(1, "malloc"); len->and = strdup("and"); len->avail = strdup("Available: "); len->ed = strdup("ed. "); len->p = strdup("p. "); len->pp = strdup("pp. "); len->tr = strdup("Trans. "); len->vo = strdup("vol. "); } return len; }else if(strncmp(lg, "ru", 2) == 0){ if(!lru){ if(!(lru = malloc(sizeof(struct trans)))) err(1, "malloc"); lru->and = strdup("и"); lru->avail = strdup("Доступная: "); lru->ed = strdup("ред. "); lru->p = strdup("c. "); lru->pp = strdup("с. "); lru->tr = strdup("Перев. "); lru->vo = strdup("том "); } return lru; }else if(strncmp(lg, "sv", 2) == 0){ if(!lsv){ if(!(lsv = malloc(sizeof(struct trans)))) err(1, "malloc"); lsv->and = strdup("och"); lsv->avail = strdup("Tillgänglig: "); lsv->ed = strdup("red. "); lsv->p = strdup("s. "); lsv->pp = strdup("ss. "); lsv->tr = strdup("Övers. "); lsv->vo = strdup("vol. "); } return lsv; }else{ fprintf(stderr, "%s: unrecognized language %s" " in reference at line %d\n", name, lg, line); exit(1); } } /* * 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 %. */ int pf(char *fmt, ...) { char *buf, *p, **fs; int i, n, punct, sz; va_list ap; /* Count interpolated strings. */ for(n = 0, p = fmt; *p; p++) if(*p == '%' || *p == '*') n++; if(!(fs = malloc(sizeof(char *)*n))) err(1, "malloc"); /* * The strings to be interpolated are saved in fs. * If any string is empty, the function returns false. */ va_start(ap, fmt); for(i = 0; i < n; i++){ p = va_arg(ap, char *); if(!p || !*p) return 0; fs[i] = p; } va_end(ap); /* Keep track of prior punctuation. */ punct = 0; /* Print formatted string. */ for(i = 0; *fmt; fmt++) switch(*fmt){ case '%': p = fs[i]+strlen(fs[i])-1; punct = strpbrk(p, ".,?!") ? 1 : 0; printf("%s", fs[i]); fs[i][0] = 0; i++; break; case '*': punct = 0; printf("%s", fs[i]); i++; break; case '.': case ',': case '?': case '!': if(punct && punct--) break; /* FALLTHROUGH */ default: punct = 0; putchar(*fmt); } return 1; }