1 /* $NetBSD: join.c,v 1.34 2021/11/02 10:05:49 nia Exp $ */
2
3 /*-
4 * Copyright (c) 1991 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Steve Hayman of Indiana University, Michiro Hikida and David
9 * Goodenough.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
38 #endif
39
40 #include <sys/cdefs.h>
41 #ifndef lint
42 __COPYRIGHT("@(#) Copyright (c) 1991\
43 The Regents of the University of California. All rights reserved.");
44 #endif /* not lint */
45
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "from: @(#)join.c 5.1 (Berkeley) 11/18/91";
49 #else
50 __RCSID("$NetBSD: join.c,v 1.34 2021/11/02 10:05:49 nia Exp $");
51 #endif
52 #endif /* not lint */
53
54 #include <sys/types.h>
55 #include <ctype.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 /*
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.
68 */
69 typedef struct {
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 */
75 } LINE;
76
77 static char nolineline[1] = { '\0' };
78 static LINE noline = {nolineline, 0, 0, 0, 0}; /* arg for outfield if no line to output */
79
80 typedef struct {
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 */
85
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 */
90 } INPUT;
91
92 static INPUT input1 = { NULL, 0, 0, 1, NULL, (u_long)-1, 0, 0, };
93 static INPUT input2 = { NULL, 0, 0, 2, NULL, (u_long)-1, 0, 0, };
94
95 typedef struct {
96 u_long fileno; /* file number */
97 u_long fieldno; /* field number */
98 } OLIST;
99
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 */
103
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) */
109
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);
120
121 int
main(int argc,char * argv[])122 main(int argc, char *argv[])
123 {
124 INPUT *F1, *F2;
125 int aflag, ch, cval, vflag;
126 char *end;
127
128 F1 = &input1;
129 F2 = &input2;
130
131 aflag = vflag = 0;
132 obsolete(argv);
133 while ((ch = getopt(argc, argv, "\01a:e:j:1:2:o:t:v:")) != -1) {
134 switch (ch) {
135 case '\01':
136 aflag = 1;
137 F1->unpair = F2->unpair = 1;
138 break;
139 case '1':
140 if ((F1->joinf = strtol(optarg, &end, 10)) < 1) {
141 warnx("-1 option field number less than 1");
142 usage();
143 }
144 if (*end) {
145 warnx("illegal field number -- %s", optarg);
146 usage();
147 }
148 --F1->joinf;
149 break;
150 case '2':
151 if ((F2->joinf = strtol(optarg, &end, 10)) < 1) {
152 warnx("-2 option field number less than 1");
153 usage();
154 }
155 if (*end) {
156 warnx("illegal field number -- %s", optarg);
157 usage();
158 }
159 --F2->joinf;
160 break;
161 case 'a':
162 aflag = 1;
163 switch(strtol(optarg, &end, 10)) {
164 case 1:
165 F1->unpair = 1;
166 break;
167 case 2:
168 F2->unpair = 1;
169 break;
170 default:
171 warnx("-a option file number not 1 or 2");
172 usage();
173 break;
174 }
175 if (*end) {
176 warnx("illegal file number -- %s", optarg);
177 usage();
178 }
179 break;
180 case 'e':
181 empty = optarg;
182 break;
183 case 'j':
184 if ((F1->joinf = F2->joinf =
185 strtol(optarg, &end, 10)) < 1) {
186 warnx("-j option field number less than 1");
187 usage();
188 }
189 if (*end) {
190 warnx("illegal field number -- %s", optarg);
191 usage();
192 }
193 --F1->joinf;
194 --F2->joinf;
195 break;
196 case 'o':
197 fieldarg(optarg);
198 break;
199 case 't':
200 spans = 0;
201 if (strlen(tabchar = optarg) != 1) {
202 warnx("illegal tab character specification");
203 usage();
204 }
205 break;
206 case 'v':
207 vflag = 1;
208 joinout = 0;
209 switch(strtol(optarg, &end, 10)) {
210 case 1:
211 F1->unpair = 1;
212 break;
213 case 2:
214 F2->unpair = 1;
215 break;
216 default:
217 warnx("-v option file number not 1 or 2");
218 usage();
219 break;
220 }
221 if (*end) {
222 warnx("illegal file number -- %s", optarg);
223 usage();
224 }
225 break;
226 case '?':
227 default:
228 usage();
229 }
230 }
231 argc -= optind;
232 argv += optind;
233
234 if (aflag && vflag)
235 errx(1, "-a and -v options mutually exclusive");
236
237 if (argc != 2)
238 usage();
239
240 /* Open the files; "-" means stdin. */
241 if (!strcmp(*argv, "-"))
242 F1->fp = stdin;
243 else if ((F1->fp = fopen(*argv, "r")) == NULL)
244 err(1, "%s", *argv);
245 ++argv;
246 if (!strcmp(*argv, "-"))
247 F2->fp = stdin;
248 else if ((F2->fp = fopen(*argv, "r")) == NULL)
249 err(1, "%s", *argv);
250 if (F1->fp == stdin && F2->fp == stdin)
251 errx(1, "only one input file may be stdin");
252
253 slurp(F1);
254 slurp(F2);
255 while (F1->setcnt && F2->setcnt) {
256 cval = cmp(F1->set, F1->joinf, F2->set, F2->joinf);
257 if (cval == 0) {
258 /* Oh joy, oh rapture, oh beauty divine! */
259 if (joinout)
260 joinlines(F1, F2);
261 slurp(F1);
262 slurp(F2);
263 } else if (cval < 0) {
264 /* File 1 takes the lead... */
265 if (F1->unpair)
266 joinlines(F1, NULL);
267 slurp(F1);
268 } else {
269 /* File 2 takes the lead... */
270 if (F2->unpair)
271 joinlines(F2, NULL);
272 slurp(F2);
273 }
274 }
275
276 /*
277 * Now that one of the files is used up, optionally output any
278 * remaining lines from the other file.
279 */
280 if (F1->unpair)
281 while (F1->setcnt) {
282 joinlines(F1, NULL);
283 slurp(F1);
284 }
285 if (F1->fp != stdin)
286 fclose(F1->fp);
287
288 if (F2->unpair)
289 while (F2->setcnt) {
290 joinlines(F2, NULL);
291 slurp(F2);
292 }
293 if (F2->fp != stdin)
294 fclose(F2->fp);
295
296 return 0;
297 }
298
299 static void
slurp(INPUT * F)300 slurp(INPUT *F)
301 {
302 LINE *lp;
303 LINE tmp;
304 size_t len;
305 u_long cnt;
306 char *bp, *fieldp;
307 u_long nsize;
308
309 /*
310 * Read all of the lines from an input file that have the same
311 * join field.
312 */
313 for (F->setcnt = 0;; ++F->setcnt) {
314 /*
315 * If we're out of space to hold line structures, allocate
316 * more. Initialize the structure so that we know that this
317 * is new space.
318 */
319 if (F->setcnt == F->setalloc) {
320 cnt = F->setalloc;
321 if (F->setalloc == 0)
322 nsize = 64;
323 else
324 nsize = F->setalloc << 1;
325 if (reallocarr(&F->set, nsize, sizeof(LINE)) != 0)
326 enomem();
327 F->setalloc = nsize;
328 memset(F->set + cnt, 0,
329 (F->setalloc - cnt) * sizeof(LINE));
330 }
331
332 /*
333 * Get any pushed back line, else get the next line. Allocate
334 * space as necessary. If taking the line from the stack swap
335 * the two structures so that we don't lose the allocated space.
336 * This could be avoided by doing another level of indirection,
337 * but it's probably okay as is.
338 */
339 lp = &F->set[F->setcnt];
340 if (F->pushback != (u_long)-1) {
341 tmp = F->set[F->setcnt];
342 F->set[F->setcnt] = F->set[F->pushback];
343 F->set[F->pushback] = tmp;
344 F->pushback = (u_long)-1;
345 continue;
346 }
347 if ((bp = fgetln(F->fp, &len)) == NULL)
348 return;
349 if (lp->linealloc <= len + 1) {
350 char *n;
351
352 if (lp->linealloc == 0)
353 nsize = 128;
354 else
355 nsize = lp->linealloc;
356 while (nsize <= len + 1)
357 nsize <<= 1;
358 if ((n = realloc(lp->line,
359 nsize * sizeof(char))) == NULL)
360 enomem();
361 lp->line = n;
362 lp->linealloc = nsize;
363 }
364 memmove(lp->line, bp, len);
365
366 /* Replace trailing newline, if it exists. */
367 if (bp[len - 1] == '\n')
368 lp->line[len - 1] = '\0';
369 else
370 lp->line[len] = '\0';
371 bp = lp->line;
372
373 /* Split the line into fields, allocate space as necessary. */
374 lp->fieldcnt = 0;
375 while ((fieldp = strsep(&bp, tabchar)) != NULL) {
376 if (spans && *fieldp == '\0')
377 continue;
378 if (lp->fieldcnt == lp->fieldalloc) {
379 if (lp->fieldalloc == 0)
380 nsize = 16;
381 else
382 nsize = lp->fieldalloc << 1;
383 if (reallocarr(&lp->fields,
384 nsize, sizeof(char *)) != 0)
385 enomem();
386 lp->fieldalloc = nsize;
387 }
388 lp->fields[lp->fieldcnt++] = fieldp;
389 }
390
391 /* See if the join field value has changed. */
392 if (F->setcnt && cmp(lp, F->joinf, lp - 1, F->joinf)) {
393 F->pushback = F->setcnt;
394 break;
395 }
396 }
397 }
398
399 static int
cmp(LINE * lp1,u_long fieldno1,LINE * lp2,u_long fieldno2)400 cmp(LINE *lp1, u_long fieldno1, LINE *lp2, u_long fieldno2)
401 {
402
403 if (lp1->fieldcnt <= fieldno1)
404 return (lp2->fieldcnt <= fieldno2 ? 0 : 1);
405 if (lp2->fieldcnt <= fieldno2)
406 return (-1);
407 return (strcmp(lp1->fields[fieldno1], lp2->fields[fieldno2]));
408 }
409
410 static void
joinlines(INPUT * F1,INPUT * F2)411 joinlines(INPUT *F1, INPUT *F2)
412 {
413 u_long cnt1, cnt2;
414
415 /*
416 * Output the results of a join comparison. The output may be from
417 * either file 1 or file 2 (in which case the first argument is the
418 * file from which to output) or from both.
419 */
420 if (F2 == NULL) {
421 for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
422 outoneline(F1, &F1->set[cnt1]);
423 return;
424 }
425 for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
426 for (cnt2 = 0; cnt2 < F2->setcnt; ++cnt2)
427 outtwoline(F1, &F1->set[cnt1], F2, &F2->set[cnt2]);
428 }
429
430 static void
outoneline(INPUT * F,LINE * lp)431 outoneline(INPUT *F, LINE *lp)
432 {
433 u_long cnt;
434
435 /*
436 * Output a single line from one of the files, according to the
437 * join rules. This happens when we are writing unmatched single
438 * lines. Output empty fields in the right places.
439 */
440 if (olist)
441 for (cnt = 0; cnt < olistcnt; ++cnt) {
442 if (olist[cnt].fileno == (u_long)F->number)
443 outfield(lp, olist[cnt].fieldno);
444 else
445 outfield(&noline, 1);
446 }
447 else
448 for (cnt = 0; cnt < lp->fieldcnt; ++cnt)
449 outfield(lp, cnt);
450 (void)printf("\n");
451 if (ferror(stdout))
452 err(1, "stdout");
453 needsep = 0;
454 }
455
456 static void
outtwoline(INPUT * F1,LINE * lp1,INPUT * F2,LINE * lp2)457 outtwoline(INPUT *F1, LINE *lp1, INPUT *F2, LINE *lp2)
458 {
459 u_long cnt;
460
461 /* Output a pair of lines according to the join list (if any). */
462 if (olist) {
463 for (cnt = 0; cnt < olistcnt; ++cnt)
464 if (olist[cnt].fileno == 1)
465 outfield(lp1, olist[cnt].fieldno);
466 else /* if (olist[cnt].fileno == 2) */
467 outfield(lp2, olist[cnt].fieldno);
468 } else {
469 /*
470 * Output the join field, then the remaining fields from F1
471 * and F2.
472 */
473 outfield(lp1, F1->joinf);
474 for (cnt = 0; cnt < lp1->fieldcnt; ++cnt)
475 if (F1->joinf != cnt)
476 outfield(lp1, cnt);
477 for (cnt = 0; cnt < lp2->fieldcnt; ++cnt)
478 if (F2->joinf != cnt)
479 outfield(lp2, cnt);
480 }
481 (void)printf("\n");
482 if (ferror(stdout))
483 err(1, "stdout");
484 needsep = 0;
485 }
486
487 static void
outfield(LINE * lp,u_long fieldno)488 outfield(LINE *lp, u_long fieldno)
489 {
490 if (needsep++)
491 (void)printf("%c", *tabchar);
492 if (!ferror(stdout)) {
493 if (lp->fieldcnt <= fieldno) {
494 if (empty != NULL)
495 (void)printf("%s", empty);
496 } else {
497 if (*lp->fields[fieldno] == '\0')
498 return;
499 (void)printf("%s", lp->fields[fieldno]);
500 }
501 }
502 if (ferror(stdout))
503 err(1, "stdout");
504 }
505
506 /*
507 * Convert an output list argument "2.1, 1.3, 2.4" into an array of output
508 * fields.
509 */
510 static void
fieldarg(char * option)511 fieldarg(char *option)
512 {
513 u_long fieldno;
514 char *end, *token;
515
516 while ((token = strsep(&option, ", \t")) != NULL) {
517 if (*token == '\0')
518 continue;
519 if ((token[0] != '1' && token[0] != '2') || token[1] != '.')
520 errx(1, "malformed -o option field");
521 fieldno = strtol(token + 2, &end, 10);
522 if (*end)
523 errx(1, "malformed -o option field");
524 if (fieldno == 0)
525 errx(1, "field numbers are 1 based");
526 if (olistcnt == olistalloc) {
527 if (reallocarr(&olist,
528 olistalloc + 50, sizeof(OLIST)) != 0)
529 enomem();
530 olistalloc += 50;
531 }
532 olist[olistcnt].fileno = token[0] - '0';
533 olist[olistcnt].fieldno = fieldno - 1;
534 ++olistcnt;
535 }
536 }
537
538 static void
obsolete(char ** argv)539 obsolete(char **argv)
540 {
541 size_t len;
542 char **p, *ap, *t;
543
544 while ((ap = *++argv) != NULL) {
545 /* Return if "--". */
546 if (ap[0] == '-' && ap[1] == '-')
547 return;
548 switch (ap[1]) {
549 case 'a':
550 /*
551 * The original join allowed "-a", which meant the
552 * same as -a1 plus -a2. POSIX 1003.2, Draft 11.2
553 * only specifies this as "-a 1" and "a -2", so we
554 * have to use another option flag, one that is
555 * unlikely to ever be used or accidentally entered
556 * on the command line. (Well, we could reallocate
557 * the argv array, but that hardly seems worthwhile.)
558 */
559 if (ap[2] == '\0')
560 ap[1] = '\01';
561 break;
562 case 'j':
563 /*
564 * The original join allowed "-j[12] arg" and "-j arg".
565 * Convert the former to "-[12] arg". Don't convert
566 * the latter since getopt(3) can handle it.
567 */
568 switch(ap[2]) {
569 case '1':
570 if (ap[3] != '\0')
571 goto jbad;
572 ap[1] = '1';
573 ap[2] = '\0';
574 break;
575 case '2':
576 if (ap[3] != '\0')
577 goto jbad;
578 ap[1] = '2';
579 ap[2] = '\0';
580 break;
581 case '\0':
582 break;
583 default:
584 jbad: warnx("illegal option -- %s", ap);
585 usage();
586 exit(1);
587 }
588 break;
589 case 'o':
590 /*
591 * The original join allowed "-o arg arg". Convert to
592 * "-o arg -o arg".
593 */
594 if (ap[2] != '\0')
595 break;
596 for (p = argv + 2; *p; ++p) {
597 if ((p[0][0] != '1' && p[0][0] != '2') ||
598 p[0][1] != '.')
599 break;
600 len = strlen(*p);
601 if (len - 2 != strspn(*p + 2, "0123456789"))
602 break;
603 if ((t = malloc(len + 3)) == NULL)
604 enomem();
605 t[0] = '-';
606 t[1] = 'o';
607 memmove(t + 2, *p, len + 1);
608 *p = t;
609 }
610 argv = p - 1;
611 break;
612 }
613 }
614 }
615
616 static void
enomem(void)617 enomem(void)
618 {
619 errx(1, "no memory");
620 }
621
622 static void
usage(void)623 usage(void)
624 {
625 (void)fprintf(stderr,
626 "usage: %s [-a fileno | -v fileno] [-e string] [-j fileno field]\n"
627 " [-o list] [-t char] [-1 field] [-2 field] file1 file2\n",
628 getprogname());
629 exit(1);
630 }
631