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
125
126
127
128
129
130
131
132
133
134
135
136
|
/* ISC license. */
#include <errno.h>
#include <regex.h>
#include <string.h>
#include <skalibs/bytestr.h>
#include <skalibs/sgetopt.h>
#include <skalibs/buffer.h>
#include <skalibs/uint.h>
#include <skalibs/strerr2.h>
#include <skalibs/stralloc.h>
#include <skalibs/skamisc.h>
#define USAGE "s6-grep [ -E | -F ] [ -i ] [ -c ] [ -n ] [ -q ] [ -v ] pattern"
typedef struct flags_s flags_t, *flags_t_ref ;
struct flags_s
{
unsigned int extended : 1 ;
unsigned int ignorecase: 1 ;
unsigned int fixed : 1 ;
unsigned int count : 1 ;
unsigned int num : 1 ;
unsigned int quiet : 1 ;
unsigned int not : 1 ;
} ;
#define FLAGS_ZERO { .extended = 0, .ignorecase = 0, .fixed = 0, .count = 0, .num = 0, .quiet = 0, .not = 0 }
int main (int argc, char const *const *argv)
{
unsigned int count = 0 ;
flags_t flags = FLAGS_ZERO ;
PROG = "s6-grep" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
register int opt = subgetopt_r(argc, argv, "EFicnqv", &l) ;
if (opt == -1) break ;
switch (opt)
{
case 'E': flags.extended = 1 ; break ;
case 'F': flags.fixed = 1 ; break ;
case 'i': flags.ignorecase = 1 ; break ;
case 'c': flags.count = 1 ; break ;
case 'n': flags.num = 1 ; break ;
case 'q': flags.quiet = 1 ; break ;
case 'v': flags.not = 1 ; break ;
default : strerr_dieusage(100, USAGE) ;
}
}
argc -= l.ind ; argv += l.ind ;
}
if (!argc) strerr_dieusage(100, USAGE) ;
{
stralloc line = STRALLOC_ZERO ;
regex_t re ;
unsigned int num = 0 ;
unsigned int arglen ;
if (flags.fixed)
{
if (flags.ignorecase) arglen = str_len(argv[0]) ;
}
else
{
register int e = regcomp(&re, argv[0], REG_NOSUB | (flags.extended ? REG_EXTENDED : 0) | (flags.ignorecase ? REG_ICASE : 0)) ;
if (e)
{
char buf[256] ;
regerror(e, &re, buf, 256) ;
strerr_diefu2x(111, "compile regular expression: ", buf) ;
}
}
for (;;)
{
register int r ;
line.len = 0 ;
r = skagetln(buffer_0f1, &line, '\n') ;
if (!r) break ;
if (r < 0)
{
if ((errno != EPIPE) || !stralloc_catb(&line, "\n", 1))
strerr_diefu1sys(111, "read from stdin") ;
}
num++ ; line.s[line.len-1] = 0 ;
if (flags.fixed)
{
if (flags.ignorecase)
r = case_str(line.s, argv[0]) >= arglen ;
else
r = !strstr(line.s, argv[0]) ;
}
else
{
r = regexec(&re, line.s, 0, 0, 0) ;
if (r && r != REG_NOMATCH)
{
char buf[256] ;
regerror(r, &re, buf, 256) ;
strerr_diefu2x(111, "match regular expression: ", buf) ;
}
}
line.s[line.len-1] = '\n' ;
if (!r ^ flags.not)
{
count++ ;
if (!flags.quiet && !flags.count)
{
if (flags.num)
{
char fmt[UINT_FMT] ;
register unsigned int n = uint_fmt(fmt, num) ;
fmt[n++] = ':' ;
if (buffer_put(buffer_1, fmt, n) < (int)n)
strerr_diefu1sys(111, "write to stdout") ;
}
if (buffer_put(buffer_1, line.s, line.len) < (int)line.len)
strerr_diefu1sys(111, "write to stdout") ;
}
}
}
if (flags.quiet) return !count ;
stralloc_free(&line) ;
if (!flags.fixed) regfree(&re) ;
}
if (flags.count)
{
char fmt[UINT_FMT] ;
register unsigned int n = uint_fmt(fmt, count) ;
fmt[n++] = '\n' ;
if (buffer_put(buffer_1, fmt, n) < (int)n)
strerr_diefu1sys(111, "write to stdout") ;
}
return !count ;
}
|