xref: /titanic_52/usr/src/cmd/sh/echo.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*	Portions Copyright (c) 1988, Sun Microsystems, Inc.	*/
27*7c478bd9Sstevel@tonic-gate /*	All Rights Reserved.					*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  *	UNIX shell
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate #include	"defs.h"
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	exit(a)	flushb(); return (a)
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate extern int exitval;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate echo(argc, argv)
41*7c478bd9Sstevel@tonic-gate unsigned char **argv;
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	register unsigned char	*cp;
44*7c478bd9Sstevel@tonic-gate 	register int	i, wd;
45*7c478bd9Sstevel@tonic-gate 	int	nflg = 0;
46*7c478bd9Sstevel@tonic-gate 	int	j;
47*7c478bd9Sstevel@tonic-gate 	int	len;
48*7c478bd9Sstevel@tonic-gate 	wchar_t	wc;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #ifdef	_iBCS2		/* SCO compatibility support */
51*7c478bd9Sstevel@tonic-gate 	struct namnod   *sysv3;
52*7c478bd9Sstevel@tonic-gate 	int	do_sysv3 = 0;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	sysv3 = findnam("SYSV3");
55*7c478bd9Sstevel@tonic-gate 	if (sysv3 && (sysv3->namflg & (N_EXPORT | N_ENVNAM)))
56*7c478bd9Sstevel@tonic-gate 		do_sysv3 = 1;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	/* Do the -n parsing if sysv3 is set or if ucb_builtsin is set */
59*7c478bd9Sstevel@tonic-gate 	if (ucb_builtins && !do_sysv3) {
60*7c478bd9Sstevel@tonic-gate #else
61*7c478bd9Sstevel@tonic-gate 	if (ucb_builtins) {
62*7c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 		nflg = 0;
65*7c478bd9Sstevel@tonic-gate 		if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'n') {
66*7c478bd9Sstevel@tonic-gate 			nflg++;
67*7c478bd9Sstevel@tonic-gate 			argc--;
68*7c478bd9Sstevel@tonic-gate 			argv++;
69*7c478bd9Sstevel@tonic-gate 		}
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 		for (i = 1; i < argc; i++) {
72*7c478bd9Sstevel@tonic-gate 			sigchk();
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 			for (cp = argv[i]; *cp; cp++) {
75*7c478bd9Sstevel@tonic-gate 				prc_buff(*cp);
76*7c478bd9Sstevel@tonic-gate 			}
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 			if (i < argc-1)
79*7c478bd9Sstevel@tonic-gate 				prc_buff(' ');
80*7c478bd9Sstevel@tonic-gate 		}
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 		if (nflg == 0)
83*7c478bd9Sstevel@tonic-gate 			prc_buff('\n');
84*7c478bd9Sstevel@tonic-gate 		exit(0);
85*7c478bd9Sstevel@tonic-gate 	} else {
86*7c478bd9Sstevel@tonic-gate 		if (--argc == 0) {
87*7c478bd9Sstevel@tonic-gate 			prc_buff('\n');
88*7c478bd9Sstevel@tonic-gate 			exit(0);
89*7c478bd9Sstevel@tonic-gate 		}
90*7c478bd9Sstevel@tonic-gate #ifdef  _iBCS2
91*7c478bd9Sstevel@tonic-gate 		if (do_sysv3) {
92*7c478bd9Sstevel@tonic-gate 			if (argc > 1 && argv[1][0] == '-' &&
93*7c478bd9Sstevel@tonic-gate 					argv[1][1] == 'n') {
94*7c478bd9Sstevel@tonic-gate 				nflg++;
95*7c478bd9Sstevel@tonic-gate 				/* Step past the -n */
96*7c478bd9Sstevel@tonic-gate 				argc--;
97*7c478bd9Sstevel@tonic-gate 				argv++;
98*7c478bd9Sstevel@tonic-gate 			}
99*7c478bd9Sstevel@tonic-gate 		}
100*7c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 		for (i = 1; i <= argc; i++)
103*7c478bd9Sstevel@tonic-gate 		{
104*7c478bd9Sstevel@tonic-gate 			sigchk();
105*7c478bd9Sstevel@tonic-gate 			for (cp = argv[i]; *cp; cp++) {
106*7c478bd9Sstevel@tonic-gate 				if ((len = mbtowc(&wc, (char *)cp,
107*7c478bd9Sstevel@tonic-gate 						MB_LEN_MAX)) <= 0) {
108*7c478bd9Sstevel@tonic-gate 					prc_buff(*cp);
109*7c478bd9Sstevel@tonic-gate 					continue;
110*7c478bd9Sstevel@tonic-gate 				}
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 				if (wc == '\\') {
113*7c478bd9Sstevel@tonic-gate 					switch (*++cp) {
114*7c478bd9Sstevel@tonic-gate 					case 'b':
115*7c478bd9Sstevel@tonic-gate 						prc_buff('\b');
116*7c478bd9Sstevel@tonic-gate 						continue;
117*7c478bd9Sstevel@tonic-gate 					case 'c':
118*7c478bd9Sstevel@tonic-gate 						exit(0);
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 					case 'f':
121*7c478bd9Sstevel@tonic-gate 						prc_buff('\f');
122*7c478bd9Sstevel@tonic-gate 						continue;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 					case 'n':
125*7c478bd9Sstevel@tonic-gate 						prc_buff('\n');
126*7c478bd9Sstevel@tonic-gate 						continue;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 					case 'r':
129*7c478bd9Sstevel@tonic-gate 						prc_buff('\r');
130*7c478bd9Sstevel@tonic-gate 						continue;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 					case 't':
133*7c478bd9Sstevel@tonic-gate 						prc_buff('\t');
134*7c478bd9Sstevel@tonic-gate 						continue;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 					case 'v':
137*7c478bd9Sstevel@tonic-gate 						prc_buff('\v');
138*7c478bd9Sstevel@tonic-gate 						continue;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 					case '\\':
141*7c478bd9Sstevel@tonic-gate 						prc_buff('\\');
142*7c478bd9Sstevel@tonic-gate 						continue;
143*7c478bd9Sstevel@tonic-gate 					case '0':
144*7c478bd9Sstevel@tonic-gate 						j = wd = 0;
145*7c478bd9Sstevel@tonic-gate 						while ((*++cp >= '0' &&
146*7c478bd9Sstevel@tonic-gate 						*cp <= '7') && j++ < 3) {
147*7c478bd9Sstevel@tonic-gate 							wd <<= 3;
148*7c478bd9Sstevel@tonic-gate 							wd |= (*cp - '0');
149*7c478bd9Sstevel@tonic-gate 						}
150*7c478bd9Sstevel@tonic-gate 						prc_buff(wd);
151*7c478bd9Sstevel@tonic-gate 						--cp;
152*7c478bd9Sstevel@tonic-gate 						continue;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 					default:
155*7c478bd9Sstevel@tonic-gate 						cp--;
156*7c478bd9Sstevel@tonic-gate 					}
157*7c478bd9Sstevel@tonic-gate 					prc_buff(*cp);
158*7c478bd9Sstevel@tonic-gate 					continue;
159*7c478bd9Sstevel@tonic-gate 				} else {
160*7c478bd9Sstevel@tonic-gate 					for (; len > 0; len--)
161*7c478bd9Sstevel@tonic-gate 						prc_buff(*cp++);
162*7c478bd9Sstevel@tonic-gate 					cp--;
163*7c478bd9Sstevel@tonic-gate 					continue;
164*7c478bd9Sstevel@tonic-gate 				}
165*7c478bd9Sstevel@tonic-gate 			}
166*7c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
167*7c478bd9Sstevel@tonic-gate 			/* Don't do if don't want newlines & out of args */
168*7c478bd9Sstevel@tonic-gate 			if (!(nflg && i == argc))
169*7c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */
170*7c478bd9Sstevel@tonic-gate 				prc_buff(i == argc? '\n': ' ');
171*7c478bd9Sstevel@tonic-gate 		}
172*7c478bd9Sstevel@tonic-gate 		exit(0);
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate }
175