1 /* unexpand - convert spaces to tabs
2 Copyright (C) 1989, 1991, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* By default, convert only maximal strings of initial blanks and tabs
20 Preserves backspace characters in the output; they decrement the
21 column count for tab calculations.
22 The default action is equivalent to -8.
25 --tabs=tab1[,tab2[,...]]
27 -tab1[,tab2[,...]] If only one tab stop is given, set the tabs tab1
28 spaces apart instead of the default 8. Otherwise,
29 set the tabs at columns tab1, tab2, etc. (numbered from
30 0); replace any tabs beyond the tabstops given with
33 -a Use tabs wherever they would replace 2 or more spaces,
34 not just at the beginnings of lines.
36 David MacKenzie <djm@gnu.ai.mit.edu> */
40 /* Get isblank from GNU libc. */
45 #include <sys/types.h>
50 /* The number of bytes added at a time to the amount of memory
51 allocated for the output line. */
52 #define OUTPUT_BLOCK 256
54 /* The number of bytes added at a time to the amount of memory
55 allocated for the list of tabstops. */
56 #define TABLIST_BLOCK 256
61 /* The name this program was run with. */
64 /* If nonzero, convert blanks even after nonblank characters have been
66 static int convert_entire_line
;
68 /* If nonzero, the size of all tab stops. If zero, use `tab_list' instead. */
71 /* Array of the explicit column numbers of the tab stops;
72 after `tab_list' is exhausted, the rest of the line is printed
73 unchanged. The first column is column 0. */
76 /* The index of the first invalid element of `tab_list',
77 where the next element can be added. */
78 static int first_free_tab
;
80 /* Null-terminated array of input filenames. */
81 static char **file_list
;
83 /* Default for `file_list' if no files are given on the command line. */
84 static char *stdin_argv
[] =
89 /* Nonzero if we have ever read standard input. */
90 static int have_read_stdin
;
92 /* Status to return to the system. */
93 static int exit_status
;
95 /* If nonzero, display usage information and exit. */
98 /* If nonzero, print the version on standard output then exit. */
99 static int show_version
;
101 static struct option
const longopts
[] =
103 {"tabs", required_argument
, NULL
, 't'},
104 {"all", no_argument
, NULL
, 'a'},
105 {"help", no_argument
, &show_help
, 1},
106 {"version", no_argument
, &show_version
, 1},
110 /* Add tab stop TABVAL to the end of `tab_list', except
111 if TABVAL is -1, do nothing. */
114 add_tabstop (int tabval
)
118 if (first_free_tab
% TABLIST_BLOCK
== 0)
119 tab_list
= (int *) xrealloc (tab_list
, first_free_tab
+ TABLIST_BLOCK
);
120 tab_list
[first_free_tab
++] = tabval
;
123 /* Add the comma or blank separated list of tabstops STOPS
124 to the list of tabstops. */
127 parse_tabstops (const char *stops
)
131 for (; *stops
; stops
++)
133 if (*stops
== ',' || ISBLANK (*stops
))
135 add_tabstop (tabval
);
138 else if (ISDIGIT (*stops
))
142 tabval
= tabval
* 10 + *stops
- '0';
145 error (1, 0, _("tab size contains an invalid character"));
148 add_tabstop (tabval
);
151 /* Check that the list of tabstops TABS, with ENTRIES entries,
152 contains only nonzero, ascending values. */
155 validate_tabstops (const int *tabs
, int entries
)
160 for (i
= 0; i
< entries
; i
++)
163 error (1, 0, _("tab size cannot be 0"));
164 if (tabs
[i
] <= prev_tab
)
165 error (1, 0, _("tab sizes must be ascending"));
170 /* Close the old stream pointer FP if it is non-NULL,
171 and return a new one opened to read the next input file.
172 Open a filename of `-' as the standard input.
173 Return NULL if there are no more input files. */
178 static char *prev_file
;
185 error (0, errno
, "%s", prev_file
);
189 clearerr (fp
); /* Also clear EOF. */
190 else if (fclose (fp
) == EOF
)
192 error (0, errno
, "%s", prev_file
);
197 while ((file
= *file_list
++) != NULL
)
199 if (file
[0] == '-' && file
[1] == '\0')
205 fp
= fopen (file
, "r");
211 error (0, errno
, "%s", file
);
217 /* Change spaces to tabs, writing to stdout.
218 Read each file in `file_list', in order. */
223 FILE *fp
; /* Input stream. */
224 int c
; /* Each input character. */
225 /* Index in `tab_list' of next tabstop: */
226 int tab_index
= 0; /* For calculating width of pending tabs. */
227 int print_tab_index
= 0; /* For printing as many tabs as possible. */
228 int column
= 0; /* Column on screen of next char. */
229 int next_tab_column
; /* Column the next tab stop is on. */
230 int convert
= 1; /* If nonzero, perform translations. */
231 int pending
= 0; /* Pending columns of blanks. */
233 fp
= next_file ((FILE *) NULL
);
244 break; /* No more files. */
249 if (c
== ' ' && convert
)
254 else if (c
== '\t' && convert
)
258 /* Do not let tab_index == first_free_tab;
259 stop when it is 1 less. */
260 while (tab_index
< first_free_tab
- 1
261 && column
>= tab_list
[tab_index
])
263 next_tab_column
= tab_list
[tab_index
];
264 if (tab_index
< first_free_tab
- 1)
266 if (column
>= next_tab_column
)
268 convert
= 0; /* Ran out of tab stops. */
274 next_tab_column
= column
+ tab_size
- column
% tab_size
;
276 pending
+= next_tab_column
- column
;
277 column
= next_tab_column
;
282 /* Flush pending spaces. Print as many tabs as possible,
283 then print the rest as spaces. */
294 /* Do not let tab_index == first_free_tab;
295 stop when it is 1 less. */
296 while (tab_index
< first_free_tab
- 1
297 && column
>= tab_list
[tab_index
])
299 next_tab_column
= tab_list
[print_tab_index
];
300 if (print_tab_index
< first_free_tab
- 1)
305 next_tab_column
= column
+ tab_size
- column
% tab_size
;
307 if (next_tab_column
- column
<= pending
)
310 pending
-= next_tab_column
- column
;
311 column
= next_tab_column
;
335 if (convert_entire_line
== 0)
344 tab_index
= print_tab_index
= 0;
345 column
= pending
= 0;
356 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
361 Usage: %s [OPTION]... [FILE]...\n\
365 Convert spaces in each FILE to tabs, writing to standard output.\n\
366 With no FILE, or when FILE is -, read standard input.\n\
368 -a, --all convert all whitespace, instead of initial whitespace\n\
369 -t, --tabs=NUMBER have tabs NUMBER characters apart instead of 8\n\
370 -t, --tabs=LIST use comma separated list of explicit tab positions\n\
371 --help display this help and exit\n\
372 --version output version information and exit\n\
374 Instead of -t NUMBER or -t LIST, -NUMBER or -LIST may be used.\n\
381 main (int argc
, char **argv
)
383 int tabval
= -1; /* Value of tabstop being read, or -1. */
384 int c
; /* Option character. */
386 program_name
= argv
[0];
387 setlocale (LC_ALL
, "");
388 bindtextdomain (PACKAGE
, LOCALEDIR
);
389 textdomain (PACKAGE
);
393 convert_entire_line
= 0;
397 while ((c
= getopt_long (argc
, argv
, "at:,0123456789", longopts
, (int *) 0))
408 convert_entire_line
= 1;
411 convert_entire_line
= 1;
412 parse_tabstops (optarg
);
415 add_tabstop (tabval
);
421 tabval
= tabval
* 10 + c
- '0';
428 printf ("unexpand - %s\n", version_string
);
435 add_tabstop (tabval
);
437 validate_tabstops (tab_list
, first_free_tab
);
439 if (first_free_tab
== 0)
441 else if (first_free_tab
== 1)
442 tab_size
= tab_list
[0];
447 file_list
= stdin_argv
;
449 file_list
= &argv
[optind
];
453 if (have_read_stdin
&& fclose (stdin
) == EOF
)
454 error (1, errno
, "-");
455 if (fclose (stdout
) == EOF
)
456 error (1, errno
, _("write error"));