From 97f9f0b699316ba60009b395948a712ec0b671d2 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Fri, 27 Dec 2019 23:33:55 -0500 Subject: parse_color: return success + drop fallback color This is the first in a series of commits to refactor the color handling in sway. This changes parse_color to return whether it was success and no longer uses 0xFFFFFFFF as the fallback color. This also verifies that the string actually contains a valid hexadecimal number along with the length checks. In the process of altering the calls to parse_color, I also took the opportunity to heavily refactor swaybar's ipc_parse_colors function. This allowed for several lines of duplicated code to be removed. --- swaynag/config.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'swaynag') diff --git a/swaynag/config.c b/swaynag/config.c index 2fa7cb61..f1161b39 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -221,28 +221,28 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); return -1; case TO_COLOR_BACKGROUND: // Background color - if (type) { - type->background = parse_color(optarg); + if (type && !parse_color(optarg, &type->background)) { + fprintf(stderr, "Invalid background color: %s", optarg); } break; case TO_COLOR_BORDER: // Border color - if (type) { - type->border = parse_color(optarg); + if (type && !parse_color(optarg, &type->border)) { + fprintf(stderr, "Invalid border color: %s", optarg); } break; case TO_COLOR_BORDER_BOTTOM: // Bottom border color - if (type) { - type->border_bottom = parse_color(optarg); + if (type && !parse_color(optarg, &type->border_bottom)) { + fprintf(stderr, "Invalid border bottom color: %s", optarg); } break; case TO_COLOR_BUTTON: // Button background color - if (type) { - type->button_background = parse_color(optarg); + if (type && !parse_color(optarg, &type->button_background)) { + fprintf(stderr, "Invalid button background color: %s", optarg); } break; case TO_COLOR_TEXT: // Text color - if (type) { - type->text = parse_color(optarg); + if (type && !parse_color(optarg, &type->text)) { + fprintf(stderr, "Invalid text color: %s", optarg); } break; case TO_THICK_BAR_BORDER: // Bottom border thickness -- cgit v1.2.3-54-g00ecf