xref: /original-bsd/bin/sh/bltin/echo.c (revision d272e02a)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)echo.c	5.2 (Berkeley) 03/13/91
11  */
12 
13 /*
14  * Echo command.
15  */
16 
17 #define main echocmd
18 
19 #include "bltin.h"
20 
21 /* #define eflag 1 */
22 
23 main(argc, argv)  char **argv; {
24 	register char **ap;
25 	register char *p;
26 	register char c;
27 	int count;
28 	int nflag = 0;
29 #ifndef eflag
30 	int eflag = 0;
31 #endif
32 
33 	ap = argv;
34 	if (argc)
35 		ap++;
36 	if ((p = *ap) != NULL) {
37 		if (equal(p, "-n")) {
38 			nflag++;
39 			ap++;
40 		} else if (equal(p, "-e")) {
41 #ifndef eflag
42 			eflag++;
43 #endif
44 			ap++;
45 		}
46 	}
47 	while ((p = *ap++) != NULL) {
48 		while ((c = *p++) != '\0') {
49 			if (c == '\\' && eflag) {
50 				switch (*p++) {
51 				case 'b':  c = '\b';  break;
52 				case 'c':  return 0;		/* exit */
53 				case 'f':  c = '\f';  break;
54 				case 'n':  c = '\n';  break;
55 				case 'r':  c = '\r';  break;
56 				case 't':  c = '\t';  break;
57 				case 'v':  c = '\v';  break;
58 				case '\\':  break;		/* c = '\\' */
59 				case '0':
60 					c = 0;
61 					count = 3;
62 					while (--count >= 0 && (unsigned)(*p - '0') < 8)
63 						c = (c << 3) + (*p++ - '0');
64 					break;
65 				default:
66 					p--;
67 					break;
68 				}
69 			}
70 			putchar(c);
71 		}
72 		if (*ap)
73 			putchar(' ');
74 	}
75 	if (! nflag)
76 		putchar('\n');
77 	return 0;
78 }
79