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