Mercurial > hg > toybox
annotate toys/echo.c @ 563:b24c4fe9f4fd
Use FLAG_ macros for echo optflags.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 14 Apr 2012 21:41:19 -0500 |
parents | f062652562bd |
children |
rev | line source |
---|---|
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
1 /* vi: set sw=4 ts=4: |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
2 * |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * echo.c - echo supporting -n and -e. |
194
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
4 * |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
5 * Copyright 2007 Rob Landley <rob@landley.net> |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
6 * |
198
3227c5316260
Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/echo.html |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
8 |
304
93223118c813
Option parsing: stopearly is now a ^ prefix (not +), and an option string with
Rob Landley <rob@landley.net>
parents:
294
diff
changeset
|
9 USE_ECHO(NEWTOY(echo, "^?en", TOYFLAG_BIN)) |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
10 |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
11 config ECHO |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
12 bool "echo" |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
13 default y |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
14 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
15 usage: echo [-ne] [args...] |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
16 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
17 Write each argument to stdout, with one space between each, followed |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
18 by a newline. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
19 |
450
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
20 -n No trailing newline. |
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
21 -e Process the following escape sequences: |
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
22 \\ backslash |
449
d3544d2cdb26
Fix overflow in octal formatting in echo, add support for hexadecimal input, add tests for octal and hexadecimal formatting
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
304
diff
changeset
|
23 \0NNN octal values (1 to 3 digits) |
450
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
24 \a alert (beep/flash) |
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
25 \b backspace |
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
26 \c stop output here (avoids trailing newline) |
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
27 \f form feed |
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
28 \n newline |
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
29 \r carriage return |
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
30 \t horizontal tab |
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
31 \v vertical tab |
d8ff3b0980cf
Minor cleanups to echo: collapse together three tolower() calls, make indent/brackets a bit more regular/obvious, and replace some spaces with tabs in help text (in a way that won't matter until I finish rewriting scripts/config2help.py in C).
Rob Landley <rob@landley.net>
parents:
449
diff
changeset
|
32 \xHH hexadecimal values (1 to 2 digits) |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
33 */ |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 |
563
b24c4fe9f4fd
Use FLAG_ macros for echo optflags.
Rob Landley <rob@landley.net>
parents:
508
diff
changeset
|
35 #define THIS echo |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 #include "toys.h" |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 |
563
b24c4fe9f4fd
Use FLAG_ macros for echo optflags.
Rob Landley <rob@landley.net>
parents:
508
diff
changeset
|
38 #define FLAG_e (1<<1) |
b24c4fe9f4fd
Use FLAG_ macros for echo optflags.
Rob Landley <rob@landley.net>
parents:
508
diff
changeset
|
39 #define FLAG_n (1<<0) |
b24c4fe9f4fd
Use FLAG_ macros for echo optflags.
Rob Landley <rob@landley.net>
parents:
508
diff
changeset
|
40 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
41 void echo_main(void) |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 { |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
43 int i = 0, out; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
44 char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v", *c; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
72
diff
changeset
|
45 |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 for (;;) { |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 arg = toys.optargs[i]; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 if (!arg) break; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 if (i++) xputc(' '); |
72
116405f248cb
Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents:
71
diff
changeset
|
50 |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
51 // Should we output arg verbatim? |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
52 |
563
b24c4fe9f4fd
Use FLAG_ macros for echo optflags.
Rob Landley <rob@landley.net>
parents:
508
diff
changeset
|
53 if (!(toys.optflags&FLAG_e)) { |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
54 xprintf("%s", arg); |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
55 continue; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
56 } |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
57 |
72
116405f248cb
Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents:
71
diff
changeset
|
58 // Handle -e |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
72
diff
changeset
|
59 |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
60 for (c=arg;;) { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
61 if (!(out = *(c++))) break; |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
63 // handle \escapes |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
64 if (out == '\\' && *c) { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
65 int n = 0, slash = *(c++); |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
66 char *found = strchr(from, slash); |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
67 if (found) out = to[found-from]; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
68 else if (slash == 'c') goto done; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
69 else if (slash == '0') { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
70 out = 0; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
71 while (*c>='0' && *c<='7' && n++<3) |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
72 out = (out*8)+*(c++)-'0'; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
73 } else if (slash == 'x') { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
74 out = 0; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
75 while (n++<2) { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
76 if (*c>='0' && *c<='9') |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
77 out = (out*16)+*(c++)-'0'; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
78 else { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
79 int temp = tolower(*c); |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
80 if (temp>='a' && temp<='f') { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
81 out = (out*16)+temp-'a'+10; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
82 c++; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
83 } else break; |
449
d3544d2cdb26
Fix overflow in octal formatting in echo, add support for hexadecimal input, add tests for octal and hexadecimal formatting
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
304
diff
changeset
|
84 } |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 } |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
86 // Slash in front of unknown character, print literal. |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
87 } else c--; |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 } |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
89 xputc(out); |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
90 } |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 } |
72
116405f248cb
Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents:
71
diff
changeset
|
92 |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 // Output "\n" if no -n |
563
b24c4fe9f4fd
Use FLAG_ macros for echo optflags.
Rob Landley <rob@landley.net>
parents:
508
diff
changeset
|
94 if (!(toys.optflags&FLAG_n)) xputc('\n'); |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 done: |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 xflush(); |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 } |