17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <dirent.h>
327c478bd9Sstevel@tonic-gate #include <regexpr.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <fcntl.h>
367c478bd9Sstevel@tonic-gate #include <locale.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/file.h>
397c478bd9Sstevel@tonic-gate #include <sys/mman.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #define P_locale "/usr/lib/locale/"
447c478bd9Sstevel@tonic-gate #define L_locale (sizeof (P_locale))
457c478bd9Sstevel@tonic-gate #define MESSAGES "/LC_MESSAGES/"
467c478bd9Sstevel@tonic-gate #define ESIZE BUFSIZ
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate /* External functions */
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate extern int getopt();
517c478bd9Sstevel@tonic-gate extern void exit();
527c478bd9Sstevel@tonic-gate extern char *strecpy();
537c478bd9Sstevel@tonic-gate extern char *strrchr();
547c478bd9Sstevel@tonic-gate extern char *strchr();
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /* External variables */
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate extern char *optarg;
607c478bd9Sstevel@tonic-gate extern int optind;
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate /* Internal functions */
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate static void usage();
657c478bd9Sstevel@tonic-gate static void prnt_str();
667c478bd9Sstevel@tonic-gate static int attach();
677c478bd9Sstevel@tonic-gate static void find_msgs();
687c478bd9Sstevel@tonic-gate static char *syserr();
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /* Internal variables */
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate static char *cmdname; /* points to the name of the command */
737c478bd9Sstevel@tonic-gate static int lflg; /* set if locale is specified on command line */
747c478bd9Sstevel@tonic-gate static int mflg; /* set if message file is specified on */
757c478bd9Sstevel@tonic-gate /* command line */
767c478bd9Sstevel@tonic-gate static char locale[15]; /* save name of current locale */
777c478bd9Sstevel@tonic-gate static char *msgfile; /* points to the argument immediately */
787c478bd9Sstevel@tonic-gate /* following the m option */
797c478bd9Sstevel@tonic-gate static char *text; /* pointer to search pattern */
807c478bd9Sstevel@tonic-gate static int textflg; /* set if text pattern is specified on */
817c478bd9Sstevel@tonic-gate /* command line */
827c478bd9Sstevel@tonic-gate static int sflg; /* set if the s option is specified */
837c478bd9Sstevel@tonic-gate static char *fname; /* points to message file name */
847c478bd9Sstevel@tonic-gate static int msgnum; /* message number */
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)877c478bd9Sstevel@tonic-gate main(int argc, char **argv)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate int ch;
907c478bd9Sstevel@tonic-gate char *end;
917c478bd9Sstevel@tonic-gate int addr;
927c478bd9Sstevel@tonic-gate int len;
937c478bd9Sstevel@tonic-gate int len1;
947c478bd9Sstevel@tonic-gate int fd;
957c478bd9Sstevel@tonic-gate size_t size;
967c478bd9Sstevel@tonic-gate char pathname[128];
977c478bd9Sstevel@tonic-gate char *cp;
987c478bd9Sstevel@tonic-gate char ebuf[ESIZE];
997c478bd9Sstevel@tonic-gate DIR *dirp;
1007c478bd9Sstevel@tonic-gate struct dirent *dp;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /* find last level of path in command name */
1037c478bd9Sstevel@tonic-gate if (cmdname = strrchr(*argv, '/'))
1047c478bd9Sstevel@tonic-gate ++cmdname;
1057c478bd9Sstevel@tonic-gate else
1067c478bd9Sstevel@tonic-gate cmdname = *argv;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /* parse command line */
1097c478bd9Sstevel@tonic-gate while ((ch = getopt(argc, argv, "sl:m:")) != -1)
1107c478bd9Sstevel@tonic-gate switch (ch) {
1117c478bd9Sstevel@tonic-gate case 'l':
1127c478bd9Sstevel@tonic-gate lflg++;
1137c478bd9Sstevel@tonic-gate (void) strcpy(locale, optarg);
1147c478bd9Sstevel@tonic-gate continue;
1157c478bd9Sstevel@tonic-gate case 'm':
1167c478bd9Sstevel@tonic-gate mflg++;
1177c478bd9Sstevel@tonic-gate msgfile = optarg;
1187c478bd9Sstevel@tonic-gate continue;
1197c478bd9Sstevel@tonic-gate case 's':
1207c478bd9Sstevel@tonic-gate sflg++;
1217c478bd9Sstevel@tonic-gate continue;
1227c478bd9Sstevel@tonic-gate default:
1237c478bd9Sstevel@tonic-gate usage();
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate if (mflg && optind < argc) {
1267c478bd9Sstevel@tonic-gate text = argv[optind++];
1277c478bd9Sstevel@tonic-gate textflg++;
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate if (optind != argc)
1307c478bd9Sstevel@tonic-gate usage();
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate /* create full path name to message files */
1337c478bd9Sstevel@tonic-gate if (!lflg)
1347c478bd9Sstevel@tonic-gate (void) strcpy(locale, setlocale(LC_MESSAGES, ""));
1357c478bd9Sstevel@tonic-gate (void) strcpy(pathname, P_locale);
1367c478bd9Sstevel@tonic-gate (void) strcpy(&pathname[L_locale - 1], locale);
1377c478bd9Sstevel@tonic-gate (void) strcat(pathname, MESSAGES);
1387c478bd9Sstevel@tonic-gate len = strlen(pathname);
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate if (textflg) {
1417c478bd9Sstevel@tonic-gate /* compile regular expression */
1427c478bd9Sstevel@tonic-gate if (compile(text, &ebuf[0], &ebuf[ESIZE]) == (char *)NULL) {
1437c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1447c478bd9Sstevel@tonic-gate "%s: ERROR: regular expression compile failed\n",
1457c478bd9Sstevel@tonic-gate cmdname);
1467c478bd9Sstevel@tonic-gate exit(1);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /* access message files */
1517c478bd9Sstevel@tonic-gate if (mflg) {
1527c478bd9Sstevel@tonic-gate end = msgfile + strlen(msgfile) + 1;
1537c478bd9Sstevel@tonic-gate if (*msgfile == ',' || *(end - 2) == ',')
1547c478bd9Sstevel@tonic-gate usage();
1557c478bd9Sstevel@tonic-gate while ((fname = strtok(msgfile, ",\0")) != NULL) {
1567c478bd9Sstevel@tonic-gate if (strchr(fname, '/') != (char *)NULL) {
1577c478bd9Sstevel@tonic-gate cp = fname;
1587c478bd9Sstevel@tonic-gate len1 = 0;
1597c478bd9Sstevel@tonic-gate } else {
1607c478bd9Sstevel@tonic-gate cp = pathname;
1617c478bd9Sstevel@tonic-gate len1 = len;
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate msgfile = msgfile + strlen(fname) + 1;
1647c478bd9Sstevel@tonic-gate if ((addr = attach(cp, len1, &fd, &size)) == -1) {
1657c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1667c478bd9Sstevel@tonic-gate "%s: ERROR: failed to access message file '%s'\n", cmdname, cp);
1677c478bd9Sstevel@tonic-gate if (end != msgfile)
1687c478bd9Sstevel@tonic-gate continue;
1697c478bd9Sstevel@tonic-gate else
1707c478bd9Sstevel@tonic-gate break;
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate find_msgs(addr, ebuf);
1737c478bd9Sstevel@tonic-gate (void) munmap((caddr_t)addr, size);
1747c478bd9Sstevel@tonic-gate (void) close(fd);
1757c478bd9Sstevel@tonic-gate if (end == msgfile)
1767c478bd9Sstevel@tonic-gate break;
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate } else { /* end if (mflg) */
1797c478bd9Sstevel@tonic-gate if ((dirp = opendir(pathname)) == NULL) {
1807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ERROR: %s %s\n",
1817c478bd9Sstevel@tonic-gate cmdname, pathname, syserr());
1827c478bd9Sstevel@tonic-gate exit(1);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) {
1857c478bd9Sstevel@tonic-gate if (dp->d_name[0] == '.')
1867c478bd9Sstevel@tonic-gate continue;
1877c478bd9Sstevel@tonic-gate fname = dp->d_name;
1887c478bd9Sstevel@tonic-gate if ((addr = attach(pathname, len, &fd, &size)) == -1) {
1897c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1907c478bd9Sstevel@tonic-gate "%s: ERROR: failed to access message file '%s'\n", cmdname, pathname);
1917c478bd9Sstevel@tonic-gate continue;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate find_msgs(addr, ebuf);
1947c478bd9Sstevel@tonic-gate (void) munmap((caddr_t)addr, size);
1957c478bd9Sstevel@tonic-gate (void) close(fd);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate (void) closedir(dirp);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate return (0);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /* print usage message */
2047c478bd9Sstevel@tonic-gate static void
usage()2057c478bd9Sstevel@tonic-gate usage()
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2087c478bd9Sstevel@tonic-gate "usage: srchtxt [-s]\n srchtxt [-s] -l locale\n"
2097c478bd9Sstevel@tonic-gate " srchtxt [-s] [-l locale] [-m msgfile,...] [text]\n");
2107c478bd9Sstevel@tonic-gate exit(1);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate * print string - non-graphic characters are printed as alphabetic
2157c478bd9Sstevel@tonic-gate * escape sequences
2167c478bd9Sstevel@tonic-gate */
2177c478bd9Sstevel@tonic-gate static void
prnt_str(instring)2187c478bd9Sstevel@tonic-gate prnt_str(instring)
2197c478bd9Sstevel@tonic-gate char *instring;
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate char outstring[1024];
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate (void) strecpy(outstring, instring, NULL);
2247c478bd9Sstevel@tonic-gate if (sflg)
2257c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", outstring);
2267c478bd9Sstevel@tonic-gate else
2277c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "<%s:%d>%s\n", fname, msgnum, outstring);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate /* mmap a message file to the address space */
2317c478bd9Sstevel@tonic-gate static int
attach(path,len,fdescr,size)2327c478bd9Sstevel@tonic-gate attach(path, len, fdescr, size)
2337c478bd9Sstevel@tonic-gate char *path;
2347c478bd9Sstevel@tonic-gate int len;
2357c478bd9Sstevel@tonic-gate int *fdescr;
2367c478bd9Sstevel@tonic-gate size_t *size;
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate int fd = -1;
2397c478bd9Sstevel@tonic-gate caddr_t addr;
2407c478bd9Sstevel@tonic-gate struct stat sb;
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate (void) strcpy(&path[len], fname);
2437c478bd9Sstevel@tonic-gate if ((fd = open(path, O_RDONLY)) != -1 &&
2447c478bd9Sstevel@tonic-gate fstat(fd, &sb) != -1 &&
2457c478bd9Sstevel@tonic-gate (addr = mmap(0, sb.st_size,
2467c478bd9Sstevel@tonic-gate PROT_READ, MAP_SHARED,
2477c478bd9Sstevel@tonic-gate fd, 0)) != (caddr_t)-1) {
2487c478bd9Sstevel@tonic-gate *fdescr = fd;
2497c478bd9Sstevel@tonic-gate *size = sb.st_size;
2507c478bd9Sstevel@tonic-gate return ((int)addr);
2517c478bd9Sstevel@tonic-gate } else {
2527c478bd9Sstevel@tonic-gate if (fd == -1)
2537c478bd9Sstevel@tonic-gate (void) close(fd);
2547c478bd9Sstevel@tonic-gate return (-1);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate /* find messages in message files */
2607c478bd9Sstevel@tonic-gate static void
find_msgs(addr,regexpr)2617c478bd9Sstevel@tonic-gate find_msgs(addr, regexpr)
2627c478bd9Sstevel@tonic-gate int addr;
2637c478bd9Sstevel@tonic-gate char *regexpr;
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate int num_msgs;
2667c478bd9Sstevel@tonic-gate char *msg;
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate num_msgs = *(int *)addr;
2697c478bd9Sstevel@tonic-gate for (msgnum = 1; msgnum <= num_msgs; msgnum++) {
2707c478bd9Sstevel@tonic-gate msg = (char *)(*(int *)(addr + sizeof (int) * msgnum) + addr);
271*8487916bSToomas Soome if (textflg) {
272*8487916bSToomas Soome if (step(msg, regexpr))
2737c478bd9Sstevel@tonic-gate prnt_str(msg);
274*8487916bSToomas Soome continue;
275*8487916bSToomas Soome }
276*8487916bSToomas Soome prnt_str(msg);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate /* print description of error */
2817c478bd9Sstevel@tonic-gate static char *
syserr()2827c478bd9Sstevel@tonic-gate syserr()
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate return (strerror(errno));
2857c478bd9Sstevel@tonic-gate }
286