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 | #include <stdio.h>
#include "corr.h"
#define LINE_CAPACITY 1024
int convert_cflags(const corr_t* corr);
void corr_search_file(corr_t* corr, FILE* input);
void corr_init(corr_t* corr)
{
args_init(&corr->args);
}
int corr_run(corr_t* corr, int argc, char* argv[])
{
int error = args_analyze(&corr->args, argc, argv);
if ( error )
return error;
if ( corr->args.pattern == NULL )
return args_print_help();
int cflags = convert_cflags(corr);
if ( regcomp(&corr->regex, corr->args.pattern, cflags) )
return fprintf(stderr, "error: invalid regular expression: %s\n", corr->args.pattern), 3;
corr_search_file(corr, stdin);
regfree(&corr->regex);
return 0;
}
void corr_destroy(corr_t* corr)
{
args_destroy(&corr->args);
}
int convert_cflags(const corr_t* corr)
{
int cflags = REG_NOSUB;
if ( corr->args.extended )
cflags |= REG_EXTENDED;
if ( corr->args.ignore_case )
cflags |= REG_ICASE;
if ( corr->args.new_line )
cflags |= REG_NEWLINE;
return cflags;
}
void corr_search_file(corr_t* corr, FILE* input)
{
int eflags = 0; // REG_NOTBOL | REG_NOTEOL
//regmatch_t matches[32];
char line[LINE_CAPACITY];
while ( fgets(line, LINE_CAPACITY, input) )
if ( regexec(&corr->regex, line, /*nmatch*/ 0, /*matches*/ NULL, eflags) == 0 )
printf("%s", line);
}
|