tr.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. if (!*arg || arg[1] != '=' || arg[2] != ']')
  134. bb_show_usage();
  135. arg += 3; /* skip CHAR=] */
  136. continue;
  137. }
  138. if (i == '\0' || *arg != '-') { /* not [x-...] - copy verbatim */
  139. *buffer++ = '[';
  140. arg--; /* points to x */
  141. continue; /* copy all, including eventual ']' */
  142. }
  143. /* [x-z] */
  144. arg++; /* skip - */
  145. if (arg[0] == '\0' || arg[1] != ']')
  146. bb_show_usage();
  147. ac = *arg++;
  148. while (i <= ac)
  149. *buffer++ = i++;
  150. arg++; /* skip ] */
  151. continue;
  152. }
  153. *buffer++ = *arg++;
  154. }
  155. return (buffer - buffer_start);
  156. }
  157. static int complement(char *buffer, int buffer_len)
  158. {
  159. int i, j, ix;
  160. char conv[ASCII + 2];
  161. ix = 0;
  162. for (i = '\0'; i <= ASCII; i++) {
  163. for (j = 0; j < buffer_len; j++)
  164. if (buffer[j] == i)
  165. break;
  166. if (j == buffer_len)
  167. conv[ix++] = i & ASCII;
  168. }
  169. memcpy(buffer, conv, ix);
  170. return ix;
  171. }
  172. int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  173. int tr_main(int argc UNUSED_PARAM, char **argv)
  174. {
  175. int output_length = 0, input_length;
  176. int i;
  177. smalluint flags;
  178. ssize_t read_chars = 0;
  179. size_t in_index = 0, out_index = 0;
  180. unsigned last = UCHAR_MAX + 1; /* not equal to any char */
  181. unsigned char coded, c;
  182. unsigned char *output = xmalloc(BUFSIZ);
  183. char *vector = xzalloc((ASCII+1) * 3);
  184. char *invec = vector + (ASCII+1);
  185. char *outvec = vector + (ASCII+1) * 2;
  186. #define TR_OPT_complement (1 << 0)
  187. #define TR_OPT_delete (1 << 1)
  188. #define TR_OPT_squeeze_reps (1 << 2)
  189. flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */
  190. argv += optind;
  191. for (i = 0; i <= ASCII; i++) {
  192. vector[i] = i;
  193. /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
  194. }
  195. #define tr_buf bb_common_bufsiz1
  196. if (*argv != NULL) {
  197. input_length = expand(*argv++, tr_buf);
  198. if (flags & TR_OPT_complement)
  199. input_length = complement(tr_buf, input_length);
  200. if (*argv) {
  201. if (argv[0][0] == '\0')
  202. bb_error_msg_and_die("STRING2 cannot be empty");
  203. output_length = expand(*argv, (char *)output);
  204. map(vector, (unsigned char *)tr_buf, input_length, output, output_length);
  205. }
  206. for (i = 0; i < input_length; i++)
  207. invec[(unsigned char)tr_buf[i]] = TRUE;
  208. for (i = 0; i < output_length; i++)
  209. outvec[output[i]] = TRUE;
  210. }
  211. for (;;) {
  212. /* If we're out of input, flush output and read more input. */
  213. if ((ssize_t)in_index == read_chars) {
  214. if (out_index) {
  215. xwrite(STDOUT_FILENO, (char *)output, out_index);
  216. out_index = 0;
  217. }
  218. read_chars = safe_read(STDIN_FILENO, tr_buf, BUFSIZ);
  219. if (read_chars <= 0) {
  220. if (read_chars < 0)
  221. bb_perror_msg_and_die(bb_msg_read_error);
  222. exit(EXIT_SUCCESS);
  223. }
  224. in_index = 0;
  225. }
  226. c = tr_buf[in_index++];
  227. coded = vector[c];
  228. if ((flags & TR_OPT_delete) && invec[c])
  229. continue;
  230. if ((flags & TR_OPT_squeeze_reps) && last == coded
  231. && (invec[c] || outvec[coded]))
  232. continue;
  233. output[out_index++] = last = coded;
  234. }
  235. /* NOTREACHED */
  236. return EXIT_SUCCESS;
  237. }