replace strbuf_expand() with strbuf_expand_step()
Avoid the overhead of passing context to a callback function of strbuf_expand() by using strbuf_expand_step() in a loop instead. It requires explicit handling of %% and unrecognized placeholders, but is simpler, more direct and avoids void pointers. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
39dbd49b41
commit
6f1e2d5279
61
daemon.c
61
daemon.c
@ -144,42 +144,6 @@ static void NORETURN daemon_die(const char *err, va_list params)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
struct expand_path_context {
|
||||
const char *directory;
|
||||
struct hostinfo *hostinfo;
|
||||
};
|
||||
|
||||
static size_t expand_path(struct strbuf *sb, const char *placeholder, void *ctx)
|
||||
{
|
||||
struct expand_path_context *context = ctx;
|
||||
struct hostinfo *hi = context->hostinfo;
|
||||
|
||||
switch (placeholder[0]) {
|
||||
case 'H':
|
||||
strbuf_addbuf(sb, &hi->hostname);
|
||||
return 1;
|
||||
case 'C':
|
||||
if (placeholder[1] == 'H') {
|
||||
strbuf_addstr(sb, get_canon_hostname(hi));
|
||||
return 2;
|
||||
}
|
||||
break;
|
||||
case 'I':
|
||||
if (placeholder[1] == 'P') {
|
||||
strbuf_addstr(sb, get_ip_address(hi));
|
||||
return 2;
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
strbuf_addbuf(sb, &hi->tcp_port);
|
||||
return 1;
|
||||
case 'D':
|
||||
strbuf_addstr(sb, context->directory);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const char *path_ok(const char *directory, struct hostinfo *hi)
|
||||
{
|
||||
static char rpath[PATH_MAX];
|
||||
@ -223,10 +187,7 @@ static const char *path_ok(const char *directory, struct hostinfo *hi)
|
||||
}
|
||||
else if (interpolated_path && hi->saw_extended_args) {
|
||||
struct strbuf expanded_path = STRBUF_INIT;
|
||||
struct expand_path_context context;
|
||||
|
||||
context.directory = directory;
|
||||
context.hostinfo = hi;
|
||||
const char *format = interpolated_path;
|
||||
|
||||
if (*dir != '/') {
|
||||
/* Allow only absolute */
|
||||
@ -234,8 +195,24 @@ static const char *path_ok(const char *directory, struct hostinfo *hi)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strbuf_expand(&expanded_path, interpolated_path,
|
||||
expand_path, &context);
|
||||
while (strbuf_expand_step(&expanded_path, &format)) {
|
||||
if (skip_prefix(format, "%", &format))
|
||||
strbuf_addch(&expanded_path, '%');
|
||||
else if (skip_prefix(format, "H", &format))
|
||||
strbuf_addbuf(&expanded_path, &hi->hostname);
|
||||
else if (skip_prefix(format, "CH", &format))
|
||||
strbuf_addstr(&expanded_path,
|
||||
get_canon_hostname(hi));
|
||||
else if (skip_prefix(format, "IP", &format))
|
||||
strbuf_addstr(&expanded_path,
|
||||
get_ip_address(hi));
|
||||
else if (skip_prefix(format, "P", &format))
|
||||
strbuf_addbuf(&expanded_path, &hi->tcp_port);
|
||||
else if (skip_prefix(format, "D", &format))
|
||||
strbuf_addstr(&expanded_path, directory);
|
||||
else
|
||||
strbuf_addch(&expanded_path, '%');
|
||||
}
|
||||
|
||||
rlen = strlcpy(interp_path, expanded_path.buf,
|
||||
sizeof(interp_path));
|
||||
|
Reference in New Issue
Block a user