| 1 |
// Regular expression subexpression tapset |
| 2 |
// Copyright (C) 2017 Serhei Makarov |
| 3 |
// Copyright (C) 2013 Red Hat, Inc. |
| 4 |
// |
| 5 |
// This file is part of systemtap, and is free software. You can |
| 6 |
// redistribute it and/or modify it under the terms of the GNU General |
| 7 |
// Public License (GPL); either version 2, or (at your option) any |
| 8 |
// later version. |
| 9 |
|
| 10 |
%{ |
| 11 |
#define STAP_NEED_CONTEXT_SUBEXPRESSION 1 |
| 12 |
%} |
| 13 |
|
| 14 |
/** |
| 15 |
* sfunction matched_str - Return the last matched string. |
| 16 |
* |
| 17 |
* Description: returns the string matched by the last successful |
| 18 |
* use of the =~ regexp matching operator. Returns an error if the |
| 19 |
* last use of =~ led to a failed match. |
| 20 |
*/ |
| 21 |
function matched_str:string() { return matched(0) } |
| 22 |
|
| 23 |
/** |
| 24 |
* sfunction matched - Return a given matched subexpression. |
| 25 |
* |
| 26 |
* @n: index to the subexpression to return. 0 corresponds to the |
| 27 |
* entire regular expression. |
| 28 |
* |
| 29 |
* Description: returns the content of the n'th subexpression of the |
| 30 |
* last successful use of the =~ regex matching operator. Returns an |
| 31 |
* empty string if the n'th subexpression was not matched (e.g. due to |
| 32 |
* alternation). Throws an error if the last use of =~ was a failed |
| 33 |
* match, or if fewer than n subexpressions are present in the |
| 34 |
* original regexp. |
| 35 |
*/ |
| 36 |
function matched:string(n:long) |
| 37 |
%{ /* pure */ /* unprivileged */ /* pragma:tagged_dfa */ |
| 38 |
int start_ix, end_ix; // indices into tag buffer |
| 39 |
int start, end, length; // actual coordinate values |
| 40 |
|
| 41 |
if (!CONTEXT->last_match.result) { |
| 42 |
snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer), |
| 43 |
"Attempted to get subexpression %lld from failed match", (long long) STAP_ARG_n); |
| 44 |
CONTEXT->last_error = CONTEXT->error_buffer; |
| 45 |
} |
| 46 |
|
| 47 |
start_ix = 2 * STAP_ARG_n, end_ix = start_ix + 1; |
| 48 |
|
| 49 |
if (end_ix >= CONTEXT->last_match.num_final_tags) { |
| 50 |
snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer), |
| 51 |
"Attempted to get nonexistent subexpression %lld", (long long) STAP_ARG_n); |
| 52 |
CONTEXT->last_error = CONTEXT->error_buffer; |
| 53 |
} |
| 54 |
|
| 55 |
start = CONTEXT->last_match.tag_vals[start_ix]; |
| 56 |
end = CONTEXT->last_match.tag_vals[end_ix]; |
| 57 |
// _stp_printf ("**DEBUG** Extracted subexpression #%lld:(%d,%d) from %d to %d\n", STAP_ARG_n, start_ix, end_ix, start, end); |
| 58 |
|
| 59 |
if (start < 0 || end < 0) { |
| 60 |
// If indices are negative, the group was not matched. Return empty string: |
| 61 |
start = end = 0; |
| 62 |
// snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer), |
| 63 |
// "Unknown coordinates for subexpression %lld", STAP_ARG_n); |
| 64 |
// CONTEXT->last_error = CONTEXT->error_buffer; |
| 65 |
} |
| 66 |
|
| 67 |
if (start > end) { |
| 68 |
// This should not happen. |
| 69 |
snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer), |
| 70 |
"BUG: inverted coordinates for subexpression %lld", (long long) STAP_ARG_n); |
| 71 |
CONTEXT->last_error = CONTEXT->error_buffer; |
| 72 |
} |
| 73 |
|
| 74 |
length = end - start; |
| 75 |
|
| 76 |
// TODOXXX assert (start <= strlen(matched_str)) ?? |
| 77 |
// XXX: Must add 1 to length to account for NUL byte in strlcpy(). |
| 78 |
strlcpy(STAP_RETVALUE, CONTEXT->last_match.matched_str + start, length + 1); |
| 79 |
%} |
| 80 |
|
| 81 |
/** |
| 82 |
* sfunction ngroups - Number of subexpressions in the last match. |
| 83 |
* |
| 84 |
* Description: returns the number of subexpressions from the |
| 85 |
* last successful use of the =~ regex matching operator. |
| 86 |
* |
| 87 |
* Note that this number includes subexpressions which are present in |
| 88 |
* the regex but did not match any string; for example, given the |
| 89 |
* regex "a|(b)", the subexpressions will count the group for (b) |
| 90 |
* regardless of whether it matched a string or not. Throws an error |
| 91 |
* if the last use of =~ was a failed match. |
| 92 |
*/ |
| 93 |
function ngroups:long() |
| 94 |
%{ /* pure */ /* unprivileged */ /* pragma:tagged_dfa */ |
| 95 |
if (!CONTEXT->last_match.result) { |
| 96 |
snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer), |
| 97 |
"Attempted to get subexpression count from failed match"); |
| 98 |
CONTEXT->last_error = CONTEXT->error_buffer; |
| 99 |
} |
| 100 |
|
| 101 |
STAP_RETVALUE = CONTEXT->last_match.num_final_tags / 2; |
| 102 |
%} |
| 103 |
|
| 104 |
// XXX: perhaps implement matched_start, matched_end to get indices? |
| 105 |
// XXX: some kind of find-replace functionality? |
| 106 |
// XXX: some kind of splitting / multiple-match functionality? |