xref: /freebsd/usr.bin/paste/paste.c (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
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 #endif
43 
44 #include <sys/cdefs.h>
45 #include <sys/types.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <limits.h>
50 #include <locale.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <wchar.h>
56 
57 static wchar_t *delim;
58 static int delimcnt;
59 
60 static int parallel(char **);
61 static int sequential(char **);
62 static int tr(wchar_t *);
63 static void usage(void) __dead2;
64 
65 static wchar_t tab[] = L"\t";
66 
67 int
68 main(int argc, char *argv[])
69 {
70 	int ch, rval, seq;
71 	wchar_t *warg;
72 	const char *arg;
73 	size_t len;
74 
75 	setlocale(LC_CTYPE, "");
76 
77 	seq = 0;
78 	while ((ch = getopt(argc, argv, "d:s")) != -1)
79 		switch(ch) {
80 		case 'd':
81 			arg = optarg;
82 			len = mbsrtowcs(NULL, &arg, 0, NULL);
83 			if (len == (size_t)-1)
84 				err(1, "delimiters");
85 			warg = malloc((len + 1) * sizeof(*warg));
86 			if (warg == NULL)
87 				err(1, NULL);
88 			arg = optarg;
89 			len = mbsrtowcs(warg, &arg, len + 1, NULL);
90 			if (len == (size_t)-1)
91 				err(1, "delimiters");
92 			delimcnt = tr(delim = warg);
93 			break;
94 		case 's':
95 			seq = 1;
96 			break;
97 		case '?':
98 		default:
99 			usage();
100 		}
101 	argc -= optind;
102 	argv += optind;
103 
104 	if (*argv == NULL)
105 		usage();
106 	if (!delim) {
107 		delimcnt = 1;
108 		delim = tab;
109 	}
110 
111 	if (seq)
112 		rval = sequential(argv);
113 	else
114 		rval = parallel(argv);
115 	exit(rval);
116 }
117 
118 typedef struct _list {
119 	struct _list *next;
120 	FILE *fp;
121 	int cnt;
122 	char *name;
123 } LIST;
124 
125 static int
126 parallel(char **argv)
127 {
128 	LIST *lp;
129 	int cnt;
130 	wint_t ich;
131 	wchar_t ch;
132 	char *p;
133 	LIST *head, *tmp;
134 	int opencnt, output;
135 
136 	for (cnt = 0, head = tmp = NULL; (p = *argv); ++argv, ++cnt) {
137 		if ((lp = malloc(sizeof(LIST))) == NULL)
138 			err(1, NULL);
139 		if (p[0] == '-' && !p[1])
140 			lp->fp = stdin;
141 		else if (!(lp->fp = fopen(p, "r")))
142 			err(1, "%s", p);
143 		lp->next = NULL;
144 		lp->cnt = cnt;
145 		lp->name = p;
146 		if (!head)
147 			head = tmp = lp;
148 		else {
149 			tmp->next = lp;
150 			tmp = lp;
151 		}
152 	}
153 
154 	for (opencnt = cnt; opencnt;) {
155 		for (output = 0, lp = head; lp; lp = lp->next) {
156 			if (!lp->fp) {
157 				if (output && lp->cnt &&
158 				    (ch = delim[(lp->cnt - 1) % delimcnt]))
159 					putwchar(ch);
160 				continue;
161 			}
162 			if ((ich = getwc(lp->fp)) == WEOF) {
163 				if (!--opencnt)
164 					break;
165 				lp->fp = NULL;
166 				if (output && lp->cnt &&
167 				    (ch = delim[(lp->cnt - 1) % delimcnt]))
168 					putwchar(ch);
169 				continue;
170 			}
171 			/*
172 			 * make sure that we don't print any delimiters
173 			 * unless there's a non-empty file.
174 			 */
175 			if (!output) {
176 				output = 1;
177 				for (cnt = 0; cnt < lp->cnt; ++cnt)
178 					if ((ch = delim[cnt % delimcnt]))
179 						putwchar(ch);
180 			} else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
181 				putwchar(ch);
182 			if (ich == '\n')
183 				continue;
184 			do {
185 				putwchar(ich);
186 			} while ((ich = getwc(lp->fp)) != WEOF && ich != '\n');
187 		}
188 		if (output)
189 			putwchar('\n');
190 	}
191 
192 	return (0);
193 }
194 
195 static int
196 sequential(char **argv)
197 {
198 	FILE *fp;
199 	int cnt, failed, needdelim;
200 	wint_t ch;
201 	char *p;
202 
203 	failed = 0;
204 	for (; (p = *argv); ++argv) {
205 		if (p[0] == '-' && !p[1])
206 			fp = stdin;
207 		else if (!(fp = fopen(p, "r"))) {
208 			warn("%s", p);
209 			failed = 1;
210 			continue;
211 		}
212 		cnt = needdelim = 0;
213 		while ((ch = getwc(fp)) != WEOF) {
214 			if (needdelim) {
215 				needdelim = 0;
216 				if (delim[cnt] != '\0')
217 					putwchar(delim[cnt]);
218 				if (++cnt == delimcnt)
219 					cnt = 0;
220 			}
221 			if (ch != '\n')
222 				putwchar(ch);
223 			else
224 				needdelim = 1;
225 		}
226 		if (needdelim)
227 			putwchar('\n');
228 		if (fp != stdin)
229 			(void)fclose(fp);
230 	}
231 
232 	return (failed != 0);
233 }
234 
235 static int
236 tr(wchar_t *arg)
237 {
238 	int cnt;
239 	wchar_t ch, *p;
240 
241 	for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt)
242 		if (ch == '\\')
243 			switch(ch = *p++) {
244 			case 'n':
245 				*arg = '\n';
246 				break;
247 			case 't':
248 				*arg = '\t';
249 				break;
250 			case '0':
251 				*arg = '\0';
252 				break;
253 			default:
254 				*arg = ch;
255 				break;
256 		} else
257 			*arg = ch;
258 
259 	if (!cnt)
260 		errx(1, "no delimiters specified");
261 	return(cnt);
262 }
263 
264 static void
265 usage(void)
266 {
267 	(void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n");
268 	exit(1);
269 }
270