File: lookup.c

package info (click to toggle)
dico 2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 16,908 kB
  • sloc: ansic: 85,886; sh: 50,207; lex: 2,624; yacc: 1,437; tcl: 1,434; python: 1,281; makefile: 1,266; perl: 881; lisp: 489; awk: 157; pascal: 125; cpp: 44; fortran: 25; asm: 19; sed: 16; xml: 8
file content (306 lines) | stat: -rw-r--r-- 6,978 bytes parent folder | download
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
/* This file is part of GNU Dico. 
   Copyright (C) 1998-2019 Sergey Poznyakoff

   GNU Dico is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Dico is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */

#include "dico-priv.h"

void
print_reply(struct dict_connection *conn)
{
    if (strncmp(conn->buf, "552", 3) == 0)
	printf("%s\n", _("No match"));
    else
	printf("Error: %s\n", conn->buf);
}

static size_t
utf8_count_newlines(char *str)
{
    size_t count = 0;
    struct utf8_iterator itr;
    for (utf8_iter_first(&itr, (char *)str);
	 !utf8_iter_end_p(&itr); utf8_iter_next(&itr)) {
	if (utf8_iter_isascii(itr) && *itr.curptr == '\n')
	    count++;
    }
    return count;
}

size_t
result_count_lines(struct dict_result *res)
{
    size_t i, count = 0;
    
    switch (res->type) {
    case dict_result_define:
	for (i = 0; i < res->count; i++)
	    count += res->set.def[i].nlines + 3;
	break;

    case dict_result_match:
	count = res->count;
	break;

    case dict_result_text:
	count = utf8_count_newlines(res->base);
    }
    return count;
}

static void
format_defn(dico_stream_t str, struct define_result *def)
{
    stream_printf(str, _("From %s, %s:\n"),
		  def->database, def->descr);
    stream_writez(str, def->defn);
    dico_stream_write(str, "\n", 1);
}

static void
format_match(dico_stream_t str, struct match_result *mat)
{
    stream_printf(str, "%s \"%s\"\n", mat->database, mat->word);
}

void
print_result(struct dict_result *res)
{
    unsigned long i;
    dico_stream_t str;
    
    str = create_pager_stream(result_count_lines(res));
    switch (res->type) {
    case dict_result_define:
	for (i = 0; i < res->count; i++)
	    format_defn(str, &res->set.def[i]);
	break;

    case dict_result_match:
	for (i = 0; i < res->count; i++)
	    format_match(str, &res->set.mat[i]);
	break;

    case dict_result_text:
	stream_writez(str, res->base);
	break;
    }
    dico_stream_close(str);
    dico_stream_destroy(&str);
}


struct result_display {
    struct result_display *next;
    char *database;
    int count;
    char **matches;
};

struct result_display *
alloc_display(struct dict_result *res, size_t from, size_t to)
{
    struct result_display *disp = xmalloc(sizeof(*disp));
    size_t i;
    
    disp->next = NULL;
    disp->database = res->set.mat[from].database;
    disp->count = to - from;
    disp->matches = xcalloc(to - from, sizeof(disp->matches[0]));
    for (i = 0; from < to; i++, from++)
	disp->matches[i] = res->set.mat[from].word;
    return disp;
}

static char *
find_descr(struct dict_connection *conn, const char *name)
{
    if (conn->db_result) {
	size_t i;
	
	for (i = 0; i < conn->db_result->count; i++) {
	    if (strcmp(conn->db_result->set.mat[i].database, name) == 0)
		return conn->db_result->set.mat[i].word;
	}
    }
    return _("(no description available)");
}

void
print_match_result(struct dict_result *res)
{
    struct result_display *head = NULL, *tail = NULL;
    char *dbname = NULL;
    size_t i, j = 0;
    struct dict_connection *conn = res->conn;
    size_t ndb = 0;
    dico_stream_t str;
    struct result_display *p;
    
#define ALLOC_DISPLAY() do { 					\
	struct result_display *p = alloc_display(res, j, i);    \
	if (!tail)                                              \
	    head = p;                                           \
	else               					\
	    tail->next = p;					\
	tail = p;						\
	ndb++;							\
	j = i;							\
	dbname = res->set.mat[i].database;			\
    } while (0)

    dbname = res->set.mat[0].database;
    for (i = 1; i < res->count; i++) {
	if (strcmp(res->set.mat[i].database, dbname))
	    ALLOC_DISPLAY();
    }
    ALLOC_DISPLAY();

    str = create_pager_stream(ndb + result_count_lines(res));
    j = 0;
    for (p = head; p; ) {
	struct result_display *next = p->next;
	
	stream_printf(str, _("From %s, %s:\n"), p->database,
		      find_descr(conn, p->database));
	for (i = 0; i < p->count; i++, j++)
	    stream_printf(str, "%4d) \"%s\"\n", j, p->matches[i]);
	free(p->matches);
	free(p);
	p = next;
    }
    dico_stream_close(str);
    dico_stream_destroy(&str);
}



int
dict_lookup(struct dict_connection *conn, dico_url_t url)
{
    int rc;
    switch (url->req.type) {
    case DICO_REQUEST_DEFINE:
	rc = dict_define(conn,
			 quotearg_n (0, url->req.database),
			 quotearg_n (1, url->req.word));
	break;
	
    case DICO_REQUEST_MATCH:
	rc = dict_match(conn,
			quotearg_n (0, url->req.database),
			quotearg_n (1, url->req.strategy),
			quotearg_n (2, url->req.word));
	break;
	
    default:
	dico_log(L_CRIT, 0,
		 _("%s:%d: INTERNAL ERROR: unexpected request type"),
		 __FILE__, __LINE__);
    }

    if (rc == 0) {
	struct dict_result *res = dict_conn_last_result(conn);
	print_result(res);
	dict_result_free(res);
    } else
	print_reply(conn);
    
    return 0;
}

int
dict_lookup_url(dico_url_t url)
{
    struct dict_connection *conn;
    
    if (!url->host) {
	dico_log(L_ERR, 0, _("Server name or IP not specified"));
	return 1;
    }

    if (dict_connect(&conn, url))
	return 1;

    dict_lookup(conn, url);

    XDICO_DEBUG(1, _("Quitting\n"));
    stream_printf(conn->str, "QUIT\r\n");
    dict_read_reply(conn);
    dict_conn_close(conn);
    /* FIXME */
    return 0;
}

int
dict_word(char *word)
{
    int rc;
    dico_url_t url;

    if (memcmp (word, "dict://", 7) == 0) {
	if (dico_url_parse(&url, word))
	    return 1;
	rc = dict_lookup_url(url);
	dico_url_destroy(&url);
    } else {
	dico_url.req.word = word;
	rc = dict_lookup_url(&dico_url);
    }
	
    return rc;
}

void
dict_run_single_command(struct dict_connection *conn,
			const char *cmd, const char *arg, const char *code)
{
    if (arg)
	stream_printf(conn->str, "%s \"%s\"\r\n", cmd, arg);
    else
	stream_printf(conn->str, "%s\r\n", cmd);
    
    dict_read_reply(conn);
    if (!dict_status_p(conn, code))
	print_reply(conn);
    else {
	struct dict_result *res;
	
	dict_multiline_reply(conn);
	dict_read_reply(conn);
	dict_result_create(conn, dict_result_text, 1,
			   obstack_finish(&conn->stk));
	res = dict_conn_last_result(conn);
	print_result(res);
	dict_result_free(res);
    }
}

int
dict_single_command(char *cmd, char *arg, char *code)
{
    struct dict_connection *conn;
    
    if (!dico_url.host) {
	dico_log(L_ERR, 0, _("Server name or IP not specified"));
	return 1;
    }

    if (dict_connect(&conn, &dico_url))
	return 1;

    dict_run_single_command(conn, cmd, arg, code);
    dict_conn_close(conn);    
    return 0;
}