-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpregex.c
198 lines (165 loc) · 4.62 KB
/
pregex.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* -MODULE----------------------------------------------------------------------
Phorward C/C++ Library
Copyright (C) 2006-2019 by Phorward Software Technologies, Jan Max Meyer
https://phorward.info ++ contact<at>phorward<dash>software<dot>com
All rights reserved. See LICENSE for more information.
File: pregex.c
Usage: A pregex object demonstration suite.
----------------------------------------------------------------------------- */
#include "local.h"
void help( char** argv )
{
printf( "Usage: %s OPTIONS {expression} input\n\n"
" -a --action ACTION Perform regular expression action:\n"
" match (default), find, split, replace\n"
" -d --delimiter STRING Use STRING as result delimiter\n"
" (newline is default)\n"
" -D Dump constructed DFA\n"
" -e --exec EXPR Use string EXPR as expression.\n"
" -f --file FILENAME Read expression from FILENAME\n"
" -h --help Show this help, and exit.\n"
" -i --input INPUT Use string INPUT as input.\n"
" -r --replace STRING Replacement string for 'replace' action.\n"
" -V --version Show version info and exit.\n"
"\n", *argv );
}
int main( int argc, char** argv )
{
char* actions[] = { "match", "find", "split", "replace" };
char* action = actions[0];
char* fexpr = (char*)NULL;
char* expr = (char*)NULL;
char* finput = (char*)NULL;
char* input = (char*)NULL;
char* replace = "";
char* delimiter = "\n";
char* start;
char* end;
char* nstart;
pboolean dump = FALSE;
pregex* re;
int i;
int j;
int rc;
int next;
char opt [ 10 + 1 ];
char* param;
PROC( "pregex" );
/* Analyze command-line parameters */
for( i = 0; ( rc = pgetopt( opt, ¶m, &next, argc, argv,
"a:d:De:hf:i:r:V",
"action: delimiter: exec: file: help "
"input: replace: version", i ) )
== 0; i++ )
{
VARS( "opt", "%s", opt );
VARS( "param", "%s", param );
if( !strcmp( opt, "action" ) || !strcmp( opt, "a" ) )
{
action = (char*)NULL;
for( j = 0; j < sizeof(actions) / sizeof(*actions); j++ )
if( pstrcasecmp( param, actions[j] ) == 0 )
{
action = actions[j];
break;
}
if( !action )
{
fprintf( stderr, "Invalid action '%s'\n", param );
help( argv );
RETURN( 1 );
}
}
else if( !strcmp( opt, "delimiter" ) || !strcmp( opt, "d" ) )
delimiter = pstrunescape( param );
else if( !strcmp( opt, "exec" ) || !strcmp( opt, "e" ) )
expr = param;
else if( !strcmp( opt, "file" ) || !strcmp( opt, "f" ) )
{
fexpr = pfree( fexpr );
if( !pfiletostr( &fexpr, param ) )
{
fprintf( stderr, "Unable to read expression file '%s'\n",
param );
RETURN( 1 );
}
expr = fexpr;
}
else if( !strcmp( opt, "help" ) || !strcmp( opt, "h" ) )
{
help( argv );
RETURN( 0 );
}
else if( !strcmp( opt, "input" ) || !strcmp( opt, "i" ) )
input = param;
else if( !strcmp( opt, "replace" ) || !strcmp( opt, "r" ) )
replace = param;
else if( !strcmp( opt, "version" ) || !strcmp( opt, "V" ) )
{
version( argv, "Regular expression command-line utility" );
RETURN( 0 );
}
}
VARS( "rc", "%d", rc );
if( rc == 1 )
{
if( !expr && argc > next )
expr = argv[next++];
if( !input && argc > next )
{
if( !pfiletostr( &finput, argv[next] ) )
input = argv[next];
else
input = finput;
next++; /* <- obsolete, but complete ;-) */
}
}
VARS( "expr", "%s", expr );
VARS( "input", "%s", input );
if( !( expr && input ) || ( rc < 0 && param ) )
{
if( rc < 0 && param )
fprintf( stderr, "Unknown option '%s'\n", param );
else if( argc > 1 )
fprintf( stderr, "Too less parameters given.\n" );
help( argv );
RETURN( 1 );
}
/* Process */
start = input;
re = pregex_create( expr, PREGEX_FLAG_NONE );
VARS( "action", "%s", action );
if( strcmp( action, "match" ) == 0 )
{
if( pregex_match( re, start, &end ) )
printf( "%.*s%s", end - start, start, delimiter );
}
else if( strcmp( action, "find" ) == 0 )
{
while( ( start = pregex_find( re, start, &end ) ) )
{
printf( "%.*s%s", end - start, start, delimiter );
start = end;
}
}
else if( strcmp( action, "split" ) == 0 )
{
while( start )
{
if( pregex_split( re, start, &end, &nstart ) )
printf( "%.*s%s", end - start, start, delimiter );
start = nstart;
}
}
else if( strcmp( action, "replace" ) == 0 )
{
VARS( "replace", "%s", replace );
start = pregex_replace( re, start, replace );
printf( "%s", start );
pfree( start );
}
fflush( stdout );
pfree( finput );
pfree( fexpr );
RETURN( 0 );
}