summaryrefslogtreecommitdiff
path: root/usr.bin/tmux/arguments.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2020-03-21 13:16:16 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2020-03-21 13:16:16 +0000
commite276c8b448ca2534b8f2c9942d2e1b00a44770ec (patch)
treef5d5ec45a83b65510e4978cb54afc40ce8f7efa5 /usr.bin/tmux/arguments.c
parentb2db4ec1f9547271f4437644f8d840d60e816d7e (diff)
Break code to convert an argument as a percentage into a common function.
Diffstat (limited to 'usr.bin/tmux/arguments.c')
-rw-r--r--usr.bin/tmux/arguments.c52
1 files changed, 51 insertions, 1 deletions
diff --git a/usr.bin/tmux/arguments.c b/usr.bin/tmux/arguments.c
index 9dcf7cbceb2..c59564e86ee 100644
--- a/usr.bin/tmux/arguments.c
+++ b/usr.bin/tmux/arguments.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: arguments.c,v 1.28 2020/03/17 16:02:38 nicm Exp $ */
+/* $OpenBSD: arguments.c,v 1.29 2020/03/21 13:16:15 nicm Exp $ */
/*
* Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -344,3 +344,53 @@ args_strtonum(struct args *args, u_char ch, long long minval, long long maxval,
*cause = NULL;
return (ll);
}
+
+/* Convert an argument to a number which may be a percentage. */
+long long
+args_percentage(struct args *args, u_char ch, long long minval,
+ long long maxval, long long curval, char **cause)
+{
+ const char *errstr;
+ long long ll;
+ struct args_entry *entry;
+ struct args_value *value;
+ size_t valuelen;
+ char *copy;
+
+ if ((entry = args_find(args, ch)) == NULL) {
+ *cause = xstrdup("missing");
+ return (0);
+ }
+ value = TAILQ_LAST(&entry->values, args_values);
+ valuelen = strlen(value->value);
+
+ if (value->value[valuelen - 1] == '%') {
+ copy = xstrdup(value->value);
+ copy[valuelen - 1] = '\0';
+
+ ll = strtonum(copy, 0, 100, &errstr);
+ free(copy);
+ if (errstr != NULL) {
+ *cause = xstrdup(errstr);
+ return (0);
+ }
+ ll = (curval * ll) / 100;
+ if (ll < minval) {
+ *cause = xstrdup("too large");
+ return (0);
+ }
+ if (ll > maxval) {
+ *cause = xstrdup("too small");
+ return (0);
+ }
+ } else {
+ ll = strtonum(value->value, minval, maxval, &errstr);
+ if (errstr != NULL) {
+ *cause = xstrdup(errstr);
+ return (0);
+ }
+ }
+
+ *cause = NULL;
+ return (ll);
+}