xref: /onnv-gate/usr/src/cmd/sh/echo.c (revision 12836:0d0733997434)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
512704SAjaykumar.Venkatesulu@Sun.COM  * Common Development and Distribution License (the "License").
612704SAjaykumar.Venkatesulu@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21527Schin 
22527Schin /*
2312704SAjaykumar.Venkatesulu@Sun.COM  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24527Schin  */
25527Schin 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  *	UNIX shell
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate #include	"defs.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #define	exit(a)	flushb(); return (a)
370Sstevel@tonic-gate 
380Sstevel@tonic-gate extern int exitval;
390Sstevel@tonic-gate 
40527Schin int
echo(int argc,unsigned char ** argv)41527Schin echo(int argc, unsigned char **argv)
420Sstevel@tonic-gate {
43527Schin 	unsigned char	*cp;
44527Schin 	int	i, wd;
450Sstevel@tonic-gate 	int	nflg = 0;
460Sstevel@tonic-gate 	int	j;
470Sstevel@tonic-gate 	int	len;
480Sstevel@tonic-gate 	wchar_t	wc;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	if (ucb_builtins) {
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 		nflg = 0;
5312704SAjaykumar.Venkatesulu@Sun.COM 		if (argc > 1 && argv[1][0] == '-' &&
5412704SAjaykumar.Venkatesulu@Sun.COM 		    argv[1][1] == 'n' && !argv[1][2]) {
550Sstevel@tonic-gate 			nflg++;
560Sstevel@tonic-gate 			argc--;
570Sstevel@tonic-gate 			argv++;
580Sstevel@tonic-gate 		}
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 		for (i = 1; i < argc; i++) {
610Sstevel@tonic-gate 			sigchk();
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 			for (cp = argv[i]; *cp; cp++) {
640Sstevel@tonic-gate 				prc_buff(*cp);
650Sstevel@tonic-gate 			}
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 			if (i < argc-1)
680Sstevel@tonic-gate 				prc_buff(' ');
690Sstevel@tonic-gate 		}
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 		if (nflg == 0)
720Sstevel@tonic-gate 			prc_buff('\n');
730Sstevel@tonic-gate 		exit(0);
740Sstevel@tonic-gate 	} else {
750Sstevel@tonic-gate 		if (--argc == 0) {
760Sstevel@tonic-gate 			prc_buff('\n');
770Sstevel@tonic-gate 			exit(0);
780Sstevel@tonic-gate 		}
790Sstevel@tonic-gate 
80527Schin 		for (i = 1; i <= argc; i++) {
810Sstevel@tonic-gate 			sigchk();
820Sstevel@tonic-gate 			for (cp = argv[i]; *cp; cp++) {
830Sstevel@tonic-gate 				if ((len = mbtowc(&wc, (char *)cp,
8412704SAjaykumar.Venkatesulu@Sun.COM 				    MB_LEN_MAX)) <= 0) {
850Sstevel@tonic-gate 					prc_buff(*cp);
860Sstevel@tonic-gate 					continue;
870Sstevel@tonic-gate 				}
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 				if (wc == '\\') {
900Sstevel@tonic-gate 					switch (*++cp) {
910Sstevel@tonic-gate 					case 'b':
920Sstevel@tonic-gate 						prc_buff('\b');
930Sstevel@tonic-gate 						continue;
940Sstevel@tonic-gate 					case 'c':
950Sstevel@tonic-gate 						exit(0);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 					case 'f':
980Sstevel@tonic-gate 						prc_buff('\f');
990Sstevel@tonic-gate 						continue;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 					case 'n':
1020Sstevel@tonic-gate 						prc_buff('\n');
1030Sstevel@tonic-gate 						continue;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 					case 'r':
1060Sstevel@tonic-gate 						prc_buff('\r');
1070Sstevel@tonic-gate 						continue;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 					case 't':
1100Sstevel@tonic-gate 						prc_buff('\t');
1110Sstevel@tonic-gate 						continue;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 					case 'v':
1140Sstevel@tonic-gate 						prc_buff('\v');
1150Sstevel@tonic-gate 						continue;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 					case '\\':
1180Sstevel@tonic-gate 						prc_buff('\\');
1190Sstevel@tonic-gate 						continue;
1200Sstevel@tonic-gate 					case '0':
1210Sstevel@tonic-gate 						j = wd = 0;
1220Sstevel@tonic-gate 						while ((*++cp >= '0' &&
12312704SAjaykumar.Venkatesulu@Sun.COM 						    *cp <= '7') && j++ < 3) {
1240Sstevel@tonic-gate 							wd <<= 3;
1250Sstevel@tonic-gate 							wd |= (*cp - '0');
1260Sstevel@tonic-gate 						}
1270Sstevel@tonic-gate 						prc_buff(wd);
1280Sstevel@tonic-gate 						--cp;
1290Sstevel@tonic-gate 						continue;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 					default:
1320Sstevel@tonic-gate 						cp--;
1330Sstevel@tonic-gate 					}
1340Sstevel@tonic-gate 					prc_buff(*cp);
1350Sstevel@tonic-gate 					continue;
1360Sstevel@tonic-gate 				} else {
1370Sstevel@tonic-gate 					for (; len > 0; len--)
1380Sstevel@tonic-gate 						prc_buff(*cp++);
1390Sstevel@tonic-gate 					cp--;
1400Sstevel@tonic-gate 					continue;
1410Sstevel@tonic-gate 				}
1420Sstevel@tonic-gate 			}
143*12836Srich.burridge@oracle.com 			prc_buff(i == argc? '\n': ' ');
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 		exit(0);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate }
148