1 #ifndef LIBRARIES_TRE_H
    2 #define LIBRARIES_TRE_H
    3 
    4 
    5 #include "tre-config.h"
    6 
    7 #ifdef HAVE_SYS_TYPES_H
    8 #include <sys/types.h>
    9 #endif /* HAVE_SYS_TYPES_H */
   10 
   11 #ifdef HAVE_LIBUTF8_H
   12 #include <libutf8.h>
   13 #endif /* HAVE_LIBUTF8_H */
   14 
   15 #ifdef TRE_USE_SYSTEM_REGEX_H
   16 /* Include the system regex.h to make TRE ABI compatible with the
   17    system regex. */
   18 #include TRE_SYSTEM_REGEX_H_PATH
   19 #define tre_regcomp  regcomp
   20 #define tre_regexec  regexec
   21 #define tre_regerror regerror
   22 #define tre_regfree  regfree
   23 #endif /* TRE_USE_SYSTEM_REGEX_H */
   24 
   25 #ifdef __cplusplus
   26 extern "C" {
   27 #endif
   28 
   29 #ifdef TRE_USE_SYSTEM_REGEX_H
   30 
   31 #ifndef REG_OK
   32 #define REG_OK 0
   33 #endif /* !REG_OK */
   34 
   35 #ifndef HAVE_REG_ERRCODE_T
   36 typedef int reg_errcode_t;
   37 #endif /* !HAVE_REG_ERRCODE_T */
   38 
   39 #if !defined(REG_NOSPEC) && !defined(REG_LITERAL)
   40 #define REG_LITERAL 0x1000
   41 #endif
   42 
   43 /* Extra tre_regcomp() flags. */
   44 #ifndef REG_BASIC
   45 #define REG_BASIC	0
   46 #endif /* !REG_BASIC */
   47 #define REG_RIGHT_ASSOC (REG_LITERAL << 1)
   48 #define REG_UNGREEDY    (REG_RIGHT_ASSOC << 1)
   49 
   50 /* Extra tre_regexec() flags. */
   51 #define REG_APPROX_MATCHER	 0x1000
   52 #define REG_BACKTRACKING_MATCHER (REG_APPROX_MATCHER << 1)
   53 
   54 #else /* !TRE_USE_SYSTEM_REGEX_H */
   55 
   56 /* If the we're not using system regex.h, we need to define the
   57    structs and enums ourselves. */
   58 
   59 typedef int regoff_t;
   60 typedef struct {
   61   size_t re_nsub;  /* Number of parenthesized subexpressions. */
   62   void *value;	   /* For internal use only. */
   63 } regex_t;
   64 
   65 typedef struct {
   66   regoff_t rm_so;
   67   regoff_t rm_eo;
   68 } regmatch_t;
   69 
   70 
   71 typedef enum {
   72   REG_OK = 0,		/* No error. */
   73   /* POSIX tre_regcomp() return error codes.  (In the order listed in the
   74      standard.)	 */
   75   REG_NOMATCH,		/* No match. */
   76   REG_BADPAT,		/* Invalid regexp. */
   77   REG_ECOLLATE,		/* Unknown collating element. */
   78   REG_ECTYPE,		/* Unknown character class name. */
   79   REG_EESCAPE,		/* Trailing backslash. */
   80   REG_ESUBREG,		/* Invalid back reference. */
   81   REG_EBRACK,		/* "[]" imbalance */
   82   REG_EPAREN,		/* "\(\)" or "()" imbalance */
   83   REG_EBRACE,		/* "\{\}" or "{}" imbalance */
   84   REG_BADBR,		/* Invalid content of {} */
   85   REG_ERANGE,		/* Invalid use of range operator */
   86   REG_ESPACE,		/* Out of memory.  */
   87   REG_BADRPT            /* Invalid use of repetition operators. */
   88 } reg_errcode_t;
   89 
   90 /* POSIX tre_regcomp() flags. */
   91 #define REG_EXTENDED	1
   92 #define REG_ICASE	(REG_EXTENDED << 1)
   93 #define REG_NEWLINE	(REG_ICASE << 1)
   94 #define REG_NOSUB	(REG_NEWLINE << 1)
   95 
   96 /* Extra tre_regcomp() flags. */
   97 #define REG_BASIC	0
   98 #define REG_LITERAL	(REG_NOSUB << 1)
   99 #define REG_RIGHT_ASSOC (REG_LITERAL << 1)
  100 #define REG_UNGREEDY    (REG_RIGHT_ASSOC << 1)
  101 
  102 /* POSIX tre_regexec() flags. */
  103 #define REG_NOTBOL 1
  104 #define REG_NOTEOL (REG_NOTBOL << 1)
  105 
  106 /* Extra tre_regexec() flags. */
  107 #define REG_APPROX_MATCHER	 (REG_NOTEOL << 1)
  108 #define REG_BACKTRACKING_MATCHER (REG_APPROX_MATCHER << 1)
  109 
  110 #endif /* !TRE_USE_SYSTEM_REGEX_H */
  111 
  112 /* REG_NOSPEC and REG_LITERAL mean the same thing. */
  113 #if defined(REG_LITERAL) && !defined(REG_NOSPEC)
  114 #define REG_NOSPEC	REG_LITERAL
  115 #elif defined(REG_NOSPEC) && !defined(REG_LITERAL)
  116 #define REG_LITERAL	REG_NOSPEC
  117 #endif /* defined(REG_NOSPEC) */
  118 
  119 /* The maximum number of iterations in a bound expression. */
  120 #undef RE_DUP_MAX
  121 #define RE_DUP_MAX 255
  122 
  123 #ifdef TRE_WCHAR
  124 #ifdef HAVE_WCHAR_H
  125 #include <wchar.h>
  126 #endif /* HAVE_WCHAR_H */
  127 #endif /* TRE_WCHAR */
  128 
  129 
  130 /* Approximate matching parameter struct. */
  131 typedef struct {
  132   int cost_ins;	       /* Default cost of an inserted character. */
  133   int cost_del;	       /* Default cost of a deleted character. */
  134   int cost_subst;      /* Default cost of a substituted character. */
  135   int max_cost;	       /* Maximum allowed cost of a match. */
  136 
  137   int max_ins;	       /* Maximum allowed number of inserts. */
  138   int max_del;	       /* Maximum allowed number of deletes. */
  139   int max_subst;       /* Maximum allowed number of substitutes. */
  140   int max_err;	       /* Maximum allowed number of errors total. */
  141 } regaparams_t;
  142 
  143 /* Approximate matching result struct. */
  144 typedef struct {
  145   size_t nmatch;       /* Length of pmatch[] array. */
  146   regmatch_t *pmatch;  /* Submatch data. */
  147   int cost;	       /* Cost of the match. */
  148   int num_ins;	       /* Number of inserts in the match. */
  149   int num_del;	       /* Number of deletes in the match. */
  150   int num_subst;       /* Number of substitutes in the match. */
  151 } regamatch_t;
  152 
  153 #ifdef TRE_WCHAR
  154 typedef wchar_t tre_char_t;
  155 #else /* !TRE_WCHAR */
  156 typedef unsigned char tre_char_t;
  157 #endif /* !TRE_WCHAR */
  158 
  159 typedef struct {
  160   int (*get_next_char)(tre_char_t *c, unsigned int *pos_add, void *context);
  161   void (*rewind)(size_t pos, void *context);
  162   int (*compare)(size_t pos1, size_t pos2, size_t len, void *context);
  163   void *context;
  164 } tre_str_source;
  165 
  166 enum {
  167   TRE_CONFIG_APPROX,
  168   TRE_CONFIG_WCHAR,
  169   TRE_CONFIG_MULTIBYTE,
  170   TRE_CONFIG_SYSTEM_ABI,
  171   TRE_CONFIG_VERSION
  172 };
  173 
  174 
  175 #ifdef __cplusplus
  176 }
  177 #endif
  178 
  179 #endif /* LIBRARIES_TRE_H */
  180