Mercurial > hg > toybox
annotate toys/posix/echo.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
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 * |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
7 * See http://opengroup.org/onlinepubs/9699919799/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 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
35 #define FOR_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 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
38 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
|
39 { |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
40 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
|
41 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
|
42 |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 for (;;) { |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 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
|
45 if (!arg) break; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 if (i++) xputc(' '); |
72
116405f248cb
Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents:
71
diff
changeset
|
47 |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
48 // 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
|
49 |
563
b24c4fe9f4fd
Use FLAG_ macros for echo optflags.
Rob Landley <rob@landley.net>
parents:
508
diff
changeset
|
50 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
|
51 xprintf("%s", arg); |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
52 continue; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
53 } |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
54 |
72
116405f248cb
Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents:
71
diff
changeset
|
55 // Handle -e |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
72
diff
changeset
|
56 |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
57 for (c=arg;;) { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
58 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
|
59 |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
60 // handle \escapes |
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) { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 out = 0; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
68 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
|
69 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
|
70 } 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
|
71 out = 0; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
72 while (n++<2) { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
73 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
|
74 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
|
75 else { |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 c++; |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
80 } 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
|
81 } |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 } |
508
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
83 // 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
|
84 } else c--; |
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 xputc(out); |
f062652562bd
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
Rob Landley <rob@landley.net>
parents:
450
diff
changeset
|
87 } |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 } |
72
116405f248cb
Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents:
71
diff
changeset
|
89 |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 // Output "\n" if no -n |
563
b24c4fe9f4fd
Use FLAG_ macros for echo optflags.
Rob Landley <rob@landley.net>
parents:
508
diff
changeset
|
91 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
|
92 done: |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 xflush(); |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 } |