1247131Sbrooks/* $NetBSD: unvis.c,v 1.13 2010/11/27 19:46:25 christos Exp $ */ 2247131Sbrooks 3247131Sbrooks/*- 4247131Sbrooks * Copyright (c) 1989, 1993 5247131Sbrooks * The Regents of the University of California. All rights reserved. 6247131Sbrooks * 7247131Sbrooks * Redistribution and use in source and binary forms, with or without 8247131Sbrooks * modification, are permitted provided that the following conditions 9247131Sbrooks * are met: 10247131Sbrooks * 1. Redistributions of source code must retain the above copyright 11247131Sbrooks * notice, this list of conditions and the following disclaimer. 12247131Sbrooks * 2. Redistributions in binary form must reproduce the above copyright 13247131Sbrooks * notice, this list of conditions and the following disclaimer in the 14247131Sbrooks * documentation and/or other materials provided with the distribution. 15247131Sbrooks * 3. Neither the name of the University nor the names of its contributors 16247131Sbrooks * may be used to endorse or promote products derived from this software 17247131Sbrooks * without specific prior written permission. 18247131Sbrooks * 19247131Sbrooks * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20247131Sbrooks * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21247131Sbrooks * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22247131Sbrooks * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23247131Sbrooks * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24247131Sbrooks * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25247131Sbrooks * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26247131Sbrooks * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27247131Sbrooks * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28247131Sbrooks * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29247131Sbrooks * SUCH DAMAGE. 30247131Sbrooks */ 31247131Sbrooks 32247131Sbrooks#include <sys/cdefs.h> 33247131Sbrooks#ifndef lint 34247131Sbrooks__COPYRIGHT("@(#) Copyright (c) 1989, 1993\ 35247131Sbrooks The Regents of the University of California. All rights reserved."); 36247131Sbrooks#endif /* not lint */ 37247131Sbrooks 38247131Sbrooks#ifndef lint 39247131Sbrooks#if 0 40247131Sbrooksstatic char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/6/93"; 41247131Sbrooks#endif 42247131Sbrooks__RCSID("$NetBSD: unvis.c,v 1.13 2010/11/27 19:46:25 christos Exp $"); 43247131Sbrooks#endif /* not lint */ 44247131Sbrooks 45247131Sbrooks#include <err.h> 46247131Sbrooks#include <stdio.h> 47247131Sbrooks#include <stdlib.h> 48247131Sbrooks#include <unistd.h> 49247131Sbrooks#include <vis.h> 50247131Sbrooks 51247131Sbrooksstatic void process(FILE *, const char *, int); 52247131Sbrooks 53247131Sbrooksint 54247131Sbrooksmain(int argc, char *argv[]) 55247131Sbrooks{ 56247131Sbrooks FILE *fp; 57247131Sbrooks int ch, eflags = 0; 58247131Sbrooks 59247131Sbrooks setprogname(argv[0]); 60247131Sbrooks while ((ch = getopt(argc, argv, "eHhm")) != -1) 61247131Sbrooks switch((char)ch) { 62247131Sbrooks case 'e': 63247131Sbrooks eflags |= VIS_NOESCAPE; 64247131Sbrooks break; 65247131Sbrooks case 'H': 66247131Sbrooks eflags |= VIS_HTTP1866; 67247131Sbrooks break; 68247131Sbrooks case 'h': 69247131Sbrooks eflags |= VIS_HTTP1808; 70247131Sbrooks break; 71247131Sbrooks case 'm': 72247131Sbrooks eflags |= VIS_MIMESTYLE; 73247131Sbrooks break; 74247131Sbrooks case '?': 75247131Sbrooks default: 76247131Sbrooks (void)fprintf(stderr, 77247131Sbrooks "Usage: %s [-e] [-Hh | -m] [file...]\n", 78247131Sbrooks getprogname()); 79247131Sbrooks return EXIT_FAILURE; 80247131Sbrooks } 81247131Sbrooks argc -= optind; 82247131Sbrooks argv += optind; 83247131Sbrooks 84247131Sbrooks switch (eflags & (VIS_HTTP1808|VIS_HTTP1866|VIS_MIMESTYLE)) { 85247131Sbrooks case VIS_HTTP1808|VIS_MIMESTYLE: 86247131Sbrooks case VIS_HTTP1866|VIS_MIMESTYLE: 87247131Sbrooks case VIS_HTTP1808|VIS_HTTP1866|VIS_MIMESTYLE: 88247131Sbrooks errx(EXIT_FAILURE, "Can't mix -m with -h and/or -H"); 89247131Sbrooks /*NOTREACHED*/ 90247131Sbrooks default: 91247131Sbrooks break; 92247131Sbrooks } 93247131Sbrooks 94247131Sbrooks if (*argv) 95247131Sbrooks while (*argv) { 96247131Sbrooks if ((fp = fopen(*argv, "r")) != NULL) 97247131Sbrooks process(fp, *argv, eflags); 98247131Sbrooks else 99247131Sbrooks warn("%s", *argv); 100247131Sbrooks argv++; 101247131Sbrooks } 102247131Sbrooks else 103247131Sbrooks process(stdin, "<stdin>", eflags); 104247131Sbrooks return EXIT_SUCCESS; 105247131Sbrooks} 106247131Sbrooks 107247131Sbrooksstatic void 108247131Sbrooksprocess(FILE *fp, const char *filename, int eflags) 109247131Sbrooks{ 110247131Sbrooks int offset = 0, c, ret; 111247131Sbrooks int state = 0; 112247131Sbrooks char outc; 113247131Sbrooks 114247131Sbrooks while ((c = getc(fp)) != EOF) { 115247131Sbrooks offset++; 116247131Sbrooks again: 117247131Sbrooks switch(ret = unvis(&outc, (char)c, &state, eflags)) { 118247131Sbrooks case UNVIS_VALID: 119247131Sbrooks (void)putchar(outc); 120247131Sbrooks break; 121247131Sbrooks case UNVIS_VALIDPUSH: 122247131Sbrooks (void)putchar(outc); 123247131Sbrooks goto again; 124247131Sbrooks case UNVIS_SYNBAD: 125247131Sbrooks warnx("%s: offset: %d: can't decode", filename, offset); 126247131Sbrooks state = 0; 127247131Sbrooks break; 128247131Sbrooks case 0: 129247131Sbrooks case UNVIS_NOCHAR: 130247131Sbrooks break; 131247131Sbrooks default: 132247131Sbrooks errx(1, "bad return value (%d), can't happen", ret); 133247131Sbrooks /* NOTREACHED */ 134247131Sbrooks } 135247131Sbrooks } 136247131Sbrooks if (unvis(&outc, (char)0, &state, eflags | UNVIS_END) == UNVIS_VALID) 137247131Sbrooks (void)putchar(outc); 138247131Sbrooks} 139