home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / useful / dist / gnu / textutils / textutils-1.9-amiga / src / fold.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  7.1 KB  |  325 lines

  1. /* fold -- wrap each input line to fit in specified width.
  2.    Copyright (C) 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. /* Written by David MacKenzie, djm@gnu.ai.mit.edu. */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. #if defined (CONFIG_BROKETS)
  22. /* We use <config.h> instead of "config.h" so that a compilation
  23.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  24.    (which it would do because it found this file in $srcdir).  */
  25. #include <config.h>
  26. #else
  27. #include "config.h"
  28. #endif
  29. #endif
  30.  
  31. /* Get isblank from GNU libc.  */
  32. #define _GNU_SOURCE
  33.  
  34. #include <stdio.h>
  35. #include <getopt.h>
  36. #include <sys/types.h>
  37. #include "system.h"
  38. #include "version.h"
  39.  
  40. char *xrealloc ();
  41. char *xmalloc ();
  42. void error ();
  43.  
  44. /* The name this program was run with. */
  45. char *program_name;
  46.  
  47. static int adjust_column ();
  48. static int fold_file ();
  49.  
  50. /* If nonzero, try to break on whitespace. */
  51. static int break_spaces;
  52.  
  53. /* If nonzero, count bytes, not column positions. */
  54. static int count_bytes;
  55.  
  56. /* If nonzero, at least one of the files we read was standard input. */
  57. static int have_read_stdin;
  58.  
  59. /* If non-zero, display usage information and exit.  */
  60. static int show_help;
  61.  
  62. /* If non-zero, print the version on standard output then exit.  */
  63. static int show_version;
  64.  
  65. static struct option const longopts[] =
  66. {
  67.   {"bytes", no_argument, NULL, 'b'},
  68.   {"spaces", no_argument, NULL, 's'},
  69.   {"width", required_argument, NULL, 'w'},
  70.   {"help", no_argument, &show_help, 1},
  71.   {"version", no_argument, &show_version, 1},
  72.   {NULL, 0, NULL, 0}
  73. };
  74.  
  75. static void
  76. usage (status)
  77.      int status;
  78. {
  79.   if (status != 0)
  80.     fprintf (stderr, "Try `%s --help' for more information.\n",
  81.          program_name);
  82.   else
  83.     {
  84.       printf ("\
  85. Usage: %s [OPTION]... [FILE]...\n\
  86. ",
  87.           program_name);
  88.       printf ("\
  89. \n\
  90.   -b, --bytes         count bytes rather than columns\n\
  91.   -s, --spaces        break at word boundaries\n\
  92.   -w, --width=WIDTH   use WIDTH columns instead of 80\n\
  93. ");
  94.     }
  95.   exit (status);
  96. }
  97.  
  98. main (argc, argv)
  99.      int argc;
  100.      char **argv;
  101. {
  102.   int width = 80;
  103.   int i;
  104.   int optc;
  105.   int errs = 0;
  106.  
  107.   program_name = argv[0];
  108.   break_spaces = count_bytes = have_read_stdin = 0;
  109.  
  110.   /* Turn any numeric options into -w options.  */
  111.   for (i = 1; i < argc; i++)
  112.     {
  113.       if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
  114.     {
  115.       char *s;
  116.  
  117.       s = xmalloc (strlen (argv[i]) + 2);
  118.       s[0] = '-';
  119.       s[1] = 'w';
  120.       strcpy (s + 2, argv[i] + 1);
  121.       argv[i] = s;
  122.     }
  123.     }
  124.  
  125.   while ((optc = getopt_long (argc, argv, "bsw:", longopts, (int *) 0))
  126.      != EOF)
  127.     {
  128.       switch (optc)
  129.     {
  130.     case 0:
  131.       break;
  132.  
  133.     case 'b':        /* Count bytes rather than columns. */
  134.       count_bytes = 1;
  135.       break;
  136.  
  137.     case 's':        /* Break at word boundaries. */
  138.       break_spaces = 1;
  139.       break;
  140.  
  141.     case 'w':        /* Line width. */
  142.       width = atoi (optarg);
  143.       if (width < 1)
  144.         error (1, 0, "%s: invalid line width", optarg);
  145.       break;
  146.  
  147.     default:
  148.       usage (1);
  149.     }
  150.     }
  151.  
  152.   if (show_version)
  153.     {
  154.       printf ("%s\n", version_string);
  155.       exit (0);
  156.     }
  157.  
  158.   if (show_help)
  159.     usage (0);
  160.  
  161.   if (argc == optind)
  162.     errs |= fold_file ("-", width);
  163.   else
  164.     for (i = optind; i < argc; i++)
  165.       errs |= fold_file (argv[i], width);
  166.  
  167.   if (have_read_stdin && fclose (stdin) == EOF)
  168.     error (1, errno, "-");
  169.   if (fclose (stdout) == EOF)
  170.     error (1, errno, "write error");
  171.  
  172.   exit (errs);
  173. }
  174.  
  175. /* Fold file FILENAME, or standard input if FILENAME is "-",
  176.    to stdout, with maximum line length WIDTH.
  177.    Return 0 if successful, 1 if an error occurs. */
  178.  
  179. static int
  180. fold_file (filename, width)
  181.      char *filename;
  182.      int width;
  183. {
  184.   FILE *istream;
  185.   register int c;
  186.   int column = 0;        /* Screen column where next char will go. */
  187.   int offset_out = 0;        /* Index in `line_out' for next char. */
  188.   static char *line_out = NULL;
  189.   static size_t allocated_out = 0;
  190.  
  191.   if (!strcmp (filename, "-"))
  192.     {
  193.       istream = stdin;
  194.       have_read_stdin = 1;
  195.     }
  196.   else
  197.     istream = fopen (filename, "r");
  198.  
  199.   if (istream == NULL)
  200.     {
  201.       error (0, errno, "%s", filename);
  202.       return 1;
  203.     }
  204.  
  205.   while ((c = getc (istream)) != EOF)
  206.     {
  207.       if (offset_out + 1 >= allocated_out)
  208.     {
  209.       allocated_out += 1024;
  210.       line_out = xrealloc (line_out, allocated_out);
  211.     }
  212.  
  213.       if (c == '\n')
  214.     {
  215.       line_out[offset_out++] = c;
  216.       fwrite (line_out, sizeof (char), offset_out, stdout);
  217.       column = offset_out = 0;
  218.       continue;
  219.     }
  220.  
  221.     rescan:
  222.       column = adjust_column (column, c);
  223.  
  224.       if (column > width)
  225.     {
  226.       /* This character would make the line too long.
  227.          Print the line plus a newline, and make this character
  228.          start the next line. */
  229.       if (break_spaces)
  230.         {
  231.           /* Look for the last blank. */
  232.           int logical_end;
  233.  
  234.           for (logical_end = offset_out - 1; logical_end >= 0;
  235.            logical_end--)
  236.         if (ISBLANK (line_out[logical_end]))
  237.           break;
  238.           if (logical_end >= 0)
  239.         {
  240.           int i;
  241.  
  242.           /* Found a blank.  Don't output the part after it. */
  243.           logical_end++;
  244.           fwrite (line_out, sizeof (char), logical_end, stdout);
  245.           putchar ('\n');
  246.           /* Move the remainder to the beginning of the next line.
  247.              The areas being copied here might overlap. */
  248.           bcopy (line_out + logical_end, line_out,
  249.              offset_out - logical_end);
  250.           offset_out -= logical_end;
  251.           for (column = i = 0; i < offset_out; i++)
  252.             column = adjust_column (column, line_out[i]);
  253.           goto rescan;
  254.         }
  255.         }
  256.       else
  257.         {
  258.           if (offset_out == 0)
  259.         {
  260.           line_out[offset_out++] = c;
  261.           continue;
  262.         }
  263.         }
  264.       line_out[offset_out++] = '\n';
  265.       fwrite (line_out, sizeof (char), offset_out, stdout);
  266.       column = offset_out = 0;
  267.       goto rescan;
  268.     }
  269.  
  270.       line_out[offset_out++] = c;
  271.     }
  272.  
  273.   if (offset_out)
  274.     fwrite (line_out, sizeof (char), offset_out, stdout);
  275.  
  276.   if (ferror (istream))
  277.     {
  278.       error (0, errno, "%s", filename);
  279.       if (strcmp (filename, "-"))
  280.     fclose (istream);
  281.       return 1;
  282.     }
  283.   if (strcmp (filename, "-") && fclose (istream) == EOF)
  284.     {
  285.       error (0, errno, "%s", filename);
  286.       return 1;
  287.     }
  288.  
  289.   if (ferror (stdout))
  290.     {
  291.       error (0, errno, "write error");
  292.       return 1;
  293.     }
  294.  
  295.   return 0;
  296. }
  297.  
  298. /* Assuming the current column is COLUMN, return the column that
  299.    printing C will move the cursor to.
  300.    The first column is 0. */
  301.  
  302. static int
  303. adjust_column (column, c)
  304.      int column;
  305.      char c;
  306. {
  307.   if (!count_bytes)
  308.     {
  309.       if (c == '\b')
  310.     {
  311.       if (column > 0)
  312.         column--;
  313.     }
  314.       else if (c == '\r')
  315.     column = 0;
  316.       else if (c == '\t')
  317.     column = column + 8 - column % 8;
  318.       else /* if (isprint (c)) */
  319.     column++;
  320.     }
  321.   else
  322.     column++;
  323.   return column;
  324. }
  325.