File: ispell.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (297 lines) | stat: -rw-r--r-- 5,224 bytes parent folder | download | duplicates (3)
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
/*
** Copyright 1998 - 2006 Double Precision, Inc.  See COPYING for
** distribution information.
*/


/*
** $Id: ispell.c,v 1.5 2006/05/28 15:29:52 mrsam Exp $
*/
#include	"ispell.h"
#include	<stdio.h>
#include	<stdlib.h>
#if	HAVE_CONFIG_H
#include	<config.h>
#endif

#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif

#include	<sys/types.h>
#if	HAVE_SYS_WAIT_H
#include	<sys/wait.h>
#endif

#include	<signal.h>
#include	<string.h>

static pid_t ispell_pid;
static RETSIGTYPE (*prevsighandler)(int);

#ifndef	ISPELL
#define	ISPELL	"/usr/bin/ispell"
#endif

static void wait4pid()
{
int	waitstat;

	if (prevsighandler == SIG_DFL)
		while ( wait(&waitstat) != ispell_pid)
			;
}

static int fork_ispell(const char *ispellline, const char *dict,
				struct ispell *isp)
{
int	fdbuf[2], fdbuf2[2];
size_t	l;
int	waitstat;
int	c;
FILE	*ispell_resp;
size_t	ispell_resp_buf_size=0;
const char *args[6];
int	argn;

	isp->ispell_buf=0;

	prevsighandler=signal(SIGCHLD, SIG_DFL);
	signal(SIGCHLD, prevsighandler);

	if (pipe(fdbuf) < 0)
	{
		perror("pipe");
		return (-1);
	}

	switch (ispell_pid=fork())	{
	case -1:
		perror("fork");
		close(fdbuf[0]);
		close(fdbuf[1]);
		return (-1);
	case 0:
		if (pipe(fdbuf2) < 0) {
			perror("pipe");
			_exit(-1);
		}

		/*
		** First child will fork and run the real ispell, and write to
		** it on stdin.
		*/

		switch (ispell_pid=fork())	{
		case -1:
			perror("fork");
			_exit(-1);
		case 0:
			dup2(fdbuf2[0], 0);
			dup2(fdbuf[1], 1);
			close(fdbuf[0]);
			close(fdbuf[1]);
			close(fdbuf2[0]);
			close(fdbuf2[1]);
			argn=0;
			args[argn++]="ispell";
			args[argn++]="-a";
			if (dict)
			{
				args[argn++]="-d";
				args[argn++]=dict;
			}
			args[argn]=0;
			execv(ISPELL, (char **)args);
			perror("fork_ispell: execv " ISPELL);
			_exit(1);
		}
		close(fdbuf[0]);
		close(fdbuf[1]);
		close(fdbuf2[0]);
		signal(SIGCHLD, SIG_DFL);
		l=strlen(ispellline);
		while (l)
		{
		int	n=write(fdbuf2[1], ispellline, l);

			if (n <= 0)
			{
				perror("fork_ispell: write");
				_exit(1);
			}
			ispellline += n;
			l -= n;
		}
		if (write(fdbuf2[1], "\n", 1) != 1)
		{
			perror("fork_ispell: write");
			_exit(1);
		}
		close(fdbuf2[1]);
		while ( wait(&waitstat) != ispell_pid)
			;
		_exit(0);
	}

	close(fdbuf[1]);
	ispell_resp=fdopen(fdbuf[0], "r");
	if (!ispell_resp)
	{
		perror("fork_ispell: fdopen");
		close(fdbuf[0]);
		wait4pid();
		return (-1);
	}

	l=0;

	for (;;)
	{
		if (l >= ispell_resp_buf_size)
		{
		char	*newbuf;
		size_t	l=ispell_resp_buf_size + BUFSIZ;
			newbuf=isp->ispell_buf ? realloc(isp->ispell_buf, l)
					:malloc(l);
			if (!newbuf)
			{
				perror("fork_ispell: malloc/realloc");
				if (isp->ispell_buf)	free(isp->ispell_buf);
				isp->ispell_buf=0;
				wait4pid();
				fclose(ispell_resp);
				return (-1);
			}
			isp->ispell_buf=newbuf;
			ispell_resp_buf_size=l;
		}

		if ( (c=getc(ispell_resp)) == EOF )
			break;

		isp->ispell_buf[l++]=c;
	}

	isp->ispell_buf[l]=0;
	fclose(ispell_resp);
	wait4pid();
	return (0);
}

struct ispell *ispell_run(const char *dict, const char *line)
{
char	*p, *q;
struct	ispell *isp;
struct	ispell_misspelled **islistptr, *ip;
int	c;
int	nmisses;
struct	ispell_suggestion **sp;

	if ((isp=(struct ispell *)malloc(sizeof(struct ispell))) == 0)
	{
		perror("malloc");
		return (0);
	}
	isp->ispell_buf=0;
	isp->first_misspelled=0;
	islistptr=&isp->first_misspelled;

	if (fork_ispell(line, dict, isp))
	{
		ispell_free(isp);
		fprintf(stderr, "ERR: ispell_run: fork_ispell failed\n");
		return (0);
	}

	if ((p=strchr(isp->ispell_buf, '\n')) == 0)
	{
		fprintf(stderr, "ERR: ispell_run: incomplete result from ispell\n");
		ispell_free(isp);
		return (0);
	}
	++p;
	q=0;
	islistptr= &isp->first_misspelled;
	for ( ; p && *p != '\n'; p=q)
	{
		if ((q=strchr(p, '\n')) != 0)
			*q++=0;

		if ( *p != '&' && *p != '?' && *p != '#')	continue;

		if ( (ip=*islistptr=(struct ispell_misspelled *)
			malloc(sizeof(struct ispell_misspelled))) == 0)
		{
			perror("malloc");
			ispell_free(isp);
			return (0);
		}

		ip->next=0;
		islistptr= &ip->next;

		c=*p++;
		while (*p == ' ')	++p;
		ip->misspelled_word=p;
		while (*p && *p != ' ')	++p;
		if (*p)	*p++=0;

		nmisses=0;
		while (*p == ' ')	++p;
		if (c == '&' || c == '?')
		{
			while (*p >= '0' && *p <= '9')
				nmisses=nmisses * 10 + *p++ - '0';
			while (*p == ' ')	++p;
		}

		ip->word_pos=0;
		while (*p >= '0' && *p <= '9')
			ip->word_pos=ip->word_pos * 10 + *p++ - '0';
		ip->first_suggestion=0;
		if (nmisses == 0 || *p != ':')	continue;
		++p;
		sp= &ip->first_suggestion;
		while (nmisses)
		{
			if ( (*sp=(struct ispell_suggestion *)
				malloc(sizeof(struct ispell_suggestion)))
				== 0)
			{
				perror("malloc");
				ispell_free(isp);
				return (0);
			}
			(*sp)->suggested_word=strtok(p, ", ");
			p=0;
			(*sp)->next=0;
			sp= &(*sp)->next;
			--nmisses;
		}
	}
	return (isp);
}

void ispell_free(struct ispell *isp)
{
struct ispell_misspelled *msp;

	while ((msp=isp->first_misspelled) != 0)
	{
	struct ispell_suggestion *sgp;

		isp->first_misspelled=msp->next;

		while ((sgp=msp->first_suggestion) != 0)
		{
			msp->first_suggestion=sgp->next;
			free(sgp);
		}

		free(msp);
	}

	if (isp->ispell_buf)	free(isp->ispell_buf);
	free(isp);
}