xref: /freebsd/usr.bin/asa/asa.c (revision aa7957345732816fb0ba8308798d2f79f45597f9)
1 /*	$NetBSD: asa.c,v 1.11 1997/09/20 14:55:00 lukem Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 1993,94 Winning Strategies, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Winning Strategies, Inc.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #if 0
37 #ifndef lint
38 __RCSID("$NetBSD: asa.c,v 1.11 1997/09/20 14:55:00 lukem Exp $");
39 #endif
40 #endif
41 __FBSDID("$FreeBSD$");
42 
43 #include <err.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 static void asa(FILE *);
50 static void usage(void) __dead2;
51 
52 int
53 main(int argc, char *argv[])
54 {
55 	int ch, exval;
56 	FILE *fp;
57 	const char *fn;
58 
59 	while ((ch = getopt(argc, argv, "")) != -1) {
60 		switch (ch) {
61 		case '?':
62 		default:
63 			usage();
64 			/*NOTREACHED*/
65 		}
66 	}
67 	argc -= optind;
68 	argv += optind;
69 
70 	exval = 0;
71 	if (argc == 0)
72 		asa(stdin);
73 	else {
74 		while ((fn = *argv++) != NULL) {
75 			if (strcmp(fn, "-") == 0) {
76 				asa(stdin);
77 			} else {
78 				if ((fp = fopen(fn, "r")) == NULL) {
79 					warn("%s", fn);
80 					exval = 1;
81 					continue;
82 				}
83 				asa(fp);
84 				fclose(fp);
85 			}
86 		}
87 	}
88 
89 	if (fflush(stdout) != 0)
90 		err(1, "stdout");
91 
92 	exit(exval);
93 }
94 
95 static void
96 usage(void)
97 {
98 
99 	fprintf(stderr, "usage: asa [file ...]\n");
100 	exit(1);
101 }
102 
103 static void
104 asa(FILE *f)
105 {
106 	size_t len;
107 	char *buf;
108 
109 	if ((buf = fgetln(f, &len)) != NULL) {
110 		if (buf[len - 1] == '\n')
111 			buf[--len] = '\0';
112 		/* special case the first line */
113 		switch (buf[0]) {
114 		case '0':
115 			putchar('\n');
116 			break;
117 		case '1':
118 			putchar('\f');
119 			break;
120 		}
121 
122 		if (len > 1 && buf[0] && buf[1])
123 			printf("%.*s", (int)(len - 1), buf + 1);
124 
125 		while ((buf = fgetln(f, &len)) != NULL) {
126 			if (buf[len - 1] == '\n')
127 				buf[--len] = '\0';
128 			switch (buf[0]) {
129 			default:
130 			case ' ':
131 				putchar('\n');
132 				break;
133 			case '0':
134 				putchar('\n');
135 				putchar('\n');
136 				break;
137 			case '1':
138 				putchar('\f');
139 				break;
140 			case '+':
141 				putchar('\r');
142 				break;
143 			}
144 
145 			if (len > 1 && buf[0] && buf[1])
146 				printf("%.*s", (int)(len - 1), buf + 1);
147 		}
148 
149 		putchar('\n');
150 	}
151 
152 	if (ferror(stdout) != 0)
153 		err(1, "stdout");
154 }
155