xref: /titanic_50/usr/src/cmd/sh/echo.c (revision e07d9cb85217949d497b02d7211de8a197d2f2eb)
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 
23 /*
24  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 /*
35  *	UNIX shell
36  */
37 #include	"defs.h"
38 
39 #define	exit(a)	flushb(); return (a)
40 
41 extern int exitval;
42 
43 int
44 echo(int argc, unsigned char **argv)
45 {
46 	unsigned char	*cp;
47 	int	i, wd;
48 	int	nflg = 0;
49 	int	j;
50 	int	len;
51 	wchar_t	wc;
52 
53 #ifdef	_iBCS2		/* SCO compatibility support */
54 	struct namnod   *sysv3;
55 	int	do_sysv3 = 0;
56 
57 	sysv3 = findnam("SYSV3");
58 	if (sysv3 && (sysv3->namflg & (N_EXPORT | N_ENVNAM)))
59 		do_sysv3 = 1;
60 
61 	/* Do the -n parsing if sysv3 is set or if ucb_builtsin is set */
62 	if (ucb_builtins && !do_sysv3) {
63 #else
64 	if (ucb_builtins) {
65 #endif /* _iBCS2 */
66 
67 		nflg = 0;
68 		if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'n') {
69 			nflg++;
70 			argc--;
71 			argv++;
72 		}
73 
74 		for (i = 1; i < argc; i++) {
75 			sigchk();
76 
77 			for (cp = argv[i]; *cp; cp++) {
78 				prc_buff(*cp);
79 			}
80 
81 			if (i < argc-1)
82 				prc_buff(' ');
83 		}
84 
85 		if (nflg == 0)
86 			prc_buff('\n');
87 		exit(0);
88 	} else {
89 		if (--argc == 0) {
90 			prc_buff('\n');
91 			exit(0);
92 		}
93 #ifdef  _iBCS2
94 		if (do_sysv3) {
95 			if (argc > 1 && argv[1][0] == '-' &&
96 					argv[1][1] == 'n') {
97 				nflg++;
98 				/* Step past the -n */
99 				argc--;
100 				argv++;
101 			}
102 		}
103 #endif /* _iBCS2 */
104 
105 		for (i = 1; i <= argc; i++) {
106 			sigchk();
107 			for (cp = argv[i]; *cp; cp++) {
108 				if ((len = mbtowc(&wc, (char *)cp,
109 						MB_LEN_MAX)) <= 0) {
110 					prc_buff(*cp);
111 					continue;
112 				}
113 
114 				if (wc == '\\') {
115 					switch (*++cp) {
116 					case 'b':
117 						prc_buff('\b');
118 						continue;
119 					case 'c':
120 						exit(0);
121 
122 					case 'f':
123 						prc_buff('\f');
124 						continue;
125 
126 					case 'n':
127 						prc_buff('\n');
128 						continue;
129 
130 					case 'r':
131 						prc_buff('\r');
132 						continue;
133 
134 					case 't':
135 						prc_buff('\t');
136 						continue;
137 
138 					case 'v':
139 						prc_buff('\v');
140 						continue;
141 
142 					case '\\':
143 						prc_buff('\\');
144 						continue;
145 					case '0':
146 						j = wd = 0;
147 						while ((*++cp >= '0' &&
148 						*cp <= '7') && j++ < 3) {
149 							wd <<= 3;
150 							wd |= (*cp - '0');
151 						}
152 						prc_buff(wd);
153 						--cp;
154 						continue;
155 
156 					default:
157 						cp--;
158 					}
159 					prc_buff(*cp);
160 					continue;
161 				} else {
162 					for (; len > 0; len--)
163 						prc_buff(*cp++);
164 					cp--;
165 					continue;
166 				}
167 			}
168 #ifdef	_iBCS2
169 			/* Don't do if don't want newlines & out of args */
170 			if (!(nflg && i == argc))
171 #endif /* _iBCS2 */
172 				prc_buff(i == argc? '\n': ' ');
173 		}
174 		exit(0);
175 	}
176 }
177