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