fmt.c (f4c1975c26e06e5870cf6e831168fa790959bc52) fmt.c (63ffb113474aff32b0e8bad11d52b816655aa701)
1/*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 24 unchanged lines hidden (view full) ---

33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
1/*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 24 unchanged lines hidden (view full) ---

33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
41static char sccsid[] = "@(#)fmt.c 8.1 (Berkeley) 7/20/93";
42static char sccsid[] = "@(#)fmt.c 8.1 (Berkeley) 7/20/93";
43#else
44static const char rcsid[] =
45 "$Id$";
46#endif
42#endif /* not lint */
43
47#endif /* not lint */
48
44#include <stdio.h>
45#include <ctype.h>
49#include <ctype.h>
50#include <err.h>
46#include <locale.h>
51#include <locale.h>
52#include <stdio.h>
47#include <stdlib.h>
53#include <stdlib.h>
54#include <string.h>
48
49/*
50 * fmt -- format the concatenation of input files or standard input
51 * onto standard output. Designed for use with Mail ~|
52 *
53 * Syntax : fmt [ goal [ max ] ] [ name ... ]
54 * Authors: Kurt Shoens (UCB) 12/7/78;
55 * Liz Allen (UMCP) 2/24/83 [Addition of goal length concept].

--- 11 unchanged lines hidden (view full) ---

67int max_length; /* Max line length in output */
68int pfx; /* Current leading blank count */
69int lineno; /* Current input line */
70int mark; /* Last place we saw a head line */
71int center;
72
73char *headnames[] = {"To", "Subject", "Cc", 0};
74
55
56/*
57 * fmt -- format the concatenation of input files or standard input
58 * onto standard output. Designed for use with Mail ~|
59 *
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].

--- 11 unchanged lines hidden (view full) ---

74int max_length; /* Max line length in output */
75int pfx; /* Current leading blank count */
76int lineno; /* Current input line */
77int mark; /* Last place we saw a head line */
78int center;
79
80char *headnames[] = {"To", "Subject", "Cc", 0};
81
82void fmt __P((FILE *));
83int ispref __P((char *, char *));
84void leadin __P((void));
85void oflush __P((void));
86void pack __P((char [], int));
87void prefix __P((char []));
88void setout __P((void));
89void split __P((char []));
90void tabulate __P((char []));
91
75/*
76 * Drive the whole formatter by managing input files. Also,
77 * cause initialization of the output stuff and flush it out
78 * at the end.
79 */
80
92/*
93 * Drive the whole formatter by managing input files. Also,
94 * cause initialization of the output stuff and flush it out
95 * at the end.
96 */
97
98int
81main(argc, argv)
82 int argc;
83 char **argv;
84{
85 register FILE *fi;
86 register int errs = 0;
87 int number; /* LIZ@UOM 6/18/85 */
88

--- 17 unchanged lines hidden (view full) ---

106 argc--;
107 goal_length = number;
108 if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
109 argv++;
110 argc--;
111 max_length = number;
112 }
113 }
99main(argc, argv)
100 int argc;
101 char **argv;
102{
103 register FILE *fi;
104 register int errs = 0;
105 int number; /* LIZ@UOM 6/18/85 */
106

--- 17 unchanged lines hidden (view full) ---

124 argc--;
125 goal_length = number;
126 if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
127 argv++;
128 argc--;
129 max_length = number;
130 }
131 }
114 if (max_length <= goal_length) {
115 fprintf(stderr, "Max length must be greater than %s\n",
116 "goal length");
117 exit(1);
118 }
132 if (max_length <= goal_length)
133 errx(1, "max length must be greater than goal length");
119 if (argc < 2) {
120 fmt(stdin);
121 oflush();
122 exit(0);
123 }
124 while (--argc) {
125 if ((fi = fopen(*++argv, "r")) == NULL) {
126 perror(*argv);

--- 7 unchanged lines hidden (view full) ---

134 exit(errs);
135}
136
137/*
138 * Read up characters from the passed input file, forming lines,
139 * doing ^H processing, expanding tabs, stripping trailing blanks,
140 * and sending each line down for analysis.
141 */
134 if (argc < 2) {
135 fmt(stdin);
136 oflush();
137 exit(0);
138 }
139 while (--argc) {
140 if ((fi = fopen(*++argv, "r")) == NULL) {
141 perror(*argv);

--- 7 unchanged lines hidden (view full) ---

149 exit(errs);
150}
151
152/*
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.
156 */
157void
142fmt(fi)
143 FILE *fi;
144{
145 static char *linebuf = 0, *canonb = 0;
146 register char *cp, *cp2, cc;
147 register int c, col;
148#define CHUNKSIZE 1024
149 static int lbufsize = 0, cbufsize = 0;

--- 64 unchanged lines hidden (view full) ---

214 }
215
216 /*
217 * Expand tabs on the way to canonb.
218 */
219 col = 0;
220 cp = linebuf;
221 cp2 = canonb;
158fmt(fi)
159 FILE *fi;
160{
161 static char *linebuf = 0, *canonb = 0;
162 register char *cp, *cp2, cc;
163 register int c, col;
164#define CHUNKSIZE 1024
165 static int lbufsize = 0, cbufsize = 0;

--- 64 unchanged lines hidden (view full) ---

230 }
231
232 /*
233 * Expand tabs on the way to canonb.
234 */
235 col = 0;
236 cp = linebuf;
237 cp2 = canonb;
222 while (cc = *cp++) {
238 while ((cc = *cp++)) {
223 if (cc != '\t') {
224 col++;
225 if (cp2 - canonb >= cbufsize) {
226 int offset = cp2 - canonb;
227 cbufsize += CHUNKSIZE;
228 canonb = realloc(canonb, cbufsize);
229 if(canonb == 0)
230 abort();

--- 30 unchanged lines hidden (view full) ---

261
262/*
263 * Take a line devoid of tabs and other garbage and determine its
264 * blank prefix. If the indent changes, call for a linebreak.
265 * If the input line is blank, echo the blank line on the output.
266 * Finally, if the line minus the prefix is a mail header, try to keep
267 * it on a line by itself.
268 */
239 if (cc != '\t') {
240 col++;
241 if (cp2 - canonb >= cbufsize) {
242 int offset = cp2 - canonb;
243 cbufsize += CHUNKSIZE;
244 canonb = realloc(canonb, cbufsize);
245 if(canonb == 0)
246 abort();

--- 30 unchanged lines hidden (view full) ---

277
278/*
279 * Take a line devoid of tabs and other garbage and determine its
280 * blank prefix. If the indent changes, call for a linebreak.
281 * If the input line is blank, echo the blank line on the output.
282 * Finally, if the line minus the prefix is a mail header, try to keep
283 * it on a line by itself.
284 */
285void
269prefix(line)
270 char line[];
271{
272 register char *cp, **hp;
273 register int np, h;
274
275 if (!*line) {
276 oflush();

--- 5 unchanged lines hidden (view full) ---

282 np = cp - line;
283
284 /*
285 * The following horrible expression attempts to avoid linebreaks
286 * when the indent changes due to a paragraph.
287 */
288 if (np != pfx && (np > pfx || abs(pfx-np) > 8))
289 oflush();
286prefix(line)
287 char line[];
288{
289 register char *cp, **hp;
290 register int np, h;
291
292 if (!*line) {
293 oflush();

--- 5 unchanged lines hidden (view full) ---

299 np = cp - line;
300
301 /*
302 * The following horrible expression attempts to avoid linebreaks
303 * when the indent changes due to a paragraph.
304 */
305 if (np != pfx && (np > pfx || abs(pfx-np) > 8))
306 oflush();
290 if (h = ishead(cp))
307 if ((h = ishead(cp)))
291 oflush(), mark = lineno;
292 if (lineno - mark < 3 && lineno - mark > 0)
293 for (hp = &headnames[0]; *hp != (char *) 0; hp++)
294 if (ispref(*hp, cp)) {
295 h = 1;
296 oflush();
297 break;
298 }

--- 9 unchanged lines hidden (view full) ---

308}
309
310/*
311 * Split up the passed line into output "words" which are
312 * maximal strings of non-blanks with the blank separation
313 * attached at the end. Pass these words along to the output
314 * line packer.
315 */
308 oflush(), mark = lineno;
309 if (lineno - mark < 3 && lineno - mark > 0)
310 for (hp = &headnames[0]; *hp != (char *) 0; hp++)
311 if (ispref(*hp, cp)) {
312 h = 1;
313 oflush();
314 break;
315 }

--- 9 unchanged lines hidden (view full) ---

325}
326
327/*
328 * Split up the passed line into output "words" which are
329 * maximal strings of non-blanks with the blank separation
330 * attached at the end. Pass these words along to the output
331 * line packer.
332 */
333void
316split(line)
317 char line[];
318{
319 register char *cp, *cp2;
320 char word[BUFSIZ];
321 int wordl; /* LIZ@UOM 6/18/85 */
322
323 cp = line;

--- 41 unchanged lines hidden (view full) ---

365 * leading tabs are reinserted.
366 */
367char outbuf[BUFSIZ]; /* Sandbagged output line image */
368char *outp; /* Pointer in above */
369
370/*
371 * Initialize the output section.
372 */
334split(line)
335 char line[];
336{
337 register char *cp, *cp2;
338 char word[BUFSIZ];
339 int wordl; /* LIZ@UOM 6/18/85 */
340
341 cp = line;

--- 41 unchanged lines hidden (view full) ---

383 * leading tabs are reinserted.
384 */
385char outbuf[BUFSIZ]; /* Sandbagged output line image */
386char *outp; /* Pointer in above */
387
388/*
389 * Initialize the output section.
390 */
391void
373setout()
374{
375 outp = NOSTR;
376}
377
378/*
379 * Pack a word onto the output line. If this is the beginning of
380 * the line, push on the appropriately-sized string of blanks first.

--- 9 unchanged lines hidden (view full) ---

390 * the next line accordingly.
391 */
392
393/*
394 * LIZ@UOM 6/18/85 -- pass in the length of the word as well
395 * pack(word)
396 * char word[];
397 */
392setout()
393{
394 outp = NOSTR;
395}
396
397/*
398 * Pack a word onto the output line. If this is the beginning of
399 * the line, push on the appropriately-sized string of blanks first.

--- 9 unchanged lines hidden (view full) ---

409 * the next line accordingly.
410 */
411
412/*
413 * LIZ@UOM 6/18/85 -- pass in the length of the word as well
414 * pack(word)
415 * char word[];
416 */
417void
398pack(word,wl)
399 char word[];
400 int wl;
401{
402 register char *cp;
403 register int s, t;
404
405 if (outp == NOSTR)

--- 22 unchanged lines hidden (view full) ---

428 for (cp = word; *cp; *outp++ = *cp++);
429}
430
431/*
432 * If there is anything on the current output line, send it on
433 * its way. Set outp to NOSTR to indicate the absence of the current
434 * line prefix.
435 */
418pack(word,wl)
419 char word[];
420 int wl;
421{
422 register char *cp;
423 register int s, t;
424
425 if (outp == NOSTR)

--- 22 unchanged lines hidden (view full) ---

448 for (cp = word; *cp; *outp++ = *cp++);
449}
450
451/*
452 * If there is anything on the current output line, send it on
453 * its way. Set outp to NOSTR to indicate the absence of the current
454 * line prefix.
455 */
456void
436oflush()
437{
438 if (outp == NOSTR)
439 return;
440 *outp = '\0';
441 tabulate(outbuf);
442 outp = NOSTR;
443}
444
445/*
446 * Take the passed line buffer, insert leading tabs where possible, and
447 * output on standard output (finally).
448 */
457oflush()
458{
459 if (outp == NOSTR)
460 return;
461 *outp = '\0';
462 tabulate(outbuf);
463 outp = NOSTR;
464}
465
466/*
467 * Take the passed line buffer, insert leading tabs where possible, and
468 * output on standard output (finally).
469 */
470void
449tabulate(line)
450 char line[];
451{
452 register char *cp;
453 register int b, t;
454
455 /*
456 * Toss trailing blanks in the output line.

--- 23 unchanged lines hidden (view full) ---

480 putc(*cp++, stdout);
481 putc('\n', stdout);
482}
483
484/*
485 * Initialize the output line with the appropriate number of
486 * leading blanks.
487 */
471tabulate(line)
472 char line[];
473{
474 register char *cp;
475 register int b, t;
476
477 /*
478 * Toss trailing blanks in the output line.

--- 23 unchanged lines hidden (view full) ---

502 putc(*cp++, stdout);
503 putc('\n', stdout);
504}
505
506/*
507 * Initialize the output line with the appropriate number of
508 * leading blanks.
509 */
510void
488leadin()
489{
490 register int b;
491 register char *cp;
492
493 for (b = 0, cp = outbuf; b < pfx; b++)
494 *cp++ = ' ';
495 outp = cp;

--- 6 unchanged lines hidden (view full) ---

502 */
503char *
504savestr(str)
505 char str[];
506{
507 register char *top;
508
509 top = malloc(strlen(str) + 1);
511leadin()
512{
513 register int b;
514 register char *cp;
515
516 for (b = 0, cp = outbuf; b < pfx; b++)
517 *cp++ = ' ';
518 outp = cp;

--- 6 unchanged lines hidden (view full) ---

525 */
526char *
527savestr(str)
528 char str[];
529{
530 register char *top;
531
532 top = malloc(strlen(str) + 1);
510 if (top == NOSTR) {
511 fprintf(stderr, "fmt: Ran out of memory\n");
512 exit(1);
513 }
533 if (top == NOSTR)
534 errx(1, "ran out of memory");
514 strcpy(top, str);
515 return (top);
516}
517
518/*
519 * Is s1 a prefix of s2??
520 */
535 strcpy(top, str);
536 return (top);
537}
538
539/*
540 * Is s1 a prefix of s2??
541 */
542int
521ispref(s1, s2)
522 register char *s1, *s2;
523{
524
525 while (*s1++ == *s2)
526 ;
527 return (*s1 == '\0');
528}
543ispref(s1, s2)
544 register char *s1, *s2;
545{
546
547 while (*s1++ == *s2)
548 ;
549 return (*s1 == '\0');
550}