2
2
* STFL - The Structured Terminal Forms Language/Library
3
* Copyright (C) 2006 Clifford Wolf <clifford@clifford.at>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
3
* Copyright (C) 2006, 2007 Clifford Wolf <clifford@clifford.at>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 3 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
20
* style.c: Helper functions for text colors and attributes
24
25
#include <string.h>
25
26
#include <stdlib.h>
29
static wchar_t *wcssep(wchar_t **stringp, const wchar_t *delim) {
30
register wchar_t *tmp=*stringp;
31
register wchar_t *tmp2=tmp;
32
register const wchar_t *tmp3;
33
if (!*stringp) return 0;
34
for (tmp2=tmp; *tmp2; ++tmp2) {
35
for (tmp3=delim; *tmp3; ++tmp3)
36
if (*tmp2==*tmp3) { /* delimiter found */
27
47
static int stfl_colorpair_begin = -1;
28
48
static int stfl_colorpair_counter = -1;
30
void stfl_style(WINDOW *win, const char *style)
50
void stfl_style(WINDOW *win, const wchar_t *style)
32
52
int bg_color = -1, fg_color = -1, attr = A_NORMAL;
34
style += strspn(style, " \t");
54
style += wcsspn(style, L" \t");
38
int field_len = strcspn(style, ",");
39
char field[field_len+1];
40
memcpy(field, style, field_len);
58
int field_len = wcscspn(style, L",");
59
wchar_t field[field_len+1];
60
wmemcpy(field, style, field_len);
41
61
field[field_len] = 0;
42
62
style += field_len;
48
char *key = strsep(&sepp, "=");
49
char *value = strsep(&sepp, "");
67
wchar_t *sepp = field;
68
wchar_t *key = wcssep(&sepp, L"=");
69
wchar_t *value = wcssep(&sepp, L"");
51
71
if (!key || !value) continue;
53
key += strspn(key, " \t");
54
key = strsep(&key, " \t");
56
value += strspn(value, " \t");
57
value = strsep(&value, " \t");
59
if (!strcmp(key, "bg") || !strcmp(key, "fg"))
73
key += wcsspn(key, L" \t");
74
key = wcssep(&key, L" \t");
76
value += wcsspn(value, L" \t");
77
value = wcssep(&value, L" \t");
79
if (!wcscmp(key, L"bg") || !wcscmp(key, L"fg"))
62
if (!strcmp(value, "black"))
82
if (!wcscmp(value, L"black"))
63
83
color = COLOR_BLACK;
65
if (!strcmp(value, "red"))
85
if (!wcscmp(value, L"red"))
68
if (!strcmp(value, "green"))
88
if (!wcscmp(value, L"green"))
69
89
color = COLOR_GREEN;
71
if (!strcmp(value, "yellow"))
91
if (!wcscmp(value, L"yellow"))
72
92
color = COLOR_YELLOW;
74
if (!strcmp(value, "blue"))
94
if (!wcscmp(value, L"blue"))
75
95
color = COLOR_BLUE;
77
if (!strcmp(value, "magenta"))
97
if (!wcscmp(value, L"magenta"))
78
98
color = COLOR_MAGENTA;
80
if (!strcmp(value, "cyan"))
100
if (!wcscmp(value, L"cyan"))
81
101
color = COLOR_CYAN;
83
if (!strcmp(value, "white"))
103
if (!wcscmp(value, L"white"))
84
104
color = COLOR_WHITE;
86
fprintf(stderr, "STFL Style Error: Unknown %s color: '%s'\n", key, value);
106
fprintf(stderr, "STFL Style Error: Unknown %ls color: '%ls'\n", key, value);
90
if (!strcmp(key, "bg"))
110
if (!wcscmp(key, L"bg"))
96
if (!strcmp(key, "attr"))
116
if (!wcscmp(key, L"attr"))
98
if (!strcmp(value, "standout"))
118
if (!wcscmp(value, L"standout"))
99
119
attr |= A_STANDOUT;
101
if (!strcmp(value, "underline"))
121
if (!wcscmp(value, L"underline"))
102
122
attr |= A_UNDERLINE;
104
if (!strcmp(value, "reverse"))
124
if (!wcscmp(value, L"reverse"))
105
125
attr |= A_REVERSE;
107
if (!strcmp(value, "blink"))
127
if (!wcscmp(value, L"blink"))
110
if (!strcmp(value, "dim"))
130
if (!wcscmp(value, L"dim"))
113
if (!strcmp(value, "bold"))
133
if (!wcscmp(value, L"bold"))
116
if (!strcmp(value, "protect"))
136
if (!wcscmp(value, L"protect"))
117
137
attr |= A_PROTECT;
119
if (!strcmp(value, "invis"))
139
if (!wcscmp(value, L"invis"))
122
fprintf(stderr, "STFL Style Error: Unknown attribute: '%s'\n", value);
142
fprintf(stderr, "STFL Style Error: Unknown attribute: '%ls'\n", value);
127
fprintf(stderr, "STFL Style Error: Unknown keyword: '%s'\n", key);
147
fprintf(stderr, "STFL Style Error: Unknown keyword: '%ls'\n", key);