/* * 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 MAX 255 /* Max field length. */ #define DO_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("jo",jo,e.jo) /* Journal. */ \ DO("la",la,e.la) /* Label. */ \ DO("no",no,e.no) /* TODO: Issue number. */ \ DO("pp",pp,e.pp) /* Pages. */ \ DO("pu",pu,e.pu) /* Publisher. */ \ 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 field(char *); void entry(void); void harvard(void); int pf(char *, ...); extern char *optarg; extern int optind; char *name; int line; /* Line at which entry begins. */ int lines = 0; /* Total number of lines. */ /* Fields of current entry. */ struct entry { #define DO(s,n,m) char n[MAX]; DO_FIELDS #undef DO int a; /* Number of authors. */ char au[MAX][MAX]; /* Authors. */ } e; /* Translation strings. */ struct translations { char *and; char *avail; char *ed; char *p; char *pp; char *tr; char *vo; } t; %% \n lines++; ECHO; ^%.*\n line = ++lines; field(yytext); BEGIN(ENTRY); ^%.*\n lines++; field(yytext); ^[^%] entry(); ECHO; BEGIN(0); <> entry(); yyterminate(); %% void main(int argc, char *argv[]) { char *lang; int c; /* 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; /* Set translation strings according to -l. */ if(strncmp(lang, "sv", 2) == 0){ t.and = strdup("och"); t.avail = strdup("Tillgänglig: "); t.ed = strdup("red. "); t.p = strdup("s. "); t.pp = strdup("ss. "); t.tr = strdup("Övers. "); t.vo = strdup("vol. "); }else{ t.and = strdup("and"); t.avail = strdup("Available: "); t.ed = strdup("ed. "); t.p = strdup("p. "); t.pp = strdup("pp. "); t.tr = strdup("Trans. "); t.vo = strdup("vol. "); } /* Clear entry struct. */ memset(&e, 0, sizeof(e)); yylex(); } /* Save entry field. */ void field(char *s) { s++; s[strcspn(s, "\n")] = 0; /* Fill corresponding field. */ #define AS(a,b) if(strncmp(s, a, 2) == 0 && (s += 3)){ strcpy(b, s); return; } AS("au",e.au[e.a++]); #define DO(s,n,m) AS(s,m) DO_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, s, lines); exit(1); } /* Print formatted entry. */ void entry() { /* Presently, fref supports only Harvard-style citations, but this allows for future extensions. */ harvard(); } /* Print Harvard-formatted entry. */ void harvard() { int i; /* Print label. */ pf("% = ", e.la); /* Print authors. */ for(i = 0; i < e.a-2; i++) printf("%s, ", e.au[i]); if(i == e.a-2) printf("%s %s ", e.au[i++], t.and); if(i == e.a-1) printf("%s", e.au[i]); /* Print date. */ (pf(" (%).\n", e.da) || (e.a ? pf(".\n") : 0)); /* 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.pp){ printf(", %s", strpbrk(e.pp, ",-") ? t.pp : t.p); for(i = 0; i < strlen(e.pp); i++){ if(e.pp[i] == '-') putchar('\\'); putchar(e.pp[i]); } *e.pp = 0; } pf(".\n"); /* Print translator. */ pf("*%.\n", t.tr, e.tr); /* Print city, publisher. */ (pf("%: %.\n", e.ci, e.pu) || pf("%.\n", e.pu)); /* Print other information. */ pf("%.\n", e.xx); /* Print hypertext reference. */ pf("*%\n", t.avail, e.hr); /* Print access date. */ pf("[%]\n", e.ad); /* Clear unused fields. */ e.a = 0; #define DO(s,n,m) \ if(*m) fprintf(stderr, "%s: unused field %%"s" at line %d\n", \ name, line); \ *m = 0; DO_FIELDS #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. */ int pf(char *fmt, ...) { char *buf, *p, **fs; int i, n, sz; va_list ap; /* Count interpolated strings. */ for(n = 0, p = fmt; *p; p++) if(*p == '%' || *p == '*') n++; if(!(fs = malloc(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); /* Print formatted string. */ for(i = 0; *fmt; fmt++) switch(*fmt){ case '%': printf("%s", fs[i]); fs[i][0] = 0; i++; break; case '*': printf("%s", fs[i]); i++; break; default: putchar(*fmt); } return 1; }