.
[coreutils.git] / src / unexpand.c
blob0d09594746111f16857639fff89b136ea8aa15a0
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)
7 any later version.
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
19 into 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.
24 Options:
25 --tabs=tab1[,tab2[,...]]
26 -t 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
31 single spaces.
32 --all
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> */
38 #include <config.h>
40 /* Get isblank from GNU libc. */
41 #define _GNU_SOURCE
43 #include <stdio.h>
44 #include <getopt.h>
45 #include <sys/types.h>
46 #include "system.h"
47 #include "version.h"
48 #include "error.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
58 char *xmalloc ();
59 char *xrealloc ();
61 /* The name this program was run with. */
62 char *program_name;
64 /* If nonzero, convert blanks even after nonblank characters have been
65 read on the line. */
66 static int convert_entire_line;
68 /* If nonzero, the size of all tab stops. If zero, use `tab_list' instead. */
69 static int tab_size;
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. */
74 static int *tab_list;
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[] =
86 "-", NULL
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. */
96 static int show_help;
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},
107 {NULL, 0, NULL, 0}
110 /* Add tab stop TABVAL to the end of `tab_list', except
111 if TABVAL is -1, do nothing. */
113 static void
114 add_tabstop (int tabval)
116 if (tabval == -1)
117 return;
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. */
126 static void
127 parse_tabstops (const char *stops)
129 int tabval = -1;
131 for (; *stops; stops++)
133 if (*stops == ',' || ISBLANK (*stops))
135 add_tabstop (tabval);
136 tabval = -1;
138 else if (ISDIGIT (*stops))
140 if (tabval == -1)
141 tabval = 0;
142 tabval = tabval * 10 + *stops - '0';
144 else
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. */
154 static void
155 validate_tabstops (const int *tabs, int entries)
157 int prev_tab = 0;
158 int i;
160 for (i = 0; i < entries; i++)
162 if (tabs[i] == 0)
163 error (1, 0, _("tab size cannot be 0"));
164 if (tabs[i] <= prev_tab)
165 error (1, 0, _("tab sizes must be ascending"));
166 prev_tab = tabs[i];
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. */
175 static FILE *
176 next_file (FILE *fp)
178 static char *prev_file;
179 char *file;
181 if (fp)
183 if (ferror (fp))
185 error (0, errno, "%s", prev_file);
186 exit_status = 1;
188 if (fp == stdin)
189 clearerr (fp); /* Also clear EOF. */
190 else if (fclose (fp) == EOF)
192 error (0, errno, "%s", prev_file);
193 exit_status = 1;
197 while ((file = *file_list++) != NULL)
199 if (file[0] == '-' && file[1] == '\0')
201 have_read_stdin = 1;
202 prev_file = file;
203 return stdin;
205 fp = fopen (file, "r");
206 if (fp)
208 prev_file = file;
209 return fp;
211 error (0, errno, "%s", file);
212 exit_status = 1;
214 return NULL;
217 /* Change spaces to tabs, writing to stdout.
218 Read each file in `file_list', in order. */
220 static void
221 unexpand (void)
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);
234 if (fp == NULL)
235 return;
237 for (;;)
239 c = getc (fp);
240 if (c == EOF)
242 fp = next_file (fp);
243 if (fp == NULL)
244 break; /* No more files. */
245 else
246 continue;
249 if (c == ' ' && convert)
251 ++pending;
252 ++column;
254 else if (c == '\t' && convert)
256 if (tab_size == 0)
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])
262 tab_index++;
263 next_tab_column = tab_list[tab_index];
264 if (tab_index < first_free_tab - 1)
265 tab_index++;
266 if (column >= next_tab_column)
268 convert = 0; /* Ran out of tab stops. */
269 goto flush_pend;
272 else
274 next_tab_column = column + tab_size - column % tab_size;
276 pending += next_tab_column - column;
277 column = next_tab_column;
279 else
281 flush_pend:
282 /* Flush pending spaces. Print as many tabs as possible,
283 then print the rest as spaces. */
284 if (pending == 1)
286 putchar (' ');
287 pending = 0;
289 column -= pending;
290 while (pending != 0)
292 if (tab_size == 0)
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])
298 print_tab_index++;
299 next_tab_column = tab_list[print_tab_index];
300 if (print_tab_index < first_free_tab - 1)
301 print_tab_index++;
303 else
305 next_tab_column = column + tab_size - column % tab_size;
307 if (next_tab_column - column <= pending)
309 putchar ('\t');
310 pending -= next_tab_column - column;
311 column = next_tab_column;
313 else
315 --print_tab_index;
316 column += pending;
317 while (pending != 0)
319 putchar (' ');
320 pending--;
325 if (convert)
327 if (c == '\b')
329 if (column > 0)
330 --column;
332 else
334 ++column;
335 if (convert_entire_line == 0)
336 convert = 0;
340 putchar (c);
342 if (c == '\n')
344 tab_index = print_tab_index = 0;
345 column = pending = 0;
346 convert = 1;
352 static void
353 usage (int status)
355 if (status != 0)
356 fprintf (stderr, _("Try `%s --help' for more information.\n"),
357 program_name);
358 else
360 printf (_("\
361 Usage: %s [OPTION]... [FILE]...\n\
363 program_name);
364 printf (_("\
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\
375 "));
377 exit (status);
380 void
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);
391 have_read_stdin = 0;
392 exit_status = 0;
393 convert_entire_line = 0;
394 tab_list = NULL;
395 first_free_tab = 0;
397 while ((c = getopt_long (argc, argv, "at:,0123456789", longopts, (int *) 0))
398 != EOF)
400 switch (c)
402 case 0:
403 break;
405 case '?':
406 usage (1);
407 case 'a':
408 convert_entire_line = 1;
409 break;
410 case 't':
411 convert_entire_line = 1;
412 parse_tabstops (optarg);
413 break;
414 case ',':
415 add_tabstop (tabval);
416 tabval = -1;
417 break;
418 default:
419 if (tabval == -1)
420 tabval = 0;
421 tabval = tabval * 10 + c - '0';
422 break;
426 if (show_version)
428 printf ("unexpand - %s\n", version_string);
429 exit (0);
432 if (show_help)
433 usage (0);
435 add_tabstop (tabval);
437 validate_tabstops (tab_list, first_free_tab);
439 if (first_free_tab == 0)
440 tab_size = 8;
441 else if (first_free_tab == 1)
442 tab_size = tab_list[0];
443 else
444 tab_size = 0;
446 if (optind == argc)
447 file_list = stdin_argv;
448 else
449 file_list = &argv[optind];
451 unexpand ();
453 if (have_read_stdin && fclose (stdin) == EOF)
454 error (1, errno, "-");
455 if (fclose (stdout) == EOF)
456 error (1, errno, _("write error"));
457 exit (exit_status);