2006-09-20 03:31:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2006 Jon Loeliger
|
|
|
|
*/
|
|
|
|
|
2006-09-27 18:16:10 +02:00
|
|
|
#include "git-compat-util.h"
|
2006-09-20 03:31:51 +02:00
|
|
|
#include "interpolate.h"
|
|
|
|
|
|
|
|
|
2006-09-28 21:12:55 +02:00
|
|
|
void interp_set_entry(struct interp *table, int slot, const char *value)
|
2006-09-27 18:16:10 +02:00
|
|
|
{
|
|
|
|
char *oldval = table[slot].value;
|
2006-09-28 21:12:55 +02:00
|
|
|
char *newval = NULL;
|
2006-09-27 18:16:10 +02: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 03:31:51 +02: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 12:35:05 +02: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 03:31:51 +02:00
|
|
|
*/
|
|
|
|
|
2007-06-11 09:34:54 +02:00
|
|
|
unsigned long interpolate(char *result, unsigned long reslen,
|
2006-09-25 23:19:00 +02:00
|
|
|
const char *orig,
|
|
|
|
const struct interp *interps, int ninterps)
|
2006-09-20 03:31:51 +02:00
|
|
|
{
|
2006-09-25 23:19:00 +02:00
|
|
|
const char *src = orig;
|
2006-09-20 03:31:51 +02:00
|
|
|
char *dest = result;
|
2007-06-11 09:34:54 +02:00
|
|
|
unsigned long newlen = 0;
|
2007-03-07 02:44:17 +01:00
|
|
|
const char *name, *value;
|
2007-06-11 09:34:54 +02:00
|
|
|
unsigned long namelen, valuelen;
|
2006-09-20 03:31:51 +02:00
|
|
|
int i;
|
|
|
|
char c;
|
|
|
|
|
2007-06-11 09:34:54 +02:00
|
|
|
while ((c = *src)) {
|
2006-09-20 03:31:51 +02: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 09:34:54 +02:00
|
|
|
if (strncmp(src, name, namelen) == 0)
|
2006-09-20 03:31:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for valid interpolation. */
|
|
|
|
if (i < ninterps) {
|
|
|
|
value = interps[i].value;
|
2007-09-25 16:38:46 +02:00
|
|
|
if (!value) {
|
|
|
|
src += namelen;
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-20 03:31:51 +02:00
|
|
|
|
2007-09-25 16:38:46 +02:00
|
|
|
valuelen = strlen(value);
|
2007-09-10 12:35:05 +02:00
|
|
|
if (newlen + valuelen < reslen) {
|
2006-09-20 03:31:51 +02:00
|
|
|
/* Substitute. */
|
2007-09-10 12:35:05 +02:00
|
|
|
memcpy(dest, value, valuelen);
|
2006-09-20 03:31:51 +02:00
|
|
|
dest += valuelen;
|
|
|
|
}
|
2007-06-11 09:34:54 +02:00
|
|
|
newlen += valuelen;
|
|
|
|
src += namelen;
|
|
|
|
continue;
|
2006-09-20 03:31:51 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-11 09:34:54 +02:00
|
|
|
/* Straight copy one non-interpolation character. */
|
|
|
|
if (newlen + 1 < reslen)
|
|
|
|
*dest++ = *src;
|
|
|
|
src++;
|
|
|
|
newlen++;
|
2006-09-20 03:31:51 +02:00
|
|
|
}
|
|
|
|
|
2007-09-10 12:35:05 +02: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 03:31:51 +02:00
|
|
|
}
|