2006-03-04 18:16:49 +00:00
|
|
|
/*
|
|
|
|
htop - Settings.c
|
2011-05-26 16:35:07 +00:00
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2006-03-04 18:16:49 +00:00
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Settings.h"
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "String.h"
|
|
|
|
#include "Vector.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#define DEFAULT_DELAY 15
|
|
|
|
|
|
|
|
/*{
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "ProcessList.h"
|
|
|
|
#include "Header.h"
|
|
|
|
#include <stdbool.h>
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
typedef struct Settings_ {
|
|
|
|
char* userSettings;
|
|
|
|
ProcessList* pl;
|
|
|
|
Header* header;
|
|
|
|
int colorScheme;
|
|
|
|
int delay;
|
2014-02-27 19:35:22 +00:00
|
|
|
bool changed;
|
2006-03-04 18:16:49 +00:00
|
|
|
} Settings;
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
void Settings_delete(Settings* this) {
|
|
|
|
free(this->userSettings);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2006-06-06 20:41:01 +00:00
|
|
|
static void Settings_readMeters(Settings* this, char* line, HeaderSide side) {
|
2006-03-04 18:16:49 +00:00
|
|
|
char* trim = String_trim(line);
|
2011-08-29 20:45:29 +00:00
|
|
|
int nIds;
|
|
|
|
char** ids = String_split(trim, ' ', &nIds);
|
2006-03-04 18:16:49 +00:00
|
|
|
free(trim);
|
2011-08-29 20:45:29 +00:00
|
|
|
for (int i = 0; ids[i]; i++) {
|
2006-03-04 18:16:49 +00:00
|
|
|
Header_createMeter(this->header, ids[i], side);
|
|
|
|
}
|
|
|
|
String_freeArray(ids);
|
|
|
|
}
|
|
|
|
|
2006-06-06 20:41:01 +00:00
|
|
|
static void Settings_readMeterModes(Settings* this, char* line, HeaderSide side) {
|
2006-03-04 18:16:49 +00:00
|
|
|
char* trim = String_trim(line);
|
2011-08-29 20:45:29 +00:00
|
|
|
int nIds;
|
|
|
|
char** ids = String_split(trim, ' ', &nIds);
|
2006-03-04 18:16:49 +00:00
|
|
|
free(trim);
|
2011-08-29 20:45:29 +00:00
|
|
|
for (int i = 0; ids[i]; i++) {
|
2006-03-04 18:16:49 +00:00
|
|
|
int mode = atoi(ids[i]);
|
|
|
|
Header_setMode(this->header, i, mode, side);
|
|
|
|
}
|
|
|
|
String_freeArray(ids);
|
|
|
|
}
|
|
|
|
|
2011-11-18 06:08:56 +00:00
|
|
|
static bool Settings_read(Settings* this, char* fileName, int cpuCount) {
|
2011-12-25 20:22:41 +00:00
|
|
|
FILE* fd = fopen(fileName, "r");
|
|
|
|
if (!fd)
|
2006-03-04 18:16:49 +00:00
|
|
|
return false;
|
2011-12-25 20:22:41 +00:00
|
|
|
|
2010-02-22 20:54:01 +00:00
|
|
|
const int maxLine = 2048;
|
2006-03-04 18:16:49 +00:00
|
|
|
char buffer[maxLine];
|
|
|
|
bool readMeters = false;
|
2010-02-22 20:54:01 +00:00
|
|
|
while (fgets(buffer, maxLine, fd)) {
|
2011-08-29 20:45:29 +00:00
|
|
|
int nOptions;
|
|
|
|
char** option = String_split(buffer, '=', &nOptions);
|
|
|
|
if (nOptions < 2) {
|
|
|
|
String_freeArray(option);
|
|
|
|
continue;
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
if (String_eq(option[0], "fields")) {
|
|
|
|
char* trim = String_trim(option[1]);
|
2011-08-29 20:45:29 +00:00
|
|
|
int nIds;
|
|
|
|
char** ids = String_split(trim, ' ', &nIds);
|
2006-03-04 18:16:49 +00:00
|
|
|
free(trim);
|
|
|
|
int i, j;
|
2013-05-24 22:46:01 +00:00
|
|
|
this->pl->flags = 0;
|
2011-08-29 20:45:29 +00:00
|
|
|
for (j = 0, i = 0; i < LAST_PROCESSFIELD && ids[i]; i++) {
|
2006-03-04 18:16:49 +00:00
|
|
|
// This "+1" is for compatibility with the older enum format.
|
|
|
|
int id = atoi(ids[i]) + 1;
|
|
|
|
if (id > 0 && id < LAST_PROCESSFIELD) {
|
|
|
|
this->pl->fields[j] = id;
|
2013-05-24 22:46:01 +00:00
|
|
|
this->pl->flags |= Process_fieldFlags[id];
|
2006-03-04 18:16:49 +00:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->pl->fields[j] = (ProcessField) NULL;
|
|
|
|
String_freeArray(ids);
|
|
|
|
} else if (String_eq(option[0], "sort_key")) {
|
|
|
|
// This "+1" is for compatibility with the older enum format.
|
|
|
|
this->pl->sortKey = atoi(option[1]) + 1;
|
|
|
|
} else if (String_eq(option[0], "sort_direction")) {
|
|
|
|
this->pl->direction = atoi(option[1]);
|
|
|
|
} else if (String_eq(option[0], "tree_view")) {
|
|
|
|
this->pl->treeView = atoi(option[1]);
|
|
|
|
} else if (String_eq(option[0], "hide_threads")) {
|
|
|
|
this->pl->hideThreads = atoi(option[1]);
|
|
|
|
} else if (String_eq(option[0], "hide_kernel_threads")) {
|
|
|
|
this->pl->hideKernelThreads = atoi(option[1]);
|
|
|
|
} else if (String_eq(option[0], "hide_userland_threads")) {
|
|
|
|
this->pl->hideUserlandThreads = atoi(option[1]);
|
|
|
|
} else if (String_eq(option[0], "shadow_other_users")) {
|
|
|
|
this->pl->shadowOtherUsers = atoi(option[1]);
|
2010-02-25 01:37:31 +00:00
|
|
|
} else if (String_eq(option[0], "show_thread_names")) {
|
|
|
|
this->pl->showThreadNames = atoi(option[1]);
|
2006-03-04 18:16:49 +00:00
|
|
|
} else if (String_eq(option[0], "highlight_base_name")) {
|
|
|
|
this->pl->highlightBaseName = atoi(option[1]);
|
|
|
|
} else if (String_eq(option[0], "highlight_megabytes")) {
|
|
|
|
this->pl->highlightMegabytes = atoi(option[1]);
|
2008-03-08 23:39:48 +00:00
|
|
|
} else if (String_eq(option[0], "highlight_threads")) {
|
|
|
|
this->pl->highlightThreads = atoi(option[1]);
|
2006-03-04 18:16:49 +00:00
|
|
|
} else if (String_eq(option[0], "header_margin")) {
|
|
|
|
this->header->margin = atoi(option[1]);
|
2006-10-04 14:21:27 +00:00
|
|
|
} else if (String_eq(option[0], "expand_system_time")) {
|
2007-11-09 00:40:59 +00:00
|
|
|
// Compatibility option.
|
|
|
|
this->pl->detailedCPUTime = atoi(option[1]);
|
|
|
|
} else if (String_eq(option[0], "detailed_cpu_time")) {
|
|
|
|
this->pl->detailedCPUTime = atoi(option[1]);
|
2011-03-22 20:37:08 +00:00
|
|
|
} else if (String_eq(option[0], "cpu_count_from_zero")) {
|
|
|
|
this->pl->countCPUsFromZero = atoi(option[1]);
|
2012-10-20 00:43:25 +00:00
|
|
|
} else if (String_eq(option[0], "update_process_names")) {
|
|
|
|
this->pl->updateProcessNames = atoi(option[1]);
|
2013-12-18 02:58:34 +00:00
|
|
|
} else if (String_eq(option[0], "account_guest_in_cpu_meter")) {
|
|
|
|
this->pl->accountGuestInCPUMeter = atoi(option[1]);
|
2006-03-04 18:16:49 +00:00
|
|
|
} else if (String_eq(option[0], "delay")) {
|
|
|
|
this->delay = atoi(option[1]);
|
|
|
|
} else if (String_eq(option[0], "color_scheme")) {
|
|
|
|
this->colorScheme = atoi(option[1]);
|
|
|
|
if (this->colorScheme < 0) this->colorScheme = 0;
|
|
|
|
if (this->colorScheme > 5) this->colorScheme = 5;
|
|
|
|
} else if (String_eq(option[0], "left_meters")) {
|
2006-04-10 20:40:38 +00:00
|
|
|
Settings_readMeters(this, option[1], LEFT_HEADER);
|
|
|
|
readMeters = true;
|
2006-03-04 18:16:49 +00:00
|
|
|
} else if (String_eq(option[0], "right_meters")) {
|
2006-04-10 20:40:38 +00:00
|
|
|
Settings_readMeters(this, option[1], RIGHT_HEADER);
|
|
|
|
readMeters = true;
|
2006-03-04 18:16:49 +00:00
|
|
|
} else if (String_eq(option[0], "left_meter_modes")) {
|
2006-04-10 20:40:38 +00:00
|
|
|
Settings_readMeterModes(this, option[1], LEFT_HEADER);
|
|
|
|
readMeters = true;
|
2006-03-04 18:16:49 +00:00
|
|
|
} else if (String_eq(option[0], "right_meter_modes")) {
|
2006-04-10 20:40:38 +00:00
|
|
|
Settings_readMeterModes(this, option[1], RIGHT_HEADER);
|
|
|
|
readMeters = true;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
String_freeArray(option);
|
|
|
|
}
|
|
|
|
fclose(fd);
|
|
|
|
if (!readMeters) {
|
2011-11-18 06:08:56 +00:00
|
|
|
Header_defaultMeters(this->header, cpuCount);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Settings_write(Settings* this) {
|
|
|
|
// TODO: implement File object and make
|
|
|
|
// file I/O object-oriented.
|
|
|
|
FILE* fd;
|
|
|
|
fd = fopen(this->userSettings, "w");
|
|
|
|
if (fd == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-12 16:37:27 +00:00
|
|
|
fprintf(fd, "# Beware! This file is rewritten by htop when settings are changed in the interface.\n");
|
2006-03-04 18:16:49 +00:00
|
|
|
fprintf(fd, "# The parser is also very primitive, and not human-friendly.\n");
|
|
|
|
fprintf(fd, "fields=");
|
|
|
|
for (int i = 0; this->pl->fields[i]; i++) {
|
|
|
|
// This "-1" is for compatibility with the older enum format.
|
|
|
|
fprintf(fd, "%d ", (int) this->pl->fields[i]-1);
|
|
|
|
}
|
|
|
|
fprintf(fd, "\n");
|
|
|
|
// This "-1" is for compatibility with the older enum format.
|
|
|
|
fprintf(fd, "sort_key=%d\n", (int) this->pl->sortKey-1);
|
|
|
|
fprintf(fd, "sort_direction=%d\n", (int) this->pl->direction);
|
|
|
|
fprintf(fd, "hide_threads=%d\n", (int) this->pl->hideThreads);
|
|
|
|
fprintf(fd, "hide_kernel_threads=%d\n", (int) this->pl->hideKernelThreads);
|
|
|
|
fprintf(fd, "hide_userland_threads=%d\n", (int) this->pl->hideUserlandThreads);
|
|
|
|
fprintf(fd, "shadow_other_users=%d\n", (int) this->pl->shadowOtherUsers);
|
2010-02-25 01:37:31 +00:00
|
|
|
fprintf(fd, "show_thread_names=%d\n", (int) this->pl->showThreadNames);
|
2006-03-04 18:16:49 +00:00
|
|
|
fprintf(fd, "highlight_base_name=%d\n", (int) this->pl->highlightBaseName);
|
|
|
|
fprintf(fd, "highlight_megabytes=%d\n", (int) this->pl->highlightMegabytes);
|
2008-03-08 23:39:48 +00:00
|
|
|
fprintf(fd, "highlight_threads=%d\n", (int) this->pl->highlightThreads);
|
2006-03-04 18:16:49 +00:00
|
|
|
fprintf(fd, "tree_view=%d\n", (int) this->pl->treeView);
|
|
|
|
fprintf(fd, "header_margin=%d\n", (int) this->header->margin);
|
2007-11-09 00:40:59 +00:00
|
|
|
fprintf(fd, "detailed_cpu_time=%d\n", (int) this->pl->detailedCPUTime);
|
2011-03-22 20:37:08 +00:00
|
|
|
fprintf(fd, "cpu_count_from_zero=%d\n", (int) this->pl->countCPUsFromZero);
|
2012-10-20 00:43:25 +00:00
|
|
|
fprintf(fd, "update_process_names=%d\n", (int) this->pl->updateProcessNames);
|
2013-12-18 02:58:34 +00:00
|
|
|
fprintf(fd, "account_guest_in_cpu_meter=%d\n", (int) this->pl->accountGuestInCPUMeter);
|
2006-03-04 18:16:49 +00:00
|
|
|
fprintf(fd, "color_scheme=%d\n", (int) this->colorScheme);
|
|
|
|
fprintf(fd, "delay=%d\n", (int) this->delay);
|
|
|
|
fprintf(fd, "left_meters=");
|
|
|
|
for (int i = 0; i < Header_size(this->header, LEFT_HEADER); i++) {
|
2006-04-10 20:40:38 +00:00
|
|
|
char* name = Header_readMeterName(this->header, i, LEFT_HEADER);
|
|
|
|
fprintf(fd, "%s ", name);
|
|
|
|
free(name);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
fprintf(fd, "\n");
|
|
|
|
fprintf(fd, "left_meter_modes=");
|
|
|
|
for (int i = 0; i < Header_size(this->header, LEFT_HEADER); i++)
|
|
|
|
fprintf(fd, "%d ", Header_readMeterMode(this->header, i, LEFT_HEADER));
|
|
|
|
fprintf(fd, "\n");
|
|
|
|
fprintf(fd, "right_meters=");
|
2006-04-10 20:40:38 +00:00
|
|
|
for (int i = 0; i < Header_size(this->header, RIGHT_HEADER); i++) {
|
|
|
|
char* name = Header_readMeterName(this->header, i, RIGHT_HEADER);
|
|
|
|
fprintf(fd, "%s ", name);
|
|
|
|
free(name);
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
fprintf(fd, "\n");
|
|
|
|
fprintf(fd, "right_meter_modes=");
|
|
|
|
for (int i = 0; i < Header_size(this->header, RIGHT_HEADER); i++)
|
|
|
|
fprintf(fd, "%d ", Header_readMeterMode(this->header, i, RIGHT_HEADER));
|
2007-08-10 06:26:39 +00:00
|
|
|
fprintf(fd, "\n");
|
2006-03-04 18:16:49 +00:00
|
|
|
fclose(fd);
|
|
|
|
return true;
|
|
|
|
}
|
2008-03-09 08:58:38 +00:00
|
|
|
|
2011-11-18 06:08:56 +00:00
|
|
|
Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) {
|
2008-03-09 08:58:38 +00:00
|
|
|
Settings* this = malloc(sizeof(Settings));
|
|
|
|
this->pl = pl;
|
|
|
|
this->header = header;
|
2011-12-25 20:22:41 +00:00
|
|
|
char* legacyDotfile = NULL;
|
|
|
|
char* rcfile = getenv("HTOPRC");
|
|
|
|
if (rcfile) {
|
|
|
|
this->userSettings = strdup(rcfile);
|
|
|
|
} else {
|
|
|
|
const char* home = getenv("HOME");
|
|
|
|
if (!home) home = "";
|
|
|
|
const char* xdgConfigHome = getenv("XDG_CONFIG_HOME");
|
|
|
|
char* configDir = NULL;
|
|
|
|
char* htopDir = NULL;
|
|
|
|
if (xdgConfigHome) {
|
|
|
|
this->userSettings = String_cat(xdgConfigHome, "/htop/htoprc");
|
|
|
|
configDir = strdup(xdgConfigHome);
|
|
|
|
htopDir = String_cat(xdgConfigHome, "/htop");
|
|
|
|
} else {
|
|
|
|
this->userSettings = String_cat(home, "/.config/htop/htoprc");
|
|
|
|
configDir = String_cat(home, "/.config");
|
|
|
|
htopDir = String_cat(home, "/.config/htop");
|
|
|
|
}
|
|
|
|
legacyDotfile = String_cat(home, "/.htoprc");
|
2014-04-22 23:35:57 +00:00
|
|
|
(void) mkdir(configDir, 0700);
|
|
|
|
(void) mkdir(htopDir, 0700);
|
2011-12-25 20:22:41 +00:00
|
|
|
free(htopDir);
|
|
|
|
free(configDir);
|
2012-03-05 11:12:58 +00:00
|
|
|
struct stat st;
|
2014-04-22 23:35:57 +00:00
|
|
|
if (lstat(legacyDotfile, &st) != 0) {
|
|
|
|
st.st_mode = 0;
|
|
|
|
}
|
2012-03-05 11:12:58 +00:00
|
|
|
if (access(legacyDotfile, R_OK) != 0 || S_ISLNK(st.st_mode)) {
|
2011-12-25 20:22:41 +00:00
|
|
|
free(legacyDotfile);
|
|
|
|
legacyDotfile = NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-03-09 08:58:38 +00:00
|
|
|
this->colorScheme = 0;
|
|
|
|
this->changed = false;
|
|
|
|
this->delay = DEFAULT_DELAY;
|
2011-12-25 20:22:41 +00:00
|
|
|
bool ok = Settings_read(this, legacyDotfile ? legacyDotfile : this->userSettings, cpuCount);
|
|
|
|
if (ok) {
|
|
|
|
if (legacyDotfile) {
|
|
|
|
// Transition to new location and delete old configuration file
|
|
|
|
if (Settings_write(this))
|
|
|
|
unlink(legacyDotfile);
|
|
|
|
}
|
|
|
|
} else {
|
2008-03-09 08:58:38 +00:00
|
|
|
this->changed = true;
|
|
|
|
// TODO: how to get SYSCONFDIR correctly through Autoconf?
|
|
|
|
char* systemSettings = String_cat(SYSCONFDIR, "/htoprc");
|
2011-11-18 06:08:56 +00:00
|
|
|
ok = Settings_read(this, systemSettings, cpuCount);
|
2008-03-09 08:58:38 +00:00
|
|
|
free(systemSettings);
|
|
|
|
if (!ok) {
|
2011-11-18 06:08:56 +00:00
|
|
|
Header_defaultMeters(this->header, cpuCount);
|
2008-03-09 08:58:38 +00:00
|
|
|
pl->hideKernelThreads = true;
|
|
|
|
pl->highlightMegabytes = true;
|
|
|
|
pl->highlightThreads = false;
|
|
|
|
}
|
|
|
|
}
|
2014-04-24 22:29:20 +00:00
|
|
|
free(legacyDotfile);
|
2008-03-09 08:58:38 +00:00
|
|
|
return this;
|
|
|
|
}
|