xref: /titanic_44/usr/src/lib/libcmd/common/cat.c (revision 55553f719b521a0bb4deab6efc944cd30c1a56aa)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1992-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                                                                      *
20 ***********************************************************************/
21 #pragma prototyped
22 /*
23  * David Korn
24  * Glenn Fowler
25  * AT&T Bell Laboratories
26  *
27  * cat
28  */
29 
30 #include <cmd.h>
31 #include <fcntl.h>
32 
33 static const char usage[] =
34 "[-?\n@(#)$Id: cat (AT&T Research) 2006-05-17 $\n]"
35 USAGE_LICENSE
36 "[+NAME?cat - concatenate files]"
37 "[+DESCRIPTION?\bcat\b copies each \afile\a in sequence to the standard"
38 "	output. If no \afile\a is given, or if the \afile\a is \b-\b,"
39 "	\bcat\b copies from standard input starting at the current location.]"
40 
41 "[b:number-nonblank?Number lines as with \b-n\b but omit line numbers from"
42 "	blank lines.]"
43 "[d:dos-input?Input files are opened in \atext\amode which removes carriage"
44 "	returns in front of new-lines on some systems.]"
45 "[e?Equivalent to \b-vE\b.]"
46 "[n:number?Causes a line number to be inserted at the beginning of each line.]"
47 "[s?Equivalent to \b-S\b for \aatt\a universe and \b-B\b otherwise.]"
48 "[t?Equivalent to \b-vT\b.]"
49 "[u:unbuffer?The output is not delayed by buffering.]"
50 "[v:show-nonprinting?Causes non-printing characters (whith the exception of"
51 "	tabs, new-lines, and form-feeds) to be output as printable charater"
52 "	sequences. ASCII control characters are printed as \b^\b\an\a,"
53 "	where \an\a is the corresponding ASCII character in the range"
54 "	octal 100-137. The DEL character (octal 0177) is copied"
55 "	as \b^?\b. Other non-printable characters are copied as \bM-\b\ax\a"
56 "	where \ax\a is the ASCII character specified by the low-order seven"
57 "	bits.  Multibyte characters in the current locale are treated as"
58 "	printable characters.]"
59 "[A:show-all?Equivalent to \b-vET\b.]"
60 "[B:squeeze-blank?Multiple adjacent new-line characters are replace by one"
61 "	new-line.]"
62 "[D:dos-output?Output files are opened in \atext\amode which inserts carriage"
63 "	returns in front of new-lines on some systems.]"
64 "[E:show-ends?Causes a \b$\b to be inserted before each new-line.]"
65 "[S:silent?\bcat\b is silent about non-existent files.]"
66 "[T:show-blank?Causes tabs to be copied as \b^I\b and formfeeds as \b^L\b.]"
67 
68 "\n"
69 "\n[file ...]\n"
70 "\n"
71 
72 "[+SEE ALSO?\bcp\b(1), \bgetconf\b(1), \bpr\b(1)]"
73 ;
74 
75 #define RUBOUT	0177
76 
77 /* control flags */
78 #define B_FLAG		(1<<0)
79 #define E_FLAG		(1<<1)
80 #define F_FLAG		(1<<2)
81 #define N_FLAG		(1<<3)
82 #define S_FLAG		(1<<4)
83 #define T_FLAG		(1<<5)
84 #define U_FLAG		(1<<6)
85 #define V_FLAG		(1<<7)
86 #define D_FLAG		(1<<8)
87 #define d_FLAG		(1<<9)
88 
89 /* character types */
90 #define T_ENDBUF	1
91 #define T_CONTROL	2
92 #define T_NEWLINE	3
93 #define T_EIGHTBIT	4
94 #define T_CNTL8BIT	5
95 
96 #define printof(c)	((c)^0100)
97 
98 /*
99  * called for any special output processing
100  */
101 
102 static int
103 vcat(register char* states, Sfio_t *fdin, Sfio_t *fdout, int flags)
104 {
105 	register unsigned char*	cp;
106 	register unsigned char*	cpold;
107 	register int		n;
108 	register int		m;
109 	register int		line = 1;
110 	register unsigned char*	endbuff;
111 	unsigned char*		inbuff;
112 	int			printdefer = (flags&(B_FLAG|N_FLAG));
113 	int			lastchar;
114 
115 	unsigned char		meta[4];
116 
117 	meta[0] = 'M';
118 	meta[1] = '-';
119 	for (;;)
120 	{
121 		/* read in a buffer full */
122 		if (!(inbuff = (unsigned char*)sfreserve(fdin, SF_UNBOUND, 0)))
123 			return sfvalue(fdin) ? -1 : 0;
124 		if ((n = sfvalue(fdin)) <= 0)
125 			return n;
126 		cp = inbuff;
127 		lastchar = *(endbuff = cp + --n);
128 		*endbuff = 0;
129 		if (printdefer)
130 		{
131 			if (states[*cp]!=T_NEWLINE || !(flags&B_FLAG))
132 				sfprintf(fdout,"%6d\t",line);
133 			printdefer = 0;
134 		}
135 		while (endbuff)
136 		{
137 			cpold = cp;
138 			/* skip over printable characters */
139 			if (mbwide())
140 				while ((n = (m = mbsize(cp)) < 2 ? states[*cp++] : (cp += m, states['a'])) == 0);
141 			else
142 				while ((n = states[*cp++]) == 0);
143 			if (n==T_ENDBUF)
144 			{
145 				if (cp>endbuff)
146 				{
147 					if (!(n = states[lastchar]))
148 					{
149 						*endbuff = lastchar;
150 						cp++;
151 					}
152 					else
153 					{
154 						if (--cp > cpold)
155 							sfwrite(fdout,(char*)cpold,cp-cpold);
156 						if (endbuff==inbuff)
157 							*++endbuff = 0;
158 						cp = cpold = endbuff;
159 						cp[-1] = lastchar;
160 						if (n==T_ENDBUF)
161 							n = T_CONTROL;
162 
163 					}
164 					endbuff = 0;
165 				}
166 				else n = T_CONTROL;
167 			}
168 			if (--cp>cpold)
169 				sfwrite(fdout,(char*)cpold,cp-cpold);
170 			switch(n)
171 			{
172 				case T_CNTL8BIT:
173 					meta[2] = '^';
174 					do
175 					{
176 						n = (*cp++)&~0200;
177 						meta[3] = printof(n);
178 						sfwrite(fdout,(char*)meta,4);
179 					}
180 					while ((n=states[*cp])==T_CNTL8BIT);
181 					break;
182 				case T_EIGHTBIT:
183 					do
184 					{
185 						meta[2] = (*cp++)&~0200;
186 						sfwrite(fdout,(char*)meta,3);
187 					}
188 					while ((n=states[*cp])==T_EIGHTBIT);
189 					break;
190 				case T_CONTROL:
191 					do
192 					{
193 						n = *cp++;
194 						sfputc(fdout,'^');
195 						sfputc(fdout,printof(n));
196 					}
197 					while ((n=states[*cp])==T_CONTROL);
198 					break;
199 				case T_NEWLINE:
200 					if (flags&S_FLAG)
201 					{
202 						while (states[*++cp]==T_NEWLINE)
203 							line++;
204 						cp--;
205 					}
206 					do
207 					{
208 						cp++;
209 						if (flags&E_FLAG)
210 							sfputc(fdout,'$');
211 						sfputc(fdout,'\n');
212 						if (!(flags&(N_FLAG|B_FLAG)))
213 							continue;
214 						line++;
215 						if (cp < endbuff)
216 							sfprintf(fdout,"%6d\t",line);
217 						else printdefer = 1;
218 					}
219 					while (states[*cp]==T_NEWLINE);
220 					break;
221 			}
222 		}
223 	}
224 }
225 
226 int
227 b_cat(int argc, char** argv, void* context)
228 {
229 	register int		n;
230 	register int		flags = 0;
231 	register char*		cp;
232 	register Sfio_t*	fp;
233 	char*			mode;
234 	int			att;
235 	int			dovcat=0;
236 	char			states[UCHAR_MAX+1];
237 
238 	NoP(argc);
239 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
240 	att = !strcmp(astconf("UNIVERSE", NiL, NiL), "att");
241 	mode = "r";
242 	for (;;)
243 	{
244 		switch (optget(argv, usage))
245 		{
246 		case 'A':
247 			flags |= T_FLAG|E_FLAG|V_FLAG;
248 			continue;
249 		case 'B':
250 			flags |= S_FLAG;
251 			continue;
252 		case 'b':
253 			flags |= B_FLAG;
254 			continue;
255 		case 'E':
256 			flags |= E_FLAG;
257 			continue;
258 		case 'e':
259 			flags |= E_FLAG|V_FLAG;
260 			continue;
261 		case 'n':
262 			flags |= N_FLAG;
263 			continue;
264 		case 's':
265 			flags |= att ? F_FLAG : S_FLAG;
266 			continue;
267 		case 'S':
268 			flags |= F_FLAG;
269 			continue;
270 		case 'T':
271 			flags |= T_FLAG;
272 			continue;
273 		case 't':
274 			flags |= T_FLAG|V_FLAG;
275 			continue;
276 		case 'u':
277 			flags |= U_FLAG;
278 			continue;
279 		case 'v':
280 			flags |= V_FLAG;
281 			continue;
282 		case 'd':
283 			mode = "rt";
284 			continue;
285 		case 'D':
286 			flags |= d_FLAG;
287 			continue;
288 		case ':':
289 			error(2, "%s", opt_info.arg);
290 			break;
291 		case '?':
292 			error(ERROR_usage(2), "%s", opt_info.arg);
293 			break;
294 		}
295 		break;
296 	}
297 	argv += opt_info.index;
298 	if (error_info.errors)
299 		error(ERROR_usage(2), "%s", optusage(NiL));
300 	memset(states, 0, sizeof(states));
301 	if (flags&V_FLAG)
302 	{
303 		memset(states, T_CONTROL, ' ');
304 		states[RUBOUT] = T_CONTROL;
305 		memset(states+0200, T_EIGHTBIT, 0200);
306 		memset(states+0200, T_CNTL8BIT, ' ');
307 		states[RUBOUT|0200] = T_CNTL8BIT;
308 		states['\n'] = 0;
309 	}
310 	if (flags&T_FLAG)
311 		states['\t'] = T_CONTROL;
312 	states[0] = T_ENDBUF;
313 	if (att)
314 	{
315 		if (flags&V_FLAG)
316 		{
317 			states['\n'|0200] = T_EIGHTBIT;
318 			if (!(flags&T_FLAG))
319 			{
320 				states['\t'] = states['\f'] = 0;
321 				states['\t'|0200] = states['\f'|0200] = T_EIGHTBIT;
322 			}
323 		}
324 	}
325 	else if (flags)
326 	{
327 		if (!(flags&T_FLAG))
328 			states['\t'] = 0;
329 	}
330 	if (flags&(V_FLAG|T_FLAG|N_FLAG|E_FLAG|B_FLAG))
331 	{
332 		states['\n'] = T_NEWLINE;
333 		dovcat = 1;
334 	}
335 	if (flags&B_FLAG)
336 		flags |= S_FLAG;
337 	if (flags&d_FLAG)
338 		sfopen(sfstdout, NiL, "wt");
339 	if (cp = *argv)
340 		argv++;
341 	do
342 	{
343 		if (!cp || streq(cp,"-"))
344 		{
345 			fp = sfstdin;
346 			if (flags&D_FLAG)
347 				sfopen(fp, NiL, mode);
348 		}
349 		else if (!(fp = sfopen(NiL, cp, mode)))
350 		{
351 			if (!(flags&F_FLAG))
352 				error(ERROR_system(0), "%s: cannot open", cp);
353 			error_info.errors = 1;
354 			continue;
355 		}
356 		if (flags&U_FLAG)
357 			sfsetbuf(fp, (void*)fp, -1);
358 		if (dovcat)
359 			n = vcat(states, fp, sfstdout, flags);
360 		else if (sfmove(fp, sfstdout, SF_UNBOUND, -1) >= 0 && sfeof(fp))
361 			n = 0;
362 		else
363 			n = -1;
364 		if (fp != sfstdin)
365 			sfclose(fp);
366 		if (n < 0 && errno != EPIPE)
367 		{
368 			if (cp)
369 				error(ERROR_system(0), "%s: read error", cp);
370 			else
371 				error(ERROR_system(0), "read error");
372 		}
373 		if (sferror(sfstdout))
374 			break;
375 	} while (cp = *argv++);
376 	if (sfsync(sfstdout))
377 		error(ERROR_system(0), "write error");
378 	if (flags&d_FLAG)
379 		sfopen(sfstdout, NiL, "w");
380 	return error_info.errors;
381 }
382