xref: /minix3/usr.bin/unvis/unvis.c (revision 4e1bec4a1a6523e9f445eab6492d334814f59f13)
1*4e1bec4aSThomas Cort /*	$NetBSD: unvis.c,v 1.13 2010/11/27 19:46:25 christos Exp $	*/
2*4e1bec4aSThomas Cort 
3*4e1bec4aSThomas Cort /*-
4*4e1bec4aSThomas Cort  * Copyright (c) 1989, 1993
5*4e1bec4aSThomas Cort  *	The Regents of the University of California.  All rights reserved.
6*4e1bec4aSThomas Cort  *
7*4e1bec4aSThomas Cort  * Redistribution and use in source and binary forms, with or without
8*4e1bec4aSThomas Cort  * modification, are permitted provided that the following conditions
9*4e1bec4aSThomas Cort  * are met:
10*4e1bec4aSThomas Cort  * 1. Redistributions of source code must retain the above copyright
11*4e1bec4aSThomas Cort  *    notice, this list of conditions and the following disclaimer.
12*4e1bec4aSThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
13*4e1bec4aSThomas Cort  *    notice, this list of conditions and the following disclaimer in the
14*4e1bec4aSThomas Cort  *    documentation and/or other materials provided with the distribution.
15*4e1bec4aSThomas Cort  * 3. Neither the name of the University nor the names of its contributors
16*4e1bec4aSThomas Cort  *    may be used to endorse or promote products derived from this software
17*4e1bec4aSThomas Cort  *    without specific prior written permission.
18*4e1bec4aSThomas Cort  *
19*4e1bec4aSThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*4e1bec4aSThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*4e1bec4aSThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*4e1bec4aSThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*4e1bec4aSThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*4e1bec4aSThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*4e1bec4aSThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*4e1bec4aSThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*4e1bec4aSThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*4e1bec4aSThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*4e1bec4aSThomas Cort  * SUCH DAMAGE.
30*4e1bec4aSThomas Cort  */
31*4e1bec4aSThomas Cort 
32*4e1bec4aSThomas Cort #include <sys/cdefs.h>
33*4e1bec4aSThomas Cort #ifndef lint
34*4e1bec4aSThomas Cort __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
35*4e1bec4aSThomas Cort  The Regents of the University of California.  All rights reserved.");
36*4e1bec4aSThomas Cort #endif /* not lint */
37*4e1bec4aSThomas Cort 
38*4e1bec4aSThomas Cort #ifndef lint
39*4e1bec4aSThomas Cort #if 0
40*4e1bec4aSThomas Cort static char sccsid[] = "@(#)unvis.c	8.1 (Berkeley) 6/6/93";
41*4e1bec4aSThomas Cort #endif
42*4e1bec4aSThomas Cort __RCSID("$NetBSD: unvis.c,v 1.13 2010/11/27 19:46:25 christos Exp $");
43*4e1bec4aSThomas Cort #endif /* not lint */
44*4e1bec4aSThomas Cort 
45*4e1bec4aSThomas Cort #include <err.h>
46*4e1bec4aSThomas Cort #include <stdio.h>
47*4e1bec4aSThomas Cort #include <stdlib.h>
48*4e1bec4aSThomas Cort #include <unistd.h>
49*4e1bec4aSThomas Cort #include <vis.h>
50*4e1bec4aSThomas Cort 
51*4e1bec4aSThomas Cort static void process(FILE *, const char *, int);
52*4e1bec4aSThomas Cort 
53*4e1bec4aSThomas Cort int
main(int argc,char * argv[])54*4e1bec4aSThomas Cort main(int argc, char *argv[])
55*4e1bec4aSThomas Cort {
56*4e1bec4aSThomas Cort 	FILE *fp;
57*4e1bec4aSThomas Cort 	int ch, eflags = 0;
58*4e1bec4aSThomas Cort 
59*4e1bec4aSThomas Cort 	setprogname(argv[0]);
60*4e1bec4aSThomas Cort 	while ((ch = getopt(argc, argv, "eHhm")) != -1)
61*4e1bec4aSThomas Cort 		switch((char)ch) {
62*4e1bec4aSThomas Cort 		case 'e':
63*4e1bec4aSThomas Cort 			eflags |= VIS_NOESCAPE;
64*4e1bec4aSThomas Cort 			break;
65*4e1bec4aSThomas Cort 		case 'H':
66*4e1bec4aSThomas Cort 			eflags |= VIS_HTTP1866;
67*4e1bec4aSThomas Cort 			break;
68*4e1bec4aSThomas Cort 		case 'h':
69*4e1bec4aSThomas Cort 			eflags |= VIS_HTTP1808;
70*4e1bec4aSThomas Cort 			break;
71*4e1bec4aSThomas Cort 		case 'm':
72*4e1bec4aSThomas Cort 			eflags |= VIS_MIMESTYLE;
73*4e1bec4aSThomas Cort 			break;
74*4e1bec4aSThomas Cort 		case '?':
75*4e1bec4aSThomas Cort 		default:
76*4e1bec4aSThomas Cort 			(void)fprintf(stderr,
77*4e1bec4aSThomas Cort 			    "Usage: %s [-e] [-Hh | -m] [file...]\n",
78*4e1bec4aSThomas Cort 			    getprogname());
79*4e1bec4aSThomas Cort 			return EXIT_FAILURE;
80*4e1bec4aSThomas Cort 		}
81*4e1bec4aSThomas Cort 	argc -= optind;
82*4e1bec4aSThomas Cort 	argv += optind;
83*4e1bec4aSThomas Cort 
84*4e1bec4aSThomas Cort 	switch (eflags & (VIS_HTTP1808|VIS_HTTP1866|VIS_MIMESTYLE)) {
85*4e1bec4aSThomas Cort 	case VIS_HTTP1808|VIS_MIMESTYLE:
86*4e1bec4aSThomas Cort 	case VIS_HTTP1866|VIS_MIMESTYLE:
87*4e1bec4aSThomas Cort 	case VIS_HTTP1808|VIS_HTTP1866|VIS_MIMESTYLE:
88*4e1bec4aSThomas Cort 		errx(EXIT_FAILURE, "Can't mix -m with -h and/or -H");
89*4e1bec4aSThomas Cort 		/*NOTREACHED*/
90*4e1bec4aSThomas Cort 	default:
91*4e1bec4aSThomas Cort 		break;
92*4e1bec4aSThomas Cort 	}
93*4e1bec4aSThomas Cort 
94*4e1bec4aSThomas Cort 	if (*argv)
95*4e1bec4aSThomas Cort 		while (*argv) {
96*4e1bec4aSThomas Cort 			if ((fp = fopen(*argv, "r")) != NULL)
97*4e1bec4aSThomas Cort 				process(fp, *argv, eflags);
98*4e1bec4aSThomas Cort 			else
99*4e1bec4aSThomas Cort 				warn("%s", *argv);
100*4e1bec4aSThomas Cort 			argv++;
101*4e1bec4aSThomas Cort 		}
102*4e1bec4aSThomas Cort 	else
103*4e1bec4aSThomas Cort 		process(stdin, "<stdin>", eflags);
104*4e1bec4aSThomas Cort 	return EXIT_SUCCESS;
105*4e1bec4aSThomas Cort }
106*4e1bec4aSThomas Cort 
107*4e1bec4aSThomas Cort static void
process(FILE * fp,const char * filename,int eflags)108*4e1bec4aSThomas Cort process(FILE *fp, const char *filename, int eflags)
109*4e1bec4aSThomas Cort {
110*4e1bec4aSThomas Cort 	int offset = 0, c, ret;
111*4e1bec4aSThomas Cort 	int state = 0;
112*4e1bec4aSThomas Cort 	char outc;
113*4e1bec4aSThomas Cort 
114*4e1bec4aSThomas Cort 	while ((c = getc(fp)) != EOF) {
115*4e1bec4aSThomas Cort 		offset++;
116*4e1bec4aSThomas Cort 	again:
117*4e1bec4aSThomas Cort 		switch(ret = unvis(&outc, (char)c, &state, eflags)) {
118*4e1bec4aSThomas Cort 		case UNVIS_VALID:
119*4e1bec4aSThomas Cort 			(void)putchar(outc);
120*4e1bec4aSThomas Cort 			break;
121*4e1bec4aSThomas Cort 		case UNVIS_VALIDPUSH:
122*4e1bec4aSThomas Cort 			(void)putchar(outc);
123*4e1bec4aSThomas Cort 			goto again;
124*4e1bec4aSThomas Cort 		case UNVIS_SYNBAD:
125*4e1bec4aSThomas Cort 			warnx("%s: offset: %d: can't decode", filename, offset);
126*4e1bec4aSThomas Cort 			state = 0;
127*4e1bec4aSThomas Cort 			break;
128*4e1bec4aSThomas Cort 		case 0:
129*4e1bec4aSThomas Cort 		case UNVIS_NOCHAR:
130*4e1bec4aSThomas Cort 			break;
131*4e1bec4aSThomas Cort 		default:
132*4e1bec4aSThomas Cort 			errx(1, "bad return value (%d), can't happen", ret);
133*4e1bec4aSThomas Cort 			/* NOTREACHED */
134*4e1bec4aSThomas Cort 		}
135*4e1bec4aSThomas Cort 	}
136*4e1bec4aSThomas Cort 	if (unvis(&outc, (char)0, &state, eflags | UNVIS_END) == UNVIS_VALID)
137*4e1bec4aSThomas Cort 		(void)putchar(outc);
138*4e1bec4aSThomas Cort }
139