-
Notifications
You must be signed in to change notification settings - Fork 1
/
zgrep.c
124 lines (106 loc) · 2.66 KB
/
zgrep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* zgrep.c - simple standalone grep
* Copyright (C) 2015 Sean MacLennan
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this project; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "z.h"
#include <fnmatch.h>
#ifdef WIN32
#define FNM_FLAGS FNM_CASEFOLD
#else
#define FNM_FLAGS 0
#endif
static void grep_one(char *fname, regexp_t *re,
struct buff *inbuff, struct buff *outbuff)
{
if (breadfile(inbuff, fname, NULL))
return;
while (re_step(inbuff, re, NULL)) {
struct mark start;
unsigned long line = bline(inbuff);
binstr(outbuff, "%s:%u: ", fname, line);
zrefresh();
tobegline(inbuff);
bmrktopnt(inbuff, &start);
toendline(inbuff);
bmove1(inbuff); /* grab NL */
bcopyrgn(&start, outbuff);
}
}
static void grepit(char *input, char *files)
{
regexp_t re;
DIR *dir;
struct dirent *ent;
struct buff *inbuff, *outbuff = Curbuff->buff;
int rc = re_compile(&re, input, REG_EXTENDED);
if (rc) {
re_error(rc, &re, PawStr, COLMAX);
error("%s", PawStr);
return;
}
dir = opendir(".");
if (!dir) {
error("Unable to open directory");
return;
}
if (!(inbuff = bcreate())) {
error("Unable to create tmp file buffer.");
goto cleanup;
}
while ((ent = readdir(dir)) != NULL)
if (fnmatch(files, ent->d_name, FNM_FLAGS) == 0)
grep_one(ent->d_name, &re, inbuff, outbuff);
cleanup:
closedir(dir);
if (inbuff)
bdelbuff(inbuff);
re_free(&re);
message(Curbuff, "Done");
}
void Zgrep(void)
{
char input[STRMAX + 1], files[STRMAX + 1], *p;
int rc;
struct wdo *save = Curwdo;
getbword(input, STRMAX, bistoken);
if (getarg("Regex: ", input, STRMAX))
return;
if (Curbuff->bmode & CMODE)
strcpy(files, "*.[ch]");
else if (Curbuff->bmode & SHMODE)
strcpy(files, "*.sh");
else
strcpy(files, "*");
if (getarg("File(s): ", files, STRMAX))
return;
p = strrchr(files, '/');
if (p) {
*p++ = '\0';
rc = chdir(files);
strcpy(files, p);
} else
rc = do_chdir(Curbuff);
if (rc) {
error("Unable to chdir");
return;
}
if (wuseother(SHELLBUFF)) {
set_shell_mark();
grepit(input, files);
wswitchto(save);
}
}