]>
jfr.im git - solanum.git/blob - src/substitution.c
2 * charybdis: an advanced ircd
3 * substitution.c: parses substitution-keyword expansions
5 * Copyright (c) 2006-2007 William Pitcock <nenolod@nenolod.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
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.
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.
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.
38 #include "irc_string.h"
41 * Simple mappings for $foo -> 'bar'.
42 * Everything is a string, so typing doesn't really matter too
43 * horribly much right now.
45 struct substitution_variable
52 * substitution_append_var
54 * Inputs - A variable list (rb_dlink_list), name -> value for mapping to make
56 * Side Effects - Adds a name->value mapping to a list.
58 void substitution_append_var(rb_dlink_list
*varlist
, const char *name
, const char *value
)
60 struct substitution_variable
*tmp
= rb_malloc(sizeof(struct substitution_variable
));
62 tmp
->name
= rb_strdup(name
);
63 tmp
->value
= rb_strdup(value
);
65 rb_dlinkAddAlloc(tmp
, varlist
);
71 * Inputs - A rb_dlink_list of markup variables to free.
73 * Side Effects - Empties a list of markup variables.
75 void substitution_free(rb_dlink_list
*varlist
)
77 rb_dlink_node
*nptr
, *nptr2
;
79 RB_DLINK_FOREACH_SAFE(nptr
, nptr2
, varlist
->head
)
81 struct substitution_variable
*tmp
= (struct substitution_variable
*) nptr
->data
;
83 rb_dlinkDelete(nptr
, varlist
);
93 * Inputs - A markup string, rb_dlink-list of markup values
94 * Output - A string which has been markup-replaced.
95 * Side Effects - Strings larger than BUFSIZE are terminated.
97 char *substitution_parse(const char *fmt
, rb_dlink_list
*varlist
)
99 static char buf
[BUFSIZE
];
103 for (ptr
= fmt
; *ptr
!= '\0' && bptr
- buf
< BUFSIZE
; ptr
++)
106 else if (*(ptr
+ 1) == '{')
108 static char varname
[BUFSIZE
];
109 char *vptr
= varname
;
115 /* break out ${var} */
116 for (pptr
= ptr
+ 2; *pptr
!= '\0'; pptr
++)
127 s_assert(*varname
!= '\0');
128 s_assert(*pptr
!= '\0');
130 /* advance ptr by length of variable */
133 RB_DLINK_FOREACH(nptr
, varlist
->head
)
135 struct substitution_variable
*val
= (struct substitution_variable
*) nptr
->data
;
137 if (!strcasecmp(varname
, val
->name
))
139 strlcpy(bptr
, val
->value
, BUFSIZE
- (bptr
- buf
));
140 bptr
+= strlen(val
->value
);