tr.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini tr implementation for busybox
  4. *
  5. ** Copyright (c) 1987,1997, Prentice Hall All rights reserved.
  6. *
  7. * The name of Prentice Hall may not be used to endorse or promote
  8. * products derived from this software without specific prior
  9. * written permission.
  10. *
  11. * Copyright (c) Michiel Huisjes
  12. *
  13. * This version of tr is adapted from Minix tr and was modified
  14. * by Erik Andersen <andersen@codepoet.org> to be used in busybox.
  15. *
  16. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  17. */
  18. /* http://www.opengroup.org/onlinepubs/009695399/utilities/tr.html
  19. * TODO: xdigit, graph, print
  20. */
  21. #include "libbb.h"
  22. #define ASCII 0377
  23. static void map(char *pvector,
  24. unsigned char *string1, unsigned int string1_len,
  25. unsigned char *string2, unsigned int string2_len)
  26. {
  27. char last = '0';
  28. unsigned int i, j;
  29. for (j = 0, i = 0; i < string1_len; i++) {
  30. if (string2_len <= j)
  31. pvector[string1[i]] = last;
  32. else
  33. pvector[string1[i]] = last = string2[j++];
  34. }
  35. }
  36. /* supported constructs:
  37. * Ranges, e.g., 0-9 ==> 0123456789
  38. * Ranges, e.g., [0-9] ==> 0123456789
  39. * Escapes, e.g., \a ==> Control-G
  40. * Character classes, e.g. [:upper:] ==> A...Z
  41. * Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?)
  42. */
  43. static unsigned int expand(const char *arg, char *buffer)
  44. {
  45. char *buffer_start = buffer;
  46. unsigned i; /* can't be unsigned char: must be able to hold 256 */
  47. unsigned char ac;
  48. while (*arg) {
  49. if (*arg == '\\') {
  50. arg++;
  51. *buffer++ = bb_process_escape_sequence(&arg);
  52. continue;
  53. }
  54. if (arg[1] == '-') { /* "0-9..." */
  55. ac = arg[2];
  56. if (ac == '\0') { /* "0-": copy verbatim */
  57. *buffer++ = *arg++; /* copy '0' */
  58. continue; /* next iter will copy '-' and stop */
  59. }
  60. i = *arg;
  61. while (i <= ac) /* ok: i is unsigned _int_ */
  62. *buffer++ = i++;
  63. arg += 3; /* skip 0-9 */
  64. continue;
  65. }
  66. if (*arg == '[') { /* "[xyz..." */
  67. arg++;
  68. i = *arg++;
  69. /* "[xyz...", i=x, arg points to y */
  70. if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
  71. #define CLO ":]\0"
  72. static const char classes[] ALIGN1 =
  73. "alpha"CLO "alnum"CLO "digit"CLO
  74. "lower"CLO "upper"CLO "space"CLO
  75. "blank"CLO "punct"CLO "cntrl"CLO;
  76. #define CLASS_invalid 0 /* we increment the retval */
  77. #define CLASS_alpha 1
  78. #define CLASS_alnum 2
  79. #define CLASS_digit 3
  80. #define CLASS_lower 4
  81. #define CLASS_upper 5
  82. #define CLASS_space 6
  83. #define CLASS_blank 7
  84. #define CLASS_punct 8
  85. #define CLASS_cntrl 9
  86. //#define CLASS_xdigit 10
  87. //#define CLASS_graph 11
  88. //#define CLASS_print 12
  89. smalluint j;
  90. { /* not really pretty.. */
  91. char *tmp = xstrndup(arg, 7); // warning: xdigit would need 8, not 7
  92. j = index_in_strings(classes, tmp) + 1;
  93. free(tmp);
  94. }
  95. if (j == CLASS_alnum || j == CLASS_digit) {
  96. for (i = '0'; i <= '9'; i++)
  97. *buffer++ = i;
  98. }
  99. if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
  100. for (i = 'A'; i <= 'Z'; i++)
  101. *buffer++ = i;
  102. }
  103. if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
  104. for (i = 'a'; i <= 'z'; i++)
  105. *buffer++ = i;
  106. }
  107. if (j == CLASS_space || j == CLASS_blank) {
  108. *buffer++ = '\t';
  109. if (j == CLASS_space) {
  110. *buffer++ = '\n';
  111. *buffer++ = '\v';
  112. *buffer++ = '\f';
  113. *buffer++ = '\r';
  114. }
  115. *buffer++ = ' ';
  116. }
  117. if (j == CLASS_punct || j == CLASS_cntrl) {
  118. for (i = '\0'; i <= ASCII; i++)
  119. if ((j == CLASS_punct && isprint(i) && !isalnum(i) && !isspace(i))
  120. || (j == CLASS_cntrl && iscntrl(i)))
  121. *buffer++ = i;
  122. }
  123. if (j == CLASS_invalid) {
  124. *buffer++ = '[';
  125. *buffer++ = ':';
  126. continue;
  127. }
  128. break;
  129. }
  130. /* "[xyz...", i=x, arg points to y */
  131. if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */
  132. *buffer++ = *arg; /* copy CHAR */
  133. arg += 3; /* skip CHAR=] */
  134. continue;
  135. }
  136. if (*arg != '-') { /* not [x-...] - copy verbatim */
  137. *buffer++ = '[';
  138. arg--; /* points to x */
  139. continue; /* copy all, including eventual ']' */
  140. }
  141. /* [x-y...] */
  142. arg++;
  143. ac = *arg++;
  144. while (i <= ac)
  145. *buffer++ = i++;
  146. arg++; /* skip the assumed ']' */
  147. continue;
  148. }
  149. *buffer++ = *arg++;
  150. }
  151. return (buffer - buffer_start);
  152. }
  153. static int complement(char *buffer, int buffer_len)
  154. {
  155. int i, j, ix;
  156. char conv[ASCII + 2];
  157. ix = 0;
  158. for (i = '\0'; i <= ASCII; i++) {
  159. for (j = 0; j < buffer_len; j++)
  160. if (buffer[j] == i)
  161. break;
  162. if (j == buffer_len)
  163. conv[ix++] = i & ASCII;
  164. }
  165. memcpy(buffer, conv, ix);
  166. return ix;
  167. }
  168. int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  169. int tr_main(int argc ATTRIBUTE_UNUSED, char **argv)
  170. {
  171. int output_length = 0, input_length;
  172. int i;
  173. smalluint flags;
  174. ssize_t read_chars = 0;
  175. size_t in_index = 0, out_index = 0;
  176. unsigned last = UCHAR_MAX + 1; /* not equal to any char */
  177. unsigned char coded, c;
  178. unsigned char *output = xmalloc(BUFSIZ);
  179. char *vector = xzalloc((ASCII+1) * 3);
  180. char *invec = vector + (ASCII+1);
  181. char *outvec = vector + (ASCII+1) * 2;
  182. #define TR_OPT_complement (1 << 0)
  183. #define TR_OPT_delete (1 << 1)
  184. #define TR_OPT_squeeze_reps (1 << 2)
  185. flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */
  186. argv += optind;
  187. for (i = 0; i <= ASCII; i++) {
  188. vector[i] = i;
  189. /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
  190. }
  191. #define tr_buf bb_common_bufsiz1
  192. if (*argv != NULL) {
  193. input_length = expand(*argv++, tr_buf);
  194. if (flags & TR_OPT_complement)
  195. input_length = complement(tr_buf, input_length);
  196. if (*argv) {
  197. if (argv[0][0] == '\0')
  198. bb_error_msg_and_die("STRING2 cannot be empty");
  199. output_length = expand(*argv, (char *)output);
  200. map(vector, (unsigned char *)tr_buf, input_length, output, output_length);
  201. }
  202. for (i = 0; i < input_length; i++)
  203. invec[(unsigned char)tr_buf[i]] = TRUE;
  204. for (i = 0; i < output_length; i++)
  205. outvec[output[i]] = TRUE;
  206. }
  207. for (;;) {
  208. /* If we're out of input, flush output and read more input. */
  209. if ((ssize_t)in_index == read_chars) {
  210. if (out_index) {
  211. xwrite(STDOUT_FILENO, (char *)output, out_index);
  212. out_index = 0;
  213. }
  214. read_chars = safe_read(STDIN_FILENO, tr_buf, BUFSIZ);
  215. if (read_chars <= 0) {
  216. if (read_chars < 0)
  217. bb_perror_msg_and_die(bb_msg_read_error);
  218. exit(EXIT_SUCCESS);
  219. }
  220. in_index = 0;
  221. }
  222. c = tr_buf[in_index++];
  223. coded = vector[c];
  224. if ((flags & TR_OPT_delete) && invec[c])
  225. continue;
  226. if ((flags & TR_OPT_squeeze_reps) && last == coded
  227. && (invec[c] || outvec[coded]))
  228. continue;
  229. output[out_index++] = last = coded;
  230. }
  231. /* NOTREACHED */
  232. return EXIT_SUCCESS;
  233. }