2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)fmt.c 8.1 (Berkeley) 7/20/93";
44 static const char rcsid[] =
57 * fmt -- format the concatenation of input files or standard input
58 * onto standard output. Designed for use with Mail ~|
60 * Syntax : fmt [ goal [ max ] ] [ name ... ]
61 * Authors: Kurt Shoens (UCB) 12/7/78;
62 * Liz Allen (UMCP) 2/24/83 [Addition of goal length concept].
65 /* LIZ@UOM 6/18/85 -- Don't need LENGTH any more.
66 * #define LENGTH 72 Max line length in output
68 #define NOSTR ((char *) 0) /* Null string pointer for lint */
70 /* LIZ@UOM 6/18/85 --New variables goal_length and max_length */
71 #define GOAL_LENGTH 65
73 int goal_length; /* Target or goal line length in output */
74 int max_length; /* Max line length in output */
75 int pfx; /* Current leading blank count */
76 int lineno; /* Current input line */
77 int mark; /* Last place we saw a head line */
80 char *headnames[] = {"To", "Subject", "Cc", 0};
82 void fmt __P((FILE *));
83 int ispref __P((char *, char *));
84 void leadin __P((void));
85 void oflush __P((void));
86 void pack __P((char [], int));
87 void prefix __P((char []));
88 void setout __P((void));
89 void split __P((char []));
90 void tabulate __P((char []));
93 * Drive the whole formatter by managing input files. Also,
94 * cause initialization of the output stuff and flush it out
104 register int errs = 0;
105 int number; /* LIZ@UOM 6/18/85 */
107 (void) setlocale(LC_CTYPE, "");
109 goal_length = GOAL_LENGTH;
110 max_length = MAX_LENGTH;
115 * LIZ@UOM 6/18/85 -- Check for goal and max length arguments
117 if (argc > 1 && !strcmp(argv[1], "-c")) {
122 if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
125 goal_length = number;
126 if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
132 if (max_length <= goal_length)
133 errx(1, "max length must be greater than goal length");
140 if ((fi = fopen(*++argv, "r")) == NULL) {
153 * Read up characters from the passed input file, forming lines,
154 * doing ^H processing, expanding tabs, stripping trailing blanks,
155 * and sending each line down for analysis.
161 static char *linebuf = 0, *canonb = 0;
162 register char *cp, *cp2, cc;
164 #define CHUNKSIZE 1024
165 static int lbufsize = 0, cbufsize = CHUNKSIZE;
167 canonb = malloc(CHUNKSIZE);
172 linebuf = malloc(BUFSIZ);
174 cp = fgets(linebuf, BUFSIZ, fi);
177 while (*cp && isspace(*cp))
179 cp2 = cp + strlen(cp) - 1;
180 while (cp2 > cp && isspace(*cp2))
185 for (c = 0; c < (goal_length-col)/2; c++)
195 * Collect a line, doing ^H processing.
196 * Leave tabs for now.
199 while (c != '\n' && c != EOF) {
200 if (cp - linebuf >= lbufsize) {
201 int offset = cp - linebuf;
202 lbufsize += CHUNKSIZE;
203 linebuf = realloc(linebuf, lbufsize);
206 cp = linebuf + offset;
214 if (!isprint(c) && c != '\t') {
223 * Toss anything remaining on the input line.
225 while (c != '\n' && c != EOF)
237 * Expand tabs on the way to canonb.
242 while ((cc = *cp++)) {
245 if (cp2 - canonb >= cbufsize) {
246 int offset = cp2 - canonb;
247 cbufsize += CHUNKSIZE;
248 canonb = realloc(canonb, cbufsize);
251 cp2 = canonb + offset;
257 if (cp2 - canonb >= cbufsize) {
258 int offset = cp2 - canonb;
259 cbufsize += CHUNKSIZE;
260 canonb = realloc(canonb, cbufsize);
263 cp2 = canonb + offset;
267 } while ((col & 07) != 0);
271 * Swipe trailing blanks from the line.
273 for (cp2--; cp2 >= canonb && *cp2 == ' '; cp2--)
283 * Take a line devoid of tabs and other garbage and determine its
284 * blank prefix. If the indent changes, call for a linebreak.
285 * If the input line is blank, echo the blank line on the output.
286 * Finally, if the line minus the prefix is a mail header, try to keep
287 * it on a line by itself.
293 register char *cp, **hp;
301 for (cp = line; *cp == ' '; cp++)
306 * The following horrible expression attempts to avoid linebreaks
307 * when the indent changes due to a paragraph.
309 if (np != pfx && (np > pfx || abs(pfx-np) > 8))
311 if ((h = ishead(cp)))
312 oflush(), mark = lineno;
313 if (lineno - mark < 3 && lineno - mark > 0)
314 for (hp = &headnames[0]; *hp != (char *) 0; hp++)
315 if (ispref(*hp, cp)) {
320 if (!h && (h = (*cp == '.')))
324 pack(cp, strlen(cp));
332 * Split up the passed line into output "words" which are
333 * maximal strings of non-blanks with the blank separation
334 * attached at the end. Pass these words along to the output
341 register char *cp, *cp2;
343 static int wordsize=0;
344 int wordl; /* LIZ@UOM 6/18/85 */
347 int l = strlen(line);
351 wordsize = (l+66)&~63;
352 word = malloc(wordsize);
361 wordl = 0; /* LIZ@UOM 6/18/85 */
364 * Collect a 'word,' allowing it to contain escaped white
367 while (*cp && *cp != ' ') {
368 if (*cp == '\\' && isspace(cp[1]))
371 wordl++;/* LIZ@UOM 6/18/85 */
375 * Guarantee a space at end of line. Two spaces after end of
376 * sentence punctuation.
380 if (cp != line && index(".:!", cp[-1]))
387 * LIZ@UOM 6/18/85 pack(word);
395 * Build up line images from the words passed in. Prefix
396 * each line with correct number of blanks. The buffer "outbuf"
397 * contains the current partial line image, including prefixed blanks.
398 * "outp" points to the next available space therein. When outp is NOSTR,
399 * there ain't nothing in there yet. At the bottom of this whole mess,
400 * leading tabs are reinserted.
402 char *outbuf; /* Sandbagged output line image */
403 char *outp; /* Pointer in above */
404 int outbuf_size; /* er, size of outbuf */
407 * Initialize the output section.
412 outbuf = malloc(BUFSIZ);
415 outbuf_size = BUFSIZ;
420 * Pack a word onto the output line. If this is the beginning of
421 * the line, push on the appropriately-sized string of blanks first.
422 * If the word won't fit on the current line, flush and begin a new
423 * line. If the word is too long to fit all by itself on a line,
424 * just give it its own and hope for the best.
426 * LIZ@UOM 6/18/85 -- If the new word will fit in at less than the
427 * goal length, take it. If not, then check to see if the line
428 * will be over the max length; if so put the word on the next
429 * line. If not, check to see if the line will be closer to the
430 * goal length with or without the word and take it or put it on
431 * the next line accordingly.
435 * LIZ@UOM 6/18/85 -- pass in the length of the word as well
447 if (((outp==NOSTR) ? wl : outp-outbuf + wl) >= outbuf_size) {
448 char *old_outbuf = outbuf;
450 outbuf = realloc(outbuf, outbuf_size);
453 outp += outbuf-old_outbuf;
459 * LIZ@UOM 6/18/85 -- change condition to check goal_length; s is the
460 * length of the line before the word is added; t is now the length
461 * of the line after the word is added
467 if ((t <= goal_length) ||
468 ((t <= max_length) && (t - goal_length <= goal_length - s))) {
472 for (cp = word; *cp; *outp++ = *cp++);
479 for (cp = word; *cp; *outp++ = *cp++);
483 * If there is anything on the current output line, send it on
484 * its way. Set outp to NOSTR to indicate the absence of the current
498 * Take the passed line buffer, insert leading tabs where possible, and
499 * output on standard output (finally).
509 * Toss trailing blanks in the output line.
511 cp = line + strlen(line) - 1;
512 while (cp >= line && *cp == ' ')
517 * Count the leading blank space and tabulate.
519 for (cp = line; *cp == ' '; cp++)
538 * Initialize the output line with the appropriate number of
547 for (b = 0, cp = outbuf; b < pfx; b++)
553 * Save a string in dynamic space.
554 * This little goodie is needed for
555 * a headline detector in head.c
563 top = malloc(strlen(str) + 1);
565 errx(1, "ran out of memory");
571 * Is s1 a prefix of s2??
575 register char *s1, *s2;
580 return (*s1 == '\0');