xref: /freebsd/usr.bin/paste/paste.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam S. Moskowitz of Menlo Consulting.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 
41 #if 0
42 #ifndef lint
43 static char sccsid[] = "@(#)paste.c	8.1 (Berkeley) 6/6/93";
44 #endif /* not lint */
45 #endif
46 
47 #include <sys/cdefs.h>
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 
60 static wchar_t *delim;
61 static int delimcnt;
62 
63 static int parallel(char **);
64 static int sequential(char **);
65 static int tr(wchar_t *);
66 static void usage(void) __dead2;
67 
68 static wchar_t tab[] = L"\t";
69 
70 int
71 main(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 
121 typedef struct _list {
122 	struct _list *next;
123 	FILE *fp;
124 	int cnt;
125 	char *name;
126 } LIST;
127 
128 static int
129 parallel(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 
198 static int
199 sequential(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 
238 static int
239 tr(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 
267 static void
268 usage(void)
269 {
270 	(void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n");
271 	exit(1);
272 }
273