blob: 8d2c915be55176e3403c6540d8d0017f6284cbda [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
Mark Whitleyd3721892000-06-28 22:00:26 +00002 * Mini grep implementation for busybox using libc regex.
Eric Andersenc4996011999-10-20 22:08:37 +00003 *
Erik Andersen61677fe2000-04-13 01:18:56 +00004 * Copyright (C) 1999,2000 by Lineo, inc.
Mark Whitleyd3721892000-06-28 22:00:26 +00005 * Written by Mark Whitley <markw@lineo.com>, <markw@enol.com>
Eric Andersencc8ed391999-10-05 16:24:54 +00006 *
Eric Andersen3e0fbae1999-10-19 06:02:44 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
Eric Andersencc8ed391999-10-05 16:24:54 +000021 */
22
Eric Andersencc8ed391999-10-05 16:24:54 +000023#include <stdio.h>
Mark Whitleyd3721892000-06-28 22:00:26 +000024#include <stdlib.h>
Eric Andersene9b527a2000-07-09 05:56:14 +000025#include <getopt.h>
Mark Whitleyd3721892000-06-28 22:00:26 +000026#include <regex.h>
27#include <string.h> /* for strerror() */
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <errno.h>
Mark Whitleyd3721892000-06-28 22:00:26 +000029#include "internal.h"
30
31extern int optind; /* in unistd.h */
32extern int errno; /* for use with strerror() */
Eric Andersencc8ed391999-10-05 16:24:54 +000033
Eric Andersene77ae3a1999-10-19 20:03:34 +000034static const char grep_usage[] =
Mark Whitleyd3721892000-06-28 22:00:26 +000035 "grep [-ihHnqvs] pattern [files...]\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000036#ifndef BB_FEATURE_TRIVIAL_HELP
37 "\nSearch for PATTERN in each FILE or standard input.\n\n"
Eric Andersen2086e3d2000-07-04 22:17:01 +000038 "Options:\n"
Mark Whitleyd3721892000-06-28 22:00:26 +000039 "\t-H\tprefix output lines with filename where match was found\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000040 "\t-h\tsuppress the prefixing filename on output\n"
41 "\t-i\tignore case distinctions\n"
42 "\t-n\tprint line number with output lines\n"
John Beppuf93a95d2000-04-24 18:07:30 +000043 "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n"
Mark Whitleyd3721892000-06-28 22:00:26 +000044 "\t-v\tselect non-matching lines\n"
45 "\t-s\tsuppress file open/read error messages\n\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000046#endif
47 ;
Eric Andersencc8ed391999-10-05 16:24:54 +000048
Mark Whitleyd3721892000-06-28 22:00:26 +000049/* options */
50static int ignore_case = 0;
51static int print_filename = 0;
52static int print_line_num = 0;
53static int be_quiet = 0;
54static int invert_search = 0;
55static int suppress_err_msgs = 0;
56
57/* globals */
58static regex_t regex; /* storage space for compiled regular expression */
59static int nmatches = 0; /* keeps track of the number of matches */
60static char *cur_file = NULL; /* the current file we are reading */
61
62
Mark Whitleyd3721892000-06-28 22:00:26 +000063static void print_matched_line(char *line, int linenum)
64{
65 if (print_filename)
66 printf("%s:", cur_file);
67 if (print_line_num)
68 printf("%i:", linenum);
69
70 printf("%s", line);
71}
72
73static void grep_file(FILE *file)
74{
75 char *line = NULL;
76 int ret;
77 int linenum = 0;
78
79 while ((line = get_line_from_file(file)) != NULL) {
80 linenum++;
81 ret = regexec(&regex, line, 0, NULL, 0);
82 if (ret == 0 && !invert_search) { /* match */
83
84 /* if we found a match but were told to be quiet, stop here and
85 * return success */
86 if (be_quiet) {
87 regfree(&regex);
88 exit(0);
89 }
90
91 nmatches++;
92
93 print_matched_line(line, linenum);
94
95 } else if (ret == REG_NOMATCH && invert_search) {
96 print_matched_line(line, linenum);
97 }
98
99 free(line);
100 }
101}
Eric Andersencc8ed391999-10-05 16:24:54 +0000102
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103extern int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000104{
Mark Whitleyd3721892000-06-28 22:00:26 +0000105 int opt;
106 int reflags;
107 int ret;
Eric Andersencc8ed391999-10-05 16:24:54 +0000108
Mark Whitleyd3721892000-06-28 22:00:26 +0000109 /* do special-case option parsing */
110 if (argv[1] && (strcmp(argv[1], "--help") == 0))
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000111 usage(grep_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112
Mark Whitleyd3721892000-06-28 22:00:26 +0000113 /* do normal option parsing */
114 while ((opt = getopt(argc, argv, "iHhnqvs")) > 0) {
115 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 case 'i':
Mark Whitleyd3721892000-06-28 22:00:26 +0000117 ignore_case++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000119 case 'H':
120 print_filename++;
121 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 case 'h':
Mark Whitleyd3721892000-06-28 22:00:26 +0000123 print_filename--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125 case 'n':
Mark Whitleyd3721892000-06-28 22:00:26 +0000126 print_line_num++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 case 'q':
Mark Whitleyd3721892000-06-28 22:00:26 +0000129 be_quiet++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 break;
John Beppuf93a95d2000-04-24 18:07:30 +0000131 case 'v':
Mark Whitleyd3721892000-06-28 22:00:26 +0000132 invert_search++;
John Beppuf93a95d2000-04-24 18:07:30 +0000133 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000134 case 's':
135 suppress_err_msgs++;
136 break;
Eric Andersen053b1462000-06-13 06:24:53 +0000137 }
Eric Andersen053b1462000-06-13 06:24:53 +0000138 }
139
Mark Whitleyd3721892000-06-28 22:00:26 +0000140 /* argv[optind] should be the regex pattern; no pattern, no worky */
141 if (argv[optind] == NULL)
142 usage(grep_usage);
143
Mark Whitley44735f82000-07-10 15:50:26 +0000144 /* compile the regular expression
145 * we're not going to mess with sub-expressions, and we need to
146 * treat newlines right. */
147 reflags = REG_NOSUB | REG_NEWLINE;
Mark Whitleyd3721892000-06-28 22:00:26 +0000148 if (ignore_case)
149 reflags |= REG_ICASE;
150 if ((ret = regcomp(&regex, argv[optind], reflags)) != 0) {
151 int errmsgsz = regerror(ret, &regex, NULL, 0);
152 char *errmsg = malloc(errmsgsz);
153 if (errmsg == NULL) {
154 fprintf(stderr, "grep: memory error\n");
155 regfree(&regex);
156 exit(1);
157 }
158 regerror(ret, &regex, errmsg, errmsgsz);
159 fprintf(stderr, "grep: %s\n", errmsg);
160 free(errmsg);
161 regfree(&regex);
162 exit(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 }
164
Mark Whitleyd3721892000-06-28 22:00:26 +0000165 /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If
166 * there is more than one file to grep, we will print the filenames */
167 if ((argc-1) - (optind+1) > 0)
168 print_filename++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169
Mark Whitley2e1148b2000-06-28 22:59:30 +0000170 /* If no files were specified, or '-' was specified, take input from
171 * stdin. Otherwise, we grep through all the files specified. */
Mark Whitley8bd891c2000-06-28 22:55:59 +0000172 if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000173 grep_file(stdin);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000174 } else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000175 int i;
176 FILE *file;
177 for (i = optind + 1; i < argc; i++) {
178 cur_file = argv[i];
179 file = fopen(cur_file, "r");
180 if (file == NULL) {
181 if (!suppress_err_msgs)
182 fprintf(stderr, "grep: %s: %s\n", cur_file, strerror(errno));
183 } else {
184 grep_file(file);
185 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000186 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 }
188 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000189
190 regfree(&regex);
191
192 if (nmatches == 0)
193 return 1;
194
195 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000196}