blob: a2c07c52371513e07ad41408eb9140f9230edbb3 [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
Mark Whitleyd3721892000-06-28 22:00:26 +000034/* options */
35static int ignore_case = 0;
36static int print_filename = 0;
37static int print_line_num = 0;
38static int be_quiet = 0;
39static int invert_search = 0;
40static int suppress_err_msgs = 0;
41
42/* globals */
43static regex_t regex; /* storage space for compiled regular expression */
44static int nmatches = 0; /* keeps track of the number of matches */
45static char *cur_file = NULL; /* the current file we are reading */
46
47
Mark Whitleyd3721892000-06-28 22:00:26 +000048static void print_matched_line(char *line, int linenum)
49{
50 if (print_filename)
51 printf("%s:", cur_file);
52 if (print_line_num)
53 printf("%i:", linenum);
54
55 printf("%s", line);
56}
57
58static void grep_file(FILE *file)
59{
60 char *line = NULL;
61 int ret;
62 int linenum = 0;
63
64 while ((line = get_line_from_file(file)) != NULL) {
65 linenum++;
66 ret = regexec(&regex, line, 0, NULL, 0);
67 if (ret == 0 && !invert_search) { /* match */
68
69 /* if we found a match but were told to be quiet, stop here and
70 * return success */
71 if (be_quiet) {
72 regfree(&regex);
73 exit(0);
74 }
75
76 nmatches++;
77
78 print_matched_line(line, linenum);
79
80 } else if (ret == REG_NOMATCH && invert_search) {
81 print_matched_line(line, linenum);
82 }
83
84 free(line);
85 }
86}
Eric Andersencc8ed391999-10-05 16:24:54 +000087
Erik Andersene49d5ec2000-02-08 19:58:47 +000088extern int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000089{
Mark Whitleyd3721892000-06-28 22:00:26 +000090 int opt;
91 int reflags;
Eric Andersencc8ed391999-10-05 16:24:54 +000092
Mark Whitleyd3721892000-06-28 22:00:26 +000093 /* do special-case option parsing */
94 if (argv[1] && (strcmp(argv[1], "--help") == 0))
Eric Andersen3e0fbae1999-10-19 06:02:44 +000095 usage(grep_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +000096
Mark Whitleyd3721892000-06-28 22:00:26 +000097 /* do normal option parsing */
98 while ((opt = getopt(argc, argv, "iHhnqvs")) > 0) {
99 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 case 'i':
Mark Whitleyd3721892000-06-28 22:00:26 +0000101 ignore_case++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000103 case 'H':
104 print_filename++;
105 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 case 'h':
Mark Whitleyd3721892000-06-28 22:00:26 +0000107 print_filename--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 case 'n':
Mark Whitleyd3721892000-06-28 22:00:26 +0000110 print_line_num++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 case 'q':
Mark Whitleyd3721892000-06-28 22:00:26 +0000113 be_quiet++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 break;
John Beppuf93a95d2000-04-24 18:07:30 +0000115 case 'v':
Mark Whitleyd3721892000-06-28 22:00:26 +0000116 invert_search++;
John Beppuf93a95d2000-04-24 18:07:30 +0000117 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000118 case 's':
119 suppress_err_msgs++;
120 break;
Eric Andersen053b1462000-06-13 06:24:53 +0000121 }
Eric Andersen053b1462000-06-13 06:24:53 +0000122 }
123
Mark Whitleyd3721892000-06-28 22:00:26 +0000124 /* argv[optind] should be the regex pattern; no pattern, no worky */
125 if (argv[optind] == NULL)
126 usage(grep_usage);
127
Mark Whitley44735f82000-07-10 15:50:26 +0000128 /* compile the regular expression
129 * we're not going to mess with sub-expressions, and we need to
130 * treat newlines right. */
131 reflags = REG_NOSUB | REG_NEWLINE;
Mark Whitleyd3721892000-06-28 22:00:26 +0000132 if (ignore_case)
133 reflags |= REG_ICASE;
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000134 xregcomp(&regex, argv[optind], reflags);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135
Mark Whitleyd3721892000-06-28 22:00:26 +0000136 /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If
137 * there is more than one file to grep, we will print the filenames */
138 if ((argc-1) - (optind+1) > 0)
139 print_filename++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140
Mark Whitley2e1148b2000-06-28 22:59:30 +0000141 /* If no files were specified, or '-' was specified, take input from
142 * stdin. Otherwise, we grep through all the files specified. */
Mark Whitley8bd891c2000-06-28 22:55:59 +0000143 if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000144 grep_file(stdin);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 } else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000146 int i;
147 FILE *file;
148 for (i = optind + 1; i < argc; i++) {
149 cur_file = argv[i];
150 file = fopen(cur_file, "r");
151 if (file == NULL) {
152 if (!suppress_err_msgs)
Matt Kraaid537a952000-07-14 01:51:25 +0000153 errorMsg("%s: %s\n", cur_file, strerror(errno));
Mark Whitleyd3721892000-06-28 22:00:26 +0000154 } else {
155 grep_file(file);
156 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 }
159 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000160
161 regfree(&regex);
162
163 if (nmatches == 0)
164 return 1;
165
166 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000167}