1
by Bartosz Fenski
Import upstream version 0.3.3 |
1 |
/*
|
1.1.3
by Bartosz Fenski
Import upstream version 0.5.4 |
2 |
htop - Settings.c
|
1.1.5
by Bartosz Fenski
Import upstream version 0.6.2 |
3 |
(C) 2004-2006 Hisham H. Muhammad
|
1
by Bartosz Fenski
Import upstream version 0.3.3 |
4 |
Released under the GNU GPL, see the COPYING file
|
5 |
in the source distribution for its full text.
|
|
6 |
*/
|
|
7 |
||
8 |
#include "Settings.h" |
|
9 |
#include "String.h" |
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
10 |
#include "ProcessList.h" |
11 |
#include "Header.h" |
|
12 |
||
13 |
#include "debug.h" |
|
1
by Bartosz Fenski
Import upstream version 0.3.3 |
14 |
|
1.1.3
by Bartosz Fenski
Import upstream version 0.5.4 |
15 |
#define DEFAULT_DELAY 15
|
16 |
||
1
by Bartosz Fenski
Import upstream version 0.3.3 |
17 |
/*{
|
18 |
||
19 |
typedef struct Settings_ {
|
|
20 |
char* userSettings;
|
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
21 |
ProcessList* pl;
|
22 |
Header* header;
|
|
1.1.3
by Bartosz Fenski
Import upstream version 0.5.4 |
23 |
int colorScheme;
|
1.1.4
by Bartosz Fenski
Import upstream version 0.6 |
24 |
bool changed;
|
1.1.3
by Bartosz Fenski
Import upstream version 0.5.4 |
25 |
int delay;
|
1
by Bartosz Fenski
Import upstream version 0.3.3 |
26 |
} Settings;
|
27 |
||
28 |
}*/
|
|
29 |
||
30 |
void Settings_delete(Settings* this) { |
|
31 |
free(this->userSettings); |
|
32 |
free(this); |
|
33 |
}
|
|
34 |
||
1.2.2
by Bartosz Fenski
Import upstream version 0.6.3 |
35 |
static void Settings_readMeters(Settings* this, char* line, HeaderSide side) { |
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
36 |
char* trim = String_trim(line); |
37 |
char** ids = String_split(trim, ' '); |
|
38 |
free(trim); |
|
39 |
int i; |
|
40 |
for (i = 0; ids[i] != NULL; i++) { |
|
41 |
Header_createMeter(this->header, ids[i], side); |
|
42 |
}
|
|
43 |
String_freeArray(ids); |
|
44 |
}
|
|
45 |
||
1.2.2
by Bartosz Fenski
Import upstream version 0.6.3 |
46 |
static void Settings_readMeterModes(Settings* this, char* line, HeaderSide side) { |
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
47 |
char* trim = String_trim(line); |
48 |
char** ids = String_split(trim, ' '); |
|
49 |
free(trim); |
|
50 |
int i; |
|
51 |
for (i = 0; ids[i] != NULL; i++) { |
|
52 |
int mode = atoi(ids[i]); |
|
53 |
Header_setMode(this->header, i, mode, side); |
|
54 |
}
|
|
55 |
String_freeArray(ids); |
|
56 |
}
|
|
57 |
||
1.1.11
by David Futcher
Import upstream version 0.8 |
58 |
static bool Settings_read(Settings* this, char* fileName) { |
1
by Bartosz Fenski
Import upstream version 0.3.3 |
59 |
// TODO: implement File object and make
|
60 |
// file I/O object-oriented.
|
|
61 |
FILE* fd; |
|
62 |
fd = fopen(fileName, "r"); |
|
63 |
if (fd == NULL) { |
|
64 |
return false; |
|
65 |
}
|
|
1.1.11
by David Futcher
Import upstream version 0.8 |
66 |
const int maxLine = 65535; |
1
by Bartosz Fenski
Import upstream version 0.3.3 |
67 |
char buffer[maxLine]; |
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
68 |
bool readMeters = false; |
1
by Bartosz Fenski
Import upstream version 0.3.3 |
69 |
while (!feof(fd)) { |
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
70 |
buffer[0] = '\0'; |
1
by Bartosz Fenski
Import upstream version 0.3.3 |
71 |
fgets(buffer, maxLine, fd); |
72 |
char** option = String_split(buffer, '='); |
|
73 |
if (String_eq(option[0], "fields")) { |
|
74 |
char* trim = String_trim(option[1]); |
|
75 |
char** ids = String_split(trim, ' '); |
|
76 |
free(trim); |
|
1.1.4
by Bartosz Fenski
Import upstream version 0.6 |
77 |
int i, j; |
78 |
for (j = 0, i = 0; i < LAST_PROCESSFIELD && ids[i] != NULL; i++) { |
|
79 |
// This "+1" is for compatibility with the older enum format.
|
|
80 |
int id = atoi(ids[i]) + 1; |
|
81 |
if (id > 0 && id < LAST_PROCESSFIELD) { |
|
82 |
this->pl->fields[j] = id; |
|
83 |
j++; |
|
84 |
}
|
|
1
by Bartosz Fenski
Import upstream version 0.3.3 |
85 |
}
|
1.1.4
by Bartosz Fenski
Import upstream version 0.6 |
86 |
this->pl->fields[j] = (ProcessField) NULL; |
1
by Bartosz Fenski
Import upstream version 0.3.3 |
87 |
String_freeArray(ids); |
88 |
} else if (String_eq(option[0], "sort_key")) { |
|
1.1.4
by Bartosz Fenski
Import upstream version 0.6 |
89 |
// This "+1" is for compatibility with the older enum format.
|
90 |
this->pl->sortKey = atoi(option[1]) + 1; |
|
1
by Bartosz Fenski
Import upstream version 0.3.3 |
91 |
} else if (String_eq(option[0], "sort_direction")) { |
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
92 |
this->pl->direction = atoi(option[1]); |
93 |
} else if (String_eq(option[0], "tree_view")) { |
|
94 |
this->pl->treeView = atoi(option[1]); |
|
95 |
} else if (String_eq(option[0], "hide_threads")) { |
|
96 |
this->pl->hideThreads = atoi(option[1]); |
|
97 |
} else if (String_eq(option[0], "hide_kernel_threads")) { |
|
98 |
this->pl->hideKernelThreads = atoi(option[1]); |
|
1.2.1
by Bartosz Fenski
Import upstream version 0.5.1 |
99 |
} else if (String_eq(option[0], "hide_userland_threads")) { |
100 |
this->pl->hideUserlandThreads = atoi(option[1]); |
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
101 |
} else if (String_eq(option[0], "shadow_other_users")) { |
102 |
this->pl->shadowOtherUsers = atoi(option[1]); |
|
103 |
} else if (String_eq(option[0], "highlight_base_name")) { |
|
104 |
this->pl->highlightBaseName = atoi(option[1]); |
|
105 |
} else if (String_eq(option[0], "highlight_megabytes")) { |
|
106 |
this->pl->highlightMegabytes = atoi(option[1]); |
|
1.1.11
by David Futcher
Import upstream version 0.8 |
107 |
} else if (String_eq(option[0], "highlight_threads")) { |
108 |
this->pl->highlightThreads = atoi(option[1]); |
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
109 |
} else if (String_eq(option[0], "header_margin")) { |
110 |
this->header->margin = atoi(option[1]); |
|
1.1.7
by Adrien Cunin
Import upstream version 0.6.5 |
111 |
} else if (String_eq(option[0], "expand_system_time")) { |
1.2.3
by Bartosz Fenski
Import upstream version 0.7 |
112 |
// Compatibility option.
|
113 |
this->pl->detailedCPUTime = atoi(option[1]); |
|
114 |
} else if (String_eq(option[0], "detailed_cpu_time")) { |
|
115 |
this->pl->detailedCPUTime = atoi(option[1]); |
|
1.1.3
by Bartosz Fenski
Import upstream version 0.5.4 |
116 |
} else if (String_eq(option[0], "delay")) { |
117 |
this->delay = atoi(option[1]); |
|
118 |
} else if (String_eq(option[0], "color_scheme")) { |
|
119 |
this->colorScheme = atoi(option[1]); |
|
120 |
if (this->colorScheme < 0) this->colorScheme = 0; |
|
121 |
if (this->colorScheme > 5) this->colorScheme = 5; |
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
122 |
} else if (String_eq(option[0], "left_meters")) { |
1.1.5
by Bartosz Fenski
Import upstream version 0.6.2 |
123 |
Settings_readMeters(this, option[1], LEFT_HEADER); |
124 |
readMeters = true; |
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
125 |
} else if (String_eq(option[0], "right_meters")) { |
1.1.5
by Bartosz Fenski
Import upstream version 0.6.2 |
126 |
Settings_readMeters(this, option[1], RIGHT_HEADER); |
127 |
readMeters = true; |
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
128 |
} else if (String_eq(option[0], "left_meter_modes")) { |
1.1.5
by Bartosz Fenski
Import upstream version 0.6.2 |
129 |
Settings_readMeterModes(this, option[1], LEFT_HEADER); |
130 |
readMeters = true; |
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
131 |
} else if (String_eq(option[0], "right_meter_modes")) { |
1.1.5
by Bartosz Fenski
Import upstream version 0.6.2 |
132 |
Settings_readMeterModes(this, option[1], RIGHT_HEADER); |
133 |
readMeters = true; |
|
1
by Bartosz Fenski
Import upstream version 0.3.3 |
134 |
}
|
135 |
String_freeArray(option); |
|
136 |
}
|
|
137 |
fclose(fd); |
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
138 |
if (!readMeters) { |
139 |
Header_defaultMeters(this->header); |
|
140 |
}
|
|
1
by Bartosz Fenski
Import upstream version 0.3.3 |
141 |
return true; |
142 |
}
|
|
143 |
||
144 |
bool Settings_write(Settings* this) { |
|
145 |
// TODO: implement File object and make
|
|
146 |
// file I/O object-oriented.
|
|
147 |
FILE* fd; |
|
148 |
fd = fopen(this->userSettings, "w"); |
|
149 |
if (fd == NULL) { |
|
150 |
return false; |
|
151 |
}
|
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
152 |
fprintf(fd, "# Beware! This file is rewritten every time htop exits.\n"); |
153 |
fprintf(fd, "# The parser is also very primitive, and not human-friendly.\n"); |
|
154 |
fprintf(fd, "# (I know, it's in the todo list).\n"); |
|
1
by Bartosz Fenski
Import upstream version 0.3.3 |
155 |
fprintf(fd, "fields="); |
1.1.4
by Bartosz Fenski
Import upstream version 0.6 |
156 |
for (int i = 0; this->pl->fields[i]; i++) { |
157 |
// This "-1" is for compatibility with the older enum format.
|
|
158 |
fprintf(fd, "%d ", (int) this->pl->fields[i]-1); |
|
1
by Bartosz Fenski
Import upstream version 0.3.3 |
159 |
}
|
160 |
fprintf(fd, "\n"); |
|
1.1.4
by Bartosz Fenski
Import upstream version 0.6 |
161 |
// This "-1" is for compatibility with the older enum format.
|
162 |
fprintf(fd, "sort_key=%d\n", (int) this->pl->sortKey-1); |
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
163 |
fprintf(fd, "sort_direction=%d\n", (int) this->pl->direction); |
164 |
fprintf(fd, "hide_threads=%d\n", (int) this->pl->hideThreads); |
|
165 |
fprintf(fd, "hide_kernel_threads=%d\n", (int) this->pl->hideKernelThreads); |
|
1.2.1
by Bartosz Fenski
Import upstream version 0.5.1 |
166 |
fprintf(fd, "hide_userland_threads=%d\n", (int) this->pl->hideUserlandThreads); |
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
167 |
fprintf(fd, "shadow_other_users=%d\n", (int) this->pl->shadowOtherUsers); |
168 |
fprintf(fd, "highlight_base_name=%d\n", (int) this->pl->highlightBaseName); |
|
169 |
fprintf(fd, "highlight_megabytes=%d\n", (int) this->pl->highlightMegabytes); |
|
1.1.11
by David Futcher
Import upstream version 0.8 |
170 |
fprintf(fd, "highlight_threads=%d\n", (int) this->pl->highlightThreads); |
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
171 |
fprintf(fd, "tree_view=%d\n", (int) this->pl->treeView); |
172 |
fprintf(fd, "header_margin=%d\n", (int) this->header->margin); |
|
1.2.3
by Bartosz Fenski
Import upstream version 0.7 |
173 |
fprintf(fd, "detailed_cpu_time=%d\n", (int) this->pl->detailedCPUTime); |
1.1.3
by Bartosz Fenski
Import upstream version 0.5.4 |
174 |
fprintf(fd, "color_scheme=%d\n", (int) this->colorScheme); |
175 |
fprintf(fd, "delay=%d\n", (int) this->delay); |
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
176 |
fprintf(fd, "left_meters="); |
1.1.3
by Bartosz Fenski
Import upstream version 0.5.4 |
177 |
for (int i = 0; i < Header_size(this->header, LEFT_HEADER); i++) { |
1.1.5
by Bartosz Fenski
Import upstream version 0.6.2 |
178 |
char* name = Header_readMeterName(this->header, i, LEFT_HEADER); |
179 |
fprintf(fd, "%s ", name); |
|
180 |
free(name); |
|
1.1.3
by Bartosz Fenski
Import upstream version 0.5.4 |
181 |
}
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
182 |
fprintf(fd, "\n"); |
183 |
fprintf(fd, "left_meter_modes="); |
|
184 |
for (int i = 0; i < Header_size(this->header, LEFT_HEADER); i++) |
|
185 |
fprintf(fd, "%d ", Header_readMeterMode(this->header, i, LEFT_HEADER)); |
|
186 |
fprintf(fd, "\n"); |
|
187 |
fprintf(fd, "right_meters="); |
|
1.1.5
by Bartosz Fenski
Import upstream version 0.6.2 |
188 |
for (int i = 0; i < Header_size(this->header, RIGHT_HEADER); i++) { |
189 |
char* name = Header_readMeterName(this->header, i, RIGHT_HEADER); |
|
190 |
fprintf(fd, "%s ", name); |
|
191 |
free(name); |
|
192 |
}
|
|
1.1.1
by Bartosz Fenski
Import upstream version 0.5 |
193 |
fprintf(fd, "\n"); |
194 |
fprintf(fd, "right_meter_modes="); |
|
195 |
for (int i = 0; i < Header_size(this->header, RIGHT_HEADER); i++) |
|
196 |
fprintf(fd, "%d ", Header_readMeterMode(this->header, i, RIGHT_HEADER)); |
|
1.1.9
by Bartosz Fenski
Import upstream version 0.6.6+svn20070915 |
197 |
fprintf(fd, "\n"); |
1
by Bartosz Fenski
Import upstream version 0.3.3 |
198 |
fclose(fd); |
199 |
return true; |
|
200 |
}
|
|
1.1.11
by David Futcher
Import upstream version 0.8 |
201 |
|
202 |
Settings* Settings_new(ProcessList* pl, Header* header) { |
|
203 |
Settings* this = malloc(sizeof(Settings)); |
|
204 |
this->pl = pl; |
|
205 |
this->header = header; |
|
206 |
char* home; |
|
207 |
char* rcfile; |
|
208 |
home = getenv("HOME_ETC"); |
|
209 |
if (!home) home = getenv("HOME"); |
|
210 |
if (!home) home = ""; |
|
211 |
rcfile = getenv("HOMERC"); |
|
212 |
if (!rcfile) |
|
213 |
this->userSettings = String_cat(home, "/.htoprc"); |
|
214 |
else
|
|
215 |
this->userSettings = String_copy(rcfile); |
|
216 |
this->colorScheme = 0; |
|
217 |
this->changed = false; |
|
218 |
this->delay = DEFAULT_DELAY; |
|
219 |
bool ok = Settings_read(this, this->userSettings); |
|
220 |
if (!ok) { |
|
221 |
this->changed = true; |
|
222 |
// TODO: how to get SYSCONFDIR correctly through Autoconf?
|
|
223 |
char* systemSettings = String_cat(SYSCONFDIR, "/htoprc"); |
|
224 |
ok = Settings_read(this, systemSettings); |
|
225 |
free(systemSettings); |
|
226 |
if (!ok) { |
|
227 |
Header_defaultMeters(this->header); |
|
228 |
pl->hideKernelThreads = true; |
|
229 |
pl->highlightMegabytes = true; |
|
230 |
pl->highlightThreads = false; |
|
231 |
}
|
|
232 |
}
|
|
233 |
return this; |
|
234 |
}
|