From: Lionel Sambuc Date: Tue, 4 Dec 2012 14:07:20 +0000 (+0100) Subject: Replacing join with NetBSD join X-Git-Tag: v3.2.1~164 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=refs%2Fchanges%2F35%2F135%2F5;p=minix.git Replacing join with NetBSD join Change-Id: I1257736f755eab0ce39124f0d78bfa48426ba820 --- diff --git a/commands/Makefile b/commands/Makefile index 92500969d..73e104dd4 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -12,7 +12,7 @@ SUBDIR= add_route arp ash at backup banner basename btrace cal \ find finger fingerd fix fold format fortune fsck.mfs \ ftp101 gcore gcov-pull getty grep head hexdump host \ hostaddr id ifconfig ifdef \ - intr ipcrm ipcs irdpd isoread join kill last \ + intr ipcrm ipcs irdpd isoread kill last \ less loadkeys loadramdisk logger look lp \ lpd ls lspci mail MAKEDEV \ mesg mined mkfifo \ diff --git a/commands/join/Makefile b/commands/join/Makefile deleted file mode 100644 index c7d224c5e..000000000 --- a/commands/join/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -PROG= join -MAN= - -.include diff --git a/commands/join/join.c b/commands/join/join.c deleted file mode 100644 index 5f8f59f0e..000000000 --- a/commands/join/join.c +++ /dev/null @@ -1,360 +0,0 @@ -/* join - relation data base operator Author: Saeko Hirabayashi */ - -/* Written by Saeko Hirabayashi, 1989. - * 1992-01-28 Modified by Kouichi Hirabayashi to add some POSIX1003.2 options. - * - * This a free program. - */ - -#include -#include - -#define MAXFLD 200 /* maximum # of fields to accept */ - -int main(int argc, char **argv); -void error(char *s, char *t); -void usage(void); -void match(void); -void f1_only(void); -void f2_only(void); -void output(int flag); -void outfld(int file); -void outputf(int flag); -int compare(void); -int get1(void); -int get2(int back); -int getrec(int file); -int split(int file); -int atoi(char *str); -int exit(int val); -FILE * efopen(char *file, char *mode); -void(*outfun) (int file); - -#define F1 1 -#define F2 2 -#define SEP (sep ? sep : ' ') - -FILE *fp[2]; /* file pointer for file1 and file2 */ -long head; /* head of the current (same)key group of the - * file2 */ - -char buf[2][BUFSIZ]; /* input buffer for file1 and file2 */ -char *fld[2][MAXFLD]; /* field vector for file1 and file2 */ -int nfld[2]; /* # of fields for file1 and file2 */ - -int kpos[2]; /* key field position for file1 and file2 - * (from 0) */ -char oldkey[BUFSIZ]; /* previous key of the file1 */ - -struct { /* output list by -o option */ - int o_file; /* file #: 0 or 1 */ - int o_field; /* field #: 0, 1, 2, .. */ -} olist[MAXFLD]; -int nout; /* # of output filed */ - -int aflag; /* n for '-an': F1 or F2 or both */ -int vflag; /* n for '-vn': F1 or F2 or both */ -char *es; /* s for '-e s' */ -char sep; /* c for -tc: filed separator */ -char *cmd; /* name of this program */ - -int main(argc, argv) -int argc; -char **argv; -{ - register char *s; - int c, i, j; - - cmd = argv[0]; - outfun = output; /* default output form */ - - while (--argc > 0 && (*++argv)[0] == '-' && (*argv)[1]) { - /* "-" is a file name (stdin) */ - s = argv[0] + 1; - if ((c = *s) == '-' && !s[1]) { - ++argv; - --argc; - break; /* -- */ - } - if (*++s == '\0') { - s = *++argv; - --argc; - } - switch (c) { - case 'a': /* add unpairable line to output */ - vflag = 0; - switch (*s) { - case '1': aflag |= F1; break; - case '2': aflag |= F2; break; - default: aflag |= (F1 | F2); break; - } - break; - - case 'e': /* replace empty field by es */ - es = s; - break; - - case 'j': /* key field (obsolute) */ - c = *s++; - if (*s == '\0') { - s = *++argv; - --argc; - } - - case '1': /* key field of file1 */ - case '2': /* key field of file2 */ - i = atoi(s) - 1; - - switch (c) { - case '1': kpos[0] = i; break; - case '2': kpos[1] = i; break; - default: kpos[0] = kpos[1] = i; - break; - } - break; - - case 'o': /* specify output format */ - do { - i = j = 0; - sscanf(s, "%d.%d", &i, &j); - if (i < 1 || j < 1 || i > 2) usage(); - olist[nout].o_file = i - 1; - olist[nout].o_field = j - 1; - nout++; - if ((s = strchr(s, ',')) != (char *) 0) - s++; - else { - s = *++argv; - --argc; - } - } while (argc > 2 && *s != '-'); - ++argc; - --argv; /* compensation */ - outfun = outputf; - break; - - case 't': /* tab char */ - sep = *s; - break; - - case 'v': /* output unpairable line only */ - aflag = 0; - switch (*s) { - case '1': vflag |= F1; break; - case '2': vflag |= F2; break; - default: vflag |= (F1 | F2); break; - } - break; - - default: usage(); - } - } - if (argc != 2) usage(); - - fp[0] = strcmp(argv[0], "-") ? efopen(argv[0], "r") : stdin; - fp[1] = efopen(argv[1], "r"); - - nfld[0] = get1(); /* read file1 */ - nfld[1] = get2(0); /* read file2 */ - - while (nfld[0] || nfld[1]) { - if ((i = compare()) == 0) - match(); - else if (i < 0) - f1_only(); - else - f2_only(); - } - fflush(stdout); - - exit(0); -} - -void usage() -{ - fprintf(stderr, - "Usage: %s [-an|-vn] [-e str] [-o list] [-tc] [-1 f] [-2 f] file1 file2\n", - cmd); - exit(1); -} - -int compare() -{ /* compare key field */ - register int r; - - if (nfld[1] == 0) /* file2 EOF */ - r = -1; - else if (nfld[0] == 0) /* file1 EOF */ - r = 1; - else { - if (nfld[0] <= kpos[0]) - error("missing key field in file1", (char *) 0); - if (nfld[1] <= kpos[1]) - error("missing key field in file2", (char *) 0); - - r = strcmp(fld[0][kpos[0]], fld[1][kpos[1]]); - } - return r; -} - -void match() -{ - long p; - - if (!vflag) (*outfun) (F1 | F2); - - p = ftell(fp[1]); - nfld[1] = get2(0); /* check key order */ - if (nfld[1] == 0 || strcmp(fld[0][kpos[0]], fld[1][kpos[1]])) { - nfld[0] = get1(); - if (strcmp(fld[0][kpos[0]], oldkey) == 0) { - fseek(fp[1], head, 0); /* re-do from head */ - nfld[1] = get2(1); /* don't check key order */ - } else - head = p; /* mark here */ - } -} - -void f1_only() -{ - if ((aflag & F1) || (vflag & F1)) (*outfun) (F1); - nfld[0] = get1(); -} - -void f2_only() -{ - if ((aflag & F2) || (vflag & F2)) (*outfun) (F2); - head = ftell(fp[1]); /* mark */ - nfld[1] = get2(0); /* check key order */ -} - -void output(f) -{ /* default output form */ - if (f & F1) - fputs(fld[0][kpos[0]], stdout); - else - fputs(fld[1][kpos[1]], stdout); - if (f & F1) outfld(0); - if (f & F2) outfld(1); - fputc('\n', stdout); -} - -void outfld(file) -{ /* output all fields except key_field */ - register int i; - int k, n; - - k = kpos[file]; - n = nfld[file]; - for (i = 0; i < n; i++) - if (i != k) { - fputc(SEP, stdout); - fputs(fld[file][i], stdout); - } -} - -void outputf(f) -{ /* output by '-o list' */ - int i, j, k; - register char *s; - - for (i = k = 0; i < nout; i++) { - j = olist[i].o_file; - if ((f & (j + 1)) && (olist[i].o_field < nfld[j])) - s = fld[j][olist[i].o_field]; - else - s = es; - if (s) { - if (k++) fputc(SEP, stdout); - fputs(s, stdout); - } - } - fputc('\n', stdout); -} - -int get1() -{ /* read file1 */ - int r; - static char oldkey1[BUFSIZ]; - - if (fld[0][kpos[0]]) { - strcpy(oldkey, fld[0][kpos[0]]); /* save previous key for control */ - } - r = getrec(0); - - if (r) { - if (strcmp(oldkey1, fld[0][kpos[0]]) > 0) - error("file1 is not sorted", (char *) 0); - strcpy(oldkey1, fld[0][kpos[0]]); /* save prev key for sort check */ - } - return r; -} - -int get2(back) -{ /* read file2 */ - static char oldkey2[BUFSIZ]; - int r; - - r = getrec(1); - - if (r) { - if (!back && strcmp(oldkey2, fld[1][kpos[1]]) > 0) - error("file2 is not sorted", (char *) 0); - strcpy(oldkey2, fld[1][kpos[1]]); /* save prev key for sort check */ - } - return r; -} - -int getrec(file) -{ /* read one line to split it */ - if (fgets(buf[file], BUFSIZ, fp[file]) == (char *) 0) - *buf[file] = '\0'; - else if (*buf[file] == '\n' || *buf[file] == '\r') - error("null line in file%s", file ? "1" : "0"); - - return split(file); -} - -int split(file) -{ /* setup fields */ - register int n; - register char *s, *t; - - for (n = 0, s = buf[file]; *s && *s != '\n' && *s != '\r';) { - if (sep) { - for (t = s; *s && *s != sep && *s != '\n' && *s != '\r'; s++); - } else { - while (*s == ' ' || *s == '\t') - s++; /* skip leading white space */ - for (t = s; *s && *s != ' ' && *s != '\t' - && *s != '\n' && *s != '\r'; s++); - /* We will treat trailing white space as NULL field */ - } - if (*s) *s++ = '\0'; - fld[file][n++] = t; - if (n == MAXFLD) error("too many filed in file%s", file ? "1" : "0"); - } - fld[file][n] = (char *) 0; - - return n; -} - -FILE *efopen(file, mode) -char *file, *mode; -{ - FILE *fp; - - if ((fp = fopen(file, mode)) == (FILE *) 0) error("can't open %s", file); - - return fp; -} - -void error(s, t) -char *s, *t; -{ - fprintf(stderr, "%s: ", cmd); - fprintf(stderr, s, t); - fprintf(stderr, "\n"); - - exit(1); -} diff --git a/man/man1/Makefile b/man/man1/Makefile index f1edbc0c5..0abec304e 100644 --- a/man/man1/Makefile +++ b/man/man1/Makefile @@ -7,7 +7,7 @@ MAN= ash.1 at.1 banner.1 basename.1 \ env.1 expand.1 expr.1 factor.1 \ finger.1 flexdoc.1 fmt.1 fold.1 format.1 fortune.1 \ fsck.mfs.1 head.1 host.1 hostaddr.1 ifdef.1 \ - install.1 isodir.1 isoinfo.1 isoread.1 join.1 kill.1 \ + install.1 isodir.1 isoinfo.1 isoread.1 kill.1 \ last.1 loadfont.1 loadkeys.1 logger.1 \ look.1 lp.1 ls.1 lspci.1 mail.1 \ mesg.1 mixer.1 mkfs.mfs.1 \ diff --git a/man/man1/join.1 b/man/man1/join.1 deleted file mode 100644 index e43a346ef..000000000 --- a/man/man1/join.1 +++ /dev/null @@ -1,119 +0,0 @@ -.\" @(#)join.1 6.1 (Berkeley) 4/29/85 -.\" -.TH JOIN 1 "April 29, 1985" -.AT 3 -.SH NAME -join \- relational database operator -.SH SYNOPSIS -.B join -.RB [ \-a\fIn ] -.RB [ \-e -.IR s ] -.RB [ \-o -.IR list ] -.RB [ \-t\fIc ] -file1 file2 -.SH DESCRIPTION -.B Join -forms, on the standard output, -a join -of the two relations specified by the lines of -.I file1 -and -.IR file2 . -If -.I file1 -is `\-', the standard input is used. -.PP -.I File1 -and -.I file2 -must be sorted in increasing ASCII collating -sequence on the fields -on which they are to be joined, -normally the first in each line. -.PP -There is one line in the output -for each pair of lines in -.I file1 -and -.I file2 -that have identical join fields. -The output line normally consists of the common field, -then the rest of the line from -.IR file1 , -then the rest of the line from -.IR file2 . -.PP -Fields are normally separated by blank, tab or newline. -In this case, multiple separators count as one, and -leading separators are discarded. -.PP -These options are recognized: -.TP -.BI \-a n -In addition to the normal output, -produce a line for each unpairable line in file -.IR n , -where -.I n -is 1 or 2. -.TP -.BI \-e " s" -Replace empty output fields by string -.IR s . -.ig -.TP -.BI \-j "n m" -Join on the -.IR m th -field of file -.IR n . -If -.I n -is missing, use the -.IR m th -field in each file. -.. -.TP -.BI \-o " list" -Each output line comprises the fields specified in -.IR list , -each element of which has the form -.IR n . m , -where -.I n -is a file number and -.I m -is a field number. -.PP -.TP -.BI \-t c -Use character -.I c -as a separator (tab character). -Every appearance of -.I c -in a line is significant. -.SH "SEE ALSO" -.BR sort (1), -.BR comm (1), -.BR awk (1x). -.SH BUGS -With default field separation, -the collating sequence is that of -.BR "sort \-b" ; -with -.BR \-t , -the sequence is that of a plain sort. -.PP -The conventions of -.BR join , -.BR sort , -.BR comm , -.BR uniq , -.BR look -and -.BR awk (1x) -are wildly incongruous. -.\" ref. to awk(9) man page corrected -- ASW 2005-01-15 diff --git a/releasetools/nbsd_ports b/releasetools/nbsd_ports index 285e5d06a..661bdc036 100644 --- a/releasetools/nbsd_ports +++ b/releasetools/nbsd_ports @@ -53,6 +53,7 @@ 2012/10/17 12:00:00,tools/gmp 2012/10/17 12:00:00,tools/headerlist 2012/10/17 12:00:00,tools/host-mkdep +2012/10/17 12:00:00,tools/join 2012/10/17 12:00:00,tools/lex 2012/10/17 12:00:00,tools/lorder 2012/10/17 12:00:00,tools/Makefile @@ -68,6 +69,7 @@ 2012/10/17 12:00:00,tools/tic 2012/10/17 12:00:00,usr.bin/gzip/Makefile 2012/10/17 12:00:00,usr.bin/indent +2012/10/17 12:00:00,usr.bin/join 2012/10/17 12:00:00,usr.bin/lorder 2012/10/17 12:00:00,usr.bin/Makefile 2012/10/17 12:00:00,usr.bin/Makefile.inc diff --git a/tools/Makefile b/tools/Makefile index d43d89b5b..e4b8d71d8 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -60,7 +60,7 @@ LINT_BITS= lint lint2 # Dependencies in SUBDIR below ordered to maximize parallel ability. SUBDIR= host-mkdep .WAIT compat .WAIT \ binstall .WAIT mktemp .WAIT sed .WAIT \ - genassym \ + genassym join \ lorder makewhatis mkdep mtree nbperf .WAIT \ m4 \ .WAIT mkfs.mfs \ diff --git a/tools/join/Makefile b/tools/join/Makefile new file mode 100644 index 000000000..22081cfb3 --- /dev/null +++ b/tools/join/Makefile @@ -0,0 +1,6 @@ +# $NetBSD: Makefile,v 1.1 2007/01/14 16:17:29 apb Exp $ + +HOSTPROGNAME= ${_TOOL_PREFIX}join +HOST_SRCDIR= usr.bin/join + +.include "${.CURDIR}/../Makefile.host" diff --git a/usr.bin/Makefile b/usr.bin/Makefile index 69085dde3..92f1ea20e 100644 --- a/usr.bin/Makefile +++ b/usr.bin/Makefile @@ -10,7 +10,7 @@ SUBDIR= \ \ du \ genassym \ - indent \ + indent join \ ldd \ login lorder m4 \ make man \ diff --git a/usr.bin/join/Makefile b/usr.bin/join/Makefile new file mode 100644 index 000000000..bb68cbea5 --- /dev/null +++ b/usr.bin/join/Makefile @@ -0,0 +1,6 @@ +# $NetBSD: Makefile,v 1.4 1997/10/19 03:32:11 lukem Exp $ +# from: @(#)Makefile 8.1 (Berkeley) 6/6/93 + +PROG= join + +.include diff --git a/usr.bin/join/join.1 b/usr.bin/join/join.1 new file mode 100644 index 000000000..db48ef302 --- /dev/null +++ b/usr.bin/join/join.1 @@ -0,0 +1,209 @@ +.\" $NetBSD: join.1,v 1.13 2012/04/08 22:00:39 wiz Exp $ +.\" +.\" Copyright (c) 1990, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the Institute of Electrical and Electronics Engineers, Inc. +.\" +.\" 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. Neither the name of the University nor the names of its contributors +.\" 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. +.\" +.\" from: @(#)join.1 8.3 (Berkeley) 4/28/95 +.\" $NetBSD: join.1,v 1.13 2012/04/08 22:00:39 wiz Exp $ +.\" +.Dd April 28, 1995 +.Dt JOIN 1 +.Os +.Sh NAME +.Nm join +.Nd relational database operator +.Sh SYNOPSIS +.Nm +.Op Fl a Ar file_number | Fl v Ar file_number +.Op Fl e Ar string +.Op Fl j Ar file_number field +.Op Fl o Ar list +.Op Fl t Ar char +.Op Fl \&1 Ar field +.Op Fl \&2 Ar field +.Ar file1 file2 +.Sh DESCRIPTION +The join utility performs an ``equality join'' on the specified files +and writes the result to the standard output. +The ``join field'' is the field in each file by which the files are compared. +The first field in each line is used by default. +There is one line in the output for each pair of lines in +.Ar file1 +and +.Ar file2 +which have identical join fields. +Each output line consists of the join field, the remaining fields from +.Ar file1 +and then the remaining fields from +.Ar file2 . +.Pp +The default field separators are tab and space characters. +In this case, multiple tabs and spaces count as a single field separator, +and leading tabs and spaces are ignored. +The default output field separator is a single space character. +.Pp +Many of the options use file and field numbers. +Both file numbers and field numbers are 1 based, i.e. the first file on +the command line is file number 1 and the first field is field number 1. +The following options are available: +.Bl -tag -width Fl +.It Fl a Ar file_number +In addition to the default output, produce a line for each unpairable +line in file +.Ar file_number . +(The argument to +.Fl a +must not be preceded by a space; see the +.Sx COMPATIBILITY +section.) +.It Fl e Ar string +Replace empty output fields with +.Ar string . +.It Fl o Ar list +The +.Fl o +option specifies the fields that will be output from each file for +each line with matching join fields. +Each element of +.Ar list +has the form +.Ql file_number.field , +where +.Ar file_number +is a file number and +.Ar field +is a field number. +The elements of list must be either comma (``,'') or whitespace separated. +(The latter requires quoting to protect it from the shell, or, a simpler +approach is to use multiple +.Fl o +options.) +.It Fl t Ar char +Use character +.Ar char +as a field delimiter for both input and output. +Every occurrence of +.Ar char +in a line is significant. +.It Fl v Ar file_number +Do not display the default output, but display a line for each unpairable +line in file +.Ar file_number . +The options +.Fl v Ar 1 +and +.Fl v Ar 2 +may be specified at the same time. +.It Fl 1 Ar field +Join on the +.Ar field Ns 'th +field of file 1. +.It Fl 2 Ar field +Join on the +.Ar field Ns 'th +field of file 2. +.El +.Pp +When the default field delimiter characters are used, the files to be joined +should be ordered in the collating sequence of +.Xr sort 1 , +using the +.Fl b +option, on the fields on which they are to be joined, otherwise +.Nm +may not report all field matches. +When the field delimiter characters are specified by the +.Fl t +option, the collating sequence should be the same as +.Xr sort 1 +without the +.Fl b +option. +.Pp +If one of the arguments +.Ar file1 +or +.Ar file2 +is ``-'', the standard input is used. +.Pp +The +.Nm +utility exits 0 on success, and \*[Gt]0 if an error occurs. +.Sh COMPATIBILITY +For compatibility with historic versions of +.Nm , +the following options are available: +.Bl -tag -width Fl +.It Fl a +In addition to the default output, produce a line for each unpairable line +in both file 1 and file 2. +(To distinguish between this and +.Fl a Ar file_number , +.Nm +currently requires that the latter not include any white space.) +.It Fl j1 Ar field +Join on the +.Ar field Ns 'th +field of file 1. +.It Fl j2 Ar field +Join on the +.Ar field Ns 'th +field of file 2. +.It Fl j Ar field +Join on the +.Ar field Ns 'th +field of both file 1 and file 2. +.It Fl o Ar list ... +Historical implementations of +.Nm +permitted multiple arguments to the +.Fl o +option. +These arguments were of the form ``file_number.field_number'' as described +for the current +.Fl o +option. +This has obvious difficulties in the presence of files named ``1.2''. +.El +.Pp +These options are available only so historic shell scripts don't require +modification and should not be used. +.Sh SEE ALSO +.Xr awk 1 , +.Xr comm 1 , +.Xr paste 1 , +.Xr sort 1 , +.Xr uniq 1 +.Sh STANDARDS +The +.Nm +command is expected to be +.St -p1003.2 +compatible. diff --git a/usr.bin/join/join.c b/usr.bin/join/join.c new file mode 100644 index 000000000..b377ec275 --- /dev/null +++ b/usr.bin/join/join.c @@ -0,0 +1,637 @@ +/* $NetBSD: join.c,v 1.31 2011/09/04 20:27:52 joerg Exp $ */ + +/*- + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * 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. + * 3. Neither the name of the University nor the names of its contributors + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#ifndef lint +__COPYRIGHT("@(#) Copyright (c) 1991\ + The Regents of the University of California. All rights reserved."); +#endif /* not lint */ + +#ifndef lint +#if 0 +static char sccsid[] = "from: @(#)join.c 5.1 (Berkeley) 11/18/91"; +#else +__RCSID("$NetBSD: join.c,v 1.31 2011/09/04 20:27:52 joerg Exp $"); +#endif +#endif /* not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * 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; + +static char nolineline[1] = { '\0' }; +static LINE noline = {nolineline, 0, 0, 0, 0}; /* arg for outfield if no line to output */ + +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; + +static INPUT input1 = { NULL, 0, 0, 1, NULL, -1, 0, 0, }, + input2 = { NULL, 0, 0, 2, NULL, -1, 0, 0, }; + +typedef struct { + u_long fileno; /* file number */ + u_long fieldno; /* field number */ +} OLIST; + +static OLIST *olist; /* output field list */ +static u_long olistcnt; /* output field list count */ +static u_long olistalloc; /* output field allocated count */ + +static int joinout = 1; /* show lines with matched join fields (-v) */ +static int needsep; /* need separator character */ +static int spans = 1; /* span multiple delimiters (-t) */ +static char *empty; /* empty field replacement string (-e) */ +static const char *tabchar = " \t"; /* delimiter characters (-t) */ + +static int cmp(LINE *, u_long, LINE *, u_long); +__dead static void enomem(void); +static void fieldarg(char *); +static void joinlines(INPUT *, INPUT *); +static void obsolete(char **); +static void outfield(LINE *, u_long); +static void outoneline(INPUT *, LINE *); +static void outtwoline(INPUT *, LINE *, INPUT *, LINE *); +static void slurp(INPUT *); +__dead static void usage(void); + +int +main(int argc, char *argv[]) +{ + INPUT *F1, *F2; + int aflag, ch, cval, vflag; + char *end; + + F1 = &input1; + F2 = &input2; + + aflag = vflag = 0; + obsolete(argv); + while ((ch = getopt(argc, argv, "\01a:e:j:1:2:o:t:v:")) != -1) { + switch (ch) { + case '\01': + aflag = 1; + F1->unpair = F2->unpair = 1; + break; + case '1': + 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(); + } + --F1->joinf; + break; + case '2': + 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(); + } + --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: + warnx("-a option file number not 1 or 2"); + usage(); + break; + } + if (*end) { + warnx("illegal file number -- %s", optarg); + usage(); + } + break; + case 'e': + empty = optarg; + break; + case 'j': + if ((F1->joinf = F2->joinf = + strtol(optarg, &end, 10)) < 1) { + warnx("-j option field number less than 1"); + usage(); + } + if (*end) { + warnx("illegal field number -- %s", optarg); + usage(); + } + --F1->joinf; + --F2->joinf; + break; + case 'o': + fieldarg(optarg); + break; + case 't': + spans = 0; + if (strlen(tabchar = optarg) != 1) { + warnx("illegal tab character specification"); + usage(); + } + break; + case 'v': + vflag = 1; + joinout = 0; + switch(strtol(optarg, &end, 10)) { + case 1: + F1->unpair = 1; + break; + case 2: + F2->unpair = 1; + break; + default: + warnx("-v option file number not 1 or 2"); + usage(); + break; + } + if (*end) { + warnx("illegal file number -- %s", optarg); + usage(); + } + break; + case '?': + default: + usage(); + } + } + argc -= optind; + argv += optind; + + if (aflag && vflag) + errx(1, "-a and -v options mutually exclusive"); + + if (argc != 2) + usage(); + + /* Open the files; "-" means stdin. */ + if (!strcmp(*argv, "-")) + F1->fp = stdin; + else if ((F1->fp = fopen(*argv, "r")) == NULL) + err(1, "%s", *argv); + ++argv; + if (!strcmp(*argv, "-")) + F2->fp = stdin; + else if ((F2->fp = fopen(*argv, "r")) == NULL) + err(1, "%s", *argv); + if (F1->fp == stdin && F2->fp == stdin) + errx(1, "only one input file may be stdin"); + + 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 (F1->fp != stdin) + fclose(F1->fp); + + if (F2->unpair) + while (F2->setcnt) { + joinlines(F2, NULL); + slurp(F2); + } + if (F2->fp != stdin) + fclose(F2->fp); + + return 0; +} + +static void +slurp(INPUT *F) +{ + LINE *lp; + LINE tmp; + LINE *nline; + size_t len; + u_long cnt; + char *bp, *fieldp; + u_long nsize; + + /* + * Read all of the lines from an input file that have the same + * join field. + */ + for (F->setcnt = 0;; ++F->setcnt) { + /* + * 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; + if (F->setalloc == 0) + nsize = 64; + else + nsize = F->setalloc << 1; + if ((nline = realloc(F->set, + nsize * sizeof(LINE))) == NULL) + enomem(); + F->set = nline; + F->setalloc = nsize; + memset(F->set + cnt, 0, + (F->setalloc - cnt) * sizeof(LINE)); + } + + /* + * Get any pushed back line, else get the next line. Allocate + * space as necessary. If taking the line from the stack swap + * 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. + */ + lp = &F->set[F->setcnt]; + if (F->pushback != (u_long)-1) { + tmp = F->set[F->setcnt]; + F->set[F->setcnt] = F->set[F->pushback]; + F->set[F->pushback] = tmp; + F->pushback = (u_long)-1; + continue; + } + if ((bp = fgetln(F->fp, &len)) == NULL) + return; + if (lp->linealloc <= len + 1) { + char *n; + + if (lp->linealloc == 0) + nsize = 128; + else + nsize = lp->linealloc; + while (nsize <= len + 1) + nsize <<= 1; + if ((n = realloc(lp->line, + nsize * sizeof(char))) == NULL) + enomem(); + lp->line = n; + lp->linealloc = nsize; + } + memmove(lp->line, bp, len); + + /* Replace trailing newline, if it exists. */ + if (bp[len - 1] == '\n') + lp->line[len - 1] = '\0'; + else + lp->line[len] = '\0'; + bp = lp->line; + + /* Split the line into fields, allocate space as necessary. */ + lp->fieldcnt = 0; + while ((fieldp = strsep(&bp, tabchar)) != NULL) { + if (spans && *fieldp == '\0') + continue; + if (lp->fieldcnt == lp->fieldalloc) { + char **n; + + if (lp->fieldalloc == 0) + nsize = 16; + else + nsize = lp->fieldalloc << 1; + if ((n = realloc(lp->fields, + nsize * sizeof(char *))) == NULL) + enomem(); + lp->fields = n; + lp->fieldalloc = nsize; + } + lp->fields[lp->fieldcnt++] = fieldp; + } + + /* See if the join field value has changed. */ + if (F->setcnt && cmp(lp, F->joinf, lp - 1, F->joinf)) { + F->pushback = F->setcnt; + break; + } + } +} + +static int +cmp(LINE *lp1, u_long fieldno1, LINE *lp2, u_long fieldno2) +{ + + if (lp1->fieldcnt <= fieldno1) + return (lp2->fieldcnt <= fieldno2 ? 0 : 1); + if (lp2->fieldcnt <= fieldno2) + return (-1); + return (strcmp(lp1->fields[fieldno1], lp2->fields[fieldno2])); +} + +static void +joinlines(INPUT *F1, INPUT *F2) +{ + u_long cnt1, cnt2; + + /* + * 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]); +} + +static void +outoneline(INPUT *F, LINE *lp) +{ + u_long cnt; + + /* + * 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) { + if (olist[cnt].fileno == (u_long)F->number) + outfield(lp, olist[cnt].fieldno); + else + outfield(&noline, 1); + } + else + for (cnt = 0; cnt < lp->fieldcnt; ++cnt) + outfield(lp, cnt); + (void)printf("\n"); + if (ferror(stdout)) + err(1, "stdout"); + needsep = 0; +} + +static void +outtwoline(INPUT *F1, LINE *lp1, INPUT *F2, LINE *lp2) +{ + u_long cnt; + + /* Output a pair of lines according to the join list (if any). */ + if (olist) { + for (cnt = 0; cnt < olistcnt; ++cnt) + if (olist[cnt].fileno == 1) + outfield(lp1, olist[cnt].fieldno); + else /* if (olist[cnt].fileno == 2) */ + outfield(lp2, olist[cnt].fieldno); + } else { + /* + * Output the join field, then the remaining fields from F1 + * and F2. + */ + outfield(lp1, F1->joinf); + for (cnt = 0; cnt < lp1->fieldcnt; ++cnt) + if (F1->joinf != cnt) + outfield(lp1, cnt); + for (cnt = 0; cnt < lp2->fieldcnt; ++cnt) + if (F2->joinf != cnt) + outfield(lp2, cnt); + } + (void)printf("\n"); + if (ferror(stdout)) + err(1, "stdout"); + needsep = 0; +} + +static void +outfield(LINE *lp, u_long fieldno) +{ + if (needsep++) + (void)printf("%c", *tabchar); + if (!ferror(stdout)) { + if (lp->fieldcnt <= fieldno) { + if (empty != NULL) + (void)printf("%s", empty); + } else { + if (*lp->fields[fieldno] == '\0') + return; + (void)printf("%s", lp->fields[fieldno]); + } + } + if (ferror(stdout)) + err(1, "stdout"); +} + +/* + * Convert an output list argument "2.1, 1.3, 2.4" into an array of output + * fields. + */ +static void +fieldarg(char *option) +{ + u_long fieldno; + char *end, *token; + OLIST *n; + + while ((token = strsep(&option, ", \t")) != NULL) { + if (*token == '\0') + continue; + if ((token[0] != '1' && token[0] != '2') || token[1] != '.') + errx(1, "malformed -o option field"); + fieldno = strtol(token + 2, &end, 10); + if (*end) + errx(1, "malformed -o option field"); + if (fieldno == 0) + errx(1, "field numbers are 1 based"); + if (olistcnt == olistalloc) { + if ((n = realloc(olist, + (olistalloc + 50) * sizeof(OLIST))) == NULL) + enomem(); + olist = n; + olistalloc += 50; + } + olist[olistcnt].fileno = token[0] - '0'; + olist[olistcnt].fieldno = fieldno - 1; + ++olistcnt; + } +} + +static void +obsolete(char **argv) +{ + size_t len; + char **p, *ap, *t; + + while ((ap = *++argv) != NULL) { + /* 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: +jbad: errx(1, "illegal option -- %s", ap); + usage(); + } + break; + case 'o': + /* + * The original join allowed "-o arg arg". Convert to + * "-o arg -o arg". + */ + if (ap[2] != '\0') + break; + for (p = argv + 2; *p; ++p) { + if ((p[0][0] != '1' && p[0][0] != '2') || + p[0][1] != '.') + break; + len = strlen(*p); + if (len - 2 != strspn(*p + 2, "0123456789")) + break; + if ((t = malloc(len + 3)) == NULL) + enomem(); + t[0] = '-'; + t[1] = 'o'; + memmove(t + 2, *p, len + 1); + *p = t; + } + argv = p - 1; + break; + } + } +} + +static void +enomem(void) +{ + errx(1, "no memory"); +} + +static void +usage(void) +{ + (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()); + exit(1); +}