/
posixregex.c
351 lines (309 loc) · 10.1 KB
/
posixregex.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* DMS Tools and Utilities
* Copyright (c) 2008, 2009 David M. Syzdek <david@syzdek.net>.
*
* @SYZDEK_LICENSE_HEADER_START@
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation.
*
* 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 program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @SYZDEK_LICENSE_HEADER_END@
*/
/**
* @file src/posixregex.c simple POSIX regex testing utility/example
*/
/*
* Simple Build:
* gcc -W -Wall -O2 -c posixregex.c
* gcc -W -Wall -O2 -o posixregex posixregex.o
*
* GNU Libtool Build:
* libtool --mode=compile gcc -W -Wall -g -O2 -c posixregex.c
* libtool --mode=link gcc -W -Wall -g -O2 -o posixregex posixregex.lo
*
* GNU Libtool Install:
* libtool --mode=install install -c posixregex /usr/local/bin/posixregex
*
* GNU Libtool Clean:
* libtool --mode=clean rm -f posixregex.lo posixregex
*/
#define _SCT_SRC_POSIXREGEX_C 1
/////////////
// //
// Notes //
// //
/////////////
/*
* POSIX ASCII Description
* [:alnum:] [A-Za-z0-9] Alphanumeric characters
* [:alpha:] [A-Za-z] Alphabetic characters
* [:blank:] [ \t] Space and tab
* [:cntrl:] [\x00-\x1F\x7F] Control characters
* [:digit:] [0-9] Digits
* [:graph:] [\x21-\x7E] Visible characters
* [:lower:] [a-z] Lowercase letters
* [:print:] [\x20-\x7E] Visible characters and spaces
* [:space:] [ \t\r\n\v\f] Whitespace characters
* [:upper:] [A-Z] Uppercase letters
* [:xdigit:] [A-Fa-f0-9] Hexadecimal digits
* [:punct:] [-!"#$%&'()*+,./:;<=>?@[\\\]_`{|}~] Punctuation characters
*/
// Example Uses:
// posixregex '^([[:alpha:]]+)://(([[:alnum:]]+)(:([[:alnum:]]+)){0,1}@){0,1}([-.a-z]+)(:([[:digit:]]+))*/([-/[:alnum:]]*)(\?(.*)){0,1}$' http://jdoe:password@www.foo.org:123/path/to/file?query_string
// posixregex '\$([[:digit:]]+)\.([[:digit:]]{2,2})' 'Your change is $7.45.'
///////////////
// //
// Headers //
// //
///////////////
#ifdef HAVE_CONFIG_H
#include <config.h>
#include "common.h"
#endif
#include <regex.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
///////////////////
// //
// Definitions //
// //
///////////////////
#undef BUFFER_SIZE
#define BUFFER_SIZE 1024
#undef MAX_MATCHES
#define MAX_MATCHES 128
#ifndef PROGRAM_NAME
#define PROGRAM_NAME "posixregex"
#endif
#ifndef PACKAGE_NAME
#define PACKAGE_NAME ""
#endif
#ifndef PACKAGE_VERSION
#define PACKAGE_VERSION ""
#endif
#ifndef PACKAGE_BUGREPORT
#define PACKAGE_BUGREPORT "david@syzdek.net"
#endif
#ifndef PARAMS
#define PARAMS(protos) protos
#endif
//////////////////
// //
// Prototypes //
// //
//////////////////
/* main statement */
int main PARAMS((int, char **));
/* displays POSIX regular expressions */
void my_posixregex PARAMS((void));
/* displays usage */
void my_usage PARAMS((void));
/* displays usage */
void my_version PARAMS((void));
/////////////////
// //
// Functions //
// //
/////////////////
/// main statement
/// @param[in] argc number of arguments
/// @param[in] argv array of arguments
int main(int argc, char * argv[])
{
/* declare local vars */
int c;
int x;
int y;
int err;
int code;
int strlen;
int quiet;
int verbosity;
int opt_index;
char arg[BUFFER_SIZE];
char msg[BUFFER_SIZE];
char str[BUFFER_SIZE];
regex_t regex;
regmatch_t matches[MAX_MATCHES];
const char * restr;
/* getopt options */
static char short_opt[] = "hpqr:vV";
static struct option long_opt[] =
{
{"help", no_argument, 0, 'h'},
{"posixregex", no_argument, 0, 'p'},
{"quiet", no_argument, 0, 'q'},
{"silent", no_argument, 0, 'q'},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{NULL, 0, 0, 0 }
};
/* initialize variables */
code = 0;
restr = NULL;
quiet = 0;
verbosity = 0;
opt_index = 0;
/* loops through arguments */
while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1)
{
switch(c)
{
case -1: /* no more arguments */
case 0: /* long options toggles */
break;
case 'h':
my_usage();
return(0);
case 'p':
my_posixregex();
return(0);
case 'q':
quiet = 1;
verbosity = 0;
break;
case 'r':
restr = optarg;
break;
case 'V':
my_version();
return(0);
case 'v':
quiet = 0;
verbosity++;
break;
case '?':
fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
return(1);
default:
fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c);
fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
return(1);
};
};
if (!(restr))
restr = getenv("POSIXREGEX");
if (!(restr))
{
fprintf(stderr, "%s: missing required argument `-- r'\n", PROGRAM_NAME);
fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
return(1);
};
/* compile regular expression for later use */
if (!(quiet))
printf("regex: %s\n",restr);
if ((err = regcomp(®ex, restr, REG_EXTENDED|REG_ICASE)))
{
regerror(err, ®ex, msg, BUFFER_SIZE-1);
printf("%s\n", msg);
return(1);
};
/*
* Loops through command line arguments and tests the compiled expressions
* against each argument.
*/
for (x = optind; x < argc; x++)
{
/* copies string into buffer and prints to screen */
strncpy(arg, argv[x], BUFFER_SIZE-1);
if (!(quiet))
printf("%3i: %s ==> ", x-optind+1, arg);
/* tests the buffer against the regular expression */
if ((err = regexec(®ex, arg, MAX_MATCHES, matches, 0)))
{
if (!(quiet))
printf("not found\n");
code = 1;
} else {
if (!(quiet))
printf(" found\n");
strlen = 1;
/* copies sub matches in buffer string */
if (verbosity)
{
for(y = 0; ((y < MAX_MATCHES) && (matches[y].rm_eo > -1)); y++)
{
memset(str, 0, BUFFER_SIZE);
if ((strlen = matches[y].rm_eo - matches[y].rm_so))
{
strncpy(str, &arg[matches[y].rm_so], strlen);
printf(" submatch %i: %s\n", y, str);
} else if (matches[y].rm_eo > 0) {
printf(" submatch %i:\n", y);
};
};
printf("\n");
};
};
};
/* free regular expression */
regfree(®ex);
/* end function */
return(code);
}
/// displays POSIX regular expressions
void my_posixregex(void)
{
printf("POSIX ASCII Description\n");
printf("[:alnum:] [A-Za-z0-9] Alphanumeric characters\n");
printf("[:alpha:] [A-Za-z] Alphabetic characters\n");
printf("[:blank:] [ \\t] Space and tab\n");
printf("[:cntrl:] [\\x00-\\x1F\\x7F] Control characters\n");
printf("[:digit:] [0-9] Digits\n");
printf("[:graph:] [\\x21-\\x7E] Visible characters\n");
printf("[:lower:] [a-z] Lowercase letters\n");
printf("[:print:] [\\x20-\\x7E] Visible characters and spaces\n");
printf("[:space:] [ \\t\\r\\n\\v\\f] Whitespace characters\n");
printf("[:upper:] [A-Z] Uppercase letters\n");
printf("[:xdigit:] [A-Fa-f0-9] Hexadecimal digits\n");
printf("%s", "[:punct:] [-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~] Punctuation characters\n");
printf("\n");
printf("Example Uses:\n");
printf(" posixregex -r '^([[:alpha:]]+)://(([[:alnum:]]+)(:([[:alnum:]]+)){0,1}@){0,1}([-.a-z]+)(:([[:digit:]]+))*/([-/[:alnum:]]*)(\\?(.*)){0,1}$' http://jdoe:password@www.foo.org:123/path/to/file?query_string\n");
printf(" posixregex -r '\\$([[:digit:]]+)\\.([[:digit:]]{2,2})' 'Your change is $7.45.'\n");
return;
}
/// displays usage
void my_usage(void)
{
printf("Usage: %s [OPTIONS] string1 string2 ... stringN\n", PROGRAM_NAME);
printf(" -h, --help print this help and exit\n");
printf(" -p, --posixregex print regular expression patterns\n");
printf(" -q, --quiet, --silent do not print messages\n");
printf(" -r regex regular expression to use for testing strings\n");
printf(" -v, --verbose print verbose messages\n");
printf(" -V, --version print version number and exit\n");
printf("Variables:\n");
printf(" POSIXREGEX default regular expression to use for testing strings\n");
printf("\n");
#ifdef PACKAGE_BUGREPORT
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
#endif
return;
}
/// displays version
void my_version(void)
{
printf("%s (%s) %s\n", PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION);
printf("Written by David M. Syzdek.\n");
printf("\n");
#ifdef PACKAGE_COPYRIGHT
printf("%s\n", PACKAGE_COPYRIGHT);
#endif
printf("This is free software; see the source for copying conditions. There is NO\n");
printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
return;
}
/* end of source file */