diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/output.c | 43 | ||||
-rw-r--r-- | src/print_time.c | 25 | ||||
-rw-r--r-- | src/print_wireless_info.c | 2 |
3 files changed, 66 insertions, 4 deletions
diff --git a/src/output.c b/src/output.c index f7a8888..1c8c415 100644 --- a/src/output.c +++ b/src/output.c @@ -78,3 +78,46 @@ void print_separator(const char *separator) { void reset_cursor(void) { printf("\033[?25h"); } + +/* + * Escapes ampersand, less-than, greater-than, single-quote, and double-quote + * characters with the corresponding Pango markup strings if markup is enabled. + * See the glib implementation: + * https://git.gnome.org/browse/glib/tree/glib/gmarkup.c?id=03db1f455b4265654e237d2ad55464b4113cba8a#n2142 + * + */ +void maybe_escape_markup(char *text, char **buffer) { + if (markup_format == M_NONE) { + *buffer += sprintf(*buffer, "%s", text); + return; + } + for (; *text != '\0'; text++) { + switch (*text) { + case '&': + *buffer += sprintf(*buffer, "%s", "&"); + break; + case '<': + *buffer += sprintf(*buffer, "%s", "<"); + break; + case '>': + *buffer += sprintf(*buffer, "%s", ">"); + break; + case '\'': + *buffer += sprintf(*buffer, "%s", "'"); + break; + case '"': + *buffer += sprintf(*buffer, "%s", """); + break; + default: + if ((0x1 <= *text && *text <= 0x8) || + (0xb <= *text && *text <= 0xc) || + (0xe <= *text && *text <= 0x1f) || + (0x7f <= *text && *text <= 0x84) || + (0x86 <= *text && *text <= 0x9f)) + *buffer += sprintf(*buffer, "&#x%x;", *text); + else + *(*buffer)++ = *text; + break; + } + } +} diff --git a/src/print_time.c b/src/print_time.c index c70a09c..9fa6642 100644 --- a/src/print_time.c +++ b/src/print_time.c @@ -33,17 +33,36 @@ void set_timezone(const char *tz) { } } -void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, time_t t) { +void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *format_time, time_t t) { + const char *walk; char *outwalk = buffer; struct tm tm; + char timebuf[1024]; if (title != NULL) INSTANCE(title); - /* Convert time and format output. */ set_timezone(tz); localtime_r(&t, &tm); - outwalk += strftime(outwalk, 4095, format, &tm); + + if (format_time == NULL) { + strftime(timebuf, sizeof(timebuf), format, &tm); + maybe_escape_markup(timebuf, &outwalk); + } else { + for (walk = format; *walk != '\0'; walk++) { + if (*walk != '%') { + *(outwalk++) = *walk; + continue; + } + + if (BEGINS_WITH(walk + 1, "time")) { + strftime(timebuf, sizeof(timebuf), format_time, &tm); + maybe_escape_markup(timebuf, &outwalk); + walk += strlen("time"); + } + } + } + *outwalk = '\0'; OUTPUT_FULL_TEXT(buffer); } diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index aff0438..4f92507 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -519,7 +519,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, if (BEGINS_WITH(walk + 1, "essid")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) - outwalk += sprintf(outwalk, "%s", info.essid); + maybe_escape_markup(info.essid, &outwalk); else *(outwalk++) = '?'; walk += strlen("essid"); |