regexp.c File Reference

Implementation of regular expression routines. More...

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include "util.h"
#include "regexp.h"

Go to the source code of this file.

Functions

const char * regexp_error_string (int error)
 Convert a pcre regular expression error code into a string.
int regexp_compile (struct regexp_t *regexp)
 Compile a PCRE regular expression into an optimized format for subsequent execution.
int regexp_search (struct regexp_t *regexp, const char *subject)
 Execute a PCRE regular search against the subject string.
int regexp_substring (struct regexp_t *regexp, const int match_number, char *buffer, int buffer_size)
 Get the substring matched by a specific match group.
int regexp_free (struct regexp_t *regexp)
 Release the resources associated with a regular expression object.


Detailed Description

Implementation of regular expression routines.

Definition in file regexp.c.


Function Documentation

int regexp_compile ( struct regexp_t regexp  ) 

Compile a PCRE regular expression into an optimized format for subsequent execution.

Parameters:
[out] regexp Pointer to variable to receive compiled regular expression
Returns:
SUCCESS (0) or non-zero error code otherwise

Definition at line 102 of file regexp.c.

Referenced by regexp_init().

00103 {
00104     const char *error_str;
00105     int error_code, error_offset;
00106 
00107     if ((regexp->re = pcre_compile2(regexp->pattern, 0, &error_code,
00108                                     &error_str, &error_offset, NULL)) == NULL) {
00109         int i;
00110         char *ruler = malloc(error_offset + 2);
00111 
00112         if (ruler) {            /* point to where error is in pattern */
00113             for (i = 0; i < error_offset; i++) ruler[i] = ' ';
00114             ruler[error_offset] = '^';
00115             ruler[error_offset + 1] = 0;
00116         }
00117 
00118         log_msg(LOG_ERROR, _("regexp compilation failed at offset %d\n%s\n%s %s\n"),
00119                 error_offset, regexp->pattern, ruler, error_str);
00120         free(ruler);
00121         return error_code;
00122     }
00123     return SUCCESS;
00124 }

const char* regexp_error_string ( int  error  ) 

Convert a pcre regular expression error code into a string.

Parameters:
[in] error The error code whose string representation is desired
Returns:
Pointer to static string

Definition at line 66 of file regexp.c.

Referenced by regexp_search(), and regexp_substring().

00067 {
00068     switch(error) {
00069     case PCRE_ERROR_NOMATCH:        return "PCRE_ERROR_NOMATCH";
00070     case PCRE_ERROR_NULL:           return "PCRE_ERROR_NULL";
00071     case PCRE_ERROR_BADOPTION:      return "PCRE_ERROR_BADOPTION";
00072     case PCRE_ERROR_BADMAGIC:       return "PCRE_ERROR_BADMAGIC";
00073     case PCRE_ERROR_UNKNOWN_OPCODE: return "PCRE_ERROR_UNKNOWN_OPCODE";
00074     case PCRE_ERROR_NOMEMORY:       return "PCRE_ERROR_NOMEMORY";
00075     case PCRE_ERROR_NOSUBSTRING:    return "PCRE_ERROR_NOSUBSTRING";
00076     case PCRE_ERROR_MATCHLIMIT:     return "PCRE_ERROR_MATCHLIMIT";
00077     case PCRE_ERROR_CALLOUT:        return "PCRE_ERROR_CALLOUT";
00078     case PCRE_ERROR_BADUTF8:        return "PCRE_ERROR_BADUTF8";
00079     case PCRE_ERROR_BADUTF8_OFFSET: return "PCRE_ERROR_BADUTF8_OFFSET";
00080     case PCRE_ERROR_PARTIAL:        return "PCRE_ERROR_PARTIAL";
00081     case PCRE_ERROR_BADPARTIAL:     return "PCRE_ERROR_BADPARTIAL";
00082     case PCRE_ERROR_INTERNAL:       return "PCRE_ERROR_INTERNAL";
00083     case PCRE_ERROR_BADCOUNT:       return "PCRE_ERROR_BADCOUNT";
00084     case PCRE_ERROR_DFA_UITEM:      return "PCRE_ERROR_DFA_UITEM";
00085     case PCRE_ERROR_DFA_UCOND:      return "PCRE_ERROR_DFA_UCOND";
00086     case PCRE_ERROR_DFA_UMLIMIT:    return "PCRE_ERROR_DFA_UMLIMIT";
00087     case PCRE_ERROR_DFA_WSSIZE:     return "PCRE_ERROR_DFA_WSSIZE";
00088     case PCRE_ERROR_DFA_RECURSE:    return "PCRE_ERROR_DFA_RECURSE";
00089     case PCRE_ERROR_RECURSIONLIMIT: return "PCRE_ERROR_RECURSIONLIMIT";
00090     case PCRE_ERROR_NULLWSLIMIT:    return "PCRE_ERROR_NULLWSLIMIT";
00091     case PCRE_ERROR_BADNEWLINE:     return "PCRE_ERROR_BADNEWLINE";
00092     default:                        return "unknown";
00093     }
00094 }

int regexp_free ( struct regexp_t regexp  ) 

Release the resources associated with a regular expression object.

Parameters:
[in] regexp Pointer to a pointer to a regular expression object
Returns:
SUCCESS (0) or non-zero error code otherwise

Definition at line 194 of file regexp.c.

Referenced by regexp_fini().

00195 {
00196     if (regexp->re) pcre_free(regexp->re);
00197     return SUCCESS;
00198 }

int regexp_search ( struct regexp_t regexp,
const char *  subject 
)

Execute a PCRE regular search against the subject string.

Parameters:
[in] regexp Previously compiled regular expression object
[in] subject The string which is being searched for the regular expression match
Returns:
The number of matches found

Definition at line 135 of file regexp.c.

Referenced by is_log_backup().

00136 {
00137     int result;
00138 
00139     regexp->subject = subject;
00140     result = pcre_exec(regexp->re, NULL,
00141                        subject, strlen(subject), 0, 0,
00142                        regexp->matches, sizeof(regexp->matches)/sizeof(int));
00143 
00144     if (result < 0) {
00145         regexp->num_matches = 0;
00146         regexp->error = result;
00147         if (result != PCRE_ERROR_NOMATCH) {
00148             log_msg(LOG_ERROR, _("regexp_search failed, error=%s(%d) pattern=\"%s\" subject=\"%s\"\n"),
00149                     regexp_error_string(regexp->error), regexp->error, regexp->pattern, subject);
00150         }
00151 
00152     } else {
00153         regexp->num_matches = result;
00154         regexp->error = 0;
00155     }
00156     return regexp->num_matches;
00157 }

int regexp_substring ( struct regexp_t regexp,
const int  match_number,
char *  buffer,
int  buffer_size 
)

Get the substring matched by a specific match group.

Parameters:
[in] regexp The regular expression object
[in] match_number The index of the match group
[out] buffer The substring is written into this location
[in] buffer_size The size of the buffer to receive the substring
Returns:
SUCCESS (0) or non-zero error code otherwise

Definition at line 168 of file regexp.c.

Referenced by is_log_backup().

00169 {
00170     if (buffer && buffer_size > 0) buffer[0] = 0; /* always NULL terminate buffer */
00171 
00172     if (match_number >= regexp->num_matches) {
00173         return PCRE_ERROR_NOSUBSTRING;
00174     }
00175 
00176     if ((regexp->error = pcre_copy_substring(regexp->subject, regexp->matches, regexp->num_matches,
00177                                      match_number, buffer, buffer_size)) < 0) {
00178         log_msg(LOG_ERROR, _("regexp_substring failed, match_number=%d num_matches=%d error=%s(%d) pattern=\"%s\" subject=\"%s\"\n"),
00179                 match_number, regexp->num_matches,
00180                 regexp_error_string(regexp->error), regexp->error, regexp->pattern, regexp->subject);
00181         return regexp->error;
00182     }
00183     return SUCCESS;
00184 }


Generated on Mon Aug 31 10:06:21 2009 by  doxygen 1.5.8