11.15Skre/*	$NetBSD: echo.c,v 1.15 2021/05/18 21:39:06 kre Exp $	*/
21.7Scgd
31.1Scgd/*-
41.5Sjtc * Copyright (c) 1991, 1993
51.5Sjtc *	The Regents of the University of California.  All rights reserved.
61.1Scgd *
71.1Scgd * This code is derived from software contributed to Berkeley by
81.1Scgd * Kenneth Almquist.
91.1Scgd *
101.1Scgd * Redistribution and use in source and binary forms, with or without
111.1Scgd * modification, are permitted provided that the following conditions
121.1Scgd * are met:
131.1Scgd * 1. Redistributions of source code must retain the above copyright
141.1Scgd *    notice, this list of conditions and the following disclaimer.
151.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
161.1Scgd *    notice, this list of conditions and the following disclaimer in the
171.1Scgd *    documentation and/or other materials provided with the distribution.
181.11Sagc * 3. Neither the name of the University nor the names of its contributors
191.1Scgd *    may be used to endorse or promote products derived from this software
201.1Scgd *    without specific prior written permission.
211.1Scgd *
221.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321.1Scgd * SUCH DAMAGE.
331.5Sjtc *
341.7Scgd *	@(#)echo.c	8.1 (Berkeley) 5/31/93
351.1Scgd */
361.1Scgd
371.1Scgd/*
381.1Scgd * Echo command.
391.9Schristos *
401.9Schristos * echo is steeped in tradition - several of them!
411.9Schristos * netbsd has supported 'echo [-n | -e] args' in spite of -e not being
421.9Schristos * documented anywhere.
431.9Schristos * Posix requires that -n be supported, output from strings containing
441.9Schristos * \ is implementation defined
451.9Schristos * The Single Unix Spec requires that \ escapes be treated as if -e
461.9Schristos * were set, but that -n not be treated as an option.
471.9Schristos * (ksh supports 'echo [-eEn] args', but not -- so that it is actually
481.10Swiz * impossible to actually output '-n')
491.9Schristos *
501.9Schristos * It is suggested that 'printf "%b" "string"' be used to get \ sequences
511.9Schristos * expanded.  printf is now a builtin of netbsd's sh and csh.
521.1Scgd */
531.1Scgd
541.13Slukem#include <sys/cdefs.h>
551.15Skre__RCSID("$NetBSD: echo.c,v 1.15 2021/05/18 21:39:06 kre Exp $");
561.13Slukem
571.1Scgd#define main echocmd
581.1Scgd
591.1Scgd#include "bltin.h"
601.1Scgd
611.8Schristosint
621.9Schristosmain(int argc, char **argv)
631.9Schristos{
641.12Sperry	char **ap;
651.12Sperry	char *p;
661.12Sperry	char c;
671.1Scgd	int count;
681.1Scgd	int nflag = 0;
691.1Scgd	int eflag = 0;
701.1Scgd
711.15Skre	clearerr(stdout);
721.15Skre
731.1Scgd	ap = argv;
741.1Scgd	if (argc)
751.1Scgd		ap++;
761.9Schristos
771.1Scgd	if ((p = *ap) != NULL) {
781.1Scgd		if (equal(p, "-n")) {
791.9Schristos			nflag = 1;
801.1Scgd			ap++;
811.1Scgd		} else if (equal(p, "-e")) {
821.9Schristos			eflag = 1;
831.1Scgd			ap++;
841.1Scgd		}
851.1Scgd	}
861.9Schristos
871.1Scgd	while ((p = *ap++) != NULL) {
881.1Scgd		while ((c = *p++) != '\0') {
891.1Scgd			if (c == '\\' && eflag) {
901.1Scgd				switch (*p++) {
911.9Schristos				case 'a':  c = '\a';  break;	/* bell */
921.1Scgd				case 'b':  c = '\b';  break;
931.1Scgd				case 'c':  return 0;		/* exit */
941.9Schristos				case 'e':  c =  033;  break;	/* escape */
951.1Scgd				case 'f':  c = '\f';  break;
961.1Scgd				case 'n':  c = '\n';  break;
971.1Scgd				case 'r':  c = '\r';  break;
981.1Scgd				case 't':  c = '\t';  break;
991.1Scgd				case 'v':  c = '\v';  break;
1001.1Scgd				case '\\':  break;		/* c = '\\' */
1011.1Scgd				case '0':
1021.1Scgd					c = 0;
1031.1Scgd					count = 3;
1041.1Scgd					while (--count >= 0 && (unsigned)(*p - '0') < 8)
1051.1Scgd						c = (c << 3) + (*p++ - '0');
1061.1Scgd					break;
1071.1Scgd				default:
1081.9Schristos					/* Output the '/' and char following */
1091.1Scgd					p--;
1101.1Scgd					break;
1111.1Scgd				}
1121.1Scgd			}
1131.1Scgd			putchar(c);
1141.1Scgd		}
1151.1Scgd		if (*ap)
1161.1Scgd			putchar(' ');
1171.1Scgd	}
1181.1Scgd	if (! nflag)
1191.1Scgd		putchar('\n');
1201.14Sdholland	fflush(stdout);
1211.15Skre	if (ferror(stdout)) {
1221.15Skre		clearerr(stdout);
1231.15Skre		err(1, "write error");
1241.15Skre	}
1251.1Scgd	return 0;
1261.1Scgd}
127