aboutsummaryrefslogtreecommitdiff
path: root/refs.lex
blob: b78fa4b5a539f1c9d4f19e717ac52ab71c99cbb1 (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
	#include <err.h>
	#include <stdarg.h>

	#define MAX 255

	#define AND "och"
	#define AVAIL "Tillgänglig: "
	#define ED "red."
	#define PP "ss."
	#define TR "Övers."
	#define VOL "vol."

	void field(char *);
	void entry(void);
	int pf(char *, ...);

	char *name;

	struct entry {
		int a; /* Number of authors. */
		char au[MAX][MAX];
		char ad[MAX];
		char al[MAX];
		char bo[MAX];
		char ci[MAX];
		char da[MAX];
		char ed[MAX];
		char jo[MAX];
		char no[MAX]; /* TODO */
		char pp[MAX];
		char pu[MAX];
		char se[MAX]; /* TODO */
		char ti[MAX];
		char tr[MAX];
		char vo[MAX];
		char QQ[MAX];
	} e;

%s ENTRY
%%
^%.*\n		field(yytext); BEGIN(ENTRY);
<ENTRY>^[^%]	entry(); ECHO; BEGIN(0);
<ENTRY><<EOF>>	entry(); yyterminate();
%%

void
main(int argc, char *argv[0])
{
	name = argv[0];
	e.a = 0;
	yylex();
}

/* Save entry field. */
void
field(char *t)
{

#define AS(a,b) if(strncmp(t, a, 2) == 0 && (t += 3)){ strcpy(b, t); return; }

	t++;
	t[strcspn(t, "\n")] = 0;

	AS("au",e.au[e.a++]);
	AS("ad",e.ad);
	AS("al",e.al);
	AS("bo",e.bo);
	AS("ci",e.ci);
	AS("da",e.da);
	AS("ed",e.ed);
	AS("jo",e.jo);
	AS("no",e.no);
	AS("pp",e.pp);
	AS("pu",e.pu);
	AS("se",e.se);
	AS("ti",e.ti);
	AS("tr",e.tr);
	AS("vo",e.vo);
	AS("??",e.QQ);

	fprintf(stderr, "%s: unrecognized field %%%.2s\n", name, t);
	exit(1);
}

/* Print formatted entry. */
void
entry()
{
	int i;

#define EI(a, b) a ? a : b

	/* Print authors. */
	for(i = 0; i < e.a-2; i++)
		printf("%s, ", e.au[i]);
	if(i == e.a-2)
		printf("%s "AND" ", e.au[i++]);
	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", EI(e.bo, e.jo));
		pf(" ("ED" %)", e.ed);
	}else
		pf("\\fI%\\fP", e.ti);

	/* Print volume, pages. */
	pf(", "VOL" %", e.vo);
	pf(", "PP" %", e.pp);
	pf(".\n");

	/* Print translator. */
	pf(TR" %.\n", e.tr);

	/* Print city, publisher. */
	(pf("%: %.\n", e.ci, e.pu) || pf("%.\n", e.pu));

	/* Print other information. */
	pf("%.\n", e.QQ);

	/* Print internet address. */
	pf(AVAIL"%\n", e.ad);

#define CL(a) *a = 0

	e.a = 0;
	CL(e.ad);
	CL(e.al);
	CL(e.bo);
	CL(e.ci);
	CL(e.da);
	CL(e.ed);
	CL(e.jo);
	CL(e.no);
	CL(e.pp);
	CL(e.pu);
	CL(e.se);
	CL(e.ti);
	CL(e.tr);
	CL(e.vo);
	CL(e.QQ);
}

/* Print formatted text if given strings are non-empty. */
int
pf(char *fmt, ...)
{
	char *buf, *p;
	int n;
	va_list ap;

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

	if(!(buf = malloc(strlen(fmt)+n*MAX+1)))
		err(1, "malloc");

	va_start(ap, fmt);
	for(; *fmt; fmt++)
		switch(*fmt){
		case '%':
			p = va_arg(ap, char *);
			if(!p || !*p)
				return 0;
			strcat(buf, p);
			break;
		default:
			strncat(buf, fmt, 1);
		}
	printf("%s", buf);
	va_end(ap);
	return 1;
}