xref: /dragonfly/usr.bin/unvis/unvis.c (revision 73610d44)
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  * 3. 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  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)unvis.c	8.1 (Berkeley) 6/6/93
31  * $FreeBSD: src/usr.bin/unvis/unvis.c,v 1.5.2.1 2001/07/30 10:16:49 dd Exp $
32  * $DragonFly: src/usr.bin/unvis/unvis.c,v 1.5 2005/02/19 00:32:01 liamfoy Exp $
33  */
34 
35 #include <err.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <vis.h>
40 
41 static void process(FILE *, const char *);
42 static void usage(void);
43 
44 int
45 main(int argc, char **argv)
46 {
47 	FILE *fp;
48 	int ch;
49 
50 	while ((ch = getopt(argc, argv, "")) != -1)
51 		switch(ch) {
52 		case '?':
53 		default:
54 			usage();
55 		}
56 	argc -= optind;
57 	argv += optind;
58 
59 	if (*argv)
60 		while (*argv) {
61 			if ((fp = fopen(*argv, "r")) != NULL)
62 				process(fp, *argv);
63 			else
64 				warn("%s", *argv);
65 			argv++;
66 		}
67 	else
68 		process(stdin, "<stdin>");
69 	exit(0);
70 }
71 
72 static void
73 usage(void)
74 {
75 	fprintf(stderr, "usage: unvis [file ...]\n");
76 	exit(1);
77 }
78 
79 static void
80 process(FILE *fp, const char *filename)
81 {
82 	int offset = 0, c, ret;
83 	int state = 0;
84 	char outc;
85 
86 	while ((c = getc(fp)) != EOF) {
87 		offset++;
88 	again:
89 		switch(ret = unvis(&outc, c, &state, 0)) {
90 		case UNVIS_VALID:
91 			putchar(outc);
92 			break;
93 		case UNVIS_VALIDPUSH:
94 			putchar(outc);
95 			goto again;
96 		case UNVIS_SYNBAD:
97 			warnx("%s: offset: %d: can't decode", filename, offset);
98 			state = 0;
99 			break;
100 		case 0:
101 		case UNVIS_NOCHAR:
102 			break;
103 		default:
104 			errx(1, "bad return value (%d), can't happen", ret);
105 		}
106 	}
107 	if (unvis(&outc, (char)0, &state, UNVIS_END) == UNVIS_VALID)
108 		putchar(outc);
109 }
110