xref: /onnv-gate/usr/src/cmd/asa/asa.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <ctype.h>
31*0Sstevel@tonic-gate #include <limits.h>
32*0Sstevel@tonic-gate #include <locale.h>
33*0Sstevel@tonic-gate #include <nl_types.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <unistd.h>
38*0Sstevel@tonic-gate #include <errno.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #define	FF '\f'
41*0Sstevel@tonic-gate #define	NL '\n'
42*0Sstevel@tonic-gate #define	NUL '\0'
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static	void usage(void);
45*0Sstevel@tonic-gate static	void disp_file(FILE *f, char *filename);
46*0Sstevel@tonic-gate static	FILE *get_next_file(int, char *, char *[], int);
47*0Sstevel@tonic-gate static	void finish(int need_a_newline);
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate int estatus = 0;	/* exit status */
50*0Sstevel@tonic-gate int i;			/* argv index */
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate int
main(int argc,char * argv[])53*0Sstevel@tonic-gate main(int argc, char *argv[])
54*0Sstevel@tonic-gate {
55*0Sstevel@tonic-gate 	int c;
56*0Sstevel@tonic-gate 	int form_feeds		= 0;
57*0Sstevel@tonic-gate 	int need_a_newline	= 0;
58*0Sstevel@tonic-gate 	char *filename		= NULL;
59*0Sstevel@tonic-gate 	FILE *f;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
62*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
63*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"  /* Use this only if it were not */
64*0Sstevel@tonic-gate #endif
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "f")) != EOF) {
69*0Sstevel@tonic-gate 		switch (c) {
70*0Sstevel@tonic-gate 		case 'f':
71*0Sstevel@tonic-gate 			form_feeds = 1;
72*0Sstevel@tonic-gate 			break;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 		case '?':
75*0Sstevel@tonic-gate 			usage();
76*0Sstevel@tonic-gate 		}
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	i = optind;
80*0Sstevel@tonic-gate 	if (argc <= i) {
81*0Sstevel@tonic-gate 		filename = NULL;
82*0Sstevel@tonic-gate 		f = stdin;
83*0Sstevel@tonic-gate 	} else {
84*0Sstevel@tonic-gate 		f = get_next_file(need_a_newline, filename, argv, argc);
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	need_a_newline = 0;
88*0Sstevel@tonic-gate 	for (;;) {
89*0Sstevel@tonic-gate 		/* interpret the first character in the line */
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 		c = getc(f);
92*0Sstevel@tonic-gate 		switch (c) {
93*0Sstevel@tonic-gate 		case EOF:
94*0Sstevel@tonic-gate 			disp_file(f, filename);
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 			if (i >= argc)
97*0Sstevel@tonic-gate 				finish(need_a_newline);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 			f = get_next_file(need_a_newline, filename, argv, argc);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 			if (need_a_newline) {
102*0Sstevel@tonic-gate 				(void) putchar(NL);
103*0Sstevel@tonic-gate 				need_a_newline = 0;
104*0Sstevel@tonic-gate 			}
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 			if (form_feeds)
107*0Sstevel@tonic-gate 				(void) putchar(FF);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 			continue;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 		case NL:
112*0Sstevel@tonic-gate 			if (need_a_newline)
113*0Sstevel@tonic-gate 				(void) putchar(NL);
114*0Sstevel@tonic-gate 			need_a_newline = 1;
115*0Sstevel@tonic-gate 			continue;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 		case '+':
118*0Sstevel@tonic-gate 			if (need_a_newline)
119*0Sstevel@tonic-gate 				(void) putchar('\r');
120*0Sstevel@tonic-gate 			break;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		case '0':
123*0Sstevel@tonic-gate 			if (need_a_newline)
124*0Sstevel@tonic-gate 				(void) putchar(NL);
125*0Sstevel@tonic-gate 			(void) putchar(NL);
126*0Sstevel@tonic-gate 			break;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 		case '1':
129*0Sstevel@tonic-gate 			if (need_a_newline)
130*0Sstevel@tonic-gate 				(void) putchar(NL);
131*0Sstevel@tonic-gate 			(void) putchar(FF);
132*0Sstevel@tonic-gate 			break;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		case ' ':
135*0Sstevel@tonic-gate 		default:
136*0Sstevel@tonic-gate 			if (need_a_newline)
137*0Sstevel@tonic-gate 				(void) putchar(NL);
138*0Sstevel@tonic-gate 			break;
139*0Sstevel@tonic-gate 		}
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 		need_a_newline = 0;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 		for (;;) {
144*0Sstevel@tonic-gate 			c = getc(f);
145*0Sstevel@tonic-gate 			if (c == NL) {
146*0Sstevel@tonic-gate 				need_a_newline = 1;
147*0Sstevel@tonic-gate 				break;
148*0Sstevel@tonic-gate 			} else if (c == EOF) {
149*0Sstevel@tonic-gate 				disp_file(f, filename);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 				if (i >= argc)
152*0Sstevel@tonic-gate 					finish(need_a_newline);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 				f = get_next_file(need_a_newline, filename,
155*0Sstevel@tonic-gate 				    argv, argc);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 				if (form_feeds) {
158*0Sstevel@tonic-gate 					(void) putchar(NL);
159*0Sstevel@tonic-gate 					(void) putchar(FF);
160*0Sstevel@tonic-gate 					need_a_newline = 0;
161*0Sstevel@tonic-gate 					break;
162*0Sstevel@tonic-gate 				}
163*0Sstevel@tonic-gate 			} else {
164*0Sstevel@tonic-gate 				(void) putchar(c);
165*0Sstevel@tonic-gate 			}
166*0Sstevel@tonic-gate 		}
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 	/* NOTREACHED */
169*0Sstevel@tonic-gate 	return (0);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate static void
usage(void)173*0Sstevel@tonic-gate usage(void)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage: asa [-f] [-|file...]\n"));
176*0Sstevel@tonic-gate 	exit(1);
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate static	void
disp_file(FILE * f,char * filename)181*0Sstevel@tonic-gate disp_file(FILE *f, char *filename)
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	if (ferror(f)) {
185*0Sstevel@tonic-gate 		int	serror = errno;
186*0Sstevel@tonic-gate 		if (filename) {
187*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
188*0Sstevel@tonic-gate 			    "asa: read error on file %s\n"), filename);
189*0Sstevel@tonic-gate 		} else {
190*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
191*0Sstevel@tonic-gate 			    "asa: read error on standard input\n"));
192*0Sstevel@tonic-gate 		}
193*0Sstevel@tonic-gate 		errno = serror;
194*0Sstevel@tonic-gate 		perror("");
195*0Sstevel@tonic-gate 		estatus = 1;
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	(void) fclose(f);
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate static	FILE *
get_next_file(int need_a_newline,char * filename,char * argv[],int argc)203*0Sstevel@tonic-gate get_next_file(int need_a_newline, char *filename, char *argv[], int argc)
204*0Sstevel@tonic-gate {
205*0Sstevel@tonic-gate 	FILE	*f;
206*0Sstevel@tonic-gate 	if (strcmp(argv[i], "-") == 0) {
207*0Sstevel@tonic-gate 		filename = NULL;
208*0Sstevel@tonic-gate 		f = stdin;
209*0Sstevel@tonic-gate 	} else {
210*0Sstevel@tonic-gate 		/*
211*0Sstevel@tonic-gate 		 * Process each file operand.  If unsuccessful, affect the
212*0Sstevel@tonic-gate 		 * exit status and continue processing the next operand.
213*0Sstevel@tonic-gate 		 */
214*0Sstevel@tonic-gate 		filename = argv[i];
215*0Sstevel@tonic-gate 		while ((f = fopen(filename, "r")) == NULL) {
216*0Sstevel@tonic-gate 			int	serror = errno;
217*0Sstevel@tonic-gate 			(void) fprintf(stderr,
218*0Sstevel@tonic-gate 				gettext("asa: cannot open %s:"), filename);
219*0Sstevel@tonic-gate 			errno = serror;
220*0Sstevel@tonic-gate 			perror("");
221*0Sstevel@tonic-gate 			estatus = 1;
222*0Sstevel@tonic-gate 			if (++i < argc) {
223*0Sstevel@tonic-gate 				if (strcmp(argv[i], "-") == 0) {
224*0Sstevel@tonic-gate 					filename = NULL;
225*0Sstevel@tonic-gate 					f = stdin;
226*0Sstevel@tonic-gate 					break;
227*0Sstevel@tonic-gate 				} else {
228*0Sstevel@tonic-gate 					filename = argv[i];
229*0Sstevel@tonic-gate 				}
230*0Sstevel@tonic-gate 			} else {
231*0Sstevel@tonic-gate 				finish(need_a_newline);
232*0Sstevel@tonic-gate 			}
233*0Sstevel@tonic-gate 		}
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 	++i;
236*0Sstevel@tonic-gate 	return (f);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate static	void
finish(int need_a_newline)240*0Sstevel@tonic-gate finish(int need_a_newline)
241*0Sstevel@tonic-gate {
242*0Sstevel@tonic-gate 	if (need_a_newline)
243*0Sstevel@tonic-gate 		(void) putchar(NL);
244*0Sstevel@tonic-gate 	exit(estatus);
245*0Sstevel@tonic-gate }
246