Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/coreutils/expand.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/coreutils/expand.c
r1765 r2725 4 4 * Copyright (C) 89, 91, 1995-2006 Free Software Foundation, Inc. 5 5 * 6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.6 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 7 7 * 8 8 * David MacKenzie <djm@gnu.ai.mit.edu> … … 21 21 * Caveat: this versions of expand and unexpand don't accept tab lists. 22 22 */ 23 24 23 #include "libbb.h" 24 #include "unicode.h" 25 25 26 26 enum { … … 30 30 }; 31 31 32 static void xputchar(char c)33 {34 if (putchar(c) < 0)35 bb_error_msg_and_die(bb_msg_write_error);36 }37 38 32 #if ENABLE_EXPAND 39 33 static void expand(FILE *file, unsigned tab_size, unsigned opt) 40 34 { 41 35 char *line; 42 char *ptr;43 int convert;44 int pos;45 46 /* Increment tab_size by 1 locally.*/47 tab_size++;48 36 49 37 while ((line = xmalloc_fgets(file)) != NULL) { 50 convert = 1; 51 pos = 0; 52 ptr = line; 53 while (*line) { 54 pos++; 55 if (*line == '\t' && convert) { 56 for (; pos < tab_size; pos++) { 57 xputchar(' '); 38 unsigned char c; 39 char *ptr; 40 char *ptr_strbeg; 41 42 ptr = ptr_strbeg = line; 43 while ((c = *ptr) != '\0') { 44 if ((opt & OPT_INITIAL) && !isblank(c)) { 45 /* not space or tab */ 46 break; 47 } 48 if (c == '\t') { 49 unsigned len; 50 *ptr = '\0'; 51 # if ENABLE_UNICODE_SUPPORT 52 { 53 uni_stat_t uni_stat; 54 printable_string(&uni_stat, ptr_strbeg); 55 len = uni_stat.unicode_width; 58 56 } 59 } else { 60 if ((opt & OPT_INITIAL) && !isblank(*line)) {61 convert = 0; 62 }63 xputchar(*line);64 }65 if (pos == tab_size) {66 pos = 0;67 }68 line++;69 }70 free( ptr);57 # else 58 len = ptr - ptr_strbeg; 59 # endif 60 len = tab_size - (len % tab_size); 61 /*while (ptr[1] == '\t') { ptr++; len += tab_size; } - can handle many tabs at once */ 62 printf("%s%*s", ptr_strbeg, len, ""); 63 ptr_strbeg = ptr + 1; 64 } 65 ptr++; 66 } 67 fputs(ptr_strbeg, stdout); 68 free(line); 71 69 } 72 70 } … … 74 72 75 73 #if ENABLE_UNEXPAND 76 static void unexpand(FILE *file, unsigned inttab_size, unsigned opt)74 static void unexpand(FILE *file, unsigned tab_size, unsigned opt) 77 75 { 78 76 char *line; 79 char *ptr;80 int convert;81 int pos;82 int i = 0;83 int column = 0;84 77 85 78 while ((line = xmalloc_fgets(file)) != NULL) { 86 convert = 1; 87 pos = 0; 88 ptr = line; 89 while (*line) { 90 while ((*line == ' ' || *line == '\t') && convert) { 91 pos += (*line == ' ') ? 1 : tab_size; 92 line++; 93 column++; 94 if ((opt & OPT_ALL) && column == tab_size) { 95 column = 0; 96 goto put_tab; 97 } 98 } 99 if (pos) { 100 i = pos / tab_size; 101 if (i) { 102 for (; i > 0; i--) { 103 put_tab: 104 xputchar('\t'); 105 } 106 } else { 107 for (i = pos % tab_size; i > 0; i--) { 108 xputchar(' '); 109 } 110 } 111 pos = 0; 112 } else { 113 if (opt & OPT_INITIAL) { 114 convert = 0; 115 } 116 if (opt & OPT_ALL) { 117 column++; 118 } 119 xputchar(*line); 120 line++; 121 } 122 } 123 free(ptr); 79 char *ptr = line; 80 unsigned column = 0; 81 82 while (*ptr) { 83 unsigned n; 84 unsigned len = 0; 85 86 while (*ptr == ' ') { 87 ptr++; 88 len++; 89 } 90 column += len; 91 if (*ptr == '\t') { 92 column += tab_size - (column % tab_size); 93 ptr++; 94 continue; 95 } 96 97 n = column / tab_size; 98 if (n) { 99 len = column = column % tab_size; 100 while (n--) 101 putchar('\t'); 102 } 103 104 if ((opt & OPT_INITIAL) && ptr != line) { 105 printf("%*s%s", len, "", ptr); 106 break; 107 } 108 n = strcspn(ptr, "\t "); 109 printf("%*s%.*s", len, "", n, ptr); 110 # if ENABLE_UNICODE_SUPPORT 111 { 112 char c; 113 uni_stat_t uni_stat; 114 c = ptr[n]; 115 ptr[n] = '\0'; 116 printable_string(&uni_stat, ptr); 117 len = uni_stat.unicode_width; 118 ptr[n] = c; 119 } 120 # else 121 len = n; 122 # endif 123 ptr += n; 124 column = (column + len) % tab_size; 125 } 126 free(line); 124 127 } 125 128 } 126 129 #endif 127 130 128 int expand_main(int argc, char **argv) ;129 int expand_main(int argc , char **argv)131 int expand_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 132 int expand_main(int argc UNUSED_PARAM, char **argv) 130 133 { 131 134 /* Default 8 spaces for 1 tab */ … … 151 154 ; 152 155 #endif 156 init_unicode(); 153 157 154 158 if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) { 155 USE_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);159 IF_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts); 156 160 opt = getopt32(argv, "it:", &opt_t); 157 } else if (ENABLE_UNEXPAND){158 USE_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);161 } else { 162 IF_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts); 159 163 /* -t NUM sets also -a */ 160 164 opt_complementary = "ta"; … … 167 171 argv += optind; 168 172 169 /* If no args are given, read from stdin */170 173 if (!*argv) { 171 174 *--argv = (char*)bb_msg_standard_input; 172 goto use_stdin; 173 } 174 175 } 175 176 do { 176 if (NOT_LONE_CHAR(*argv, '-')) { 177 file = fopen_or_warn(*argv, "r"); 178 if (!file) { 179 exit_status = EXIT_FAILURE; 180 continue; 181 } 182 } else { 183 use_stdin: 184 file = stdin; 177 file = fopen_or_warn_stdin(*argv); 178 if (!file) { 179 exit_status = EXIT_FAILURE; 180 continue; 185 181 } 186 182 187 183 if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) 188 USE_EXPAND(expand(file, tab_size, opt));189 else if (ENABLE_UNEXPAND)190 USE_UNEXPAND(unexpand(file, tab_size, opt));184 IF_EXPAND(expand(file, tab_size, opt)); 185 else 186 IF_UNEXPAND(unexpand(file, tab_size, opt)); 191 187 192 188 /* Check and close the file */ 193 /* We do want all of them to execute, thus | instead of || */ 194 if (ferror(file) | fclose_if_not_stdin(file)) { 195 bb_perror_msg("%s", *argv); 189 if (fclose_if_not_stdin(file)) { 190 bb_simple_perror_msg(*argv); 196 191 exit_status = EXIT_FAILURE; 197 192 }
Note:
See TracChangeset
for help on using the changeset viewer.