xref: /illumos-gate/usr/src/cmd/sh/echo.c (revision 965005c8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*965005c8Schin 
23*965005c8Schin /*
24*965005c8Schin  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
25*965005c8Schin  * Use is subject to license terms.
26*965005c8Schin  */
27*965005c8Schin 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  *	UNIX shell
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate #include	"defs.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #define	exit(a)	flushb(); return (a)
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate extern int exitval;
427c478bd9Sstevel@tonic-gate 
43*965005c8Schin int
44*965005c8Schin echo(int argc, unsigned char **argv)
457c478bd9Sstevel@tonic-gate {
46*965005c8Schin 	unsigned char	*cp;
47*965005c8Schin 	int	i, wd;
487c478bd9Sstevel@tonic-gate 	int	nflg = 0;
497c478bd9Sstevel@tonic-gate 	int	j;
507c478bd9Sstevel@tonic-gate 	int	len;
517c478bd9Sstevel@tonic-gate 	wchar_t	wc;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #ifdef	_iBCS2		/* SCO compatibility support */
547c478bd9Sstevel@tonic-gate 	struct namnod   *sysv3;
557c478bd9Sstevel@tonic-gate 	int	do_sysv3 = 0;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	sysv3 = findnam("SYSV3");
587c478bd9Sstevel@tonic-gate 	if (sysv3 && (sysv3->namflg & (N_EXPORT | N_ENVNAM)))
597c478bd9Sstevel@tonic-gate 		do_sysv3 = 1;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	/* Do the -n parsing if sysv3 is set or if ucb_builtsin is set */
627c478bd9Sstevel@tonic-gate 	if (ucb_builtins && !do_sysv3) {
637c478bd9Sstevel@tonic-gate #else
647c478bd9Sstevel@tonic-gate 	if (ucb_builtins) {
657c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 		nflg = 0;
687c478bd9Sstevel@tonic-gate 		if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'n') {
697c478bd9Sstevel@tonic-gate 			nflg++;
707c478bd9Sstevel@tonic-gate 			argc--;
717c478bd9Sstevel@tonic-gate 			argv++;
727c478bd9Sstevel@tonic-gate 		}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 		for (i = 1; i < argc; i++) {
757c478bd9Sstevel@tonic-gate 			sigchk();
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 			for (cp = argv[i]; *cp; cp++) {
787c478bd9Sstevel@tonic-gate 				prc_buff(*cp);
797c478bd9Sstevel@tonic-gate 			}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 			if (i < argc-1)
827c478bd9Sstevel@tonic-gate 				prc_buff(' ');
837c478bd9Sstevel@tonic-gate 		}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 		if (nflg == 0)
867c478bd9Sstevel@tonic-gate 			prc_buff('\n');
877c478bd9Sstevel@tonic-gate 		exit(0);
887c478bd9Sstevel@tonic-gate 	} else {
897c478bd9Sstevel@tonic-gate 		if (--argc == 0) {
907c478bd9Sstevel@tonic-gate 			prc_buff('\n');
917c478bd9Sstevel@tonic-gate 			exit(0);
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate #ifdef  _iBCS2
947c478bd9Sstevel@tonic-gate 		if (do_sysv3) {
957c478bd9Sstevel@tonic-gate 			if (argc > 1 && argv[1][0] == '-' &&
967c478bd9Sstevel@tonic-gate 					argv[1][1] == 'n') {
977c478bd9Sstevel@tonic-gate 				nflg++;
987c478bd9Sstevel@tonic-gate 				/* Step past the -n */
997c478bd9Sstevel@tonic-gate 				argc--;
1007c478bd9Sstevel@tonic-gate 				argv++;
1017c478bd9Sstevel@tonic-gate 			}
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */
1047c478bd9Sstevel@tonic-gate 
105*965005c8Schin 		for (i = 1; i <= argc; i++) {
1067c478bd9Sstevel@tonic-gate 			sigchk();
1077c478bd9Sstevel@tonic-gate 			for (cp = argv[i]; *cp; cp++) {
1087c478bd9Sstevel@tonic-gate 				if ((len = mbtowc(&wc, (char *)cp,
1097c478bd9Sstevel@tonic-gate 						MB_LEN_MAX)) <= 0) {
1107c478bd9Sstevel@tonic-gate 					prc_buff(*cp);
1117c478bd9Sstevel@tonic-gate 					continue;
1127c478bd9Sstevel@tonic-gate 				}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 				if (wc == '\\') {
1157c478bd9Sstevel@tonic-gate 					switch (*++cp) {
1167c478bd9Sstevel@tonic-gate 					case 'b':
1177c478bd9Sstevel@tonic-gate 						prc_buff('\b');
1187c478bd9Sstevel@tonic-gate 						continue;
1197c478bd9Sstevel@tonic-gate 					case 'c':
1207c478bd9Sstevel@tonic-gate 						exit(0);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 					case 'f':
1237c478bd9Sstevel@tonic-gate 						prc_buff('\f');
1247c478bd9Sstevel@tonic-gate 						continue;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 					case 'n':
1277c478bd9Sstevel@tonic-gate 						prc_buff('\n');
1287c478bd9Sstevel@tonic-gate 						continue;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 					case 'r':
1317c478bd9Sstevel@tonic-gate 						prc_buff('\r');
1327c478bd9Sstevel@tonic-gate 						continue;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 					case 't':
1357c478bd9Sstevel@tonic-gate 						prc_buff('\t');
1367c478bd9Sstevel@tonic-gate 						continue;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 					case 'v':
1397c478bd9Sstevel@tonic-gate 						prc_buff('\v');
1407c478bd9Sstevel@tonic-gate 						continue;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 					case '\\':
1437c478bd9Sstevel@tonic-gate 						prc_buff('\\');
1447c478bd9Sstevel@tonic-gate 						continue;
1457c478bd9Sstevel@tonic-gate 					case '0':
1467c478bd9Sstevel@tonic-gate 						j = wd = 0;
1477c478bd9Sstevel@tonic-gate 						while ((*++cp >= '0' &&
1487c478bd9Sstevel@tonic-gate 						*cp <= '7') && j++ < 3) {
1497c478bd9Sstevel@tonic-gate 							wd <<= 3;
1507c478bd9Sstevel@tonic-gate 							wd |= (*cp - '0');
1517c478bd9Sstevel@tonic-gate 						}
1527c478bd9Sstevel@tonic-gate 						prc_buff(wd);
1537c478bd9Sstevel@tonic-gate 						--cp;
1547c478bd9Sstevel@tonic-gate 						continue;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 					default:
1577c478bd9Sstevel@tonic-gate 						cp--;
1587c478bd9Sstevel@tonic-gate 					}
1597c478bd9Sstevel@tonic-gate 					prc_buff(*cp);
1607c478bd9Sstevel@tonic-gate 					continue;
1617c478bd9Sstevel@tonic-gate 				} else {
1627c478bd9Sstevel@tonic-gate 					for (; len > 0; len--)
1637c478bd9Sstevel@tonic-gate 						prc_buff(*cp++);
1647c478bd9Sstevel@tonic-gate 					cp--;
1657c478bd9Sstevel@tonic-gate 					continue;
1667c478bd9Sstevel@tonic-gate 				}
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
1697c478bd9Sstevel@tonic-gate 			/* Don't do if don't want newlines & out of args */
1707c478bd9Sstevel@tonic-gate 			if (!(nflg && i == argc))
1717c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */
1727c478bd9Sstevel@tonic-gate 				prc_buff(i == argc? '\n': ' ');
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 		exit(0);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate }
177