tr.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #define TR_OPT_complement (1<<0)
  24. #define TR_OPT_delete (1<<1)
  25. #define TR_OPT_squeeze_reps (1<<2)
  26. static void map(char *pvector,
  27. unsigned char *string1, unsigned int string1_len,
  28. unsigned char *string2, unsigned int string2_len)
  29. {
  30. char last = '0';
  31. unsigned int i, j;
  32. for (j = 0, i = 0; i < string1_len; i++) {
  33. if (string2_len <= j)
  34. pvector[string1[i]] = last;
  35. else
  36. pvector[string1[i]] = last = string2[j++];
  37. }
  38. }
  39. /* supported constructs:
  40. * Ranges, e.g., [0-9] ==> 0123456789
  41. * Escapes, e.g., \a ==> Control-G
  42. * Character classes, e.g. [:upper:] ==> A ... Z
  43. */
  44. static unsigned int expand(const char *arg, char *buffer)
  45. {
  46. char *buffer_start = buffer;
  47. unsigned i; /* XXX: FIXME: use unsigned char? */
  48. unsigned char ac;
  49. #define CLO ":]\0"
  50. static const char classes[] ALIGN1 =
  51. "alpha"CLO "alnum"CLO "digit"CLO "lower"CLO "upper"CLO "space"CLO
  52. "blank"CLO "punct"CLO "cntrl"CLO;
  53. #define CLASS_invalid 0 /* we increment the retval */
  54. #define CLASS_alpha 1
  55. #define CLASS_alnum 2
  56. #define CLASS_digit 3
  57. #define CLASS_lower 4
  58. #define CLASS_upper 5
  59. #define CLASS_space 6
  60. #define CLASS_blank 7
  61. #define CLASS_punct 8
  62. #define CLASS_cntrl 9
  63. //#define CLASS_xdigit 10
  64. //#define CLASS_graph 11
  65. //#define CLASS_print 12
  66. while (*arg) {
  67. if (*arg == '\\') {
  68. arg++;
  69. *buffer++ = bb_process_escape_sequence(&arg);
  70. } else if (*(arg+1) == '-') {
  71. ac = *(arg+2);
  72. if (ac == 0) {
  73. *buffer++ = *arg++;
  74. continue;
  75. }
  76. i = *arg;
  77. while (i <= ac)
  78. *buffer++ = i++;
  79. arg += 3; /* Skip the assumed a-z */
  80. } else if (*arg == '[') {
  81. arg++;
  82. i = *arg++;
  83. if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
  84. smalluint j;
  85. { /* not really pretty.. */
  86. char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7
  87. j = index_in_strings(classes, tmp) + 1;
  88. free(tmp);
  89. }
  90. if (j == CLASS_alnum || j == CLASS_digit) {
  91. for (i = '0'; i <= '9'; i++)
  92. *buffer++ = i;
  93. }
  94. if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
  95. for (i = 'A'; i <= 'Z'; i++)
  96. *buffer++ = i;
  97. }
  98. if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
  99. for (i = 'a'; i <= 'z'; i++)
  100. *buffer++ = i;
  101. }
  102. if (j == CLASS_space || j == CLASS_blank) {
  103. *buffer++ = '\t';
  104. if (j == CLASS_space) {
  105. *buffer++ = '\n';
  106. *buffer++ = '\v';
  107. *buffer++ = '\f';
  108. *buffer++ = '\r';
  109. }
  110. *buffer++ = ' ';
  111. }
  112. if (j == CLASS_punct || j == CLASS_cntrl) {
  113. for (i = 0; i <= ASCII; i++)
  114. if ((j == CLASS_punct &&
  115. isprint(i) && (!isalnum(i)) && (!isspace(i))) ||
  116. (j == CLASS_cntrl && iscntrl(i)))
  117. *buffer++ = i;
  118. }
  119. if (j == CLASS_invalid) {
  120. *buffer++ = '[';
  121. *buffer++ = ':';
  122. continue;
  123. }
  124. break;
  125. }
  126. if (ENABLE_FEATURE_TR_EQUIV && i == '=') {
  127. *buffer++ = *arg;
  128. arg += 3; /* Skip the closing =] */
  129. continue;
  130. }
  131. if (*arg++ != '-') {
  132. *buffer++ = '[';
  133. arg -= 2;
  134. continue;
  135. }
  136. ac = *arg++;
  137. while (i <= ac)
  138. *buffer++ = i++;
  139. arg++; /* Skip the assumed ']' */
  140. } else
  141. *buffer++ = *arg++;
  142. }
  143. return (buffer - buffer_start);
  144. }
  145. static int complement(char *buffer, int buffer_len)
  146. {
  147. int i, j, ix;
  148. char conv[ASCII + 2];
  149. ix = 0;
  150. for (i = 0; i <= ASCII; i++) {
  151. for (j = 0; j < buffer_len; j++)
  152. if (buffer[j] == i)
  153. break;
  154. if (j == buffer_len)
  155. conv[ix++] = i & ASCII;
  156. }
  157. memcpy(buffer, conv, ix);
  158. return ix;
  159. }
  160. int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  161. int tr_main(int argc, char **argv)
  162. {
  163. unsigned char *ptr;
  164. int output_length = 0, input_length;
  165. int idx = 1;
  166. int i;
  167. smalluint flags = 0;
  168. ssize_t read_chars = 0;
  169. size_t in_index = 0, out_index = 0, c, coded, last = -1;
  170. RESERVE_CONFIG_UBUFFER(output, BUFSIZ);
  171. RESERVE_CONFIG_BUFFER(vector, ASCII+1);
  172. RESERVE_CONFIG_BUFFER(invec, ASCII+1);
  173. RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
  174. if (argc > 1 && argv[idx][0] == '-') {
  175. for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
  176. if (*ptr == 'c')
  177. flags |= TR_OPT_complement;
  178. else if (*ptr == 'd')
  179. flags |= TR_OPT_delete;
  180. else if (*ptr == 's')
  181. flags |= TR_OPT_squeeze_reps;
  182. else
  183. bb_show_usage();
  184. }
  185. idx++;
  186. }
  187. for (i = 0; i <= ASCII; i++) {
  188. vector[i] = i;
  189. invec[i] = outvec[i] = FALSE;
  190. }
  191. #define tr_buf bb_common_bufsiz1
  192. if (argv[idx] != NULL) {
  193. input_length = expand(argv[idx++], tr_buf);
  194. if (flags & TR_OPT_complement)
  195. input_length = complement(tr_buf, input_length);
  196. if (argv[idx] != NULL) {
  197. if (*argv[idx] == '\0')
  198. bb_error_msg_and_die("STRING2 cannot be empty");
  199. output_length = expand(argv[idx], output);
  200. map(vector, 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 (in_index == read_chars) {
  210. if (out_index) {
  211. xwrite(STDOUT_FILENO, (char *)output, out_index);
  212. out_index = 0;
  213. }
  214. read_chars = read(STDIN_FILENO, tr_buf, BUFSIZ);
  215. if (read_chars <= 0) {
  216. xwrite(STDOUT_FILENO, (char *)output, out_index);
  217. if (read_chars < 0)
  218. bb_perror_msg_and_die(bb_msg_read_error);
  219. exit(EXIT_SUCCESS);
  220. }
  221. in_index = 0;
  222. }
  223. c = tr_buf[in_index++];
  224. coded = vector[c];
  225. if ((flags & TR_OPT_delete) && invec[c])
  226. continue;
  227. if ((flags & TR_OPT_squeeze_reps) && last == coded &&
  228. (invec[c] || outvec[coded]))
  229. continue;
  230. output[out_index++] = last = coded;
  231. }
  232. /* NOTREACHED */
  233. return EXIT_SUCCESS;
  234. }