1 /* $NetBSD: join.c,v 1.31 2011/09/04 20:27:52 joerg Exp $ */
4 * Copyright (c) 1991 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
8 * Steve Hayman of Indiana University, Michiro Hikida and David
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
40 #include <sys/cdefs.h>
42 __COPYRIGHT("@(#) Copyright (c) 1991\
43 The Regents of the University of California. All rights reserved.");
48 static char sccsid
[] = "from: @(#)join.c 5.1 (Berkeley) 11/18/91";
50 __RCSID("$NetBSD: join.c,v 1.31 2011/09/04 20:27:52 joerg Exp $");
54 #include <sys/types.h>
64 * There's a structure per input file which encapsulates the state of the
65 * file. We repeatedly read lines from each file until we've read in all
66 * the consecutive lines from the file with a common join field. Then we
67 * compare the set of lines with an equivalent set from the other file.
70 char *line
; /* line */
71 u_long linealloc
; /* line allocated count */
72 char **fields
; /* line field(s) */
73 u_long fieldcnt
; /* line field(s) count */
74 u_long fieldalloc
; /* line field(s) allocated count */
77 static char nolineline
[1] = { '\0' };
78 static LINE noline
= {nolineline
, 0, 0, 0, 0}; /* arg for outfield if no line to output */
81 FILE *fp
; /* file descriptor */
82 u_long joinf
; /* join field (-1, -2, -j) */
83 int unpair
; /* output unpairable lines (-a) */
84 int number
; /* 1 for file 1, 2 for file 2 */
86 LINE
*set
; /* set of lines with same field */
87 u_long pushback
; /* line on the stack */
88 u_long setcnt
; /* set count */
89 u_long setalloc
; /* set allocated count */
92 static INPUT input1
= { NULL
, 0, 0, 1, NULL
, -1, 0, 0, },
93 input2
= { NULL
, 0, 0, 2, NULL
, -1, 0, 0, };
96 u_long fileno
; /* file number */
97 u_long fieldno
; /* field number */
100 static OLIST
*olist
; /* output field list */
101 static u_long olistcnt
; /* output field list count */
102 static u_long olistalloc
; /* output field allocated count */
104 static int joinout
= 1; /* show lines with matched join fields (-v) */
105 static int needsep
; /* need separator character */
106 static int spans
= 1; /* span multiple delimiters (-t) */
107 static char *empty
; /* empty field replacement string (-e) */
108 static const char *tabchar
= " \t"; /* delimiter characters (-t) */
110 static int cmp(LINE
*, u_long
, LINE
*, u_long
);
111 __dead
static void enomem(void);
112 static void fieldarg(char *);
113 static void joinlines(INPUT
*, INPUT
*);
114 static void obsolete(char **);
115 static void outfield(LINE
*, u_long
);
116 static void outoneline(INPUT
*, LINE
*);
117 static void outtwoline(INPUT
*, LINE
*, INPUT
*, LINE
*);
118 static void slurp(INPUT
*);
119 __dead
static void usage(void);
122 main(int argc
, char *argv
[])
125 int aflag
, ch
, cval
, vflag
;
133 while ((ch
= getopt(argc
, argv
, "\01a:e:j:1:2:o:t:v:")) != -1) {
137 F1
->unpair
= F2
->unpair
= 1;
140 if ((F1
->joinf
= strtol(optarg
, &end
, 10)) < 1) {
141 warnx("-1 option field number less than 1");
145 warnx("illegal field number -- %s", optarg
);
151 if ((F2
->joinf
= strtol(optarg
, &end
, 10)) < 1) {
152 warnx("-2 option field number less than 1");
156 warnx("illegal field number -- %s", optarg
);
163 switch(strtol(optarg
, &end
, 10)) {
171 warnx("-a option file number not 1 or 2");
176 warnx("illegal file number -- %s", optarg
);
184 if ((F1
->joinf
= F2
->joinf
=
185 strtol(optarg
, &end
, 10)) < 1) {
186 warnx("-j option field number less than 1");
190 warnx("illegal field number -- %s", optarg
);
201 if (strlen(tabchar
= optarg
) != 1) {
202 warnx("illegal tab character specification");
209 switch(strtol(optarg
, &end
, 10)) {
217 warnx("-v option file number not 1 or 2");
222 warnx("illegal file number -- %s", optarg
);
235 errx(1, "-a and -v options mutually exclusive");
240 /* Open the files; "-" means stdin. */
241 if (!strcmp(*argv
, "-"))
243 else if ((F1
->fp
= fopen(*argv
, "r")) == NULL
)
246 if (!strcmp(*argv
, "-"))
248 else if ((F2
->fp
= fopen(*argv
, "r")) == NULL
)
250 if (F1
->fp
== stdin
&& F2
->fp
== stdin
)
251 errx(1, "only one input file may be stdin");
255 while (F1
->setcnt
&& F2
->setcnt
) {
256 cval
= cmp(F1
->set
, F1
->joinf
, F2
->set
, F2
->joinf
);
258 /* Oh joy, oh rapture, oh beauty divine! */
263 } else if (cval
< 0) {
264 /* File 1 takes the lead... */
269 /* File 2 takes the lead... */
277 * Now that one of the files is used up, optionally output any
278 * remaining lines from the other file.
311 * Read all of the lines from an input file that have the same
314 for (F
->setcnt
= 0;; ++F
->setcnt
) {
316 * If we're out of space to hold line structures, allocate
317 * more. Initialize the structure so that we know that this
320 if (F
->setcnt
== F
->setalloc
) {
322 if (F
->setalloc
== 0)
325 nsize
= F
->setalloc
<< 1;
326 if ((nline
= realloc(F
->set
,
327 nsize
* sizeof(LINE
))) == NULL
)
331 memset(F
->set
+ cnt
, 0,
332 (F
->setalloc
- cnt
) * sizeof(LINE
));
336 * Get any pushed back line, else get the next line. Allocate
337 * space as necessary. If taking the line from the stack swap
338 * the two structures so that we don't lose the allocated space.
339 * This could be avoided by doing another level of indirection,
340 * but it's probably okay as is.
342 lp
= &F
->set
[F
->setcnt
];
343 if (F
->pushback
!= (u_long
)-1) {
344 tmp
= F
->set
[F
->setcnt
];
345 F
->set
[F
->setcnt
] = F
->set
[F
->pushback
];
346 F
->set
[F
->pushback
] = tmp
;
347 F
->pushback
= (u_long
)-1;
350 if ((bp
= fgetln(F
->fp
, &len
)) == NULL
)
352 if (lp
->linealloc
<= len
+ 1) {
355 if (lp
->linealloc
== 0)
358 nsize
= lp
->linealloc
;
359 while (nsize
<= len
+ 1)
361 if ((n
= realloc(lp
->line
,
362 nsize
* sizeof(char))) == NULL
)
365 lp
->linealloc
= nsize
;
367 memmove(lp
->line
, bp
, len
);
369 /* Replace trailing newline, if it exists. */
370 if (bp
[len
- 1] == '\n')
371 lp
->line
[len
- 1] = '\0';
373 lp
->line
[len
] = '\0';
376 /* Split the line into fields, allocate space as necessary. */
378 while ((fieldp
= strsep(&bp
, tabchar
)) != NULL
) {
379 if (spans
&& *fieldp
== '\0')
381 if (lp
->fieldcnt
== lp
->fieldalloc
) {
384 if (lp
->fieldalloc
== 0)
387 nsize
= lp
->fieldalloc
<< 1;
388 if ((n
= realloc(lp
->fields
,
389 nsize
* sizeof(char *))) == NULL
)
392 lp
->fieldalloc
= nsize
;
394 lp
->fields
[lp
->fieldcnt
++] = fieldp
;
397 /* See if the join field value has changed. */
398 if (F
->setcnt
&& cmp(lp
, F
->joinf
, lp
- 1, F
->joinf
)) {
399 F
->pushback
= F
->setcnt
;
406 cmp(LINE
*lp1
, u_long fieldno1
, LINE
*lp2
, u_long fieldno2
)
409 if (lp1
->fieldcnt
<= fieldno1
)
410 return (lp2
->fieldcnt
<= fieldno2
? 0 : 1);
411 if (lp2
->fieldcnt
<= fieldno2
)
413 return (strcmp(lp1
->fields
[fieldno1
], lp2
->fields
[fieldno2
]));
417 joinlines(INPUT
*F1
, INPUT
*F2
)
422 * Output the results of a join comparison. The output may be from
423 * either file 1 or file 2 (in which case the first argument is the
424 * file from which to output) or from both.
427 for (cnt1
= 0; cnt1
< F1
->setcnt
; ++cnt1
)
428 outoneline(F1
, &F1
->set
[cnt1
]);
431 for (cnt1
= 0; cnt1
< F1
->setcnt
; ++cnt1
)
432 for (cnt2
= 0; cnt2
< F2
->setcnt
; ++cnt2
)
433 outtwoline(F1
, &F1
->set
[cnt1
], F2
, &F2
->set
[cnt2
]);
437 outoneline(INPUT
*F
, LINE
*lp
)
442 * Output a single line from one of the files, according to the
443 * join rules. This happens when we are writing unmatched single
444 * lines. Output empty fields in the right places.
447 for (cnt
= 0; cnt
< olistcnt
; ++cnt
) {
448 if (olist
[cnt
].fileno
== (u_long
)F
->number
)
449 outfield(lp
, olist
[cnt
].fieldno
);
451 outfield(&noline
, 1);
454 for (cnt
= 0; cnt
< lp
->fieldcnt
; ++cnt
)
463 outtwoline(INPUT
*F1
, LINE
*lp1
, INPUT
*F2
, LINE
*lp2
)
467 /* Output a pair of lines according to the join list (if any). */
469 for (cnt
= 0; cnt
< olistcnt
; ++cnt
)
470 if (olist
[cnt
].fileno
== 1)
471 outfield(lp1
, olist
[cnt
].fieldno
);
472 else /* if (olist[cnt].fileno == 2) */
473 outfield(lp2
, olist
[cnt
].fieldno
);
476 * Output the join field, then the remaining fields from F1
479 outfield(lp1
, F1
->joinf
);
480 for (cnt
= 0; cnt
< lp1
->fieldcnt
; ++cnt
)
481 if (F1
->joinf
!= cnt
)
483 for (cnt
= 0; cnt
< lp2
->fieldcnt
; ++cnt
)
484 if (F2
->joinf
!= cnt
)
494 outfield(LINE
*lp
, u_long fieldno
)
497 (void)printf("%c", *tabchar
);
498 if (!ferror(stdout
)) {
499 if (lp
->fieldcnt
<= fieldno
) {
501 (void)printf("%s", empty
);
503 if (*lp
->fields
[fieldno
] == '\0')
505 (void)printf("%s", lp
->fields
[fieldno
]);
513 * Convert an output list argument "2.1, 1.3, 2.4" into an array of output
517 fieldarg(char *option
)
523 while ((token
= strsep(&option
, ", \t")) != NULL
) {
526 if ((token
[0] != '1' && token
[0] != '2') || token
[1] != '.')
527 errx(1, "malformed -o option field");
528 fieldno
= strtol(token
+ 2, &end
, 10);
530 errx(1, "malformed -o option field");
532 errx(1, "field numbers are 1 based");
533 if (olistcnt
== olistalloc
) {
534 if ((n
= realloc(olist
,
535 (olistalloc
+ 50) * sizeof(OLIST
))) == NULL
)
540 olist
[olistcnt
].fileno
= token
[0] - '0';
541 olist
[olistcnt
].fieldno
= fieldno
- 1;
547 obsolete(char **argv
)
552 while ((ap
= *++argv
) != NULL
) {
553 /* Return if "--". */
554 if (ap
[0] == '-' && ap
[1] == '-')
559 * The original join allowed "-a", which meant the
560 * same as -a1 plus -a2. POSIX 1003.2, Draft 11.2
561 * only specifies this as "-a 1" and "a -2", so we
562 * have to use another option flag, one that is
563 * unlikely to ever be used or accidentally entered
564 * on the command line. (Well, we could reallocate
565 * the argv array, but that hardly seems worthwhile.)
572 * The original join allowed "-j[12] arg" and "-j arg".
573 * Convert the former to "-[12] arg". Don't convert
574 * the latter since getopt(3) can handle it.
592 jbad
: errx(1, "illegal option -- %s", ap
);
598 * The original join allowed "-o arg arg". Convert to
603 for (p
= argv
+ 2; *p
; ++p
) {
604 if ((p
[0][0] != '1' && p
[0][0] != '2') ||
608 if (len
- 2 != strspn(*p
+ 2, "0123456789"))
610 if ((t
= malloc(len
+ 3)) == NULL
)
614 memmove(t
+ 2, *p
, len
+ 1);
626 errx(1, "no memory");
632 (void)fprintf(stderr
,
633 "usage: %s [-a fileno | -v fileno] [-e string] [-j fileno field]\n"
634 " [-o list] [-t char] [-1 field] [-2 field] file1 file2\n",