OPT_ALL = 1 << 2,
};
-static void xputchar(char c)
-{
- if (putchar(c) < 0)
- bb_error_msg_and_die(bb_msg_write_error);
-}
-
#if ENABLE_EXPAND
-static void expand(FILE *file, unsigned tab_size, unsigned opt)
+static void expand(FILE *file, int tab_size, unsigned opt)
{
char *line;
- char *ptr;
- int convert;
- int pos;
- /* Increment tab_size by 1 locally.*/
- tab_size++;
+ tab_size = -tab_size;
while ((line = xmalloc_fgets(file)) != NULL) {
- convert = 1;
- pos = 0;
- ptr = line;
- while (*line) {
- pos++;
- if (*line == '\t' && convert) {
- for (; pos < tab_size; pos++) {
- xputchar(' ');
- }
- } else {
- if ((opt & OPT_INITIAL) && !isblank(*line)) {
- convert = 0;
- }
- xputchar(*line);
+ int pos;
+ unsigned char c;
+ char *ptr = line;
+
+ goto start;
+ while ((c = *ptr) != '\0') {
+ if ((opt & OPT_INITIAL) && !isblank(c)) {
+ fputs(ptr, stdout);
+ break;
+ }
+ ptr++;
+ if (c == '\t') {
+ c = ' ';
+ while (++pos < 0)
+ bb_putchar(c);
}
- if (pos == tab_size) {
- pos = 0;
+ bb_putchar(c);
+ if (++pos >= 0) {
+ start:
+ pos = tab_size;
}
- line++;
}
- free(ptr);
+ free(line);
}
}
#endif
#if ENABLE_UNEXPAND
-static void unexpand(FILE *file, unsigned int tab_size, unsigned opt)
+static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
{
char *line;
- char *ptr;
- int convert;
- int pos;
- int i = 0;
- int column = 0;
while ((line = xmalloc_fgets(file)) != NULL) {
- convert = 1;
- pos = 0;
- ptr = line;
- while (*line) {
- while ((*line == ' ' || *line == '\t') && convert) {
- pos += (*line == ' ') ? 1 : tab_size;
- line++;
+ char *ptr = line;
+ unsigned column = 0;
+
+ while (*ptr) {
+ unsigned n;
+
+ while (*ptr == ' ') {
column++;
- if ((opt & OPT_ALL) && column == tab_size) {
- column = 0;
- goto put_tab;
- }
+ ptr++;
+ }
+ if (*ptr == '\t') {
+ column += tab_size - (column % tab_size);
+ ptr++;
+ continue;
}
- if (pos) {
- i = pos / tab_size;
- if (i) {
- for (; i > 0; i--) {
- put_tab:
- xputchar('\t');
- }
- } else {
- for (i = pos % tab_size; i > 0; i--) {
- xputchar(' ');
- }
- }
- pos = 0;
- } else {
- if (opt & OPT_INITIAL) {
- convert = 0;
- }
- if (opt & OPT_ALL) {
- column++;
- }
- xputchar(*line);
- line++;
+
+ n = column / tab_size;
+ column = column % tab_size;
+ while (n--)
+ putchar('\t');
+
+ if ((opt & OPT_INITIAL) && ptr != line) {
+ printf("%*s%s", column, "", ptr);
+ break;
}
+ n = strcspn(ptr, "\t ");
+ printf("%*s%.*s", column, "", n, ptr);
+ ptr += n;
+ column = (column + n) % tab_size;
}
- free(ptr);
+ free(line);
}
}
#endif
int expand_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int expand_main(int argc ATTRIBUTE_UNUSED, char **argv)
+int expand_main(int argc UNUSED_PARAM, char **argv)
{
/* Default 8 spaces for 1 tab */
const char *opt_t = "8";
#endif
if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
- USE_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
+ IF_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
opt = getopt32(argv, "it:", &opt_t);
- } else if (ENABLE_UNEXPAND) {
- USE_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);
+ } else {
+ IF_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);
/* -t NUM sets also -a */
opt_complementary = "ta";
opt = getopt32(argv, "ft:a", &opt_t);
argv += optind;
- /* If no args are given, read from stdin */
if (!*argv) {
*--argv = (char*)bb_msg_standard_input;
- goto use_stdin;
}
-
do {
- if (NOT_LONE_CHAR(*argv, '-')) {
- file = fopen_or_warn(*argv, "r");
- if (!file) {
- exit_status = EXIT_FAILURE;
- continue;
- }
- } else {
- use_stdin:
- file = stdin;
+ file = fopen_or_warn_stdin(*argv);
+ if (!file) {
+ exit_status = EXIT_FAILURE;
+ continue;
}
if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e'))
- USE_EXPAND(expand(file, tab_size, opt));
- else if (ENABLE_UNEXPAND)
- USE_UNEXPAND(unexpand(file, tab_size, opt));
+ IF_EXPAND(expand(file, tab_size, opt));
+ else
+ IF_UNEXPAND(unexpand(file, tab_size, opt));
/* Check and close the file */
- /* We do want all of them to execute, thus | instead of || */
- if (ferror(file) | fclose_if_not_stdin(file)) {
+ if (fclose_if_not_stdin(file)) {
bb_simple_perror_msg(*argv);
exit_status = EXIT_FAILURE;
}