xref: /original-bsd/usr.bin/unvis/unvis.c (revision 7dc4431a)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms is permitted
6  * provided that all copyright information, including this notice,
7  * is retained in all such forms, and that any documentation,
8  * advertising or other materials related to such distribution and
9  * use acknowledge that the software was
10  * developed by the University of California, Berkeley.  The name
11  * of the University may not be used to endorse or promote products
12  * derived from this software without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17  */
18 
19 #ifndef lint
20 char copyright[] =
21 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
22  All rights reserved.\n";
23 #endif /* not lint */
24 
25 #ifndef lint
26 static char sccsid[] = "@(#)unvis.c	1.1 (Berkeley) 05/23/90";
27 #endif /* not lint */
28 
29 #include <stdio.h>
30 #include <vis.h>
31 
32 char	*Program;
33 #define usage()	fprintf(stderr, "usage: %s %s\n", Program, USAGE)
34 #define USAGE "[file...]"
35 
36 main(argc, argv)
37 	char *argv[];
38 {
39 	FILE *fp;
40 	extern char *optarg;
41 	extern int optind;
42 	int ch;
43 
44 	Program = argv[0];
45 	while ((ch = getopt(argc, argv, "")) != EOF)
46 		switch((char)ch) {
47 		case '?':
48 		default:
49 			usage();
50 			exit(1);
51 		}
52 	argc -= optind;
53 	argv += optind;
54 
55 	if (*argv)
56 		while (*argv) {
57 			if ((fp=fopen(*argv, "r")) != NULL)
58 				process(fp, *argv);
59 			else
60 				syserror("%s", *argv);
61 			argv++;
62 		}
63 	else
64 		process(stdin, "<stdin>");
65 	exit(0);
66 }
67 
68 process(fp, filename)
69 	FILE *fp;
70 	char *filename;
71 {
72 	register int offset = 0, c, ret;
73 	int state = 0;
74 	char outc;
75 
76 	while ((c = getc(fp)) != EOF) {
77 		offset++;
78 	again:
79 		switch(ret = unvis(&outc, (char)c, &state, 0)) {
80 		case UNVIS_VALID:
81 			putchar(outc);
82 			break;
83 		case UNVIS_VALIDPUSH:
84 			putchar(outc);
85 			goto again;
86 		case UNVIS_SYNBAD:
87 			error("%s: offset: %d: can't decode", filename, offset);
88 			state = 0;
89 			break;
90 		case 0:
91 		case UNVIS_NOCHAR:
92 			break;
93 		default:
94 			error("bad return value (%d), can't happen", ret);
95 			exit(1);
96 		}
97 	}
98 	if (unvis(&outc, (char)0, &state, UNVIS_END) == UNVIS_VALID)
99 		putchar(outc);
100 }
101 
102 #include <varargs.h>
103 
104 error(va_alist)
105 	va_dcl
106 {
107 	char *fmt;
108 	va_list ap;
109 	extern errno;
110 
111 	fprintf(stderr, "%s: ", Program);
112 	va_start(ap);
113 	fmt = va_arg(ap, char *);
114 	(void) vfprintf(stderr, fmt, ap);
115 	va_end(ap);
116 	fprintf(stderr, "\n");
117 }
118 
119 syserror(va_alist)
120 	va_dcl
121 {
122 	char *fmt;
123 	va_list ap;
124 	extern errno;
125 
126 	fprintf(stderr, "%s: ", Program);
127 	va_start(ap);
128 	fmt = va_arg(ap, char *);
129 	(void) vfprintf(stderr, fmt, ap);
130 	va_end(ap);
131 	fprintf(stderr, ": %s\n", strerror(errno));
132 }
133