aboutsummaryrefslogtreecommitdiff
path: root/fref.lex
blob: 00a998064eb48c05f4b2e5082c8038589f9741a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * 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 <err.h>
	#include <stdarg.h>

	#define MAXFIELD 255

	#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 printif(char *, ...);

	struct trans {
		char *and;
		char *avail;
		char *ed;
		char *p;
		char *P;
		char *pp;
		char *Pp;
		char *tr;
		char *vo;
		char *Vo;
	};

	/* Fields of current entry. */
	struct entry {
		char au[MAXFIELD][MAXFIELD];	/* Authors. */
		int a;				/* Number of 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). */
	struct trans *tglob;		/* Global translation strings (-l). */
	unsigned int line;		/* Line at which entry begins. */
	unsigned int lines;		/* Total number of lines. */
	unsigned int punct;		/* Was punctuation printed? */

%%
\n		lines++;	ECHO;
^%.*\n		line = ++lines;	field(); BEGIN(ENTRY);
<ENTRY>^%.*\n	lines++;	field();
<ENTRY>^[^%]			entry(); ECHO; BEGIN(0);
<ENTRY><<EOF>>			entry(); yyterminate();
%%

void
main(int argc, char *argv[])
{
	int c;

	line = lines = punct = 0;
	lang = NULL;
	tglob = NULL;
	memset(&e, 0, sizeof(e));

	name = strrchr(argv[0], '/');
	if(name) name++;
	else name = argv[0];

	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;

	tglob = gettrans(lang? lang: "en");

	yylex();
}

/* Return pointer to translation struct. */
struct trans *
gettrans(char *lg)
{
	static struct trans *len = NULL;
	static struct trans *lru = NULL;
	static struct trans *lsv = NULL;

	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->P = strdup("P. ");
			len->pp = strdup("pp. ");
			len->Pp = strdup("Pp. ");
			len->tr = strdup("Trans. ");
			len->vo = strdup("vol. ");
			len->Vo = strdup("Vol. ");
		}
		return len;
	}

	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("с. ");
			lru->P = strdup("С. ");
			lru->pp = strdup("с. ");
			lru->Pp = strdup("С. ");
			lru->tr = strdup("Перев. ");
			lru->vo = strdup("том ");
			lru->Vo = strdup("Том ");
		}
		return lru;
	}

	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->P = strdup("S. ");
			lsv->pp = strdup("ss. ");
			lsv->Pp = strdup("Ss. ");
			lsv->tr = strdup("Övers. ");
			lsv->vo = strdup("vol. ");
			lsv->Vo = strdup("Vol. ");
		}
		return lsv;
	}

	if(line)
		fprintf(stderr, "%s: unrecognized language %s"
			" in reference at line %d\n",
			name, lg, line);
	else
		fprintf(stderr, "%s: unrecognized language %s\n",
			name, lg);
	exit(1);
}

/* Save entry field. */
void
field()
{
	yytext++;
	yytext[strcspn(yytext, "\n")] = 0;

	#define CMP(s,m) if(strncmp(yytext,s,2) == 0){			\
		/* Don't copy empty field. */				\
		if(strlen(yytext) < 3)					\
			return;						\
		yytext += 3;						\
		strcpy(m, yytext);					\
		return;							\
	}

	CMP("au",e.au[e.a++]);
	#define DO(s,n,m) CMP(s,m)
	FOR_FIELDS
	#undef DO

	fprintf(stderr, "%s: unrecognized field %%%.2s at line %d\n",
		name, yytext, lines);
	exit(1);
}

/*
 * 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;

	t = *e.lc? gettrans(e.lc): tglob;
	*e.lc = 0;

	/* Print label. */
	printif("% = ", e.la);

	/* Print authors. */
	for(i = 0; i < e.a-2; i++)
		printif("*, ", e.au[i]);
	if(i == e.a-2)
		printif("* * ", e.au[i++], t->and);
	if(i == e.a-1)
		printif("%", e.au[i]);

	/* Print date. */
	printif(" (%)", e.da);
	printif(".\n");

	/* Print title, book/journal. */
	if(*e.bo || *e.jo){
		printif("%.\n", e.ti);
		printif("\\fI%\\fP", e.bo? e.bo: e.jo);
		printif(" (*%)", t->ed, e.ed);
	}else{
		printif("\\fI%\\fP", e.ti);
	}

	/* Print volume. */
	printif(",\n*%", punct? t->Vo: t->vo, e.vo);

	/* Print pages, converting hyphens to en dashes. */
	if(*e.pg){
		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('\\');
			putchar(e.pg[i]);
		}
		*e.pg = 0;
	}
	printif(".\n");

	/* Print translator. */
	printif("*%.\n", t->tr, e.tr);

	/* Print city, issuer. */
	printif("%*", e.ci, *e.is? ": ": ".\n");
	printif("%.\n", e.is);

	/* Print other information. */
	printif("%.\n", e.xx);

	/* Print hypertext reference. */
	printif("*%\n", t->avail, e.hr);

	/* Print access date. */
	printif("[%]\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
}

/* Print formatted string if no string (* or %) is empty. */
int
printif(char *fmt, ...)
{
	char *buf, *p, **fs;
	int i, n, sz;
	va_list ap;

	for(n = 0, p = fmt; *p; p++)
		if(*p == '%' || *p == '*')
			n++;

	if(!(fs = malloc(sizeof(char *)*n)))
		err(1, "malloc");

	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);

	/*
	 * 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;
			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)
				putchar(*fmt);
			punct = 0;
			break;
		case '\\':
			/* Don't reset punctuation for \fX escape. */
			if(strncmp(fmt, "\\f", 2) == 0 && fmt[2]){
				printf("%.3s", fmt);
				fmt += 2;
				break;
			}
			/* FALLTHROUGH */
		default:
			punct = 0;
			putchar(*fmt);
		}
	return 1;
}