3
0

expand.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* expand - convert tabs to spaces
  2. * unexpand - convert spaces to tabs
  3. *
  4. * Copyright (C) 89, 91, 1995-2006 Free Software Foundation, Inc.
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. *
  8. * David MacKenzie <djm@gnu.ai.mit.edu>
  9. *
  10. * Options for expand:
  11. * -t num --tabs NUM Convert tabs to num spaces (default 8 spaces).
  12. * -i --initial Only convert initial tabs on each line to spaces.
  13. *
  14. * Options for unexpand:
  15. * -a --all Convert all blanks, instead of just initial blanks.
  16. * -f --first-only Convert only leading sequences of blanks (default).
  17. * -t num --tabs NUM Have tabs num characters apart instead of 8.
  18. *
  19. * Busybox version (C) 2007 by Tito Ragusa <farmatito@tiscali.it>
  20. *
  21. * Caveat: this versions of expand and unexpand don't accept tab lists.
  22. */
  23. //config:config EXPAND
  24. //config: bool "expand (5.1 kb)"
  25. //config: default y
  26. //config: help
  27. //config: By default, convert all tabs to spaces.
  28. //config:
  29. //config:config UNEXPAND
  30. //config: bool "unexpand (5.3 kb)"
  31. //config: default y
  32. //config: help
  33. //config: By default, convert only leading sequences of blanks to tabs.
  34. //applet:IF_EXPAND(APPLET(expand, BB_DIR_USR_BIN, BB_SUID_DROP))
  35. // APPLET_ODDNAME:name main location suid_type help
  36. //applet:IF_UNEXPAND(APPLET_ODDNAME(unexpand, expand, BB_DIR_USR_BIN, BB_SUID_DROP, unexpand))
  37. //kbuild:lib-$(CONFIG_EXPAND) += expand.o
  38. //kbuild:lib-$(CONFIG_UNEXPAND) += expand.o
  39. //usage:#define expand_trivial_usage
  40. //usage: "[-i] [-t N] [FILE]..."
  41. //usage:#define expand_full_usage "\n\n"
  42. //usage: "Convert tabs to spaces, writing to stdout\n"
  43. //usage: "\n -i Don't convert tabs after non blanks"
  44. //usage: "\n -t Tabstops every N chars"
  45. //usage:#define unexpand_trivial_usage
  46. //usage: "[-fa][-t N] [FILE]..."
  47. //usage:#define unexpand_full_usage "\n\n"
  48. //usage: "Convert spaces to tabs, writing to stdout\n"
  49. //usage: "\n -a Convert all blanks"
  50. //usage: "\n -f Convert only leading blanks"
  51. //usage: "\n -t N Tabstops every N chars"
  52. #include "libbb.h"
  53. #include "unicode.h"
  54. enum {
  55. OPT_INITIAL = 1 << 0,
  56. OPT_TABS = 1 << 1,
  57. OPT_ALL = 1 << 2,
  58. };
  59. #if ENABLE_EXPAND
  60. static void expand(FILE *file, unsigned tab_size, unsigned opt)
  61. {
  62. char *line;
  63. while ((line = xmalloc_fgets(file)) != NULL) {
  64. unsigned char c;
  65. char *ptr;
  66. char *ptr_strbeg;
  67. ptr = ptr_strbeg = line;
  68. while ((c = *ptr) != '\0') {
  69. if ((opt & OPT_INITIAL) && !isblank(c)) {
  70. /* not space or tab */
  71. break;
  72. }
  73. if (c == '\t') {
  74. unsigned len;
  75. *ptr = '\0';
  76. # if ENABLE_UNICODE_SUPPORT
  77. len = unicode_strwidth(ptr_strbeg);
  78. # else
  79. len = ptr - ptr_strbeg;
  80. # endif
  81. len = tab_size - (len % tab_size);
  82. /*while (ptr[1] == '\t') { ptr++; len += tab_size; } - can handle many tabs at once */
  83. printf("%s%*s", ptr_strbeg, len, "");
  84. ptr_strbeg = ptr + 1;
  85. }
  86. ptr++;
  87. }
  88. fputs(ptr_strbeg, stdout);
  89. free(line);
  90. }
  91. }
  92. #endif
  93. #if ENABLE_UNEXPAND
  94. static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
  95. {
  96. char *line;
  97. while ((line = xmalloc_fgets(file)) != NULL) {
  98. char *ptr = line;
  99. unsigned column = 0;
  100. while (*ptr) {
  101. unsigned n;
  102. unsigned len = 0;
  103. while (*ptr == ' ') {
  104. ptr++;
  105. len++;
  106. }
  107. column += len;
  108. if (*ptr == '\t') {
  109. column += tab_size - (column % tab_size);
  110. ptr++;
  111. continue;
  112. }
  113. n = column / tab_size;
  114. if (n) {
  115. len = column = column % tab_size;
  116. while (n--)
  117. putchar('\t');
  118. }
  119. if ((opt & OPT_INITIAL) && ptr != line) {
  120. printf("%*s%s", len, "", ptr);
  121. break;
  122. }
  123. n = strcspn(ptr, "\t ");
  124. printf("%*s%.*s", len, "", n, ptr);
  125. # if ENABLE_UNICODE_SUPPORT
  126. {
  127. char c = ptr[n];
  128. ptr[n] = '\0';
  129. len = unicode_strwidth(ptr);
  130. ptr[n] = c;
  131. }
  132. # else
  133. len = n;
  134. # endif
  135. ptr += n;
  136. column = (column + len) % tab_size;
  137. }
  138. free(line);
  139. }
  140. }
  141. #endif
  142. int expand_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  143. int expand_main(int argc UNUSED_PARAM, char **argv)
  144. {
  145. /* Default 8 spaces for 1 tab */
  146. const char *opt_t = "8";
  147. FILE *file;
  148. unsigned tab_size;
  149. unsigned opt;
  150. int exit_status = EXIT_SUCCESS;
  151. init_unicode();
  152. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
  153. opt = getopt32long(argv, "it:",
  154. "initial\0" No_argument "i"
  155. "tabs\0" Required_argument "t"
  156. , &opt_t
  157. );
  158. } else {
  159. opt = getopt32long(argv, "^"
  160. "ft:a"
  161. "\0"
  162. "ta" /* -t NUM sets -a */,
  163. "first-only\0" No_argument "i"
  164. "tabs\0" Required_argument "t"
  165. "all\0" No_argument "a"
  166. , &opt_t
  167. );
  168. /* -f --first-only is the default */
  169. if (!(opt & OPT_ALL)) opt |= OPT_INITIAL;
  170. }
  171. tab_size = xatou_range(opt_t, 1, UINT_MAX);
  172. argv += optind;
  173. if (!*argv) {
  174. *--argv = (char*)bb_msg_standard_input;
  175. }
  176. do {
  177. file = fopen_or_warn_stdin(*argv);
  178. if (!file) {
  179. exit_status = EXIT_FAILURE;
  180. continue;
  181. }
  182. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e'))
  183. IF_EXPAND(expand(file, tab_size, opt));
  184. else
  185. IF_UNEXPAND(unexpand(file, tab_size, opt));
  186. /* Check and close the file */
  187. if (fclose_if_not_stdin(file)) {
  188. bb_simple_perror_msg(*argv);
  189. exit_status = EXIT_FAILURE;
  190. }
  191. /* If stdin also clear EOF */
  192. if (file == stdin)
  193. clearerr(file);
  194. } while (*++argv);
  195. /* Now close stdin also */
  196. /* (if we didn't read from it, it's a no-op) */
  197. if (fclose(stdin))
  198. bb_perror_msg_and_die(bb_msg_standard_input);
  199. fflush_stdout_and_exit(exit_status);
  200. }