From 2a71bce1e5871f8b82e17bf7272c48bdac870949 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 30 May 2022 12:52:02 +0000 Subject: [PATCH] Better error reporting when applying custom layouts. --- usr.bin/tmux/cmd-select-layout.c | 9 +++++---- usr.bin/tmux/layout-custom.c | 25 ++++++++++++++++++------- usr.bin/tmux/tmux.h | 4 ++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/usr.bin/tmux/cmd-select-layout.c b/usr.bin/tmux/cmd-select-layout.c index cbb6c7fa87a..3ae023962db 100644 --- a/usr.bin/tmux/cmd-select-layout.c +++ b/usr.bin/tmux/cmd-select-layout.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-select-layout.c,v 1.40 2021/08/21 10:22:39 nicm Exp $ */ +/* $OpenBSD: cmd-select-layout.c,v 1.41 2022/05/30 12:52:02 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -77,7 +77,7 @@ cmd_select_layout_exec(struct cmd *self, struct cmdq_item *item) struct window *w = wl->window; struct window_pane *wp = target->wp; const char *layoutname; - char *oldlayout; + char *oldlayout, *cause; int next, previous, layout; server_unzoom_window(w); @@ -124,8 +124,9 @@ cmd_select_layout_exec(struct cmd *self, struct cmdq_item *item) } if (layoutname != NULL) { - if (layout_parse(w, layoutname) == -1) { - cmdq_error(item, "can't set layout: %s", layoutname); + if (layout_parse(w, layoutname, &cause) == -1) { + cmdq_error(item, "%s: %s", cause, layoutname); + free(cause); goto error; } goto changed; diff --git a/usr.bin/tmux/layout-custom.c b/usr.bin/tmux/layout-custom.c index 7b35bc11bc9..53d49e491b9 100644 --- a/usr.bin/tmux/layout-custom.c +++ b/usr.bin/tmux/layout-custom.c @@ -1,4 +1,4 @@ -/* $OpenBSD: layout-custom.c,v 1.20 2021/03/11 06:31:05 nicm Exp $ */ +/* $OpenBSD: layout-custom.c,v 1.21 2022/05/30 12:52:02 nicm Exp $ */ /* * Copyright (c) 2010 Nicholas Marriott @@ -154,7 +154,7 @@ layout_check(struct layout_cell *lc) /* Parse a layout string and arrange window as layout. */ int -layout_parse(struct window *w, const char *layout) +layout_parse(struct window *w, const char *layout, char **cause) { struct layout_cell *lc, *lcchild; struct window_pane *wp; @@ -165,22 +165,31 @@ layout_parse(struct window *w, const char *layout) if (sscanf(layout, "%hx,", &csum) != 1) return (-1); layout += 5; - if (csum != layout_checksum(layout)) + if (csum != layout_checksum(layout)) { + *cause = xstrdup("invalid layout"); return (-1); + } /* Build the layout. */ lc = layout_construct(NULL, &layout); - if (lc == NULL) + if (lc == NULL) { + *cause = xstrdup("invalid layout"); return (-1); - if (*layout != '\0') + } + if (*layout != '\0') { + *cause = xstrdup("invalid layout"); goto fail; + } /* Check this window will fit into the layout. */ for (;;) { npanes = window_count_panes(w); ncells = layout_count_cells(lc); - if (npanes > ncells) + if (npanes > ncells) { + xasprintf(cause, "have %u panes but need %u", npanes, + ncells); goto fail; + } if (npanes == ncells) break; @@ -217,8 +226,10 @@ layout_parse(struct window *w, const char *layout) } /* Check the new layout. */ - if (!layout_check(lc)) + if (!layout_check(lc)) { + *cause = xstrdup("size mismatch after applying layout"); return (-1); + } /* Resize to the layout size. */ window_resize(w, lc->sx, lc->sy, -1, -1); diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 9cf1a6160b3..a54dc95888d 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.1167 2022/05/30 12:48:57 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.1168 2022/05/30 12:52:02 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -3018,7 +3018,7 @@ void layout_spread_out(struct window_pane *); /* layout-custom.c */ char *layout_dump(struct layout_cell *); -int layout_parse(struct window *, const char *); +int layout_parse(struct window *, const char *, char **); /* layout-set.c */ int layout_set_lookup(const char *); -- 2.20.1