3
0

tr.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #include "busybox.h"
  19. // Even with -funsigned-char, gcc still complains about char as an array index.
  20. #define GCC4_IS_STUPID int
  21. #define ASCII 0377
  22. /* some "globals" shared across this file */
  23. static char com_fl, del_fl, sq_fl;
  24. /* these last are pointers to static buffers declared in tr_main */
  25. static char *poutput, *pvector, *pinvec, *poutvec;
  26. static void convert(void)
  27. {
  28. int read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
  29. for (;;) {
  30. // If we're out of input, flush output and read more input.
  31. if (in_index == read_chars) {
  32. if (out_index) {
  33. if (write(1, (char *) poutput, out_index) != out_index)
  34. bb_error_msg_and_die(bb_msg_write_error);
  35. out_index = 0;
  36. }
  37. if ((read_chars = read(0, bb_common_bufsiz1, BUFSIZ)) <= 0) {
  38. if (write(1, (char *) poutput, out_index) != out_index)
  39. bb_error_msg(bb_msg_write_error);
  40. exit(0);
  41. }
  42. in_index = 0;
  43. }
  44. c = bb_common_bufsiz1[in_index++];
  45. coded = pvector[c];
  46. if (del_fl && pinvec[c])
  47. continue;
  48. if (sq_fl && last == coded && (pinvec[c] || poutvec[coded]))
  49. continue;
  50. poutput[out_index++] = last = coded;
  51. }
  52. /* NOTREACHED */
  53. }
  54. static void map(char *string1, unsigned int string1_len,
  55. char *string2, unsigned int string2_len)
  56. {
  57. char last = '0';
  58. unsigned int i, j;
  59. for (j = 0, i = 0; i < string1_len; i++) {
  60. if (string2_len <= j)
  61. pvector[(GCC4_IS_STUPID)string1[i]] = last;
  62. else
  63. pvector[(GCC4_IS_STUPID)string1[i]] = last = string2[j++];
  64. }
  65. }
  66. /* supported constructs:
  67. * Ranges, e.g., [0-9] ==> 0123456789
  68. * Escapes, e.g., \a ==> Control-G
  69. * Character classes, e.g. [:upper:] ==> A ... Z
  70. */
  71. static unsigned int expand(const char *arg, char *buffer)
  72. {
  73. char *buffer_start = buffer;
  74. int i, ac;
  75. while (*arg) {
  76. if (*arg == '\\') {
  77. arg++;
  78. *buffer++ = bb_process_escape_sequence(&arg);
  79. } else if (*(arg+1) == '-') {
  80. ac = *(arg+2);
  81. if(ac == 0) {
  82. *buffer++ = *arg++;
  83. continue;
  84. }
  85. i = *arg;
  86. while (i <= ac)
  87. *buffer++ = i++;
  88. arg += 3; /* Skip the assumed a-z */
  89. } else if (*arg == '[') {
  90. arg++;
  91. i = *arg++;
  92. if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
  93. if (strncmp(arg, "alpha", 5) == 0) {
  94. for (i = 'A'; i <= 'Z'; i++)
  95. *buffer++ = i;
  96. for (i = 'a'; i <= 'z'; i++)
  97. *buffer++ = i;
  98. }
  99. else if (strncmp(arg, "alnum", 5) == 0) {
  100. for (i = '0'; i <= '9'; i++)
  101. *buffer++ = i;
  102. for (i = 'A'; i <= 'Z'; i++)
  103. *buffer++ = i;
  104. for (i = 'a'; i <= 'z'; i++)
  105. *buffer++ = i;
  106. }
  107. else if (strncmp(arg, "digit", 5) == 0)
  108. for (i = '0'; i <= '9'; i++)
  109. *buffer++ = i;
  110. else if (strncmp(arg, "lower", 5) == 0)
  111. for (i = 'a'; i <= 'z'; i++)
  112. *buffer++ = i;
  113. else if (strncmp(arg, "upper", 5) == 0)
  114. for (i = 'A'; i <= 'Z'; i++)
  115. *buffer++ = i;
  116. else if (strncmp(arg, "space", 5) == 0) {
  117. const char s[] = "\t\n\v\f\r ";
  118. strcat((char*)buffer, s);
  119. buffer += sizeof(s) - 1;
  120. }
  121. else if (strncmp(arg, "blank", 5) == 0) {
  122. *buffer++ = '\t';
  123. *buffer++ = ' ';
  124. }
  125. /* gcc gives a warning if braces aren't used here */
  126. else if (strncmp(arg, "punct", 5) == 0) {
  127. for (i = 0; i <= ASCII; i++)
  128. if (isprint(i) && (!isalnum(i)) && (!isspace(i)))
  129. *buffer++ = i;
  130. }
  131. else if (strncmp(arg, "cntrl", 5) == 0) {
  132. for (i = 0; i <= ASCII; i++)
  133. if (iscntrl(i))
  134. *buffer++ = i;
  135. }
  136. else {
  137. *buffer++ = '[';
  138. *buffer++ = ':';
  139. continue;
  140. }
  141. break;
  142. }
  143. if (ENABLE_FEATURE_TR_EQUIV && i == '=') {
  144. *buffer++ = *arg;
  145. /* skip the closing =] */
  146. arg += 3;
  147. continue;
  148. }
  149. if (*arg++ != '-') {
  150. *buffer++ = '[';
  151. arg -= 2;
  152. continue;
  153. }
  154. ac = *arg++;
  155. while (i <= ac)
  156. *buffer++ = i++;
  157. arg++; /* Skip the assumed ']' */
  158. } else
  159. *buffer++ = *arg++;
  160. }
  161. return (buffer - buffer_start);
  162. }
  163. static int complement(char *buffer, int buffer_len)
  164. {
  165. short i, j, ix;
  166. char conv[ASCII + 2];
  167. ix = 0;
  168. for (i = 0; i <= ASCII; i++) {
  169. for (j = 0; j < buffer_len; j++)
  170. if (buffer[j] == i)
  171. break;
  172. if (j == buffer_len)
  173. conv[ix++] = i & ASCII;
  174. }
  175. memcpy(buffer, conv, ix);
  176. return ix;
  177. }
  178. int tr_main(int argc, char **argv)
  179. {
  180. unsigned char *ptr;
  181. int output_length=0, input_length;
  182. int idx = 1;
  183. int i;
  184. RESERVE_CONFIG_BUFFER(output, BUFSIZ);
  185. RESERVE_CONFIG_BUFFER(vector, ASCII+1);
  186. RESERVE_CONFIG_BUFFER(invec, ASCII+1);
  187. RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
  188. /* ... but make them available globally */
  189. poutput = output;
  190. pvector = vector;
  191. pinvec = invec;
  192. poutvec = outvec;
  193. if (argc > 1 && argv[idx][0] == '-') {
  194. for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
  195. switch (*ptr) {
  196. case 'c':
  197. com_fl = TRUE;
  198. break;
  199. case 'd':
  200. del_fl = TRUE;
  201. break;
  202. case 's':
  203. sq_fl = TRUE;
  204. break;
  205. default:
  206. bb_show_usage();
  207. }
  208. }
  209. idx++;
  210. }
  211. for (i = 0; i <= ASCII; i++) {
  212. vector[i] = i;
  213. invec[i] = outvec[i] = FALSE;
  214. }
  215. if (argv[idx] != NULL) {
  216. input_length = expand(argv[idx++], bb_common_bufsiz1);
  217. if (com_fl)
  218. input_length = complement(bb_common_bufsiz1, input_length);
  219. if (argv[idx] != NULL) {
  220. if (*argv[idx] == '\0')
  221. bb_error_msg_and_die("STRING2 cannot be empty");
  222. output_length = expand(argv[idx], output);
  223. map(bb_common_bufsiz1, input_length, output, output_length);
  224. }
  225. for (i = 0; i < input_length; i++)
  226. invec[(GCC4_IS_STUPID)bb_common_bufsiz1[i]] = TRUE;
  227. for (i = 0; i < output_length; i++)
  228. outvec[(GCC4_IS_STUPID)output[i]] = TRUE;
  229. }
  230. convert();
  231. return 0;
  232. }