aio: Add dgram unix socket support
[jimtcl.git] / jim-regexp.c
blob771773a618e0ff48d0c8dc68ec5b4f4df37a1b7e
1 /*
2 * Implements the regexp and regsub commands for Jim
4 * (c) 2008 Steve Bennett <steveb@workware.net.au>
6 * Uses C library regcomp()/regexec() for the matching.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials
17 * provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * The views and conclusions contained in the software and documentation
33 * are those of the authors and should not be interpreted as representing
34 * official policies, either expressed or implied, of the Jim Tcl Project.
36 * Based on code originally from Tcl 6.7:
38 * Copyright 1987-1991 Regents of the University of California
39 * Permission to use, copy, modify, and distribute this
40 * software and its documentation for any purpose and without
41 * fee is hereby granted, provided that the above copyright
42 * notice appear in all copies. The University of California
43 * makes no representations about the suitability of this
44 * software for any purpose. It is provided "as is" without
45 * express or implied warranty.
48 #include <stdlib.h>
49 #include <string.h>
51 #include "jimautoconf.h"
52 #if defined(JIM_REGEXP)
53 #include "jimregexp.h"
54 #else
55 #include <regex.h>
56 #endif
57 #include "jim.h"
59 static void FreeRegexpInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
61 regfree(objPtr->internalRep.ptrIntValue.ptr);
62 Jim_Free(objPtr->internalRep.ptrIntValue.ptr);
65 /* internal rep is stored in ptrIntvalue
66 * ptr = compiled regex
67 * int1 = flags
69 static const Jim_ObjType regexpObjType = {
70 "regexp",
71 FreeRegexpInternalRep,
72 NULL,
73 NULL,
74 JIM_TYPE_NONE
77 static regex_t *SetRegexpFromAny(Jim_Interp *interp, Jim_Obj *objPtr, unsigned flags)
79 regex_t *compre;
80 const char *pattern;
81 int ret;
83 /* Check if the object is already an uptodate variable */
84 if (objPtr->typePtr == &regexpObjType &&
85 objPtr->internalRep.ptrIntValue.ptr && objPtr->internalRep.ptrIntValue.int1 == flags) {
86 /* nothing to do */
87 return objPtr->internalRep.ptrIntValue.ptr;
90 /* Not a regexp or the flags do not match */
92 /* Get the string representation */
93 pattern = Jim_String(objPtr);
94 compre = Jim_Alloc(sizeof(regex_t));
96 if ((ret = regcomp(compre, pattern, REG_EXTENDED | flags)) != 0) {
97 char buf[100];
99 regerror(ret, compre, buf, sizeof(buf));
100 Jim_SetResultFormatted(interp, "couldn't compile regular expression pattern: %s", buf);
101 regfree(compre);
102 Jim_Free(compre);
103 return NULL;
106 Jim_FreeIntRep(interp, objPtr);
108 objPtr->typePtr = &regexpObjType;
109 objPtr->internalRep.ptrIntValue.int1 = flags;
110 objPtr->internalRep.ptrIntValue.ptr = compre;
112 return compre;
115 int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
117 int opt_indices = 0;
118 int opt_all = 0;
119 int opt_inline = 0;
120 regex_t *regex;
121 int match, i, j;
122 int offset = 0;
123 regmatch_t *pmatch = NULL;
124 int source_len;
125 int result = JIM_OK;
126 const char *pattern;
127 const char *source_str;
128 int num_matches = 0;
129 int num_vars;
130 Jim_Obj *resultListObj = NULL;
131 int regcomp_flags = 0;
132 int eflags = 0;
133 int option;
134 enum {
135 OPT_INDICES, OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_INLINE, OPT_START, OPT_END
137 static const char * const options[] = {
138 "-indices", "-nocase", "-line", "-all", "-inline", "-start", "--", NULL
141 if (argc < 3) {
142 wrongNumArgs:
143 Jim_WrongNumArgs(interp, 1, argv,
144 "?-switch ...? exp string ?matchVar? ?subMatchVar ...?");
145 return JIM_ERR;
148 for (i = 1; i < argc; i++) {
149 const char *opt = Jim_String(argv[i]);
151 if (*opt != '-') {
152 break;
154 if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
155 return JIM_ERR;
157 if (option == OPT_END) {
158 i++;
159 break;
161 switch (option) {
162 case OPT_INDICES:
163 opt_indices = 1;
164 break;
166 case OPT_NOCASE:
167 regcomp_flags |= REG_ICASE;
168 break;
170 case OPT_LINE:
171 regcomp_flags |= REG_NEWLINE;
172 break;
174 case OPT_ALL:
175 opt_all = 1;
176 break;
178 case OPT_INLINE:
179 opt_inline = 1;
180 break;
182 case OPT_START:
183 if (++i == argc) {
184 goto wrongNumArgs;
186 if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) {
187 return JIM_ERR;
189 break;
192 if (argc - i < 2) {
193 goto wrongNumArgs;
196 regex = SetRegexpFromAny(interp, argv[i], regcomp_flags);
197 if (!regex) {
198 return JIM_ERR;
201 pattern = Jim_String(argv[i]);
202 source_str = Jim_GetString(argv[i + 1], &source_len);
204 num_vars = argc - i - 2;
206 if (opt_inline) {
207 if (num_vars) {
208 Jim_SetResultString(interp, "regexp match variables not allowed when using -inline",
209 -1);
210 result = JIM_ERR;
211 goto done;
213 num_vars = regex->re_nsub + 1;
216 pmatch = Jim_Alloc((num_vars + 1) * sizeof(*pmatch));
218 /* If an offset has been specified, adjust for that now.
219 * If it points past the end of the string, point to the terminating null
221 if (offset) {
222 if (offset < 0) {
223 offset += source_len + 1;
225 if (offset > source_len) {
226 source_str += source_len;
228 else if (offset > 0) {
229 source_str += offset;
231 eflags |= REG_NOTBOL;
234 if (opt_inline) {
235 resultListObj = Jim_NewListObj(interp, NULL, 0);
238 next_match:
239 match = regexec(regex, source_str, num_vars + 1, pmatch, eflags);
240 if (match >= REG_BADPAT) {
241 char buf[100];
243 regerror(match, regex, buf, sizeof(buf));
244 Jim_SetResultFormatted(interp, "error while matching pattern: %s", buf);
245 result = JIM_ERR;
246 goto done;
249 if (match == REG_NOMATCH) {
250 goto done;
253 num_matches++;
255 if (opt_all && !opt_inline) {
256 /* Just count the number of matches, so skip the substitution h */
257 goto try_next_match;
261 * If additional variable names have been specified, return
262 * index information in those variables.
265 j = 0;
266 for (i += 2; opt_inline ? j < num_vars : i < argc; i++, j++) {
267 Jim_Obj *resultObj;
269 if (opt_indices) {
270 resultObj = Jim_NewListObj(interp, NULL, 0);
272 else {
273 resultObj = Jim_NewStringObj(interp, "", 0);
276 if (pmatch[j].rm_so == -1) {
277 if (opt_indices) {
278 Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, -1));
279 Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, -1));
282 else {
283 int len = pmatch[j].rm_eo - pmatch[j].rm_so;
285 if (opt_indices) {
286 Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp,
287 offset + pmatch[j].rm_so));
288 Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp,
289 offset + pmatch[j].rm_so + len - 1));
291 else {
292 Jim_AppendString(interp, resultObj, source_str + pmatch[j].rm_so, len);
296 if (opt_inline) {
297 Jim_ListAppendElement(interp, resultListObj, resultObj);
299 else {
300 /* And now set the result variable */
301 result = Jim_SetVariable(interp, argv[i], resultObj);
303 if (result != JIM_OK) {
304 Jim_FreeObj(interp, resultObj);
305 break;
310 try_next_match:
311 if (opt_all && (pattern[0] != '^' || (regcomp_flags & REG_NEWLINE)) && *source_str) {
312 if (pmatch[0].rm_eo) {
313 offset += pmatch[0].rm_eo;
314 source_str += pmatch[0].rm_eo;
316 else {
317 source_str++;
318 offset++;
320 if (*source_str) {
321 eflags = REG_NOTBOL;
322 goto next_match;
326 done:
327 if (result == JIM_OK) {
328 if (opt_inline) {
329 Jim_SetResult(interp, resultListObj);
331 else {
332 Jim_SetResultInt(interp, num_matches);
336 Jim_Free(pmatch);
337 return result;
340 #define MAX_SUB_MATCHES 50
342 int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
344 int regcomp_flags = 0;
345 int regexec_flags = 0;
346 int opt_all = 0;
347 int offset = 0;
348 regex_t *regex;
349 const char *p;
350 int result;
351 regmatch_t pmatch[MAX_SUB_MATCHES + 1];
352 int num_matches = 0;
354 int i, j, n;
355 Jim_Obj *varname;
356 Jim_Obj *resultObj;
357 const char *source_str;
358 int source_len;
359 const char *replace_str;
360 int replace_len;
361 const char *pattern;
362 int option;
363 enum {
364 OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_START, OPT_END
366 static const char * const options[] = {
367 "-nocase", "-line", "-all", "-start", "--", NULL
370 if (argc < 4) {
371 wrongNumArgs:
372 Jim_WrongNumArgs(interp, 1, argv,
373 "?-switch ...? exp string subSpec ?varName?");
374 return JIM_ERR;
377 for (i = 1; i < argc; i++) {
378 const char *opt = Jim_String(argv[i]);
380 if (*opt != '-') {
381 break;
383 if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
384 return JIM_ERR;
386 if (option == OPT_END) {
387 i++;
388 break;
390 switch (option) {
391 case OPT_NOCASE:
392 regcomp_flags |= REG_ICASE;
393 break;
395 case OPT_LINE:
396 regcomp_flags |= REG_NEWLINE;
397 break;
399 case OPT_ALL:
400 opt_all = 1;
401 break;
403 case OPT_START:
404 if (++i == argc) {
405 goto wrongNumArgs;
407 if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) {
408 return JIM_ERR;
410 break;
413 if (argc - i != 3 && argc - i != 4) {
414 goto wrongNumArgs;
417 regex = SetRegexpFromAny(interp, argv[i], regcomp_flags);
418 if (!regex) {
419 return JIM_ERR;
421 pattern = Jim_String(argv[i]);
423 source_str = Jim_GetString(argv[i + 1], &source_len);
424 replace_str = Jim_GetString(argv[i + 2], &replace_len);
425 varname = argv[i + 3];
427 /* Create the result string */
428 resultObj = Jim_NewStringObj(interp, "", 0);
430 /* If an offset has been specified, adjust for that now.
431 * If it points past the end of the string, point to the terminating null
433 if (offset) {
434 if (offset < 0) {
435 offset += source_len + 1;
437 if (offset > source_len) {
438 offset = source_len;
440 else if (offset < 0) {
441 offset = 0;
445 /* Copy the part before -start */
446 Jim_AppendString(interp, resultObj, source_str, offset);
449 * The following loop is to handle multiple matches within the
450 * same source string; each iteration handles one match and its
451 * corresponding substitution. If "-all" hasn't been specified
452 * then the loop body only gets executed once.
455 n = source_len - offset;
456 p = source_str + offset;
457 do {
458 int match = regexec(regex, p, MAX_SUB_MATCHES, pmatch, regexec_flags);
460 if (match >= REG_BADPAT) {
461 char buf[100];
463 regerror(match, regex, buf, sizeof(buf));
464 Jim_SetResultFormatted(interp, "error while matching pattern: %s", buf);
465 return JIM_ERR;
467 if (match == REG_NOMATCH) {
468 break;
471 num_matches++;
474 * Copy the portion of the source string before the match to the
475 * result variable.
477 Jim_AppendString(interp, resultObj, p, pmatch[0].rm_so);
480 * Append the subSpec (replace_str) argument to the variable, making appropriate
481 * substitutions. This code is a bit hairy because of the backslash
482 * conventions and because the code saves up ranges of characters in
483 * subSpec to reduce the number of calls to Jim_SetVar.
486 for (j = 0; j < replace_len; j++) {
487 int idx;
488 int c = replace_str[j];
490 if (c == '&') {
491 idx = 0;
493 else if (c == '\\' && j < replace_len) {
494 c = replace_str[++j];
495 if ((c >= '0') && (c <= '9')) {
496 idx = c - '0';
498 else if ((c == '\\') || (c == '&')) {
499 Jim_AppendString(interp, resultObj, replace_str + j, 1);
500 continue;
502 else {
503 /* If the replacement is a trailing backslash, just replace with a backslash, otherwise
504 * with the literal backslash and the following character
506 Jim_AppendString(interp, resultObj, replace_str + j - 1, (j == replace_len) ? 1 : 2);
507 continue;
510 else {
511 Jim_AppendString(interp, resultObj, replace_str + j, 1);
512 continue;
514 if ((idx < MAX_SUB_MATCHES) && pmatch[idx].rm_so != -1 && pmatch[idx].rm_eo != -1) {
515 Jim_AppendString(interp, resultObj, p + pmatch[idx].rm_so,
516 pmatch[idx].rm_eo - pmatch[idx].rm_so);
520 p += pmatch[0].rm_eo;
521 n -= pmatch[0].rm_eo;
523 /* If -all is not specified, or there is no source left, we are done */
524 if (!opt_all || n == 0) {
525 break;
528 /* An anchored pattern without -line must be done */
529 if ((regcomp_flags & REG_NEWLINE) == 0 && pattern[0] == '^') {
530 break;
533 /* If the pattern is empty, need to step forwards */
534 if (pattern[0] == '\0' && n) {
535 /* Need to copy the char we are moving over */
536 Jim_AppendString(interp, resultObj, p, 1);
537 p++;
538 n--;
541 regexec_flags |= REG_NOTBOL;
542 } while (n);
545 * Copy the portion of the string after the last match to the
546 * result variable.
548 Jim_AppendString(interp, resultObj, p, -1);
550 /* And now set or return the result variable */
551 if (argc - i == 4) {
552 result = Jim_SetVariable(interp, varname, resultObj);
554 if (result == JIM_OK) {
555 Jim_SetResultInt(interp, num_matches);
557 else {
558 Jim_FreeObj(interp, resultObj);
561 else {
562 Jim_SetResult(interp, resultObj);
563 result = JIM_OK;
566 return result;
569 int Jim_regexpInit(Jim_Interp *interp)
571 if (Jim_PackageProvide(interp, "regexp", "1.0", JIM_ERRMSG))
572 return JIM_ERR;
574 Jim_CreateCommand(interp, "regexp", Jim_RegexpCmd, NULL, NULL);
575 Jim_CreateCommand(interp, "regsub", Jim_RegsubCmd, NULL, NULL);
576 return JIM_OK;