(usage): Include one- or two-line synopsis in --help output.
[platform/upstream/coreutils.git] / src / expand.c
index a4fb1af..d7366a2 100644 (file)
@@ -1,5 +1,5 @@
 /* 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. */
@@ -60,7 +55,6 @@
 
 char *xmalloc ();
 char *xrealloc ();
-void error ();
 
 static FILE *next_file ();
 static void add_tabstop ();
@@ -69,6 +63,9 @@ static void parse_tabstops ();
 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;
@@ -100,13 +97,18 @@ static int have_read_stdin;
 /* 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}
 };
 
@@ -130,8 +132,11 @@ main (argc, argv)
     {
       switch (c)
        {
+       case 0:
+         break;
+
        case '?':
-         usage ();
+         usage (1);
        case 'i':
          convert_entire_line = 0;
          break;
@@ -150,6 +155,15 @@ main (argc, argv)
        }
     }
 
+  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);
@@ -171,7 +185,7 @@ main (argc, argv)
   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);
 }
@@ -187,7 +201,7 @@ parse_tabstops (stops)
 
   for (; *stops; stops++)
     {
-      if (*stops == ',' || isblank (*stops))
+      if (*stops == ',' || ISBLANK (*stops))
        {
          add_tabstop (tabval);
          tabval = -1;
@@ -215,7 +229,8 @@ add_tabstop (tabval)
   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;
 }
 
@@ -229,7 +244,7 @@ validate_tabstops (tabs, entries)
 {
   int prev_tab = 0;
   int i;
-  
+
   for (i = 0; i < entries; i++)
     {
       if (tabs[i] == 0)
@@ -254,6 +269,8 @@ expand ()
   int convert = 1;             /* If nonzero, perform translations. */
 
   fp = next_file ((FILE *) NULL);
+  if (fp == NULL)
+    return;
   for (;;)
     {
       c = getc (fp);
@@ -368,11 +385,30 @@ next_file (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);
 }