Merge branch 'config-code-size'

This commit is contained in:
Daniel Eklöf 2021-05-08 13:58:25 +02:00
commit df6c6f5bd6
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
6 changed files with 186 additions and 106 deletions

179
config.c
View file

@ -91,43 +91,76 @@ static const char *const binding_action_map[] = {
static_assert(ALEN(binding_action_map) == BIND_ACTION_COUNT,
"binding action map size mismatch");
#define LOG_AND_NOTIFY_ERR(...) \
do { \
LOG_ERR(__VA_ARGS__); \
char *text = xasprintf(__VA_ARGS__); \
struct user_notification notif = { \
.kind = USER_NOTIFICATION_ERROR, \
.text = text, \
}; \
tll_push_back(conf->notifications, notif); \
} while (0)
static void NOINLINE PRINTF(5)
log_and_notify(struct config *conf, enum log_class log_class,
const char *file, int lineno, const char *fmt, ...)
{
enum user_notification_kind kind;
#define LOG_AND_NOTIFY_WARN(...) \
do { \
LOG_WARN(__VA_ARGS__); \
char *text = xasprintf(__VA_ARGS__); \
struct user_notification notif = { \
.kind = USER_NOTIFICATION_WARNING, \
.text = text, \
}; \
tll_push_back(conf->notifications, notif); \
} while (0)
switch (log_class) {
case LOG_CLASS_WARNING: kind = USER_NOTIFICATION_WARNING; break;
case LOG_CLASS_ERROR: kind = USER_NOTIFICATION_ERROR; break;
#define LOG_AND_NOTIFY_ERRNO(...) \
do { \
int errno_copy = errno; \
LOG_ERRNO(__VA_ARGS__); \
int len = snprintf(NULL, 0, __VA_ARGS__); \
int errno_len = snprintf(NULL, 0, ": %s", strerror(errno_copy)); \
char *text = xmalloc(len + errno_len + 1); \
snprintf(text, len + errno_len + 1, __VA_ARGS__); \
snprintf(&text[len], errno_len + 1, ": %s", strerror(errno_copy)); \
struct user_notification notif = { \
.kind = USER_NOTIFICATION_ERROR, \
.text = text, \
}; \
tll_push_back(conf->notifications, notif); \
} while(0)
case LOG_CLASS_INFO:
case LOG_CLASS_DEBUG:
BUG("unsupported log class: %d", log_class);
break;
}
va_list va1, va2;
va_start(va1, fmt);
va_copy(va2, va1);
log_msg_va(log_class, LOG_MODULE, file, lineno, fmt, va1);
char *text = xvasprintf(fmt, va2);
tll_push_back(
conf->notifications,
((struct user_notification){.kind = kind, .text = text}));
va_end(va2);
va_end(va1);
}
static void NOINLINE PRINTF(5)
log_errno_and_notify(struct config *conf, enum log_class log_class,
const char *file, int lineno, const char *fmt, ...)
{
int errno_copy = errno;
va_list va1, va2, va3;
va_start(va1, fmt);
va_copy(va2, va1);
va_copy(va3, va2);
log_errno_provided_va(
log_class, LOG_MODULE, file, lineno, errno_copy, fmt, va1);
int len = vsnprintf(NULL, 0, fmt, va2);
int errno_len = snprintf(NULL, 0, ": %s", strerror(errno_copy));
char *text = xmalloc(len + errno_len + 1);
vsnprintf(text, len + errno_len + 1, fmt, va3);
snprintf(&text[len], errno_len + 1, ": %s", strerror(errno_copy));
tll_push_back(
conf->notifications,
((struct user_notification){
.kind = USER_NOTIFICATION_ERROR, .text = text}));
va_end(va3);
va_end(va2);
va_end(va1);
}
#define LOG_AND_NOTIFY_ERR(...) \
log_and_notify(conf, LOG_CLASS_ERROR, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_AND_NOTIFY_WARN(...) \
log_and_notify(conf, LOG_CLASS_WARNING, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_AND_NOTIFY_ERRNO(...) \
log_errno_and_notify(conf, LOG_CLASS_ERROR, __FILE__, __LINE__, __VA_ARGS__)
static char *
get_shell(void)
@ -329,7 +362,7 @@ done:
goto out;
}
static bool
static bool NOINLINE
str_to_bool(const char *s)
{
return strcasecmp(s, "on") == 0 ||
@ -338,7 +371,7 @@ str_to_bool(const char *s)
strtoul(s, NULL, 0) > 0;
}
static bool
static bool NOINLINE
str_to_ulong(const char *s, int base, unsigned long *res)
{
if (s == NULL)
@ -351,7 +384,7 @@ str_to_ulong(const char *s, int base, unsigned long *res)
return errno == 0 && *end == '\0';
}
static bool
static bool NOINLINE
str_to_double(const char *s, double *res)
{
if (s == NULL)
@ -364,7 +397,7 @@ str_to_double(const char *s, double *res)
return errno == 0 && *end == '\0';
}
static bool
static bool NOINLINE
str_to_wchars(const char *s, wchar_t **res, struct config *conf,
const char *path, int lineno,
const char *section, const char *key)
@ -383,7 +416,7 @@ str_to_wchars(const char *s, wchar_t **res, struct config *conf,
return true;
}
static bool
static bool NOINLINE
str_to_color(const char *s, uint32_t *color, bool allow_alpha,
struct config *conf, const char *path, int lineno,
const char *section, const char *key)
@ -406,7 +439,7 @@ str_to_color(const char *s, uint32_t *color, bool allow_alpha,
return true;
}
static bool
static bool NOINLINE
str_to_two_colors(const char *s, uint32_t *first, uint32_t *second,
bool allow_alpha, struct config *conf, const char *path,
int lineno, const char *section, const char *key)
@ -430,7 +463,7 @@ str_to_two_colors(const char *s, uint32_t *first, uint32_t *second,
return true;
}
static bool
static bool NOINLINE
str_to_pt_or_px(const char *s, struct pt_or_px *res, struct config *conf,
const char *path, int lineno, const char *section, const char *key)
{
@ -464,7 +497,7 @@ str_to_pt_or_px(const char *s, struct pt_or_px *res, struct config *conf,
return true;
}
static bool
static bool NOINLINE
str_to_spawn_template(struct config *conf,
const char *s, struct config_spawn_template *template,
const char *path, int lineno, const char *section,
@ -2112,15 +2145,20 @@ get_server_socket_path(void)
return xasprintf("%s/foot-%s.sock", xdg_runtime, wayland_display);
}
static void NOINLINE
add_key_binding(config_key_binding_list_t *list, int action,
const struct config_key_modifiers *mods, xkb_keysym_t sym)
{
tll_push_back(
*list,
((struct config_key_binding){action, *mods, sym}));
}
static void
add_default_key_bindings(struct config *conf)
{
#define add_binding(action, mods, sym) \
do { \
tll_push_back( \
conf->bindings.key, \
((struct config_key_binding){action, mods, sym})); \
} while (0)
#define add_binding(action, mods, sym) \
add_key_binding(&conf->bindings.key, action, &mods, sym)
const struct config_key_modifiers shift = {.shift = true};
const struct config_key_modifiers ctrl = {.ctrl = true};
@ -2142,18 +2180,15 @@ add_default_key_bindings(struct config *conf)
add_binding(BIND_ACTION_SPAWN_TERMINAL, ctrl_shift, XKB_KEY_n);
add_binding(BIND_ACTION_SHOW_URLS_LAUNCH, ctrl_shift, XKB_KEY_u);
#undef add_binding
#undef add_binding
}
static void
add_default_search_bindings(struct config *conf)
{
#define add_binding(action, mods, sym) \
do { \
tll_push_back( \
conf->bindings.search, \
((struct config_key_binding){action, mods, sym})); \
} while (0)
#define add_binding(action, mods, sym) \
add_key_binding(&conf->bindings.search, action, &mods, sym)
const struct config_key_modifiers none = {0};
const struct config_key_modifiers alt = {.alt = true};
@ -2190,18 +2225,14 @@ add_default_search_bindings(struct config *conf)
add_binding(BIND_ACTION_SEARCH_CLIPBOARD_PASTE, ctrl, XKB_KEY_y);
add_binding(BIND_ACTION_SEARCH_PRIMARY_PASTE, shift, XKB_KEY_Insert);
#undef add_binding
#undef add_binding
}
static void
add_default_url_bindings(struct config *conf)
{
#define add_binding(action, mods, sym) \
do { \
tll_push_back( \
conf->bindings.url, \
((struct config_key_binding){action, mods, sym})); \
} while (0)
#define add_binding(action, mods, sym) \
add_key_binding(&conf->bindings.url, action, &mods, sym)
const struct config_key_modifiers none = {0};
const struct config_key_modifiers ctrl = {.ctrl = true};
@ -2211,18 +2242,24 @@ add_default_url_bindings(struct config *conf)
add_binding(BIND_ACTION_URL_CANCEL, none, XKB_KEY_Escape);
add_binding(BIND_ACTION_URL_TOGGLE_URL_ON_JUMP_LABEL, none, XKB_KEY_t);
#undef add_binding
#undef add_binding
}
static void NOINLINE
add_mouse_binding(config_mouse_binding_list_t *list, int action,
const struct config_key_modifiers *mods,
int button, int count)
{
tll_push_back(
*list,
((struct config_mouse_binding){action, *mods, button, count, {0}}));
}
static void
add_default_mouse_bindings(struct config *conf)
{
#define add_binding(action, mods, btn, count) \
do { \
tll_push_back( \
conf->bindings.mouse, \
((struct config_mouse_binding){action, mods, btn, count, {0}})); \
} while (0)
#define add_binding(action, mods, btn, count) \
add_mouse_binding(&conf->bindings.mouse, action, &mods, btn, count)
const struct config_key_modifiers none = {0};
const struct config_key_modifiers ctrl = {.ctrl = true};
@ -2236,7 +2273,7 @@ add_default_mouse_bindings(struct config *conf)
add_binding(BIND_ACTION_SELECT_WORD_WS, ctrl, BTN_LEFT, 2);
add_binding(BIND_ACTION_SELECT_ROW, none, BTN_LEFT, 3);
#undef add_binding
#undef add_binding
}
bool

View file

@ -52,6 +52,7 @@ struct config_mouse_binding {
int count;
struct config_binding_pipe pipe;
};
typedef tll(struct config_mouse_binding) config_mouse_binding_list_t;
struct config_spawn_template {
char *raw_cmd;
@ -166,7 +167,7 @@ struct config {
struct {
/* Bindings for "normal" mode */
config_key_binding_list_t key;
tll(struct config_mouse_binding) mouse;
config_mouse_binding_list_t mouse;
/*
* Special modes

78
log.c
View file

@ -122,43 +122,67 @@ _sys_log(enum log_class log_class, const char *module,
syslog(level, "%s: %s", module, msg);
}
void
log_msg_va(enum log_class log_class, const char *module,
const char *file, int lineno, const char *fmt, va_list va)
{
va_list va2;
va_copy(va2, va);
_log(log_class, module, file, lineno, fmt, 0, va);
_sys_log(log_class, module, file, lineno, fmt, 0, va2);
va_end(va2);
}
void
log_msg(enum log_class log_class, const char *module,
const char *file, int lineno, const char *fmt, ...)
{
va_list ap1, ap2;
va_start(ap1, fmt);
va_copy(ap2, ap1);
_log(log_class, module, file, lineno, fmt, 0, ap1);
_sys_log(log_class, module, file, lineno, fmt, 0, ap2);
va_end(ap1);
va_end(ap2);
va_list va;
va_start(va, fmt);
log_msg_va(log_class, module, file, lineno, fmt, va);
va_end(va);
}
void log_errno(enum log_class log_class, const char *module,
const char *file, int lineno,
const char *fmt, ...)
void
log_errno_va(enum log_class log_class, const char *module,
const char *file, int lineno,
const char *fmt, va_list va)
{
va_list ap1, ap2;
va_start(ap1, fmt);
va_copy(ap2, ap1);
_log(log_class, module, file, lineno, fmt, errno, ap1);
_sys_log(log_class, module, file, lineno, fmt, errno, ap2);
va_end(ap1);
va_end(ap2);
log_errno_provided_va(log_class, module, file, lineno, errno, fmt, va);
}
void log_errno_provided(enum log_class log_class, const char *module,
const char *file, int lineno, int errno_copy,
const char *fmt, ...)
void
log_errno(enum log_class log_class, const char *module,
const char *file, int lineno,
const char *fmt, ...)
{
va_list ap1, ap2;
va_start(ap1, fmt);
va_copy(ap2, ap1);
_log(log_class, module, file, lineno, fmt, errno_copy, ap1);
_sys_log(log_class, module, file, lineno, fmt, errno_copy, ap2);
va_end(ap1);
va_end(ap2);
va_list va;
va_start(va, fmt);
log_errno_va(log_class, module, file, lineno, fmt, va);
va_end(va);
}
void
log_errno_provided_va(enum log_class log_class, const char *module,
const char *file, int lineno, int errno_copy,
const char *fmt, va_list va)
{
va_list va2;
va_copy(va2, va);
_log(log_class, module, file, lineno, fmt, errno_copy, va);
_sys_log(log_class, module, file, lineno, fmt, errno_copy, va2);
va_end(va2);
}
void
log_errno_provided(enum log_class log_class, const char *module,
const char *file, int lineno, int errno_copy,
const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
log_errno_provided_va(log_class, module, file, lineno, errno_copy, fmt, va);
va_end(va);
}
int

28
log.h
View file

@ -1,5 +1,6 @@
#pragma once
#include <stdbool.h>
#include <stdarg.h>
#include "macros.h"
enum log_colorize { LOG_COLORIZE_NEVER, LOG_COLORIZE_ALWAYS, LOG_COLORIZE_AUTO };
@ -10,19 +11,34 @@ void log_init(enum log_colorize colorize, bool do_syslog,
enum log_facility syslog_facility, enum log_class log_level);
void log_deinit(void);
void log_msg(enum log_class log_class, const char *module,
const char *file, int lineno,
const char *fmt, ...) PRINTF(5);
void log_msg(
enum log_class log_class, const char *module,
const char *file, int lineno,
const char *fmt, ...) PRINTF(5);
void log_errno(enum log_class log_class, const char *module,
const char *file, int lineno,
const char *fmt, ...) PRINTF(5);
void log_errno(
enum log_class log_class, const char *module,
const char *file, int lineno,
const char *fmt, ...) PRINTF(5);
void log_errno_provided(
enum log_class log_class, const char *module,
const char *file, int lineno, int _errno,
const char *fmt, ...) PRINTF(6);
void log_msg_va(
enum log_class log_class, const char *module,
const char *file, int lineno, const char *fmt, va_list va) VPRINTF(5);
void log_errno_va(
enum log_class log_class, const char *module,
const char *file, int lineno,
const char *fmt, va_list va) VPRINTF(5);
void log_errno_provided_va(
enum log_class log_class, const char *module,
const char *file, int lineno, int _errno,
const char *fmt, va_list va) VPRINTF(6);
int log_level_from_string(const char *str);
const char *log_level_string_hint(void);

View file

@ -71,7 +71,7 @@ xvasprintf_(char **strp, const char *format, va_list ap)
return vsnprintf(*strp, n + 1, format, ap);
}
static VPRINTF(1) char *
char *
xvasprintf(const char *format, va_list ap)
{
char *str;

View file

@ -1,5 +1,6 @@
#pragma once
#include <stdarg.h>
#include <stddef.h>
#include <wchar.h>
#include "macros.h"
@ -10,4 +11,5 @@ void *xrealloc(void *ptr, size_t size);
char *xstrdup(const char *str) XSTRDUP;
char *xstrndup(const char *str, size_t n) XSTRDUP;
char *xasprintf(const char *format, ...) PRINTF(1) XMALLOC;
char *xvasprintf(const char *format, va_list va) VPRINTF(1) XMALLOC;
wchar_t *xwcsdup(const wchar_t *str) XSTRDUP;