home *** CD-ROM | disk | FTP | other *** search
- /* trunc.c - Remove trailing white space from a file. In addition *
- * perform some simple formatting. Options include lm (left *
- * margin), lc (leftmost column to copy), rc (rightmost column *
- * to copy), mc (convert CR's to LF's), and md (delete CR's). *
- * *
- * trunc [-<mc>|<md>][-lm n][-lc n][-rc n] [<file> ...] *
- * *
- * trunc (C) 1988 by Gary L. Brant *
- * *
- * :ts=8 */
-
- #define MAXLINE 1000
- #include <stdio.h>
- int lc = 0, /* leftmost column to copy */
- lm = 0, /* left margin to insert */
- rc = 0; /* rightmost column to copy */
- int m = 0; /* conversion flag for ^M's */
- int flag = 0; /* indicates CR seen and conversion mode on */
- int head = 0;
- void fputs (), putc ();
-
- main (argc, argv) /*remove trailing blanks and tabs from source lines */
- int argc;
- char *argv[];
-
- {
- int i = 0, j;
- char ch, *pch;
- FILE *ifile, *fopen ();
-
- while ((++i < argc) && (argv[i][0] == '-')) {
- j = 1;
- switch (ch = argv[i][j++]) {
- case 'l': if ((ch = argv[i][j]) == 'c' && i < argc - 1) {
- lc = convert (argv[++i]);
- if (lc > 0)
- lc--;
- } else if (ch = 'm' && i < argc - 1)
- lm = convert (argv[++i]);
- else
- badarg ();
- break;
- case 'm': if ((ch = argv[i][j]) == 'd')
- m = 1;
- else if (ch == 'c')
- m = 2;
- break;
- case 'r': if ((ch = argv[i][j]) == 'c' && i < argc - 1)
- rc = convert (argv[++i]);
- else
- badarg ();
- break;
- default: badarg ();
- break;
- }
- }
- if (rc == 0) rc = MAXLINE - 1 - lm;
- if (lc > rc || rc - lc + lm >= MAXLINE) badarg ();
-
- while (i < argc) {
- ++head;
- if ((ifile = fopen ((pch = argv[i++]), "r")) == NULL) {
- fputs ("trunc: cant open ", stderr);
- fputs (pch, stderr);
- putc ('\n', stderr);
- exit (20);
- } else
- copy (ifile);
- }
- if (head == 0)
- copy (stdin);
- }
-
-
- /* copy - copy file, remove trailing white space and format as we go */
- /* inspired by example in: K & R, P. 61 */
-
- copy (ifile)
- FILE *ifile;
- {
- int irc, n = 0;
- char line[MAXLINE];
-
- irc = rc + lm - lc;
- while (n < lm)
- line[n++] = ' ';
- while ((n = getline (ifile, &line[lm], MAXLINE-1-lm)) > 0) {
- register int in = (lm + n < irc) ? lm + n : irc;
- while (in-- >= lm)
- if (line[in] != ' ' && line[in] != '\t' && line[in] != '\n')
- break;
- if (in < lm) {
- putc ('\n', stdout);
- if (flag) {
- flag = 0;
- putc ('\n', stdout);
- }
- } else {
- if (flag != 0) {
- line[++in] = '\n';
- flag = 0;
- }
- line[++in] = '\n';
- line[++in] = '\0';
- fputs (line, stdout);
- }
- }
- }
-
-
- /* get line into s, return length
- * inspired by example in: K & R, P. 67 */
-
- getline (ifile, s, lim)
- FILE *ifile;
- char s[];
- int lim;
- {
- register int c, i, j;
-
- i = 0; j = lc; /* leftmost columns to ignore */
- while (i < lim && (c = getc (ifile)) != EOF && c != '\n') {
- if (c == '\r') {
- if (m == 1)
- continue;
- else if (m == 2) {
- flag = 1;
- continue;
- }
- }
- if (j-- <= 0)
- s[i++] = c;
- }
- if (c == '\n')
- s[i++] = c;
- s[i] = '\0';
- return (i);
- }
-
-
- /* convert - convert numeric command-line arguments to binary *
- * returns -1 if non-numeric data encountered */
-
- convert (argv)
- char *argv;
- {
- register long i = 0;
- register char ch;
- register int j=0;
-
- while ((ch = argv[j++]) != '\0')
- if (ch >= '0' && ch <= '9') {
- i *= 10;
- i += ch - '0';
- } else
- return (-1);
- return (i);
- }
-
-
- /* badarg - complain about bad argument */
-
- badarg ()
- {
- fputs ("bad args\n", stderr);
- exit (20);
- }
-
-
- _wb_parse ()
- {
- }
-