expand.c 6.6 KB

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