home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / textutils-1.11-src.lha / textutils-1.11 / src / expand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-13  |  9.1 KB  |  412 lines

  1. /* expand - convert tabs to spaces
  2.    Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  3.  
  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.
  8.  
  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.
  13.  
  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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* By default, convert all tabs to spaces.
  19.    Preserves backspace characters in the output; they decrement the
  20.    column count for tab calculations.
  21.    The default action is equivalent to -8.
  22.  
  23.    Options:
  24.    --tabs=tab1[,tab2[,...]]
  25.    -t tab1[,tab2[,...]]
  26.    -tab1[,tab2[,...]]    If only one tab stop is given, set the tabs tab1
  27.             spaces apart instead of the default 8.  Otherwise,
  28.             set the tabs at columns tab1, tab2, etc. (numbered from
  29.             0); replace any tabs beyond the tabstops given with
  30.             single spaces.
  31.    --initial
  32.    -i            Only convert initial tabs on each line to spaces.
  33.  
  34.    David MacKenzie <djm@gnu.ai.mit.edu> */
  35.  
  36. #include <config.h>
  37.  
  38. /* Get isblank from GNU libc.  */
  39. #define _GNU_SOURCE
  40.  
  41. #include <stdio.h>
  42. #include <getopt.h>
  43. #include <sys/types.h>
  44. #include "system.h"
  45. #include "version.h"
  46.  
  47. /* The number of bytes added at a time to the amount of memory
  48.    allocated for the output line. */
  49. #define OUTPUT_BLOCK 256
  50.  
  51. /* The number of bytes added at a time to the amount of memory
  52.    allocated for the list of tabstops. */
  53. #define TABLIST_BLOCK 256
  54.  
  55. char *xmalloc ();
  56. char *xrealloc ();
  57. void error ();
  58.  
  59. static FILE *next_file ();
  60. static void add_tabstop ();
  61. static void expand ();
  62. static void parse_tabstops ();
  63. static void usage ();
  64. static void validate_tabstops ();
  65.  
  66. /* The name this program was run with. */
  67. char *program_name;
  68.  
  69. /* If nonzero, convert blanks even after nonblank characters have been
  70.    read on the line. */
  71. static int convert_entire_line;
  72.  
  73. /* If nonzero, the size of all tab stops.  If zero, use `tab_list' instead. */
  74. static int tab_size;
  75.  
  76. /* Array of the explicit column numbers of the tab stops;
  77.    after `tab_list' is exhausted, each additional tab is replaced
  78.    by a space.  The first column is column 0. */
  79. static int *tab_list;
  80.  
  81. /* The index of the first invalid element of `tab_list',
  82.    where the next element can be added. */
  83. static int first_free_tab;
  84.  
  85. /* Null-terminated array of input filenames. */
  86. static char **file_list;
  87.  
  88. /* Default for `file_list' if no files are given on the command line. */
  89. static char *stdin_argv[] =
  90. {
  91.   "-", NULL
  92. };
  93.  
  94. /* Nonzero if we have ever read standard input. */
  95. static int have_read_stdin;
  96.  
  97. /* Status to return to the system. */
  98. static int exit_status;
  99.  
  100. /* If non-zero, display usage information and exit.  */
  101. static int show_help;
  102.  
  103. /* If non-zero, print the version on standard output then exit.  */
  104. static int show_version;
  105.  
  106. static struct option const longopts[] =
  107. {
  108.   {"tabs", required_argument, NULL, 't'},
  109.   {"initial", no_argument, NULL, 'i'},
  110.   {"help", no_argument, &show_help, 1},
  111.   {"version", no_argument, &show_version, 1},
  112.   {NULL, 0, NULL, 0}
  113. };
  114.  
  115. main (argc, argv)
  116.      int argc;
  117.      char **argv;
  118. {
  119.   int tabval = -1;        /* Value of tabstop being read, or -1. */
  120.   int c;            /* Option character. */
  121.  
  122.   have_read_stdin = 0;
  123.   exit_status = 0;
  124.   convert_entire_line = 1;
  125.   tab_list = NULL;
  126.   first_free_tab = 0;
  127.   program_name = argv[0];
  128.  
  129.   while ((c = getopt_long (argc, argv, "it:,0123456789", longopts, (int *) 0))
  130.      != EOF)
  131.     {
  132.       switch (c)
  133.     {
  134.     case 0:
  135.       break;
  136.  
  137.     case '?':
  138.       usage (1);
  139.     case 'i':
  140.       convert_entire_line = 0;
  141.       break;
  142.     case 't':
  143.       parse_tabstops (optarg);
  144.       break;
  145.     case ',':
  146.       add_tabstop (tabval);
  147.       tabval = -1;
  148.       break;
  149.     default:
  150.       if (tabval == -1)
  151.         tabval = 0;
  152.       tabval = tabval * 10 + c - '0';
  153.       break;
  154.     }
  155.     }
  156.  
  157.   if (show_version)
  158.     {
  159.       printf ("expand - %s\n", version_string);
  160.       exit (0);
  161.     }
  162.  
  163.   if (show_help)
  164.     usage (0);
  165.  
  166.   add_tabstop (tabval);
  167.  
  168.   validate_tabstops (tab_list, first_free_tab);
  169.  
  170.   if (first_free_tab == 0)
  171.     tab_size = 8;
  172.   else if (first_free_tab == 1)
  173.     tab_size = tab_list[0];
  174.   else
  175.     tab_size = 0;
  176.  
  177.   if (optind == argc)
  178.     file_list = stdin_argv;
  179.   else
  180.     file_list = &argv[optind];
  181.  
  182.   expand ();
  183.  
  184.   if (have_read_stdin && fclose (stdin) == EOF)
  185.     error (1, errno, "-");
  186.   if (ferror (stdout) || fclose (stdout) == EOF)
  187.     error (1, errno, "write error");
  188.  
  189.   exit (exit_status);
  190. }
  191.  
  192. /* Add the comma or blank separated list of tabstops STOPS
  193.    to the list of tabstops. */
  194.  
  195. static void
  196. parse_tabstops (stops)
  197.      char *stops;
  198. {
  199.   int tabval = -1;
  200.  
  201.   for (; *stops; stops++)
  202.     {
  203.       if (*stops == ',' || ISBLANK (*stops))
  204.     {
  205.       add_tabstop (tabval);
  206.       tabval = -1;
  207.     }
  208.       else if (ISDIGIT (*stops))
  209.     {
  210.       if (tabval == -1)
  211.         tabval = 0;
  212.       tabval = tabval * 10 + *stops - '0';
  213.     }
  214.       else
  215.     error (1, 0, "tab size contains an invalid character");
  216.     }
  217.  
  218.   add_tabstop (tabval);
  219. }
  220.  
  221. /* Add tab stop TABVAL to the end of `tab_list', except
  222.    if TABVAL is -1, do nothing. */
  223.  
  224. static void
  225. add_tabstop (tabval)
  226.      int tabval;
  227. {
  228.   if (tabval == -1)
  229.     return;
  230.   if (first_free_tab % TABLIST_BLOCK == 0)
  231.     tab_list = (int *) xrealloc (tab_list, first_free_tab + TABLIST_BLOCK);
  232.   tab_list[first_free_tab++] = tabval;
  233. }
  234.  
  235. /* Check that the list of tabstops TABS, with ENTRIES entries,
  236.    contains only nonzero, ascending values. */
  237.  
  238. static void
  239. validate_tabstops (tabs, entries)
  240.      int *tabs;
  241.      int entries;
  242. {
  243.   int prev_tab = 0;
  244.   int i;
  245.  
  246.   for (i = 0; i < entries; i++)
  247.     {
  248.       if (tabs[i] == 0)
  249.     error (1, 0, "tab size cannot be 0");
  250.       if (tabs[i] <= prev_tab)
  251.     error (1, 0, "tab sizes must be ascending");
  252.       prev_tab = tabs[i];
  253.     }
  254. }
  255.  
  256. /* Change tabs to spaces, writing to stdout.
  257.    Read each file in `file_list', in order. */
  258.  
  259. static void
  260. expand ()
  261. {
  262.   FILE *fp;            /* Input stream. */
  263.   int c;            /* Each input character. */
  264.   int tab_index = 0;        /* Index in `tab_list' of next tabstop. */
  265.   int column = 0;        /* Column on screen of the next char. */
  266.   int next_tab_column;        /* Column the next tab stop is on. */
  267.   int convert = 1;        /* If nonzero, perform translations. */
  268.  
  269.   fp = next_file ((FILE *) NULL);
  270.   if (fp == NULL)
  271.     return;
  272.   for (;;)
  273.     {
  274.       c = getc (fp);
  275.       if (c == EOF)
  276.     {
  277.       fp = next_file (fp);
  278.       if (fp == NULL)
  279.         break;        /* No more files. */
  280.       else
  281.         continue;
  282.     }
  283.  
  284.       if (c == '\n')
  285.     {
  286.       putchar (c);
  287.       tab_index = 0;
  288.       column = 0;
  289.       convert = 1;
  290.     }
  291.       else if (c == '\t' && convert)
  292.     {
  293.       if (tab_size == 0)
  294.         {
  295.           /* Do not let tab_index == first_free_tab;
  296.          stop when it is 1 less. */
  297.           while (tab_index < first_free_tab - 1
  298.              && column >= tab_list[tab_index])
  299.         tab_index++;
  300.           next_tab_column = tab_list[tab_index];
  301.           if (tab_index < first_free_tab - 1)
  302.         tab_index++;
  303.           if (column >= next_tab_column)
  304.         next_tab_column = column + 1; /* Ran out of tab stops. */
  305.         }
  306.       else
  307.         {
  308.           next_tab_column = column + tab_size - column % tab_size;
  309.         }
  310.       while (column < next_tab_column)
  311.         {
  312.           putchar (' ');
  313.           ++column;
  314.         }
  315.     }
  316.       else
  317.     {
  318.       if (convert)
  319.         {
  320.           if (c == '\b')
  321.         {
  322.           if (column > 0)
  323.             --column;
  324.         }
  325.           else
  326.         {
  327.           ++column;
  328.           if (convert_entire_line == 0)
  329.             convert = 0;
  330.         }
  331.         }
  332.       putchar (c);
  333.     }
  334.     }
  335. }
  336.  
  337. /* Close the old stream pointer FP if it is non-NULL,
  338.    and return a new one opened to read the next input file.
  339.    Open a filename of `-' as the standard input.
  340.    Return NULL if there are no more input files.  */
  341.  
  342. static FILE *
  343. next_file (fp)
  344.      FILE *fp;
  345. {
  346.   static char *prev_file;
  347.   char *file;
  348.  
  349.   if (fp)
  350.     {
  351.       if (ferror (fp))
  352.     {
  353.       error (0, errno, "%s", prev_file);
  354.       exit_status = 1;
  355.     }
  356.       if (fp == stdin)
  357.     clearerr (fp);        /* Also clear EOF. */
  358.       else if (fclose (fp) == EOF)
  359.     {
  360.       error (0, errno, "%s", prev_file);
  361.       exit_status = 1;
  362.     }
  363.     }
  364.  
  365.   while ((file = *file_list++) != NULL)
  366.     {
  367.       if (file[0] == '-' && file[1] == '\0')
  368.     {
  369.       have_read_stdin = 1;
  370.       prev_file = file;
  371.       return stdin;
  372.     }
  373.       fp = fopen (file, "r");
  374.       if (fp)
  375.     {
  376.       prev_file = file;
  377.       return fp;
  378.     }
  379.       error (0, errno, "%s", file);
  380.       exit_status = 1;
  381.     }
  382.   return NULL;
  383. }
  384.  
  385. static void
  386. usage (status)
  387.      int status;
  388. {
  389.   if (status != 0)
  390.     fprintf (stderr, "Try `%s --help' for more information.\n",
  391.          program_name);
  392.   else
  393.     {
  394.       printf ("\
  395. Usage: %s [OPTION]... [FILE]...\n\
  396. ",
  397.           program_name);
  398.       printf ("\
  399. \n\
  400.   -i, --initial       do not convert TABs after non whitespace\n\
  401.   -t, --tabs=NUMBER   have tabs NUMBER characters apart, not 8\n\
  402.   -t, --tabs=LIST     use comma separated list of explicit tab positions\n\
  403.       --help          display this help and exit\n\
  404.       --version       output version information and exit\n\
  405. \n\
  406. Instead of -t NUMBER or -t LIST, -NUMBER or -LIST may be used.  With\n\
  407. no FILE, or when FILE is -, read standard input.\n\
  408. ");
  409.     }
  410.   exit (status);
  411. }
  412.