You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bugs@httpd.apache.org by bu...@apache.org on 2002/07/21 20:14:29 UTC

DO NOT REPLY [Bug 11018] New: - export regex subexpressions to environment

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=11018>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=11018

export regex subexpressions to environment

           Summary: export regex subexpressions to environment
           Product: Apache httpd-1.3
           Version: 1.3.26
          Platform: PC
        OS/Version: FreeBSD
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: mod_include
        AssignedTo: bugs@httpd.apache.org
        ReportedBy: apache-include@oldach.net


mod_include allows for regexp pattern matching if the right operand of the = or
!= operator is a /pattern/. Extended regexp's (which are used by mod_include)
allow for match of sub-expressions in parentheses. It would be useful (and
actually a quite simple modification) to be able to use the sub-expressions
later in the document. For example:

        <!--#if expr="$REQUEST_URI = '/(blue|yellow).*(keen|nervous)/'" -->
        we are <!--#echo var="1" --> and mood is <!--#echo var="2" --> today
        <!--#else -->
        no special color or mood today
        <!--#endif -->

Another example: I am using this feature to dynamically customize a navigation
bar depending on *where* in the navigation tree I am currently. This allows me
to #include a general header file in the HTML documents that "knows" at which
position in the navigation tree we are. It just examines REQUEST_URI and for
instance doesn't emit an HTML anchor for the current document but just for the
others.

The modification is quite simple and straightforward. A patch (relative to
1.3.26's mod_include.c) follows:

@@ -1202,15 +1206,27 @@
 static int re_check(request_rec *r, char *string, char *rexp)
 {
     regex_t *compiled;
+    regmatch_t *regmatch;
     int regex_error;
 
-    compiled = ap_pregcomp(r->pool, rexp, REG_EXTENDED | REG_NOSUB);
+    compiled = ap_pregcomp(r->pool, rexp, REG_EXTENDED);
     if (compiled == NULL) {
         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
                     "unable to compile pattern \"%s\"", rexp);
         return -1;
     }
-    regex_error = ap_regexec(compiled, string, 0, (regmatch_t *) NULL, 0);
+    regmatch = (regmatch_t *) ap_palloc(r->pool, (compiled->re_nsub+1) *
sizeof(regmatch_t));
+    regex_error = ap_regexec(compiled, string, compiled->re_nsub+1, regmatch,
0);
+    if (!regex_error) {
+       int i;
+       for (i = 1; i <= compiled->re_nsub; ++i) {
+           char *mark = ap_psprintf(r->pool, "$%d", i);
+           char *result = ap_palloc(r->pool, regmatch[i].rm_eo -
regmatch[i].rm_so + 1);
+           memcpy(result, string + regmatch[i].rm_so, regmatch[i].rm_eo -
regmatch[i].rm_so);
+           result[regmatch[i].rm_eo - regmatch[i].rm_so] = '\0';
+           ap_table_setn(r->subprocess_env, &mark[1], result);
+       }
+    }
     ap_pregfree(r->pool, compiled);
     return (!regex_error);
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: bugs-unsubscribe@httpd.apache.org
For additional commands, e-mail: bugs-help@httpd.apache.org