version 1.4, 1997/01/26 03:56:52 |
version 1.5, 1997/01/27 04:06:49 |
|
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
|
#ifdef __GNUC__ |
|
#define inline __inline |
|
#else /* !__GNUC__ */ |
|
#define inline |
|
#endif /* !__GNUC__ */ |
|
|
/* |
/* |
* fmt -- format the concatenation of input files or standard input |
* fmt -- format the concatenation of input files or standard input |
* onto standard output. Designed for use with Mail ~| |
* onto standard output. Designed for use with Mail ~| |
|
|
void tabulate __P((char *)); |
void tabulate __P((char *)); |
void leadin __P((void)); |
void leadin __P((void)); |
char *savestr __P((char *)); |
char *savestr __P((char *)); |
|
char *extstr __P((char *, int *, int)); |
int ispref __P((char *, char *)); |
int ispref __P((char *, char *)); |
int ishead __P((char *)); |
int ishead __P((char *)); |
|
|
|
|
fmt(fi) |
fmt(fi) |
FILE *fi; |
FILE *fi; |
{ |
{ |
static char *linebuf = 0, *canonb = 0; |
static char *linebuf, *canonb; |
|
static int lbufsize, cbufsize; |
register char *cp, *cp2, cc; |
register char *cp, *cp2, cc; |
register int c, col; |
register int c, col; |
#define CHUNKSIZE 1024 |
#define CHUNKSIZE 1024 |
static int lbufsize = 0, cbufsize = 0; |
|
|
|
if (center) { |
if (center) { |
if ((linebuf = malloc(BUFSIZ)) == NOSTR) |
register int len; |
errx(1, "Ran out of memory"); |
|
|
linebuf = extstr(linebuf, &lbufsize, NULL); |
for (;;) { |
for (;;) { |
cp = fgets(linebuf, BUFSIZ, fi); |
len = 0; |
if (!cp) |
for (;;) { |
|
if (!fgets(linebuf + len, lbufsize - len, fi)) |
|
break; |
|
len = strlen(linebuf); |
|
if (linebuf[len-1] == '\n' || feof(fi)) |
|
break; |
|
linebuf = extstr(linebuf, &lbufsize, NULL); |
|
} |
|
if (len == 0) |
return; |
return; |
|
cp = linebuf; |
while (*cp && isspace(*cp)) |
while (*cp && isspace(*cp)) |
cp++; |
cp++; |
cp2 = cp + strlen(cp) - 1; |
cp2 = cp + strlen(cp) - 1; |
|
|
while (c != '\n' && c != EOF) { |
while (c != '\n' && c != EOF) { |
if (cp - linebuf >= lbufsize) { |
if (cp - linebuf >= lbufsize) { |
int offset = cp - linebuf; |
int offset = cp - linebuf; |
lbufsize += CHUNKSIZE; |
linebuf = extstr(linebuf, &lbufsize, NULL); |
linebuf = realloc(linebuf, lbufsize); |
|
if(linebuf == 0) |
|
abort(); |
|
cp = linebuf + offset; |
cp = linebuf + offset; |
} |
} |
if (c == '\b') { |
if (c == '\b') { |
|
|
col++; |
col++; |
if (cp2 - canonb >= cbufsize) { |
if (cp2 - canonb >= cbufsize) { |
int offset = cp2 - canonb; |
int offset = cp2 - canonb; |
cbufsize += CHUNKSIZE; |
canonb = extstr(canonb, &cbufsize, NULL); |
canonb = realloc(canonb, cbufsize); |
|
if(canonb == 0) |
|
abort(); |
|
cp2 = canonb + offset; |
cp2 = canonb + offset; |
} |
} |
*cp2++ = cc; |
*cp2++ = cc; |
|
|
do { |
do { |
if (cp2 - canonb >= cbufsize) { |
if (cp2 - canonb >= cbufsize) { |
int offset = cp2 - canonb; |
int offset = cp2 - canonb; |
cbufsize += CHUNKSIZE; |
canonb = extstr(canonb, &cbufsize, NULL); |
canonb = realloc(canonb, cbufsize); |
|
if(canonb == 0) |
|
abort(); |
|
cp2 = canonb + offset; |
cp2 = canonb + offset; |
} |
} |
*cp2++ = ' '; |
*cp2++ = ' '; |
|
|
if ((h = ishead(cp))) |
if ((h = ishead(cp))) |
oflush(), mark = lineno; |
oflush(), mark = lineno; |
if (lineno - mark < 3 && lineno - mark > 0) |
if (lineno - mark < 3 && lineno - mark > 0) |
for (hp = &headnames[0]; *hp != (char *) 0; hp++) |
for (hp = &headnames[0]; *hp != NULL; hp++) |
if (ispref(*hp, cp)) { |
if (ispref(*hp, cp)) { |
h = 1; |
h = 1; |
oflush(); |
oflush(); |
|
|
pfx = np; |
pfx = np; |
if (h) |
if (h) |
pack(cp, strlen(cp)); |
pack(cp, strlen(cp)); |
else split(cp); |
else |
|
split(cp); |
if (h) |
if (h) |
oflush(); |
oflush(); |
lineno++; |
lineno++; |
|
|
char line[]; |
char line[]; |
{ |
{ |
register char *cp, *cp2; |
register char *cp, *cp2; |
char word[BUFSIZ]; |
static char *word; |
|
static int wordsize; |
int wordl; /* LIZ@UOM 6/18/85 */ |
int wordl; /* LIZ@UOM 6/18/85 */ |
|
|
|
if (strlen(line) >= wordsize) |
|
word = extstr(word, &wordsize, strlen(line) + 1); |
|
|
cp = line; |
cp = line; |
while (*cp) { |
while (*cp) { |
cp2 = word; |
cp2 = word; |
|
|
* there ain't nothing in there yet. At the bottom of this whole mess, |
* there ain't nothing in there yet. At the bottom of this whole mess, |
* leading tabs are reinserted. |
* leading tabs are reinserted. |
*/ |
*/ |
char outbuf[BUFSIZ]; /* Sandbagged output line image */ |
static char *outbuf; /* Sandbagged output line image */ |
char *outp; /* Pointer in above */ |
static int obufsize; /* Size of outbuf */ |
|
static char *outp; /* Pointer in above */ |
|
|
/* |
/* |
* Initialize the output section. |
* Initialize the output section. |
|
|
*/ |
*/ |
s = outp - outbuf; |
s = outp - outbuf; |
t = wl + s; |
t = wl + s; |
|
if (t + 1 > obufsize) { |
|
outbuf = extstr(outbuf, &obufsize, t + 1); |
|
outp = outbuf + s; |
|
} |
if ((t <= goal_length) || |
if ((t <= goal_length) || |
((t <= max_length) && (t - goal_length <= goal_length - s))) { |
((t <= max_length) && (t - goal_length <= goal_length - s))) { |
/* |
/* |
* In like flint! |
* In like flint! |
*/ |
*/ |
for (cp = word; *cp; *outp++ = *cp++); |
for (cp = word; *cp; *outp++ = *cp++) |
|
; |
return; |
return; |
} |
} |
if (s > pfx) { |
if (s > pfx) { |
oflush(); |
oflush(); |
leadin(); |
leadin(); |
} |
} |
for (cp = word; *cp; *outp++ = *cp++); |
for (cp = word; *cp; *outp++ = *cp++) |
|
; |
} |
} |
|
|
/* |
/* |
|
|
register int b; |
register int b; |
register char *cp; |
register char *cp; |
|
|
|
if (obufsize == 0 || (outp != NULL && outp - outbuf <= pfx)) |
|
outbuf = extstr(outbuf, &obufsize, pfx); |
for (b = 0, cp = outbuf; b < pfx; b++) |
for (b = 0, cp = outbuf; b < pfx; b++) |
*cp++ = ' '; |
*cp++ = ' '; |
outp = cp; |
outp = cp; |
|
|
while (*s1++ == *s2) |
while (*s1++ == *s2) |
; |
; |
return (*s1 == '\0'); |
return (*s1 == '\0'); |
|
} |
|
|
|
inline char * |
|
extstr(str, size, gsize) |
|
char *str; |
|
int *size; |
|
int gsize; |
|
{ |
|
do { |
|
*size += CHUNKSIZE; |
|
} while (gsize && *size < gsize); |
|
|
|
if ((str = realloc(str, *size)) == NULL) |
|
errx(1, "Ran out of memory"); |
|
|
|
return(str); |
} |
} |