2006-09-20 05:31:51 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2006 Jon Loeliger
|
|
|
|
*/
|
|
|
|
|
2006-09-27 20:16:10 +04:00
|
|
|
#include "git-compat-util.h"
|
2006-09-20 05:31:51 +04:00
|
|
|
#include "interpolate.h"
|
|
|
|
|
|
|
|
|
2006-09-28 23:12:55 +04:00
|
|
|
void interp_set_entry(struct interp *table, int slot, const char *value)
|
2006-09-27 20:16:10 +04:00
|
|
|
{
|
|
|
|
char *oldval = table[slot].value;
|
2006-09-28 23:12:55 +04:00
|
|
|
char *newval = NULL;
|
2006-09-27 20:16:10 +04:00
|
|
|
|
|
|
|
if (oldval)
|
|
|
|
free(oldval);
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
newval = xstrdup(value);
|
|
|
|
|
|
|
|
table[slot].value = newval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void interp_clear_table(struct interp *table, int ninterps)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ninterps; i++) {
|
|
|
|
interp_set_entry(table, i, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-20 05:31:51 +04:00
|
|
|
/*
|
|
|
|
* Convert a NUL-terminated string in buffer orig
|
|
|
|
* into the supplied buffer, result, whose length is reslen,
|
|
|
|
* performing substitutions on %-named sub-strings from
|
|
|
|
* the table, interps, with ninterps entries.
|
|
|
|
*
|
|
|
|
* Example interps:
|
|
|
|
* {
|
|
|
|
* { "%H", "example.org"},
|
|
|
|
* { "%port", "123"},
|
|
|
|
* { "%%", "%"},
|
|
|
|
* }
|
|
|
|
*
|
2007-09-10 14:35:05 +04:00
|
|
|
* Returns the length of the substituted string (not including the final \0).
|
|
|
|
* Like with snprintf, if the result is >= reslen, then it overflowed.
|
2006-09-20 05:31:51 +04:00
|
|
|
*/
|
|
|
|
|
2007-06-11 11:34:54 +04:00
|
|
|
unsigned long interpolate(char *result, unsigned long reslen,
|
2006-09-26 01:19:00 +04:00
|
|
|
const char *orig,
|
|
|
|
const struct interp *interps, int ninterps)
|
2006-09-20 05:31:51 +04:00
|
|
|
{
|
2006-09-26 01:19:00 +04:00
|
|
|
const char *src = orig;
|
2006-09-20 05:31:51 +04:00
|
|
|
char *dest = result;
|
2007-06-11 11:34:54 +04:00
|
|
|
unsigned long newlen = 0;
|
2007-03-07 04:44:17 +03:00
|
|
|
const char *name, *value;
|
2007-06-11 11:34:54 +04:00
|
|
|
unsigned long namelen, valuelen;
|
2006-09-20 05:31:51 +04:00
|
|
|
int i;
|
|
|
|
char c;
|
|
|
|
|
2007-06-11 11:34:54 +04:00
|
|
|
while ((c = *src)) {
|
2006-09-20 05:31:51 +04:00
|
|
|
if (c == '%') {
|
|
|
|
/* Try to match an interpolation string. */
|
|
|
|
for (i = 0; i < ninterps; i++) {
|
|
|
|
name = interps[i].name;
|
|
|
|
namelen = strlen(name);
|
2007-06-11 11:34:54 +04:00
|
|
|
if (strncmp(src, name, namelen) == 0)
|
2006-09-20 05:31:51 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for valid interpolation. */
|
|
|
|
if (i < ninterps) {
|
|
|
|
value = interps[i].value;
|
2007-09-25 18:38:46 +04:00
|
|
|
if (!value) {
|
|
|
|
src += namelen;
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-20 05:31:51 +04:00
|
|
|
|
2007-09-25 18:38:46 +04:00
|
|
|
valuelen = strlen(value);
|
2007-09-10 14:35:05 +04:00
|
|
|
if (newlen + valuelen < reslen) {
|
2006-09-20 05:31:51 +04:00
|
|
|
/* Substitute. */
|
2007-09-10 14:35:05 +04:00
|
|
|
memcpy(dest, value, valuelen);
|
2006-09-20 05:31:51 +04:00
|
|
|
dest += valuelen;
|
|
|
|
}
|
2007-06-11 11:34:54 +04:00
|
|
|
newlen += valuelen;
|
|
|
|
src += namelen;
|
|
|
|
continue;
|
2006-09-20 05:31:51 +04:00
|
|
|
}
|
|
|
|
}
|
2007-06-11 11:34:54 +04:00
|
|
|
/* Straight copy one non-interpolation character. */
|
|
|
|
if (newlen + 1 < reslen)
|
|
|
|
*dest++ = *src;
|
|
|
|
src++;
|
|
|
|
newlen++;
|
2006-09-20 05:31:51 +04:00
|
|
|
}
|
|
|
|
|
2007-09-10 14:35:05 +04:00
|
|
|
/* XXX: the previous loop always keep room for the ending NUL,
|
|
|
|
we just need to check if there was room for a NUL in the first place */
|
|
|
|
if (reslen > 0)
|
|
|
|
*dest = '\0';
|
|
|
|
return newlen;
|
2006-09-20 05:31:51 +04:00
|
|
|
}
|