expand.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. {
  75. uni_stat_t uni_stat;
  76. printable_string(&uni_stat, ptr_strbeg);
  77. len = uni_stat.unicode_width;
  78. }
  79. # else
  80. len = ptr - ptr_strbeg;
  81. # endif
  82. len = tab_size - (len % tab_size);
  83. /*while (ptr[1] == '\t') { ptr++; len += tab_size; } - can handle many tabs at once */
  84. printf("%s%*s", ptr_strbeg, len, "");
  85. ptr_strbeg = ptr + 1;
  86. }
  87. ptr++;
  88. }
  89. fputs(ptr_strbeg, stdout);
  90. free(line);
  91. }
  92. }
  93. #endif
  94. #if ENABLE_UNEXPAND
  95. static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
  96. {
  97. char *line;
  98. while ((line = xmalloc_fgets(file)) != NULL) {
  99. char *ptr = line;
  100. unsigned column = 0;
  101. while (*ptr) {
  102. unsigned n;
  103. unsigned len = 0;
  104. while (*ptr == ' ') {
  105. ptr++;
  106. len++;
  107. }
  108. column += len;
  109. if (*ptr == '\t') {
  110. column += tab_size - (column % tab_size);
  111. ptr++;
  112. continue;
  113. }
  114. n = column / tab_size;
  115. if (n) {
  116. len = column = column % tab_size;
  117. while (n--)
  118. putchar('\t');
  119. }
  120. if ((opt & OPT_INITIAL) && ptr != line) {
  121. printf("%*s%s", len, "", ptr);
  122. break;
  123. }
  124. n = strcspn(ptr, "\t ");
  125. printf("%*s%.*s", len, "", n, ptr);
  126. # if ENABLE_UNICODE_SUPPORT
  127. {
  128. char c;
  129. uni_stat_t uni_stat;
  130. c = ptr[n];
  131. ptr[n] = '\0';
  132. printable_string(&uni_stat, ptr);
  133. len = uni_stat.unicode_width;
  134. ptr[n] = c;
  135. }
  136. # else
  137. len = n;
  138. # endif
  139. ptr += n;
  140. column = (column + len) % tab_size;
  141. }
  142. free(line);
  143. }
  144. }
  145. #endif
  146. int expand_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  147. int expand_main(int argc UNUSED_PARAM, char **argv)
  148. {
  149. /* Default 8 spaces for 1 tab */
  150. const char *opt_t = "8";
  151. FILE *file;
  152. unsigned tab_size;
  153. unsigned opt;
  154. int exit_status = EXIT_SUCCESS;
  155. #if ENABLE_FEATURE_EXPAND_LONG_OPTIONS
  156. static const char expand_longopts[] ALIGN1 =
  157. /* name, has_arg, val */
  158. "initial\0" No_argument "i"
  159. "tabs\0" Required_argument "t"
  160. ;
  161. #endif
  162. #if ENABLE_FEATURE_UNEXPAND_LONG_OPTIONS
  163. static const char unexpand_longopts[] ALIGN1 =
  164. /* name, has_arg, val */
  165. "first-only\0" No_argument "i"
  166. "tabs\0" Required_argument "t"
  167. "all\0" No_argument "a"
  168. ;
  169. #endif
  170. init_unicode();
  171. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
  172. IF_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
  173. opt = getopt32(argv, "it:", &opt_t);
  174. } else {
  175. IF_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);
  176. /* -t NUM sets also -a */
  177. opt_complementary = "ta";
  178. opt = getopt32(argv, "ft:a", &opt_t);
  179. /* -f --first-only is the default */
  180. if (!(opt & OPT_ALL)) opt |= OPT_INITIAL;
  181. }
  182. tab_size = xatou_range(opt_t, 1, UINT_MAX);
  183. argv += optind;
  184. if (!*argv) {
  185. *--argv = (char*)bb_msg_standard_input;
  186. }
  187. do {
  188. file = fopen_or_warn_stdin(*argv);
  189. if (!file) {
  190. exit_status = EXIT_FAILURE;
  191. continue;
  192. }
  193. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e'))
  194. IF_EXPAND(expand(file, tab_size, opt));
  195. else
  196. IF_UNEXPAND(unexpand(file, tab_size, opt));
  197. /* Check and close the file */
  198. if (fclose_if_not_stdin(file)) {
  199. bb_simple_perror_msg(*argv);
  200. exit_status = EXIT_FAILURE;
  201. }
  202. /* If stdin also clear EOF */
  203. if (file == stdin)
  204. clearerr(file);
  205. } while (*++argv);
  206. /* Now close stdin also */
  207. /* (if we didn't read from it, it's a no-op) */
  208. if (fclose(stdin))
  209. bb_perror_msg_and_die(bb_msg_standard_input);
  210. fflush_stdout_and_exit(exit_status);
  211. }