libpcre_sys/
lib.rs

1// Copyright 2015 The rust-pcre authors.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9extern crate libc;
10
11use libc::{c_char, c_int, c_uchar, c_ulong, c_void};
12use std::option::{Option};
13use std::ptr;
14
15#[allow(non_camel_case_types)]
16pub type compile_options = c_int;
17#[allow(non_camel_case_types)]
18pub type exec_options = c_int;
19#[allow(non_camel_case_types)]
20pub type fullinfo_field = c_int;
21#[allow(non_camel_case_types)]
22pub type study_options = c_int;
23
24pub const PCRE_UTF8: compile_options = 0x00000800;
25
26// PCRE_NO_UTF8_CHECK is both a compile and exec option
27pub const PCRE_NO_UTF8_CHECK: c_int = 0x00002000;
28
29pub const PCRE_ERROR_NOMATCH: c_int = -1;
30pub const PCRE_ERROR_NULL: c_int = -2;
31
32pub const PCRE_INFO_CAPTURECOUNT: fullinfo_field = 2;
33pub const PCRE_INFO_NAMEENTRYSIZE: fullinfo_field = 7;
34pub const PCRE_INFO_NAMECOUNT: fullinfo_field = 8;
35pub const PCRE_INFO_NAMETABLE: fullinfo_field = 9;
36
37//const PCRE_EXTRA_STUDY_DATA: c_ulong = 0x0001;
38const PCRE_EXTRA_MATCH_LIMIT: c_ulong = 0x0002;
39//const PCRE_EXTRA_CALLOUT_DATA: c_ulong = 0x0004;
40//const PCRE_EXTRA_TABLES: c_ulong = 0x0008;
41const PCRE_EXTRA_MATCH_LIMIT_RECURSION: c_ulong = 0x0010;
42const PCRE_EXTRA_MARK: c_ulong = 0x0020;
43//const PCRE_EXTRA_EXECUTABLE_JIT: c_ulong = 0x0040;
44
45#[allow(non_camel_case_types)]
46pub enum pcre {}
47
48#[allow(non_camel_case_types)]
49#[repr(C)]
50pub struct pcre_extra {
51    flags: c_ulong,
52    study_data: *mut c_void,
53    match_limit_: c_ulong,
54    callout_data: *mut c_void,
55    tables: *const c_uchar,
56    match_limit_recursion_: c_ulong,
57    mark: *mut *mut c_uchar,
58    executable_jit: *mut c_void
59}
60
61impl pcre_extra {
62    /// Returns the match limit, if previously set by [set_match_limit()](#method.set_match_limit).
63    ///
64    /// The default value for this limit is set when PCRE is built. The default default is 10 million.
65    pub fn match_limit(&self) -> Option<usize> {
66        if (self.flags & PCRE_EXTRA_MATCH_LIMIT) == 0 {
67            None
68        } else {
69            Some(self.match_limit_ as usize)
70        }
71    }
72
73    /// Sets the match limit to `limit` instead of using PCRE's default.
74    pub fn set_match_limit(&mut self, limit: u32) {
75        self.flags |= PCRE_EXTRA_MATCH_LIMIT;
76        self.match_limit_ = limit as c_ulong;
77    }
78
79    /// Returns the recursion depth limit, if previously set by [set_match_limit_recursion()](#method.set_match_limit_recursion).
80    ///
81    /// The default value for this limit is set when PCRE is built.
82    pub fn match_limit_recursion(&self) -> Option<usize> {
83        if (self.flags & PCRE_EXTRA_MATCH_LIMIT_RECURSION) == 0 {
84            None
85        } else {
86            Some(self.match_limit_recursion_ as usize)
87        }
88    }
89
90    /// Sets the recursion depth limit to `limit` instead of using PCRE's default.
91    pub fn set_match_limit_recursion(&mut self, limit: u32) {
92        self.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
93        self.match_limit_ = limit as c_ulong;
94    }
95
96    /// Sets the mark field.
97    pub unsafe fn set_mark(&mut self, mark: &mut *mut c_uchar) {
98        self.flags |= PCRE_EXTRA_MARK;
99        self.mark = mark as *mut *mut c_uchar;
100    }
101
102    /// Unsets the mark field. PCRE will not save mark names when matching the compiled regular expression.
103    pub fn unset_mark(&mut self) {
104        self.flags &= !PCRE_EXTRA_MARK;
105        self.mark = ptr::null_mut();
106    }
107}
108
109#[link(name = "pcre")]
110extern {
111    pub static pcre_free: extern "C" fn(ptr: *mut c_void);
112
113    pub fn pcre_compile(pattern: *const c_char, options: compile_options, errptr: *mut *const c_char, erroffset: *mut c_int, tableptr: *const c_uchar) -> *mut pcre;
114    pub fn pcre_exec(code: *const pcre, extra: *const pcre_extra, subject: *const c_char, length: c_int, startoffset: c_int, options: exec_options, ovector: *mut c_int, ovecsize: c_int) -> c_int;
115    pub fn pcre_free_study(extra: *mut pcre_extra);
116    pub fn pcre_fullinfo(code: *const pcre, extra: *const pcre_extra, what: fullinfo_field, where_: *mut c_void) -> c_int;
117    // Note: libpcre's pcre_refcount() function is not thread-safe.
118    pub fn pcre_refcount(code: *mut pcre, adjust: c_int) -> c_int;
119    pub fn pcre_study(code: *const pcre, options: study_options, errptr: *mut *const c_char) -> *mut pcre_extra;
120    pub fn pcre_version() -> *const c_char;
121}