2006-01-04 04:44:06 +03:00
|
|
|
/* $NetBSD: join.c,v 1.26 2006/01/04 01:44:06 perry Exp $ */
|
1997-01-09 16:29:56 +03:00
|
|
|
|
1993-04-30 12:04:31 +04:00
|
|
|
/*-
|
1997-01-13 23:57:48 +03:00
|
|
|
* Copyright (c) 1991 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
1993-04-30 12:04:31 +04:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Steve Hayman of Indiana University, Michiro Hikida and David
|
|
|
|
* Goodenough.
|
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-04-30 12:04:31 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 THE REGENTS 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.
|
|
|
|
*/
|
|
|
|
|
1997-10-19 07:32:11 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-04-30 12:04:31 +04:00
|
|
|
#ifndef lint
|
1997-10-19 07:32:11 +04:00
|
|
|
__COPYRIGHT(
|
|
|
|
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
|
|
|
|
All rights reserved.\n");
|
1993-04-30 12:04:31 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-10-19 07:32:11 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "from: @(#)join.c 5.1 (Berkeley) 11/18/91";
|
|
|
|
#else
|
2006-01-04 04:44:06 +03:00
|
|
|
__RCSID("$NetBSD: join.c,v 1.26 2006/01/04 01:44:06 perry Exp $");
|
1997-10-19 07:32:11 +04:00
|
|
|
#endif
|
1993-04-30 12:04:31 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1997-10-19 07:32:11 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
1993-04-30 12:04:31 +04:00
|
|
|
#include <stdio.h>
|
1993-10-13 21:33:45 +03:00
|
|
|
#include <stdlib.h>
|
1993-04-30 12:04:31 +04:00
|
|
|
#include <string.h>
|
1998-02-03 06:47:47 +03:00
|
|
|
#include <unistd.h>
|
1993-04-30 12:04:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There's a structure per input file which encapsulates the state of the
|
|
|
|
* file. We repeatedly read lines from each file until we've read in all
|
|
|
|
* the consecutive lines from the file with a common join field. Then we
|
|
|
|
* compare the set of lines with an equivalent set from the other file.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
char *line; /* line */
|
|
|
|
u_long linealloc; /* line allocated count */
|
|
|
|
char **fields; /* line field(s) */
|
|
|
|
u_long fieldcnt; /* line field(s) count */
|
|
|
|
u_long fieldalloc; /* line field(s) allocated count */
|
|
|
|
} LINE;
|
|
|
|
|
2002-07-24 21:03:00 +04:00
|
|
|
LINE noline = {"", 0, 0, 0, 0}; /* arg for outfield if no line to output */
|
|
|
|
|
1993-04-30 12:04:31 +04:00
|
|
|
typedef struct {
|
|
|
|
FILE *fp; /* file descriptor */
|
|
|
|
u_long joinf; /* join field (-1, -2, -j) */
|
|
|
|
int unpair; /* output unpairable lines (-a) */
|
|
|
|
int number; /* 1 for file 1, 2 for file 2 */
|
|
|
|
|
|
|
|
LINE *set; /* set of lines with same field */
|
|
|
|
u_long pushback; /* line on the stack */
|
|
|
|
u_long setcnt; /* set count */
|
|
|
|
u_long setalloc; /* set allocated count */
|
|
|
|
} INPUT;
|
1997-01-13 23:57:48 +03:00
|
|
|
INPUT input1 = { NULL, 0, 0, 1, NULL, -1, 0, 0, },
|
2002-02-14 06:21:07 +03:00
|
|
|
input2 = { NULL, 0, 0, 2, NULL, -1, 0, 0, };
|
1993-04-30 12:04:31 +04:00
|
|
|
|
|
|
|
typedef struct {
|
1997-01-13 23:57:48 +03:00
|
|
|
u_long fileno; /* file number */
|
1993-04-30 12:04:31 +04:00
|
|
|
u_long fieldno; /* field number */
|
|
|
|
} OLIST;
|
|
|
|
OLIST *olist; /* output field list */
|
|
|
|
u_long olistcnt; /* output field list count */
|
|
|
|
u_long olistalloc; /* output field allocated count */
|
|
|
|
|
|
|
|
int joinout = 1; /* show lines with matched join fields (-v) */
|
|
|
|
int needsep; /* need separator character */
|
|
|
|
int spans = 1; /* span multiple delimiters (-t) */
|
|
|
|
char *empty; /* empty field replacement string (-e) */
|
|
|
|
char *tabchar = " \t"; /* delimiter characters (-t) */
|
|
|
|
|
2006-01-04 04:44:06 +03:00
|
|
|
int cmp(LINE *, u_long, LINE *, u_long);
|
|
|
|
void enomem(void);
|
|
|
|
void fieldarg(char *);
|
|
|
|
void joinlines(INPUT *, INPUT *);
|
|
|
|
int main(int, char **);
|
|
|
|
void obsolete(char **);
|
|
|
|
void outfield(LINE *, u_long);
|
|
|
|
void outoneline(INPUT *, LINE *);
|
|
|
|
void outtwoline(INPUT *, LINE *, INPUT *, LINE *);
|
|
|
|
void slurp(INPUT *);
|
|
|
|
void usage(void);
|
1993-04-30 12:04:31 +04:00
|
|
|
|
|
|
|
int
|
2006-01-04 04:44:06 +03:00
|
|
|
main(int argc, char *argv[])
|
1993-04-30 12:04:31 +04:00
|
|
|
{
|
1997-10-19 07:32:11 +04:00
|
|
|
INPUT *F1, *F2;
|
1993-04-30 12:04:31 +04:00
|
|
|
int aflag, ch, cval, vflag;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
F1 = &input1;
|
|
|
|
F2 = &input2;
|
|
|
|
|
|
|
|
aflag = vflag = 0;
|
|
|
|
obsolete(argv);
|
1997-10-19 07:32:11 +04:00
|
|
|
while ((ch = getopt(argc, argv, "\01a:e:j:1:2:o:t:v:")) != -1) {
|
1993-04-30 12:04:31 +04:00
|
|
|
switch (ch) {
|
1997-01-13 23:57:48 +03:00
|
|
|
case '\01':
|
1993-04-30 12:04:31 +04:00
|
|
|
aflag = 1;
|
|
|
|
F1->unpair = F2->unpair = 1;
|
|
|
|
break;
|
|
|
|
case '1':
|
1997-10-19 07:32:11 +04:00
|
|
|
if ((F1->joinf = strtol(optarg, &end, 10)) < 1) {
|
|
|
|
warnx("-1 option field number less than 1");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (*end) {
|
|
|
|
warnx("illegal field number -- %s", optarg);
|
|
|
|
usage();
|
|
|
|
}
|
1993-04-30 12:04:31 +04:00
|
|
|
--F1->joinf;
|
|
|
|
break;
|
|
|
|
case '2':
|
1997-10-19 07:32:11 +04:00
|
|
|
if ((F2->joinf = strtol(optarg, &end, 10)) < 1) {
|
|
|
|
warnx("-2 option field number less than 1");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (*end) {
|
|
|
|
warnx("illegal field number -- %s", optarg);
|
|
|
|
usage();
|
|
|
|
}
|
1993-04-30 12:04:31 +04:00
|
|
|
--F2->joinf;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
switch(strtol(optarg, &end, 10)) {
|
|
|
|
case 1:
|
|
|
|
F1->unpair = 1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
F2->unpair = 1;
|
|
|
|
break;
|
|
|
|
default:
|
1997-10-19 07:32:11 +04:00
|
|
|
warnx("-a option file number not 1 or 2");
|
|
|
|
usage();
|
1993-04-30 12:04:31 +04:00
|
|
|
break;
|
|
|
|
}
|
1997-10-19 07:32:11 +04:00
|
|
|
if (*end) {
|
|
|
|
warnx("illegal file number -- %s", optarg);
|
|
|
|
usage();
|
|
|
|
}
|
1993-04-30 12:04:31 +04:00
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
empty = optarg;
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
if ((F1->joinf = F2->joinf =
|
1997-10-19 07:32:11 +04:00
|
|
|
strtol(optarg, &end, 10)) < 1) {
|
|
|
|
warnx("-j option field number less than 1");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (*end) {
|
|
|
|
warnx("illegal field number -- %s", optarg);
|
|
|
|
usage();
|
|
|
|
}
|
1993-04-30 12:04:31 +04:00
|
|
|
--F1->joinf;
|
|
|
|
--F2->joinf;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
fieldarg(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
spans = 0;
|
1997-10-19 07:32:11 +04:00
|
|
|
if (strlen(tabchar = optarg) != 1) {
|
|
|
|
warnx("illegal tab character specification");
|
|
|
|
usage();
|
|
|
|
}
|
1993-04-30 12:04:31 +04:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
vflag = 1;
|
|
|
|
joinout = 0;
|
1997-01-13 23:57:48 +03:00
|
|
|
switch(strtol(optarg, &end, 10)) {
|
1993-04-30 12:04:31 +04:00
|
|
|
case 1:
|
|
|
|
F1->unpair = 1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
F2->unpair = 1;
|
|
|
|
break;
|
|
|
|
default:
|
1997-10-19 07:32:11 +04:00
|
|
|
warnx("-v option file number not 1 or 2");
|
|
|
|
usage();
|
1993-04-30 12:04:31 +04:00
|
|
|
break;
|
|
|
|
}
|
1997-10-19 07:32:11 +04:00
|
|
|
if (*end) {
|
|
|
|
warnx("illegal file number -- %s", optarg);
|
|
|
|
usage();
|
|
|
|
}
|
1993-04-30 12:04:31 +04:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (aflag && vflag)
|
1997-10-19 07:32:11 +04:00
|
|
|
errx(1, "-a and -v options mutually exclusive");
|
1993-04-30 12:04:31 +04:00
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
/* Open the files; "-" means stdin. */
|
|
|
|
if (!strcmp(*argv, "-"))
|
|
|
|
F1->fp = stdin;
|
|
|
|
else if ((F1->fp = fopen(*argv, "r")) == NULL)
|
1997-10-19 07:32:11 +04:00
|
|
|
err(1, "%s", *argv);
|
1993-04-30 12:04:31 +04:00
|
|
|
++argv;
|
|
|
|
if (!strcmp(*argv, "-"))
|
|
|
|
F2->fp = stdin;
|
|
|
|
else if ((F2->fp = fopen(*argv, "r")) == NULL)
|
1997-10-19 07:32:11 +04:00
|
|
|
err(1, "%s", *argv);
|
1993-04-30 12:04:31 +04:00
|
|
|
if (F1->fp == stdin && F2->fp == stdin)
|
1997-10-19 07:32:11 +04:00
|
|
|
errx(1, "only one input file may be stdin");
|
1993-04-30 12:04:31 +04:00
|
|
|
|
|
|
|
slurp(F1);
|
|
|
|
slurp(F2);
|
|
|
|
while (F1->setcnt && F2->setcnt) {
|
|
|
|
cval = cmp(F1->set, F1->joinf, F2->set, F2->joinf);
|
|
|
|
if (cval == 0) {
|
|
|
|
/* Oh joy, oh rapture, oh beauty divine! */
|
|
|
|
if (joinout)
|
|
|
|
joinlines(F1, F2);
|
|
|
|
slurp(F1);
|
|
|
|
slurp(F2);
|
|
|
|
} else if (cval < 0) {
|
|
|
|
/* File 1 takes the lead... */
|
|
|
|
if (F1->unpair)
|
|
|
|
joinlines(F1, NULL);
|
|
|
|
slurp(F1);
|
|
|
|
} else {
|
|
|
|
/* File 2 takes the lead... */
|
|
|
|
if (F2->unpair)
|
|
|
|
joinlines(F2, NULL);
|
|
|
|
slurp(F2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that one of the files is used up, optionally output any
|
|
|
|
* remaining lines from the other file.
|
|
|
|
*/
|
|
|
|
if (F1->unpair)
|
|
|
|
while (F1->setcnt) {
|
|
|
|
joinlines(F1, NULL);
|
|
|
|
slurp(F1);
|
|
|
|
}
|
|
|
|
if (F2->unpair)
|
|
|
|
while (F2->setcnt) {
|
|
|
|
joinlines(F2, NULL);
|
|
|
|
slurp(F2);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-01-04 04:44:06 +03:00
|
|
|
slurp(INPUT *F)
|
1993-04-30 12:04:31 +04:00
|
|
|
{
|
2000-06-10 23:03:22 +04:00
|
|
|
LINE *lp;
|
1997-01-13 23:57:48 +03:00
|
|
|
LINE tmp;
|
2003-10-16 10:56:17 +04:00
|
|
|
LINE *nline;
|
1993-04-30 12:04:31 +04:00
|
|
|
size_t len;
|
|
|
|
int cnt;
|
1993-12-22 10:23:27 +03:00
|
|
|
char *bp, *fieldp;
|
2003-10-16 10:56:17 +04:00
|
|
|
u_long nsize;
|
1993-04-30 12:04:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read all of the lines from an input file that have the same
|
|
|
|
* join field.
|
|
|
|
*/
|
2000-06-10 23:03:22 +04:00
|
|
|
for (F->setcnt = 0;; ++F->setcnt) {
|
1993-04-30 12:04:31 +04:00
|
|
|
/*
|
|
|
|
* If we're out of space to hold line structures, allocate
|
|
|
|
* more. Initialize the structure so that we know that this
|
|
|
|
* is new space.
|
|
|
|
*/
|
|
|
|
if (F->setcnt == F->setalloc) {
|
|
|
|
cnt = F->setalloc;
|
2000-06-10 23:03:22 +04:00
|
|
|
if (F->setalloc == 0)
|
2003-10-16 10:56:17 +04:00
|
|
|
nsize = 64;
|
2000-06-10 23:03:22 +04:00
|
|
|
else
|
2003-10-16 10:56:17 +04:00
|
|
|
nsize = F->setalloc << 1;
|
|
|
|
if ((nline = realloc(F->set,
|
|
|
|
nsize * sizeof(LINE))) == NULL)
|
1997-01-13 23:57:48 +03:00
|
|
|
enomem();
|
2003-10-16 10:56:17 +04:00
|
|
|
F->set = nline;
|
|
|
|
F->setalloc = nsize;
|
2000-06-10 23:03:22 +04:00
|
|
|
memset(F->set + cnt, 0,
|
|
|
|
(F->setalloc - cnt) * sizeof(LINE));
|
1993-04-30 12:04:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get any pushed back line, else get the next line. Allocate
|
|
|
|
* space as necessary. If taking the line from the stack swap
|
1997-01-13 23:57:48 +03:00
|
|
|
* the two structures so that we don't lose the allocated space.
|
|
|
|
* This could be avoided by doing another level of indirection,
|
|
|
|
* but it's probably okay as is.
|
1993-04-30 12:04:31 +04:00
|
|
|
*/
|
|
|
|
lp = &F->set[F->setcnt];
|
1997-01-13 23:57:48 +03:00
|
|
|
if (F->pushback != -1) {
|
1993-04-30 12:04:31 +04:00
|
|
|
tmp = F->set[F->setcnt];
|
|
|
|
F->set[F->setcnt] = F->set[F->pushback];
|
|
|
|
F->set[F->pushback] = tmp;
|
1997-01-13 23:57:48 +03:00
|
|
|
F->pushback = -1;
|
1993-04-30 12:04:31 +04:00
|
|
|
continue;
|
|
|
|
}
|
1994-01-04 08:16:44 +03:00
|
|
|
if ((bp = fgetln(F->fp, &len)) == NULL)
|
1993-04-30 12:04:31 +04:00
|
|
|
return;
|
1993-12-22 10:23:27 +03:00
|
|
|
if (lp->linealloc <= len + 1) {
|
2003-10-16 10:56:17 +04:00
|
|
|
char *n;
|
|
|
|
|
1993-11-15 12:30:52 +03:00
|
|
|
if (lp->linealloc == 0)
|
2003-10-16 10:56:17 +04:00
|
|
|
nsize = 128;
|
|
|
|
else
|
|
|
|
nsize = lp->linealloc;
|
|
|
|
while (nsize <= len + 1)
|
|
|
|
nsize <<= 1;
|
|
|
|
if ((n = realloc(lp->line,
|
|
|
|
nsize * sizeof(char))) == NULL)
|
1997-01-13 23:57:48 +03:00
|
|
|
enomem();
|
2003-10-16 10:56:17 +04:00
|
|
|
lp->line = n;
|
|
|
|
lp->linealloc = nsize;
|
1993-04-30 12:04:31 +04:00
|
|
|
}
|
1999-08-11 00:09:02 +04:00
|
|
|
memmove(lp->line, bp, len);
|
1993-04-30 12:04:31 +04:00
|
|
|
|
1993-12-22 10:23:27 +03:00
|
|
|
/* Replace trailing newline, if it exists. */
|
|
|
|
if (bp[len - 1] == '\n')
|
|
|
|
lp->line[len - 1] = '\0';
|
|
|
|
else
|
|
|
|
lp->line[len] = '\0';
|
|
|
|
bp = lp->line;
|
|
|
|
|
1993-04-30 12:04:31 +04:00
|
|
|
/* Split the line into fields, allocate space as necessary. */
|
|
|
|
lp->fieldcnt = 0;
|
1993-12-22 10:23:27 +03:00
|
|
|
while ((fieldp = strsep(&bp, tabchar)) != NULL) {
|
1993-04-30 12:04:31 +04:00
|
|
|
if (spans && *fieldp == '\0')
|
|
|
|
continue;
|
|
|
|
if (lp->fieldcnt == lp->fieldalloc) {
|
2003-10-16 10:56:17 +04:00
|
|
|
char **n;
|
|
|
|
|
2000-06-10 23:03:22 +04:00
|
|
|
if (lp->fieldalloc == 0)
|
2003-10-16 10:56:17 +04:00
|
|
|
nsize = 16;
|
2000-06-10 23:03:22 +04:00
|
|
|
else
|
2003-10-16 10:56:17 +04:00
|
|
|
nsize = lp->fieldalloc << 1;
|
|
|
|
if ((n = realloc(lp->fields,
|
|
|
|
nsize * sizeof(char *))) == NULL)
|
1997-01-13 23:57:48 +03:00
|
|
|
enomem();
|
2003-10-16 10:56:17 +04:00
|
|
|
lp->fields = n;
|
|
|
|
lp->fieldalloc = nsize;
|
1993-04-30 12:04:31 +04:00
|
|
|
}
|
|
|
|
lp->fields[lp->fieldcnt++] = fieldp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See if the join field value has changed. */
|
2000-06-10 23:03:22 +04:00
|
|
|
if (F->setcnt && cmp(lp, F->joinf, lp - 1, F->joinf)) {
|
1993-04-30 12:04:31 +04:00
|
|
|
F->pushback = F->setcnt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-01-04 04:44:06 +03:00
|
|
|
cmp(LINE *lp1, u_long fieldno1, LINE *lp2, u_long fieldno2)
|
1993-04-30 12:04:31 +04:00
|
|
|
{
|
1996-02-08 21:24:16 +03:00
|
|
|
|
|
|
|
if (lp1->fieldcnt <= fieldno1)
|
2000-06-10 23:15:15 +04:00
|
|
|
return (lp2->fieldcnt <= fieldno2 ? 0 : 1);
|
1996-02-08 21:24:16 +03:00
|
|
|
if (lp2->fieldcnt <= fieldno2)
|
1993-04-30 12:04:31 +04:00
|
|
|
return (-1);
|
|
|
|
return (strcmp(lp1->fields[fieldno1], lp2->fields[fieldno2]));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-01-04 04:44:06 +03:00
|
|
|
joinlines(INPUT *F1, INPUT *F2)
|
1993-04-30 12:04:31 +04:00
|
|
|
{
|
1997-10-19 07:32:11 +04:00
|
|
|
int cnt1, cnt2;
|
1993-04-30 12:04:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Output the results of a join comparison. The output may be from
|
|
|
|
* either file 1 or file 2 (in which case the first argument is the
|
|
|
|
* file from which to output) or from both.
|
|
|
|
*/
|
|
|
|
if (F2 == NULL) {
|
|
|
|
for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
|
|
|
|
outoneline(F1, &F1->set[cnt1]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
|
|
|
|
for (cnt2 = 0; cnt2 < F2->setcnt; ++cnt2)
|
|
|
|
outtwoline(F1, &F1->set[cnt1], F2, &F2->set[cnt2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-01-04 04:44:06 +03:00
|
|
|
outoneline(INPUT *F, LINE *lp)
|
1993-04-30 12:04:31 +04:00
|
|
|
{
|
1997-10-19 07:32:11 +04:00
|
|
|
int cnt;
|
1993-04-30 12:04:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Output a single line from one of the files, according to the
|
|
|
|
* join rules. This happens when we are writing unmatched single
|
|
|
|
* lines. Output empty fields in the right places.
|
|
|
|
*/
|
|
|
|
if (olist)
|
|
|
|
for (cnt = 0; cnt < olistcnt; ++cnt) {
|
1997-01-13 23:57:48 +03:00
|
|
|
if (olist[cnt].fileno == F->number)
|
|
|
|
outfield(lp, olist[cnt].fieldno);
|
2002-07-24 21:03:00 +04:00
|
|
|
else
|
|
|
|
outfield(&noline, 1);
|
1993-04-30 12:04:31 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
for (cnt = 0; cnt < lp->fieldcnt; ++cnt)
|
1997-01-13 23:57:48 +03:00
|
|
|
outfield(lp, cnt);
|
1993-04-30 12:04:31 +04:00
|
|
|
(void)printf("\n");
|
|
|
|
if (ferror(stdout))
|
1997-10-19 07:32:11 +04:00
|
|
|
err(1, "stdout");
|
1993-04-30 12:04:31 +04:00
|
|
|
needsep = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-01-04 04:44:06 +03:00
|
|
|
outtwoline(INPUT *F1, LINE *lp1, INPUT *F2, LINE *lp2)
|
1993-04-30 12:04:31 +04:00
|
|
|
{
|
1997-10-19 07:32:11 +04:00
|
|
|
int cnt;
|
1993-04-30 12:04:31 +04:00
|
|
|
|
|
|
|
/* Output a pair of lines according to the join list (if any). */
|
1998-12-19 20:05:30 +03:00
|
|
|
if (olist) {
|
1993-04-30 12:04:31 +04:00
|
|
|
for (cnt = 0; cnt < olistcnt; ++cnt)
|
1997-01-13 23:57:48 +03:00
|
|
|
if (olist[cnt].fileno == 1)
|
|
|
|
outfield(lp1, olist[cnt].fieldno);
|
|
|
|
else /* if (olist[cnt].fileno == 2) */
|
|
|
|
outfield(lp2, olist[cnt].fieldno);
|
1998-12-19 20:05:30 +03:00
|
|
|
} else {
|
1993-04-30 12:04:31 +04:00
|
|
|
/*
|
|
|
|
* Output the join field, then the remaining fields from F1
|
|
|
|
* and F2.
|
|
|
|
*/
|
1997-01-13 23:57:48 +03:00
|
|
|
outfield(lp1, F1->joinf);
|
1993-04-30 12:04:31 +04:00
|
|
|
for (cnt = 0; cnt < lp1->fieldcnt; ++cnt)
|
|
|
|
if (F1->joinf != cnt)
|
1997-01-13 23:57:48 +03:00
|
|
|
outfield(lp1, cnt);
|
1993-04-30 12:04:31 +04:00
|
|
|
for (cnt = 0; cnt < lp2->fieldcnt; ++cnt)
|
|
|
|
if (F2->joinf != cnt)
|
1997-01-13 23:57:48 +03:00
|
|
|
outfield(lp2, cnt);
|
1993-04-30 12:04:31 +04:00
|
|
|
}
|
|
|
|
(void)printf("\n");
|
|
|
|
if (ferror(stdout))
|
1997-10-19 07:32:11 +04:00
|
|
|
err(1, "stdout");
|
1993-04-30 12:04:31 +04:00
|
|
|
needsep = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-01-04 04:44:06 +03:00
|
|
|
outfield(LINE *lp, u_long fieldno)
|
1993-04-30 12:04:31 +04:00
|
|
|
{
|
|
|
|
if (needsep++)
|
|
|
|
(void)printf("%c", *tabchar);
|
1998-08-26 00:59:36 +04:00
|
|
|
if (!ferror(stdout)) {
|
2000-06-10 23:21:05 +04:00
|
|
|
if (lp->fieldcnt <= fieldno) {
|
1993-04-30 12:04:31 +04:00
|
|
|
if (empty != NULL)
|
|
|
|
(void)printf("%s", empty);
|
|
|
|
} else {
|
|
|
|
if (*lp->fields[fieldno] == '\0')
|
|
|
|
return;
|
|
|
|
(void)printf("%s", lp->fields[fieldno]);
|
|
|
|
}
|
1998-08-26 00:59:36 +04:00
|
|
|
}
|
1993-04-30 12:04:31 +04:00
|
|
|
if (ferror(stdout))
|
1997-10-19 07:32:11 +04:00
|
|
|
err(1, "stdout");
|
1993-04-30 12:04:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert an output list argument "2.1, 1.3, 2.4" into an array of output
|
|
|
|
* fields.
|
|
|
|
*/
|
|
|
|
void
|
2006-01-04 04:44:06 +03:00
|
|
|
fieldarg(char *option)
|
1993-04-30 12:04:31 +04:00
|
|
|
{
|
|
|
|
u_long fieldno;
|
|
|
|
char *end, *token;
|
2003-10-16 10:56:17 +04:00
|
|
|
OLIST *n;
|
1993-04-30 12:04:31 +04:00
|
|
|
|
2000-06-10 23:15:15 +04:00
|
|
|
while ((token = strsep(&option, ", \t")) != NULL) {
|
1993-04-30 12:04:31 +04:00
|
|
|
if (*token == '\0')
|
|
|
|
continue;
|
1997-10-19 07:32:11 +04:00
|
|
|
if ((token[0] != '1' && token[0] != '2') || token[1] != '.')
|
|
|
|
errx(1, "malformed -o option field");
|
1993-04-30 12:04:31 +04:00
|
|
|
fieldno = strtol(token + 2, &end, 10);
|
|
|
|
if (*end)
|
1997-10-19 07:32:11 +04:00
|
|
|
errx(1, "malformed -o option field");
|
1993-04-30 12:04:31 +04:00
|
|
|
if (fieldno == 0)
|
1997-10-19 07:32:11 +04:00
|
|
|
errx(1, "field numbers are 1 based");
|
1993-04-30 12:04:31 +04:00
|
|
|
if (olistcnt == olistalloc) {
|
2003-10-16 10:56:17 +04:00
|
|
|
if ((n = realloc(olist,
|
|
|
|
(olistalloc + 50) * sizeof(OLIST))) == NULL)
|
1997-01-13 23:57:48 +03:00
|
|
|
enomem();
|
2003-10-16 10:56:17 +04:00
|
|
|
olist = n;
|
|
|
|
olistalloc += 50;
|
1993-04-30 12:04:31 +04:00
|
|
|
}
|
1997-01-13 23:57:48 +03:00
|
|
|
olist[olistcnt].fileno = token[0] - '0';
|
1993-04-30 12:04:31 +04:00
|
|
|
olist[olistcnt].fieldno = fieldno - 1;
|
|
|
|
++olistcnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-01-04 04:44:06 +03:00
|
|
|
obsolete(char **argv)
|
1993-04-30 12:04:31 +04:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char **p, *ap, *t;
|
|
|
|
|
1997-10-19 07:32:11 +04:00
|
|
|
while ((ap = *++argv) != NULL) {
|
1993-04-30 12:04:31 +04:00
|
|
|
/* Return if "--". */
|
|
|
|
if (ap[0] == '-' && ap[1] == '-')
|
|
|
|
return;
|
|
|
|
switch (ap[1]) {
|
|
|
|
case 'a':
|
|
|
|
/*
|
|
|
|
* The original join allowed "-a", which meant the
|
|
|
|
* same as -a1 plus -a2. POSIX 1003.2, Draft 11.2
|
|
|
|
* only specifies this as "-a 1" and "a -2", so we
|
|
|
|
* have to use another option flag, one that is
|
|
|
|
* unlikely to ever be used or accidentally entered
|
|
|
|
* on the command line. (Well, we could reallocate
|
|
|
|
* the argv array, but that hardly seems worthwhile.)
|
|
|
|
*/
|
|
|
|
if (ap[2] == '\0')
|
|
|
|
ap[1] = '\01';
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
/*
|
|
|
|
* The original join allowed "-j[12] arg" and "-j arg".
|
|
|
|
* Convert the former to "-[12] arg". Don't convert
|
|
|
|
* the latter since getopt(3) can handle it.
|
|
|
|
*/
|
|
|
|
switch(ap[2]) {
|
|
|
|
case '1':
|
|
|
|
if (ap[3] != '\0')
|
|
|
|
goto jbad;
|
|
|
|
ap[1] = '1';
|
|
|
|
ap[2] = '\0';
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
if (ap[3] != '\0')
|
|
|
|
goto jbad;
|
|
|
|
ap[1] = '2';
|
|
|
|
ap[2] = '\0';
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
default:
|
1997-10-19 07:32:11 +04:00
|
|
|
jbad: errx(1, "illegal option -- %s", ap);
|
1993-04-30 12:04:31 +04:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
/*
|
1997-01-13 23:57:48 +03:00
|
|
|
* The original join allowed "-o arg arg". Convert to
|
|
|
|
* "-o arg -o arg".
|
1993-04-30 12:04:31 +04:00
|
|
|
*/
|
|
|
|
if (ap[2] != '\0')
|
|
|
|
break;
|
|
|
|
for (p = argv + 2; *p; ++p) {
|
1997-10-19 07:32:11 +04:00
|
|
|
if ((p[0][0] != '1' && p[0][0] != '2') ||
|
1997-01-13 23:57:48 +03:00
|
|
|
p[0][1] != '.')
|
1993-04-30 12:04:31 +04:00
|
|
|
break;
|
|
|
|
len = strlen(*p);
|
|
|
|
if (len - 2 != strspn(*p + 2, "0123456789"))
|
|
|
|
break;
|
|
|
|
if ((t = malloc(len + 3)) == NULL)
|
1997-01-13 23:57:48 +03:00
|
|
|
enomem();
|
1993-04-30 12:04:31 +04:00
|
|
|
t[0] = '-';
|
|
|
|
t[1] = 'o';
|
1997-10-19 07:32:11 +04:00
|
|
|
memmove(t + 2, *p, len + 1);
|
1993-04-30 12:04:31 +04:00
|
|
|
*p = t;
|
|
|
|
}
|
|
|
|
argv = p - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-01-13 23:57:48 +03:00
|
|
|
void
|
2006-01-04 04:44:06 +03:00
|
|
|
enomem(void)
|
1997-01-13 23:57:48 +03:00
|
|
|
{
|
1997-10-19 07:32:11 +04:00
|
|
|
errx(1, "no memory");
|
1997-01-13 23:57:48 +03:00
|
|
|
}
|
|
|
|
|
1993-04-30 12:04:31 +04:00
|
|
|
void
|
2006-01-04 04:44:06 +03:00
|
|
|
usage(void)
|
1993-04-30 12:04:31 +04:00
|
|
|
{
|
2004-07-13 15:53:57 +04:00
|
|
|
(void)fprintf(stderr,
|
|
|
|
"usage: %s [-a fileno | -v fileno] [-e string] [-j fileno field]\n"
|
|
|
|
" [-o list] [-t char] [-1 field] [-2 field] file1 file2\n",
|
|
|
|
getprogname());
|
1993-04-30 12:04:31 +04:00
|
|
|
exit(1);
|
|
|
|
}
|