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