Mercurial > hg > toybox
annotate toys/echo.c @ 481:e1b9a8579ddb
mkswap helpstring and argument string
author | Elie De Brauwer <eliedebrauwer@gmail.com> |
---|---|
date | Sat, 18 Feb 2012 14:13:55 +0100 |
parents | d8ff3b0980cf |
children | f062652562bd |
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 |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 #include "toys.h" |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
37 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
|
38 { |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 int i = 0; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v"; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
72
diff
changeset
|
41 |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 for (;;) { |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 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
|
44 if (!arg) break; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 if (i++) xputc(' '); |
72
116405f248cb
Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents:
71
diff
changeset
|
46 |
116405f248cb
Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents:
71
diff
changeset
|
47 // Handle -e |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
72
diff
changeset
|
48 |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 if (toys.optflags&2) { |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 int c, j = 0; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 for (;;) { |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 c = arg[j++]; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 if (!c) break; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 if (c == '\\') { |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 char *found; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 int d = arg[j++]; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 |
72
116405f248cb
Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents:
71
diff
changeset
|
58 // handle \escapes |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 if (d) { |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 found = strchr(from, d); |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 if (found) c = to[found-from]; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 else if (d == 'c') goto done; |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 else if (d == '0') { |
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
|
65 int n = 0; |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 c = 0; |
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
|
67 while (arg[j]>='0' && arg[j]<='7' && n++<3) |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 c = (c*8)+arg[j++]-'0'; |
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
|
69 } else if (d == 'x') { |
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
|
70 int n = 0; |
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
|
71 c = 0; |
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
|
72 while (n++<2) { |
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
|
73 if (arg[j]>='0' && arg[j]<='9') |
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
|
74 c = (c*16)+arg[j++]-'0'; |
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
|
75 else { |
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
|
76 int temp = tolower(arg[j]); |
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
|
77 if (temp>='a' && temp<='f') { |
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
|
78 c = (c*16)+temp-'a'+10; |
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
|
79 j++; |
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
|
80 } else break; |
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
|
81 } |
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
|
82 } |
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
|
83 } |
71
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 } |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 } |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 xputc(c); |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 } |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 } else xprintf("%s", arg); |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 } |
72
116405f248cb
Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents:
71
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 // Output "\n" if no -n |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
92 if (!(toys.optflags&1)) xputc('\n'); |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 done: |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 xflush(); |
40103a3ddcb0
Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 } |