2017-10-13 03:11:56 +03:00
|
|
|
/* $NetBSD: fmt.c,v 1.33 2017/10/13 00:11:56 christos Exp $ */
|
1995-09-01 05:29:39 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1995-09-01 05:29:39 +04:00
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-18 19:00:11 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
2008-07-21 18:19:20 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-09-01 05:29:39 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)fmt.c 8.1 (Berkeley) 7/20/93";
|
|
|
|
#endif
|
2017-10-13 03:11:56 +03:00
|
|
|
__RCSID("$NetBSD: fmt.c,v 1.33 2017/10/13 00:11:56 christos Exp $");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2017-10-13 03:11:56 +03:00
|
|
|
#include <wctype.h>
|
2002-03-02 16:55:13 +03:00
|
|
|
#include <locale.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1994-12-24 19:35:17 +03:00
|
|
|
#include <stdlib.h>
|
2007-05-29 19:27:37 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <limits.h>
|
1994-12-24 19:35:17 +03:00
|
|
|
#include <string.h>
|
2017-10-13 03:11:56 +03:00
|
|
|
#include <locale.h>
|
2005-12-16 00:32:00 +03:00
|
|
|
#include "buffer.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* fmt -- format the concatenation of input files or standard input
|
|
|
|
* onto standard output. Designed for use with Mail ~|
|
|
|
|
*
|
|
|
|
* Syntax : fmt [ goal [ max ] ] [ name ... ]
|
|
|
|
* Authors: Kurt Shoens (UCB) 12/7/78;
|
|
|
|
* Liz Allen (UMCP) 2/24/83 [Addition of goal length concept].
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* LIZ@UOM 6/18/85 --New variables goal_length and max_length */
|
|
|
|
#define GOAL_LENGTH 65
|
|
|
|
#define MAX_LENGTH 75
|
2005-12-16 00:32:00 +03:00
|
|
|
static size_t goal_length; /* Target or goal line length in output */
|
|
|
|
static size_t max_length; /* Max line length in output */
|
|
|
|
static size_t pfx; /* Current leading blank count */
|
2007-05-29 19:27:37 +04:00
|
|
|
static int raw; /* Don't treat mail specially */
|
2006-01-15 17:26:10 +03:00
|
|
|
static int lineno; /* Current input line */
|
|
|
|
static int mark; /* Last place we saw a head line */
|
2005-12-16 00:32:00 +03:00
|
|
|
static int center;
|
|
|
|
static struct buffer outbuf;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2017-10-13 03:11:56 +03:00
|
|
|
static const wchar_t *headnames[] = { L"To", L"Subject", L"Cc", NULL };
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2007-12-15 22:44:37 +03:00
|
|
|
static void usage(void) __dead;
|
2007-05-29 19:27:37 +04:00
|
|
|
static int getnum(const char *, const char *, size_t *, int);
|
2002-03-02 16:55:13 +03:00
|
|
|
static void fmt(FILE *);
|
2017-10-13 03:11:56 +03:00
|
|
|
static int ispref(const wchar_t *, const wchar_t *);
|
2002-03-02 16:55:13 +03:00
|
|
|
static void leadin(void);
|
|
|
|
static void oflush(void);
|
2017-10-13 03:11:56 +03:00
|
|
|
static void pack(const wchar_t *, size_t);
|
2005-12-16 00:32:00 +03:00
|
|
|
static void prefix(const struct buffer *, int);
|
2017-10-13 03:11:56 +03:00
|
|
|
static void split(const wchar_t *, int);
|
2005-12-16 00:32:00 +03:00
|
|
|
static void tabulate(struct buffer *);
|
1999-11-02 15:50:04 +03:00
|
|
|
|
2005-12-16 00:32:00 +03:00
|
|
|
|
2017-10-13 03:11:56 +03:00
|
|
|
int ishead(const wchar_t *);
|
1997-10-18 19:00:11 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Drive the whole formatter by managing input files. Also,
|
|
|
|
* cause initialization of the output stuff and flush it out
|
|
|
|
* at the end.
|
|
|
|
*/
|
|
|
|
|
1997-10-18 19:00:11 +04:00
|
|
|
int
|
2002-03-02 16:55:13 +03:00
|
|
|
main(int argc, char **argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-18 19:00:11 +04:00
|
|
|
FILE *fi;
|
|
|
|
int errs = 0;
|
2007-05-29 19:27:37 +04:00
|
|
|
int compat = 1;
|
|
|
|
int c;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
goal_length = GOAL_LENGTH;
|
|
|
|
max_length = MAX_LENGTH;
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_init(&outbuf);
|
1993-03-21 12:45:37 +03:00
|
|
|
lineno = 1;
|
2006-01-15 17:26:10 +03:00
|
|
|
mark = -10;
|
1997-05-31 19:13:49 +04:00
|
|
|
|
2005-12-16 00:32:00 +03:00
|
|
|
setprogname(*argv);
|
2006-01-05 05:07:29 +03:00
|
|
|
(void)setlocale(LC_ALL, "");
|
1997-05-31 19:13:49 +04:00
|
|
|
|
2012-07-01 01:31:15 +04:00
|
|
|
while ((c = getopt(argc, argv, "Cg:m:rw:")) != -1)
|
2007-05-29 19:27:37 +04:00
|
|
|
switch (c) {
|
|
|
|
case 'C':
|
|
|
|
center++;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
(void)getnum(optarg, "goal", &goal_length, 1);
|
|
|
|
compat = 0;
|
|
|
|
break;
|
|
|
|
case 'm':
|
2012-07-01 01:31:15 +04:00
|
|
|
case 'w':
|
2007-05-29 19:27:37 +04:00
|
|
|
(void)getnum(optarg, "max", &max_length, 1);
|
|
|
|
compat = 0;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
raw++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
2007-05-29 19:27:37 +04:00
|
|
|
* compatibility with old usage.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
2007-06-04 02:39:21 +04:00
|
|
|
if (compat && argc > 0 && getnum(*argv, "goal", &goal_length, 0)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
argv++;
|
|
|
|
argc--;
|
2007-06-04 02:39:21 +04:00
|
|
|
if (argc > 0 && getnum(*argv, "max", &max_length, 0)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
}
|
2007-05-29 19:27:37 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (max_length <= goal_length) {
|
2005-12-16 00:32:00 +03:00
|
|
|
errx(1, "Max length (%zu) must be greater than goal "
|
|
|
|
"length (%zu)", max_length, goal_length);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2007-05-29 19:27:37 +04:00
|
|
|
if (argc == 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
fmt(stdin);
|
|
|
|
oflush();
|
2005-12-16 00:32:00 +03:00
|
|
|
return 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2008-03-09 20:26:37 +03:00
|
|
|
for (;argc; argc--, argv++) {
|
|
|
|
if ((fi = fopen(*argv, "r")) == NULL) {
|
2005-12-16 00:32:00 +03:00
|
|
|
warn("Cannot open `%s'", *argv);
|
1993-03-21 12:45:37 +03:00
|
|
|
errs++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fmt(fi);
|
2006-01-05 05:07:29 +03:00
|
|
|
(void)fclose(fi);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
oflush();
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_end(&outbuf);
|
|
|
|
return errs;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:27:37 +04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
2012-07-01 01:31:15 +04:00
|
|
|
"Usage: %s [-Cr] [-g <goal>] [-m|w <max>] [<files>..]\n"
|
2007-05-29 19:27:37 +04:00
|
|
|
"\t %s [-Cr] [<goal>] [<max>] [<files>]\n",
|
|
|
|
getprogname(), getprogname());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getnum(const char *str, const char *what, size_t *res, int badnum)
|
|
|
|
{
|
|
|
|
unsigned long ul;
|
|
|
|
char *ep;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ul = strtoul(str, &ep, 0);
|
|
|
|
if (*str != '\0' && *ep == '\0') {
|
|
|
|
if ((errno == ERANGE && ul == ULONG_MAX) || ul > SIZE_T_MAX)
|
|
|
|
errx(1, "%s number `%s' too big", what, str);
|
|
|
|
*res = (size_t)ul;
|
|
|
|
return 1;
|
|
|
|
} else if (badnum)
|
|
|
|
errx(1, "Bad %s number `%s'", what, str);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Read up characters from the passed input file, forming lines,
|
|
|
|
* doing ^H processing, expanding tabs, stripping trailing blanks,
|
|
|
|
* and sending each line down for analysis.
|
|
|
|
*/
|
1999-11-02 15:50:04 +03:00
|
|
|
static void
|
2002-03-02 16:55:13 +03:00
|
|
|
fmt(FILE *fi)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-12-16 00:32:00 +03:00
|
|
|
struct buffer lbuf, cbuf;
|
2017-10-13 03:11:56 +03:00
|
|
|
wchar_t *cp, *cp2;
|
|
|
|
wint_t c;
|
|
|
|
int add_space;
|
2008-04-13 07:46:30 +04:00
|
|
|
size_t len, col, i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2000-09-15 15:23:17 +04:00
|
|
|
if (center) {
|
2005-12-16 00:32:00 +03:00
|
|
|
for (;;) {
|
2017-10-13 03:11:56 +03:00
|
|
|
cp = fgetwln(fi, &len);
|
2000-09-15 15:23:17 +04:00
|
|
|
if (!cp)
|
|
|
|
return;
|
2008-04-13 07:46:30 +04:00
|
|
|
|
|
|
|
/* skip over leading space */
|
|
|
|
while (len > 0) {
|
2017-10-13 03:11:56 +03:00
|
|
|
if (!iswspace(*cp))
|
2008-04-13 07:46:30 +04:00
|
|
|
break;
|
2000-09-15 15:23:17 +04:00
|
|
|
cp++;
|
2008-04-13 07:46:30 +04:00
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear trailing space */
|
|
|
|
while (len > 0) {
|
2017-10-13 03:11:56 +03:00
|
|
|
if (!iswspace((unsigned char)cp[len-1]))
|
2008-04-13 07:46:30 +04:00
|
|
|
break;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
/* blank line */
|
2017-10-13 03:11:56 +03:00
|
|
|
(void)putwchar(L'\n');
|
2008-04-13 07:46:30 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (goal_length > len) {
|
|
|
|
for (i = 0; i < (goal_length - len) / 2; i++) {
|
2017-10-13 03:11:56 +03:00
|
|
|
(void)putwchar(L' ');
|
2008-04-13 07:46:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < len; i++) {
|
2017-10-13 03:11:56 +03:00
|
|
|
(void)putwchar(cp[i]);
|
2008-04-13 07:46:30 +04:00
|
|
|
}
|
2017-10-13 03:11:56 +03:00
|
|
|
(void)putwchar(L'\n');
|
2000-09-15 15:23:17 +04:00
|
|
|
}
|
|
|
|
}
|
2005-12-16 00:32:00 +03:00
|
|
|
|
|
|
|
buf_init(&lbuf);
|
|
|
|
buf_init(&cbuf);
|
2017-10-13 03:11:56 +03:00
|
|
|
c = getwc(fi);
|
2005-12-16 00:32:00 +03:00
|
|
|
|
2017-10-13 03:11:56 +03:00
|
|
|
while (c != WEOF) {
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Collect a line, doing ^H processing.
|
|
|
|
* Leave tabs for now.
|
|
|
|
*/
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_reset(&lbuf);
|
2017-10-13 03:11:56 +03:00
|
|
|
while (c != '\n' && c != WEOF) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (c == '\b') {
|
2006-01-05 05:07:29 +03:00
|
|
|
(void)buf_unputc(&lbuf);
|
2017-10-13 03:11:56 +03:00
|
|
|
c = getwc(fi);
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
2017-10-13 03:11:56 +03:00
|
|
|
if(!(iswprint(c) || c == '\t' || c >= 160)) {
|
|
|
|
c = getwc(fi);
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_putc(&lbuf, c);
|
2017-10-13 03:11:56 +03:00
|
|
|
c = getwc(fi);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_putc(&lbuf, '\0');
|
2006-01-05 05:07:29 +03:00
|
|
|
(void)buf_unputc(&lbuf);
|
2017-10-13 03:11:56 +03:00
|
|
|
add_space = c != WEOF;
|
1999-11-02 15:50:04 +03:00
|
|
|
|
|
|
|
/*
|
2005-12-16 00:32:00 +03:00
|
|
|
* Expand tabs on the way.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
col = 0;
|
2005-12-16 00:32:00 +03:00
|
|
|
cp = lbuf.bptr;
|
|
|
|
buf_reset(&cbuf);
|
|
|
|
while ((c = *cp++) != '\0') {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (c != '\t') {
|
|
|
|
col++;
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_putc(&cbuf, c);
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
do {
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_putc(&cbuf, ' ');
|
1993-03-21 12:45:37 +03:00
|
|
|
col++;
|
|
|
|
} while ((col & 07) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Swipe trailing blanks from the line.
|
|
|
|
*/
|
2005-12-16 00:32:00 +03:00
|
|
|
for (cp2 = cbuf.ptr - 1; cp2 >= cbuf.bptr && *cp2 == ' '; cp2--)
|
|
|
|
continue;
|
|
|
|
cbuf.ptr = cp2 + 1;
|
|
|
|
buf_putc(&cbuf, '\0');
|
2006-01-05 05:07:29 +03:00
|
|
|
(void)buf_unputc(&cbuf);
|
2005-12-16 00:32:00 +03:00
|
|
|
prefix(&cbuf, add_space);
|
2017-10-13 03:11:56 +03:00
|
|
|
if (c != WEOF)
|
|
|
|
c = getwc(fi);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_end(&cbuf);
|
|
|
|
buf_end(&lbuf);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a line devoid of tabs and other garbage and determine its
|
|
|
|
* blank prefix. If the indent changes, call for a linebreak.
|
|
|
|
* If the input line is blank, echo the blank line on the output.
|
|
|
|
* Finally, if the line minus the prefix is a mail header, try to keep
|
|
|
|
* it on a line by itself.
|
|
|
|
*/
|
1999-11-02 15:50:04 +03:00
|
|
|
static void
|
2005-12-16 00:32:00 +03:00
|
|
|
prefix(const struct buffer *buf, int add_space)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2017-10-13 03:11:56 +03:00
|
|
|
const wchar_t *cp;
|
|
|
|
const wchar_t **hp;
|
2005-12-16 00:32:00 +03:00
|
|
|
size_t np;
|
|
|
|
int h;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-12-16 00:32:00 +03:00
|
|
|
if (buf->ptr == buf->bptr) {
|
1993-03-21 12:45:37 +03:00
|
|
|
oflush();
|
2017-10-13 03:11:56 +03:00
|
|
|
(void)putwchar(L'\n');
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
2005-12-16 00:32:00 +03:00
|
|
|
for (cp = buf->bptr; *cp == ' '; cp++)
|
|
|
|
continue;
|
|
|
|
np = cp - buf->bptr;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The following horrible expression attempts to avoid linebreaks
|
|
|
|
* when the indent changes due to a paragraph.
|
|
|
|
*/
|
2005-12-16 00:32:00 +03:00
|
|
|
if (np != pfx && (np > pfx || abs((int)(pfx - np)) > 8))
|
|
|
|
oflush();
|
2007-05-29 19:27:37 +04:00
|
|
|
if (!raw) {
|
|
|
|
if ((h = ishead(cp)) != 0) {
|
|
|
|
oflush();
|
|
|
|
mark = lineno;
|
|
|
|
}
|
|
|
|
if (lineno - mark < 3 && lineno - mark > 0)
|
|
|
|
for (hp = &headnames[0]; *hp != NULL; hp++)
|
|
|
|
if (ispref(*hp, cp)) {
|
|
|
|
h = 1;
|
|
|
|
oflush();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!h && (h = (*cp == '.')))
|
|
|
|
oflush();
|
|
|
|
} else
|
|
|
|
h = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
pfx = np;
|
1999-11-02 15:50:04 +03:00
|
|
|
if (h) {
|
2005-12-16 00:32:00 +03:00
|
|
|
pack(cp, (size_t)(buf->ptr - cp));
|
1993-03-21 12:45:37 +03:00
|
|
|
oflush();
|
1999-11-02 15:50:04 +03:00
|
|
|
} else
|
|
|
|
split(cp, add_space);
|
1993-03-21 12:45:37 +03:00
|
|
|
lineno++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Split up the passed line into output "words" which are
|
|
|
|
* maximal strings of non-blanks with the blank separation
|
|
|
|
* attached at the end. Pass these words along to the output
|
|
|
|
* line packer.
|
|
|
|
*/
|
1999-11-02 15:50:04 +03:00
|
|
|
static void
|
2017-10-13 03:11:56 +03:00
|
|
|
split(const wchar_t line[], int add_space)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2017-10-13 03:11:56 +03:00
|
|
|
const wchar_t *cp;
|
2005-12-16 00:32:00 +03:00
|
|
|
struct buffer word;
|
|
|
|
size_t wlen;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_init(&word);
|
1993-03-21 12:45:37 +03:00
|
|
|
cp = line;
|
|
|
|
while (*cp) {
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_reset(&word);
|
|
|
|
wlen = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Collect a 'word,' allowing it to contain escaped white
|
|
|
|
* space.
|
|
|
|
*/
|
|
|
|
while (*cp && *cp != ' ') {
|
2017-10-13 03:11:56 +03:00
|
|
|
if (*cp == '\\' && iswspace(cp[1]))
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_putc(&word, *cp++);
|
|
|
|
buf_putc(&word, *cp++);
|
|
|
|
wlen++;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Guarantee a space at end of line. Two spaces after end of
|
|
|
|
* sentence punctuation.
|
|
|
|
*/
|
1999-11-02 15:50:04 +03:00
|
|
|
if (*cp == '\0' && add_space) {
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_putc(&word, ' ');
|
1997-10-18 19:01:05 +04:00
|
|
|
if (strchr(".:!", cp[-1]))
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_putc(&word, ' ');
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
while (*cp == ' ')
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_putc(&word, *cp++);
|
|
|
|
|
|
|
|
buf_putc(&word, '\0');
|
2006-01-05 05:07:29 +03:00
|
|
|
(void)buf_unputc(&word);
|
2005-12-16 00:32:00 +03:00
|
|
|
|
|
|
|
pack(word.bptr, wlen);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_end(&word);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output section.
|
|
|
|
* Build up line images from the words passed in. Prefix
|
2005-12-16 01:37:37 +03:00
|
|
|
* each line with correct number of blanks.
|
|
|
|
*
|
|
|
|
* At the bottom of this whole mess, leading tabs are reinserted.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pack a word onto the output line. If this is the beginning of
|
|
|
|
* the line, push on the appropriately-sized string of blanks first.
|
|
|
|
* If the word won't fit on the current line, flush and begin a new
|
|
|
|
* line. If the word is too long to fit all by itself on a line,
|
|
|
|
* just give it its own and hope for the best.
|
|
|
|
*
|
|
|
|
* LIZ@UOM 6/18/85 -- If the new word will fit in at less than the
|
|
|
|
* goal length, take it. If not, then check to see if the line
|
|
|
|
* will be over the max length; if so put the word on the next
|
|
|
|
* line. If not, check to see if the line will be closer to the
|
|
|
|
* goal length with or without the word and take it or put it on
|
|
|
|
* the next line accordingly.
|
|
|
|
*/
|
|
|
|
|
1999-11-02 15:50:04 +03:00
|
|
|
static void
|
2017-10-13 03:11:56 +03:00
|
|
|
pack(const wchar_t *word, size_t wlen)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2017-10-13 03:11:56 +03:00
|
|
|
const wchar_t *cp;
|
2005-12-16 00:32:00 +03:00
|
|
|
size_t s, t;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-12-16 00:32:00 +03:00
|
|
|
if (outbuf.bptr == outbuf.ptr)
|
1993-03-21 12:45:37 +03:00
|
|
|
leadin();
|
|
|
|
/*
|
|
|
|
* LIZ@UOM 6/18/85 -- change condition to check goal_length; s is the
|
|
|
|
* length of the line before the word is added; t is now the length
|
|
|
|
* of the line after the word is added
|
|
|
|
*/
|
2005-12-16 00:32:00 +03:00
|
|
|
s = outbuf.ptr - outbuf.bptr;
|
|
|
|
t = wlen + s;
|
2006-01-04 23:44:57 +03:00
|
|
|
if ((t <= goal_length) || ((t <= max_length) &&
|
|
|
|
(s <= goal_length) && (t - goal_length <= goal_length - s))) {
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* In like flint!
|
|
|
|
*/
|
2005-12-16 00:32:00 +03:00
|
|
|
for (cp = word; *cp;)
|
|
|
|
buf_putc(&outbuf, *cp++);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (s > pfx) {
|
|
|
|
oflush();
|
|
|
|
leadin();
|
|
|
|
}
|
2005-12-16 00:32:00 +03:00
|
|
|
for (cp = word; *cp;)
|
|
|
|
buf_putc(&outbuf, *cp++);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is anything on the current output line, send it on
|
2005-12-16 01:37:37 +03:00
|
|
|
* its way. Reset outbuf.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1999-11-02 15:50:04 +03:00
|
|
|
static void
|
2002-03-02 16:55:13 +03:00
|
|
|
oflush(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-12-16 00:32:00 +03:00
|
|
|
if (outbuf.bptr == outbuf.ptr)
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
2005-12-16 00:32:00 +03:00
|
|
|
buf_putc(&outbuf, '\0');
|
2006-01-05 05:07:29 +03:00
|
|
|
(void)buf_unputc(&outbuf);
|
2005-12-16 00:32:00 +03:00
|
|
|
tabulate(&outbuf);
|
|
|
|
buf_reset(&outbuf);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take the passed line buffer, insert leading tabs where possible, and
|
|
|
|
* output on standard output (finally).
|
|
|
|
*/
|
1999-11-02 15:50:04 +03:00
|
|
|
static void
|
2005-12-16 00:32:00 +03:00
|
|
|
tabulate(struct buffer *buf)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2017-10-13 03:11:56 +03:00
|
|
|
wchar_t *cp;
|
2005-12-16 00:32:00 +03:00
|
|
|
size_t b, t;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Toss trailing blanks in the output line.
|
|
|
|
*/
|
2006-01-15 17:41:45 +03:00
|
|
|
for (cp = buf->ptr - 1; cp >= buf->bptr && *cp == ' '; cp--)
|
2005-12-16 00:32:00 +03:00
|
|
|
continue;
|
2006-01-15 17:41:45 +03:00
|
|
|
*++cp = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Count the leading blank space and tabulate.
|
|
|
|
*/
|
2005-12-16 00:32:00 +03:00
|
|
|
for (cp = buf->bptr; *cp == ' '; cp++)
|
|
|
|
continue;
|
|
|
|
b = cp - buf->bptr;
|
2005-12-16 01:37:37 +03:00
|
|
|
t = b / 8;
|
|
|
|
b = b % 8;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (t > 0)
|
|
|
|
do
|
2017-10-13 03:11:56 +03:00
|
|
|
(void)putwchar(L'\t');
|
1993-03-21 12:45:37 +03:00
|
|
|
while (--t);
|
|
|
|
if (b > 0)
|
|
|
|
do
|
2017-10-13 03:11:56 +03:00
|
|
|
(void)putwchar(L' ');
|
1993-03-21 12:45:37 +03:00
|
|
|
while (--b);
|
|
|
|
while (*cp)
|
2017-10-13 03:11:56 +03:00
|
|
|
(void)putwchar(*cp++);
|
|
|
|
(void)putwchar(L'\n');
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the output line with the appropriate number of
|
|
|
|
* leading blanks.
|
|
|
|
*/
|
1999-11-02 15:50:04 +03:00
|
|
|
static void
|
2002-03-02 16:55:13 +03:00
|
|
|
leadin(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-12-16 00:32:00 +03:00
|
|
|
size_t b;
|
|
|
|
|
|
|
|
buf_reset(&outbuf);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-12-16 00:32:00 +03:00
|
|
|
for (b = 0; b < pfx; b++)
|
|
|
|
buf_putc(&outbuf, ' ');
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is s1 a prefix of s2??
|
|
|
|
*/
|
1999-11-02 15:50:04 +03:00
|
|
|
static int
|
2017-10-13 03:11:56 +03:00
|
|
|
ispref(const wchar_t *s1, const wchar_t *s2)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
while (*s1++ == *s2)
|
2005-12-16 00:32:00 +03:00
|
|
|
continue;
|
|
|
|
return *s1 == '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|