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

157
config.c
View file

@ -91,43 +91,76 @@ static const char *const binding_action_map[] = {
static_assert(ALEN(binding_action_map) == BIND_ACTION_COUNT, static_assert(ALEN(binding_action_map) == BIND_ACTION_COUNT,
"binding action map size mismatch"); "binding action map size mismatch");
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;
switch (log_class) {
case LOG_CLASS_WARNING: kind = USER_NOTIFICATION_WARNING; break;
case LOG_CLASS_ERROR: kind = USER_NOTIFICATION_ERROR; break;
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(...) \ #define LOG_AND_NOTIFY_ERR(...) \
do { \ log_and_notify(conf, LOG_CLASS_ERROR, __FILE__, __LINE__, __VA_ARGS__)
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)
#define LOG_AND_NOTIFY_WARN(...) \ #define LOG_AND_NOTIFY_WARN(...) \
do { \ log_and_notify(conf, LOG_CLASS_WARNING, __FILE__, __LINE__, __VA_ARGS__)
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)
#define LOG_AND_NOTIFY_ERRNO(...) \ #define LOG_AND_NOTIFY_ERRNO(...) \
do { \ log_errno_and_notify(conf, LOG_CLASS_ERROR, __FILE__, __LINE__, __VA_ARGS__)
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)
static char * static char *
get_shell(void) get_shell(void)
@ -329,7 +362,7 @@ done:
goto out; goto out;
} }
static bool static bool NOINLINE
str_to_bool(const char *s) str_to_bool(const char *s)
{ {
return strcasecmp(s, "on") == 0 || return strcasecmp(s, "on") == 0 ||
@ -338,7 +371,7 @@ str_to_bool(const char *s)
strtoul(s, NULL, 0) > 0; strtoul(s, NULL, 0) > 0;
} }
static bool static bool NOINLINE
str_to_ulong(const char *s, int base, unsigned long *res) str_to_ulong(const char *s, int base, unsigned long *res)
{ {
if (s == NULL) if (s == NULL)
@ -351,7 +384,7 @@ str_to_ulong(const char *s, int base, unsigned long *res)
return errno == 0 && *end == '\0'; return errno == 0 && *end == '\0';
} }
static bool static bool NOINLINE
str_to_double(const char *s, double *res) str_to_double(const char *s, double *res)
{ {
if (s == NULL) if (s == NULL)
@ -364,7 +397,7 @@ str_to_double(const char *s, double *res)
return errno == 0 && *end == '\0'; return errno == 0 && *end == '\0';
} }
static bool static bool NOINLINE
str_to_wchars(const char *s, wchar_t **res, struct config *conf, str_to_wchars(const char *s, wchar_t **res, struct config *conf,
const char *path, int lineno, const char *path, int lineno,
const char *section, const char *key) const char *section, const char *key)
@ -383,7 +416,7 @@ str_to_wchars(const char *s, wchar_t **res, struct config *conf,
return true; return true;
} }
static bool static bool NOINLINE
str_to_color(const char *s, uint32_t *color, bool allow_alpha, str_to_color(const char *s, uint32_t *color, bool allow_alpha,
struct config *conf, const char *path, int lineno, struct config *conf, const char *path, int lineno,
const char *section, const char *key) const char *section, const char *key)
@ -406,7 +439,7 @@ str_to_color(const char *s, uint32_t *color, bool allow_alpha,
return true; return true;
} }
static bool static bool NOINLINE
str_to_two_colors(const char *s, uint32_t *first, uint32_t *second, str_to_two_colors(const char *s, uint32_t *first, uint32_t *second,
bool allow_alpha, struct config *conf, const char *path, bool allow_alpha, struct config *conf, const char *path,
int lineno, const char *section, const char *key) 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; return true;
} }
static bool static bool NOINLINE
str_to_pt_or_px(const char *s, struct pt_or_px *res, struct config *conf, 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) 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; return true;
} }
static bool static bool NOINLINE
str_to_spawn_template(struct config *conf, str_to_spawn_template(struct config *conf,
const char *s, struct config_spawn_template *template, const char *s, struct config_spawn_template *template,
const char *path, int lineno, const char *section, 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); 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 static void
add_default_key_bindings(struct config *conf) add_default_key_bindings(struct config *conf)
{ {
#define add_binding(action, mods, sym) \ #define add_binding(action, mods, sym) \
do { \ add_key_binding(&conf->bindings.key, action, &mods, sym)
tll_push_back( \
conf->bindings.key, \
((struct config_key_binding){action, mods, sym})); \
} while (0)
const struct config_key_modifiers shift = {.shift = true}; const struct config_key_modifiers shift = {.shift = true};
const struct config_key_modifiers ctrl = {.ctrl = true}; const struct config_key_modifiers ctrl = {.ctrl = true};
@ -2145,15 +2183,12 @@ add_default_key_bindings(struct config *conf)
#undef add_binding #undef add_binding
} }
static void static void
add_default_search_bindings(struct config *conf) add_default_search_bindings(struct config *conf)
{ {
#define add_binding(action, mods, sym) \ #define add_binding(action, mods, sym) \
do { \ add_key_binding(&conf->bindings.search, action, &mods, sym)
tll_push_back( \
conf->bindings.search, \
((struct config_key_binding){action, mods, sym})); \
} while (0)
const struct config_key_modifiers none = {0}; const struct config_key_modifiers none = {0};
const struct config_key_modifiers alt = {.alt = true}; const struct config_key_modifiers alt = {.alt = true};
@ -2197,11 +2232,7 @@ static void
add_default_url_bindings(struct config *conf) add_default_url_bindings(struct config *conf)
{ {
#define add_binding(action, mods, sym) \ #define add_binding(action, mods, sym) \
do { \ add_key_binding(&conf->bindings.url, action, &mods, sym)
tll_push_back( \
conf->bindings.url, \
((struct config_key_binding){action, mods, sym})); \
} while (0)
const struct config_key_modifiers none = {0}; const struct config_key_modifiers none = {0};
const struct config_key_modifiers ctrl = {.ctrl = true}; const struct config_key_modifiers ctrl = {.ctrl = true};
@ -2214,15 +2245,21 @@ add_default_url_bindings(struct config *conf)
#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 static void
add_default_mouse_bindings(struct config *conf) add_default_mouse_bindings(struct config *conf)
{ {
#define add_binding(action, mods, btn, count) \ #define add_binding(action, mods, btn, count) \
do { \ add_mouse_binding(&conf->bindings.mouse, action, &mods, btn, count)
tll_push_back( \
conf->bindings.mouse, \
((struct config_mouse_binding){action, mods, btn, count, {0}})); \
} while (0)
const struct config_key_modifiers none = {0}; const struct config_key_modifiers none = {0};
const struct config_key_modifiers ctrl = {.ctrl = true}; const struct config_key_modifiers ctrl = {.ctrl = true};

View file

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

70
log.c
View file

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

20
log.h
View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <stdbool.h> #include <stdbool.h>
#include <stdarg.h>
#include "macros.h" #include "macros.h"
enum log_colorize { LOG_COLORIZE_NEVER, LOG_COLORIZE_ALWAYS, LOG_COLORIZE_AUTO }; enum log_colorize { LOG_COLORIZE_NEVER, LOG_COLORIZE_ALWAYS, LOG_COLORIZE_AUTO };
@ -10,11 +11,13 @@ void log_init(enum log_colorize colorize, bool do_syslog,
enum log_facility syslog_facility, enum log_class log_level); enum log_facility syslog_facility, enum log_class log_level);
void log_deinit(void); void log_deinit(void);
void log_msg(enum log_class log_class, const char *module, void log_msg(
enum log_class log_class, const char *module,
const char *file, int lineno, const char *file, int lineno,
const char *fmt, ...) PRINTF(5); const char *fmt, ...) PRINTF(5);
void log_errno(enum log_class log_class, const char *module, void log_errno(
enum log_class log_class, const char *module,
const char *file, int lineno, const char *file, int lineno,
const char *fmt, ...) PRINTF(5); const char *fmt, ...) PRINTF(5);
@ -23,6 +26,19 @@ void log_errno_provided(
const char *file, int lineno, int _errno, const char *file, int lineno, int _errno,
const char *fmt, ...) PRINTF(6); 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); int log_level_from_string(const char *str);
const char *log_level_string_hint(void); 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); return vsnprintf(*strp, n + 1, format, ap);
} }
static VPRINTF(1) char * char *
xvasprintf(const char *format, va_list ap) xvasprintf(const char *format, va_list ap)
{ {
char *str; char *str;

View file

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