1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1992-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                                                                      *
20*4887Schin ***********************************************************************/
21*4887Schin #pragma prototyped
22*4887Schin /*
23*4887Schin  * David Korn
24*4887Schin  * AT&T Bell Laboratories
25*4887Schin  *
26*4887Schin  * paste [-s] [-d delim] [file] ...
27*4887Schin  *
28*4887Schin  * paste lines from files together
29*4887Schin  */
30*4887Schin 
31*4887Schin static const char usage[] =
32*4887Schin "[-?\n@(#)$Id: paste (AT&T Research) 1999-06-22 $\n]"
33*4887Schin USAGE_LICENSE
34*4887Schin "[+NAME?paste - merge lines of files]"
35*4887Schin "[+DESCRIPTION?\bpaste\b concatenates the corresponding lines of a "
36*4887Schin 	"given input file and writes the resulting lines to standard "
37*4887Schin 	"output.  By default \bpaste\b replaces the newline character of "
38*4887Schin 	"every line other than the last input file with the TAB character.]"
39*4887Schin "[+?Unless the \b-s\b option is specified, if an end-of-file is encountered "
40*4887Schin 	"on one or more input files, but not all input files, \bpaste\b "
41*4887Schin 	"behaves as if empty lines were read from the file(s) on which "
42*4887Schin 	"end-of-file was detected.]"
43*4887Schin "[+?Unless the \b-s\b option is specified, \bpaste\b is limited by "
44*4887Schin 	"the underlying operating system on how many \afile\a operands "
45*4887Schin 	"can be specified.]"
46*4887Schin "[+?If no \afile\a operands are given or if the \afile\a is \b-\b, \bpaste\b "
47*4887Schin 	"reads from standard input. The start of the file is defined as the "
48*4887Schin 	"current offset.]"
49*4887Schin 
50*4887Schin "[s:serial?Paste the lines of one file at a time rather than one line "
51*4887Schin 	"from each file.  In this case if the \b-d\b option is "
52*4887Schin 	"specified the delimiter will be reset to the first in the "
53*4887Schin 	"list at the beginning of each file.]"
54*4887Schin "[d:delimiters]:[list?\alist\a specifies a list of delimiters.  These "
55*4887Schin 	"delimiters are used circularly instead of TAB to replace "
56*4887Schin 	"the newline character of the input lines. Unless the \b-s\b "
57*4887Schin 	"option is specified, the delimiter will be reset to the first "
58*4887Schin 	"element of \alist\a each time a line is processed from each file.  "
59*4887Schin 	"The delimiter characters corresponding to \alist\a will be found "
60*4887Schin 	"by treating \alist\a as an ANSI-C string, except that the \b\\0\b "
61*4887Schin 	"sequence will insert the empty string instead of the null character.]"
62*4887Schin "\n"
63*4887Schin "\n[file ...]\n"
64*4887Schin "\n"
65*4887Schin "[+EXIT STATUS?]{"
66*4887Schin 	"[+0?All files processed successfully.]"
67*4887Schin 	"[+>0?An error occurred.]"
68*4887Schin "}"
69*4887Schin "[+SEE ALSO?\bcut\b(1), \bcat\b(1), \bjoin\b(1)]"
70*4887Schin ;
71*4887Schin 
72*4887Schin 
73*4887Schin #include <cmd.h>
74*4887Schin 
75*4887Schin /*
76*4887Schin  * paste the lines of the <nstreams> defined in <streams> and put results
77*4887Schin  * to <out>
78*4887Schin  */
79*4887Schin 
80*4887Schin static int paste(int nstream,Sfio_t* streams[],Sfio_t *out, register const char *delim,int dlen)
81*4887Schin {
82*4887Schin 	register const char *cp;
83*4887Schin 	register int d, n, more=1;
84*4887Schin 	register Sfio_t *fp;
85*4887Schin 	do
86*4887Schin 	{
87*4887Schin 		d = (dlen>0?0:-1);
88*4887Schin 		for(n=more-1,more=0; n < nstream;)
89*4887Schin 		{
90*4887Schin 			if(fp=streams[n])
91*4887Schin 			{
92*4887Schin 				if(cp = sfgetr(fp,'\n',0))
93*4887Schin 				{
94*4887Schin 					if(n==0)
95*4887Schin 						more = 1;
96*4887Schin 					else if(!more) /* first stream with output */
97*4887Schin 					{
98*4887Schin 						if(dlen==1)
99*4887Schin 							sfnputc(out, *delim, n);
100*4887Schin 						else if(dlen>0)
101*4887Schin 						{
102*4887Schin 							for(d=n; d>dlen; d-=dlen)
103*4887Schin 								sfwrite(out,delim,dlen);
104*4887Schin 							if(d)
105*4887Schin 								sfwrite(out,delim,d);
106*4887Schin 						}
107*4887Schin 						more = n+1;
108*4887Schin 					}
109*4887Schin 					if(sfwrite(out,cp,sfvalue(fp)-((n+1)<nstream)) < 0)
110*4887Schin 						return(-1);
111*4887Schin 				}
112*4887Schin 				else
113*4887Schin 					streams[n] = 0;
114*4887Schin 			}
115*4887Schin 			if(++n<nstream && more && d>=0)
116*4887Schin 			{
117*4887Schin 				register int c;
118*4887Schin 				if(d >= dlen)
119*4887Schin 					d = 0;
120*4887Schin 				if(c=delim[d++])
121*4887Schin 					sfputc(out,c);
122*4887Schin 			}
123*4887Schin 			else if(n==nstream && !streams[n-1] && more)
124*4887Schin 				sfputc(out,'\n');
125*4887Schin 		}
126*4887Schin 	}
127*4887Schin 	while(more);
128*4887Schin 	return(0);
129*4887Schin }
130*4887Schin 
131*4887Schin /*
132*4887Schin  * Handles paste -s, for file <in> to file <out> using delimiters <delim>
133*4887Schin  */
134*4887Schin static int spaste(Sfio_t *in,register Sfio_t* out,register const char *delim,int dlen)
135*4887Schin {
136*4887Schin 	register const char *cp;
137*4887Schin 	register int d=0;
138*4887Schin 	if(cp = sfgetr(in,'\n',0))
139*4887Schin 	{
140*4887Schin 		if(sfwrite(out,cp,sfvalue(in)-1) < 0)
141*4887Schin 			return(-1);
142*4887Schin 	}
143*4887Schin 	while(cp=sfgetr(in, '\n',0))
144*4887Schin 	{
145*4887Schin 		if(dlen)
146*4887Schin 		{
147*4887Schin 			register int c;
148*4887Schin 			if(d >= dlen)
149*4887Schin 				d = 0;
150*4887Schin 			if(c=delim[d++])
151*4887Schin 				sfputc(out,c);
152*4887Schin 		}
153*4887Schin 		if(sfwrite(out,cp,sfvalue(in)-1) < 0)
154*4887Schin 			return(-1);
155*4887Schin 	}
156*4887Schin 	sfputc(out,'\n');
157*4887Schin 	return(0);
158*4887Schin }
159*4887Schin 
160*4887Schin int
161*4887Schin b_paste(int argc,register char *argv[], void* context)
162*4887Schin {
163*4887Schin 	register int		n, sflag=0;
164*4887Schin 	register Sfio_t		*fp, **streams;
165*4887Schin 	register char 		*cp, *delim;
166*4887Schin 	int			dlen;
167*4887Schin 	char			defdelim[2];
168*4887Schin 
169*4887Schin 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
170*4887Schin 	delim = 0;
171*4887Schin 	while (n = optget(argv, usage)) switch (n)
172*4887Schin 	{
173*4887Schin 	    case 'd':
174*4887Schin 		delim = opt_info.arg;
175*4887Schin 		break;
176*4887Schin 	    case 's':
177*4887Schin 		sflag++;
178*4887Schin 		break;
179*4887Schin 	    case ':':
180*4887Schin 		error(2, "%s", opt_info.arg);
181*4887Schin 		break;
182*4887Schin 	    case '?':
183*4887Schin 		error(ERROR_usage(2), "%s", opt_info.arg);
184*4887Schin 		break;
185*4887Schin 	}
186*4887Schin 	argv += opt_info.index;
187*4887Schin 	if(error_info.errors)
188*4887Schin 		error(ERROR_usage(2),"%s", optusage(NiL));
189*4887Schin 	if(delim)
190*4887Schin 		dlen = stresc(delim);
191*4887Schin 	else
192*4887Schin 	{
193*4887Schin 		*(delim = defdelim) = '\t';
194*4887Schin 		dlen = 1;
195*4887Schin 	}
196*4887Schin 	if(cp = *argv)
197*4887Schin 	{
198*4887Schin 		n = argc - opt_info.index;
199*4887Schin 		argv++;
200*4887Schin 	}
201*4887Schin 	else
202*4887Schin 		n = 1;
203*4887Schin 	if(!sflag)
204*4887Schin 	{
205*4887Schin 		if (!(streams = (Sfio_t**)stakalloc(n*sizeof(Sfio_t*))))
206*4887Schin 			error(ERROR_exit(1), "out of space");
207*4887Schin 		n = 0;
208*4887Schin 	}
209*4887Schin 	do
210*4887Schin 	{
211*4887Schin 		if(!cp || streq(cp,"-"))
212*4887Schin 			fp = sfstdin;
213*4887Schin 		else if(!(fp = sfopen(NiL,cp,"r")))
214*4887Schin 		{
215*4887Schin 			error(ERROR_system(0),"%s: cannot open",cp);
216*4887Schin 			error_info.errors = 1;
217*4887Schin 		}
218*4887Schin 		if(fp && sflag)
219*4887Schin 		{
220*4887Schin 			if(spaste(fp,sfstdout,delim,dlen) < 0)
221*4887Schin 			{
222*4887Schin 				error(ERROR_system(0),"write failed");
223*4887Schin 				error_info.errors = 1;
224*4887Schin 			}
225*4887Schin 			if(fp!=sfstdin)
226*4887Schin 				sfclose(fp);
227*4887Schin 		}
228*4887Schin 		else
229*4887Schin 			streams[n++] = fp;
230*4887Schin 	}
231*4887Schin 	while(cp= *argv++);
232*4887Schin 	if(!sflag)
233*4887Schin 	{
234*4887Schin 		if(error_info.errors==0 && paste(n,streams,sfstdout,delim,dlen) < 0)
235*4887Schin 		{
236*4887Schin 			error(ERROR_system(0),"write failed");
237*4887Schin 			error_info.errors = 1;
238*4887Schin 		}
239*4887Schin 		while(--n>=0)
240*4887Schin 		{
241*4887Schin 			if((fp=streams[n]) && fp!=sfstdin)
242*4887Schin 				sfclose(fp);
243*4887Schin 		}
244*4887Schin 	}
245*4887Schin 	return(error_info.errors);
246*4887Schin }
247*4887Schin 
248