X-Git-Url: https://git.subgeniuskitty.com/unix-history/.git/blobdiff_plain/79e55e872150ac0c3fcf508355efbac03b7d46f4..470c33f3448aad96e77401f41b39bd0b4c648a55:/usr/src/usr.bin/fmt/fmt.c diff --git a/usr/src/usr.bin/fmt/fmt.c b/usr/src/usr.bin/fmt/fmt.c index 6fe340aa23..cab46b4bc7 100644 --- a/usr/src/usr.bin/fmt/fmt.c +++ b/usr/src/usr.bin/fmt/fmt.c @@ -1,4 +1,29 @@ -# +/* + * Copyright (c) 1980 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +#ifndef lint +char copyright[] = +"@(#) Copyright (c) 1980 Regents of the University of California.\n\ + All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static char sccsid[] = "@(#)fmt.c 5.8 (Berkeley) %G%"; +#endif /* not lint */ #include #include @@ -7,20 +32,26 @@ * fmt -- format the concatenation of input files or standard input * onto standard output. Designed for use with Mail ~| * - * Syntax: fmt [ name ... ] - * Author: Kurt Shoens (UCB) 12/7/78 + * Syntax : fmt [ goal [ max ] ] [ name ... ] + * Authors: Kurt Shoens (UCB) 12/7/78; + * Liz Allen (UMCP) 2/24/83 [Addition of goal length concept]. */ -static char *SccsId = "@(#)fmt.c 1.1 %G%"; - -#define LENGTH 72 /* Max line length in output */ +/* LIZ@UOM 6/18/85 -- Don't need LENGTH any more. + * #define LENGTH 72 Max line length in output + */ #define NOSTR ((char *) 0) /* Null string pointer for lint */ +/* LIZ@UOM 6/18/85 --New variables goal_length and max_length */ +#define GOAL_LENGTH 65 +#define MAX_LENGTH 75 +int goal_length; /* Target or goal line length in output */ +int max_length; /* Max line length in output */ int pfx; /* Current leading blank count */ int lineno; /* Current input line */ int mark; /* Last place we saw a head line */ -char *calloc(); /* for lint . . . */ +char *malloc(); /* for lint . . . */ char *headnames[] = {"To", "Subject", "Cc", 0}; /* @@ -30,17 +61,37 @@ char *headnames[] = {"To", "Subject", "Cc", 0}; */ main(argc, argv) + int argc; char **argv; { register FILE *fi; register int errs = 0; + int number; /* LIZ@UOM 6/18/85 */ + goal_length = GOAL_LENGTH; + max_length = MAX_LENGTH; setout(); lineno = 1; mark = -10; - setbuf(stdout, calloc(1, BUFSIZ)); + /* + * LIZ@UOM 6/18/85 -- Check for goal and max length arguments + */ + if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) { + argv++; + argc--; + goal_length = number; + if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) { + argv++; + argc--; + max_length = number; + } + } + if (max_length <= goal_length) { + fprintf(stderr, "Max length must be greater than %s\n", + "goal length"); + exit(1); + } if (argc < 2) { - setbuf(stdin, calloc(1, BUFSIZ)); fmt(stdin); oflush(); exit(0); @@ -63,7 +114,6 @@ main(argc, argv) * doing ^H processing, expanding tabs, stripping trailing blanks, * and sending each line down for analysis. */ - fmt(fi) FILE *fi; { @@ -73,12 +123,10 @@ fmt(fi) c = getc(fi); while (c != EOF) { - /* * Collect a line, doing ^H processing. * Leave tabs for now. */ - cp = linebuf; while (c != '\n' && c != EOF && cp-linebuf < BUFSIZ-1) { if (c == '\b') { @@ -99,14 +147,12 @@ fmt(fi) /* * Toss anything remaining on the input line. */ - while (c != '\n' && c != EOF) c = getc(fi); /* * Expand tabs on the way to canonb. */ - col = 0; cp = linebuf; cp2 = canonb; @@ -127,7 +173,6 @@ fmt(fi) /* * Swipe trailing blanks from the line. */ - for (cp2--; cp2 >= canonb && *cp2 == ' '; cp2--) ; *++cp2 = '\0'; @@ -144,7 +189,6 @@ fmt(fi) * Finally, if the line minus the prefix is a mail header, try to keep * it on a line by itself. */ - prefix(line) char line[]; { @@ -164,7 +208,6 @@ prefix(line) * The following horrible expression attempts to avoid linebreaks * when the indent changes due to a paragraph. */ - if (np != pfx && (np > pfx || abs(pfx-np) > 8)) oflush(); if (h = ishead(cp)) @@ -191,42 +234,45 @@ prefix(line) * attached at the end. Pass these words along to the output * line packer. */ - split(line) char line[]; { register char *cp, *cp2; char word[BUFSIZ]; + int wordl; /* LIZ@UOM 6/18/85 */ cp = line; while (*cp) { cp2 = word; + wordl = 0; /* LIZ@UOM 6/18/85 */ /* - * Collect a 'word,' allowing it to contain escaped - * white space. + * Collect a 'word,' allowing it to contain escaped white + * space. */ - while (*cp && *cp != ' ') { if (*cp == '\\' && isspace(cp[1])) *cp2++ = *cp++; *cp2++ = *cp++; + wordl++;/* LIZ@UOM 6/18/85 */ } /* - * Guarantee a space at end of line. - * Two spaces after end of sentence punctuation. + * Guarantee a space at end of line. Two spaces after end of + * sentence punctuation. */ - if (*cp == '\0') { *cp2++ = ' '; - if (any(cp[-1], ".:!")) + if (index(".:!", cp[-1])) *cp2++ = ' '; } while (*cp == ' ') *cp2++ = *cp++; *cp2 = '\0'; - pack(word); + /* + * LIZ@UOM 6/18/85 pack(word); + */ + pack(word, wordl); } } @@ -239,14 +285,12 @@ split(line) * there ain't nothing in there yet. At the bottom of this whole mess, * leading tabs are reinserted. */ - char outbuf[BUFSIZ]; /* Sandbagged output line image */ char *outp; /* Pointer in above */ /* * Initialize the output section. */ - setout() { outp = NOSTR; @@ -258,34 +302,51 @@ setout() * 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. */ -pack(word) +/* + * LIZ@UOM 6/18/85 -- pass in the length of the word as well + * pack(word) + * char word[]; + */ +pack(word,wl) char word[]; + int wl; { register char *cp; register int s, t; if (outp == NOSTR) leadin(); - t = strlen(word); - s = outp-outbuf; - if (t+s <= LENGTH) { - + /* + * 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 + * t = strlen(word); + * if (t+s <= LENGTH) + */ + s = outp - outbuf; + t = wl + s; + if ((t <= goal_length) || + ((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; } if (s > pfx) { oflush(); leadin(); } - for (cp = word; *cp; *outp++ = *cp++) - ; + for (cp = word; *cp; *outp++ = *cp++); } /* @@ -293,7 +354,6 @@ pack(word) * its way. Set outp to NOSTR to indicate the absence of the current * line prefix. */ - oflush() { if (outp == NOSTR) @@ -307,17 +367,15 @@ oflush() * Take the passed line buffer, insert leading tabs where possible, and * output on standard output (finally). */ - tabulate(line) char line[]; { - register char *cp, *cp2; + register char *cp; register int b, t; /* * Toss trailing blanks in the output line. */ - cp = line + strlen(line) - 1; while (cp >= line && *cp == ' ') cp--; @@ -326,7 +384,6 @@ tabulate(line) /* * Count the leading blank space and tabulate. */ - for (cp = line; *cp == ' '; cp++) ; b = cp-line; @@ -349,7 +406,6 @@ tabulate(line) * Initialize the output line with the appropriate number of * leading blanks. */ - leadin() { register int b; @@ -365,31 +421,29 @@ leadin() * This little goodie is needed for * a headline detector in head.c */ - char * savestr(str) char str[]; { register char *top; - top = calloc(strlen(str) + 1, 1); + top = malloc(strlen(str) + 1); if (top == NOSTR) { fprintf(stderr, "fmt: Ran out of memory\n"); exit(1); } - copy(str, top); - return(top); + strcpy(top, str); + return (top); } /* * Is s1 a prefix of s2?? */ - ispref(s1, s2) register char *s1, *s2; { while (*s1++ == *s2) ; - return(*s1 == '\0'); + return (*s1 == '\0'); }