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-06-11 09:34:54 +02:00
|
|
|
* Returns 0 on a successful substitution pass that fits in result,
|
|
|
|
* Returns a number of bytes needed to hold the full substituted
|
|
|
|
* string otherwise.
|
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;
|
|
|
|
|
|
|
|
memset(result, 0, reslen);
|
|
|
|
|
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;
|
|
|
|
valuelen = strlen(value);
|
|
|
|
|
2007-06-11 09:34:54 +02:00
|
|
|
if (newlen + valuelen + 1 < reslen) {
|
2006-09-20 03:31:51 +02:00
|
|
|
/* Substitute. */
|
|
|
|
strncpy(dest, value, valuelen);
|
|
|
|
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-06-11 09:34:54 +02:00
|
|
|
if (newlen + 1 < reslen)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return newlen + 2;
|
2006-09-20 03:31:51 +02:00
|
|
|
}
|