Module Name: src Committed By: martin Date: Thu Nov 15 10:23:32 UTC 2018
Modified Files: src/usr.sbin/sysinst: defs.h util.c Log Message: Add a new helper function to show messages with positional parameters To generate a diff of this commit: cvs rdiff -u -r1.25 -r1.26 src/usr.sbin/sysinst/defs.h cvs rdiff -u -r1.16 -r1.17 src/usr.sbin/sysinst/util.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.sbin/sysinst/defs.h diff -u src/usr.sbin/sysinst/defs.h:1.25 src/usr.sbin/sysinst/defs.h:1.26 --- src/usr.sbin/sysinst/defs.h:1.25 Wed Nov 14 02:30:00 2018 +++ src/usr.sbin/sysinst/defs.h Thu Nov 15 10:23:32 2018 @@ -1,4 +1,4 @@ -/* $NetBSD: defs.h,v 1.25 2018/11/14 02:30:00 martin Exp $ */ +/* $NetBSD: defs.h,v 1.26 2018/11/15 10:23:32 martin Exp $ */ /* * Copyright 1997 Piermont Information Systems Inc. @@ -553,6 +553,7 @@ void do_reinstall_sets(void); void restore_etc(void); /* from util.c */ +void msg_display_subst(const char *, size_t, ...); int ask_yesno(const char *); int ask_noyes(const char *); int dir_exists_p(const char *); Index: src/usr.sbin/sysinst/util.c diff -u src/usr.sbin/sysinst/util.c:1.16 src/usr.sbin/sysinst/util.c:1.17 --- src/usr.sbin/sysinst/util.c:1.16 Sun Nov 11 10:06:09 2018 +++ src/usr.sbin/sysinst/util.c Thu Nov 15 10:23:32 2018 @@ -1,4 +1,4 @@ -/* $NetBSD: util.c,v 1.16 2018/11/11 10:06:09 martin Exp $ */ +/* $NetBSD: util.c,v 1.17 2018/11/15 10:23:32 martin Exp $ */ /* * Copyright 1997 Piermont Information Systems Inc. @@ -34,6 +34,8 @@ /* util.c -- routines that don't really fit anywhere else... */ +#include <assert.h> +#include <inttypes.h> #include <stdio.h> #include <stdarg.h> #include <string.h> @@ -1792,3 +1794,79 @@ set_postfix(const char *set_name) return use_tgz_for_set(set_name) ? dist_tgz_postfix : dist_postfix; } +/* + * Replace positional arguments (encoded as $0 .. $N) in the + * message by the strings passed as ... + */ +void +msg_display_subst(const char *master, size_t argc, ...) +{ + va_list ap; + const char **args, **arg; + const char *src, *p, *last; + char *out, *t; + size_t len; + + args = malloc(sizeof(const char *)*argc); + if (args == NULL) + return; + + arg = args; + va_start(ap, argc); + for (size_t i = 0; i < argc; i++) + *arg++ = va_arg(ap, const char*); + va_end(ap); + + src = msg_string(master); + len = strlen(src); + for (p = strchr(src, '$'); p; p = strchr(p+1, '$')) { + char *endp = NULL; + size_t n; + int e; + + /* $ followed by a correct numeric position? */ + n = strtou(p+1, &endp, 10, 0, INT_MAX, &e); + if ((e == 0 || e == ENOTSUP) && n < argc) { + len += strlen(args[n]); + len -= endp-p; + p = endp-1; + } + } + + out = malloc(len+1); + if (out == NULL) { + free(args); + return; + } + + t = out; + for (last = src, p = strchr(src, '$'); p; p = strchr(p+1, '$')) { + char *endp = NULL; + size_t n; + int e; + + /* $ followed by a correct numeric position? */ + n = strtou(p+1, &endp, 10, 0, INT_MAX, &e); + if ((e == 0 || e == ENOTSUP) && n < argc) { + size_t l = p-last; + memcpy(t, last, l); + t += l; + strcpy(t, args[n]); + t += strlen(args[n]); + last = endp; + } + } + if (*last) { + strcpy(t, last); + t += strlen(last); + } else { + *t = 0; + } + assert((size_t)(t-out) == len); + + msg_display(out); + + free(out); + free(args); +} +