1*5959d954SThomas Cort /* $NetBSD: what.c,v 1.11 2011/09/06 18:45:49 joerg Exp $ */
2*5959d954SThomas Cort
3*5959d954SThomas Cort /*
4*5959d954SThomas Cort * Copyright (c) 1980, 1988, 1993
5*5959d954SThomas Cort * The Regents of the University of California. All rights reserved.
6*5959d954SThomas Cort *
7*5959d954SThomas Cort * Redistribution and use in source and binary forms, with or without
8*5959d954SThomas Cort * modification, are permitted provided that the following conditions
9*5959d954SThomas Cort * are met:
10*5959d954SThomas Cort * 1. Redistributions of source code must retain the above copyright
11*5959d954SThomas Cort * notice, this list of conditions and the following disclaimer.
12*5959d954SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
13*5959d954SThomas Cort * notice, this list of conditions and the following disclaimer in the
14*5959d954SThomas Cort * documentation and/or other materials provided with the distribution.
15*5959d954SThomas Cort * 3. Neither the name of the University nor the names of its contributors
16*5959d954SThomas Cort * may be used to endorse or promote products derived from this software
17*5959d954SThomas Cort * without specific prior written permission.
18*5959d954SThomas Cort *
19*5959d954SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*5959d954SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*5959d954SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*5959d954SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*5959d954SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*5959d954SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*5959d954SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*5959d954SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*5959d954SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*5959d954SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*5959d954SThomas Cort * SUCH DAMAGE.
30*5959d954SThomas Cort */
31*5959d954SThomas Cort
32*5959d954SThomas Cort #include <sys/cdefs.h>
33*5959d954SThomas Cort #ifndef lint
34*5959d954SThomas Cort __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\
35*5959d954SThomas Cort The Regents of the University of California. All rights reserved.");
36*5959d954SThomas Cort #endif /* not lint */
37*5959d954SThomas Cort
38*5959d954SThomas Cort #ifndef lint
39*5959d954SThomas Cort #if 0
40*5959d954SThomas Cort static char sccsid[] = "@(#)what.c 8.1 (Berkeley) 6/6/93";
41*5959d954SThomas Cort #endif
42*5959d954SThomas Cort __RCSID("$NetBSD: what.c,v 1.11 2011/09/06 18:45:49 joerg Exp $");
43*5959d954SThomas Cort #endif /* not lint */
44*5959d954SThomas Cort
45*5959d954SThomas Cort #include <locale.h>
46*5959d954SThomas Cort #include <stdio.h>
47*5959d954SThomas Cort #include <stdlib.h>
48*5959d954SThomas Cort #include <unistd.h>
49*5959d954SThomas Cort
50*5959d954SThomas Cort static void search(void);
51*5959d954SThomas Cort __dead static void usage(void);
52*5959d954SThomas Cort
53*5959d954SThomas Cort static int matches;
54*5959d954SThomas Cort static int sflag;
55*5959d954SThomas Cort
56*5959d954SThomas Cort /*
57*5959d954SThomas Cort * what
58*5959d954SThomas Cort */
59*5959d954SThomas Cort /* ARGSUSED */
60*5959d954SThomas Cort int
main(int argc,char ** argv)61*5959d954SThomas Cort main(int argc, char **argv)
62*5959d954SThomas Cort {
63*5959d954SThomas Cort int c;
64*5959d954SThomas Cort
65*5959d954SThomas Cort (void)setlocale(LC_ALL, "");
66*5959d954SThomas Cort
67*5959d954SThomas Cort matches = sflag = 0;
68*5959d954SThomas Cort while ((c = getopt(argc, argv, "s")) != -1) {
69*5959d954SThomas Cort switch (c) {
70*5959d954SThomas Cort case 's':
71*5959d954SThomas Cort sflag = 1;
72*5959d954SThomas Cort break;
73*5959d954SThomas Cort case '?':
74*5959d954SThomas Cort default:
75*5959d954SThomas Cort usage();
76*5959d954SThomas Cort }
77*5959d954SThomas Cort }
78*5959d954SThomas Cort argc -= optind;
79*5959d954SThomas Cort argv += optind;
80*5959d954SThomas Cort
81*5959d954SThomas Cort if (argc < 1) {
82*5959d954SThomas Cort usage();
83*5959d954SThomas Cort } else do {
84*5959d954SThomas Cort if (freopen(*argv, "r", stdin) == NULL) {
85*5959d954SThomas Cort perror(*argv);
86*5959d954SThomas Cort exit(matches ? EXIT_SUCCESS : 1);
87*5959d954SThomas Cort }
88*5959d954SThomas Cort printf("%s\n", *argv);
89*5959d954SThomas Cort search();
90*5959d954SThomas Cort } while (*++argv != NULL);
91*5959d954SThomas Cort
92*5959d954SThomas Cort /* Note: the standard explicitly specifies an exit status of 1. */
93*5959d954SThomas Cort exit(matches ? EXIT_SUCCESS : 1);
94*5959d954SThomas Cort /* NOTREACHED */
95*5959d954SThomas Cort }
96*5959d954SThomas Cort
97*5959d954SThomas Cort static void
search(void)98*5959d954SThomas Cort search(void)
99*5959d954SThomas Cort {
100*5959d954SThomas Cort int c;
101*5959d954SThomas Cort
102*5959d954SThomas Cort while ((c = getchar()) != EOF) {
103*5959d954SThomas Cort loop: if (c != '@')
104*5959d954SThomas Cort continue;
105*5959d954SThomas Cort if ((c = getchar()) != '(')
106*5959d954SThomas Cort goto loop;
107*5959d954SThomas Cort if ((c = getchar()) != '#')
108*5959d954SThomas Cort goto loop;
109*5959d954SThomas Cort if ((c = getchar()) != ')')
110*5959d954SThomas Cort goto loop;
111*5959d954SThomas Cort putchar('\t');
112*5959d954SThomas Cort while ((c = getchar()) != EOF && c != '\0' && c != '"' &&
113*5959d954SThomas Cort c != '>' && c != '\n' && c != '\\')
114*5959d954SThomas Cort putchar(c);
115*5959d954SThomas Cort putchar('\n');
116*5959d954SThomas Cort matches++;
117*5959d954SThomas Cort if (sflag)
118*5959d954SThomas Cort break;
119*5959d954SThomas Cort }
120*5959d954SThomas Cort }
121*5959d954SThomas Cort
122*5959d954SThomas Cort static void
usage(void)123*5959d954SThomas Cort usage(void)
124*5959d954SThomas Cort {
125*5959d954SThomas Cort
126*5959d954SThomas Cort (void)fprintf(stderr, "usage: what [-s] file ...\n");
127*5959d954SThomas Cort exit(1);
128*5959d954SThomas Cort }
129