File: ijoin.c

package info (click to toggle)
ispell 3.1.20.0-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,676 kB
  • ctags: 966
  • sloc: ansic: 8,374; makefile: 2,023; yacc: 1,711; lisp: 1,613; sh: 1,053; objc: 385; csh: 215; python: 112
file content (460 lines) | stat: -rw-r--r-- 14,086 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#ifndef lint
static char Rcs_Id[] =
    "$Id: ijoin.c,v 1.6 1994/10/18 04:03:21 geoff Exp $";
#endif

/*
 * Copyright 1992, 1993, Geoff Kuenning, Granada Hills, CA
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All modifications to the source code must be clearly marked as
 *    such.  Binary redistributions based on modified source code
 *    must be clearly marked as modified versions in the documentation
 *    and/or other materials provided with the distribution.
 * 4. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgment:
 *      This product includes software developed by Geoff Kuenning and
 *      other unpaid contributors.
 * 5. The name of Geoff Kuenning may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY GEOFF KUENNING AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL GEOFF KUENNING OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * "Join" command for ispell.
 *
 * This command is a complete reimplementation of the UNIX "join"
 * command, except that fields cannot be separated by a newline, it
 * can handle lines of unlimited length, and the preceding sort can
 * treat characters as either signed or unsigned.
 *
 * Usage:
 *
 *	ijoin [options] file1 file2
 *
 * See the UNIX "join" manual page for option descriptions.  Only
 * nonstandard options are described here.
 *
 * Either file1 or file2 may be "-", in which case the standard input
 * is used for that file.
 *
 * Normally, ijoin uses "strcmp" to compare fields.  This is the
 * correct thing to do on most systems if you are using the
 * system-provided "sort" command to sort the input files before
 * feeding them to ijoin.  In some cases, however, the sort command
 * you use will disagree with strcmp about how to handle characters
 * with the high bit set.  If this is the case, you can specify the
 * "-s" (signed comparisons) or "-u" (unsigned comparisons) option to
 * force ijoin to conform to the method used by the sort program.
 * This is only necessary if one of the input files contains 8-bit
 * characters in the field that is being joined on.
 *
 * On some older machines with non-ANSI compilers, the "-s" option
 * will be ineffective because characters default to unsigned.
 * However, this option should be unnecessary on those machines, so no
 * harm will be done.
 */

/*
 * $Log: ijoin.c,v $
 * Revision 1.6  1994/10/18  04:03:21  geoff
 * Fix a couple of bugs, one where the last field on a line could be
 * output incorrectly, and one where fields from the wrong file could be
 * output.
 *
 * Revision 1.5  1994/01/25  07:11:36  geoff
 * Get rid of all old RCS log lines in preparation for the 3.1 release.
 *
 */

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

#ifdef __STDC__
#define SIGNED	signed
#else /* __STDC */
#define SIGNED
#endif /* __STDC */

int		main P ((int argc, char * argv[])); /* Join files */
static void	usage P ((void));	/* Issue a usage message */
static void	dojoin P ((void));	/* Actually perform the join */
static void	full_output P ((field_t * line1, field_t * line2));
					/* Output everything from both lines */
static void	selected_output P ((field_t * line1, field_t * line2));
					/* Output selected fields */
static int	strscmp P ((SIGNED char * a, SIGNED char * b));
					/* Signed version of strcmp */
static int	strucmp P ((unsigned char * a, unsigned char * b));
					/* Unsigned version of strcmp */

typedef struct
    {
    int		file;			/* Number of file to output from */
    int		field;			/* Number of field to output */
    }
		outlist_t;		/* Output description list */

static int		(*compare) () = strcmp; /* Comparison function */
static char *		emptyfield = ""; /* Use this to replace empty fields */
static FILE *		file1;		/* First file to join */
static FILE *		file2;		/* Second file to join */
static int		join1field = 0;	/* Field to join file 1 on */
static int		join2field = 0;	/* Field to join file 2 on */
static int		maxf[2] = {0, 0}; /* Max field to parse in each file */
static outlist_t *	outlist = NULL;	/* List of fields to write */
static int		outlistsize;	/* Number of elements in outlist */
static int		runs = FLD_RUNS; /* Set if runs of tabchar same as 1 */
static char *		tabchar = " \t"; /* Field separator character(s) */
static int		unpairable1 = 0; /* NZ if -a1 */
static int		unpairable2 = 0; /* NZ if -a2 */


int main (argc, argv)			/* Join files */
    int			argc;		/* Argument count */
    char *		argv[];		/* Argument vector */
    {
    
    while (argc > 3  &&  argv[1][0] == '-')
	{
	argc--;
	argv++;
	switch (argv[0][1])
	    {
	    case 'a':			/* produce output for unpairables */
		if (argv[0][2] == '1')
		    unpairable1 = 1;
		else if (argv[0][2] == '2')
		    unpairable2 = 1;
		else if (argv[0][2] == '\0')
		    unpairable1 = unpairable2 = 1;
		else
		    usage ();
		break;
	    case 'e':			/* Replace empty fields with this */
		argc--;
		argv++;
		emptyfield = *argv;
		break;
	    case 'j':			/* Specify field to join on */
		if (argv[0][2] == '1')
		    join1field = atoi (argv[1]) - 1;
		else if (argv[0][2] == '2')
		    join2field = atoi (argv[1]) - 1;
		else if (argv[0][2] == '\0')
		    join1field = join2field = atoi (argv[1]) - 1;
		else
		    usage ();
		argc--;
		argv++;
		break;
	    case 'o':			/* Specify output list */
		/*
		 * We will assume that all remaining switch arguments
		 * are used to describe the output list.  This will
		 * occasionally result in malloc'ing a few too many
		 * elements, but no real harm will be done.
		 */
		outlist =
		  (outlist_t *) malloc ((argc - 3) * sizeof (outlist_t));
		if (outlist == NULL)
		    {
		    (void) fprintf (stderr, "ijoin:  out of memory!\n");
		    return 1;
		    }
		for (outlistsize = 0, argc--, argv++;
		  argc > 2  &&  (argv[0][0] == '1'  ||  argv[0][0] == '2')
		    &&  argv[0][1] == '.';
		  argc--, argv++, outlistsize++)
		    {
		    outlist[outlistsize].file = argv[0][0] - '0';
		    outlist[outlistsize].field = atoi (&argv[0][2]) - 1;
		    if (maxf[outlist[outlistsize].file - 1]
		      <= outlist[outlistsize].field)
			maxf[outlist[outlistsize].file - 1] =
			  outlist[outlistsize].field + 1;
		    }
		argc++;			/* Un-do arg that stopped us */
		argv--;
		break;
	    case 't':
		tabchar = &argv[0][2];
		runs &= ~FLD_RUNS;
		break;
	    case 's':
		compare = strscmp;
		break;
	    case 'u':
		compare = strucmp;
		break;
	    default:
		usage ();
		break;
	    }
	}
    if (argc != 3)
	usage ();
    if (strcmp (argv[1], "-") == 0)
	file1 = stdin;
    else
	{
	file1 = fopen (argv[1], "r");
	if (file1 == NULL)
	    perror (argv[1]);
	}
    file2 = fopen (argv[2], "r");
    if (file2 == NULL)
	perror (argv[2]);
    if (file1 == NULL  ||  file2 == NULL)
	return 1;
    dojoin ();
    return 0;
    }

static void usage ()			/* Issue a usage message */
    {

    (void) fprintf (stderr,
      "Usage:  ijoin [-an] [-e s] [-jn m] [-o n.m ...] [-tc] file1 file2\n");
    exit (1);
    }

static void dojoin ()			/* Actually perform the join */
    {
    int			comparison;	/* Result of comparing the lines */
    long		file2pos;	/* Position file 2 started at */
    register field_t *	line1;		/* Line from file 1 */
    register field_t *	line2;		/* Line from file 2 */
    int			pairable;	/* NZ if lines can be paired */
    int			skip2;		/* No. of "unpairable" 2's to skip */

    runs |= FLD_NOSHRINK;		/* Don't do excessive reallocations */
    field_line_inc = BUFSIZ;		/* Allocate line bfr in huge chunks */
    line1 = fieldread (file1, tabchar, runs, maxf[0]);
    file2pos = ftell (file2);
    skip2 = 0;
    if (file2pos == -1)
	{
	(void) fprintf (stderr, "ijoin:  Can't seek file ");
	perror ("2");
	exit (1);
	}
    line2 = fieldread (file2, tabchar, runs, maxf[1]);
    while (line1 != NULL  ||  line2 != NULL)
	{
	/*
	 * Do a little work to reduce the number of calls to realloc, at
	 * the expense of slightly-increased memory usage.
	 */
	if (line1 != NULL  &&  line1->nfields >= field_field_inc)
	    field_field_inc = line1->nfields + 1;
	if (line2 != NULL  &&  line2->nfields >= field_field_inc)
	    field_field_inc = line2->nfields + 1;
	/*
	 * Determine if the lines can be paired.
	 */
	pairable = 1;
	if (line1 == NULL)
	    {
	    pairable = 0;
	    comparison = 1;		/* This causes file 2 to advance */
	    }
	else if (join1field >= line1->nfields)
	    {
	    pairable = 0;
	    comparison = -1;		/* This causes file 1 to advance */
	    }
	if (line2 == NULL)
	    {
	    pairable = 0;
	    comparison = -1;		/* This causes file 1 to advance */
	    }
	else if (join2field >= line2->nfields)
	    {
	    pairable = 0;
	    comparison = 1;		/* This causes file 2 to advance */
	    }
	if (pairable)
	    {
	    comparison = (*compare) (line1->fields[join1field],
	      line2->fields[join2field]);
	    pairable = (comparison == 0);
	    }
	if (pairable)
	    {
	    /*
	     * The two lines can be paired.  Produce output.
	     */
	    if (outlist == NULL)
		full_output (line1, line2);
	    else
		selected_output (line1, line2);
	    }
	/*
	 * Advance through the files
	 */
	if (comparison < 0)
	    {
	    if (unpairable1)
		{
		if (outlist == NULL)
		    (void) fieldwrite (stdout, line1, tabchar[0]);
		else
		    selected_output (line1, (field_t *) NULL);
		}
	    fieldfree (line1);
	    line1 = fieldread (file1, tabchar, runs, maxf[0]);
	    }
	else if (comparison > 0)
	    {
	    if (skip2 > 0)
		skip2--;
	    else if (unpairable2)
		{
		if (outlist == NULL)
		    (void) fieldwrite (stdout, line2, tabchar[0]);
		else
		    selected_output ((field_t *) NULL, line2);
		}
	    fieldfree (line2);
	    file2pos = ftell (file2);
	    line2 = fieldread (file2, tabchar, runs, maxf[1]);
	    }
	else
	    {
	    /*
	     * Here's the tricky part.  We have to advance file 2
	     * until comparisons fail, and then back it up and advance
	     * file 1.
	     */
	    skip2++;
	    fieldfree (line2);
	    line2 = fieldread (file2, tabchar, runs, maxf[1]);
	    if (line2 == NULL
	      ||  join2field >= line2->nfields
	      ||  (*compare) (line1->fields[join1field],
		  line2->fields[join2field])
		!= 0)
		{
		(void) fseek (file2, file2pos, 0);
		fieldfree (line2);
		line2 = fieldread (file2, tabchar, runs, maxf[1]);
		fieldfree (line1);
		line1 = fieldread (file1, tabchar, runs, maxf[0]);
		if (line1 != NULL  &&  line2 != NULL
		  &&  join1field < line1->nfields
		  &&  join2field < line2->nfields
		  &&  (*compare) (line1->fields[join1field],
			line2->fields[join2field])
		    == 0)
		    skip2 = 0;
		}
	    }
	}
    }

static void full_output (line1, line2)	/* Output everything from both lines */
    register field_t *	line1;		/* Line from file 1 */
    register field_t *	line2;		/* Line from file 2 */
    {
    register int	fieldno;	/* Number of field being handled */

    (void) fputs (line1->fields[join1field], stdout);
    for (fieldno = 0;  fieldno < line1->nfields;  fieldno++)
	{
	if (fieldno == join1field)
	    continue;
	(void) putchar (tabchar[0]);
	if (line1->fields[fieldno][0] == '\0')
	    (void) fputs (emptyfield, stdout);
	else
	    (void) fputs (line1->fields[fieldno], stdout);
	}
    for (fieldno = 0;  fieldno < line2->nfields;  fieldno++)
	{
	if (fieldno == join2field)
	    continue;
	(void) putchar (tabchar[0]);
	if (line2->fields[fieldno][0] == '\0')
	    (void) fputs (emptyfield, stdout);
	else
	    (void) fputs (line2->fields[fieldno], stdout);
	}
    (void) putchar ('\n');
    }

static void	selected_output (line1, line2) /* Output selected fields */
    field_t *		line1;		/* Line from file 1 */
    field_t *		line2;		/* Line from file 2 */
    {
    register field_t *	cline;		/* Current line being handled */
    register int	listno;		/* Number of output list entry */
    
    for (listno = 0;  listno < outlistsize;  listno++)
	{
	if (listno != 0)
	    (void) putchar (tabchar[0]);
	if (outlist[listno].file == 1)
	    cline = line1;
	else
	    cline = line2;
	if (cline == NULL
	  ||  outlist[listno].field >= cline->nfields
	  ||  cline->fields[outlist[listno].field][0] == '\0')
	    (void) fputs (emptyfield, stdout);
	else
	    (void) fputs (cline->fields[outlist[listno].field], stdout);
	}
    (void) putchar ('\n');
    }

static int strscmp (a, b)		/* Compare signed strings */
    register SIGNED char * a;		/* First string to compare */
    register SIGNED char * b;		/* Second string to compare */
    {

    while (*a != '\0')
	{
	if (*a++ != *b++)
	    return *--a - *--b;
	}
    return *a - *b;
    }

static int strucmp (a, b)		/* Compare unsigned strings */
    register unsigned char * a;		/* First string to compare */
    register unsigned char * b;		/* Second string to compare */
    {

    while (*a != '\0')
	{
	if (*a++ != *b++)
	    return *--a - *--b;
	}
    return *a - *b;
    }