xref: /freebsd/usr.bin/paste/paste.c (revision b2eeeae0)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Adam S. Moskowitz of Menlo Consulting.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
381e133604SPhilippe Charnier static const char copyright[] =
399b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
409b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
419b50d902SRodney W. Grimes #endif /* not lint */
429b50d902SRodney W. Grimes 
431e133604SPhilippe Charnier #if 0
44d345176dSMike Barcroft #ifndef lint
459b50d902SRodney W. Grimes static char sccsid[] = "@(#)paste.c	8.1 (Berkeley) 6/6/93";
469b50d902SRodney W. Grimes #endif /* not lint */
47d345176dSMike Barcroft #endif
48d345176dSMike Barcroft 
49d345176dSMike Barcroft #include <sys/cdefs.h>
50d345176dSMike Barcroft __FBSDID("$FreeBSD$");
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes #include <sys/types.h>
5378552e79SMark Murray 
541e133604SPhilippe Charnier #include <err.h>
559b50d902SRodney W. Grimes #include <errno.h>
569b50d902SRodney W. Grimes #include <limits.h>
570f10c7bbSTim J. Robbins #include <locale.h>
589b50d902SRodney W. Grimes #include <stdio.h>
59c93bd87aSJohn Birrell #include <stdlib.h>
609b50d902SRodney W. Grimes #include <string.h>
611e133604SPhilippe Charnier #include <unistd.h>
620f10c7bbSTim J. Robbins #include <wchar.h>
639b50d902SRodney W. Grimes 
640f10c7bbSTim J. Robbins wchar_t *delim;
659b50d902SRodney W. Grimes int delimcnt;
669b50d902SRodney W. Grimes 
670968654cSTim J. Robbins int parallel(char **);
680968654cSTim J. Robbins int sequential(char **);
690f10c7bbSTim J. Robbins int tr(wchar_t *);
70d3cb5dedSWarner Losh static void usage(void);
711e133604SPhilippe Charnier 
720f10c7bbSTim J. Robbins wchar_t tab[] = L"\t";
7378552e79SMark Murray 
741e133604SPhilippe Charnier int
7578552e79SMark Murray main(int argc, char *argv[])
769b50d902SRodney W. Grimes {
770968654cSTim J. Robbins 	int ch, rval, seq;
780f10c7bbSTim J. Robbins 	wchar_t *warg;
790f10c7bbSTim J. Robbins 	const char *arg;
800f10c7bbSTim J. Robbins 	size_t len;
810f10c7bbSTim J. Robbins 
820f10c7bbSTim J. Robbins 	setlocale(LC_CTYPE, "");
839b50d902SRodney W. Grimes 
849b50d902SRodney W. Grimes 	seq = 0;
851c8af878SWarner Losh 	while ((ch = getopt(argc, argv, "d:s")) != -1)
869b50d902SRodney W. Grimes 		switch(ch) {
879b50d902SRodney W. Grimes 		case 'd':
880f10c7bbSTim J. Robbins 			arg = optarg;
890f10c7bbSTim J. Robbins 			len = mbsrtowcs(NULL, &arg, 0, NULL);
900f10c7bbSTim J. Robbins 			if (len == (size_t)-1)
910f10c7bbSTim J. Robbins 				err(1, "delimiters");
920f10c7bbSTim J. Robbins 			warg = malloc((len + 1) * sizeof(*warg));
930f10c7bbSTim J. Robbins 			if (warg == NULL)
940f10c7bbSTim J. Robbins 				err(1, NULL);
950f10c7bbSTim J. Robbins 			arg = optarg;
960f10c7bbSTim J. Robbins 			len = mbsrtowcs(warg, &arg, len + 1, NULL);
970f10c7bbSTim J. Robbins 			if (len == (size_t)-1)
980f10c7bbSTim J. Robbins 				err(1, "delimiters");
990f10c7bbSTim J. Robbins 			delimcnt = tr(delim = warg);
1009b50d902SRodney W. Grimes 			break;
1019b50d902SRodney W. Grimes 		case 's':
1029b50d902SRodney W. Grimes 			seq = 1;
1039b50d902SRodney W. Grimes 			break;
1049b50d902SRodney W. Grimes 		case '?':
1059b50d902SRodney W. Grimes 		default:
1069b50d902SRodney W. Grimes 			usage();
1079b50d902SRodney W. Grimes 		}
1089b50d902SRodney W. Grimes 	argc -= optind;
1099b50d902SRodney W. Grimes 	argv += optind;
1109b50d902SRodney W. Grimes 
111d345176dSMike Barcroft 	if (*argv == NULL)
112d345176dSMike Barcroft 		usage();
1139b50d902SRodney W. Grimes 	if (!delim) {
1149b50d902SRodney W. Grimes 		delimcnt = 1;
11578552e79SMark Murray 		delim = tab;
1169b50d902SRodney W. Grimes 	}
1179b50d902SRodney W. Grimes 
1189b50d902SRodney W. Grimes 	if (seq)
1190968654cSTim J. Robbins 		rval = sequential(argv);
1209b50d902SRodney W. Grimes 	else
1210968654cSTim J. Robbins 		rval = parallel(argv);
1220968654cSTim J. Robbins 	exit(rval);
1239b50d902SRodney W. Grimes }
1249b50d902SRodney W. Grimes 
1259b50d902SRodney W. Grimes typedef struct _list {
1269b50d902SRodney W. Grimes 	struct _list *next;
1279b50d902SRodney W. Grimes 	FILE *fp;
1289b50d902SRodney W. Grimes 	int cnt;
1299b50d902SRodney W. Grimes 	char *name;
1309b50d902SRodney W. Grimes } LIST;
1319b50d902SRodney W. Grimes 
1320968654cSTim J. Robbins int
13378552e79SMark Murray parallel(char **argv)
1349b50d902SRodney W. Grimes {
13578552e79SMark Murray 	LIST *lp;
136d4286259STim J. Robbins 	int cnt;
1370f10c7bbSTim J. Robbins 	wint_t ich;
1380f10c7bbSTim J. Robbins 	wchar_t ch;
1390f10c7bbSTim J. Robbins 	char *p;
1409b50d902SRodney W. Grimes 	LIST *head, *tmp;
1419b50d902SRodney W. Grimes 	int opencnt, output;
1429b50d902SRodney W. Grimes 
143b2eeeae0SPhilippe Charnier 	for (cnt = 0, head = tmp = NULL; (p = *argv); ++argv, ++cnt) {
14447bca8b0SJuli Mallett 		if ((lp = malloc(sizeof(LIST))) == NULL)
14547bca8b0SJuli Mallett 			err(1, NULL);
1469b50d902SRodney W. Grimes 		if (p[0] == '-' && !p[1])
1479b50d902SRodney W. Grimes 			lp->fp = stdin;
1481e133604SPhilippe Charnier 		else if (!(lp->fp = fopen(p, "r")))
1491e133604SPhilippe Charnier 			err(1, "%s", p);
1509b50d902SRodney W. Grimes 		lp->next = NULL;
1519b50d902SRodney W. Grimes 		lp->cnt = cnt;
1529b50d902SRodney W. Grimes 		lp->name = p;
1539b50d902SRodney W. Grimes 		if (!head)
1549b50d902SRodney W. Grimes 			head = tmp = lp;
1559b50d902SRodney W. Grimes 		else {
1569b50d902SRodney W. Grimes 			tmp->next = lp;
1579b50d902SRodney W. Grimes 			tmp = lp;
1589b50d902SRodney W. Grimes 		}
1599b50d902SRodney W. Grimes 	}
1609b50d902SRodney W. Grimes 
1619b50d902SRodney W. Grimes 	for (opencnt = cnt; opencnt;) {
1629b50d902SRodney W. Grimes 		for (output = 0, lp = head; lp; lp = lp->next) {
1639b50d902SRodney W. Grimes 			if (!lp->fp) {
1649b50d902SRodney W. Grimes 				if (output && lp->cnt &&
1659b50d902SRodney W. Grimes 				    (ch = delim[(lp->cnt - 1) % delimcnt]))
1660f10c7bbSTim J. Robbins 					putwchar(ch);
1679b50d902SRodney W. Grimes 				continue;
1689b50d902SRodney W. Grimes 			}
1690f10c7bbSTim J. Robbins 			if ((ich = getwc(lp->fp)) == WEOF) {
1709b50d902SRodney W. Grimes 				if (!--opencnt)
1719b50d902SRodney W. Grimes 					break;
1729b50d902SRodney W. Grimes 				lp->fp = NULL;
1739b50d902SRodney W. Grimes 				if (output && lp->cnt &&
1749b50d902SRodney W. Grimes 				    (ch = delim[(lp->cnt - 1) % delimcnt]))
1750f10c7bbSTim J. Robbins 					putwchar(ch);
1769b50d902SRodney W. Grimes 				continue;
1779b50d902SRodney W. Grimes 			}
1789b50d902SRodney W. Grimes 			/*
1799b50d902SRodney W. Grimes 			 * make sure that we don't print any delimiters
1809b50d902SRodney W. Grimes 			 * unless there's a non-empty file.
1819b50d902SRodney W. Grimes 			 */
1829b50d902SRodney W. Grimes 			if (!output) {
1839b50d902SRodney W. Grimes 				output = 1;
1849b50d902SRodney W. Grimes 				for (cnt = 0; cnt < lp->cnt; ++cnt)
1851e133604SPhilippe Charnier 					if ((ch = delim[cnt % delimcnt]))
1860f10c7bbSTim J. Robbins 						putwchar(ch);
1871e133604SPhilippe Charnier 			} else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
1880f10c7bbSTim J. Robbins 				putwchar(ch);
1890f10c7bbSTim J. Robbins 			if (ich == '\n')
1900f10c7bbSTim J. Robbins 				continue;
1910f10c7bbSTim J. Robbins 			do {
1920f10c7bbSTim J. Robbins 				putwchar(ich);
1930f10c7bbSTim J. Robbins 			} while ((ich = getwc(lp->fp)) != WEOF && ich != '\n');
1949b50d902SRodney W. Grimes 		}
1959b50d902SRodney W. Grimes 		if (output)
1960f10c7bbSTim J. Robbins 			putwchar('\n');
1979b50d902SRodney W. Grimes 	}
1980968654cSTim J. Robbins 
1990968654cSTim J. Robbins 	return (0);
2009b50d902SRodney W. Grimes }
2019b50d902SRodney W. Grimes 
2020968654cSTim J. Robbins int
20378552e79SMark Murray sequential(char **argv)
2049b50d902SRodney W. Grimes {
20578552e79SMark Murray 	FILE *fp;
206d4286259STim J. Robbins 	int cnt, failed, needdelim;
2070f10c7bbSTim J. Robbins 	wint_t ch;
2080f10c7bbSTim J. Robbins 	char *p;
2099b50d902SRodney W. Grimes 
2100968654cSTim J. Robbins 	failed = 0;
2111e133604SPhilippe Charnier 	for (; (p = *argv); ++argv) {
2129b50d902SRodney W. Grimes 		if (p[0] == '-' && !p[1])
2139b50d902SRodney W. Grimes 			fp = stdin;
2149b50d902SRodney W. Grimes 		else if (!(fp = fopen(p, "r"))) {
2151e133604SPhilippe Charnier 			warn("%s", p);
2160968654cSTim J. Robbins 			failed = 1;
2179b50d902SRodney W. Grimes 			continue;
2189b50d902SRodney W. Grimes 		}
219ce78cbf9STim J. Robbins 		cnt = needdelim = 0;
2200f10c7bbSTim J. Robbins 		while ((ch = getwc(fp)) != WEOF) {
221ce78cbf9STim J. Robbins 			if (needdelim) {
222ce78cbf9STim J. Robbins 				needdelim = 0;
223ce78cbf9STim J. Robbins 				if (delim[cnt] != '\0')
2240f10c7bbSTim J. Robbins 					putwchar(delim[cnt]);
225ce78cbf9STim J. Robbins 				if (++cnt == delimcnt)
2269b50d902SRodney W. Grimes 					cnt = 0;
2279b50d902SRodney W. Grimes 			}
2280f10c7bbSTim J. Robbins 			if (ch != '\n')
2290f10c7bbSTim J. Robbins 				putwchar(ch);
2300f10c7bbSTim J. Robbins 			else
231ce78cbf9STim J. Robbins 				needdelim = 1;
2329b50d902SRodney W. Grimes 		}
233ce78cbf9STim J. Robbins 		if (needdelim)
2340f10c7bbSTim J. Robbins 			putwchar('\n');
2359b50d902SRodney W. Grimes 		if (fp != stdin)
2369b50d902SRodney W. Grimes 			(void)fclose(fp);
2379b50d902SRodney W. Grimes 	}
2380968654cSTim J. Robbins 
2390968654cSTim J. Robbins 	return (failed != 0);
2409b50d902SRodney W. Grimes }
2419b50d902SRodney W. Grimes 
2421e133604SPhilippe Charnier int
2430f10c7bbSTim J. Robbins tr(wchar_t *arg)
2449b50d902SRodney W. Grimes {
24578552e79SMark Murray 	int cnt;
2460f10c7bbSTim J. Robbins 	wchar_t ch, *p;
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 	for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt)
2499b50d902SRodney W. Grimes 		if (ch == '\\')
2509b50d902SRodney W. Grimes 			switch(ch = *p++) {
2519b50d902SRodney W. Grimes 			case 'n':
2529b50d902SRodney W. Grimes 				*arg = '\n';
2539b50d902SRodney W. Grimes 				break;
2549b50d902SRodney W. Grimes 			case 't':
2559b50d902SRodney W. Grimes 				*arg = '\t';
2569b50d902SRodney W. Grimes 				break;
2579b50d902SRodney W. Grimes 			case '0':
2589b50d902SRodney W. Grimes 				*arg = '\0';
2599b50d902SRodney W. Grimes 				break;
2609b50d902SRodney W. Grimes 			default:
2619b50d902SRodney W. Grimes 				*arg = ch;
2629b50d902SRodney W. Grimes 				break;
2639b50d902SRodney W. Grimes 		} else
2649b50d902SRodney W. Grimes 			*arg = ch;
2659b50d902SRodney W. Grimes 
2661e133604SPhilippe Charnier 	if (!cnt)
2671e133604SPhilippe Charnier 		errx(1, "no delimiters specified");
2689b50d902SRodney W. Grimes 	return(cnt);
2699b50d902SRodney W. Grimes }
2709b50d902SRodney W. Grimes 
2711e133604SPhilippe Charnier static void
27278552e79SMark Murray usage(void)
2739b50d902SRodney W. Grimes {
2741e133604SPhilippe Charnier 	(void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n");
2759b50d902SRodney W. Grimes 	exit(1);
2769b50d902SRodney W. Grimes }
277