/* expand - convert tabs to spaces
- Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+ Copyright (C) 1989, 1991, 1995 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
David MacKenzie <djm@gnu.ai.mit.edu> */
+#include <config.h>
+
/* Get isblank from GNU libc. */
#define _GNU_SOURCE
-#include <ctype.h>
-#ifndef isblank
-#define isblank(c) ((c) == ' ' || (c) == '\t')
-#endif
+
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include "system.h"
-
-#ifdef isascii
-#define ISDIGIT(c) (isascii ((c)) && isdigit ((c)))
-#else
-#define ISDIGIT(c) (isdigit ((c)))
-#endif
+#include "version.h"
+#include "error.h"
/* The number of bytes added at a time to the amount of memory
allocated for the output line. */
char *xmalloc ();
char *xrealloc ();
-void error ();
static FILE *next_file ();
static void add_tabstop ();
static void usage ();
static void validate_tabstops ();
+/* The name this program was run with. */
+char *program_name;
+
/* If nonzero, convert blanks even after nonblank characters have been
read on the line. */
static int convert_entire_line;
/* Status to return to the system. */
static int exit_status;
-/* The name this program was run with. */
-char *program_name;
+/* If non-zero, display usage information and exit. */
+static int show_help;
+
+/* If non-zero, print the version on standard output then exit. */
+static int show_version;
static struct option const longopts[] =
{
{"tabs", required_argument, NULL, 't'},
{"initial", no_argument, NULL, 'i'},
+ {"help", no_argument, &show_help, 1},
+ {"version", no_argument, &show_version, 1},
{NULL, 0, NULL, 0}
};
{
switch (c)
{
+ case 0:
+ break;
+
case '?':
- usage ();
+ usage (1);
case 'i':
convert_entire_line = 0;
break;
}
}
+ if (show_version)
+ {
+ printf ("expand - %s\n", version_string);
+ exit (0);
+ }
+
+ if (show_help)
+ usage (0);
+
add_tabstop (tabval);
validate_tabstops (tab_list, first_free_tab);
if (have_read_stdin && fclose (stdin) == EOF)
error (1, errno, "-");
if (ferror (stdout) || fclose (stdout) == EOF)
- error (1, 0, "write error");
+ error (1, errno, "write error");
exit (exit_status);
}
for (; *stops; stops++)
{
- if (*stops == ',' || isblank (*stops))
+ if (*stops == ',' || ISBLANK (*stops))
{
add_tabstop (tabval);
tabval = -1;
if (tabval == -1)
return;
if (first_free_tab % TABLIST_BLOCK == 0)
- tab_list = (int *) xrealloc (tab_list, first_free_tab + TABLIST_BLOCK);
+ tab_list = (int *) xrealloc (tab_list, first_free_tab
+ + TABLIST_BLOCK * sizeof (tab_list[0]));
tab_list[first_free_tab++] = tabval;
}
{
int prev_tab = 0;
int i;
-
+
for (i = 0; i < entries; i++)
{
if (tabs[i] == 0)
int convert = 1; /* If nonzero, perform translations. */
fp = next_file ((FILE *) NULL);
+ if (fp == NULL)
+ return;
for (;;)
{
c = getc (fp);
}
static void
-usage ()
+usage (status)
+ int status;
{
- fprintf (stderr, "\
-Usage: %s [-tab1[,tab2[,...]]] [-t tab1[,tab2[,...]]] [-i]\n\
- [--tabs=tab1[,tab2[,...]]] [--initial] [file...]\n",
- program_name);
- exit (1);
+ if (status != 0)
+ fprintf (stderr, "Try `%s --help' for more information.\n",
+ program_name);
+ else
+ {
+ printf ("\
+Usage: %s [OPTION]... [FILE]...\n\
+",
+ program_name);
+ printf ("\
+Convert tabs in each FILE to spaces, writing to standard output.\n\
+With no FILE, or when FILE is -, read standard input.\n\
+\n\
+ -i, --initial do not convert TABs after non whitespace\n\
+ -t, --tabs=NUMBER have tabs NUMBER characters apart, not 8\n\
+ -t, --tabs=LIST use comma separated list of explicit tab positions\n\
+ --help display this help and exit\n\
+ --version output version information and exit\n\
+\n\
+Instead of -t NUMBER or -t LIST, -NUMBER or -LIST may be used.\n\
+");
+ }
+ exit (status);
}