File: editpolicy_color.c

package info (click to toggle)
ccstools 1.7.2-20100401-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 984 kB
  • ctags: 1,080
  • sloc: ansic: 20,286; sh: 890; makefile: 80
file content (229 lines) | stat: -rw-r--r-- 4,802 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * editpolicy_color.c
 *
 * TOMOYO Linux's utilities.
 *
 * Copyright (C) 2005-2010  NTT DATA CORPORATION
 *
 * Version: 1.7.2   2010/04/01
 *
 */
#include "ccstools.h"

/* Prototypes */

static void editpolicy_color_save(const _Bool flg);

/* Main functions */

#ifdef COLOR_ON

void editpolicy_color_init(void)
{
	static struct color_env_t {
		enum color_pair	tag;
		short int fore;
		short int back;
		const char *name;
	} color_env[] = {
		{ DOMAIN_HEAD,      COLOR_BLACK,
		  COLOR_GREEN,      "DOMAIN_HEAD" },
		{ DOMAIN_CURSOR,    COLOR_BLACK,
		  COLOR_GREEN,      "DOMAIN_CURSOR" },
		{ EXCEPTION_HEAD,   COLOR_BLACK,
		  COLOR_CYAN,       "EXCEPTION_HEAD" },
		{ EXCEPTION_CURSOR, COLOR_BLACK,
		  COLOR_CYAN,       "EXCEPTION_CURSOR" },
		{ ACL_HEAD,         COLOR_BLACK,
		  COLOR_YELLOW,     "ACL_HEAD" },
		{ ACL_CURSOR,       COLOR_BLACK,
		  COLOR_YELLOW,     "ACL_CURSOR" },
		{ PROFILE_HEAD,     COLOR_WHITE,
		  COLOR_RED,        "PROFILE_HEAD" },
		{ PROFILE_CURSOR,   COLOR_WHITE,
		  COLOR_RED,        "PROFILE_CURSOR" },
		{ MANAGER_HEAD,     COLOR_WHITE,
		  COLOR_GREEN,      "MANAGER_HEAD" },
		{ MANAGER_CURSOR,   COLOR_WHITE,
		  COLOR_GREEN,      "MANAGER_CURSOR" },
		{ MEMORY_HEAD,      COLOR_BLACK,
		  COLOR_YELLOW,     "MEMORY_HEAD" },
		{ MEMORY_CURSOR,    COLOR_BLACK,
		  COLOR_YELLOW,     "MEMORY_CURSOR" },
		{ NORMAL,           COLOR_WHITE,
		  COLOR_BLACK,      NULL }
	};
	FILE *fp = fopen(CCSTOOLS_CONFIG_FILE, "r");
	int i;
	if (!fp)
		goto use_default;
	get();
	while (true) {
		char *line = freadline(fp);
		char *cp;
		if (!line)
			break;
		if (!str_starts(line, "editpolicy.line_color "))
			continue;
		cp = strchr(line, '=');
		if (!cp)
			continue;
		*cp++ = '\0';
		normalize_line(line);
		normalize_line(cp);
		if (!*line || !*cp)
			continue;
		for (i = 0; color_env[i].name; i++) {
			short int fore;
			short int back;
			if (strcmp(line, color_env[i].name))
				continue;
			if (strlen(cp) != 2)
				break;
			fore = (*cp++) - '0'; /* foreground color */
			back = (*cp) - '0';   /* background color */
			if (fore < 0 || fore > 7 || back < 0 || back > 7)
				break;
			color_env[i].fore = fore;
			color_env[i].back = back;
			break;
		}
	}
	put();
	fclose(fp);
use_default:
	start_color();
	for (i = 0; color_env[i].name; i++) {
		struct color_env_t *colorp = &color_env[i];
		init_pair(colorp->tag, colorp->fore, colorp->back);
	}
	init_pair(DISP_ERR, COLOR_RED, COLOR_BLACK); /* error message */
}

static void editpolicy_color_save(const _Bool flg)
{
	static attr_t save_color = NORMAL;
	if (flg)
		save_color = getattrs(stdscr);
	else
		attrset(save_color);
}

void editpolicy_color_change(const attr_t attr, const _Bool flg)
{
	if (flg)
		attron(COLOR_PAIR(attr));
	else
		attroff(COLOR_PAIR(attr));
}

void editpolicy_attr_change(const attr_t attr, const _Bool flg)
{
	if (flg)
		attron(attr);
	else
		attroff(attr);
}

void editpolicy_sttr_save(void)
{
	editpolicy_color_save(true);
}

void editpolicy_sttr_restore(void)
{
	editpolicy_color_save(false);
}

int editpolicy_color_head(const int screen)
{
	switch (screen) {
	case SCREEN_DOMAIN_LIST:
		return DOMAIN_HEAD;
	case SCREEN_EXCEPTION_LIST:
		return EXCEPTION_HEAD;
	case SCREEN_PROFILE_LIST:
		return PROFILE_HEAD;
	case SCREEN_MANAGER_LIST:
		return MANAGER_HEAD;
	case SCREEN_MEMINFO_LIST:
		return MEMORY_HEAD;
	default:
		return ACL_HEAD;
	}
}

int editpolicy_color_cursor(const int screen)
{
	switch (screen) {
	case SCREEN_DOMAIN_LIST:
		return DOMAIN_CURSOR;
	case SCREEN_EXCEPTION_LIST:
		return EXCEPTION_CURSOR;
	case SCREEN_PROFILE_LIST:
		return PROFILE_CURSOR;
	case SCREEN_MANAGER_LIST:
		return MANAGER_CURSOR;
	case SCREEN_MEMINFO_LIST:
		return MEMORY_CURSOR;
	default:
		return ACL_CURSOR;
	}
}

void editpolicy_line_draw(const int screen)
{
	static int before_current[MAXSCREEN] = { -1, -1, -1, -1,
						 -1, -1, -1 };
	static int before_y[MAXSCREEN]       = { -1, -1, -1, -1,
						 -1, -1, -1 };
	int current = editpolicy_get_current();
	int y;
	int x;

	if (current == EOF)
		return;

	getyx(stdscr, y, x);
	if (-1 < before_current[screen] &&
	    current != before_current[screen]){
		move(header_lines + before_y[screen], 0);
		chgat(-1, A_NORMAL, NORMAL, NULL);
	}

	move(y, x);
	chgat(-1, A_NORMAL, editpolicy_color_cursor(screen), NULL);
	touchwin(stdscr);

	before_current[screen] = current;
	before_y[screen] = current_y[screen];
}

#else

void editpolicy_color_init(void)
{
}
void editpolicy_color_change(const attr_t attr, const _Bool flg)
{
}
void editpolicy_attr_change(const attr_t attr, const _Bool flg)
{
}
void editpolicy_sttr_save(void)
{
}
void editpolicy_sttr_restore(void)
{
}
int editpolicy_color_head(const int screen)
{
}
int editpolicy_color_cursor(const int screen)
{
}
void editpolicy_line_draw(const int screen)
{
}

#endif