1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <dirent.h>
34*7c478bd9Sstevel@tonic-gate #include <regexpr.h>
35*7c478bd9Sstevel@tonic-gate #include <string.h>
36*7c478bd9Sstevel@tonic-gate #include <errno.h>
37*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
38*7c478bd9Sstevel@tonic-gate #include <locale.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
43*7c478bd9Sstevel@tonic-gate #include <unistd.h>
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate #define P_locale "/usr/lib/locale/"
46*7c478bd9Sstevel@tonic-gate #define L_locale (sizeof (P_locale))
47*7c478bd9Sstevel@tonic-gate #define MESSAGES "/LC_MESSAGES/"
48*7c478bd9Sstevel@tonic-gate #define ESIZE BUFSIZ
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate /* External functions */
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate extern int getopt();
53*7c478bd9Sstevel@tonic-gate extern void exit();
54*7c478bd9Sstevel@tonic-gate extern char *strecpy();
55*7c478bd9Sstevel@tonic-gate extern char *strrchr();
56*7c478bd9Sstevel@tonic-gate extern char *strchr();
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate /* External variables */
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate extern char *optarg;
62*7c478bd9Sstevel@tonic-gate extern int optind;
63*7c478bd9Sstevel@tonic-gate
64*7c478bd9Sstevel@tonic-gate /* Internal functions */
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate static void usage();
67*7c478bd9Sstevel@tonic-gate static void prnt_str();
68*7c478bd9Sstevel@tonic-gate static int attach();
69*7c478bd9Sstevel@tonic-gate static void find_msgs();
70*7c478bd9Sstevel@tonic-gate static char *syserr();
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate /* Internal variables */
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gate static char *cmdname; /* points to the name of the command */
75*7c478bd9Sstevel@tonic-gate static int lflg; /* set if locale is specified on command line */
76*7c478bd9Sstevel@tonic-gate static int mflg; /* set if message file is specified on */
77*7c478bd9Sstevel@tonic-gate /* command line */
78*7c478bd9Sstevel@tonic-gate static char locale[15]; /* save name of current locale */
79*7c478bd9Sstevel@tonic-gate static char *msgfile; /* points to the argument immediately */
80*7c478bd9Sstevel@tonic-gate /* following the m option */
81*7c478bd9Sstevel@tonic-gate static char *text; /* pointer to search pattern */
82*7c478bd9Sstevel@tonic-gate static int textflg; /* set if text pattern is specified on */
83*7c478bd9Sstevel@tonic-gate /* command line */
84*7c478bd9Sstevel@tonic-gate static int sflg; /* set if the s option is specified */
85*7c478bd9Sstevel@tonic-gate static char *fname; /* points to message file name */
86*7c478bd9Sstevel@tonic-gate static int msgnum; /* message number */
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)89*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate int ch;
92*7c478bd9Sstevel@tonic-gate char *end;
93*7c478bd9Sstevel@tonic-gate int addr;
94*7c478bd9Sstevel@tonic-gate int len;
95*7c478bd9Sstevel@tonic-gate int len1;
96*7c478bd9Sstevel@tonic-gate int fd;
97*7c478bd9Sstevel@tonic-gate size_t size;
98*7c478bd9Sstevel@tonic-gate char pathname[128];
99*7c478bd9Sstevel@tonic-gate char *cp;
100*7c478bd9Sstevel@tonic-gate char ebuf[ESIZE];
101*7c478bd9Sstevel@tonic-gate DIR *dirp;
102*7c478bd9Sstevel@tonic-gate struct dirent *dp;
103*7c478bd9Sstevel@tonic-gate
104*7c478bd9Sstevel@tonic-gate /* find last level of path in command name */
105*7c478bd9Sstevel@tonic-gate if (cmdname = strrchr(*argv, '/'))
106*7c478bd9Sstevel@tonic-gate ++cmdname;
107*7c478bd9Sstevel@tonic-gate else
108*7c478bd9Sstevel@tonic-gate cmdname = *argv;
109*7c478bd9Sstevel@tonic-gate
110*7c478bd9Sstevel@tonic-gate /* parse command line */
111*7c478bd9Sstevel@tonic-gate while ((ch = getopt(argc, argv, "sl:m:")) != -1)
112*7c478bd9Sstevel@tonic-gate switch (ch) {
113*7c478bd9Sstevel@tonic-gate case 'l':
114*7c478bd9Sstevel@tonic-gate lflg++;
115*7c478bd9Sstevel@tonic-gate (void) strcpy(locale, optarg);
116*7c478bd9Sstevel@tonic-gate continue;
117*7c478bd9Sstevel@tonic-gate case 'm':
118*7c478bd9Sstevel@tonic-gate mflg++;
119*7c478bd9Sstevel@tonic-gate msgfile = optarg;
120*7c478bd9Sstevel@tonic-gate continue;
121*7c478bd9Sstevel@tonic-gate case 's':
122*7c478bd9Sstevel@tonic-gate sflg++;
123*7c478bd9Sstevel@tonic-gate continue;
124*7c478bd9Sstevel@tonic-gate default:
125*7c478bd9Sstevel@tonic-gate usage();
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate if (mflg && optind < argc) {
128*7c478bd9Sstevel@tonic-gate text = argv[optind++];
129*7c478bd9Sstevel@tonic-gate textflg++;
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate if (optind != argc)
132*7c478bd9Sstevel@tonic-gate usage();
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate /* create full path name to message files */
135*7c478bd9Sstevel@tonic-gate if (!lflg)
136*7c478bd9Sstevel@tonic-gate (void) strcpy(locale, setlocale(LC_MESSAGES, ""));
137*7c478bd9Sstevel@tonic-gate (void) strcpy(pathname, P_locale);
138*7c478bd9Sstevel@tonic-gate (void) strcpy(&pathname[L_locale - 1], locale);
139*7c478bd9Sstevel@tonic-gate (void) strcat(pathname, MESSAGES);
140*7c478bd9Sstevel@tonic-gate len = strlen(pathname);
141*7c478bd9Sstevel@tonic-gate
142*7c478bd9Sstevel@tonic-gate if (textflg) {
143*7c478bd9Sstevel@tonic-gate /* compile regular expression */
144*7c478bd9Sstevel@tonic-gate if (compile(text, &ebuf[0], &ebuf[ESIZE]) == (char *)NULL) {
145*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
146*7c478bd9Sstevel@tonic-gate "%s: ERROR: regular expression compile failed\n",
147*7c478bd9Sstevel@tonic-gate cmdname);
148*7c478bd9Sstevel@tonic-gate exit(1);
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate
152*7c478bd9Sstevel@tonic-gate /* access message files */
153*7c478bd9Sstevel@tonic-gate if (mflg) {
154*7c478bd9Sstevel@tonic-gate end = msgfile + strlen(msgfile) + 1;
155*7c478bd9Sstevel@tonic-gate if (*msgfile == ',' || *(end - 2) == ',')
156*7c478bd9Sstevel@tonic-gate usage();
157*7c478bd9Sstevel@tonic-gate while ((fname = strtok(msgfile, ",\0")) != NULL) {
158*7c478bd9Sstevel@tonic-gate if (strchr(fname, '/') != (char *)NULL) {
159*7c478bd9Sstevel@tonic-gate cp = fname;
160*7c478bd9Sstevel@tonic-gate len1 = 0;
161*7c478bd9Sstevel@tonic-gate } else {
162*7c478bd9Sstevel@tonic-gate cp = pathname;
163*7c478bd9Sstevel@tonic-gate len1 = len;
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate msgfile = msgfile + strlen(fname) + 1;
166*7c478bd9Sstevel@tonic-gate if ((addr = attach(cp, len1, &fd, &size)) == -1) {
167*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
168*7c478bd9Sstevel@tonic-gate "%s: ERROR: failed to access message file '%s'\n", cmdname, cp);
169*7c478bd9Sstevel@tonic-gate if (end != msgfile)
170*7c478bd9Sstevel@tonic-gate continue;
171*7c478bd9Sstevel@tonic-gate else
172*7c478bd9Sstevel@tonic-gate break;
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate find_msgs(addr, ebuf);
175*7c478bd9Sstevel@tonic-gate (void) munmap((caddr_t)addr, size);
176*7c478bd9Sstevel@tonic-gate (void) close(fd);
177*7c478bd9Sstevel@tonic-gate if (end == msgfile)
178*7c478bd9Sstevel@tonic-gate break;
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate } else { /* end if (mflg) */
181*7c478bd9Sstevel@tonic-gate if ((dirp = opendir(pathname)) == NULL) {
182*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ERROR: %s %s\n",
183*7c478bd9Sstevel@tonic-gate cmdname, pathname, syserr());
184*7c478bd9Sstevel@tonic-gate exit(1);
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) {
187*7c478bd9Sstevel@tonic-gate if (dp->d_name[0] == '.')
188*7c478bd9Sstevel@tonic-gate continue;
189*7c478bd9Sstevel@tonic-gate fname = dp->d_name;
190*7c478bd9Sstevel@tonic-gate if ((addr = attach(pathname, len, &fd, &size)) == -1) {
191*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
192*7c478bd9Sstevel@tonic-gate "%s: ERROR: failed to access message file '%s'\n", cmdname, pathname);
193*7c478bd9Sstevel@tonic-gate continue;
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate find_msgs(addr, ebuf);
196*7c478bd9Sstevel@tonic-gate (void) munmap((caddr_t)addr, size);
197*7c478bd9Sstevel@tonic-gate (void) close(fd);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate (void) closedir(dirp);
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate return (0);
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate /* print usage message */
206*7c478bd9Sstevel@tonic-gate static void
usage()207*7c478bd9Sstevel@tonic-gate usage()
208*7c478bd9Sstevel@tonic-gate {
209*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
210*7c478bd9Sstevel@tonic-gate "usage: srchtxt [-s]\n srchtxt [-s] -l locale\n"
211*7c478bd9Sstevel@tonic-gate " srchtxt [-s] [-l locale] [-m msgfile,...] [text]\n");
212*7c478bd9Sstevel@tonic-gate exit(1);
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate
215*7c478bd9Sstevel@tonic-gate /*
216*7c478bd9Sstevel@tonic-gate * print string - non-graphic characters are printed as alphabetic
217*7c478bd9Sstevel@tonic-gate * escape sequences
218*7c478bd9Sstevel@tonic-gate */
219*7c478bd9Sstevel@tonic-gate static void
prnt_str(instring)220*7c478bd9Sstevel@tonic-gate prnt_str(instring)
221*7c478bd9Sstevel@tonic-gate char *instring;
222*7c478bd9Sstevel@tonic-gate {
223*7c478bd9Sstevel@tonic-gate char outstring[1024];
224*7c478bd9Sstevel@tonic-gate
225*7c478bd9Sstevel@tonic-gate (void) strecpy(outstring, instring, NULL);
226*7c478bd9Sstevel@tonic-gate if (sflg)
227*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", outstring);
228*7c478bd9Sstevel@tonic-gate else
229*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "<%s:%d>%s\n", fname, msgnum, outstring);
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate
232*7c478bd9Sstevel@tonic-gate /* mmap a message file to the address space */
233*7c478bd9Sstevel@tonic-gate static int
attach(path,len,fdescr,size)234*7c478bd9Sstevel@tonic-gate attach(path, len, fdescr, size)
235*7c478bd9Sstevel@tonic-gate char *path;
236*7c478bd9Sstevel@tonic-gate int len;
237*7c478bd9Sstevel@tonic-gate int *fdescr;
238*7c478bd9Sstevel@tonic-gate size_t *size;
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate int fd = -1;
241*7c478bd9Sstevel@tonic-gate caddr_t addr;
242*7c478bd9Sstevel@tonic-gate struct stat sb;
243*7c478bd9Sstevel@tonic-gate
244*7c478bd9Sstevel@tonic-gate (void) strcpy(&path[len], fname);
245*7c478bd9Sstevel@tonic-gate if ((fd = open(path, O_RDONLY)) != -1 &&
246*7c478bd9Sstevel@tonic-gate fstat(fd, &sb) != -1 &&
247*7c478bd9Sstevel@tonic-gate (addr = mmap(0, sb.st_size,
248*7c478bd9Sstevel@tonic-gate PROT_READ, MAP_SHARED,
249*7c478bd9Sstevel@tonic-gate fd, 0)) != (caddr_t)-1) {
250*7c478bd9Sstevel@tonic-gate *fdescr = fd;
251*7c478bd9Sstevel@tonic-gate *size = sb.st_size;
252*7c478bd9Sstevel@tonic-gate return ((int)addr);
253*7c478bd9Sstevel@tonic-gate } else {
254*7c478bd9Sstevel@tonic-gate if (fd == -1)
255*7c478bd9Sstevel@tonic-gate (void) close(fd);
256*7c478bd9Sstevel@tonic-gate return (-1);
257*7c478bd9Sstevel@tonic-gate }
258*7c478bd9Sstevel@tonic-gate }
259*7c478bd9Sstevel@tonic-gate
260*7c478bd9Sstevel@tonic-gate
261*7c478bd9Sstevel@tonic-gate /* find messages in message files */
262*7c478bd9Sstevel@tonic-gate static void
find_msgs(addr,regexpr)263*7c478bd9Sstevel@tonic-gate find_msgs(addr, regexpr)
264*7c478bd9Sstevel@tonic-gate int addr;
265*7c478bd9Sstevel@tonic-gate char *regexpr;
266*7c478bd9Sstevel@tonic-gate {
267*7c478bd9Sstevel@tonic-gate int num_msgs;
268*7c478bd9Sstevel@tonic-gate char *msg;
269*7c478bd9Sstevel@tonic-gate
270*7c478bd9Sstevel@tonic-gate num_msgs = *(int *)addr;
271*7c478bd9Sstevel@tonic-gate for (msgnum = 1; msgnum <= num_msgs; msgnum++) {
272*7c478bd9Sstevel@tonic-gate msg = (char *)(*(int *)(addr + sizeof (int) * msgnum) + addr);
273*7c478bd9Sstevel@tonic-gate if (textflg)
274*7c478bd9Sstevel@tonic-gate if (step(msg, regexpr)) {
275*7c478bd9Sstevel@tonic-gate prnt_str(msg);
276*7c478bd9Sstevel@tonic-gate continue;
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate else
279*7c478bd9Sstevel@tonic-gate continue;
280*7c478bd9Sstevel@tonic-gate prnt_str(msg);
281*7c478bd9Sstevel@tonic-gate }
282*7c478bd9Sstevel@tonic-gate }
283*7c478bd9Sstevel@tonic-gate
284*7c478bd9Sstevel@tonic-gate /* print description of error */
285*7c478bd9Sstevel@tonic-gate static char *
syserr()286*7c478bd9Sstevel@tonic-gate syserr()
287*7c478bd9Sstevel@tonic-gate {
288*7c478bd9Sstevel@tonic-gate return (strerror(errno));
289*7c478bd9Sstevel@tonic-gate }
290