xref: /minix3/bin/sh/bltin/echo.c (revision d90bee97498b3043241050f61aed100786c59df4)
1*d90bee97SLionel Sambuc /*	$NetBSD: echo.c,v 1.14 2008/10/12 01:40:37 dholland Exp $	*/
2*d90bee97SLionel Sambuc 
3*d90bee97SLionel Sambuc /*-
4*d90bee97SLionel Sambuc  * Copyright (c) 1991, 1993
5*d90bee97SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*d90bee97SLionel Sambuc  *
7*d90bee97SLionel Sambuc  * This code is derived from software contributed to Berkeley by
8*d90bee97SLionel Sambuc  * Kenneth Almquist.
9*d90bee97SLionel Sambuc  *
10*d90bee97SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*d90bee97SLionel Sambuc  * modification, are permitted provided that the following conditions
12*d90bee97SLionel Sambuc  * are met:
13*d90bee97SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*d90bee97SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*d90bee97SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*d90bee97SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*d90bee97SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*d90bee97SLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
19*d90bee97SLionel Sambuc  *    may be used to endorse or promote products derived from this software
20*d90bee97SLionel Sambuc  *    without specific prior written permission.
21*d90bee97SLionel Sambuc  *
22*d90bee97SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*d90bee97SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*d90bee97SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*d90bee97SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*d90bee97SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*d90bee97SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*d90bee97SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*d90bee97SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*d90bee97SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*d90bee97SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*d90bee97SLionel Sambuc  * SUCH DAMAGE.
33*d90bee97SLionel Sambuc  *
34*d90bee97SLionel Sambuc  *	@(#)echo.c	8.1 (Berkeley) 5/31/93
35*d90bee97SLionel Sambuc  */
36*d90bee97SLionel Sambuc 
37*d90bee97SLionel Sambuc /*
38*d90bee97SLionel Sambuc  * Echo command.
39*d90bee97SLionel Sambuc  *
40*d90bee97SLionel Sambuc  * echo is steeped in tradition - several of them!
41*d90bee97SLionel Sambuc  * netbsd has supported 'echo [-n | -e] args' in spite of -e not being
42*d90bee97SLionel Sambuc  * documented anywhere.
43*d90bee97SLionel Sambuc  * Posix requires that -n be supported, output from strings containing
44*d90bee97SLionel Sambuc  * \ is implementation defined
45*d90bee97SLionel Sambuc  * The Single Unix Spec requires that \ escapes be treated as if -e
46*d90bee97SLionel Sambuc  * were set, but that -n not be treated as an option.
47*d90bee97SLionel Sambuc  * (ksh supports 'echo [-eEn] args', but not -- so that it is actually
48*d90bee97SLionel Sambuc  * impossible to actually output '-n')
49*d90bee97SLionel Sambuc  *
50*d90bee97SLionel Sambuc  * It is suggested that 'printf "%b" "string"' be used to get \ sequences
51*d90bee97SLionel Sambuc  * expanded.  printf is now a builtin of netbsd's sh and csh.
52*d90bee97SLionel Sambuc  */
53*d90bee97SLionel Sambuc 
54*d90bee97SLionel Sambuc #include <sys/cdefs.h>
55*d90bee97SLionel Sambuc __RCSID("$NetBSD: echo.c,v 1.14 2008/10/12 01:40:37 dholland Exp $");
56*d90bee97SLionel Sambuc 
57*d90bee97SLionel Sambuc #define main echocmd
58*d90bee97SLionel Sambuc 
59*d90bee97SLionel Sambuc #include "bltin.h"
60*d90bee97SLionel Sambuc 
61*d90bee97SLionel Sambuc int
main(int argc,char ** argv)62*d90bee97SLionel Sambuc main(int argc, char **argv)
63*d90bee97SLionel Sambuc {
64*d90bee97SLionel Sambuc 	char **ap;
65*d90bee97SLionel Sambuc 	char *p;
66*d90bee97SLionel Sambuc 	char c;
67*d90bee97SLionel Sambuc 	int count;
68*d90bee97SLionel Sambuc 	int nflag = 0;
69*d90bee97SLionel Sambuc 	int eflag = 0;
70*d90bee97SLionel Sambuc 
71*d90bee97SLionel Sambuc 	ap = argv;
72*d90bee97SLionel Sambuc 	if (argc)
73*d90bee97SLionel Sambuc 		ap++;
74*d90bee97SLionel Sambuc 
75*d90bee97SLionel Sambuc 	if ((p = *ap) != NULL) {
76*d90bee97SLionel Sambuc 		if (equal(p, "-n")) {
77*d90bee97SLionel Sambuc 			nflag = 1;
78*d90bee97SLionel Sambuc 			ap++;
79*d90bee97SLionel Sambuc 		} else if (equal(p, "-e")) {
80*d90bee97SLionel Sambuc 			eflag = 1;
81*d90bee97SLionel Sambuc 			ap++;
82*d90bee97SLionel Sambuc 		}
83*d90bee97SLionel Sambuc 	}
84*d90bee97SLionel Sambuc 
85*d90bee97SLionel Sambuc 	while ((p = *ap++) != NULL) {
86*d90bee97SLionel Sambuc 		while ((c = *p++) != '\0') {
87*d90bee97SLionel Sambuc 			if (c == '\\' && eflag) {
88*d90bee97SLionel Sambuc 				switch (*p++) {
89*d90bee97SLionel Sambuc 				case 'a':  c = '\a';  break;	/* bell */
90*d90bee97SLionel Sambuc 				case 'b':  c = '\b';  break;
91*d90bee97SLionel Sambuc 				case 'c':  return 0;		/* exit */
92*d90bee97SLionel Sambuc 				case 'e':  c =  033;  break;	/* escape */
93*d90bee97SLionel Sambuc 				case 'f':  c = '\f';  break;
94*d90bee97SLionel Sambuc 				case 'n':  c = '\n';  break;
95*d90bee97SLionel Sambuc 				case 'r':  c = '\r';  break;
96*d90bee97SLionel Sambuc 				case 't':  c = '\t';  break;
97*d90bee97SLionel Sambuc 				case 'v':  c = '\v';  break;
98*d90bee97SLionel Sambuc 				case '\\':  break;		/* c = '\\' */
99*d90bee97SLionel Sambuc 				case '0':
100*d90bee97SLionel Sambuc 					c = 0;
101*d90bee97SLionel Sambuc 					count = 3;
102*d90bee97SLionel Sambuc 					while (--count >= 0 && (unsigned)(*p - '0') < 8)
103*d90bee97SLionel Sambuc 						c = (c << 3) + (*p++ - '0');
104*d90bee97SLionel Sambuc 					break;
105*d90bee97SLionel Sambuc 				default:
106*d90bee97SLionel Sambuc 					/* Output the '/' and char following */
107*d90bee97SLionel Sambuc 					p--;
108*d90bee97SLionel Sambuc 					break;
109*d90bee97SLionel Sambuc 				}
110*d90bee97SLionel Sambuc 			}
111*d90bee97SLionel Sambuc 			putchar(c);
112*d90bee97SLionel Sambuc 		}
113*d90bee97SLionel Sambuc 		if (*ap)
114*d90bee97SLionel Sambuc 			putchar(' ');
115*d90bee97SLionel Sambuc 	}
116*d90bee97SLionel Sambuc 	if (! nflag)
117*d90bee97SLionel Sambuc 		putchar('\n');
118*d90bee97SLionel Sambuc 	fflush(stdout);
119*d90bee97SLionel Sambuc 	if (ferror(stdout))
120*d90bee97SLionel Sambuc 		return 1;
121*d90bee97SLionel Sambuc 	return 0;
122*d90bee97SLionel Sambuc }
123