xref: /csrg-svn/bin/sh/bltin/echo.c (revision 69276)
147101Sbostic /*-
260695Sbostic  * Copyright (c) 1991, 1993
360695Sbostic  *	The Regents of the University of California.  All rights reserved.
447101Sbostic  *
547101Sbostic  * This code is derived from software contributed to Berkeley by
647101Sbostic  * Kenneth Almquist.
747101Sbostic  *
847101Sbostic  * %sccs.include.redist.c%
947101Sbostic  *
10*69276Schristos  *	@(#)echo.c	8.2 (Berkeley) 05/04/95
1147101Sbostic  */
1247101Sbostic 
1347101Sbostic /*
1447101Sbostic  * Echo command.
1547101Sbostic  */
1647101Sbostic 
1747101Sbostic #define main echocmd
1847101Sbostic 
1947101Sbostic #include "bltin.h"
2047101Sbostic 
2147302Smarc /* #define eflag 1 */
2247101Sbostic 
23*69276Schristos int
main(argc,argv)24*69276Schristos main(argc, argv)
25*69276Schristos 	int argc;
26*69276Schristos 	char **argv;
27*69276Schristos {
2847302Smarc 	register char **ap;
2947101Sbostic 	register char *p;
3047101Sbostic 	register char c;
3147101Sbostic 	int count;
3247101Sbostic 	int nflag = 0;
3347302Smarc #ifndef eflag
3447101Sbostic 	int eflag = 0;
3547302Smarc #endif
3647101Sbostic 
3747302Smarc 	ap = argv;
3847302Smarc 	if (argc)
3947302Smarc 		ap++;
4047302Smarc 	if ((p = *ap) != NULL) {
4147302Smarc 		if (equal(p, "-n")) {
4247101Sbostic 			nflag++;
4347302Smarc 			ap++;
4447302Smarc 		} else if (equal(p, "-e")) {
4547302Smarc #ifndef eflag
4647101Sbostic 			eflag++;
4747302Smarc #endif
4847302Smarc 			ap++;
4947101Sbostic 		}
5047302Smarc 	}
5147302Smarc 	while ((p = *ap++) != NULL) {
5247302Smarc 		while ((c = *p++) != '\0') {
5347302Smarc 			if (c == '\\' && eflag) {
5447302Smarc 				switch (*p++) {
5547302Smarc 				case 'b':  c = '\b';  break;
5647302Smarc 				case 'c':  return 0;		/* exit */
5747302Smarc 				case 'f':  c = '\f';  break;
5847302Smarc 				case 'n':  c = '\n';  break;
5947302Smarc 				case 'r':  c = '\r';  break;
6047302Smarc 				case 't':  c = '\t';  break;
6147302Smarc 				case 'v':  c = '\v';  break;
6247302Smarc 				case '\\':  break;		/* c = '\\' */
6347302Smarc 				case '0':
6447302Smarc 					c = 0;
6547302Smarc 					count = 3;
6647302Smarc 					while (--count >= 0 && (unsigned)(*p - '0') < 8)
6747302Smarc 						c = (c << 3) + (*p++ - '0');
6847302Smarc 					break;
6947302Smarc 				default:
7047302Smarc 					p--;
7147302Smarc 					break;
7247101Sbostic 				}
7347101Sbostic 			}
7447302Smarc 			putchar(c);
7547101Sbostic 		}
7647302Smarc 		if (*ap)
7747302Smarc 			putchar(' ');
7847101Sbostic 	}
7947302Smarc 	if (! nflag)
8047101Sbostic 		putchar('\n');
8147101Sbostic 	return 0;
8247101Sbostic }
83