1*66fcf23dSnia /* $NetBSD: join.c,v 1.34 2021/11/02 10:05:49 nia Exp $ */
220d49058Stls
3f7d7921aSglass /*-
49bb9a575Stls * Copyright (c) 1991 The Regents of the University of California.
59bb9a575Stls * All rights reserved.
6f7d7921aSglass *
7f7d7921aSglass * This code is derived from software contributed to Berkeley by
8f7d7921aSglass * Steve Hayman of Indiana University, Michiro Hikida and David
9f7d7921aSglass * Goodenough.
10f7d7921aSglass *
11f7d7921aSglass * Redistribution and use in source and binary forms, with or without
12f7d7921aSglass * modification, are permitted provided that the following conditions
13f7d7921aSglass * are met:
14f7d7921aSglass * 1. Redistributions of source code must retain the above copyright
15f7d7921aSglass * notice, this list of conditions and the following disclaimer.
16f7d7921aSglass * 2. Redistributions in binary form must reproduce the above copyright
17f7d7921aSglass * notice, this list of conditions and the following disclaimer in the
18f7d7921aSglass * documentation and/or other materials provided with the distribution.
1989aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
20f7d7921aSglass * may be used to endorse or promote products derived from this software
21f7d7921aSglass * without specific prior written permission.
22f7d7921aSglass *
23f7d7921aSglass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24f7d7921aSglass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f7d7921aSglass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f7d7921aSglass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27f7d7921aSglass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f7d7921aSglass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f7d7921aSglass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f7d7921aSglass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f7d7921aSglass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f7d7921aSglass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f7d7921aSglass * SUCH DAMAGE.
34f7d7921aSglass */
35f7d7921aSglass
36a059c305Sapb #if HAVE_NBTOOL_CONFIG_H
37a059c305Sapb #include "nbtool_config.h"
38a059c305Sapb #endif
39a059c305Sapb
40ac4e9aa7Slukem #include <sys/cdefs.h>
41f7d7921aSglass #ifndef lint
4298e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1991\
4398e5374cSlukem The Regents of the University of California. All rights reserved.");
44f7d7921aSglass #endif /* not lint */
45f7d7921aSglass
46f7d7921aSglass #ifndef lint
47ac4e9aa7Slukem #if 0
48ac4e9aa7Slukem static char sccsid[] = "from: @(#)join.c 5.1 (Berkeley) 11/18/91";
49ac4e9aa7Slukem #else
50*66fcf23dSnia __RCSID("$NetBSD: join.c,v 1.34 2021/11/02 10:05:49 nia Exp $");
51ac4e9aa7Slukem #endif
52f7d7921aSglass #endif /* not lint */
53f7d7921aSglass
54f7d7921aSglass #include <sys/types.h>
55ac4e9aa7Slukem #include <ctype.h>
56ac4e9aa7Slukem #include <err.h>
57ac4e9aa7Slukem #include <errno.h>
58f7d7921aSglass #include <stdio.h>
592ddbb97fSjtc #include <stdlib.h>
60f7d7921aSglass #include <string.h>
61b7110dfaSperry #include <unistd.h>
62f7d7921aSglass
63f7d7921aSglass /*
64f7d7921aSglass * There's a structure per input file which encapsulates the state of the
65f7d7921aSglass * file. We repeatedly read lines from each file until we've read in all
66f7d7921aSglass * the consecutive lines from the file with a common join field. Then we
67f7d7921aSglass * compare the set of lines with an equivalent set from the other file.
68f7d7921aSglass */
69f7d7921aSglass typedef struct {
70f7d7921aSglass char *line; /* line */
71f7d7921aSglass u_long linealloc; /* line allocated count */
72f7d7921aSglass char **fields; /* line field(s) */
73f7d7921aSglass u_long fieldcnt; /* line field(s) count */
74f7d7921aSglass u_long fieldalloc; /* line field(s) allocated count */
75f7d7921aSglass } LINE;
76f7d7921aSglass
77d6555585Sjoerg static char nolineline[1] = { '\0' };
78d6555585Sjoerg static LINE noline = {nolineline, 0, 0, 0, 0}; /* arg for outfield if no line to output */
790fb6a2fdSjonb
80f7d7921aSglass typedef struct {
81f7d7921aSglass FILE *fp; /* file descriptor */
82f7d7921aSglass u_long joinf; /* join field (-1, -2, -j) */
83f7d7921aSglass int unpair; /* output unpairable lines (-a) */
84f7d7921aSglass int number; /* 1 for file 1, 2 for file 2 */
85f7d7921aSglass
86f7d7921aSglass LINE *set; /* set of lines with same field */
87f7d7921aSglass u_long pushback; /* line on the stack */
88f7d7921aSglass u_long setcnt; /* set count */
89f7d7921aSglass u_long setalloc; /* set allocated count */
90f7d7921aSglass } INPUT;
91d6555585Sjoerg
9247b5db6cScheusov static INPUT input1 = { NULL, 0, 0, 1, NULL, (u_long)-1, 0, 0, };
9347b5db6cScheusov static INPUT input2 = { NULL, 0, 0, 2, NULL, (u_long)-1, 0, 0, };
94f7d7921aSglass
95f7d7921aSglass typedef struct {
969bb9a575Stls u_long fileno; /* file number */
97f7d7921aSglass u_long fieldno; /* field number */
98f7d7921aSglass } OLIST;
99f7d7921aSglass
100d6555585Sjoerg static OLIST *olist; /* output field list */
101d6555585Sjoerg static u_long olistcnt; /* output field list count */
102d6555585Sjoerg static u_long olistalloc; /* output field allocated count */
103f7d7921aSglass
104d6555585Sjoerg static int joinout = 1; /* show lines with matched join fields (-v) */
105d6555585Sjoerg static int needsep; /* need separator character */
106d6555585Sjoerg static int spans = 1; /* span multiple delimiters (-t) */
107d6555585Sjoerg static char *empty; /* empty field replacement string (-e) */
108d6555585Sjoerg static const char *tabchar = " \t"; /* delimiter characters (-t) */
109d6555585Sjoerg
110d6555585Sjoerg static int cmp(LINE *, u_long, LINE *, u_long);
111d6555585Sjoerg __dead static void enomem(void);
112d6555585Sjoerg static void fieldarg(char *);
113d6555585Sjoerg static void joinlines(INPUT *, INPUT *);
114d6555585Sjoerg static void obsolete(char **);
115d6555585Sjoerg static void outfield(LINE *, u_long);
116d6555585Sjoerg static void outoneline(INPUT *, LINE *);
117d6555585Sjoerg static void outtwoline(INPUT *, LINE *, INPUT *, LINE *);
118d6555585Sjoerg static void slurp(INPUT *);
119d6555585Sjoerg __dead static void usage(void);
120f7d7921aSglass
121f7d7921aSglass int
main(int argc,char * argv[])12210e955c2Sperry main(int argc, char *argv[])
123f7d7921aSglass {
124ac4e9aa7Slukem INPUT *F1, *F2;
125f7d7921aSglass int aflag, ch, cval, vflag;
126f7d7921aSglass char *end;
127f7d7921aSglass
128f7d7921aSglass F1 = &input1;
129f7d7921aSglass F2 = &input2;
130f7d7921aSglass
131f7d7921aSglass aflag = vflag = 0;
132f7d7921aSglass obsolete(argv);
133ac4e9aa7Slukem while ((ch = getopt(argc, argv, "\01a:e:j:1:2:o:t:v:")) != -1) {
134f7d7921aSglass switch (ch) {
1359bb9a575Stls case '\01':
136f7d7921aSglass aflag = 1;
137f7d7921aSglass F1->unpair = F2->unpair = 1;
138f7d7921aSglass break;
139f7d7921aSglass case '1':
140ac4e9aa7Slukem if ((F1->joinf = strtol(optarg, &end, 10)) < 1) {
141ac4e9aa7Slukem warnx("-1 option field number less than 1");
142ac4e9aa7Slukem usage();
143ac4e9aa7Slukem }
144ac4e9aa7Slukem if (*end) {
145ac4e9aa7Slukem warnx("illegal field number -- %s", optarg);
146ac4e9aa7Slukem usage();
147ac4e9aa7Slukem }
148f7d7921aSglass --F1->joinf;
149f7d7921aSglass break;
150f7d7921aSglass case '2':
151ac4e9aa7Slukem if ((F2->joinf = strtol(optarg, &end, 10)) < 1) {
152ac4e9aa7Slukem warnx("-2 option field number less than 1");
153ac4e9aa7Slukem usage();
154ac4e9aa7Slukem }
155ac4e9aa7Slukem if (*end) {
156ac4e9aa7Slukem warnx("illegal field number -- %s", optarg);
157ac4e9aa7Slukem usage();
158ac4e9aa7Slukem }
159f7d7921aSglass --F2->joinf;
160f7d7921aSglass break;
161f7d7921aSglass case 'a':
162f7d7921aSglass aflag = 1;
163f7d7921aSglass switch(strtol(optarg, &end, 10)) {
164f7d7921aSglass case 1:
165f7d7921aSglass F1->unpair = 1;
166f7d7921aSglass break;
167f7d7921aSglass case 2:
168f7d7921aSglass F2->unpair = 1;
169f7d7921aSglass break;
170f7d7921aSglass default:
171ac4e9aa7Slukem warnx("-a option file number not 1 or 2");
172ac4e9aa7Slukem usage();
173f7d7921aSglass break;
174f7d7921aSglass }
175ac4e9aa7Slukem if (*end) {
176ac4e9aa7Slukem warnx("illegal file number -- %s", optarg);
177ac4e9aa7Slukem usage();
178ac4e9aa7Slukem }
179f7d7921aSglass break;
180f7d7921aSglass case 'e':
181f7d7921aSglass empty = optarg;
182f7d7921aSglass break;
183f7d7921aSglass case 'j':
184f7d7921aSglass if ((F1->joinf = F2->joinf =
185ac4e9aa7Slukem strtol(optarg, &end, 10)) < 1) {
186ac4e9aa7Slukem warnx("-j option field number less than 1");
187ac4e9aa7Slukem usage();
188ac4e9aa7Slukem }
189ac4e9aa7Slukem if (*end) {
190ac4e9aa7Slukem warnx("illegal field number -- %s", optarg);
191ac4e9aa7Slukem usage();
192ac4e9aa7Slukem }
193f7d7921aSglass --F1->joinf;
194f7d7921aSglass --F2->joinf;
195f7d7921aSglass break;
196f7d7921aSglass case 'o':
197f7d7921aSglass fieldarg(optarg);
198f7d7921aSglass break;
199f7d7921aSglass case 't':
200f7d7921aSglass spans = 0;
201ac4e9aa7Slukem if (strlen(tabchar = optarg) != 1) {
202ac4e9aa7Slukem warnx("illegal tab character specification");
203ac4e9aa7Slukem usage();
204ac4e9aa7Slukem }
205f7d7921aSglass break;
206f7d7921aSglass case 'v':
207f7d7921aSglass vflag = 1;
208f7d7921aSglass joinout = 0;
209f7d7921aSglass switch(strtol(optarg, &end, 10)) {
210f7d7921aSglass case 1:
211f7d7921aSglass F1->unpair = 1;
212f7d7921aSglass break;
213f7d7921aSglass case 2:
214f7d7921aSglass F2->unpair = 1;
215f7d7921aSglass break;
216f7d7921aSglass default:
217ac4e9aa7Slukem warnx("-v option file number not 1 or 2");
218ac4e9aa7Slukem usage();
219f7d7921aSglass break;
220f7d7921aSglass }
221ac4e9aa7Slukem if (*end) {
222ac4e9aa7Slukem warnx("illegal file number -- %s", optarg);
223ac4e9aa7Slukem usage();
224ac4e9aa7Slukem }
225f7d7921aSglass break;
226f7d7921aSglass case '?':
227f7d7921aSglass default:
228f7d7921aSglass usage();
229f7d7921aSglass }
230f7d7921aSglass }
231f7d7921aSglass argc -= optind;
232f7d7921aSglass argv += optind;
233f7d7921aSglass
234f7d7921aSglass if (aflag && vflag)
235ac4e9aa7Slukem errx(1, "-a and -v options mutually exclusive");
236f7d7921aSglass
237f7d7921aSglass if (argc != 2)
238f7d7921aSglass usage();
239f7d7921aSglass
240f7d7921aSglass /* Open the files; "-" means stdin. */
241f7d7921aSglass if (!strcmp(*argv, "-"))
242f7d7921aSglass F1->fp = stdin;
243f7d7921aSglass else if ((F1->fp = fopen(*argv, "r")) == NULL)
244ac4e9aa7Slukem err(1, "%s", *argv);
245f7d7921aSglass ++argv;
246f7d7921aSglass if (!strcmp(*argv, "-"))
247f7d7921aSglass F2->fp = stdin;
248f7d7921aSglass else if ((F2->fp = fopen(*argv, "r")) == NULL)
249ac4e9aa7Slukem err(1, "%s", *argv);
250f7d7921aSglass if (F1->fp == stdin && F2->fp == stdin)
251ac4e9aa7Slukem errx(1, "only one input file may be stdin");
252f7d7921aSglass
253f7d7921aSglass slurp(F1);
254f7d7921aSglass slurp(F2);
255f7d7921aSglass while (F1->setcnt && F2->setcnt) {
256f7d7921aSglass cval = cmp(F1->set, F1->joinf, F2->set, F2->joinf);
257f7d7921aSglass if (cval == 0) {
258f7d7921aSglass /* Oh joy, oh rapture, oh beauty divine! */
259f7d7921aSglass if (joinout)
260f7d7921aSglass joinlines(F1, F2);
261f7d7921aSglass slurp(F1);
262f7d7921aSglass slurp(F2);
263f7d7921aSglass } else if (cval < 0) {
264f7d7921aSglass /* File 1 takes the lead... */
265f7d7921aSglass if (F1->unpair)
266f7d7921aSglass joinlines(F1, NULL);
267f7d7921aSglass slurp(F1);
268f7d7921aSglass } else {
269f7d7921aSglass /* File 2 takes the lead... */
270f7d7921aSglass if (F2->unpair)
271f7d7921aSglass joinlines(F2, NULL);
272f7d7921aSglass slurp(F2);
273f7d7921aSglass }
274f7d7921aSglass }
275f7d7921aSglass
276f7d7921aSglass /*
277f7d7921aSglass * Now that one of the files is used up, optionally output any
278f7d7921aSglass * remaining lines from the other file.
279f7d7921aSglass */
280f7d7921aSglass if (F1->unpair)
281f7d7921aSglass while (F1->setcnt) {
282f7d7921aSglass joinlines(F1, NULL);
283f7d7921aSglass slurp(F1);
284f7d7921aSglass }
285e730edb6Shubertf if (F1->fp != stdin)
286e730edb6Shubertf fclose(F1->fp);
287e730edb6Shubertf
288f7d7921aSglass if (F2->unpair)
289f7d7921aSglass while (F2->setcnt) {
290f7d7921aSglass joinlines(F2, NULL);
291f7d7921aSglass slurp(F2);
292f7d7921aSglass }
293e730edb6Shubertf if (F2->fp != stdin)
294e730edb6Shubertf fclose(F2->fp);
295e730edb6Shubertf
296e730edb6Shubertf return 0;
297f7d7921aSglass }
298f7d7921aSglass
299d6555585Sjoerg static void
slurp(INPUT * F)30010e955c2Sperry slurp(INPUT *F)
301f7d7921aSglass {
3025c886053Smycroft LINE *lp;
3039bb9a575Stls LINE tmp;
304f7d7921aSglass size_t len;
3051afaa212Slukem u_long cnt;
306a60129f5Scgd char *bp, *fieldp;
30750847da5Sitojun u_long nsize;
308f7d7921aSglass
309f7d7921aSglass /*
310f7d7921aSglass * Read all of the lines from an input file that have the same
311f7d7921aSglass * join field.
312f7d7921aSglass */
3135c886053Smycroft for (F->setcnt = 0;; ++F->setcnt) {
314f7d7921aSglass /*
315f7d7921aSglass * If we're out of space to hold line structures, allocate
316f7d7921aSglass * more. Initialize the structure so that we know that this
317f7d7921aSglass * is new space.
318f7d7921aSglass */
319f7d7921aSglass if (F->setcnt == F->setalloc) {
320f7d7921aSglass cnt = F->setalloc;
3215c886053Smycroft if (F->setalloc == 0)
32250847da5Sitojun nsize = 64;
3235c886053Smycroft else
32450847da5Sitojun nsize = F->setalloc << 1;
325*66fcf23dSnia if (reallocarr(&F->set, nsize, sizeof(LINE)) != 0)
3269bb9a575Stls enomem();
32750847da5Sitojun F->setalloc = nsize;
3285c886053Smycroft memset(F->set + cnt, 0,
3295c886053Smycroft (F->setalloc - cnt) * sizeof(LINE));
330f7d7921aSglass }
331f7d7921aSglass
332f7d7921aSglass /*
333f7d7921aSglass * Get any pushed back line, else get the next line. Allocate
334f7d7921aSglass * space as necessary. If taking the line from the stack swap
3359bb9a575Stls * the two structures so that we don't lose the allocated space.
3369bb9a575Stls * This could be avoided by doing another level of indirection,
3379bb9a575Stls * but it's probably okay as is.
338f7d7921aSglass */
339f7d7921aSglass lp = &F->set[F->setcnt];
3401afaa212Slukem if (F->pushback != (u_long)-1) {
341f7d7921aSglass tmp = F->set[F->setcnt];
342f7d7921aSglass F->set[F->setcnt] = F->set[F->pushback];
343f7d7921aSglass F->set[F->pushback] = tmp;
3441afaa212Slukem F->pushback = (u_long)-1;
345f7d7921aSglass continue;
346f7d7921aSglass }
34702254e0cScgd if ((bp = fgetln(F->fp, &len)) == NULL)
348f7d7921aSglass return;
349a60129f5Scgd if (lp->linealloc <= len + 1) {
35050847da5Sitojun char *n;
35150847da5Sitojun
352cf60e8dfScgd if (lp->linealloc == 0)
35350847da5Sitojun nsize = 128;
35450847da5Sitojun else
35550847da5Sitojun nsize = lp->linealloc;
35650847da5Sitojun while (nsize <= len + 1)
35750847da5Sitojun nsize <<= 1;
35850847da5Sitojun if ((n = realloc(lp->line,
35950847da5Sitojun nsize * sizeof(char))) == NULL)
3609bb9a575Stls enomem();
36150847da5Sitojun lp->line = n;
36250847da5Sitojun lp->linealloc = nsize;
363f7d7921aSglass }
364c8b0d049Stron memmove(lp->line, bp, len);
365f7d7921aSglass
366a60129f5Scgd /* Replace trailing newline, if it exists. */
367a60129f5Scgd if (bp[len - 1] == '\n')
368a60129f5Scgd lp->line[len - 1] = '\0';
369a60129f5Scgd else
370a60129f5Scgd lp->line[len] = '\0';
371a60129f5Scgd bp = lp->line;
372a60129f5Scgd
373f7d7921aSglass /* Split the line into fields, allocate space as necessary. */
374f7d7921aSglass lp->fieldcnt = 0;
375a60129f5Scgd while ((fieldp = strsep(&bp, tabchar)) != NULL) {
376f7d7921aSglass if (spans && *fieldp == '\0')
377f7d7921aSglass continue;
378f7d7921aSglass if (lp->fieldcnt == lp->fieldalloc) {
3795c886053Smycroft if (lp->fieldalloc == 0)
38050847da5Sitojun nsize = 16;
3815c886053Smycroft else
38250847da5Sitojun nsize = lp->fieldalloc << 1;
383*66fcf23dSnia if (reallocarr(&lp->fields,
384*66fcf23dSnia nsize, sizeof(char *)) != 0)
3859bb9a575Stls enomem();
38650847da5Sitojun lp->fieldalloc = nsize;
387f7d7921aSglass }
388f7d7921aSglass lp->fields[lp->fieldcnt++] = fieldp;
389f7d7921aSglass }
390f7d7921aSglass
391f7d7921aSglass /* See if the join field value has changed. */
3925c886053Smycroft if (F->setcnt && cmp(lp, F->joinf, lp - 1, F->joinf)) {
393f7d7921aSglass F->pushback = F->setcnt;
394f7d7921aSglass break;
395f7d7921aSglass }
396f7d7921aSglass }
397f7d7921aSglass }
398f7d7921aSglass
399d6555585Sjoerg static int
cmp(LINE * lp1,u_long fieldno1,LINE * lp2,u_long fieldno2)40010e955c2Sperry cmp(LINE *lp1, u_long fieldno1, LINE *lp2, u_long fieldno2)
401f7d7921aSglass {
4021a97a356Smycroft
4031a97a356Smycroft if (lp1->fieldcnt <= fieldno1)
404a02b2cb6Smycroft return (lp2->fieldcnt <= fieldno2 ? 0 : 1);
4051a97a356Smycroft if (lp2->fieldcnt <= fieldno2)
406f7d7921aSglass return (-1);
407f7d7921aSglass return (strcmp(lp1->fields[fieldno1], lp2->fields[fieldno2]));
408f7d7921aSglass }
409f7d7921aSglass
410d6555585Sjoerg static void
joinlines(INPUT * F1,INPUT * F2)41110e955c2Sperry joinlines(INPUT *F1, INPUT *F2)
412f7d7921aSglass {
4131afaa212Slukem u_long cnt1, cnt2;
414f7d7921aSglass
415f7d7921aSglass /*
416f7d7921aSglass * Output the results of a join comparison. The output may be from
417f7d7921aSglass * either file 1 or file 2 (in which case the first argument is the
418f7d7921aSglass * file from which to output) or from both.
419f7d7921aSglass */
420f7d7921aSglass if (F2 == NULL) {
421f7d7921aSglass for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
422f7d7921aSglass outoneline(F1, &F1->set[cnt1]);
423f7d7921aSglass return;
424f7d7921aSglass }
425f7d7921aSglass for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
426f7d7921aSglass for (cnt2 = 0; cnt2 < F2->setcnt; ++cnt2)
427f7d7921aSglass outtwoline(F1, &F1->set[cnt1], F2, &F2->set[cnt2]);
428f7d7921aSglass }
429f7d7921aSglass
430d6555585Sjoerg static void
outoneline(INPUT * F,LINE * lp)43110e955c2Sperry outoneline(INPUT *F, LINE *lp)
432f7d7921aSglass {
4331afaa212Slukem u_long cnt;
434f7d7921aSglass
435f7d7921aSglass /*
436f7d7921aSglass * Output a single line from one of the files, according to the
437f7d7921aSglass * join rules. This happens when we are writing unmatched single
438f7d7921aSglass * lines. Output empty fields in the right places.
439f7d7921aSglass */
440f7d7921aSglass if (olist)
441f7d7921aSglass for (cnt = 0; cnt < olistcnt; ++cnt) {
4421afaa212Slukem if (olist[cnt].fileno == (u_long)F->number)
4439bb9a575Stls outfield(lp, olist[cnt].fieldno);
4440fb6a2fdSjonb else
4450fb6a2fdSjonb outfield(&noline, 1);
446f7d7921aSglass }
447f7d7921aSglass else
448f7d7921aSglass for (cnt = 0; cnt < lp->fieldcnt; ++cnt)
4499bb9a575Stls outfield(lp, cnt);
450f7d7921aSglass (void)printf("\n");
451f7d7921aSglass if (ferror(stdout))
452ac4e9aa7Slukem err(1, "stdout");
453f7d7921aSglass needsep = 0;
454f7d7921aSglass }
455f7d7921aSglass
456d6555585Sjoerg static void
outtwoline(INPUT * F1,LINE * lp1,INPUT * F2,LINE * lp2)45710e955c2Sperry outtwoline(INPUT *F1, LINE *lp1, INPUT *F2, LINE *lp2)
458f7d7921aSglass {
4591afaa212Slukem u_long cnt;
460f7d7921aSglass
461f7d7921aSglass /* Output a pair of lines according to the join list (if any). */
4620d9bacd5Schristos if (olist) {
463f7d7921aSglass for (cnt = 0; cnt < olistcnt; ++cnt)
4649bb9a575Stls if (olist[cnt].fileno == 1)
4659bb9a575Stls outfield(lp1, olist[cnt].fieldno);
4669bb9a575Stls else /* if (olist[cnt].fileno == 2) */
4679bb9a575Stls outfield(lp2, olist[cnt].fieldno);
4680d9bacd5Schristos } else {
469f7d7921aSglass /*
470f7d7921aSglass * Output the join field, then the remaining fields from F1
471f7d7921aSglass * and F2.
472f7d7921aSglass */
4739bb9a575Stls outfield(lp1, F1->joinf);
474f7d7921aSglass for (cnt = 0; cnt < lp1->fieldcnt; ++cnt)
475f7d7921aSglass if (F1->joinf != cnt)
4769bb9a575Stls outfield(lp1, cnt);
477f7d7921aSglass for (cnt = 0; cnt < lp2->fieldcnt; ++cnt)
478f7d7921aSglass if (F2->joinf != cnt)
4799bb9a575Stls outfield(lp2, cnt);
480f7d7921aSglass }
481f7d7921aSglass (void)printf("\n");
482f7d7921aSglass if (ferror(stdout))
483ac4e9aa7Slukem err(1, "stdout");
484f7d7921aSglass needsep = 0;
485f7d7921aSglass }
486f7d7921aSglass
487d6555585Sjoerg static void
outfield(LINE * lp,u_long fieldno)48810e955c2Sperry outfield(LINE *lp, u_long fieldno)
489f7d7921aSglass {
490f7d7921aSglass if (needsep++)
491f7d7921aSglass (void)printf("%c", *tabchar);
492f670fa10Sross if (!ferror(stdout)) {
493f11e179eSmycroft if (lp->fieldcnt <= fieldno) {
494f7d7921aSglass if (empty != NULL)
495f7d7921aSglass (void)printf("%s", empty);
496f7d7921aSglass } else {
497f7d7921aSglass if (*lp->fields[fieldno] == '\0')
498f7d7921aSglass return;
499f7d7921aSglass (void)printf("%s", lp->fields[fieldno]);
500f7d7921aSglass }
501f670fa10Sross }
502f7d7921aSglass if (ferror(stdout))
503ac4e9aa7Slukem err(1, "stdout");
504f7d7921aSglass }
505f7d7921aSglass
506f7d7921aSglass /*
507f7d7921aSglass * Convert an output list argument "2.1, 1.3, 2.4" into an array of output
508f7d7921aSglass * fields.
509f7d7921aSglass */
510d6555585Sjoerg static void
fieldarg(char * option)51110e955c2Sperry fieldarg(char *option)
512f7d7921aSglass {
513f7d7921aSglass u_long fieldno;
514f7d7921aSglass char *end, *token;
515f7d7921aSglass
516a02b2cb6Smycroft while ((token = strsep(&option, ", \t")) != NULL) {
517f7d7921aSglass if (*token == '\0')
518f7d7921aSglass continue;
519ac4e9aa7Slukem if ((token[0] != '1' && token[0] != '2') || token[1] != '.')
520ac4e9aa7Slukem errx(1, "malformed -o option field");
521f7d7921aSglass fieldno = strtol(token + 2, &end, 10);
522f7d7921aSglass if (*end)
523ac4e9aa7Slukem errx(1, "malformed -o option field");
524f7d7921aSglass if (fieldno == 0)
525ac4e9aa7Slukem errx(1, "field numbers are 1 based");
526f7d7921aSglass if (olistcnt == olistalloc) {
527*66fcf23dSnia if (reallocarr(&olist,
528*66fcf23dSnia olistalloc + 50, sizeof(OLIST)) != 0)
5299bb9a575Stls enomem();
53050847da5Sitojun olistalloc += 50;
531f7d7921aSglass }
5329bb9a575Stls olist[olistcnt].fileno = token[0] - '0';
533f7d7921aSglass olist[olistcnt].fieldno = fieldno - 1;
534f7d7921aSglass ++olistcnt;
535f7d7921aSglass }
536f7d7921aSglass }
537f7d7921aSglass
538d6555585Sjoerg static void
obsolete(char ** argv)53910e955c2Sperry obsolete(char **argv)
540f7d7921aSglass {
5411afaa212Slukem size_t len;
542f7d7921aSglass char **p, *ap, *t;
543f7d7921aSglass
544ac4e9aa7Slukem while ((ap = *++argv) != NULL) {
545f7d7921aSglass /* Return if "--". */
546f7d7921aSglass if (ap[0] == '-' && ap[1] == '-')
547f7d7921aSglass return;
548f7d7921aSglass switch (ap[1]) {
549f7d7921aSglass case 'a':
550f7d7921aSglass /*
551f7d7921aSglass * The original join allowed "-a", which meant the
552f7d7921aSglass * same as -a1 plus -a2. POSIX 1003.2, Draft 11.2
553f7d7921aSglass * only specifies this as "-a 1" and "a -2", so we
554f7d7921aSglass * have to use another option flag, one that is
555f7d7921aSglass * unlikely to ever be used or accidentally entered
556f7d7921aSglass * on the command line. (Well, we could reallocate
557f7d7921aSglass * the argv array, but that hardly seems worthwhile.)
558f7d7921aSglass */
559f7d7921aSglass if (ap[2] == '\0')
560f7d7921aSglass ap[1] = '\01';
561f7d7921aSglass break;
562f7d7921aSglass case 'j':
563f7d7921aSglass /*
564f7d7921aSglass * The original join allowed "-j[12] arg" and "-j arg".
565f7d7921aSglass * Convert the former to "-[12] arg". Don't convert
566f7d7921aSglass * the latter since getopt(3) can handle it.
567f7d7921aSglass */
568f7d7921aSglass switch(ap[2]) {
569f7d7921aSglass case '1':
570f7d7921aSglass if (ap[3] != '\0')
571f7d7921aSglass goto jbad;
572f7d7921aSglass ap[1] = '1';
573f7d7921aSglass ap[2] = '\0';
574f7d7921aSglass break;
575f7d7921aSglass case '2':
576f7d7921aSglass if (ap[3] != '\0')
577f7d7921aSglass goto jbad;
578f7d7921aSglass ap[1] = '2';
579f7d7921aSglass ap[2] = '\0';
580f7d7921aSglass break;
581f7d7921aSglass case '\0':
582f7d7921aSglass break;
583f7d7921aSglass default:
58437e902b2Scheusov jbad: warnx("illegal option -- %s", ap);
585f7d7921aSglass usage();
58637e902b2Scheusov exit(1);
587f7d7921aSglass }
588f7d7921aSglass break;
589f7d7921aSglass case 'o':
590f7d7921aSglass /*
5919bb9a575Stls * The original join allowed "-o arg arg". Convert to
5929bb9a575Stls * "-o arg -o arg".
593f7d7921aSglass */
594f7d7921aSglass if (ap[2] != '\0')
595f7d7921aSglass break;
596f7d7921aSglass for (p = argv + 2; *p; ++p) {
597ac4e9aa7Slukem if ((p[0][0] != '1' && p[0][0] != '2') ||
5989bb9a575Stls p[0][1] != '.')
599f7d7921aSglass break;
600f7d7921aSglass len = strlen(*p);
601f7d7921aSglass if (len - 2 != strspn(*p + 2, "0123456789"))
602f7d7921aSglass break;
603f7d7921aSglass if ((t = malloc(len + 3)) == NULL)
6049bb9a575Stls enomem();
605f7d7921aSglass t[0] = '-';
606f7d7921aSglass t[1] = 'o';
607ac4e9aa7Slukem memmove(t + 2, *p, len + 1);
608f7d7921aSglass *p = t;
609f7d7921aSglass }
610f7d7921aSglass argv = p - 1;
611f7d7921aSglass break;
612f7d7921aSglass }
613f7d7921aSglass }
614f7d7921aSglass }
615f7d7921aSglass
616d6555585Sjoerg static void
enomem(void)61710e955c2Sperry enomem(void)
6189bb9a575Stls {
619ac4e9aa7Slukem errx(1, "no memory");
6209bb9a575Stls }
6219bb9a575Stls
622d6555585Sjoerg static void
usage(void)62310e955c2Sperry usage(void)
624f7d7921aSglass {
6254e4e9927Swiz (void)fprintf(stderr,
6264e4e9927Swiz "usage: %s [-a fileno | -v fileno] [-e string] [-j fileno field]\n"
6274e4e9927Swiz " [-o list] [-t char] [-1 field] [-2 field] file1 file2\n",
6284e4e9927Swiz getprogname());
629f7d7921aSglass exit(1);
630f7d7921aSglass }
631