comparison src/core/ngx_regex.c @ 0:f0b350454894 NGINX_0_1_0

nginx 0.1.0 *) The first public version.
author Igor Sysoev <http://sysoev.ru>
date Mon, 04 Oct 2004 00:00:00 +0400
parents
children 46833bd150cb
comparison
equal deleted inserted replaced
-1:000000000000 0:f0b350454894
1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9
10
11 static void *ngx_regex_malloc(size_t size);
12 static void ngx_regex_free(void *p);
13
14
15 static ngx_pool_t *ngx_pcre_pool;
16
17
18 void ngx_regex_init()
19 {
20 pcre_malloc = ngx_regex_malloc;
21 pcre_free = ngx_regex_free;
22 }
23
24
25 ngx_regex_t *ngx_regex_compile(ngx_str_t *pattern, ngx_int_t options,
26 ngx_pool_t *pool, ngx_str_t *err)
27 {
28 int erroff;
29 const char *errstr;
30 ngx_regex_t *re;
31 #if (NGX_THREADS)
32 ngx_core_tls_t *tls;
33
34 #if (NGX_SUPPRESS_WARN)
35 tls = NULL;
36 #endif
37
38 if (ngx_threaded) {
39 tls = ngx_thread_get_tls(ngx_core_tls_key);
40 tls->pool = pool;
41 } else {
42 ngx_pcre_pool = pool;
43 }
44
45 #else
46
47 ngx_pcre_pool = pool;
48
49 #endif
50
51 re = pcre_compile((const char *) pattern->data, (int) options,
52 &errstr, &erroff, NULL);
53
54 if (re == NULL) {
55 if ((size_t) erroff == pattern->len) {
56 ngx_snprintf((char *) err->data, err->len - 1,
57 "pcre_compile() failed: %s in \"%s\"",
58 errstr, pattern->data);
59 } else {
60 ngx_snprintf((char *) err->data, err->len - 1,
61 "pcre_compile() failed: %s in \"%s\" at \"%s\"",
62 errstr, pattern->data, pattern->data + erroff);
63 }
64 }
65
66 /* ensure that there is no current pool */
67
68 #if (NGX_THREADS)
69 if (ngx_threaded) {
70 tls->pool = NULL;
71 } else {
72 ngx_pcre_pool = NULL;
73 }
74 #else
75 ngx_pcre_pool = NULL;
76 #endif
77
78 return re;
79 }
80
81
82 ngx_int_t ngx_regex_exec(ngx_regex_t *re, ngx_str_t *s,
83 int *matches, ngx_int_t size)
84 {
85 int rc;
86
87 rc = pcre_exec(re, NULL, (const char *) s->data, s->len, 0, 0,
88 matches, size);
89
90 if (rc == -1) {
91 return NGX_DECLINED;
92 }
93
94 return rc;
95 }
96
97
98 static void *ngx_regex_malloc(size_t size)
99 {
100 ngx_pool_t *pool;
101 #if (NGX_THREADS)
102 ngx_core_tls_t *tls;
103
104 if (ngx_threaded) {
105 tls = ngx_thread_get_tls(ngx_core_tls_key);
106 pool = tls->pool;
107 } else {
108 pool = ngx_pcre_pool;
109 }
110 #else
111 pool = ngx_pcre_pool;
112 #endif
113
114 if (pool) {
115 return ngx_palloc(pool, size);
116 }
117
118 return NULL;
119 }
120
121
122 static void ngx_regex_free(void *p)
123 {
124 return;
125 }