File: speller.c

package info (click to toggle)
prayer 1.3.5-dfsg1-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,596 kB
  • sloc: ansic: 43,163; makefile: 817; sh: 445; perl: 166
file content (439 lines) | stat: -rw-r--r-- 12,803 bytes parent folder | download | duplicates (6)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* $Cambridge: hermes/src/prayer/session/speller.c,v 1.3 2008/09/16 09:59:58 dpc22 Exp $ */
/************************************************
 *    Prayer - a Webmail Interface              *
 ************************************************/

/* Copyright (c) University of Cambridge 2000 - 2008 */
/* See the file NOTICE for conditions of use and distribution. */

#include "prayer_session.h"

/* Class which provides support routines to the spelling checker */

/* speller_create() *****************************************************
 *
 * Create a speller structure.
 ***********************************************************************/

struct speller *speller_create(struct pool *p)
{
    struct speller *speller = pool_alloc(p, sizeof(struct speller));

    speller->pool = p;
    speller->stream = NIL;
    speller->pid = 0;
    speller->line = NIL;
    speller->offset = 0;
    speller->ignore = NIL;

    return (speller);
}

/* speller_free() *******************************************************
 *
 * Free speller structure
 ***********************************************************************/

void speller_free(struct speller *speller)
{
    struct speller_ignore_list *current, *next;

    if (speller->pool)
        return;

    for (current = speller->ignore; current; current = next) {
        next = current->next;
        free(current->word);
        free(current);
    }
    speller->ignore = NIL;

    free(speller);
}

/* ====================================================================== */

/* speller_start() ******************************************************
 *
 * Start up engine for spell check run.
 *  speller:
 *  session: 
 *     text: Input text to be spell checked
 * language: Ispell language e.g: "british", "american"
 * spell_skip_quoted: Use --mode=email in ispell.
 *
 * Returns: T if spell check engine started okay. NIL on error.
 ***********************************************************************/

BOOL
speller_start(struct speller *speller, struct session *session,
              char *text, char *language, BOOL spell_skip_quoted)
{
    pid_t childpid;
    int sockfd[2];
    int c;
    struct config *config = session->config;

    speller->language = pool_strdup(speller->pool, language);

    if (!((config->aspell_path && config->aspell_path[0]) ||
          (config->ispell_path && config->ispell_path[0]))) {
        session_paniclog(session, "[speller_start()] No speller defined");
        return (NIL);
    }

    if (!os_socketpair(sockfd)) {
        session_paniclog(session, "[speller_start()] socketpair()");
        return (NIL);
    }

    if ((childpid = fork()) < 0) {
        session_paniclog(session, "[speller_start()] fork() failed");
        return (NIL);
    }

    if (childpid == 0) {
        close(sockfd[0]);
        dup2(sockfd[1], 0);
        dup2(sockfd[1], 1);
        dup2(sockfd[1], 2);
        close(sockfd[1]);

        if (config->aspell_path) {
            if (spell_skip_quoted) {
                execl(config->aspell_path, "aspell", "-a", "--mode=email",
                      "--encoding=utf-8", "-d", speller->language, NULL);
            } else {
                execl(config->aspell_path, "aspell", "-a",
                      "-d", speller->language, NULL);
            }
        } else {
            execl(config->ispell_path, "ispell", "-a", "-d",
                  speller->language, NULL);
        }
                  
        session_fatal(session,
                      "[speller_start()] Failed to execl() ispell");
    }

    /* Parent */
    close(sockfd[1]);

    /* Create iostream with small block size and timeout */
    speller->stream = iostream_create(speller->pool, sockfd[0], 256);
    iostream_set_timeout(speller->stream, 30);

    /* Discard banner line */
    while (((c = iogetc(speller->stream)) != EOF) && (c != '\n'));

    if (c == EOF) {
        session_paniclog(session,
                         "[speller_start()] Failed to read banner line");
        return (NIL);
    }

    speller->ignore = NIL;
    speller->pid = childpid;
    speller->line = text;
    speller->offset = 0;
    speller->cursize = 0;
    speller->output = buffer_create(NIL, 4096); /* Small output buffer */
    speller->from_speller = buffer_create(NIL, 4096);   /* Small input  buffer */
    speller->no_changes = 0;
    speller->last_line = 0;     /* Offsets for last line of output */
    speller->last_line1 = 0;

    return (T);
}

/* speller_stop() *******************************************************
 *
 * Shut down current spell check session.
 ***********************************************************************/

void speller_stop(struct speller *speller)
{
    struct speller_ignore_list *current, *next;
    int status = 0;
    int rc = 0;

    if (speller->pid) {
        if (speller->output)
            buffer_free(speller->output);

        if (speller->from_speller)
            buffer_free(speller->from_speller);

        if (speller->stream)
            iostream_close(speller->stream);

        do {
            rc = waitpid(speller->pid, &status, 0);
        } while ((rc < 0) && (errno == EINTR));

        if ((rc >= 0) && WIFEXITED(status) && (WEXITSTATUS(status) != 0))
            log_misc("Non-zero return code from spell checker");

        if (!speller->pool) {
            for (current = speller->ignore; current; current = next) {
                next = current->next;
                free(current->word);
                free(current);
            }
        }
        speller->ignore = NIL;
    }

    speller->output = NIL;
    speller->from_speller = NIL;
    speller->pid = 0;
    speller->stream = NIL;
}

/* speller_active() ******************************************************
 *
 * Check whether spell check running running.
 ************************************************************************/

BOOL speller_active(struct speller *speller)
{
    if (speller->output && speller->from_speller && speller->stream)
        return (T);

    return (NIL);
}

/* ====================================================================== */

/* speller_feedline() ***************************************************
 *
 * Feed next line of input into the speller checker. Munges first character
 * to avoid confusing ispell.
 ***********************************************************************/

void speller_feedline(struct speller *speller)
{
    char *s = speller->line;
    char c;

    /* Stop ispell from trying to interpret first character of output */
    ioputc('^', speller->stream);

    while ((c = *s++) && (c != '\015') && (c != '\012'))
        ioputc(c, speller->stream);

    ioputc('\n', speller->stream);      /* Flush line to ispell */
    ioflush(speller->stream);
}

/* speller_getline() *****************************************************
 *
 * Get line of output from the spell check engine.
 ************************************************************************/

char *speller_getline(struct speller *speller, struct pool *pool)
{
    struct buffer *b = speller->from_speller;
    unsigned long start = buffer_size(b);
    int c;

    while ((c = iogetc(speller->stream)) != EOF) {
        if (c == '\n')
            break;
        bputc(b, c);
    }

    return ((char *) buffer_fetch(b, start, buffer_size(b) - start, NIL));
}

/* ====================================================================== */

/* speller_copy_line_from_offset() *************************************
 *
 * Copy remainder of current input line from draft into speller checker. 
 ***********************************************************************/

BOOL speller_copy_from_offset(struct speller *speller)
{
    char *s = speller->line;
    int c;

    utf8_find_offset(&s, speller->offset);

    /* Copy remainder of line to output buffer */
    while ((c = *s) && (c != '\015') && (c != '\012')) {
        bputc(speller->output, c);
        s++;
    }

    if (*s) {
        if ((s[0] == '\015') && (s[1] == '\012'))
            s += 2;
        else
            s += 1;
        bputs(speller->output, "" CRLF);
    }

    if (speller->last_line > 0) {
        speller->last_line1 = speller->last_line;
        speller->last_line = buffer_size(speller->output);
    } else
        speller->last_line = buffer_size(speller->output);

    speller->line = s;
    speller->offset = 0;

    return ((s[0]) ? T : NIL);
}

/* speller_copy_to_offset() *********************************************
 *
 * Copy input line from draft into output buffer up to given offset
 *  speller: 
 *   offset: Copy up to here
 ***********************************************************************/

void speller_copy_to_offset(struct speller *speller, unsigned long offset)
{
    char *s = speller->line;
    unsigned long i;

    utf8_find_offset(&s, speller->offset);

    for (i = speller->offset; i < offset; i++)
        utf8_print_char(speller->output, &s);

    speller->offset = offset;
}

/* ====================================================================== */

/* speller_copy_remainder() *********************************************
 *
 * Copy remainder of current input line to output buffer
 ***********************************************************************/

void speller_copy_remainder(struct speller *speller)
{
    char *s = speller->line;
    char c;

    utf8_find_offset(&s, speller->offset);

    while ((c = *s++))
        bputc(speller->output, c);
}

/* speller_copy_string() ************************************************
 *
 * Copy given string into output buffer
 ***********************************************************************/

void speller_copy_string(struct speller *speller, char *s)
{
    char c;

    while ((c = *s++))
        bputc(speller->output, c);
}

/* ====================================================================== */

/* speller_copy_output() ************************************************
 *
 * Copy given string into output buffer
 ***********************************************************************/

char *speller_output(struct speller *speller)
{
    return (buffer_fetch(speller->output, 0,
                         buffer_size(speller->output), NIL));
}

/* ====================================================================== */

/* speller_print_lastline() *********************************************
 *
 * Print last last from output buffer
 *  speller:
 *        b: Target buffer
 ***********************************************************************/

void speller_print_lastline(struct speller *speller, struct buffer *b)
{
    int c;

    buffer_seek_offset(speller->output, speller->last_line1);

    while ((c = bgetc(speller->output)) != EOF)
        html_quote_char(b, c);
}

/* ====================================================================== */

/* speller_record_cursize() *********************************************
 *
 * Record current size
 ***********************************************************************/

void speller_record_cursize(struct speller *speller, unsigned long size)
{
    speller->cursize = size;
}

/* speller_fetch_cursize() **********************************************
 *
 * Fetch current size
 ***********************************************************************/

unsigned long speller_fetch_cursize(struct speller *speller)
{
    return (speller->cursize);
}

/* ====================================================================== */

/* speller_skip_input() *************************************************
 *
 * Skip input
 ***********************************************************************/

void speller_skip_input(struct speller *speller, unsigned long size)
{
    speller->offset += size;
}

/* ====================================================================== */

/* speller_add_ignore_list() ********************************************
 *
 * Add word to (temporary) ignore list
 ***********************************************************************/

void speller_add_ignore_list(struct speller *speller, char *word)
{
    struct speller_ignore_list *new
        = pool_alloc(speller->pool, sizeof(struct speller_ignore_list));

    /* Add word to front of list */
    new->word = pool_strdup(speller->pool, word);
    new->next = speller->ignore;
    speller->ignore = new;
}

/* speller_check_ignore_list() ******************************************
 *
 * Check word against current temporary ignore list.
 ***********************************************************************/

BOOL speller_check_ignore_list(struct speller *speller, char *word)
{
    struct speller_ignore_list *current = speller->ignore;

    while (current) {
        if (!strcasecmp(current->word, word))
            return (T);
        current = current->next;
    }
    return (NIL);
}