1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Adam S. Moskowitz of Menlo Consulting.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34static const char copyright[] =
35"@(#) Copyright (c) 1989, 1993\n\
36	The Regents of the University of California.  All rights reserved.\n";
37#endif /* not lint */
38
39#if 0
40#ifndef lint
41static char sccsid[] = "@(#)paste.c	8.1 (Berkeley) 6/6/93";
42#endif /* not lint */
43#endif
44
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD$");
47
48#include <sys/types.h>
49
50#include <err.h>
51#include <errno.h>
52#include <limits.h>
53#include <locale.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58#include <wchar.h>
59
60static wchar_t *delim;
61static int delimcnt;
62
63static int parallel(char **);
64static int sequential(char **);
65static int tr(wchar_t *);
66static void usage(void);
67
68static wchar_t tab[] = L"\t";
69
70int
71main(int argc, char *argv[])
72{
73	int ch, rval, seq;
74	wchar_t *warg;
75	const char *arg;
76	size_t len;
77
78	setlocale(LC_CTYPE, "");
79
80	seq = 0;
81	while ((ch = getopt(argc, argv, "d:s")) != -1)
82		switch(ch) {
83		case 'd':
84			arg = optarg;
85			len = mbsrtowcs(NULL, &arg, 0, NULL);
86			if (len == (size_t)-1)
87				err(1, "delimiters");
88			warg = malloc((len + 1) * sizeof(*warg));
89			if (warg == NULL)
90				err(1, NULL);
91			arg = optarg;
92			len = mbsrtowcs(warg, &arg, len + 1, NULL);
93			if (len == (size_t)-1)
94				err(1, "delimiters");
95			delimcnt = tr(delim = warg);
96			break;
97		case 's':
98			seq = 1;
99			break;
100		case '?':
101		default:
102			usage();
103		}
104	argc -= optind;
105	argv += optind;
106
107	if (*argv == NULL)
108		usage();
109	if (!delim) {
110		delimcnt = 1;
111		delim = tab;
112	}
113
114	if (seq)
115		rval = sequential(argv);
116	else
117		rval = parallel(argv);
118	exit(rval);
119}
120
121typedef struct _list {
122	struct _list *next;
123	FILE *fp;
124	int cnt;
125	char *name;
126} LIST;
127
128static int
129parallel(char **argv)
130{
131	LIST *lp;
132	int cnt;
133	wint_t ich;
134	wchar_t ch;
135	char *p;
136	LIST *head, *tmp;
137	int opencnt, output;
138
139	for (cnt = 0, head = tmp = NULL; (p = *argv); ++argv, ++cnt) {
140		if ((lp = malloc(sizeof(LIST))) == NULL)
141			err(1, NULL);
142		if (p[0] == '-' && !p[1])
143			lp->fp = stdin;
144		else if (!(lp->fp = fopen(p, "r")))
145			err(1, "%s", p);
146		lp->next = NULL;
147		lp->cnt = cnt;
148		lp->name = p;
149		if (!head)
150			head = tmp = lp;
151		else {
152			tmp->next = lp;
153			tmp = lp;
154		}
155	}
156
157	for (opencnt = cnt; opencnt;) {
158		for (output = 0, lp = head; lp; lp = lp->next) {
159			if (!lp->fp) {
160				if (output && lp->cnt &&
161				    (ch = delim[(lp->cnt - 1) % delimcnt]))
162					putwchar(ch);
163				continue;
164			}
165			if ((ich = getwc(lp->fp)) == WEOF) {
166				if (!--opencnt)
167					break;
168				lp->fp = NULL;
169				if (output && lp->cnt &&
170				    (ch = delim[(lp->cnt - 1) % delimcnt]))
171					putwchar(ch);
172				continue;
173			}
174			/*
175			 * make sure that we don't print any delimiters
176			 * unless there's a non-empty file.
177			 */
178			if (!output) {
179				output = 1;
180				for (cnt = 0; cnt < lp->cnt; ++cnt)
181					if ((ch = delim[cnt % delimcnt]))
182						putwchar(ch);
183			} else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
184				putwchar(ch);
185			if (ich == '\n')
186				continue;
187			do {
188				putwchar(ich);
189			} while ((ich = getwc(lp->fp)) != WEOF && ich != '\n');
190		}
191		if (output)
192			putwchar('\n');
193	}
194
195	return (0);
196}
197
198static int
199sequential(char **argv)
200{
201	FILE *fp;
202	int cnt, failed, needdelim;
203	wint_t ch;
204	char *p;
205
206	failed = 0;
207	for (; (p = *argv); ++argv) {
208		if (p[0] == '-' && !p[1])
209			fp = stdin;
210		else if (!(fp = fopen(p, "r"))) {
211			warn("%s", p);
212			failed = 1;
213			continue;
214		}
215		cnt = needdelim = 0;
216		while ((ch = getwc(fp)) != WEOF) {
217			if (needdelim) {
218				needdelim = 0;
219				if (delim[cnt] != '\0')
220					putwchar(delim[cnt]);
221				if (++cnt == delimcnt)
222					cnt = 0;
223			}
224			if (ch != '\n')
225				putwchar(ch);
226			else
227				needdelim = 1;
228		}
229		if (needdelim)
230			putwchar('\n');
231		if (fp != stdin)
232			(void)fclose(fp);
233	}
234
235	return (failed != 0);
236}
237
238static int
239tr(wchar_t *arg)
240{
241	int cnt;
242	wchar_t ch, *p;
243
244	for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt)
245		if (ch == '\\')
246			switch(ch = *p++) {
247			case 'n':
248				*arg = '\n';
249				break;
250			case 't':
251				*arg = '\t';
252				break;
253			case '0':
254				*arg = '\0';
255				break;
256			default:
257				*arg = ch;
258				break;
259		} else
260			*arg = ch;
261
262	if (!cnt)
263		errx(1, "no delimiters specified");
264	return(cnt);
265}
266
267static void
268usage(void)
269{
270	(void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n");
271	exit(1);
272}
273