]>
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" | |
37 | #include "tools.h" | |
38 | #include "balloc.h" | |
39 | #include "s_user.h" | |
40 | #include "irc_string.h" | |
41 | ||
42 | /* | |
43 | * Simple mappings for $foo -> 'bar'. | |
44 | * Everything is a string, so typing doesn't really matter too | |
45 | * horribly much right now. | |
46 | */ | |
47 | struct substitution_variable | |
48 | { | |
49 | char *name; | |
50 | char *value; | |
51 | }; | |
52 | ||
53 | /* | |
54 | * substitution_append_var | |
55 | * | |
56 | * Inputs - A variable list (dlink_list), name -> value for mapping to make | |
57 | * Output - none | |
58 | * Side Effects - Adds a name->value mapping to a list. | |
59 | */ | |
60 | void substitution_append_var(dlink_list *varlist, const char *name, const char *value) | |
61 | { | |
62 | struct substitution_variable *tmp = MyMalloc(sizeof(struct substitution_variable)); | |
63 | ||
64 | DupString(tmp->name, name); | |
65 | DupString(tmp->value, value); | |
66 | ||
67 | dlinkAddAlloc(tmp, varlist); | |
68 | } | |
69 | ||
70 | /* | |
71 | * substitution_free | |
72 | * | |
73 | * Inputs - A dlink_list of markup variables to free. | |
74 | * Outputs - none | |
75 | * Side Effects - Empties a list of markup variables. | |
76 | */ | |
77 | void substitution_free(dlink_list *varlist) | |
78 | { | |
79 | dlink_node *nptr, *nptr2; | |
80 | ||
81 | DLINK_FOREACH_SAFE(nptr, nptr2, varlist->head) | |
82 | { | |
83 | struct substitution_variable *tmp = (struct substitution_variable *) nptr->data; | |
84 | ||
85 | dlinkDelete(nptr, varlist); | |
86 | MyFree(tmp->name); | |
87 | MyFree(tmp->value); | |
88 | MyFree(tmp); | |
89 | } | |
90 | } | |
91 | ||
92 | /* | |
93 | * substitution_parse | |
94 | * | |
95 | * Inputs - A markup string, dlink-list of markup values | |
96 | * Output - A string which has been markup-replaced. | |
97 | * Side Effects - Strings larger than BUFSIZE are terminated. | |
98 | */ | |
99 | char *substitution_parse(const char *fmt, dlink_list *varlist) | |
100 | { | |
101 | static char buf[BUFSIZE]; | |
102 | const char *ptr; | |
103 | char *bptr = buf; | |
104 | ||
105 | for (ptr = fmt; *ptr != '\0' && bptr - buf < BUFSIZE; ptr++) | |
106 | if (*ptr != '$') | |
107 | *bptr++ = *ptr; | |
108 | else if (*(ptr + 1) == '{') | |
109 | { | |
110 | static char varname[BUFSIZE]; | |
111 | char *vptr = varname; | |
112 | const char *pptr; | |
113 | dlink_node *nptr; | |
114 | ||
115 | *vptr = '\0'; | |
116 | ||
117 | /* break out ${var} */ | |
118 | for (pptr = ptr + 2; *pptr != '\0'; pptr++) | |
119 | { | |
120 | if (*pptr != '}') | |
121 | *vptr++ = *pptr; | |
122 | else | |
123 | { | |
124 | *vptr++ = '\0'; | |
125 | break; | |
126 | } | |
127 | } | |
128 | ||
129 | s_assert(*varname != '\0'); | |
130 | s_assert(*pptr != '\0'); | |
131 | ||
132 | /* advance ptr by length of variable */ | |
133 | ptr += (pptr - ptr); | |
134 | ||
135 | DLINK_FOREACH(nptr, varlist->head) | |
136 | { | |
137 | struct substitution_variable *val = (struct substitution_variable *) nptr->data; | |
138 | ||
139 | if (!strcasecmp(varname, val->name)) | |
140 | { | |
141 | strlcpy(bptr, val->value, BUFSIZE - (bptr - buf)); | |
142 | bptr += strlen(val->value); | |
143 | break; | |
144 | } | |
145 | } | |
146 | } | |
147 | ||
148 | *bptr = '\0'; | |
149 | return buf; | |
150 | } |