PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/usr.bin/vis/vis.c

https://bitbucket.org/freebsd/freebsd-head/
C | 189 lines | 140 code | 17 blank | 32 comment | 25 complexity | f600b22027a42b211c29ecb7de63f183 MD5 | raw file
  1/*-
  2 * Copyright (c) 1989, 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
  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 * 4. Neither the name of the University nor the names of its contributors
 14 *    may be used to endorse or promote products derived from this software
 15 *    without specific prior written permission.
 16 *
 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 27 * SUCH DAMAGE.
 28 */
 29
 30#include <sys/cdefs.h>
 31
 32__FBSDID("$FreeBSD$");
 33
 34#ifndef lint
 35static const char copyright[] =
 36"@(#) Copyright (c) 1989, 1993\n\
 37	The Regents of the University of California.  All rights reserved.\n";
 38#endif
 39
 40#ifndef lint
 41static const char sccsid[] = "@(#)vis.c	8.1 (Berkeley) 6/6/93";
 42#endif
 43
 44#include <err.h>
 45#include <locale.h>
 46#include <stdio.h>
 47#include <stdlib.h>
 48#include <unistd.h>
 49#include <vis.h>
 50
 51#include "extern.h"
 52
 53static int eflags, fold, foldwidth = 80, none, markeol;
 54#ifdef DEBUG
 55static int debug;
 56#endif
 57
 58static void process(FILE *);
 59static void usage(void);
 60
 61int
 62main(int argc, char *argv[])
 63{
 64	FILE *fp;
 65	int ch;
 66
 67	(void) setlocale(LC_CTYPE, "");
 68
 69	while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != -1)
 70		switch((char)ch) {
 71		case 'n':
 72			none++;
 73			break;
 74		case 'w':
 75			eflags |= VIS_WHITE;
 76			break;
 77		case 'c':
 78			eflags |= VIS_CSTYLE;
 79			break;
 80		case 't':
 81			eflags |= VIS_TAB;
 82			break;
 83		case 's':
 84			eflags |= VIS_SAFE;
 85			break;
 86		case 'o':
 87			eflags |= VIS_OCTAL;
 88			break;
 89		case 'b':
 90			eflags |= VIS_NOSLASH;
 91			break;
 92		case 'F':
 93			if ((foldwidth = atoi(optarg))<5)
 94				errx(1, "can't fold lines to less than 5 cols");
 95			/*FALLTHROUGH*/
 96		case 'f':
 97			fold++;		/* fold output lines to 80 cols */
 98			break;		/* using hidden newline */
 99		case 'l':
100			markeol++;	/* mark end of line with \$ */
101			break;
102#ifdef DEBUG
103		case 'd':
104			debug++;
105			break;
106#endif
107		case '?':
108		default:
109			usage();
110		}
111	argc -= optind;
112	argv += optind;
113
114	if (*argv)
115		while (*argv) {
116			if ((fp=fopen(*argv, "r")) != NULL)
117				process(fp);
118			else
119				warn("%s", *argv);
120			argv++;
121		}
122	else
123		process(stdin);
124	exit(0);
125}
126
127
128static void
129usage(void)
130{
131#ifdef DEBUG
132	fprintf(stderr, "usage: vis [-cbflnostwd] [-F foldwidth] [file ...]\n");
133#else
134	fprintf(stderr, "usage: vis [-cbflnostw] [-F foldwidth] [file ...]\n");
135#endif
136	exit(1);
137}
138
139static void
140process(FILE *fp)
141{
142	static int col = 0;
143	static char dummy[] = "\0";
144	char *cp = dummy+1; /* so *(cp-1) starts out != '\n' */
145	int c, rachar;
146	char buff[5];
147
148	c = getc(fp);
149	while (c != EOF) {
150		rachar = getc(fp);
151		if (none) {
152			cp = buff;
153			*cp++ = c;
154			if (c == '\\')
155				*cp++ = '\\';
156			*cp = '\0';
157		} else if (markeol && c == '\n') {
158			cp = buff;
159			if ((eflags & VIS_NOSLASH) == 0)
160				*cp++ = '\\';
161			*cp++ = '$';
162			*cp++ = '\n';
163			*cp = '\0';
164		} else
165			(void) vis(buff, (char)c, eflags, (char)rachar);
166
167		cp = buff;
168		if (fold) {
169#ifdef DEBUG
170			if (debug)
171				printf("<%02d,", col);
172#endif
173			col = foldit(cp, col, foldwidth);
174#ifdef DEBUG
175			if (debug)
176				printf("%02d>", col);
177#endif
178		}
179		do {
180			putchar(*cp);
181		} while (*++cp);
182		c = rachar;
183	}
184	/*
185	 * terminate partial line with a hidden newline
186	 */
187	if (fold && *(cp-1) != '\n')
188		printf("\\\n");
189}