expand.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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"
  25. //config: default y
  26. //config: help
  27. //config: By default, convert all tabs to spaces.
  28. //config:
  29. //config:config FEATURE_EXPAND_LONG_OPTIONS
  30. //config: bool "Enable long options"
  31. //config: default y
  32. //config: depends on EXPAND && LONG_OPTS
  33. //config: help
  34. //config: Support long options for the expand applet.
  35. //config:
  36. //config:config UNEXPAND
  37. //config: bool "unexpand"
  38. //config: default y
  39. //config: help
  40. //config: By default, convert only leading sequences of blanks to tabs.
  41. //config:
  42. //config:config FEATURE_UNEXPAND_LONG_OPTIONS
  43. //config: bool "Enable long options"
  44. //config: default y
  45. //config: depends on UNEXPAND && LONG_OPTS
  46. //config: help
  47. //config: Support long options for the unexpand applet.
  48. //applet:IF_EXPAND(APPLET(expand, BB_DIR_USR_BIN, BB_SUID_DROP))
  49. //applet:IF_UNEXPAND(APPLET_ODDNAME(unexpand, expand, BB_DIR_USR_BIN, BB_SUID_DROP, unexpand))
  50. //kbuild:lib-$(CONFIG_EXPAND) += expand.o
  51. //kbuild:lib-$(CONFIG_UNEXPAND) += expand.o
  52. //usage:#define expand_trivial_usage
  53. //usage: "[-i] [-t N] [FILE]..."
  54. //usage:#define expand_full_usage "\n\n"
  55. //usage: "Convert tabs to spaces, writing to stdout\n"
  56. //usage: IF_FEATURE_EXPAND_LONG_OPTIONS(
  57. //usage: "\n -i,--initial Don't convert tabs after non blanks"
  58. //usage: "\n -t,--tabs=N Tabstops every N chars"
  59. //usage: )
  60. //usage: IF_NOT_FEATURE_EXPAND_LONG_OPTIONS(
  61. //usage: "\n -i Don't convert tabs after non blanks"
  62. //usage: "\n -t Tabstops every N chars"
  63. //usage: )
  64. //usage:#define unexpand_trivial_usage
  65. //usage: "[-fa][-t N] [FILE]..."
  66. //usage:#define unexpand_full_usage "\n\n"
  67. //usage: "Convert spaces to tabs, writing to stdout\n"
  68. //usage: IF_FEATURE_UNEXPAND_LONG_OPTIONS(
  69. //usage: "\n -a,--all Convert all blanks"
  70. //usage: "\n -f,--first-only Convert only leading blanks"
  71. //usage: "\n -t,--tabs=N Tabstops every N chars"
  72. //usage: )
  73. //usage: IF_NOT_FEATURE_UNEXPAND_LONG_OPTIONS(
  74. //usage: "\n -a Convert all blanks"
  75. //usage: "\n -f Convert only leading blanks"
  76. //usage: "\n -t N Tabstops every N chars"
  77. //usage: )
  78. #include "libbb.h"
  79. #include "unicode.h"
  80. enum {
  81. OPT_INITIAL = 1 << 0,
  82. OPT_TABS = 1 << 1,
  83. OPT_ALL = 1 << 2,
  84. };
  85. #if ENABLE_EXPAND
  86. static void expand(FILE *file, unsigned tab_size, unsigned opt)
  87. {
  88. char *line;
  89. while ((line = xmalloc_fgets(file)) != NULL) {
  90. unsigned char c;
  91. char *ptr;
  92. char *ptr_strbeg;
  93. ptr = ptr_strbeg = line;
  94. while ((c = *ptr) != '\0') {
  95. if ((opt & OPT_INITIAL) && !isblank(c)) {
  96. /* not space or tab */
  97. break;
  98. }
  99. if (c == '\t') {
  100. unsigned len;
  101. *ptr = '\0';
  102. # if ENABLE_UNICODE_SUPPORT
  103. len = unicode_strwidth(ptr_strbeg);
  104. # else
  105. len = ptr - ptr_strbeg;
  106. # endif
  107. len = tab_size - (len % tab_size);
  108. /*while (ptr[1] == '\t') { ptr++; len += tab_size; } - can handle many tabs at once */
  109. printf("%s%*s", ptr_strbeg, len, "");
  110. ptr_strbeg = ptr + 1;
  111. }
  112. ptr++;
  113. }
  114. fputs(ptr_strbeg, stdout);
  115. free(line);
  116. }
  117. }
  118. #endif
  119. #if ENABLE_UNEXPAND
  120. static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
  121. {
  122. char *line;
  123. while ((line = xmalloc_fgets(file)) != NULL) {
  124. char *ptr = line;
  125. unsigned column = 0;
  126. while (*ptr) {
  127. unsigned n;
  128. unsigned len = 0;
  129. while (*ptr == ' ') {
  130. ptr++;
  131. len++;
  132. }
  133. column += len;
  134. if (*ptr == '\t') {
  135. column += tab_size - (column % tab_size);
  136. ptr++;
  137. continue;
  138. }
  139. n = column / tab_size;
  140. if (n) {
  141. len = column = column % tab_size;
  142. while (n--)
  143. putchar('\t');
  144. }
  145. if ((opt & OPT_INITIAL) && ptr != line) {
  146. printf("%*s%s", len, "", ptr);
  147. break;
  148. }
  149. n = strcspn(ptr, "\t ");
  150. printf("%*s%.*s", len, "", n, ptr);
  151. # if ENABLE_UNICODE_SUPPORT
  152. {
  153. char c = ptr[n];
  154. ptr[n] = '\0';
  155. len = unicode_strwidth(ptr);
  156. ptr[n] = c;
  157. }
  158. # else
  159. len = n;
  160. # endif
  161. ptr += n;
  162. column = (column + len) % tab_size;
  163. }
  164. free(line);
  165. }
  166. }
  167. #endif
  168. int expand_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  169. int expand_main(int argc UNUSED_PARAM, char **argv)
  170. {
  171. /* Default 8 spaces for 1 tab */
  172. const char *opt_t = "8";
  173. FILE *file;
  174. unsigned tab_size;
  175. unsigned opt;
  176. int exit_status = EXIT_SUCCESS;
  177. #if ENABLE_FEATURE_EXPAND_LONG_OPTIONS
  178. static const char expand_longopts[] ALIGN1 =
  179. /* name, has_arg, val */
  180. "initial\0" No_argument "i"
  181. "tabs\0" Required_argument "t"
  182. ;
  183. #endif
  184. #if ENABLE_FEATURE_UNEXPAND_LONG_OPTIONS
  185. static const char unexpand_longopts[] ALIGN1 =
  186. /* name, has_arg, val */
  187. "first-only\0" No_argument "i"
  188. "tabs\0" Required_argument "t"
  189. "all\0" No_argument "a"
  190. ;
  191. #endif
  192. init_unicode();
  193. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
  194. IF_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
  195. opt = getopt32(argv, "it:", &opt_t);
  196. } else {
  197. IF_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);
  198. /* -t NUM sets also -a */
  199. opt_complementary = "ta";
  200. opt = getopt32(argv, "ft:a", &opt_t);
  201. /* -f --first-only is the default */
  202. if (!(opt & OPT_ALL)) opt |= OPT_INITIAL;
  203. }
  204. tab_size = xatou_range(opt_t, 1, UINT_MAX);
  205. argv += optind;
  206. if (!*argv) {
  207. *--argv = (char*)bb_msg_standard_input;
  208. }
  209. do {
  210. file = fopen_or_warn_stdin(*argv);
  211. if (!file) {
  212. exit_status = EXIT_FAILURE;
  213. continue;
  214. }
  215. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e'))
  216. IF_EXPAND(expand(file, tab_size, opt));
  217. else
  218. IF_UNEXPAND(unexpand(file, tab_size, opt));
  219. /* Check and close the file */
  220. if (fclose_if_not_stdin(file)) {
  221. bb_simple_perror_msg(*argv);
  222. exit_status = EXIT_FAILURE;
  223. }
  224. /* If stdin also clear EOF */
  225. if (file == stdin)
  226. clearerr(file);
  227. } while (*++argv);
  228. /* Now close stdin also */
  229. /* (if we didn't read from it, it's a no-op) */
  230. if (fclose(stdin))
  231. bb_perror_msg_and_die(bb_msg_standard_input);
  232. fflush_stdout_and_exit(exit_status);
  233. }