2007-01-25 07:08:21 +00:00
|
|
|
/*
|
|
|
|
* charybdis: an advanced ircd
|
|
|
|
* substitution.c: parses substitution-keyword expansions
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2007 William Pitcock <nenolod@nenolod.net>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdinc.h"
|
|
|
|
#include "s_user.h"
|
2009-04-08 19:30:58 +00:00
|
|
|
#include "snomask.h"
|
2008-04-20 05:47:38 +00:00
|
|
|
#include "match.h"
|
2008-04-20 13:20:10 +00:00
|
|
|
#include "substitution.h"
|
2013-09-10 05:35:56 +00:00
|
|
|
#include "s_assert.h"
|
2007-01-25 07:08:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple mappings for $foo -> 'bar'.
|
|
|
|
* Everything is a string, so typing doesn't really matter too
|
|
|
|
* horribly much right now.
|
|
|
|
*/
|
|
|
|
struct substitution_variable
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* substitution_append_var
|
|
|
|
*
|
2008-04-01 20:41:52 +00:00
|
|
|
* Inputs - A variable list (rb_dlink_list), name -> value for mapping to make
|
2007-01-25 07:08:21 +00:00
|
|
|
* Output - none
|
|
|
|
* Side Effects - Adds a name->value mapping to a list.
|
|
|
|
*/
|
2008-04-01 20:41:52 +00:00
|
|
|
void substitution_append_var(rb_dlink_list *varlist, const char *name, const char *value)
|
2007-01-25 07:08:21 +00:00
|
|
|
{
|
2008-04-01 23:07:29 +00:00
|
|
|
struct substitution_variable *tmp = rb_malloc(sizeof(struct substitution_variable));
|
2007-01-25 07:08:21 +00:00
|
|
|
|
2008-04-01 23:26:34 +00:00
|
|
|
tmp->name = rb_strdup(name);
|
|
|
|
tmp->value = rb_strdup(value);
|
2007-01-25 07:08:21 +00:00
|
|
|
|
2008-04-01 20:41:52 +00:00
|
|
|
rb_dlinkAddAlloc(tmp, varlist);
|
2007-01-25 07:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* substitution_free
|
|
|
|
*
|
2008-04-01 20:41:52 +00:00
|
|
|
* Inputs - A rb_dlink_list of markup variables to free.
|
2007-01-25 07:08:21 +00:00
|
|
|
* Outputs - none
|
|
|
|
* Side Effects - Empties a list of markup variables.
|
|
|
|
*/
|
2008-04-01 20:41:52 +00:00
|
|
|
void substitution_free(rb_dlink_list *varlist)
|
2007-01-25 07:08:21 +00:00
|
|
|
{
|
2008-04-01 20:41:52 +00:00
|
|
|
rb_dlink_node *nptr, *nptr2;
|
2007-01-25 07:08:21 +00:00
|
|
|
|
2008-04-01 20:43:10 +00:00
|
|
|
RB_DLINK_FOREACH_SAFE(nptr, nptr2, varlist->head)
|
2007-01-25 07:08:21 +00:00
|
|
|
{
|
|
|
|
struct substitution_variable *tmp = (struct substitution_variable *) nptr->data;
|
|
|
|
|
2016-02-09 16:48:28 +00:00
|
|
|
rb_dlinkDestroy(nptr, varlist);
|
2008-04-01 22:47:17 +00:00
|
|
|
rb_free(tmp->name);
|
|
|
|
rb_free(tmp->value);
|
|
|
|
rb_free(tmp);
|
2007-01-25 07:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* substitution_parse
|
|
|
|
*
|
2008-04-01 20:41:52 +00:00
|
|
|
* Inputs - A markup string, rb_dlink-list of markup values
|
2007-01-25 07:08:21 +00:00
|
|
|
* Output - A string which has been markup-replaced.
|
|
|
|
* Side Effects - Strings larger than BUFSIZE are terminated.
|
|
|
|
*/
|
2008-04-01 20:41:52 +00:00
|
|
|
char *substitution_parse(const char *fmt, rb_dlink_list *varlist)
|
2007-01-25 07:08:21 +00:00
|
|
|
{
|
|
|
|
static char buf[BUFSIZE];
|
|
|
|
const char *ptr;
|
|
|
|
char *bptr = buf;
|
|
|
|
|
2017-08-05 10:27:02 +00:00
|
|
|
for (ptr = fmt; *ptr != '\0' && bptr - buf < BUFSIZE; ptr++) {
|
|
|
|
if (*ptr != '$') {
|
2007-01-25 07:08:21 +00:00
|
|
|
*bptr++ = *ptr;
|
2017-08-05 10:27:02 +00:00
|
|
|
} else if (*(ptr + 1) == '{') {
|
|
|
|
char varname[BUFSIZE] = { 0 };
|
2007-01-25 07:08:21 +00:00
|
|
|
char *vptr = varname;
|
2008-04-01 20:41:52 +00:00
|
|
|
rb_dlink_node *nptr;
|
2007-01-25 07:08:21 +00:00
|
|
|
|
|
|
|
/* break out ${var} */
|
2017-08-05 10:27:02 +00:00
|
|
|
for (ptr += 2; *ptr != '\0'; ptr++) {
|
|
|
|
if (*ptr == '$') {
|
|
|
|
ptr--;
|
2017-07-26 21:52:51 +00:00
|
|
|
break;
|
2017-08-05 10:27:02 +00:00
|
|
|
} else if (*ptr == '}') {
|
2007-01-25 07:08:21 +00:00
|
|
|
break;
|
2017-08-05 10:27:02 +00:00
|
|
|
} else if (vptr < &varname[sizeof(varname) - 1]) {
|
|
|
|
*vptr++ = *ptr;
|
2007-01-25 07:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-05 10:27:02 +00:00
|
|
|
RB_DLINK_FOREACH(nptr, varlist->head) {
|
2007-01-25 07:08:21 +00:00
|
|
|
struct substitution_variable *val = (struct substitution_variable *) nptr->data;
|
|
|
|
|
2017-08-05 10:27:02 +00:00
|
|
|
if (!rb_strcasecmp(varname, val->name)) {
|
2017-07-27 11:58:23 +00:00
|
|
|
rb_strlcpy(bptr, val->value, sizeof(buf) - (bptr - buf));
|
2007-01-25 07:08:21 +00:00
|
|
|
bptr += strlen(val->value);
|
2017-07-27 11:58:23 +00:00
|
|
|
if (bptr >= &buf[sizeof(buf)]) {
|
|
|
|
bptr = &buf[sizeof(buf) - 1];
|
|
|
|
}
|
2007-01-25 07:08:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-26 21:52:51 +00:00
|
|
|
|
|
|
|
/* don't increment ptr into a following string if the '}' is missing */
|
|
|
|
if (*ptr == '\0')
|
|
|
|
break;
|
2007-01-25 07:08:21 +00:00
|
|
|
}
|
2017-08-05 10:27:02 +00:00
|
|
|
}
|
2007-01-25 07:08:21 +00:00
|
|
|
|
|
|
|
*bptr = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|