aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/dict/slang.c
blob: bd32b9762f3f4bfcbd3169ec4f4fd51e60bf5f28 (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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "dict.h"

/* Possible tags */
enum {
	DF,		/* definition */
	DX,		/* definition/example */
	ET,		/* etymology */
	EX,		/* example */
	LA,		/* label */
	ME,		/* main entry */
	NU,		/* sense number */
	PR,		/* pronunciation */
	PS,		/* grammar part */
	XR,		/* cross reference */
	XX,		/* cross reference (whole entry) */
};

/* Assoc tables must be sorted on first field */

static Assoc tagtab[] = {
	{"df",	DF},
	{"dx",	DX},
	{"et",	ET},
	{"ex",	EX},
	{"la",	LA},
	{"me",	ME},
	{"nu",	NU},
	{"pr",	PR},
	{"ps",	PS},
	{"xr",	XR},
	{"xx",	XX},
};
static long	sget(char *, char *, char **, char **);
static void	soutpiece(char *, char *);

void
slangprintentry(Entry e, int cmd)
{
	char *p, *pe, *vs, *ve;
	long t;

	p = e.start;
	pe = e.end;
	if(cmd == 'h') {
		t = sget(p, pe, &vs, &ve);
		if(t == ME)
			soutpiece(vs, ve);
		outnl(0);
		return;
	}
	while(p < pe) {
		switch(sget(p, pe, &vs, &ve)) {
		case DF:
			soutpiece(vs, ve);
			outchars(".  ");
			break;
		case DX:
			soutpiece(vs, ve);
			outchars(".  ");
			break;
		case ET:
			outchars("[");
			soutpiece(vs, ve);
			outchars("] ");
			break;
		case EX:
			outchars("E.g., ");
			soutpiece(vs, ve);
			outchars(".  ");
			break;
		case LA:
			outchars("(");
			soutpiece(vs, ve);
			outchars(") ");
			break;
		case ME:
			outnl(0);
			soutpiece(vs, ve);
			outnl(0);
			break;
		case NU:
			outnl(2);
			soutpiece(vs, ve);
			outchars(".  ");
			break;
		case PR:
			outchars("[");
			soutpiece(vs, ve);
			outchars("] ");
			break;
		case PS:
			outnl(1);
			soutpiece(vs, ve);
			outchars(". ");
			break;
		case XR:
			outchars("See ");
			soutpiece(vs, ve);
			outchars(".  ");
			break;
		case XX:
			outchars("See ");
			soutpiece(vs, ve);
			outchars(".  ");
			break;
		default:
			ve = pe;	/* will end loop */
			break;
		}
		p = ve;
	}
	outnl(0);
}

long
slangnextoff(long fromoff)
{
	long a;
	char *p;

	a = Bseek(bdict, fromoff, 0);
	if(a < 0)
		return -1;
	for(;;) {
		p = Brdline(bdict, '\n');
		if(!p)
			break;
		if(p[0] == 'm' && p[1] == 'e' && p[2] == ' ')
			return (Boffset(bdict)-Blinelen(bdict));
	}
	return -1;
}

void
slangprintkey(void)
{
	Bprint(bout, "No key\n");
}

/*
 * Starting from b, find next line beginning with a tag.
 * Don't go past e, but assume *e==0.
 * Return tag value, or -1 if no more tags before e.
 * Set pvb to beginning of value (after tag).
 * Set pve to point at newline that ends the value.
 */
static long
sget(char *b, char *e, char **pvb, char **pve)
{
	char *p;
	char buf[3];
	long t, tans;

	buf[2] = 0;
	tans = -1;
	for(p = b;;) {
		if(p[2] == ' ') {
			buf[0] = p[0];
			buf[1] = p[1];
			t = lookassoc(tagtab, asize(tagtab), buf);
			if(t < 0) {
				if(debug)
					err("tag %s\n", buf);
				p += 3;
			} else {
				if(tans < 0) {
					p += 3;
					tans = t;
					*pvb = p;
				} else {
					*pve = p;
					break;
				}
			}
		}
		p = strchr(p, '\n');
		if(!p || ++p >= e) {
			if(tans >= 0)
				*pve = e-1;
			break;
		}
	}
	return tans;
}

static void
soutpiece(char *b, char *e)
{
	int c, lastc;

	lastc = 0;
	while(b < e) {
		c = *b++;
		if(c == '\n')
			c = ' ';
		if(!(c == ' ' && lastc == ' ') && c != '@')
			outchar(c);
		lastc = c;
	}
}