[comp.sources.misc] v14i097: mfold - fold and concatenate lines

smidt@cd.chalmers.se (09/16/90)

Posting-number: Volume 14, Issue 97
Submitted-by: smidt@cd.chalmers.se
Archive-name: mfold/part01

[Yet Another Submission Without Descriptive Text Or Subject.  I thought only
lsrhs!root (...Or Poster) did that.  ++bsa]

Mfold have previously been published in alt.sources.
A bug have been fixed and a better documentation have
been created. There have been some small changes to
the man pages and the makefile.

---- 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/15/1990 08:55 UTC by smidt@sponsz
# Source directory /nfs/alcazar/u/smidt/src/mfold/exp
#
# existing files will NOT be overwritten unless -c is specified
# This format requires very little intelligence at unshar time.
# "if test", "echo", "true", and "sed" may be needed.
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#     87 -rw-r--r-- makefile
#   3626 -rw-r--r-- mfold.1
#  13693 -rw-r--r-- mfold.c
#   2613 -rw-r--r-- mfold_doc
#    499 -rw-r--r-- README
#
# ============= 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' &&
Xmfold: mfold.o
X	cc mfold.o -o mfold
X	rm -f mfold.o
X
Xmfold.o: mfold.c
X	cc -c mfold.c -O
SHAR_EOF
true || echo 'restore of makefile failed'
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' &&
X.\" Public Domain 1990 of Chalmers Computer Society.
X.\"
X.\"	@(#)mfold.1	(G|teborg) 1990-09-11
X.\"
X.TH MFOLD 1 "September 11, 1990"
X.UC
X.SH NAME
Xmfold \- Maaniker's fold and column making
X.SH SYNOPSIS
X.B mfold
X[
X.B \-sn -ln -en -wn -f -d
X.B -rn
X.B -tn -cn -pn -n -L -Bn
X.B -D '<text>' -i '<text>'
X]
X.SH DESCRIPTION
X.I Mfold
Xreads text files from standard input and writes to standard
Xoutput.
X.I Mfold
Xconcatenates and folds the text in one or more columns
Xwithout breaking in the middle of a word. Tabs and newlines are
Xconverted to blank space. Multiple blank space is substituted with
Xone single blank space. Some flags don't do anything if they aren't
Xused together with either '-c' or '-d'.
X.PP
XThere is a limited amount of options:
X.TP
X.B \-sn
XStart line for
X.I mfold,
Xwhere n is the line number. The preceding text
Xis passed through
X.I mfold
Xwithout being changed. If an end-of-file character is encountered
Xbefore line n,
X.I mfold
Xwill end the execution.
X.TP
X.B \-i
XInsert text at the beginning of every line, or with '\-c', at every
Xcolumn. This option as the last flag doesn't need any argument if
Xa null argument is wanted. A space is required between the flag
Xand the argument. Tabs may be present in the insert text. Default
Xinsert text is a tab.
X.TP
X.B \-ln
XLenght for output lines, excluding any insert text and excluding
Xany delimiter text. Where n is the number of characters. Default
Xlength is 76.
X.TP
X.B \-en
X.I Mfold
Xwill end the folding at input line n. Text coming after this
Xline is not being changed by
X.I mfold.
X.TP
X.B \-wn
XSpecify the width n, of the not folded lines to avoid overrunning the
Xinternal reverse buffer when using the '-d' flag. '-w' is not always
Xprovided. Run
X.I mfold,
Xlook at the result and decide if you want to specify the full
Xwidth. Default is 80 characters.
X.TP
X.B \-f
XFill each line with blank space to its full line length.
X.TP
X.B \-rn
XFills the lines with extra blank space up to an amount of
Xn blank spaces, to get an even right margin. The words will get
Xa little random placement on the lines.
X.TP
X.B \-d
XReverse the text for dyslexics.
X.TP
X.B \-tn
XExpanding the tabs to spaces. Where n is the number of
Xspaces. Default is four spaces. Use only with '-d'.
X.TP
X.B \-cn
XSpecifies columns where n is the number of columns. Text is inserted at
Xevery column.
X.TP
X.B \-pn
XSpecifies page length n, meaningfull when used with the '-c'
Xflag. Default are 40 lines per page.
X.TP
X.B \-n
XMany newlines in a row is not substituted with one single 
Xblank space, which is otherwise the default. Instead they are
Xleft without change, but may be put in a separate column.
X.TP
X.B \-L
XWrite a ^L (newpage) at the end of each page. Use with '-c'.
X.TP
X.B \-Bn
XWrite n newlines at the bottom (end) of the page. Default is
Xone newline. Use when the number of columns are greater then one.
X.TP
X.B \-D
XSpecify column delimiter. Default is three blank spaces. Tabs may be
Xpresent in the delimiter. This option as the last flag doesn't need
Xany argument if a null argument is wanted.
X.SH ERRORS
XYou will notice them, when you give bad flags.
X.SH AUTHOR
XPeter Smidt, Chalmers Computer Society.
X.SH SEE ALSO
Xawk(1), sed(1), lex(1), nroff(1), fmt(1), fold(1), rev(1)
X.SH BUGS
XIf underlining is present it will get messed up with the
Xtext. The '-c' and the '-d' flags
X.I may
Xproduce some extra
Xunnecessary blank space at the end of the lines. When
Xa word is longer than the line length, the word may be
Xcut at an inappropriate place. Everything except blank space,
Xnewline and tab counts as parts of words.
X.SH BUG REPORTS TO
XPeter Smidt  smidt@cd.chalmers.se    or
X.br
XPeter Smidt  smidt@fy.chalmers.se
SHAR_EOF
true || echo 'restore of mfold.1 failed'
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' &&
X#include <stdio.h>
X#include <curses.h>
X#include <strings.h>
X
X#define DEF_LINE_LEN	76
X#define DEF_WORD_LEN	40
X#define DEF_TAB_LEN		4
X#define FULL_WIDTH		80
X#define PAGE_LEN		40
X#define POS1			1
X#define DO_ALL1 \
X	col_pos = POS1;\
X	for ( doo = 0; doo < ins_len; doo++ ) {\
X		putcolu(insert[doo]);\
X	}
X#define BOT_LINES		1
X#define STR				15
X#define TEST_STR		25
X#define GET_NUM_STR		25
X
Xint colu_chars, colu_lines;
Xint page_len = PAGE_LEN, columns = 1, lin = 0, bot_lines = BOT_LINES;
Xint got_newpage = FALSE, got_dyslexi = FALSE, full_line = FULL_WIDTH;
Xint tab_len = DEF_TAB_LEN, ins_tabs = 0, del_len, got_fill = FALSE;
Xint got_mrandom = FALSE, got_bot = FALSE, ins_len, mrandom = 1;
Xchar **cur_page, *malloc(), *delim = "   ", **glob_argv;
X
Xmain(argc, argv)
Xchar *argv[];
Xint argc;
X{
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 = "\t", *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 ) {
X				if ( 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			} 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 = strlen(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	}
X/* colu_chars is the chars on one column. colu_lines is the total number of
Xlines in all the columns in one page. page_len is the number of lines in one
Xpage. */
X	ins_len = strlen(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);
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;
X/* ******************** 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 ( cnt_nwl > 1 && got_newlines && in != '\n' ) {
X			putcolu('\n');
X			while ( --cnt_nwl ) {
X				putcolu(' ');
X				putcolu('\n');
X			}
X			DO_ALL1
X		}
X		if ( in != '\n' ) cnt_nwl = 0;
X		if ( in == '\n' || in == '\t' ) {
X			if ( in == '\n' ) {
X				if ( got_end ) row_count++;
X				if ( got_newlines ) cnt_nwl++;
X			}
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	}
X/* ************** End of the fold ****************** */
X	if ( cnt_nwl > 1 && got_newlines ) {
X		while ( cnt_nwl-- ) putcolu('\n');
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}
X
Xchar *buff;
Xint gl_i = 0, len;
X/* lin is the line index in the one long column before it is pasted out onto the
Xpage. len is the lenght of each line in the one long column. */
X
Xputrev(c)
X{
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}
X
Xflushrev() {
X	int i;
X
X	for ( i = len - 1; i > 0; i--) {
X		if ( buff[i] != '\0' ) {
X			putchar(buff[i]);
X			buff[i] = '\0';
X		} else {
X			putchar(' ');
X		}
X	}
X	if ( buff[0] != ' ' ) {
X		putchar(buff[0]);
X	}
X	gl_i = 0;
X}
X
Xinitrev(l)
Xint l;
X{
X	int i;
X
X	len = l;
X	write_err( !(buff = malloc(len * sizeof(char))),
X	"Can not malloc that internal reverse buffer length", 42);
X	for ( i = 0; i < len; i++ ) {
X		buff[i] = '\0';
X	}
X}
X
Xcheck_it(got_flag, s, err, boole, index_flag, index)
Xint *got_flag, *index_flag;
Xchar *s;
X{
X	test(*got_flag, s, err);
X	test_two(boole, index_flag, index, got_flag);
X}
X
Xtest(flag, s1, err)
Xchar *s1;
X{
X	char *s2 = "Multiple '  ' not allowed";
X
X	s2[10] = s1[0];
X	s2[11] = s1[1];
X	write_err(flag, s2, err);
X}
X
Xtest_two(boole, index_flag, index, got_flag)
Xint *index_flag, *got_flag;
X{
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}
X
Xwrite_err(flag, s, err)
Xchar *s;
X{
X	if ( flag ) {
X		fprintf(stderr, "%s.\n", s);
X		usage(err);
X	}
X}
X
Xext_num(bulle, tal, arg, s, err)
Xint *tal;
Xchar *arg, *s;
X{
X	if ( bulle ) {
X		get_num(tal, arg);
X		write_err(*tal == 0, s, err);
X	}
X}
X
Xget_num(number, argv) 
Xint *number;
Xchar argv[];
X{
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 = strlen(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}
X
Xusage(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}
X
Xint col = 0;
X
Xputcolu(c)
Xchar c;
X{
X	if ( c == '\n' || col == colu_chars ) {
X		advance_line();
X		return;
X	}
X	cur_page[lin][col++] = c;
X}
X
Xadvance_line() {
X		col = 0;
X		if ( ++lin == colu_lines ) {
X			flushpage(columns);
X			end_page();
X		}
X}
X
Xend_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}
X
Xflushpage(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			putchar('\n');
X		} else {
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		}
X		if ( got_dyslexi ) flushrev();
X		putchar('\n');
X	}
X	lin = col = 0;
X}
X
Xfill_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	}
X}
SHAR_EOF
true || echo 'restore of mfold.c failed'
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' &&
XThe documentation follows:
X
XThe 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
XVariables with names 'got_blabla' and 'index_blabla' refers to
Xthe different flags the programs can receive. The program don't
Xuse getopts(3) and have therefore a more rigid option format
Xin the shell. But it's easier to handle errors this way.
X
XThe '-i' and '-D' options are handled as special cases by mfold.
XRemaining options are handled in one separate switch construction.
X
XThe text which is to be folded is read into an internal 'page' buffer.
XThis buffer has the width of one column and the length of all the
Xlines in ALL columns on one page. The buffer is named 'cur_page' and
Xis a global variable.
X
XThe folding is done in a 'while ( 1 )' loop. The first thing done
Xin this loop is a large 'if' statment, about 44 lines long. This
X'if' stament looks if a word is larger than the line length and then
Xcuts the word, saves some charcters in som temporary variables,
X'in_2' and 'in_3'. This construction can handle 2 (two) character
Xlong lines. Then some newline handeling is done, see the '-n'
Xoption in the manual to understand the source easier. A 'if'
Xstatement look if the next charcter is a blank space or not a
Xblank space. If it is a space, the word in 'cur_word' is written to
Xthe output, and if it's not, the character is put into 'cur_word'.
XWhen encountering an EOF, mfold exits the 'while ( 1 )' loop.
X
XThe output is not sent to standard output directly. Instead a
Xfuntion 'putcolu' is used. 'putcolu' writes to the internal
Xpage buffer 'cur_page'. When 'cur_page' is full, the function
X'flush_page' writes 'cur_page to the output. But if the '-d'
Xflag is used the output is written to 'putrev' instead, where
Xthe function 'flushrev' flushes the internal reverse script
Xbuffer, one line. When the '-r' flag is set, the function
X'fill_sp' fills blank space until every line is right justified.
X
XThere is some more help functions, but they are very simple
Xand should be understand without explanation... :-) ?
X
X
XBug reports, ideas, or patches could be sent to;
XPeter Smidt   smidt@cd.chalmers.se    or
XPeter Smidt   smidt@fy.chalmers.se
X
X/Maaniker
SHAR_EOF
true || echo 'restore of mfold_doc failed'
fi
# ============= 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' &&
XThe program mfold is a simpel folding and column making program.
Xunpack the files from the shar file put them in a empty
Xdirectory, type 'make' and you'll get the executable. A manual
Xis included in the shar file.
X
XA bug have been removed and some optimization have been done since the first
Xposting to alt.sources.
X
XA more exact documentation is in mfold_doc.
X
X
XBug reports, ideas, or patches could be sent to;
XPeter Smidt   smidt@cd.chalmers.se    or
XPeter Smidt   smidt@fy.chalmers.se
X
X/Maaniker
SHAR_EOF
true || echo 'restore of README failed'
fi
exit 0