3
0

echo.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * echo implementation for busybox - used as a helper for testsuite/*
  4. * on systems lacking "echo -en"
  5. *
  6. * Copyright (c) 1991, 1993
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. *
  11. * Original copyright notice is retained at the end of this file.
  12. */
  13. /* BB_AUDIT SUSv3 compliant -- unless configured as fancy echo. */
  14. /* http://www.opengroup.org/onlinepubs/007904975/utilities/echo.html */
  15. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  16. *
  17. * Because of behavioral differences, implemented configurable SUSv3
  18. * or 'fancy' gnu-ish behaviors. Also, reduced size and fixed bugs.
  19. * 1) In handling '\c' escape, the previous version only suppressed the
  20. * trailing newline. SUSv3 specifies _no_ output after '\c'.
  21. * 2) SUSv3 specifies that octal escapes are of the form \0{#{#{#}}}.
  22. * The previous version did not allow 4-digit octals.
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <limits.h>
  27. #define WANT_HEX_ESCAPES 1
  28. /* Usual "this only works for ascii compatible encodings" disclaimer. */
  29. #undef _tolower
  30. #define _tolower(X) ((X)|((char) 0x20))
  31. static char bb_process_escape_sequence(const char **ptr)
  32. {
  33. static const char charmap[] = {
  34. 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
  35. '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
  36. const char *p;
  37. const char *q;
  38. unsigned int num_digits;
  39. unsigned int r;
  40. unsigned int n;
  41. unsigned int d;
  42. unsigned int base;
  43. num_digits = n = 0;
  44. base = 8;
  45. q = *ptr;
  46. #ifdef WANT_HEX_ESCAPES
  47. if (*q == 'x') {
  48. ++q;
  49. base = 16;
  50. ++num_digits;
  51. }
  52. #endif
  53. do {
  54. d = (unsigned char)(*q) - '0';
  55. #ifdef WANT_HEX_ESCAPES
  56. if (d >= 10) {
  57. d = (unsigned char)(_tolower(*q)) - 'a' + 10;
  58. }
  59. #endif
  60. if (d >= base) {
  61. #ifdef WANT_HEX_ESCAPES
  62. if ((base == 16) && (!--num_digits)) {
  63. /* return '\\'; */
  64. --q;
  65. }
  66. #endif
  67. break;
  68. }
  69. r = n * base + d;
  70. if (r > UCHAR_MAX) {
  71. break;
  72. }
  73. n = r;
  74. ++q;
  75. } while (++num_digits < 3);
  76. if (num_digits == 0) { /* mnemonic escape sequence? */
  77. p = charmap;
  78. do {
  79. if (*p == *q) {
  80. q++;
  81. break;
  82. }
  83. } while (*++p);
  84. n = *(p + (sizeof(charmap)/2));
  85. }
  86. *ptr = q;
  87. return (char) n;
  88. }
  89. int main(int argc, char **argv)
  90. {
  91. const char *arg;
  92. const char *p;
  93. char nflag = 1;
  94. char eflag = 0;
  95. /* We must check that stdout is not closed. */
  96. if (dup2(1, 1) != 1)
  97. return -1;
  98. while (1) {
  99. arg = *++argv;
  100. if (!arg)
  101. goto newline_ret;
  102. if (*arg != '-')
  103. break;
  104. /* If it appears that we are handling options, then make sure
  105. * that all of the options specified are actually valid.
  106. * Otherwise, the string should just be echoed.
  107. */
  108. p = arg + 1;
  109. if (!*p) /* A single '-', so echo it. */
  110. goto just_echo;
  111. do {
  112. if (!strrchr("neE", *p))
  113. goto just_echo;
  114. } while (*++p);
  115. /* All of the options in this arg are valid, so handle them. */
  116. p = arg + 1;
  117. do {
  118. if (*p == 'n')
  119. nflag = 0;
  120. if (*p == 'e')
  121. eflag = '\\';
  122. } while (*++p);
  123. }
  124. just_echo:
  125. while (1) {
  126. /* arg is already == *argv and isn't NULL */
  127. int c;
  128. if (!eflag) {
  129. /* optimization for very common case */
  130. fputs(arg, stdout);
  131. } else while ((c = *arg++)) {
  132. if (c == eflag) { /* Check for escape seq. */
  133. if (*arg == 'c') {
  134. /* '\c' means cancel newline and
  135. * ignore all subsequent chars. */
  136. goto ret;
  137. }
  138. {
  139. /* Since SUSv3 mandates a first digit of 0, 4-digit octals
  140. * of the form \0### are accepted. */
  141. if (*arg == '0') {
  142. /* NB: don't turn "...\0" into "...\" */
  143. if (arg[1] && ((unsigned char)(arg[1]) - '0') < 8) {
  144. arg++;
  145. }
  146. }
  147. /* bb_process_escape_sequence handles NUL correctly
  148. * ("...\" case. */
  149. c = bb_process_escape_sequence(&arg);
  150. }
  151. }
  152. putchar(c);
  153. }
  154. arg = *++argv;
  155. if (!arg)
  156. break;
  157. putchar(' ');
  158. }
  159. newline_ret:
  160. if (nflag) {
  161. putchar('\n');
  162. }
  163. ret:
  164. return fflush(NULL);
  165. }
  166. /*-
  167. * Copyright (c) 1991, 1993
  168. * The Regents of the University of California. All rights reserved.
  169. *
  170. * This code is derived from software contributed to Berkeley by
  171. * Kenneth Almquist.
  172. *
  173. * Redistribution and use in source and binary forms, with or without
  174. * modification, are permitted provided that the following conditions
  175. * are met:
  176. * 1. Redistributions of source code must retain the above copyright
  177. * notice, this list of conditions and the following disclaimer.
  178. * 2. Redistributions in binary form must reproduce the above copyright
  179. * notice, this list of conditions and the following disclaimer in the
  180. * documentation and/or other materials provided with the distribution.
  181. *
  182. * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  183. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  184. *
  185. * California, Berkeley and its contributors.
  186. * 4. Neither the name of the University nor the names of its contributors
  187. * may be used to endorse or promote products derived from this software
  188. * without specific prior written permission.
  189. *
  190. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  191. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  192. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  193. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  194. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  195. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  196. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  197. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  198. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  199. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  200. * SUCH DAMAGE.
  201. *
  202. * @(#)echo.c 8.1 (Berkeley) 5/31/93
  203. */