page@swan.ulowell.edu (Bob Page) (12/29/88)
Submitted-by: fgd3@jc3b21.uucp (Fabbian G. Dufoe)
Posting-number: Volume 2, Issue 105
Archive-name: utils/ww.1
Here are two utilities which I find useful. WW.c (short for Word
Wrap) breaks lines on word boundaries. It lets the user set the
maximum line length or it defaults to 75.
[similar to the 'fmt' program on some *nix boxes. No binary. ..Bob]
# This is a shell archive.
# Remove everything above and including the cut line.
# Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar: Shell Archiver
# Run the following text with /bin/sh to create:
# ww.c
# This archive created: Wed Dec 28 14:44:47 1988
cat << \SHAR_EOF > ww.c
/* WW.c
by Fabbian G. Dufoe, III
6 December 1988
This program is released into the public domain.
This program copies standard input to standard output, inserting newline
characters ('\n') where needed to limit lines to the length specified on
the command line. The newline characters will be inserted between words
where possible. If a line contains no blanks the program will insert the
newline after the maximum number of characters have been placed on the
line.
The line length is determined by a command line argument.
Usage: WW -ln
Flags Description
l Length of lines produced is to be limited to the number n. This
argument is optional. If not specified it defaults to 75. If
n is less than 3 or greater than MAXLENGTH the program sets it
to 75.
*/
#include <stdio.h>
#define FATAL 20
#define MAXLENGTH 256
main(argc, argv)
int argc;
char **argv;
{
char CLine[MAXLENGTH];
int c;
int CurrChar = 0;
int i;
int j;
int LastBlank = 0;
int LengthAllowed = 75;
int LengthRead = 0;
void Usage(char *, char *);
if (argc > 2)
Usage(argv[0], "Invalid number of arguments.");
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '?')
Usage(argv[0], "Break lines at words so length won't exceed n.");
if (argv[i][0] != '-')
Usage(argv[0], "Invalid argument specification.");
else
switch (toupper(argv[i][1]))
{
case 'L':
LengthAllowed = atoi(&argv[i][2]);
break;
default:
Usage(argv[0], "Unknown argument.");
}
}
while ((c = getchar()) != EOF)
{
LengthRead++;
if (LengthRead <= LengthAllowed)
{
if (c == '\n')
{
CLine[CurrChar++] = c;
for (i = 0; i < CurrChar; i++)
putchar(CLine[i]);
CurrChar = 0;
LengthRead = 0;
}
else
{
if (c == ' ')
LastBlank = CurrChar;
CLine[CurrChar++] = c;
}
}
else
{
if (c == ' ')
{
CLine[CurrChar++] = '\n';
for (i = 0; i < CurrChar; i++)
putchar(CLine[i]);
CurrChar = 0;
LengthRead = 0;
}
else
{
for (i = 0; i < LastBlank; i++)
putchar(CLine[i]);
putchar('\n');
for (i = 0, j = LastBlank + 1; j < CurrChar; j++)
CLine[i++] = CLine[j];
CLine[i++] = c;
CurrChar = i;
LengthRead = i;
}
}
}
return(0);
}
void
Usage(prg, msg)
char *prg;
char *msg;
{
fprintf(stderr, "Usage: %s -ln\n", prg);
fprintf(stderr, " %s\n", msg);
exit(FATAL);
}
SHAR_EOF
# End of shell archive
exit 0
--
Bob Page, U of Lowell CS Dept. page@swan.ulowell.edu ulowell!page
Have five nice days.