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