blob: 1455fb4529d87bd1c3d1c5215f994e4efa47643e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Regex and string management functions.
3 *
Willy Tarreauf4f04122010-01-28 18:10:50 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <stdlib.h>
15#include <string.h>
16
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020017#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020018#include <common/regex.h>
19#include <common/standard.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020#include <proto/log.h>
21
22/* regex trash buffer used by various regex tests */
23regmatch_t pmatch[MAX_MATCH]; /* rm_so, rm_eo for regular expressions */
24
25
Willy Tarreaub17916e2006-10-15 15:17:57 +020026int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches)
Willy Tarreaubaaee002006-06-26 02:48:02 +020027{
28 char *old_dst = dst;
29
30 while (*str) {
31 if (*str == '\\') {
32 str++;
Willy Tarreau8f8e6452007-06-17 21:51:38 +020033 if (isdigit((unsigned char)*str)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020034 int len, num;
35
36 num = *str - '0';
37 str++;
38
39 if (matches[num].rm_eo > -1 && matches[num].rm_so > -1) {
40 len = matches[num].rm_eo - matches[num].rm_so;
41 memcpy(dst, src + matches[num].rm_so, len);
42 dst += len;
43 }
44
45 } else if (*str == 'x') {
46 unsigned char hex1, hex2;
47 str++;
48
49 hex1 = toupper(*str++) - '0';
50 hex2 = toupper(*str++) - '0';
51
52 if (hex1 > 9) hex1 -= 'A' - '9' - 1;
53 if (hex2 > 9) hex2 -= 'A' - '9' - 1;
54 *dst++ = (hex1<<4) + hex2;
55 } else {
56 *dst++ = *str++;
57 }
58 } else {
59 *dst++ = *str++;
60 }
61 }
62 *dst = '\0';
63 return dst - old_dst;
64}
65
66/* returns NULL if the replacement string <str> is valid, or the pointer to the first error */
Willy Tarreaub17916e2006-10-15 15:17:57 +020067const char *check_replace_string(const char *str)
Willy Tarreaubaaee002006-06-26 02:48:02 +020068{
Willy Tarreaub17916e2006-10-15 15:17:57 +020069 const char *err = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020070 while (*str) {
71 if (*str == '\\') {
72 err = str; /* in case of a backslash, we return the pointer to it */
73 str++;
74 if (!*str)
75 return err;
Willy Tarreau8f8e6452007-06-17 21:51:38 +020076 else if (isdigit((unsigned char)*str))
Willy Tarreaubaaee002006-06-26 02:48:02 +020077 err = NULL;
78 else if (*str == 'x') {
79 str++;
80 if (!ishex(*str))
81 return err;
82 str++;
83 if (!ishex(*str))
84 return err;
85 err = NULL;
86 }
87 else {
88 Warning("'\\%c' : deprecated use of a backslash before something not '\\','x' or a digit.\n", *str);
89 err = NULL;
90 }
91 }
92 str++;
93 }
94 return err;
95}
96
97
98/* returns the pointer to an error in the replacement string, or NULL if OK */
Willy Tarreaub17916e2006-10-15 15:17:57 +020099const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
Willy Tarreauf4f04122010-01-28 18:10:50 +0100100 int action, const char *replace, void *cond)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101{
102 struct hdr_exp *exp;
103
104 if (replace != NULL) {
Willy Tarreaub17916e2006-10-15 15:17:57 +0200105 const char *err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106 err = check_replace_string(replace);
107 if (err)
108 return err;
109 }
110
111 while (*head != NULL)
112 head = &(*head)->next;
113
114 exp = calloc(1, sizeof(struct hdr_exp));
115
116 exp->preg = preg;
117 exp->replace = replace;
118 exp->action = action;
Willy Tarreauf4f04122010-01-28 18:10:50 +0100119 exp->cond = cond;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120 *head = exp;
121
122 return NULL;
123}
124
125
126
127/*
128 * Local variables:
129 * c-indent-level: 8
130 * c-basic-offset: 8
131 * End:
132 */