GUACAMOLE-630: Separate color scheme parsing into own files.

This commit is contained in:
Michael Jumper 2018-09-22 16:07:42 -07:00
parent 6f49194640
commit 6f9f2189f2
5 changed files with 349 additions and 277 deletions

View File

@ -32,6 +32,7 @@ noinst_HEADERS = \
terminal/buffer.h \ terminal/buffer.h \
terminal/char_mappings.h \ terminal/char_mappings.h \
terminal/common.h \ terminal/common.h \
terminal/color-scheme.h \
terminal/display.h \ terminal/display.h \
terminal/named-colors.h \ terminal/named-colors.h \
terminal/palette.h \ terminal/palette.h \
@ -46,6 +47,7 @@ noinst_HEADERS = \
libguac_terminal_la_SOURCES = \ libguac_terminal_la_SOURCES = \
buffer.c \ buffer.c \
char_mappings.c \ char_mappings.c \
color-scheme.c \
common.c \ common.c \
display.c \ display.c \
named-colors.c \ named-colors.c \

251
src/terminal/color-scheme.c Normal file
View File

@ -0,0 +1,251 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "config.h"
#include "terminal/color-scheme.h"
#include "terminal/palette.h"
#include "terminal/xparsecolor.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <guacamole/client.h>
/**
* Compare a non-null-terminated string to a null-terminated literal, in the
* same manner as strcmp().
*
* @param str_start
* Start of the non-null-terminated string.
*
* @param str_end
* End of the non-null-terminated string, after the last character.
*
* @param literal
* The null-terminated literal to compare against.
*
* @return
* Zero if the two strings are equal and non-zero otherwise.
*/
static int guac_terminal_color_scheme_compare_token(const char* str_start,
const char* str_end, const char* literal) {
const int result = strncmp(literal, str_start, str_end - str_start);
if (result != 0)
return result;
/* At this point, literal is same length or longer than
* | str_end - str_start |, so if the two are equal, literal should
* have its null-terminator at | str_end - str_start |. */
return (int) (unsigned char) literal[str_end - str_start];
}
/**
* Strip the leading and trailing spaces of a bounded string.
*
* @param[in,out] str_start
* Address of a pointer to the start of the string. On return, the pointer
* is advanced to after any leading spaces.
*
* @param[in,out] str_end
* Address of a pointer to the end of the string, after the last character.
* On return, the pointer is moved back to before any trailing spaces.
*/
static void guac_terminal_color_scheme_strip_spaces(const char** str_start,
const char** str_end) {
/* Strip leading spaces. */
while (*str_start < *str_end && isspace(**str_start))
(*str_start)++;
/* Strip trailing spaces. */
while (*str_end > *str_start && isspace(*(*str_end - 1)))
(*str_end)--;
}
/**
* Parse the name part of the name-value pair within the color-scheme
* configuration.
*
* @param client
* The client that the terminal is connected to.
*
* @param name_start
* Start of the name string.
*
* @param name_end
* End of the name string, after the last character.
*
* @param foreground
* Pointer to the foreground color.
*
* @param background
* Pointer to the background color.
*
* @param palette
* Pointer to the palette array.
*
* @param[out] target
* On return, pointer to the color struct that corresponds to the name.
*
* @return
* Zero if successful or non-zero otherwise.
*/
static int guac_terminal_parse_color_scheme_name(guac_client* client,
const char* name_start, const char* name_end,
guac_terminal_color* foreground, guac_terminal_color* background,
guac_terminal_color (*palette)[256],
guac_terminal_color** target) {
guac_terminal_color_scheme_strip_spaces(&name_start, &name_end);
if (!guac_terminal_color_scheme_compare_token(
name_start, name_end, GUAC_TERMINAL_SCHEME_FOREGROUND)) {
*target = foreground;
return 0;
}
if (!guac_terminal_color_scheme_compare_token(
name_start, name_end, GUAC_TERMINAL_SCHEME_BACKGROUND)) {
*target = background;
return 0;
}
/* Parse color<n> value. */
int index = -1;
if (sscanf(name_start, GUAC_TERMINAL_SCHEME_NUMBERED "%d", &index) &&
index >= 0 && index <= 255) {
*target = &(*palette)[index];
return 0;
}
guac_client_log(client, GUAC_LOG_WARNING,
"Unknown color name: \"%.*s\".",
name_end - name_start, name_start);
return 1;
}
/**
* Parse the value part of the name-value pair within the color-scheme
* configuration.
*
* @param client
* The client that the terminal is connected to.
*
* @param value_start
* Start of the value string.
*
* @param value_end
* End of the value string, after the last character.
*
* @param palette
* The current color palette.
*
* @param[out] target
* On return, the parsed color.
*
* @return
* Zero if successful or non-zero otherwise.
*/
static int guac_terminal_parse_color_scheme_value(guac_client* client,
const char* value_start, const char* value_end,
const guac_terminal_color (*palette)[256],
guac_terminal_color* target) {
guac_terminal_color_scheme_strip_spaces(&value_start, &value_end);
/* Parse color<n> value. */
int index = -1;
if (sscanf(value_start, GUAC_TERMINAL_SCHEME_NUMBERED "%d", &index) &&
index >= 0 && index <= 255) {
*target = (*palette)[index];
return 0;
}
/* Parse X11 value. */
if (!guac_terminal_xparsecolor(value_start, target))
return 0;
guac_client_log(client, GUAC_LOG_WARNING,
"Invalid color value: \"%.*s\".",
value_end - value_start, value_start);
return 1;
}
void guac_terminal_parse_color_scheme(guac_client* client,
const char* color_scheme, guac_terminal_color* foreground,
guac_terminal_color* background,
guac_terminal_color (*palette)[256]) {
/* Set default gray-black color scheme and initial palette. */
*foreground = GUAC_TERMINAL_INITIAL_PALETTE[GUAC_TERMINAL_COLOR_GRAY];
*background = GUAC_TERMINAL_INITIAL_PALETTE[GUAC_TERMINAL_COLOR_BLACK];
memcpy(palette, GUAC_TERMINAL_INITIAL_PALETTE,
sizeof(GUAC_TERMINAL_INITIAL_PALETTE));
/* Current char being parsed, or NULL if at end of parsing. */
const char* cursor = color_scheme;
while (cursor) {
/* Start of the current "name: value" pair. */
const char* pair_start = cursor;
/* End of the current name-value pair. */
const char* pair_end = strchr(pair_start, ';');
if (pair_end) {
cursor = pair_end + 1;
}
else {
pair_end = pair_start + strlen(pair_start);
cursor = NULL;
}
guac_terminal_color_scheme_strip_spaces(&pair_start, &pair_end);
if (pair_start >= pair_end)
/* Allow empty pairs, which happens, e.g., when the configuration
* string ends in a semi-colon. */
continue;
/* End of the name part of the pair. */
const char* name_end = memchr(pair_start, ':', pair_end - pair_start);
if (name_end == NULL) {
guac_client_log(client, GUAC_LOG_WARNING,
"Expecting colon: \"%.*s\".",
pair_end - pair_start, pair_start);
return;
}
/* The color that the name corresponds to. */
guac_terminal_color* color_target = NULL;
if (guac_terminal_parse_color_scheme_name(
client, pair_start, name_end, foreground, background,
palette, &color_target))
return; /* Parsing failed. */
if (guac_terminal_parse_color_scheme_value(
client, name_end + 1, pair_end,
(const guac_terminal_color(*)[256]) palette, color_target))
return; /* Parsing failed. */
}
}

View File

@ -22,6 +22,7 @@
#include "common/clipboard.h" #include "common/clipboard.h"
#include "common/cursor.h" #include "common/cursor.h"
#include "terminal/buffer.h" #include "terminal/buffer.h"
#include "terminal/color-scheme.h"
#include "terminal/common.h" #include "terminal/common.h"
#include "terminal/display.h" #include "terminal/display.h"
#include "terminal/palette.h" #include "terminal/palette.h"
@ -30,7 +31,6 @@
#include "terminal/terminal_handlers.h" #include "terminal/terminal_handlers.h"
#include "terminal/types.h" #include "terminal/types.h"
#include "terminal/typescript.h" #include "terminal/typescript.h"
#include "terminal/xparsecolor.h"
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
@ -305,247 +305,6 @@ void* guac_terminal_thread(void* data) {
} }
/**
* Compare a non-null-terminated string to a null-terminated literal, in the
* same manner as strcmp().
*
* @param str_start
* Start of the non-null-terminated string.
*
* @param str_end
* End of the non-null-terminated string, after the last character.
*
* @param literal
* The null-terminated literal to compare against.
*
* @return
* Zero if the two strings are equal and non-zero otherwise.
*/
static int guac_terminal_color_scheme_compare_token(const char* str_start,
const char* str_end, const char* literal) {
const int result = strncmp(literal, str_start, str_end - str_start);
if (result != 0)
return result;
/* At this point, literal is same length or longer than
* | str_end - str_start |, so if the two are equal, literal should
* have its null-terminator at | str_end - str_start |. */
return (int) (unsigned char) literal[str_end - str_start];
}
/**
* Strip the leading and trailing spaces of a bounded string.
*
* @param[in,out] str_start
* Address of a pointer to the start of the string. On return, the pointer
* is advanced to after any leading spaces.
*
* @param[in,out] str_end
* Address of a pointer to the end of the string, after the last character.
* On return, the pointer is moved back to before any trailing spaces.
*/
static void guac_terminal_color_scheme_strip_spaces(const char** str_start,
const char** str_end) {
/* Strip leading spaces. */
while (*str_start < *str_end && isspace(**str_start))
(*str_start)++;
/* Strip trailing spaces. */
while (*str_end > *str_start && isspace(*(*str_end - 1)))
(*str_end)--;
}
/**
* Parse the name part of the name-value pair within the color-scheme
* configuration.
*
* @param client
* The client that the terminal is connected to.
*
* @param name_start
* Start of the name string.
*
* @param name_end
* End of the name string, after the last character.
*
* @param foreground
* Pointer to the foreground color.
*
* @param background
* Pointer to the background color.
*
* @param palette
* Pointer to the palette array.
*
* @param[out] target
* On return, pointer to the color struct that corresponds to the name.
*
* @return
* Zero if successful or non-zero otherwise.
*/
static int guac_terminal_parse_color_scheme_name(guac_client* client,
const char* name_start, const char* name_end,
guac_terminal_color* foreground, guac_terminal_color* background,
guac_terminal_color (*palette)[256],
guac_terminal_color** target) {
guac_terminal_color_scheme_strip_spaces(&name_start, &name_end);
if (!guac_terminal_color_scheme_compare_token(
name_start, name_end, GUAC_TERMINAL_SCHEME_FOREGROUND)) {
*target = foreground;
return 0;
}
if (!guac_terminal_color_scheme_compare_token(
name_start, name_end, GUAC_TERMINAL_SCHEME_BACKGROUND)) {
*target = background;
return 0;
}
/* Parse color<n> value. */
int index = -1;
if (sscanf(name_start, GUAC_TERMINAL_SCHEME_NUMBERED "%d", &index) &&
index >= 0 && index <= 255) {
*target = &(*palette)[index];
return 0;
}
guac_client_log(client, GUAC_LOG_WARNING,
"Unknown color name: \"%.*s\".",
name_end - name_start, name_start);
return 1;
}
/**
* Parse the value part of the name-value pair within the color-scheme
* configuration.
*
* @param client
* The client that the terminal is connected to.
*
* @param value_start
* Start of the value string.
*
* @param value_end
* End of the value string, after the last character.
*
* @param palette
* The current color palette.
*
* @param[out] target
* On return, the parsed color.
*
* @return
* Zero if successful or non-zero otherwise.
*/
static int guac_terminal_parse_color_scheme_value(guac_client* client,
const char* value_start, const char* value_end,
const guac_terminal_color (*palette)[256],
guac_terminal_color* target) {
guac_terminal_color_scheme_strip_spaces(&value_start, &value_end);
/* Parse color<n> value. */
int index = -1;
if (sscanf(value_start, GUAC_TERMINAL_SCHEME_NUMBERED "%d", &index) &&
index >= 0 && index <= 255) {
*target = (*palette)[index];
return 0;
}
/* Parse X11 value. */
if (!guac_terminal_xparsecolor(value_start, target))
return 0;
guac_client_log(client, GUAC_LOG_WARNING,
"Invalid color value: \"%.*s\".",
value_end - value_start, value_start);
return 1;
}
/**
* Parse a color-scheme configuration string, and return specified
* foreground/background colors and color palette.
*
* @param client
* The client that the terminal is connected to.
*
* @param color_scheme
* A semicolon-separated list of name-value pairs, i.e.
* "<name>: <value> [; <name>: <value> [; ...]]".
* For example, "color2: rgb:cc/33/22; background: color5".
*
* @param[out] foreground
* Parsed foreground color.
*
* @param[out] background
* Parsed background color.
*
* @param[in,out] palette
* Parsed color palette. The caller is responsible for allocating a mutable
* array on entry. On return, the array contains the parsed palette.
*/
static void guac_terminal_parse_color_scheme(guac_client* client,
const char* color_scheme, guac_terminal_color* foreground,
guac_terminal_color* background,
guac_terminal_color (*palette)[256]) {
/* Set default gray-black color scheme and initial palette. */
*foreground = GUAC_TERMINAL_INITIAL_PALETTE[GUAC_TERMINAL_COLOR_GRAY];
*background = GUAC_TERMINAL_INITIAL_PALETTE[GUAC_TERMINAL_COLOR_BLACK];
memcpy(palette, GUAC_TERMINAL_INITIAL_PALETTE,
sizeof(GUAC_TERMINAL_INITIAL_PALETTE));
/* Current char being parsed, or NULL if at end of parsing. */
const char* cursor = color_scheme;
while (cursor) {
/* Start of the current "name: value" pair. */
const char* pair_start = cursor;
/* End of the current name-value pair. */
const char* pair_end = strchr(pair_start, ';');
if (pair_end) {
cursor = pair_end + 1;
}
else {
pair_end = pair_start + strlen(pair_start);
cursor = NULL;
}
guac_terminal_color_scheme_strip_spaces(&pair_start, &pair_end);
if (pair_start >= pair_end)
/* Allow empty pairs, which happens, e.g., when the configuration
* string ends in a semi-colon. */
continue;
/* End of the name part of the pair. */
const char* name_end = memchr(pair_start, ':', pair_end - pair_start);
if (name_end == NULL) {
guac_client_log(client, GUAC_LOG_WARNING,
"Expecting colon: \"%.*s\".",
pair_end - pair_start, pair_start);
return;
}
/* The color that the name corresponds to. */
guac_terminal_color* color_target = NULL;
if (guac_terminal_parse_color_scheme_name(
client, pair_start, name_end, foreground, background,
palette, &color_target))
return; /* Parsing failed. */
if (guac_terminal_parse_color_scheme_value(
client, name_end + 1, pair_end,
(const guac_terminal_color(*)[256]) palette, color_target))
return; /* Parsing failed. */
}
}
guac_terminal* guac_terminal_create(guac_client* client, guac_terminal* guac_terminal_create(guac_client* client,
guac_common_clipboard* clipboard, int max_scrollback, guac_common_clipboard* clipboard, int max_scrollback,
const char* font_name, int font_size, int dpi, const char* font_name, int font_size, int dpi,

View File

@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef GUAC_TERMINAL_COLOR_SCHEME_H
#define GUAC_TERMINAL_COLOR_SCHEME_H
#include "config.h"
#include "terminal/palette.h"
#include <guacamole/client.h>
#include <stdlib.h>
#include <string.h>
/**
* The name of the color scheme having black foreground and white background.
*/
#define GUAC_TERMINAL_SCHEME_BLACK_WHITE "black-white"
/**
* The name of the color scheme having gray foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_GRAY_BLACK "gray-black"
/**
* The name of the color scheme having green foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_GREEN_BLACK "green-black"
/**
* The name of the color scheme having white foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_WHITE_BLACK "white-black"
/**
* Color name representing the foreground color.
*/
#define GUAC_TERMINAL_SCHEME_FOREGROUND "foreground"
/**
* Color name representing the background color.
*/
#define GUAC_TERMINAL_SCHEME_BACKGROUND "background"
/**
* Color name representing a numbered color.
*/
#define GUAC_TERMINAL_SCHEME_NUMBERED "color"
/**
* Parse a color-scheme configuration string, and return specified
* foreground/background colors and color palette.
*
* @param client
* The client that the terminal is connected to.
*
* @param color_scheme
* A semicolon-separated list of name-value pairs, i.e.
* "<name>: <value> [; <name>: <value> [; ...]]".
* For example, "color2: rgb:cc/33/22; background: color5".
*
* @param[out] foreground
* Parsed foreground color.
*
* @param[out] background
* Parsed background color.
*
* @param[in,out] palette
* Parsed color palette. The caller is responsible for allocating a mutable
* array on entry. On return, the array contains the parsed palette.
*/
void guac_terminal_parse_color_scheme(guac_client* client,
const char* color_scheme, guac_terminal_color* foreground,
guac_terminal_color* background,
guac_terminal_color (*palette)[256]);
#endif

View File

@ -69,41 +69,6 @@
*/ */
#define GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT 3 #define GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT 3
/**
* The name of the color scheme having black foreground and white background.
*/
#define GUAC_TERMINAL_SCHEME_BLACK_WHITE "black-white"
/**
* The name of the color scheme having gray foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_GRAY_BLACK "gray-black"
/**
* The name of the color scheme having green foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_GREEN_BLACK "green-black"
/**
* The name of the color scheme having white foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_WHITE_BLACK "white-black"
/**
* Color name representing the foreground color.
*/
#define GUAC_TERMINAL_SCHEME_FOREGROUND "foreground"
/**
* Color name representing the background color.
*/
#define GUAC_TERMINAL_SCHEME_BACKGROUND "background"
/**
* Color name representing a numbered color.
*/
#define GUAC_TERMINAL_SCHEME_NUMBERED "color"
/** /**
* Flag which specifies that terminal output should be sent to both the current * Flag which specifies that terminal output should be sent to both the current
* pipe stream and the user's display. By default, terminal output will be sent * pipe stream and the user's display. By default, terminal output will be sent