mfold - Maaniker's fold and column making (new defaults)

Peter Smidt smidt at fy.chalmers.se
Thu Sep 20 04:16:57 AEST 1990


	The first version of mfold was sent to alt.sources.
	the second was sent to comp.sources.misc.
	This version has some changed options. Default insert text
	is by now a null string. Some bugs have been fixed.
	I hope this version will work on System V.

Submitted-by: smidt at cd.chalmers.se
Archive-name: mfold.shar/part01

---- Cut Here and feed the following to sh ----
#!/bin/sh
# This is mfold.shar, a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 09/19/1990 17:51 UTC by smidt at sponsz
# Source directory /nfs/alcazar/u/smidt/src/mfold/exp
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#    499 -rw-r--r-- README
#     87 -rw-r--r-- makefile
#   3676 -rw-r--r-- mfold.1
#  13960 -rw-r--r-- mfold.c
#   2613 -rw-r--r-- mfold_doc
#
# ============= README ==============
if test -f 'README' -a X"$1" != X"-c"; then
	echo 'x - skipping README (File already exists)'
else
echo 'x - extracting README (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'README' &&
The program mfold is a simpel folding and column making program.
unpack the files from the shar file put them in a empty
directory, type 'make' and you'll get the executable. A manual
is included in the shar file.
X
A bug have been removed and some optimization have been done since the first
posting to alt.sources.
X
A more exact documentation is in mfold_doc.
X
X
Bug reports, ideas, or patches could be sent to;
Peter Smidt   smidt at cd.chalmers.se    or
Peter Smidt   smidt at fy.chalmers.se
X
/Maaniker
SHAR_EOF
chmod 0644 README ||
echo 'restore of README failed'
Wc_c="`wc -c < 'README'`"
test 499 -eq "$Wc_c" ||
	echo 'README: original size 499, current size' "$Wc_c"
fi
# ============= makefile ==============
if test -f 'makefile' -a X"$1" != X"-c"; then
	echo 'x - skipping makefile (File already exists)'
else
echo 'x - extracting makefile (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'makefile' &&
mfold: mfold.o
X	cc mfold.o -o mfold
X	rm -f mfold.o
X
mfold.o: mfold.c
X	cc -c mfold.c -O
SHAR_EOF
chmod 0644 makefile ||
echo 'restore of makefile failed'
Wc_c="`wc -c < 'makefile'`"
test 87 -eq "$Wc_c" ||
	echo 'makefile: original size 87, current size' "$Wc_c"
fi
# ============= mfold.1 ==============
if test -f 'mfold.1' -a X"$1" != X"-c"; then
	echo 'x - skipping mfold.1 (File already exists)'
else
echo 'x - extracting mfold.1 (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'mfold.1' &&
.\" Public Domain 1990 of Chalmers Computer Society.
.\"
.\"	@(#)mfold.1	(G|teborg) 1990-09-11
.\"
.TH MFOLD 1 "September 11, 1990"
.UC
.SH NAME
mfold \- Maaniker's fold and column making
.SH SYNOPSIS
.B mfold
[
.B \-sn -ln -en -wn -f -d
.B -rn
.B -tn -cn -pn -n -L -Bn
.B -D '<text>' -i '<text>'
]
.SH DESCRIPTION
.I Mfold
reads text files from standard input and writes to standard
output.
.I Mfold
concatenates and folds the text in one or more columns
without breaking in the middle of a word. Tabs and newlines are
converted to blank space. Multiple blank space is substituted with
one single blank space. Some flags don't do anything if they aren't
used together with either '-c' or '-d'.
.PP
There is a limited amount of options:
.TP
.B \-sn
Start line for
.I mfold,
where n is the line number. The preceding text
is passed through
.I mfold
without being changed. If an end-of-file character is encountered
before line n,
.I mfold
will end the execution.
.TP
.B \-i
Insert text at the beginning of every line, or with '\-c', at every
column. This option as the last flag doesn't need any argument if
a null argument is wanted. A space is required between the flag
and the argument. Tabs may be present in the insert text. Default
insert text is a null string.
.TP
.B \-ln
Lenght for output lines, excluding any insert text and excluding
any delimiter text. Where n is the number of characters. Default
length is 80.
.TP
.B \-en
.I Mfold
will end the folding at input line n. Text coming after this
line is not being changed by
.I mfold.
.TP
.B \-wn
Specify the width n, of the not folded lines to avoid overrunning the
internal reverse buffer when using the '-d' flag. '-w' is not always
provided. Run
.I mfold,
look at the result and decide if you want to specify the full
width. Default is 80 characters.
.TP
.B \-f
Fill each line with blank space to its full line length.
.TP
.B \-rn
Fills the lines with extra blank space up to an amount of
n blank spaces, to get an even right margin. The words will get
a little random placement on the lines. If the text looks very
ugly specify a little smaller n.
.TP
.B \-d
Reverse the text for dyslexics.
.TP
.B \-tn
Expanding the tabs to spaces. Where n is the number of
spaces. Default is four spaces. Use only with '-d'.
.TP
.B \-cn
Specifies columns where n is the number of columns. Text is inserted at
every column.
.TP
.B \-pn
Specifies page length n, meaningfull when used with the '-c'
flag. Default are 40 lines per page.
.TP
.B \-n
Many newlines in a row is not substituted with one single 
blank space, which is otherwise the default. Instead they are
left without change, but may be put in a separate column.
.TP
.B \-L
Write a ^L (newpage) at the end of each page. Use with '-c'.
.TP
.B \-Bn
Write n newlines at the bottom (end) of the page. Default is
one newline. Use when the number of columns are greater then one.
.TP
.B \-D
Specify column delimiter. Default is three blank spaces. Tabs may be
present in the delimiter. This option as the last flag doesn't need
any argument if a null argument is wanted.
.SH ERRORS
You will notice them, when you give bad flags.
.SH AUTHOR
Peter Smidt, Chalmers Computer Society.
.SH SEE ALSO
awk(1), sed(1), lex(1), nroff(1), fmt(1), fold(1), rev(1)
.SH BUGS
If underlining is present it will get messed up with the
text. The '-c' flag
.I may
produce some extra
unnecessary blank space at the end of the lines. When
a word is longer than the line length, the word may be
cut at an inappropriate place. Everything except blank space,
newline and tab counts as parts of words.
.SH BUG REPORTS TO
Peter Smidt  smidt at cd.chalmers.se    or
.br
Peter Smidt  smidt at fy.chalmers.se
SHAR_EOF
chmod 0644 mfold.1 ||
echo 'restore of mfold.1 failed'
Wc_c="`wc -c < 'mfold.1'`"
test 3676 -eq "$Wc_c" ||
	echo 'mfold.1: original size 3676, current size' "$Wc_c"
fi
# ============= mfold.c ==============
if test -f 'mfold.c' -a X"$1" != X"-c"; then
	echo 'x - skipping mfold.c (File already exists)'
else
echo 'x - extracting mfold.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'mfold.c' &&
#include <stdio.h>
#include <curses.h>
X
#define DEF_LINE_LEN	80
#define DEF_WORD_LEN	40
#define DEF_TAB_LEN		4
#define FULL_WIDTH		80
#define PAGE_LEN		40
#define POS1			1
#define DO_ALL1 \
X	col_pos = POS1;\
X	for ( doo = 0; doo < ins_len; doo++ ) {\
X		putcolu(insert[doo]);\
X	}
#define BOT_LINES		1
#define STR				15
#define TEST_STR		25
#define GET_NUM_STR		25
X
int colu_chars, colu_lines;
int page_len = PAGE_LEN, columns = 1, lin = 0, bot_lines = BOT_LINES;
int got_newpage = FALSE, got_dyslexi = FALSE, full_line = FULL_WIDTH;
int tab_len = DEF_TAB_LEN, ins_tabs = 0, del_len, got_fill = FALSE;
int got_mrandom = FALSE, got_bot = FALSE, ins_len, mrandom = 1;
char **cur_page, *malloc(), *delim = "   ", **glob_argv;
X
main(argc, argv)
char *argv[];
int argc;
{
X	int col_pos = 1, j = 0, k, in, doo, line_len = DEF_LINE_LEN, row_count = 1;
X	int cnt_nwl = 0, new_wo_le, dummy = TRUE, end = 2;
X	int width = FULL_WIDTH, err = FALSE, in_2 = 0, in_3 = 0, tmp_chars = 0;
X	char *cur_word, *insert = "", *s = "Bad option '  '";
X	int index, start = 1, word_len = DEF_WORD_LEN;
X	int got_start = FALSE, got_line_len = FALSE, got_insert = FALSE;
X	int got_tab_len = FALSE, got_end = FALSE, got_width = FALSE;
X	int got_columns = FALSE, got_page_len = FALSE;
X	int got_newlines = FALSE, got_delim = FALSE;
X	int index_start = 0, index_insert = 0, index_line_len = 0;
X	int index_tab_len = 0, index_end = 0, index_width = 0;
X	int index_columns = 0, index_page_len = 0, index_delim = 0;
X	int index_bot = 0, index_mrandom = 0;
X	glob_argv = argv;
X	for ( index = 1; index < argc; index++) {
X		if ( argv[index][0] != '-' ) {
X			if ( index != 1 && argv[index - 1][0] == '-' ) {
X					if ( argv[index - 1][1] == 'i' ) {
X						index_insert = index;
X					} else if ( argv[index - 1][1] == 'D' ) {
X						index_delim = index;
X					} else {
X						err = TRUE;
X					}
X			} else {
X				err = TRUE;
X			}
X			if ( err ) {
X				fprintf(stderr, "Flag '%s' not allowed.\n", argv[index]);
X				usage(11);
X			}
X		}
X		doo = index_insert != index && index_delim != index;
X		switch ( argv[index][1] ) {
X			case 'i':
X				check_it(&got_insert, "-i", 8, doo, &index_insert, index + 1);
X				break;
X			case 's':
X				check_it(&got_start, "-s", 9, doo, &index_start, index);
X				break;
X			case 'l':
X				check_it(&got_line_len, "-l", 10, doo, &index_line_len, index);
X				break;
X			case 'e':
X				check_it(&got_end, "-e", 10, doo, &index_end, index);
X				break;
X			case 'w':
X				check_it(&got_width, "-w", 10, doo, &index_width, index);
X				break;
X			case 'r':
X				check_it(&got_mrandom, "-r", 37, doo, &index_mrandom, index);
X				break;
X			case 'd':
X				check_it(&got_dyslexi, "-d", 11, doo, &dummy, index);
X				break;
X			case 't':
X				check_it(&got_tab_len, "-t", 13, doo, &index_tab_len, index);
X				break;
X			case 'c':
X				check_it(&got_columns, "-c", 15, doo, &index_columns, index);
X				break;
X			case 'p':
X				check_it(&got_page_len, "-p", 16, doo, &index_page_len, index);
X				break;
X			case 'B':
X				check_it(&got_bot, "-B", 17, doo, &index_bot, index);
X				break;
X			case 'f':
X				check_it(&got_fill, "-f", 33, doo, &dummy, index);
X				break;
X			case 'n':
X				check_it(&got_newlines, "-n", 18, doo, &dummy, index);
X				break;
X			case 'L':
X				check_it(&got_newpage, "-L", 19, doo, &dummy, index);
X				break;
X			case 'D':
X				check_it(&got_delim, "-D", 36, doo, &index_delim, index + 1);
X				break;
X			case '\0':
X				write_err(doo, "Empty flag '-'", 31);
X				break;
X			default:
X				s[12] = argv[index][0];
X				s[13] = argv[index][1];
X				write_err(doo, s, 20);
X				break;
X		}
X	}
X	ext_num(got_start, &start, argv[index_start],
X	"Line zero, for start, not allowed", 21);
X	ext_num(got_page_len, &page_len, argv[index_page_len],
X	"Page length zero, not allowed", 24);
X	ext_num(got_tab_len, &tab_len, argv[index_tab_len],
X	"Tab length zero, not allowed", 27);
X	ext_num(got_line_len, &line_len, argv[index_line_len],
X	"Line length zero, not allowed", 25);
X	write_err(line_len < 2, "Too short line length, not allowed", 32);
X	ext_num(got_end, &end, argv[index_end],
X	"End length zero, not allowed", 25);
X	write_err(got_end && end <= start,
X	"End not greater than start line, not allowed", 33);
X	ext_num(got_width, &width, argv[index_width],
X	"Line length zero, not allowed", 25);
X	ext_num(got_mrandom, &mrandom, argv[index_mrandom],
X	"Zero fill length, not allowed", 36);
X	if ( got_bot ) get_num(&bot_lines, argv[index_bot]);
X	if ( got_insert ) {
X		if ( index_insert < argc ) {
X			insert = argv[index_insert];
X		} else {
X			insert = "";
X		}
X	}
X	if ( got_delim ) {
X		if ( index_delim < argc ) {
X			delim = argv[index_delim];
X		} else {
X			delim = "";
X		}
X	}
X	del_len = str_len(delim);
X	if ( got_columns ) {
X		get_num(&columns, argv[index_columns]);
X		write_err(columns == 0, "columns count zero, not allowed", 26);
X		write_err(line_len % columns,
X		"Lines not an even multiple of columns length", 27);
X	}
/* colu_chars is the chars on one column. colu_lines is the total number of
lines in all the columns in one page. page_len is the number of lines in one
page. */
X	ins_len = str_len(insert);
X	colu_chars = line_len / columns + ins_len;
X	colu_lines = page_len * columns;
X	write_err( !(cur_page = (char**) malloc(colu_lines * sizeof(char*))),
X	"Can not malloc that page length", 39);
X	for ( in = 0; in < colu_lines; in++ ) {
X		if ( !(cur_page[in] = malloc(colu_chars * sizeof(char))) ) {
X			write_err(TRUE, "Can not malloc that page length", 40);
X		}
X	}
X	for ( doo = 0; doo < ins_len; doo++ ) {
X		if ( insert[doo] == '\t' ) {
X			ins_tabs++;
X		}
X	}
X	full_line = line_len + ins_len * columns + del_len * ( columns - 1);
X	full_line += ( tab_len - 1 ) * columns * ins_tabs;
X	line_len = line_len / columns;
X	word_len = line_len;
X	write_err( !(cur_word = malloc(word_len * sizeof(char))),
X	"Can not malloc that word (line?) length", 41);
X	if ( width > full_line ) initrev(width);
X	else initrev(full_line);
/* ************* Write text according to the '-s' flag **** */
X	while ( row_count < start ) {
X		in = getchar();
X		if ( in == EOF ) exit(0);
X		if ( in == '\n' ) {
X			if ( got_dyslexi ) {
X				flushrev();
X			}
X			putchar('\n');
X			row_count++;
X		} else if ( got_dyslexi ) {
X			putrev(in);
X		} else {
X			putchar(in);
X		}
X	}
X	if ( !got_end ) end = row_count + 1;
X	lin = ( ( start - 1 ) % page_len ) * columns;
X	new_wo_le = word_len - 1;
/* ******************** The fold follows ******************** */
X	DO_ALL1
X	while ( 1 ) {
X		if ( row_count > end ) {
X			in = EOF;
X		} else if ( j == new_wo_le && tmp_chars == 0 ) {
X			in_2 = getchar();
X			if ( in_2 == ' ' || in_2 == '\n' || in_2 == '\t' ) {
X				in = in_2;
X			} else {
X				in_3 = getchar();
X				if ( in_3 == ' ' || in_3 == '\n' || in_3 == '\t' ) {
X					in = in_2;
X					tmp_chars = 1;
X				} else {
X					in = '-';
X					tmp_chars = 2;
X				}
X			}
X		} else if ( j == word_len ) { 
X			in = ' ';
X		} else if ( tmp_chars > 0 ) {
X			if ( tmp_chars == 1 && j == 0 ) {
X				in = in_3;
X				tmp_chars = 0;
X			} else if ( tmp_chars == 1 && j == 1 ) {
X				in_2 = in_3;
X				tmp_chars = 0;
X				if ( line_len != 2 || in_2 == ' ' || in_2 == '\n'
X				|| in_2 == '\t' ) {
X					in = in_2;
X				} else {
X					in_3 = getchar();
X					if ( in_3 == ' ' || in_3 == '\n' || in_3 == '\t' ) {
X						in = in_2;
X						tmp_chars = 1;
X					} else {
X						in = '-';
X						tmp_chars = 2;
X					}
X				}
X			} else {
X				in = in_2;
X				tmp_chars = 1;
X			}
X		} else { 
X			in = getchar();
X		}
X		if ( in != '\n' ) {
X			if ( cnt_nwl > 1 && got_newlines ) {
X				while ( --cnt_nwl ) {
X					putcolu('\n');
X					putcolu(' ');
X				}
X				putcolu('\n');
X				DO_ALL1
X			}
X			cnt_nwl = 0;
X			if ( in == '\t' ) in = ' ';
X		} else {
X			if ( got_end ) row_count++;
X			if ( got_newlines ) cnt_nwl++;
X			in = ' ';
X		}
X		if ( in == EOF ) {
X			putcolu('\n');
X			flushpage(columns);
X			if ( !got_end ) exit(0);
X			else break;
X		}
X		if ( in != ' ' ) {
X			write_err( j >= word_len || j < 0 ,
X			"Internal error or to long text word", 3);
X			cur_word[j++] = in;
X		} else {
X			if ( col_pos != POS1 && (col_pos + j) <= line_len &&
X			j != 0 ) {
X				putcolu(' ');
X				col_pos++;
X			} else if ( (col_pos + j) > line_len && col_pos != POS1 ) {
X				putcolu('\n');
X				DO_ALL1
X			}
X			for ( k = 0; k < j; k++ ) {
X				putcolu(cur_word[k]);
X			}
X			col_pos += j;
X			j = 0;
X		}
X	}
/* ***************** End of the fold ********************** */
/* ************* Write text according to the '-e' flag **** */
X	if ( cnt_nwl > 1 && got_newlines ) {
X		while ( --cnt_nwl ) {
X			putcolu('\n');
X			putcolu(' ');
X		}
X		putcolu('\n');
X		flushpage(columns);
X	}
X	while ( 1 ) {
X		in = getchar();
X		if ( in == EOF ) exit(0);
X		if ( in == '\n' ) {
X			if ( got_dyslexi ) {
X				flushrev();
X			}
X			putchar('\n');
X		} else if ( got_dyslexi ) {
X			putrev(in);
X		} else {
X			putchar(in);
X		}
X	}
}
X
char *buff;
int gl_i = 0, len;
/* lin is the line index in the one long column before it is pasted out onto the
page. len is the lenght of each line in the one long column. */
X
putrev(c)
{
X	int i;
X
X	if ( gl_i >= 0 && gl_i < len ) {
X		if ( c != '\t' ) {
X			buff[gl_i++] = c;
X		} else {
X			int k = tab_len - gl_i % tab_len;
X			for ( i = 0; i < k; i++ ) {
X				putrev(' ');
X			}
X		}
X	} else {
X		write_err(TRUE,
X		"Internal error in reverse buffer. Specify bigger buffer", 4);
X	}
}
X
flushrev() {
X	int i, first = 0;
X
X	if ( buff[first] == ' ' ) while ( buff[++first] == ' ' );
X	for ( i = len - 1; i >= first; i--) {
X		if ( buff[i] != '\0' ) {
X			putchar(buff[i]);
X			buff[i] = '\0';
X		} else {
X			putchar(' ');
X		}
X	}
X	gl_i = 0;
}
X
initrev(l)
int l;
{
X	int i;
X
X	len = l;
X	write_err( !(buff = malloc(len * sizeof(char) + 1)),
X	"Can not malloc that internal reverse buffer length", 42);
X	for ( i = 0; i < len; i++ ) {
X		buff[i] = '\0';
X	}
X	buff[len] = '#'; /* To help flushrev swallow blank lines */
}
X
check_it(got_flag, s, err, boole, index_flag, index)
int *got_flag, *index_flag;
char *s;
{
X	test(*got_flag, s, err);
X	test_two(boole, index_flag, index, got_flag);
}
X
test(flag, s1, err)
char *s1;
{
X	char *s2 = "Multiple '  ' not allowed";
X
X	s2[10] = s1[0];
X	s2[11] = s1[1];
X	write_err(flag, s2, err);
}
X
test_two(boole, index_flag, index, got_flag)
int *index_flag, *got_flag;
{
X	if ( boole ) {
X		if ( *index_flag && glob_argv[index][2] != '\0' ) {
X			fprintf(stderr, "Flag '%c%c' doesn't want any argument.\n",
X			glob_argv[index][0], glob_argv[index][1]);
X			usage(35);
X		}
X		*index_flag = index;
X		*got_flag = TRUE;
X	}
}
X
write_err(flag, s, err)
char *s;
{
X	if ( flag ) {
X		fprintf(stderr, "%s.\n", s);
X		usage(err);
X	}
}
X
ext_num(bulle, tal, arg, s, err)
int *tal;
char *arg, *s;
{
X	if ( bulle ) {
X		get_num(tal, arg);
X		write_err(*tal == 0, s, err);
X	}
}
X
get_num(number, argv) 
int *number;
char argv[];
{
X	int k, in, tmp;
X	char *s = "Not a number in flag '  '";
X
X	s[22] = argv[0];
X	s[23] = argv[1];
X	*number = 0;
X	k = str_len(argv);
X	write_err(k == 1, s, 9);
X	for( in = 2; in < k; in++) {
X		tmp = argv[in] - '0';
X		write_err(tmp < 0 || tmp > 9, "Bad flag, N-A-P-N", 5);
X		*number = tmp + *number * 10;
X	}
}
X
str_len(s)
char *s;
{
X	int m = 0;
X
X	if ( s[m] != '\0' ) while ( s[++m] != '\0' );
X	return m;
}
X
usage(t) {
X	fprintf(stderr, "[ %d ]  Usage: mfold [ -sn -ln -en -wn -f -d -rn -tn -cn -pn -n -L -Bn\n-D '<text>' -i '<text>' ]\n", t);
X	exit(t);
}
X
int col = 0;
X
putcolu(c)
char c;
{
X	if ( c == '\n' || col == colu_chars ) {
X		advance_line();
X		return;
X	}
X	cur_page[lin][col++] = c;
}
X
advance_line() {
X		col = 0;
X		if ( ++lin == colu_lines ) {
X			flushpage(columns);
X			end_page();
X		}
}
X
end_page() {
X	int i;
X
X	if ( columns > 1 || got_bot ) {
X		for ( i = 0; i < bot_lines; i++ ) {
X			putchar('\n');
X		}
X	}
X	if ( got_newpage ) putchar(12);		/* 12 == ^L  (ascii) */
}
X
flushpage(columns) {
X	int line_sta = 0, cs, tmpl, lin_diff, lin_end;
X	int end_col = columns - 1, lin_sto = colu_lines, end_char = colu_chars - 1;
X
X	for ( lin = 0; lin < colu_lines; lin++ ) {
X		if ( cur_page[lin][0] != '\0' ) {
X			line_sta = lin;
X			break;
X		}
X	}
X	for ( lin = line_sta; lin < colu_lines; lin += columns ) {
X		if ( cur_page[lin][0] == '\0' ) {
X			lin_sto =  lin;
X			break;
X		}
X	}
X	lin_diff = (lin_sto - line_sta) / columns;
X	lin_end = line_sta + lin_diff;
X	for ( lin = line_sta; lin < lin_end; lin++) {
X		if ( cur_page[lin][0] != ' ' || cur_page[lin][1] != '\0'
X		|| columns != 1 ) {
X			for ( cs = 0; cs < columns; cs++ ) {
X				tmpl = lin + lin_diff * cs;
X				if ( cur_page[tmpl][end_char] == '\0' && got_mrandom ) {
X					fill_sp(tmpl, end_char);
X				}
X				for ( col = 0; col < colu_chars; col++ ) {
X					if ( cur_page[tmpl][col] == '\0' ) {
X						if ( cs == end_col && !got_fill ) break;
X						if ( got_dyslexi ) putrev(' ');
X						else putchar(' ');
X					} else {
X						if ( got_dyslexi ) putrev(cur_page[tmpl][col]);
X						else putchar(cur_page[tmpl][col]);
X						cur_page[tmpl][col] = '\0';
X					}
X				}
X				if ( cs < end_col ) {
X					for ( col = 0; col < del_len; col++ ) {
X						if ( got_dyslexi ) putrev(delim[col]);
X						else putchar(delim[col]);
X					}
X				}
X			}
X			if ( got_dyslexi ) flushrev();
X		}
X		putchar('\n');
X	}
X	lin = col = 0;
}
X
fill_sp(line, end_char) {
X	int pass = 0, last, nulls = end_char, words = 0, i, found_sp = FALSE;
X	int found_word = FALSE, moves, new_end, old_end;
X
X	while ( cur_page[line][--nulls] == '\0' && nulls != 1 );
X	nulls = end_char - nulls;
X	last = end_char - nulls;
X	for ( i = ins_len; i <= last; i++) {
X		if ( cur_page[line][i] != ' ' ) {
X			if ( !found_word ) {
X				words++;
X				found_word = TRUE;
X			}
X		} else {
X			found_word = FALSE;
X		}
X	}
X	if ( words < 2 ) return;
X	old_end = last;
X	while ( ++pass < mrandom ) {
X		if ( words > nulls ) moves = nulls;
X		else moves = words - 1;
X		i = moves;
X		new_end = moves + old_end;
X		last = new_end;
X		while( 1 ) {
X			cur_page[line][new_end--] = cur_page[line][old_end--];
X			if ( old_end < 0 ) break;
X			if ( cur_page[line][old_end] == ' ' ) {
X				if ( !found_sp ) {
X					if ( moves-- > 0 ) cur_page[line][new_end--] = ' ' ;
X				}
X				found_sp = TRUE;
X			} else {
X				found_sp = FALSE;
X			}
X		}
X		if ( cur_page[line][end_char] != '\0' ) return;
X		nulls = nulls - i;
X		old_end = last;
X	}
}
SHAR_EOF
chmod 0644 mfold.c ||
echo 'restore of mfold.c failed'
Wc_c="`wc -c < 'mfold.c'`"
test 13960 -eq "$Wc_c" ||
	echo 'mfold.c: original size 13960, current size' "$Wc_c"
fi
# ============= mfold_doc ==============
if test -f 'mfold_doc' -a X"$1" != X"-c"; then
	echo 'x - skipping mfold_doc (File already exists)'
else
echo 'x - extracting mfold_doc (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'mfold_doc' &&
The documentation follows:
X
The program have some large main parts:
X
X    1) One part scans the options and their arguments from the command line.
X
X    2) One part reads text input which not should be changed and
X    writes the text to standard output.
X
X    3) One part does the actual folding and inserts text from
X    the '-i' flag. This part does also cut the words if they are too
X    long and puts a '-' at the end of the cut word.
X
X    4) One part reads input text which not should be changed after
X    the folding, writes to standard output.
X
Variables with names 'got_blabla' and 'index_blabla' refers to
the different flags the programs can receive. The program don't
use getopts(3) and have therefore a more rigid option format
in the shell. But it's easier to handle errors this way.
X
The '-i' and '-D' options are handled as special cases by mfold.
Remaining options are handled in one separate switch construction.
X
The text which is to be folded is read into an internal 'page' buffer.
This buffer has the width of one column and the length of all the
lines in ALL columns on one page. The buffer is named 'cur_page' and
is a global variable.
X
The folding is done in a 'while ( 1 )' loop. The first thing done
in this loop is a large 'if' statment, about 44 lines long. This
'if' stament looks if a word is larger than the line length and then
cuts the word, saves some charcters in som temporary variables,
'in_2' and 'in_3'. This construction can handle 2 (two) character
long lines. Then some newline handeling is done, see the '-n'
option in the manual to understand the source easier. A 'if'
statement look if the next charcter is a blank space or not a
blank space. If it is a space, the word in 'cur_word' is written to
the output, and if it's not, the character is put into 'cur_word'.
When encountering an EOF, mfold exits the 'while ( 1 )' loop.
X
The output is not sent to standard output directly. Instead a
funtion 'putcolu' is used. 'putcolu' writes to the internal
page buffer 'cur_page'. When 'cur_page' is full, the function
'flush_page' writes 'cur_page to the output. But if the '-d'
flag is used the output is written to 'putrev' instead, where
the function 'flushrev' flushes the internal reverse script
buffer, one line. When the '-r' flag is set, the function
'fill_sp' fills blank space until every line is right justified.
X
There is some more help functions, but they are very simple
and should be understand without explanation... :-) ?
X
X
Bug reports, ideas, or patches could be sent to;
Peter Smidt   smidt at cd.chalmers.se    or
Peter Smidt   smidt at fy.chalmers.se
X
/Maaniker
SHAR_EOF
chmod 0644 mfold_doc ||
echo 'restore of mfold_doc failed'
Wc_c="`wc -c < 'mfold_doc'`"
test 2613 -eq "$Wc_c" ||
	echo 'mfold_doc: original size 2613, current size' "$Wc_c"
fi
exit 0
-- 
+=======================================+
"The whole valley is like a smorgasbord."
					-- TREMORS



More information about the Alt.sources mailing list