3
0

tr.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <ctype.h>
  31. #include <sys/types.h>
  32. #include "busybox.h"
  33. #define ASCII 0377
  34. /* some "globals" shared across this file */
  35. static char com_fl, del_fl, sq_fl;
  36. static short in_index, out_index;
  37. /* these last are pointers to static buffers declared in tr_main */
  38. static unsigned char *poutput;
  39. static unsigned char *pvector;
  40. static unsigned char *pinvec, *poutvec;
  41. #define input bb_common_bufsiz1
  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, input, 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 = input[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. * Character classes, e.g. [:upper:] ==> A ... Z
  87. */
  88. static unsigned int expand(const char *arg, register unsigned char *buffer)
  89. {
  90. unsigned char *buffer_start = buffer;
  91. int i, ac;
  92. while (*arg) {
  93. if (*arg == '\\') {
  94. arg++;
  95. *buffer++ = bb_process_escape_sequence(&arg);
  96. } else if (*(arg+1) == '-') {
  97. ac = *(arg+2);
  98. if(ac == 0) {
  99. *buffer++ = *arg++;
  100. continue;
  101. }
  102. i = *arg;
  103. while (i <= ac)
  104. *buffer++ = i++;
  105. arg += 3; /* Skip the assumed a-z */
  106. } else if (*arg == '[') {
  107. arg++;
  108. if (ENABLE_FEATURE_TR_CLASSES && *arg++ == ':') {
  109. if (strncmp(arg, "alpha", 5) == 0) {
  110. for (i = 'A'; i <= 'Z'; i++)
  111. *buffer++ = i;
  112. for (i = 'a'; i <= 'z'; i++)
  113. *buffer++ = i;
  114. }
  115. else if (strncmp(arg, "alnum", 5) == 0) {
  116. for (i = 'A'; i <= 'Z'; i++)
  117. *buffer++ = i;
  118. for (i = 'a'; i <= 'z'; i++)
  119. *buffer++ = i;
  120. for (i = '0'; i <= '9'; i++)
  121. *buffer++ = i;
  122. }
  123. else if (strncmp(arg, "digit", 5) == 0)
  124. for (i = '0'; i <= '9'; i++)
  125. *buffer++ = i;
  126. else if (strncmp(arg, "lower", 5) == 0)
  127. for (i = 'a'; i <= 'z'; i++)
  128. *buffer++ = i;
  129. else if (strncmp(arg, "upper", 5) == 0)
  130. for (i = 'A'; i <= 'Z'; i++)
  131. *buffer++ = i;
  132. else if (strncmp(arg, "space", 5) == 0)
  133. strcat((char*)buffer, " \f\n\r\t\v");
  134. else if (strncmp(arg, "blank", 5) == 0)
  135. strcat((char*)buffer, " \t");
  136. /* gcc gives a warning if braces aren't used here */
  137. else if (strncmp(arg, "punct", 5) == 0) {
  138. for (i = 0; i <= ASCII; i++)
  139. if (isprint(i) && (!isalnum(i)) && (!isspace(i)))
  140. *buffer++ = i;
  141. }
  142. else if (strncmp(arg, "cntrl", 5) == 0) {
  143. for (i = 0; i <= ASCII; i++)
  144. if (iscntrl(i))
  145. *buffer++ = i;
  146. }
  147. else {
  148. strcat((char*)buffer, "[:");
  149. arg++;
  150. continue;
  151. }
  152. break;
  153. }
  154. if (ENABLE_FEATURE_TR_EQUIV && *arg++ == '=') {
  155. *buffer++ = *arg;
  156. /* skip the closing =] */
  157. arg += 3;
  158. continue;
  159. }
  160. if (*arg++ != '-') {
  161. *buffer++ = '[';
  162. arg -= 2;
  163. continue;
  164. }
  165. i = *arg++;
  166. ac = *arg++;
  167. while (i <= ac)
  168. *buffer++ = i++;
  169. arg++; /* Skip the assumed ']' */
  170. } else
  171. *buffer++ = *arg++;
  172. }
  173. return (buffer - buffer_start);
  174. }
  175. static int complement(unsigned char *buffer, int buffer_len)
  176. {
  177. register short i, j, ix;
  178. char conv[ASCII + 2];
  179. ix = 0;
  180. for (i = 0; i <= ASCII; i++) {
  181. for (j = 0; j < buffer_len; j++)
  182. if (buffer[j] == i)
  183. break;
  184. if (j == buffer_len)
  185. conv[ix++] = i & ASCII;
  186. }
  187. memcpy(buffer, conv, ix);
  188. return ix;
  189. }
  190. int tr_main(int argc, char **argv)
  191. {
  192. register unsigned char *ptr;
  193. int output_length=0, input_length;
  194. int idx = 1;
  195. int i;
  196. RESERVE_CONFIG_BUFFER(output, BUFSIZ);
  197. RESERVE_CONFIG_UBUFFER(vector, ASCII+1);
  198. RESERVE_CONFIG_BUFFER(invec, ASCII+1);
  199. RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
  200. /* ... but make them available globally */
  201. poutput = (unsigned char*)output;
  202. pvector = (unsigned char*)vector;
  203. pinvec = (unsigned char*)invec;
  204. poutvec = (unsigned char*)outvec;
  205. if (argc > 1 && argv[idx][0] == '-') {
  206. for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
  207. switch (*ptr) {
  208. case 'c':
  209. com_fl = TRUE;
  210. break;
  211. case 'd':
  212. del_fl = TRUE;
  213. break;
  214. case 's':
  215. sq_fl = TRUE;
  216. break;
  217. default:
  218. bb_show_usage();
  219. }
  220. }
  221. idx++;
  222. }
  223. for (i = 0; i <= ASCII; i++) {
  224. vector[i] = i;
  225. invec[i] = outvec[i] = FALSE;
  226. }
  227. if (argv[idx] != NULL) {
  228. input_length = expand(argv[idx++], (unsigned char*)input);
  229. if (com_fl)
  230. input_length = complement((unsigned char*)input, input_length);
  231. if (argv[idx] != NULL) {
  232. if (*argv[idx] == '\0')
  233. bb_error_msg_and_die("STRING2 cannot be empty");
  234. output_length = expand(argv[idx], (unsigned char*)output);
  235. map((unsigned char*)input, input_length, (unsigned char*)output, output_length);
  236. }
  237. for (i = 0; i < input_length; i++)
  238. invec[(unsigned char)input[i]] = TRUE;
  239. for (i = 0; i < output_length; i++)
  240. outvec[(unsigned char)output[i]] = TRUE;
  241. }
  242. convert();
  243. return (0);
  244. }
  245. /*
  246. * Copyright (c) 1987,1997, Prentice Hall
  247. * All rights reserved.
  248. *
  249. * Redistribution and use of the MINIX operating system in source and
  250. * binary forms, with or without modification, are permitted provided
  251. * that the following conditions are met:
  252. *
  253. * Redistributions of source code must retain the above copyright
  254. * notice, this list of conditions and the following disclaimer.
  255. *
  256. * Redistributions in binary form must reproduce the above
  257. * copyright notice, this list of conditions and the following
  258. * disclaimer in the documentation and/or other materials provided
  259. * with the distribution.
  260. *
  261. * Neither the name of Prentice Hall nor the names of the software
  262. * authors or contributors may be used to endorse or promote
  263. * products derived from this software without specific prior
  264. * written permission.
  265. *
  266. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
  267. * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  268. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  269. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  270. * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
  271. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  272. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  273. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  274. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  275. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  276. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  277. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  278. *
  279. */