tr.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini tr implementation for busybox
  4. *
  5. * Copyright (c) Michiel Huisjes
  6. *
  7. * This version of tr is adapted from Minix tr and was modified
  8. * by Erik Andersen <andersen@codepoet.org> to be used in busybox.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Original copyright notice is retained at the end of this file.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <sys/types.h>
  31. #include "busybox.h"
  32. /* This must be a #define, since when CONFIG_DEBUG and BUFFERS_GO_IN_BSS are
  33. * enabled, we otherwise get a "storage size isn't constant error. */
  34. #define ASCII 0377
  35. /* some "globals" shared across this file */
  36. static char com_fl, del_fl, sq_fl;
  37. static short in_index, out_index;
  38. /* these last are pointers to static buffers declared in tr_main */
  39. static unsigned char *poutput, *pinput;
  40. static unsigned char *pvector;
  41. static char *pinvec, *poutvec;
  42. static void convert(void)
  43. {
  44. short read_chars = 0;
  45. short c, coded;
  46. short last = -1;
  47. for (;;) {
  48. if (in_index == read_chars) {
  49. if ((read_chars = read(0, (char *) pinput, BUFSIZ)) <= 0) {
  50. if (write(1, (char *) poutput, out_index) != out_index)
  51. bb_error_msg(bb_msg_write_error);
  52. exit(0);
  53. }
  54. in_index = 0;
  55. }
  56. c = pinput[in_index++];
  57. coded = pvector[c];
  58. if (del_fl && pinvec[c])
  59. continue;
  60. if (sq_fl && last == coded && (pinvec[c] || poutvec[coded]))
  61. continue;
  62. poutput[out_index++] = last = coded;
  63. if (out_index == BUFSIZ) {
  64. if (write(1, (char *) poutput, out_index) != out_index)
  65. bb_error_msg_and_die(bb_msg_write_error);
  66. out_index = 0;
  67. }
  68. }
  69. /* NOTREACHED */
  70. }
  71. static void map(register unsigned char *string1, unsigned int string1_len,
  72. register unsigned char *string2, unsigned int string2_len)
  73. {
  74. unsigned char last = '0';
  75. unsigned int i, j;
  76. for (j = 0, i = 0; i < string1_len; i++) {
  77. if (string2_len <= j)
  78. pvector[string1[i]] = last;
  79. else
  80. pvector[string1[i]] = last = string2[j++];
  81. }
  82. }
  83. /* supported constructs:
  84. * Ranges, e.g., [0-9] ==> 0123456789
  85. * Escapes, e.g., \a ==> Control-G
  86. */
  87. static unsigned int expand(const char *arg, register unsigned char *buffer)
  88. {
  89. unsigned char *buffer_start = buffer;
  90. int i, ac;
  91. while (*arg) {
  92. if (*arg == '\\') {
  93. arg++;
  94. *buffer++ = bb_process_escape_sequence(&arg);
  95. } else if (*(arg+1) == '-') {
  96. ac = *(arg+2);
  97. if(ac == 0) {
  98. *buffer++ = *arg++;
  99. continue;
  100. }
  101. i = *arg;
  102. while (i <= ac)
  103. *buffer++ = i++;
  104. arg += 3; /* Skip the assumed a-z */
  105. } else if (*arg == '[') {
  106. arg++;
  107. i = *arg++;
  108. if (*arg++ != '-') {
  109. *buffer++ = '[';
  110. arg -= 2;
  111. continue;
  112. }
  113. ac = *arg++;
  114. while (i <= ac)
  115. *buffer++ = i++;
  116. arg++; /* Skip the assumed ']' */
  117. } else
  118. *buffer++ = *arg++;
  119. }
  120. return (buffer - buffer_start);
  121. }
  122. static int complement(unsigned char *buffer, int buffer_len)
  123. {
  124. register short i, j, ix;
  125. char conv[ASCII + 2];
  126. ix = 0;
  127. for (i = 0; i <= ASCII; i++) {
  128. for (j = 0; j < buffer_len; j++)
  129. if (buffer[j] == i)
  130. break;
  131. if (j == buffer_len)
  132. conv[ix++] = i & ASCII;
  133. }
  134. memcpy(buffer, conv, ix);
  135. return ix;
  136. }
  137. extern int tr_main(int argc, char **argv)
  138. {
  139. register unsigned char *ptr;
  140. int output_length=0, input_length;
  141. int idx = 1;
  142. int i;
  143. RESERVE_CONFIG_BUFFER(output, BUFSIZ);
  144. RESERVE_CONFIG_BUFFER(input, BUFSIZ);
  145. RESERVE_CONFIG_UBUFFER(vector, ASCII+1);
  146. RESERVE_CONFIG_BUFFER(invec, ASCII+1);
  147. RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
  148. /* ... but make them available globally */
  149. poutput = output;
  150. pinput = input;
  151. pvector = vector;
  152. pinvec = invec;
  153. poutvec = outvec;
  154. if (argc > 1 && argv[idx][0] == '-') {
  155. for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
  156. switch (*ptr) {
  157. case 'c':
  158. com_fl = TRUE;
  159. break;
  160. case 'd':
  161. del_fl = TRUE;
  162. break;
  163. case 's':
  164. sq_fl = TRUE;
  165. break;
  166. default:
  167. bb_show_usage();
  168. }
  169. }
  170. idx++;
  171. }
  172. for (i = 0; i <= ASCII; i++) {
  173. vector[i] = i;
  174. invec[i] = outvec[i] = FALSE;
  175. }
  176. if (argv[idx] != NULL) {
  177. input_length = expand(argv[idx++], input);
  178. if (com_fl)
  179. input_length = complement(input, input_length);
  180. if (argv[idx] != NULL) {
  181. if (*argv[idx] == '\0')
  182. bb_error_msg_and_die("STRING2 cannot be empty");
  183. output_length = expand(argv[idx], output);
  184. map(input, input_length, output, output_length);
  185. }
  186. for (i = 0; i < input_length; i++)
  187. invec[(unsigned char)input[i]] = TRUE;
  188. for (i = 0; i < output_length; i++)
  189. outvec[(unsigned char)output[i]] = TRUE;
  190. }
  191. convert();
  192. return (0);
  193. }
  194. /*
  195. * Copyright (c) 1987,1997, Prentice Hall
  196. * All rights reserved.
  197. *
  198. * Redistribution and use of the MINIX operating system in source and
  199. * binary forms, with or without modification, are permitted provided
  200. * that the following conditions are met:
  201. *
  202. * Redistributions of source code must retain the above copyright
  203. * notice, this list of conditions and the following disclaimer.
  204. *
  205. * Redistributions in binary form must reproduce the above
  206. * copyright notice, this list of conditions and the following
  207. * disclaimer in the documentation and/or other materials provided
  208. * with the distribution.
  209. *
  210. * Neither the name of Prentice Hall nor the names of the software
  211. * authors or contributors may be used to endorse or promote
  212. * products derived from this software without specific prior
  213. * written permission.
  214. *
  215. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
  216. * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  217. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  218. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  219. * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
  220. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  221. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  222. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  223. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  224. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  225. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  226. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  227. *
  228. */