comparison toys/posix/echo.c @ 694:786841fdb1e0

Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style. The actual code should be the same afterward, this is just cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Tue, 13 Nov 2012 17:14:08 -0600
parents 7e846e281e38
children 95cb37adb024
comparison
equal deleted inserted replaced
693:4a5a250e0633 694:786841fdb1e0
1 /* vi: set sw=4 ts=4: 1 /* echo.c - echo supporting -n and -e.
2 *
3 * echo.c - echo supporting -n and -e.
4 * 2 *
5 * Copyright 2007 Rob Landley <rob@landley.net> 3 * Copyright 2007 Rob Landley <rob@landley.net>
6 * 4 *
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/echo.html 5 * See http://opengroup.org/onlinepubs/9699919799/utilities/echo.html
8 6
9 USE_ECHO(NEWTOY(echo, "^?en", TOYFLAG_BIN)) 7 USE_ECHO(NEWTOY(echo, "^?en", TOYFLAG_BIN))
10 8
11 config ECHO 9 config ECHO
12 bool "echo" 10 bool "echo"
13 default y 11 default y
14 help 12 help
15 usage: echo [-ne] [args...] 13 usage: echo [-ne] [args...]
16 14
17 Write each argument to stdout, with one space between each, followed 15 Write each argument to stdout, with one space between each, followed
18 by a newline. 16 by a newline.
19 17
20 -n No trailing newline. 18 -n No trailing newline.
21 -e Process the following escape sequences: 19 -e Process the following escape sequences:
22 \\ backslash 20 \\ backslash
23 \0NNN octal values (1 to 3 digits) 21 \0NNN octal values (1 to 3 digits)
24 \a alert (beep/flash) 22 \a alert (beep/flash)
25 \b backspace 23 \b backspace
26 \c stop output here (avoids trailing newline) 24 \c stop output here (avoids trailing newline)
27 \f form feed 25 \f form feed
28 \n newline 26 \n newline
29 \r carriage return 27 \r carriage return
30 \t horizontal tab 28 \t horizontal tab
31 \v vertical tab 29 \v vertical tab
32 \xHH hexadecimal values (1 to 2 digits) 30 \xHH hexadecimal values (1 to 2 digits)
33 */ 31 */
34 32
35 #define FOR_echo 33 #define FOR_echo
36 #include "toys.h" 34 #include "toys.h"
37 35
38 void echo_main(void) 36 void echo_main(void)
39 { 37 {
40 int i = 0, out; 38 int i = 0, out;
41 char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v", *c; 39 char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v", *c;
42 40
43 for (;;) { 41 for (;;) {
44 arg = toys.optargs[i]; 42 arg = toys.optargs[i];
45 if (!arg) break; 43 if (!arg) break;
46 if (i++) xputc(' '); 44 if (i++) xputc(' ');
47 45
48 // Should we output arg verbatim? 46 // Should we output arg verbatim?
49 47
50 if (!(toys.optflags&FLAG_e)) { 48 if (!(toys.optflags & FLAG_e)) {
51 xprintf("%s", arg); 49 xprintf("%s", arg);
52 continue; 50 continue;
53 } 51 }
54 52
55 // Handle -e 53 // Handle -e
56 54
57 for (c=arg;;) { 55 for (c=arg;;) {
58 if (!(out = *(c++))) break; 56 if (!(out = *(c++))) break;
59 57
60 // handle \escapes 58 // handle \escapes
61 if (out == '\\' && *c) { 59 if (out == '\\' && *c) {
62 int n = 0, slash = *(c++); 60 int n = 0, slash = *(c++);
63 char *found = strchr(from, slash); 61 char *found = strchr(from, slash);
64 if (found) out = to[found-from]; 62 if (found) out = to[found-from];
65 else if (slash == 'c') goto done; 63 else if (slash == 'c') goto done;
66 else if (slash == '0') { 64 else if (slash == '0') {
67 out = 0; 65 out = 0;
68 while (*c>='0' && *c<='7' && n++<3) 66 while (*c>='0' && *c<='7' && n++<3) out = (out*8)+*(c++)-'0';
69 out = (out*8)+*(c++)-'0'; 67 } else if (slash == 'x') {
70 } else if (slash == 'x') { 68 out = 0;
71 out = 0; 69 while (n++<2) {
72 while (n++<2) { 70 if (*c>='0' && *c<='9') out = (out*16)+*(c++)-'0';
73 if (*c>='0' && *c<='9') 71 else {
74 out = (out*16)+*(c++)-'0'; 72 int temp = tolower(*c);
75 else { 73 if (temp>='a' && temp<='f') {
76 int temp = tolower(*c); 74 out = (out*16)+temp-'a'+10;
77 if (temp>='a' && temp<='f') { 75 c++;
78 out = (out*16)+temp-'a'+10; 76 } else break;
79 c++; 77 }
80 } else break; 78 }
81 } 79 // Slash in front of unknown character, print literal.
82 } 80 } else c--;
83 // Slash in front of unknown character, print literal. 81 }
84 } else c--; 82 xputc(out);
85 } 83 }
86 xputc(out); 84 }
87 }
88 }
89 85
90 // Output "\n" if no -n 86 // Output "\n" if no -n
91 if (!(toys.optflags&FLAG_n)) xputc('\n'); 87 if (!(toys.optflags&FLAG_n)) xputc('\n');
92 done: 88 done:
93 xflush(); 89 xflush();
94 } 90 }