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