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