expand.c 5.7 KB

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