expand.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. //FIXME: does not work properly with input containing NULs
  60. //coreutils 8.30 preserves NULs but treats them as chars of width zero:
  61. //AB<nul><tab>C will expand <tab> to 6 spaces, not 5.
  62. #if ENABLE_EXPAND
  63. static void expand(FILE *file, unsigned tab_size, unsigned opt)
  64. {
  65. for (;;) {
  66. char *line;
  67. char *ptr;
  68. char *ptr_strbeg;
  69. //commented-out code handles NULs, +90 bytes of code, not tested much
  70. // size_t linelen;
  71. // unsigned len = 0;
  72. // linelen = 1024 * 1024;
  73. // line = xmalloc_fgets_str_len(file, "\n", &linelen);
  74. line = xmalloc_fgets(file); //
  75. if (!line)
  76. break;
  77. ptr = ptr_strbeg = line;
  78. for (;;) {
  79. unsigned char c = *ptr;
  80. if (c == '\0') {
  81. // size_t rem = line + linelen - ptr;
  82. // if (rem > 0) {
  83. //# if ENABLE_UNICODE_SUPPORT
  84. // len += unicode_strwidth(ptr_strbeg);
  85. //# else
  86. // len += ptr - ptr_strbeg;
  87. //# endif
  88. // printf("%s%c", ptr_strbeg, '\0');
  89. // memmove(ptr, ptr + 1, rem + 1);
  90. // ptr_strbeg = ptr;
  91. // linelen--;
  92. // continue;
  93. // }
  94. break;
  95. }
  96. if ((opt & OPT_INITIAL) && !isblank(c)) {
  97. /* not space or tab */
  98. break;
  99. }
  100. if (c == '\t') {
  101. unsigned len = 0; //
  102. *ptr = '\0';
  103. # if ENABLE_UNICODE_SUPPORT
  104. len += unicode_strwidth(ptr_strbeg);
  105. # else
  106. len += ptr - ptr_strbeg;
  107. # endif
  108. len = tab_size - (len % tab_size);
  109. /*while (ptr[1] == '\t') { ptr++; len += tab_size; } - can handle many tabs at once */
  110. printf("%s%*s", ptr_strbeg, len, "");
  111. // len = 0;
  112. ptr_strbeg = ptr + 1;
  113. }
  114. ptr++;
  115. }
  116. fputs_stdout(ptr_strbeg);
  117. free(line);
  118. }
  119. }
  120. #endif
  121. #if ENABLE_UNEXPAND
  122. static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
  123. {
  124. char *line;
  125. while ((line = xmalloc_fgets(file)) != NULL) {
  126. char *ptr = line;
  127. unsigned column = 0;
  128. while (*ptr) {
  129. unsigned n;
  130. unsigned len = 0;
  131. while (*ptr == ' ') {
  132. ptr++;
  133. len++;
  134. }
  135. column += len;
  136. if (*ptr == '\t') {
  137. column += tab_size - (column % tab_size);
  138. ptr++;
  139. continue;
  140. }
  141. n = column / tab_size;
  142. if (n) {
  143. len = column = column % tab_size;
  144. while (n--)
  145. putchar('\t');
  146. }
  147. if (!(opt & OPT_ALL) && ptr != line) {
  148. printf("%*s%s", len, "", ptr);
  149. break;
  150. }
  151. n = strcspn(ptr, "\t ");
  152. printf("%*s%.*s", len, "", n, ptr);
  153. # if ENABLE_UNICODE_SUPPORT
  154. {
  155. char c = ptr[n];
  156. ptr[n] = '\0';
  157. len = unicode_strwidth(ptr);
  158. ptr[n] = c;
  159. }
  160. # else
  161. len = n;
  162. # endif
  163. ptr += n;
  164. column = (column + len) % tab_size;
  165. }
  166. free(line);
  167. }
  168. }
  169. #endif
  170. int expand_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  171. int expand_main(int argc UNUSED_PARAM, char **argv)
  172. {
  173. /* Default 8 spaces for 1 tab */
  174. const char *opt_t = "8";
  175. FILE *file;
  176. unsigned tab_size;
  177. unsigned opt;
  178. int exit_status = EXIT_SUCCESS;
  179. init_unicode();
  180. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
  181. opt = getopt32long(argv, "it:",
  182. "initial\0" No_argument "i"
  183. "tabs\0" Required_argument "t"
  184. , &opt_t
  185. );
  186. } else {
  187. opt = getopt32long(argv, "^"
  188. "ft:a"
  189. "\0"
  190. "ta" /* -t NUM sets -a */,
  191. "first-only\0" No_argument "f"
  192. "tabs\0" Required_argument "t"
  193. "all\0" No_argument "a"
  194. , &opt_t
  195. );
  196. /* -t implies -a, but an explicit -f overrides */
  197. if (opt & OPT_INITIAL) opt &= ~OPT_ALL;
  198. }
  199. tab_size = xatou_range(opt_t, 1, UINT_MAX);
  200. argv += optind;
  201. if (!*argv) {
  202. *--argv = (char*)bb_msg_standard_input;
  203. }
  204. do {
  205. file = fopen_or_warn_stdin(*argv);
  206. if (!file) {
  207. exit_status = EXIT_FAILURE;
  208. continue;
  209. }
  210. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e'))
  211. IF_EXPAND(expand(file, tab_size, opt));
  212. else
  213. IF_UNEXPAND(unexpand(file, tab_size, opt));
  214. /* Check and close the file */
  215. if (fclose_if_not_stdin(file)) {
  216. bb_simple_perror_msg(*argv);
  217. exit_status = EXIT_FAILURE;
  218. }
  219. /* If stdin also clear EOF */
  220. if (file == stdin)
  221. clearerr(file);
  222. } while (*++argv);
  223. /* Now close stdin also */
  224. /* (if we didn't read from it, it's a no-op) */
  225. if (fclose(stdin))
  226. bb_simple_perror_msg_and_die(bb_msg_standard_input);
  227. fflush_stdout_and_exit(exit_status);
  228. }