]>
Commit | Line | Data |
---|---|---|
92fb5c31 AC |
1 | /* |
2 | * charybdis: an advanced ircd | |
3 | * substitution.c: parses substitution-keyword expansions | |
4 | * | |
5 | * Copyright (c) 2006-2007 William Pitcock <nenolod@nenolod.net> | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions are | |
9 | * met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the above copyright notice, | |
12 | * this list of conditions and the following disclaimer. | |
13 | * | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in the | |
16 | * documentation and/or other materials provided with the distribution. | |
17 | * | |
18 | * 3. The name of the author may not be used to endorse or promote products | |
19 | * derived from this software without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
22 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
25 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
29 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
31 | * POSSIBILITY OF SUCH DAMAGE. | |
32 | * | |
33 | * $Id$ | |
34 | */ | |
35 | ||
36 | #include "stdinc.h" | |
92fb5c31 | 37 | #include "s_user.h" |
8c23bb2e | 38 | #include "snomask.h" |
4562c604 | 39 | #include "match.h" |
2e819b6b | 40 | #include "substitution.h" |
77d3d2db | 41 | #include "s_assert.h" |
92fb5c31 AC |
42 | |
43 | /* | |
44 | * Simple mappings for $foo -> 'bar'. | |
45 | * Everything is a string, so typing doesn't really matter too | |
46 | * horribly much right now. | |
47 | */ | |
48 | struct substitution_variable | |
49 | { | |
50 | char *name; | |
51 | char *value; | |
52 | }; | |
53 | ||
54 | /* | |
55 | * substitution_append_var | |
56 | * | |
330fc5c1 | 57 | * Inputs - A variable list (rb_dlink_list), name -> value for mapping to make |
92fb5c31 AC |
58 | * Output - none |
59 | * Side Effects - Adds a name->value mapping to a list. | |
60 | */ | |
330fc5c1 | 61 | void substitution_append_var(rb_dlink_list *varlist, const char *name, const char *value) |
92fb5c31 | 62 | { |
eddc2ab6 | 63 | struct substitution_variable *tmp = rb_malloc(sizeof(struct substitution_variable)); |
92fb5c31 | 64 | |
47a03750 VY |
65 | tmp->name = rb_strdup(name); |
66 | tmp->value = rb_strdup(value); | |
92fb5c31 | 67 | |
330fc5c1 | 68 | rb_dlinkAddAlloc(tmp, varlist); |
92fb5c31 AC |
69 | } |
70 | ||
71 | /* | |
72 | * substitution_free | |
73 | * | |
330fc5c1 | 74 | * Inputs - A rb_dlink_list of markup variables to free. |
92fb5c31 AC |
75 | * Outputs - none |
76 | * Side Effects - Empties a list of markup variables. | |
77 | */ | |
330fc5c1 | 78 | void substitution_free(rb_dlink_list *varlist) |
92fb5c31 | 79 | { |
330fc5c1 | 80 | rb_dlink_node *nptr, *nptr2; |
92fb5c31 | 81 | |
5cefa1d6 | 82 | RB_DLINK_FOREACH_SAFE(nptr, nptr2, varlist->head) |
92fb5c31 AC |
83 | { |
84 | struct substitution_variable *tmp = (struct substitution_variable *) nptr->data; | |
85 | ||
55d5f797 | 86 | rb_dlinkDestroy(nptr, varlist); |
637c4932 VY |
87 | rb_free(tmp->name); |
88 | rb_free(tmp->value); | |
89 | rb_free(tmp); | |
92fb5c31 AC |
90 | } |
91 | } | |
92 | ||
93 | /* | |
94 | * substitution_parse | |
95 | * | |
330fc5c1 | 96 | * Inputs - A markup string, rb_dlink-list of markup values |
92fb5c31 AC |
97 | * Output - A string which has been markup-replaced. |
98 | * Side Effects - Strings larger than BUFSIZE are terminated. | |
99 | */ | |
330fc5c1 | 100 | char *substitution_parse(const char *fmt, rb_dlink_list *varlist) |
92fb5c31 AC |
101 | { |
102 | static char buf[BUFSIZE]; | |
103 | const char *ptr; | |
104 | char *bptr = buf; | |
105 | ||
106 | for (ptr = fmt; *ptr != '\0' && bptr - buf < BUFSIZE; ptr++) | |
107 | if (*ptr != '$') | |
108 | *bptr++ = *ptr; | |
109 | else if (*(ptr + 1) == '{') | |
110 | { | |
111 | static char varname[BUFSIZE]; | |
112 | char *vptr = varname; | |
113 | const char *pptr; | |
330fc5c1 | 114 | rb_dlink_node *nptr; |
92fb5c31 AC |
115 | |
116 | *vptr = '\0'; | |
117 | ||
118 | /* break out ${var} */ | |
119 | for (pptr = ptr + 2; *pptr != '\0'; pptr++) | |
120 | { | |
121 | if (*pptr != '}') | |
122 | *vptr++ = *pptr; | |
123 | else | |
124 | { | |
125 | *vptr++ = '\0'; | |
126 | break; | |
127 | } | |
128 | } | |
129 | ||
130 | s_assert(*varname != '\0'); | |
131 | s_assert(*pptr != '\0'); | |
132 | ||
133 | /* advance ptr by length of variable */ | |
134 | ptr += (pptr - ptr); | |
135 | ||
5cefa1d6 | 136 | RB_DLINK_FOREACH(nptr, varlist->head) |
92fb5c31 AC |
137 | { |
138 | struct substitution_variable *val = (struct substitution_variable *) nptr->data; | |
139 | ||
140 | if (!strcasecmp(varname, val->name)) | |
141 | { | |
f427c8b0 | 142 | rb_strlcpy(bptr, val->value, BUFSIZE - (bptr - buf)); |
92fb5c31 AC |
143 | bptr += strlen(val->value); |
144 | break; | |
145 | } | |
146 | } | |
147 | } | |
148 | ||
149 | *bptr = '\0'; | |
150 | return buf; | |
151 | } |