2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: cat.c /main/3 1995/11/01 19:03:07 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS PROPRIETARY SOURCE CODE LICENSED BY *
31 * Copyright (c) 1995 AT&T Corp. *
32 * All Rights Reserved *
34 * This software is licensed by AT&T Corp. *
35 * under the terms and conditions of the license in *
36 * http://www.research.att.com/orgs/ssr/book/reuse *
38 * This software was created by the *
39 * Software Engineering Research Department *
40 * AT&T Bell Laboratories *
42 * For further information contact *
43 * gsf@research.att.com *
45 ***************************************************************/
47 /* : : generated by proto : : */
49 #if !defined(__PROTO__)
50 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
51 #if defined(__cplusplus)
52 #define __MANGLE__ "C"
57 #define __PROTO__(x) x
59 #define __PARAM__(n,o) n
60 #if !defined(__STDC__) && !defined(__cplusplus)
61 #if !defined(c_plusplus)
72 #define __PROTO__(x) ()
73 #define __OTORP__(x) x
74 #define __PARAM__(n,o) o
82 #if defined(__cplusplus) || defined(c_plusplus)
83 #define __VARARG__ ...
87 #if defined(__STDARG__)
88 #define __VA_START__(p,a) va_start(p,a)
90 #define __VA_START__(p,a) va_start(p)
93 static const char id[] = "\n@(#)cat (AT&T Bell Laboratories) 05/09/95\0\n";
100 #define B_FLAG (1<<0)
101 #define E_FLAG (1<<1)
102 #define F_FLAG (1<<2)
103 #define N_FLAG (1<<3)
104 #define S_FLAG (1<<4)
105 #define T_FLAG (1<<5)
106 #define U_FLAG (1<<6)
107 #define V_FLAG (1<<7)
109 /* character types */
116 #define printof(c) ((c)^0100)
118 static char states[UCHAR_MAX+1];
121 * called for any special output processing
125 vcat __PARAM__((Sfio_t *fdin, Sfio_t *fdout, int flags), (fdin, fdout, flags)) __OTORP__(Sfio_t *fdin; Sfio_t *fdout; int flags;){
126 register unsigned char* cp;
127 register unsigned char* cpold;
129 register int line = 1;
130 register unsigned char* endbuff;
131 unsigned char* inbuff;
132 int printdefer = (flags&(B_FLAG|N_FLAG));
135 static unsigned char meta[4] = "M-";
139 /* read in a buffer full */
140 if (!(inbuff = (unsigned char*)sfreserve(fdin, SF_UNBOUND, 0)))
141 return(sfslen() ? -1 : 0);
142 if ((n = sfslen()) <= 0)
145 lastchar = *(endbuff = cp + --n);
149 if (states[*cp]!=T_NEWLINE || !(flags&B_FLAG))
150 sfprintf(fdout,"%6d\t",line);
156 /* skip over ASCII characters */
157 while ((n = states[*cp++]) == 0);
162 if (!(n = states[lastchar]))
170 sfwrite(fdout,(char*)cpold,cp-cpold);
173 cp = cpold = endbuff;
184 sfwrite(fdout,(char*)cpold,cp-cpold);
192 meta[3] = printof(n);
193 sfwrite(fdout,(char*)meta,4);
195 while ((n=states[*cp])==T_CNTL8BIT);
200 meta[2] = (*cp++)&~0200;
201 sfwrite(fdout,(char*)meta,3);
203 while ((n=states[*cp])==T_EIGHTBIT);
210 sfputc(fdout,printof(n));
212 while ((n=states[*cp])==T_CONTROL);
217 while (states[*++cp]==T_NEWLINE)
227 if (!(flags&(N_FLAG|B_FLAG)))
231 sfprintf(fdout,"%6d\t",line);
234 while (states[*cp]==T_NEWLINE);
242 b_cat __PARAM__((int argc, char** argv), (argc, argv)) __OTORP__(int argc; char** argv;){
244 register int flags = 0;
252 att = !strcmp(astconf("UNIVERSE", NiL, NiL), "att");
253 while (n = optget(argv, "benstuv [file...] ")) switch (n)
265 flags |= att ? F_FLAG : S_FLAG;
277 error(2, opt_info.arg);
280 error(ERROR_usage(2), opt_info.arg);
283 argv += opt_info.index;
284 if (error_info.errors)
285 error(ERROR_usage(2), optusage(NiL));
288 memset(states, T_CONTROL, ' ');
289 states[RUBOUT] = T_CONTROL;
290 memset(states+0200, T_EIGHTBIT, 0200);
291 memset(states+0200, T_CNTL8BIT, ' ');
292 states[RUBOUT|0200] = T_CNTL8BIT;
295 states[0] = T_ENDBUF;
300 states['\n'|0200] = T_EIGHTBIT;
303 states['\t'] = states['\f'] = 0;
304 states['\t'|0200] = states['\f'|0200] = T_EIGHTBIT;
316 flags &= ~(N_FLAG|E_FLAG);
317 flags |= (V_FLAG|S_FLAG);
321 flags &= ~(B_FLAG|S_FLAG|E_FLAG);
325 states['\n'] = T_NEWLINE;
330 if (!cp || streq(cp,"-"))
332 else if (!(fp = sfopen(NiL, cp, "r")))
335 error(ERROR_system(0), "%s: cannot open", cp);
336 error_info.errors = 1;
340 sfsetbuf(fp, (__V_*)fp, -1);
341 if (flags&V_FLAG) n = vcat(fp, sfstdout, flags);
342 else if ((n = sfmove(fp, sfstdout, SF_UNBOUND, -1)) >= 0 && (!sfeof(fp) || sferror(sfstdout)))
348 if (cp) error(ERROR_system(0), "%s: cannot copy", cp);
349 else error(ERROR_system(0), "cannot copy");
350 if (sferror(sfstdout)) break;
352 } while (cp = *argv++);
353 return(error_info.errors);