File: fields.c

package info (click to toggle)
ispell 3.4.02-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,160 kB
  • sloc: ansic: 10,120; makefile: 1,835; yacc: 1,750; objc: 385; csh: 215; python: 112; perl: 84; sh: 60; sed: 32
file content (440 lines) | stat: -rw-r--r-- 11,489 bytes parent folder | download | duplicates (4)
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
440
#ifndef lint
static char Rcs_Id[] __attribute__ ((unused)) =
    "$Id: fields.c,v 1.11 2005/04/14 14:38:23 geoff Exp $";
#endif

/*
 * $Log: fields.c,v $
 * Revision 1.11  2005/04/14 14:38:23  geoff
 * Make maxf unsigned.
 *
 * Revision 1.10  1999/01/06 20:57:19  geoff
 * Include ispell.h so proto.h will work
 *
 * Revision 1.9  1999/01/05  20:46:38  geoff
 * Get declarations from proto.h
 *
 * Revision 1.8  1998/07/06  04:56:19  geoff
 * Make strlen return an unsigned int
 *
 * Revision 1.7  1994/01/06  05:26:37  geoff
 * Get rid of all references to System V string routines, for portability
 * (sigh).
 *
 * Revision 1.6  1994/01/05  20:13:43  geoff
 * Add the maxf parameter
 *
 * Revision 1.5  1994/01/04  02:40:21  geoff
 * Make the increments settable (field_line_inc and field_field_inc).
 * Add support for the FLD_NOSHRINK flag.
 *
 * Revision 1.4  1993/09/27  17:48:02  geoff
 * Fix some lint complaints and some parenthesization errors.
 *
 * Revision 1.3  1993/09/09  01:11:11  geoff
 * Add a return value to fieldwrite.  Add support for backquotes and for
 * unstripped backslashes.
 *
 * Revision 1.2  1993/08/26  00:02:50  geoff
 * Fix a stupid null-pointer bug
 *
 * Revision 1.1  1993/08/25  21:32:05  geoff
 * Initial revision
 *
 */

#include <stdio.h>
#include "config.h"
#include "fields.h"
#include "ispell.h"
#include "proto.h"

field_t *	fieldread P ((FILE * file, char * delims,
				  int flags, unsigned int maxf));
				/* Read a line with fields from a file */
field_t *	fieldmake P ((char * line, int allocated, char * delims,
				  int flags, unsigned int maxf));
				/* Make a field structure from a line */
static field_t * fieldparse P ((field_t * fieldp, char * line, char * delims,
				  int flags, unsigned int maxf));
				/* Parse the fields in a line */
static int	fieldbackch P ((char * str, char ** out, int strip));
				/* Process backslash sequences */
int		fieldwrite P ((FILE * file, field_t * fieldp, int delim));
				/* Write a line with fields to a file */
void		fieldfree P ((field_t * fieldp));
				/* Free a field returned by fieldread */

unsigned int	field_field_inc = 20; /* Increment to increase # fields by */
unsigned int	field_line_inc = 512; /* Incr to increase line length by */

#ifndef USG
#define strchr	index
#endif /* USG */

/*
 * Read one line of the given file into a buffer, break it up into
 * fields, and return them to the caller.  The field_t structure
 * returned must eventually be freed with fieldfree.
 */
field_t * fieldread (file, delims, flags, maxf)
    FILE *		file;	/* File to read lines from */
    char *		delims;	/* Characters to use for field delimiters */
    int			flags;	/* Option flags;  see fields.h */
    unsigned int	maxf;	/* Maximum number of fields to parse */
    {
    register char *	linebuf; /* Buffer to hold the line read in */
    int			linemax; /* Maximum line buffer size */
    int			linesize; /* Current line buffer size */

    linebuf = (char *) malloc (field_line_inc);
    if (linebuf == NULL)
	return NULL;
    linemax = field_line_inc;
    linesize = 0;
    /*
     * Read in the line.
     */
    while (fgets (&linebuf[linesize], linemax - linesize, file)
      != NULL)
	{
	linesize += strlen (&linebuf[linesize]);
	if (linebuf[linesize - 1] == '\n')
	    break;
	else
	    {
	    linemax += field_line_inc;
	    linebuf = (char *) realloc (linebuf, linemax);
	    if (linebuf == NULL)
		return NULL;
	    }
	}
    if (linesize == 0)
	{
	free (linebuf);
	return NULL;
	}
    return fieldmake (linebuf, 1, delims, flags, maxf);
    }

field_t * fieldmake (line, allocated, delims, flags, maxf)
    char *		line;	/* Line to make into a field structure */
    int			allocated; /* NZ if line allocated with malloc */
    char *		delims;	/* Characters to use for field delimiters */
    int			flags;	/* Option flags;  see fields.h */
    unsigned int	maxf;	/* Maximum number of fields to parse */
    {
    register field_t *	fieldp;	/* Structure describing the fields */
    int			linesize; /* Current line buffer size */

    fieldp = (field_t *) malloc (sizeof (field_t));
    if (fieldp == NULL)
	return NULL;
    fieldp->nfields = 0;
    fieldp->linebuf = allocated ? line : NULL;
    fieldp->fields = NULL;
    fieldp->hadnl = 0;
    linesize = strlen (line);
    if (line[linesize - 1] == '\n')
	{
	line[--linesize] = '\0';
	fieldp->hadnl = 1;
	}
    /*
     * Shrink the line buffer if necessary.
     */
    if (allocated  &&  (flags & FLD_NOSHRINK) == 0)
	{
	line = fieldp->linebuf =
	  (char *) realloc (fieldp->linebuf, linesize + 1);
	if (fieldp->linebuf == NULL)
	    {
	    fieldfree (fieldp);
	    return NULL;
	    }
	}
    return fieldparse (fieldp, line, delims, flags, maxf);
    }

static field_t * fieldparse (fieldp, line, delims, flags, maxf)
    register field_t *	fieldp;	/* Field structure to parse into */
    register char *	line;	/* Line to be parsed */
    char *		delims;	/* Characters to use for field delimiters */
    int			flags;	/* Option flags;  see fields.h */
    unsigned int	maxf;	/* Maximum number of fields to parse */
    {
    unsigned int	fieldmax; /* Max size of fields array */
    char *		lineout; /* Where to store xlated char in line */
    char		quote;	/* Quote character in use */

    fieldp->nfields = 0;
    fieldmax =
      (maxf != 0  &&  maxf < field_field_inc) ? maxf + 2 : field_field_inc;
    fieldp->fields = (char **) malloc (fieldmax * sizeof (char *));
    if (fieldp->fields == NULL)
	{
	fieldfree (fieldp);
	return NULL;
	}
    if ((flags
	& (FLD_SHQUOTES | FLD_SNGLQUOTES | FLD_BACKQUOTES | FLD_DBLQUOTES))
      == FLD_SHQUOTES)
	flags |= FLD_SNGLQUOTES | FLD_BACKQUOTES | FLD_DBLQUOTES;
    while (1)
	{
	if (flags & FLD_RUNS)
	    {
	    while (*line != '\0'  &&  strchr (delims, *line) != NULL)
		line++;			/* Skip runs of delimiters */
	    if (*line == '\0')
		break;
	    }
	fieldp->fields[fieldp->nfields] = lineout = line;
	/*
	 * Skip to the next delimiter.  At the end of skipping, "line" will
	 * point to either a delimiter or a null byte.
	 */
	if (flags
	  & (FLD_SHQUOTES | FLD_SNGLQUOTES | FLD_BACKQUOTES
	    | FLD_DBLQUOTES | FLD_BACKSLASH))
	    {
	    while (*line != '\0')
		{
		if (strchr (delims, *line) != NULL)
		    break;
		else if (((flags & FLD_SNGLQUOTES)  &&  *line == '\'')
		  ||  ((flags & FLD_BACKQUOTES)  &&  *line == '`')
		  ||  ((flags & FLD_DBLQUOTES)  &&  *line == '"'))
		    {
		    if ((flags & FLD_SHQUOTES) == 0
		      &&  line != fieldp->fields[fieldp->nfields])
			quote = '\0';
		    else
			quote = *line;
		    }
		else
		    quote = '\0';
		if (quote == '\0')
		    {
		    if (*line == '\\'  &&  (flags & FLD_BACKSLASH))
			{
			line++;
			if (*line == '\0')
			    break;
			line += fieldbackch (line, &lineout,
			  flags & FLD_STRIPQUOTES);
			}
		    else
			*lineout++ = *line++;
		    }
		else
		    {
		    /* Process quoted string */
		    if ((flags & FLD_STRIPQUOTES) == 0)
			*lineout++ = quote;
		    ++line;
		    while (*line != '\0')
			{
			if (*line == quote)
			    {
			    if ((flags & FLD_STRIPQUOTES) == 0)
				*lineout++ = quote;
			    line++;		/* Go on past quote */
			    if ((flags & FLD_SHQUOTES) == 0)
				{
				while (*line != '\0'
				  &&  strchr (delims, *line) == NULL)
				    line++;	/* Skip to delimiter */
				}
			    break;
			    }
			else if (*line == '\\')
			    {
			    if (flags & FLD_BACKSLASH)
				{
				line++;
				if (*line == '\0')
				    break;
				else
				    line += fieldbackch (line, &lineout,
				      flags & FLD_STRIPQUOTES);
				}
			    else
				{
				*lineout++ = '\\';
				if (*++line == '\0')
				    break;
				*lineout++ = *line;
				}
			    }
			else
			    *lineout++ = *line++;
			}
		    }
		}
	    }
	else
	    {
	    while (*line != '\0'  &&  strchr (delims, *line) == NULL)
		line++;			/* Skip to delimiter */
	    lineout = line;
	    }
	fieldp->nfields++;
	if (*line++ == '\0')
	    break;
	if (maxf != 0  &&  fieldp->nfields > maxf)
	    break;
	*lineout = '\0';
	if (fieldp->nfields >= fieldmax)
	    {
	    fieldmax += field_field_inc;
	    fieldp->fields =
	      (char **) realloc (fieldp->fields, fieldmax * sizeof (char *));
	    if (fieldp->fields == NULL)
		{
		fieldfree (fieldp);
		return NULL;
		}
	    }
	}
    /*
     * Shrink the field pointers and return the field structure.
     */
    if ((flags & FLD_NOSHRINK) == 0  &&  fieldp->nfields >= fieldmax)
	{
	fieldp->fields = (char **) realloc (fieldp->fields,
	  (fieldp->nfields + 1) * sizeof (char *));
	if (fieldp->fields == NULL)
	    {
	    fieldfree (fieldp);
	    return NULL;
	    }
	}
    fieldp->fields[fieldp->nfields] = NULL;
    return fieldp;
    }

static int fieldbackch (str, out, strip)
    register char *	str;		/* First char of backslash sequence */
    register char **	out;		/* Where to store result */
    int			strip;		/* NZ to convert the sequence */
    {
    register int	ch;		/* Character being developed */
    char *		origstr;	/* Original value of str */

    if (!strip)
	{
	*(*out)++ = '\\';
	if (*str != 'x'  &&  *str != 'X'  &&  (*str < '0'  ||  *str > '7'))
	    {
	    *(*out)++ = *str;
	    return *str != '\0';
	    }
	}
    switch (*str)
	{
	case '\0':
	    *(*out)++ = '\0';
	    return 0;
	case 'a':
	    *(*out)++ = '\007';
	    return 1;
	case 'b':
	    *(*out)++ = '\b';
	    return 1;
	case 'f':
	    *(*out)++ = '\f';
	    return 1;
	case 'n':
	    *(*out)++ = '\n';
	    return 1;
	case 'r':
	    *(*out)++ = '\r';
	    return 1;
	case 'v':
	    *(*out)++ = '\v';
	    return 1;
	case 'X':
	case 'x':
	    /* Hexadecimal sequence */
	    origstr = str++;
	    ch = 0;
	    if (*str >= '0'  &&  *str <= '9')
		ch = *str++ - '0';
	    else if (*str >= 'a'  &&  *str <= 'f')
		ch = *str++ - 'a' + 0xa;
	    else if (*str >= 'A'  &&  *str <= 'F')
		ch = *str++ - 'A' + 0xa;
	    if (*str >= '0'  &&  *str <= '9')
		ch = (ch << 4) | (*str++ - '0');
	    else if (*str >= 'a'  &&  *str <= 'f')
		ch = (ch << 4) | (*str++ - 'a' + 0xa);
	    else if (*str >= 'A'  &&  *str <= 'F')
		ch = (ch << 4) | (*str++ - 'A' + 0xa);
	    break;
	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	    /* Octal sequence */
	    origstr = str;
	    ch = *str++ - '0';
	    if (*str >= '0'  &&  *str <= '7')
		ch = (ch << 3) | (*str++ - '0');
	    if (*str >= '0'  &&  *str <= '7')
		ch = (ch << 3) | (*str++ - '0');
	    break;
	default:
	    *(*out)++ = *str;
	    return 1;
	}
    if (strip)
	{
	*(*out)++ = ch;
	return str - origstr;
	}
    else
	{
	for (ch = 0;  origstr < str;  ch++)
	    *(*out)++ = *origstr++;
	return ch;
	}
    }

int fieldwrite (file, fieldp, delim)
    FILE *		file;	/* File to write to */
    register field_t *	fieldp;	/* Field structure to write */
    int			delim;	/* Delimiter to place between fields */
    {
    int			error;	/* NZ if an error occurs */
    register unsigned int
			fieldno; /* Number of field being written */

    error = 0;
    for (fieldno = 0;  fieldno < fieldp->nfields;  fieldno++)
	{
	if (fieldno != 0)
	    error |= putc (delim, file) == EOF;
	error |= fputs (fieldp->fields[fieldno], file) == EOF;
	}
    if (fieldp->hadnl)
	error |= putc ('\n', file) == EOF;
    return error;
    }

void fieldfree (fieldp)
    register field_t *	fieldp;	/* Field structure to free */
    {

    if (fieldp == NULL)
	return;
    if (fieldp->linebuf != NULL)
	free ((char *) fieldp->linebuf);
    if (fieldp->fields != NULL)
	free ((char *) fieldp->fields);
    free ((char *) fieldp);
    }