fmt.c revision 1.2 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1980 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1980 Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.2 mycroft /*static char sccsid[] = "from: @(#)fmt.c 5.10 (Berkeley) 6/1/90";*/
42 1.2 mycroft static char rcsid[] = "$Id: fmt.c,v 1.2 1993/08/01 18:15:53 mycroft Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <stdio.h>
46 1.1 cgd #include <ctype.h>
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * fmt -- format the concatenation of input files or standard input
50 1.1 cgd * onto standard output. Designed for use with Mail ~|
51 1.1 cgd *
52 1.1 cgd * Syntax : fmt [ goal [ max ] ] [ name ... ]
53 1.1 cgd * Authors: Kurt Shoens (UCB) 12/7/78;
54 1.1 cgd * Liz Allen (UMCP) 2/24/83 [Addition of goal length concept].
55 1.1 cgd */
56 1.1 cgd
57 1.1 cgd /* LIZ@UOM 6/18/85 -- Don't need LENGTH any more.
58 1.1 cgd * #define LENGTH 72 Max line length in output
59 1.1 cgd */
60 1.1 cgd #define NOSTR ((char *) 0) /* Null string pointer for lint */
61 1.1 cgd
62 1.1 cgd /* LIZ@UOM 6/18/85 --New variables goal_length and max_length */
63 1.1 cgd #define GOAL_LENGTH 65
64 1.1 cgd #define MAX_LENGTH 75
65 1.1 cgd int goal_length; /* Target or goal line length in output */
66 1.1 cgd int max_length; /* Max line length in output */
67 1.1 cgd int pfx; /* Current leading blank count */
68 1.1 cgd int lineno; /* Current input line */
69 1.1 cgd int mark; /* Last place we saw a head line */
70 1.1 cgd
71 1.1 cgd char *malloc(); /* for lint . . . */
72 1.1 cgd char *headnames[] = {"To", "Subject", "Cc", 0};
73 1.1 cgd
74 1.1 cgd /*
75 1.1 cgd * Drive the whole formatter by managing input files. Also,
76 1.1 cgd * cause initialization of the output stuff and flush it out
77 1.1 cgd * at the end.
78 1.1 cgd */
79 1.1 cgd
80 1.1 cgd main(argc, argv)
81 1.1 cgd int argc;
82 1.1 cgd char **argv;
83 1.1 cgd {
84 1.1 cgd register FILE *fi;
85 1.1 cgd register int errs = 0;
86 1.1 cgd int number; /* LIZ@UOM 6/18/85 */
87 1.1 cgd
88 1.1 cgd goal_length = GOAL_LENGTH;
89 1.1 cgd max_length = MAX_LENGTH;
90 1.1 cgd setout();
91 1.1 cgd lineno = 1;
92 1.1 cgd mark = -10;
93 1.1 cgd /*
94 1.1 cgd * LIZ@UOM 6/18/85 -- Check for goal and max length arguments
95 1.1 cgd */
96 1.1 cgd if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
97 1.1 cgd argv++;
98 1.1 cgd argc--;
99 1.1 cgd goal_length = number;
100 1.1 cgd if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
101 1.1 cgd argv++;
102 1.1 cgd argc--;
103 1.1 cgd max_length = number;
104 1.1 cgd }
105 1.1 cgd }
106 1.1 cgd if (max_length <= goal_length) {
107 1.1 cgd fprintf(stderr, "Max length must be greater than %s\n",
108 1.1 cgd "goal length");
109 1.1 cgd exit(1);
110 1.1 cgd }
111 1.1 cgd if (argc < 2) {
112 1.1 cgd fmt(stdin);
113 1.1 cgd oflush();
114 1.1 cgd exit(0);
115 1.1 cgd }
116 1.1 cgd while (--argc) {
117 1.1 cgd if ((fi = fopen(*++argv, "r")) == NULL) {
118 1.1 cgd perror(*argv);
119 1.1 cgd errs++;
120 1.1 cgd continue;
121 1.1 cgd }
122 1.1 cgd fmt(fi);
123 1.1 cgd fclose(fi);
124 1.1 cgd }
125 1.1 cgd oflush();
126 1.1 cgd exit(errs);
127 1.1 cgd }
128 1.1 cgd
129 1.1 cgd /*
130 1.1 cgd * Read up characters from the passed input file, forming lines,
131 1.1 cgd * doing ^H processing, expanding tabs, stripping trailing blanks,
132 1.1 cgd * and sending each line down for analysis.
133 1.1 cgd */
134 1.1 cgd fmt(fi)
135 1.1 cgd FILE *fi;
136 1.1 cgd {
137 1.1 cgd char linebuf[BUFSIZ], canonb[BUFSIZ];
138 1.1 cgd register char *cp, *cp2;
139 1.1 cgd register int c, col;
140 1.1 cgd
141 1.1 cgd c = getc(fi);
142 1.1 cgd while (c != EOF) {
143 1.1 cgd /*
144 1.1 cgd * Collect a line, doing ^H processing.
145 1.1 cgd * Leave tabs for now.
146 1.1 cgd */
147 1.1 cgd cp = linebuf;
148 1.1 cgd while (c != '\n' && c != EOF && cp-linebuf < BUFSIZ-1) {
149 1.1 cgd if (c == '\b') {
150 1.1 cgd if (cp > linebuf)
151 1.1 cgd cp--;
152 1.1 cgd c = getc(fi);
153 1.1 cgd continue;
154 1.1 cgd }
155 1.1 cgd if ((c < ' ' || c >= 0177) && c != '\t') {
156 1.1 cgd c = getc(fi);
157 1.1 cgd continue;
158 1.1 cgd }
159 1.1 cgd *cp++ = c;
160 1.1 cgd c = getc(fi);
161 1.1 cgd }
162 1.1 cgd *cp = '\0';
163 1.1 cgd
164 1.1 cgd /*
165 1.1 cgd * Toss anything remaining on the input line.
166 1.1 cgd */
167 1.1 cgd while (c != '\n' && c != EOF)
168 1.1 cgd c = getc(fi);
169 1.1 cgd
170 1.1 cgd /*
171 1.1 cgd * Expand tabs on the way to canonb.
172 1.1 cgd */
173 1.1 cgd col = 0;
174 1.1 cgd cp = linebuf;
175 1.1 cgd cp2 = canonb;
176 1.1 cgd while (c = *cp++) {
177 1.1 cgd if (c != '\t') {
178 1.1 cgd col++;
179 1.1 cgd if (cp2-canonb < BUFSIZ-1)
180 1.1 cgd *cp2++ = c;
181 1.1 cgd continue;
182 1.1 cgd }
183 1.1 cgd do {
184 1.1 cgd if (cp2-canonb < BUFSIZ-1)
185 1.1 cgd *cp2++ = ' ';
186 1.1 cgd col++;
187 1.1 cgd } while ((col & 07) != 0);
188 1.1 cgd }
189 1.1 cgd
190 1.1 cgd /*
191 1.1 cgd * Swipe trailing blanks from the line.
192 1.1 cgd */
193 1.1 cgd for (cp2--; cp2 >= canonb && *cp2 == ' '; cp2--)
194 1.1 cgd ;
195 1.1 cgd *++cp2 = '\0';
196 1.1 cgd prefix(canonb);
197 1.1 cgd if (c != EOF)
198 1.1 cgd c = getc(fi);
199 1.1 cgd }
200 1.1 cgd }
201 1.1 cgd
202 1.1 cgd /*
203 1.1 cgd * Take a line devoid of tabs and other garbage and determine its
204 1.1 cgd * blank prefix. If the indent changes, call for a linebreak.
205 1.1 cgd * If the input line is blank, echo the blank line on the output.
206 1.1 cgd * Finally, if the line minus the prefix is a mail header, try to keep
207 1.1 cgd * it on a line by itself.
208 1.1 cgd */
209 1.1 cgd prefix(line)
210 1.1 cgd char line[];
211 1.1 cgd {
212 1.1 cgd register char *cp, **hp;
213 1.1 cgd register int np, h;
214 1.1 cgd
215 1.1 cgd if (strlen(line) == 0) {
216 1.1 cgd oflush();
217 1.1 cgd putchar('\n');
218 1.1 cgd return;
219 1.1 cgd }
220 1.1 cgd for (cp = line; *cp == ' '; cp++)
221 1.1 cgd ;
222 1.1 cgd np = cp - line;
223 1.1 cgd
224 1.1 cgd /*
225 1.1 cgd * The following horrible expression attempts to avoid linebreaks
226 1.1 cgd * when the indent changes due to a paragraph.
227 1.1 cgd */
228 1.1 cgd if (np != pfx && (np > pfx || abs(pfx-np) > 8))
229 1.1 cgd oflush();
230 1.1 cgd if (h = ishead(cp))
231 1.1 cgd oflush(), mark = lineno;
232 1.1 cgd if (lineno - mark < 3 && lineno - mark > 0)
233 1.1 cgd for (hp = &headnames[0]; *hp != (char *) 0; hp++)
234 1.1 cgd if (ispref(*hp, cp)) {
235 1.1 cgd h = 1;
236 1.1 cgd oflush();
237 1.1 cgd break;
238 1.1 cgd }
239 1.1 cgd if (!h && (h = (*cp == '.')))
240 1.1 cgd oflush();
241 1.1 cgd pfx = np;
242 1.1 cgd if (h)
243 1.1 cgd pack(cp);
244 1.1 cgd else split(cp);
245 1.1 cgd if (h)
246 1.1 cgd oflush();
247 1.1 cgd lineno++;
248 1.1 cgd }
249 1.1 cgd
250 1.1 cgd /*
251 1.1 cgd * Split up the passed line into output "words" which are
252 1.1 cgd * maximal strings of non-blanks with the blank separation
253 1.1 cgd * attached at the end. Pass these words along to the output
254 1.1 cgd * line packer.
255 1.1 cgd */
256 1.1 cgd split(line)
257 1.1 cgd char line[];
258 1.1 cgd {
259 1.1 cgd register char *cp, *cp2;
260 1.1 cgd char word[BUFSIZ];
261 1.1 cgd int wordl; /* LIZ@UOM 6/18/85 */
262 1.1 cgd
263 1.1 cgd cp = line;
264 1.1 cgd while (*cp) {
265 1.1 cgd cp2 = word;
266 1.1 cgd wordl = 0; /* LIZ@UOM 6/18/85 */
267 1.1 cgd
268 1.1 cgd /*
269 1.1 cgd * Collect a 'word,' allowing it to contain escaped white
270 1.1 cgd * space.
271 1.1 cgd */
272 1.1 cgd while (*cp && *cp != ' ') {
273 1.1 cgd if (*cp == '\\' && isspace(cp[1]))
274 1.1 cgd *cp2++ = *cp++;
275 1.1 cgd *cp2++ = *cp++;
276 1.1 cgd wordl++;/* LIZ@UOM 6/18/85 */
277 1.1 cgd }
278 1.1 cgd
279 1.1 cgd /*
280 1.1 cgd * Guarantee a space at end of line. Two spaces after end of
281 1.1 cgd * sentence punctuation.
282 1.1 cgd */
283 1.1 cgd if (*cp == '\0') {
284 1.1 cgd *cp2++ = ' ';
285 1.1 cgd if (index(".:!", cp[-1]))
286 1.1 cgd *cp2++ = ' ';
287 1.1 cgd }
288 1.1 cgd while (*cp == ' ')
289 1.1 cgd *cp2++ = *cp++;
290 1.1 cgd *cp2 = '\0';
291 1.1 cgd /*
292 1.1 cgd * LIZ@UOM 6/18/85 pack(word);
293 1.1 cgd */
294 1.1 cgd pack(word, wordl);
295 1.1 cgd }
296 1.1 cgd }
297 1.1 cgd
298 1.1 cgd /*
299 1.1 cgd * Output section.
300 1.1 cgd * Build up line images from the words passed in. Prefix
301 1.1 cgd * each line with correct number of blanks. The buffer "outbuf"
302 1.1 cgd * contains the current partial line image, including prefixed blanks.
303 1.1 cgd * "outp" points to the next available space therein. When outp is NOSTR,
304 1.1 cgd * there ain't nothing in there yet. At the bottom of this whole mess,
305 1.1 cgd * leading tabs are reinserted.
306 1.1 cgd */
307 1.1 cgd char outbuf[BUFSIZ]; /* Sandbagged output line image */
308 1.1 cgd char *outp; /* Pointer in above */
309 1.1 cgd
310 1.1 cgd /*
311 1.1 cgd * Initialize the output section.
312 1.1 cgd */
313 1.1 cgd setout()
314 1.1 cgd {
315 1.1 cgd outp = NOSTR;
316 1.1 cgd }
317 1.1 cgd
318 1.1 cgd /*
319 1.1 cgd * Pack a word onto the output line. If this is the beginning of
320 1.1 cgd * the line, push on the appropriately-sized string of blanks first.
321 1.1 cgd * If the word won't fit on the current line, flush and begin a new
322 1.1 cgd * line. If the word is too long to fit all by itself on a line,
323 1.1 cgd * just give it its own and hope for the best.
324 1.1 cgd *
325 1.1 cgd * LIZ@UOM 6/18/85 -- If the new word will fit in at less than the
326 1.1 cgd * goal length, take it. If not, then check to see if the line
327 1.1 cgd * will be over the max length; if so put the word on the next
328 1.1 cgd * line. If not, check to see if the line will be closer to the
329 1.1 cgd * goal length with or without the word and take it or put it on
330 1.1 cgd * the next line accordingly.
331 1.1 cgd */
332 1.1 cgd
333 1.1 cgd /*
334 1.1 cgd * LIZ@UOM 6/18/85 -- pass in the length of the word as well
335 1.1 cgd * pack(word)
336 1.1 cgd * char word[];
337 1.1 cgd */
338 1.1 cgd pack(word,wl)
339 1.1 cgd char word[];
340 1.1 cgd int wl;
341 1.1 cgd {
342 1.1 cgd register char *cp;
343 1.1 cgd register int s, t;
344 1.1 cgd
345 1.1 cgd if (outp == NOSTR)
346 1.1 cgd leadin();
347 1.1 cgd /*
348 1.1 cgd * LIZ@UOM 6/18/85 -- change condition to check goal_length; s is the
349 1.1 cgd * length of the line before the word is added; t is now the length
350 1.1 cgd * of the line after the word is added
351 1.1 cgd * t = strlen(word);
352 1.1 cgd * if (t+s <= LENGTH)
353 1.1 cgd */
354 1.1 cgd s = outp - outbuf;
355 1.1 cgd t = wl + s;
356 1.1 cgd if ((t <= goal_length) ||
357 1.1 cgd ((t <= max_length) && (t - goal_length <= goal_length - s))) {
358 1.1 cgd /*
359 1.1 cgd * In like flint!
360 1.1 cgd */
361 1.1 cgd for (cp = word; *cp; *outp++ = *cp++);
362 1.1 cgd return;
363 1.1 cgd }
364 1.1 cgd if (s > pfx) {
365 1.1 cgd oflush();
366 1.1 cgd leadin();
367 1.1 cgd }
368 1.1 cgd for (cp = word; *cp; *outp++ = *cp++);
369 1.1 cgd }
370 1.1 cgd
371 1.1 cgd /*
372 1.1 cgd * If there is anything on the current output line, send it on
373 1.1 cgd * its way. Set outp to NOSTR to indicate the absence of the current
374 1.1 cgd * line prefix.
375 1.1 cgd */
376 1.1 cgd oflush()
377 1.1 cgd {
378 1.1 cgd if (outp == NOSTR)
379 1.1 cgd return;
380 1.1 cgd *outp = '\0';
381 1.1 cgd tabulate(outbuf);
382 1.1 cgd outp = NOSTR;
383 1.1 cgd }
384 1.1 cgd
385 1.1 cgd /*
386 1.1 cgd * Take the passed line buffer, insert leading tabs where possible, and
387 1.1 cgd * output on standard output (finally).
388 1.1 cgd */
389 1.1 cgd tabulate(line)
390 1.1 cgd char line[];
391 1.1 cgd {
392 1.1 cgd register char *cp;
393 1.1 cgd register int b, t;
394 1.1 cgd
395 1.1 cgd /*
396 1.1 cgd * Toss trailing blanks in the output line.
397 1.1 cgd */
398 1.1 cgd cp = line + strlen(line) - 1;
399 1.1 cgd while (cp >= line && *cp == ' ')
400 1.1 cgd cp--;
401 1.1 cgd *++cp = '\0';
402 1.1 cgd
403 1.1 cgd /*
404 1.1 cgd * Count the leading blank space and tabulate.
405 1.1 cgd */
406 1.1 cgd for (cp = line; *cp == ' '; cp++)
407 1.1 cgd ;
408 1.1 cgd b = cp-line;
409 1.1 cgd t = b >> 3;
410 1.1 cgd b &= 07;
411 1.1 cgd if (t > 0)
412 1.1 cgd do
413 1.1 cgd putc('\t', stdout);
414 1.1 cgd while (--t);
415 1.1 cgd if (b > 0)
416 1.1 cgd do
417 1.1 cgd putc(' ', stdout);
418 1.1 cgd while (--b);
419 1.1 cgd while (*cp)
420 1.1 cgd putc(*cp++, stdout);
421 1.1 cgd putc('\n', stdout);
422 1.1 cgd }
423 1.1 cgd
424 1.1 cgd /*
425 1.1 cgd * Initialize the output line with the appropriate number of
426 1.1 cgd * leading blanks.
427 1.1 cgd */
428 1.1 cgd leadin()
429 1.1 cgd {
430 1.1 cgd register int b;
431 1.1 cgd register char *cp;
432 1.1 cgd
433 1.1 cgd for (b = 0, cp = outbuf; b < pfx; b++)
434 1.1 cgd *cp++ = ' ';
435 1.1 cgd outp = cp;
436 1.1 cgd }
437 1.1 cgd
438 1.1 cgd /*
439 1.1 cgd * Save a string in dynamic space.
440 1.1 cgd * This little goodie is needed for
441 1.1 cgd * a headline detector in head.c
442 1.1 cgd */
443 1.1 cgd char *
444 1.1 cgd savestr(str)
445 1.1 cgd char str[];
446 1.1 cgd {
447 1.1 cgd register char *top;
448 1.1 cgd
449 1.1 cgd top = malloc(strlen(str) + 1);
450 1.1 cgd if (top == NOSTR) {
451 1.1 cgd fprintf(stderr, "fmt: Ran out of memory\n");
452 1.1 cgd exit(1);
453 1.1 cgd }
454 1.1 cgd strcpy(top, str);
455 1.1 cgd return (top);
456 1.1 cgd }
457 1.1 cgd
458 1.1 cgd /*
459 1.1 cgd * Is s1 a prefix of s2??
460 1.1 cgd */
461 1.1 cgd ispref(s1, s2)
462 1.1 cgd register char *s1, *s2;
463 1.1 cgd {
464 1.1 cgd
465 1.1 cgd while (*s1++ == *s2)
466 1.1 cgd ;
467 1.1 cgd return (*s1 == '\0');
468 1.1 cgd }
469