Misc CRT cleanup

This commit is contained in:
Christian Göttsche 2020-10-05 17:24:59 +02:00 committed by cgzones
parent 59edb2e80c
commit 1df7fa387a
2 changed files with 34 additions and 55 deletions

39
CRT.c
View File

@ -28,7 +28,7 @@ in the source distribution for its full text.
#include <sys/types.h> #include <sys/types.h>
#endif #endif
#define ColorIndex(i,j) ((7-i)*8+j) #define ColorIndex(i,j) ((7-(i))*8+(j))
#define ColorPair(i,j) COLOR_PAIR(ColorIndex(i,j)) #define ColorPair(i,j) COLOR_PAIR(ColorIndex(i,j))
@ -74,11 +74,9 @@ bool CRT_utf8 = false;
const char *const *CRT_treeStr = CRT_treeStrAscii; const char *const *CRT_treeStr = CRT_treeStrAscii;
static bool CRT_hasColors; int CRT_delay;
int CRT_delay = 0; const int* CRT_colors;
int* CRT_colors;
int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = { int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[COLORSCHEME_DEFAULT] = { [COLORSCHEME_DEFAULT] = {
@ -544,9 +542,7 @@ int CRT_scrollHAmount = 5;
int CRT_scrollWheelVAmount = 10; int CRT_scrollWheelVAmount = 10;
char* CRT_termType; const char* CRT_termType;
// TODO move color scheme to Settings, perhaps?
int CRT_colorScheme = 0; int CRT_colorScheme = 0;
@ -563,35 +559,29 @@ static int CRT_euid = -1;
static int CRT_egid = -1; static int CRT_egid = -1;
#define DIE(msg) do { CRT_done(); fprintf(stderr, msg); exit(1); } while(0)
void CRT_dropPrivileges() { void CRT_dropPrivileges() {
CRT_egid = getegid(); CRT_egid = getegid();
CRT_euid = geteuid(); CRT_euid = geteuid();
if (setegid(getgid()) == -1) { if (setegid(getgid()) == -1) {
DIE("Fatal error: failed dropping group privileges.\n"); CRT_fatalError("Fatal error: failed dropping group privileges");
} }
if (seteuid(getuid()) == -1) { if (seteuid(getuid()) == -1) {
DIE("Fatal error: failed dropping user privileges.\n"); CRT_fatalError("Fatal error: failed dropping user privileges");
} }
} }
void CRT_restorePrivileges() { void CRT_restorePrivileges() {
if (CRT_egid == -1 || CRT_euid == -1) { if (CRT_egid == -1 || CRT_euid == -1) {
DIE("Fatal error: internal inconsistency.\n"); CRT_fatalError("Fatal error: internal inconsistency");
} }
if (setegid(CRT_egid) == -1) { if (setegid(CRT_egid) == -1) {
DIE("Fatal error: failed restoring group privileges.\n"); CRT_fatalError("Fatal error: failed restoring group privileges");
} }
if (seteuid(CRT_euid) == -1) { if (seteuid(CRT_euid) == -1) {
DIE("Fatal error: failed restoring user privileges.\n"); CRT_fatalError("Fatal error: failed restoring user privileges");
} }
} }
#else /* HAVE_SETUID_ENABLED */
// In this case, the setuid operations are defined as macros in CRT.h
#endif /* HAVE_SETUID_ENABLED */ #endif /* HAVE_SETUID_ENABLED */
static struct sigaction old_sig_handler[32]; static struct sigaction old_sig_handler[32];
@ -601,10 +591,7 @@ static struct sigaction old_sig_handler[32];
void CRT_init(int delay, int colorScheme, bool allowUnicode) { void CRT_init(int delay, int colorScheme, bool allowUnicode) {
initscr(); initscr();
noecho(); noecho();
CRT_delay = delay; CRT_delay = CLAMP(delay, 1, 255);
if (CRT_delay == 0) {
CRT_delay = 1;
}
CRT_colors = CRT_colorSchemes[colorScheme]; CRT_colors = CRT_colorSchemes[colorScheme];
CRT_colorScheme = colorScheme; CRT_colorScheme = colorScheme;
@ -619,12 +606,8 @@ void CRT_init(int delay, int colorScheme, bool allowUnicode) {
keypad(stdscr, true); keypad(stdscr, true);
mouseinterval(0); mouseinterval(0);
curs_set(0); curs_set(0);
if (has_colors()) { if (has_colors())
start_color(); start_color();
CRT_hasColors = true;
} else {
CRT_hasColors = false;
}
CRT_termType = getenv("TERM"); CRT_termType = getenv("TERM");
if (String_eq(CRT_termType, "linux")) if (String_eq(CRT_termType, "linux"))
CRT_scrollHAmount = 20; CRT_scrollHAmount = 20;

34
CRT.h
View File

@ -11,10 +11,6 @@ in the source distribution for its full text.
#include <stdbool.h> #include <stdbool.h>
#define KEY_WHEELUP KEY_F(20)
#define KEY_WHEELDOWN KEY_F(21)
#define KEY_RECLICK KEY_F(22)
typedef enum TreeStr_ { typedef enum TreeStr_ {
TREE_STR_HORZ, TREE_STR_HORZ,
TREE_STR_VERT, TREE_STR_VERT,
@ -28,13 +24,13 @@ typedef enum TreeStr_ {
typedef enum ColorSchemes_ { typedef enum ColorSchemes_ {
COLORSCHEME_DEFAULT = 0, COLORSCHEME_DEFAULT = 0,
COLORSCHEME_MONOCHROME = 1, COLORSCHEME_MONOCHROME,
COLORSCHEME_BLACKONWHITE = 2, COLORSCHEME_BLACKONWHITE,
COLORSCHEME_LIGHTTERMINAL = 3, COLORSCHEME_LIGHTTERMINAL,
COLORSCHEME_MIDNIGHT = 4, COLORSCHEME_MIDNIGHT,
COLORSCHEME_BLACKNIGHT = 5, COLORSCHEME_BLACKNIGHT,
COLORSCHEME_BROKENGRAY = 6, COLORSCHEME_BROKENGRAY,
LAST_COLORSCHEME = 7, LAST_COLORSCHEME,
} ColorSchemes; } ColorSchemes;
typedef enum ColorElements_ { typedef enum ColorElements_ {
@ -119,7 +115,10 @@ void CRT_fatalError(const char* note) ATTR_NORETURN;
void CRT_handleSIGSEGV(int signal) ATTR_NORETURN; void CRT_handleSIGSEGV(int signal) ATTR_NORETURN;
#define KEY_ALT(x) (KEY_F(64 - 26) + (x - 'A')) #define KEY_WHEELUP KEY_F(20)
#define KEY_WHEELDOWN KEY_F(21)
#define KEY_RECLICK KEY_F(22)
#define KEY_ALT(x) (KEY_F(64 - 26) + ((x) - 'A'))
#ifdef HAVE_LIBNCURSESW #ifdef HAVE_LIBNCURSESW
@ -132,7 +131,7 @@ extern const char *const *CRT_treeStr;
extern int CRT_delay; extern int CRT_delay;
extern int* CRT_colors; extern const int* CRT_colors;
extern int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT]; extern int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT];
@ -142,7 +141,7 @@ extern int CRT_scrollHAmount;
extern int CRT_scrollWheelVAmount; extern int CRT_scrollWheelVAmount;
extern char* CRT_termType; extern const char* CRT_termType;
extern int CRT_colorScheme; extern int CRT_colorScheme;
@ -155,11 +154,8 @@ void CRT_restorePrivileges(void);
#else /* HAVE_SETUID_ENABLED */ #else /* HAVE_SETUID_ENABLED */
/* Turn setuid operations into NOPs */ /* Turn setuid operations into NOPs */
static inline void CRT_dropPrivileges(void) { }
#ifndef CRT_dropPrivileges static inline void CRT_restorePrivileges(void) { }
#define CRT_dropPrivileges()
#define CRT_restorePrivileges()
#endif
#endif /* HAVE_SETUID_ENABLED */ #endif /* HAVE_SETUID_ENABLED */