Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * shmatch.c -- shell interface to posix regular expression matching. |
| 3 | */ |
| 4 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5 | /* Copyright (C) 2003 Free Software Foundation, Inc. |
| 6 | |
| 7 | This file is part of GNU Bash, the Bourne Again SHell. |
| 8 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 9 | Bash is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 3 of the License, or |
| 12 | (at your option) any later version. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 13 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 14 | Bash is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 18 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 19 | You should have received a copy of the GNU General Public License |
| 20 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | # include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #if defined (HAVE_POSIX_REGEXP) |
| 28 | |
| 29 | #ifdef HAVE_UNISTD_H |
| 30 | # include <unistd.h> |
| 31 | #endif |
| 32 | |
| 33 | #include "bashansi.h" |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <regex.h> |
| 37 | |
| 38 | #include "shell.h" |
| 39 | #include "variables.h" |
| 40 | #include "externs.h" |
| 41 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 42 | extern int glob_ignore_case, match_ignore_case; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 43 | |
| 44 | int |
| 45 | sh_regmatch (string, pattern, flags) |
| 46 | const char *string; |
| 47 | const char *pattern; |
| 48 | int flags; |
| 49 | { |
| 50 | regex_t regex = { 0 }; |
| 51 | regmatch_t *matches; |
| 52 | int rflags; |
| 53 | #if defined (ARRAY_VARS) |
| 54 | SHELL_VAR *rematch; |
| 55 | ARRAY *amatch; |
| 56 | int subexp_ind; |
| 57 | char *subexp_str; |
| 58 | int subexp_len; |
| 59 | #endif |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 60 | int result; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 61 | |
| 62 | #if defined (ARRAY_VARS) |
| 63 | rematch = (SHELL_VAR *)NULL; |
| 64 | #endif |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 65 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 66 | rflags = REG_EXTENDED; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 67 | if (glob_ignore_case || match_ignore_case) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 68 | rflags |= REG_ICASE; |
| 69 | #if !defined (ARRAY_VARS) |
| 70 | rflags |= REG_NOSUB; |
| 71 | #endif |
| 72 | |
| 73 | if (regcomp (®ex, pattern, rflags)) |
| 74 | return 2; /* flag for printing a warning here. */ |
| 75 | |
| 76 | #if defined (ARRAY_VARS) |
| 77 | matches = (regmatch_t *)malloc (sizeof (regmatch_t) * (regex.re_nsub + 1)); |
| 78 | #else |
| 79 | matches = NULL; |
| 80 | #endif |
| 81 | |
| 82 | if (regexec (®ex, string, regex.re_nsub + 1, matches, 0)) |
| 83 | result = EXECUTION_FAILURE; |
| 84 | else |
| 85 | result = EXECUTION_SUCCESS; /* match */ |
| 86 | |
| 87 | #if defined (ARRAY_VARS) |
| 88 | subexp_len = strlen (string) + 10; |
| 89 | subexp_str = malloc (subexp_len + 1); |
| 90 | |
| 91 | /* Store the parenthesized subexpressions in the array BASH_REMATCH. |
| 92 | Element 0 is the portion that matched the entire regexp. Element 1 |
| 93 | is the part that matched the first subexpression, and so on. */ |
| 94 | unbind_variable ("BASH_REMATCH"); |
| 95 | rematch = make_new_array_variable ("BASH_REMATCH"); |
| 96 | amatch = array_cell (rematch); |
| 97 | |
| 98 | if ((flags & SHMAT_SUBEXP) && result == EXECUTION_SUCCESS && subexp_str) |
| 99 | { |
| 100 | for (subexp_ind = 0; subexp_ind <= regex.re_nsub; subexp_ind++) |
| 101 | { |
| 102 | memset (subexp_str, 0, subexp_len); |
| 103 | strncpy (subexp_str, string + matches[subexp_ind].rm_so, |
| 104 | matches[subexp_ind].rm_eo - matches[subexp_ind].rm_so); |
| 105 | array_insert (amatch, subexp_ind, subexp_str); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | VSETATTR (rematch, att_readonly); |
| 110 | |
| 111 | free (subexp_str); |
| 112 | free (matches); |
| 113 | #endif /* ARRAY_VARS */ |
| 114 | |
| 115 | regfree (®ex); |
| 116 | |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | #endif /* HAVE_POSIX_REGEXP */ |