Use format_str() throughout

master
Simon Ser 2 years ago
parent ac8962eb62
commit 08c1946d71

@ -84,18 +84,11 @@ void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width,
int *baseline, double scale, bool markup, const char *fmt, ...) { int *baseline, double scale, bool markup, const char *fmt, ...) {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
// Add one since vsnprintf excludes null terminator. char *buf = vformat_str(fmt, args);
int length = vsnprintf(NULL, 0, fmt, args) + 1;
va_end(args); va_end(args);
char *buf = malloc(length);
if (buf == NULL) { if (buf == NULL) {
sway_log(SWAY_ERROR, "Failed to allocate memory");
return; return;
} }
va_start(args, fmt);
vsnprintf(buf, length, fmt, args);
va_end(args);
PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup); PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup);
pango_cairo_update_layout(cairo, layout); pango_cairo_update_layout(cairo, layout);
@ -104,6 +97,7 @@ void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width,
*baseline = pango_layout_get_baseline(layout) / PANGO_SCALE; *baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
} }
g_object_unref(layout); g_object_unref(layout);
free(buf); free(buf);
} }
@ -125,18 +119,11 @@ void render_text(cairo_t *cairo, const PangoFontDescription *desc,
double scale, bool markup, const char *fmt, ...) { double scale, bool markup, const char *fmt, ...) {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
// Add one since vsnprintf excludes null terminator. char *buf = vformat_str(fmt, args);
int length = vsnprintf(NULL, 0, fmt, args) + 1;
va_end(args); va_end(args);
char *buf = malloc(length);
if (buf == NULL) { if (buf == NULL) {
sway_log(SWAY_ERROR, "Failed to allocate memory");
return; return;
} }
va_start(args, fmt);
vsnprintf(buf, length, fmt, args);
va_end(args);
PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup); PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup);
cairo_font_options_t *fo = cairo_font_options_create(); cairo_font_options_t *fo = cairo_font_options_create();
@ -146,5 +133,6 @@ void render_text(cairo_t *cairo, const PangoFontDescription *desc,
pango_cairo_update_layout(cairo, layout); pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout); pango_cairo_show_layout(cairo, layout);
g_object_unref(layout); g_object_unref(layout);
free(buf); free(buf);
} }

@ -489,20 +489,10 @@ struct cmd_results *cmd_results_new(enum cmd_status status,
} }
results->status = status; results->status = status;
if (format) { if (format) {
char *error = NULL;
va_list args; va_list args;
va_start(args, format); va_start(args, format);
int slen = vsnprintf(NULL, 0, format, args); results->error = vformat_str(format, args);
va_end(args);
if (slen > 0) {
error = malloc(slen + 1);
if (error != NULL) {
va_start(args, format);
vsnprintf(error, slen + 1, format, args);
va_end(args); va_end(args);
}
}
results->error = error;
} else { } else {
results->error = NULL; results->error = NULL;
} }

@ -73,12 +73,10 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
} }
++argv; --argc; ++argv; --argc;
} else if (config->reading && !config->current_bar) { } else if (config->reading && !config->current_bar) {
int len = snprintf(NULL, 0, "bar-%d", config->bars->length) + 1; id = format_str("bar-%d", config->bars->length);
id = malloc(len * sizeof(char));
if (!id) { if (!id) {
return cmd_results_new(CMD_FAILURE, "Unable to allocate bar id"); return cmd_results_new(CMD_FAILURE, "Unable to allocate bar id");
} }
snprintf(id, len, "bar-%d", config->bars->length);
} else if (!config->reading && strcmp(argv[0], "mode") != 0 && } else if (!config->reading && strcmp(argv[0], "mode") != 0 &&
strcmp(argv[0], "hidden_state") != 0) { strcmp(argv[0], "hidden_state") != 0) {
if (is_subcommand(argv[0])) { if (is_subcommand(argv[0])) {

@ -924,23 +924,18 @@ void config_add_swaynag_warning(char *fmt, ...) {
if (config->reading && !config->validating) { if (config->reading && !config->validating) {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
size_t length = vsnprintf(NULL, 0, fmt, args) + 1; char *str = vformat_str(fmt, args);
va_end(args); va_end(args);
if (str == NULL) {
char *temp = malloc(length + 1);
if (!temp) {
sway_log(SWAY_ERROR, "Failed to allocate buffer for warning.");
return; return;
} }
va_start(args, fmt);
vsnprintf(temp, length, fmt, args);
va_end(args);
swaynag_log(config->swaynag_command, &config->swaynag_config_errors, swaynag_log(config->swaynag_command, &config->swaynag_config_errors,
"Warning on line %i (%s) '%s': %s", "Warning on line %i (%s) '%s': %s",
config->current_config_line_number, config->current_config_path, config->current_config_line_number, config->current_config_path,
config->current_config_line, temp); config->current_config_line, str);
free(str);
} }
} }

@ -162,13 +162,10 @@ static void merge_id_on_name(struct output_config *oc) {
char id[128]; char id[128];
output_get_identifier(id, sizeof(id), output); output_get_identifier(id, sizeof(id), output);
size_t size = snprintf(NULL, 0, "%s on %s", id, name) + 1; char *id_on_name = format_str("%s on %s", id, name);
char *id_on_name = malloc(size);
if (!id_on_name) { if (!id_on_name) {
sway_log(SWAY_ERROR, "Failed to allocate id on name string");
return; return;
} }
snprintf(id_on_name, size, "%s on %s", id, name);
int i = list_seq_find(config->output_configs, output_name_cmp, id_on_name); int i = list_seq_find(config->output_configs, output_name_cmp, id_on_name);
if (i >= 0) { if (i >= 0) {
@ -633,9 +630,7 @@ static struct output_config *get_output_config(char *identifier,
struct output_config *oc_name = NULL; struct output_config *oc_name = NULL;
struct output_config *oc_id = NULL; struct output_config *oc_id = NULL;
size_t length = snprintf(NULL, 0, "%s on %s", identifier, name) + 1; char *id_on_name = format_str("%s on %s", identifier, name);
char *id_on_name = malloc(length);
snprintf(id_on_name, length, "%s on %s", identifier, name);
int i = list_seq_find(config->output_configs, output_name_cmp, id_on_name); int i = list_seq_find(config->output_configs, output_name_cmp, id_on_name);
if (i >= 0) { if (i >= 0) {
oc_id_on_name = config->output_configs->items[i]; oc_id_on_name = config->output_configs->items[i];

@ -1273,11 +1273,7 @@ uint32_t get_mouse_bindsym(const char *name, char **error) {
// Get event code from name // Get event code from name
int code = libevdev_event_code_from_name(EV_KEY, name); int code = libevdev_event_code_from_name(EV_KEY, name);
if (code == -1) { if (code == -1) {
size_t len = snprintf(NULL, 0, "Unknown event %s", name) + 1; *error = format_str("Unknown event %s", name);
*error = malloc(len);
if (*error) {
snprintf(*error, len, "Unknown event %s", name);
}
return 0; return 0;
} }
return code; return code;
@ -1299,13 +1295,8 @@ uint32_t get_mouse_bindcode(const char *name, char **error) {
} }
const char *event = libevdev_event_code_get_name(EV_KEY, code); const char *event = libevdev_event_code_get_name(EV_KEY, code);
if (!event || strncmp(event, "BTN_", strlen("BTN_")) != 0) { if (!event || strncmp(event, "BTN_", strlen("BTN_")) != 0) {
size_t len = snprintf(NULL, 0, "Event code %d (%s) is not a button", *error = format_str("Event code %d (%s) is not a button",
code, event ? event : "(null)") + 1;
*error = malloc(len);
if (*error) {
snprintf(*error, len, "Event code %d (%s) is not a button",
code, event ? event : "(null)"); code, event ? event : "(null)");
}
return 0; return 0;
} }
return code; return code;

@ -80,15 +80,7 @@ char *input_device_get_identifier(struct wlr_input_device *device) {
} }
} }
const char *fmt = "%d:%d:%s"; char *identifier = format_str("%d:%d:%s", vendor, product, name);
int len = snprintf(NULL, 0, fmt, vendor, product, name) + 1;
char *identifier = malloc(len);
if (!identifier) {
sway_log(SWAY_ERROR, "Unable to allocate unique input device name");
return NULL;
}
snprintf(identifier, len, fmt, vendor, product, name);
free(name); free(name);
return identifier; return identifier;
} }

@ -717,23 +717,11 @@ struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat,
static void handle_xkb_context_log(struct xkb_context *context, static void handle_xkb_context_log(struct xkb_context *context,
enum xkb_log_level level, const char *format, va_list args) { enum xkb_log_level level, const char *format, va_list args) {
va_list args_copy; char *error = vformat_str(format, args);
va_copy(args_copy, args);
size_t length = vsnprintf(NULL, 0, format, args_copy) + 1;
va_end(args_copy);
char *error = malloc(length);
if (!error) {
sway_log(SWAY_ERROR, "Failed to allocate libxkbcommon log message");
return;
}
va_copy(args_copy, args); size_t len = strlen(error);
vsnprintf(error, length, format, args_copy); if (error[len - 1] == '\n') {
va_end(args_copy); error[len - 1] = '\0';
if (error[length - 2] == '\n') {
error[length - 2] = '\0';
} }
sway_log_importance_t importance = SWAY_DEBUG; sway_log_importance_t importance = SWAY_DEBUG;
@ -768,14 +756,9 @@ struct xkb_keymap *sway_keyboard_compile_keymap(struct input_config *ic,
if (!keymap_file) { if (!keymap_file) {
sway_log_errno(SWAY_ERROR, "cannot read xkb file %s", ic->xkb_file); sway_log_errno(SWAY_ERROR, "cannot read xkb file %s", ic->xkb_file);
if (error) { if (error) {
size_t len = snprintf(NULL, 0, "cannot read xkb file %s: %s", *error = format_str("cannot read xkb file %s: %s",
ic->xkb_file, strerror(errno)) + 1;
*error = malloc(len);
if (*error) {
snprintf(*error, len, "cannot read xkb_file %s: %s",
ic->xkb_file, strerror(errno)); ic->xkb_file, strerror(errno));
} }
}
goto cleanup; goto cleanup;
} }

@ -145,22 +145,16 @@ void swaynag_log(const char *swaynag_command, struct swaynag_instance *swaynag,
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
size_t length = vsnprintf(NULL, 0, fmt, args) + 1; char *str = vformat_str(fmt, args);
va_end(args); va_end(args);
if (!str) {
char *temp = malloc(length + 1);
if (!temp) {
sway_log(SWAY_ERROR, "Failed to allocate buffer for swaynag log entry."); sway_log(SWAY_ERROR, "Failed to allocate buffer for swaynag log entry.");
return; return;
} }
va_start(args, fmt); write(swaynag->fd[1], str, strlen(str));
vsnprintf(temp, length, fmt, args);
va_end(args);
write(swaynag->fd[1], temp, length);
free(temp); free(str);
} }
void swaynag_show(struct swaynag_instance *swaynag) { void swaynag_show(struct swaynag_instance *swaynag) {

@ -10,6 +10,7 @@
#include "swaybar/tray/tray.h" #include "swaybar/tray/tray.h"
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
#include "stringop.h"
static const char *watcher_path = "/StatusNotifierWatcher"; static const char *watcher_path = "/StatusNotifierWatcher";
@ -138,12 +139,10 @@ static int handle_new_watcher(sd_bus_message *msg,
bool init_host(struct swaybar_host *host, char *protocol, bool init_host(struct swaybar_host *host, char *protocol,
struct swaybar_tray *tray) { struct swaybar_tray *tray) {
size_t len = snprintf(NULL, 0, "org.%s.StatusNotifierWatcher", protocol) + 1; host->watcher_interface = format_str("org.%s.StatusNotifierWatcher", protocol);
host->watcher_interface = malloc(len);
if (!host->watcher_interface) { if (!host->watcher_interface) {
return false; return false;
} }
snprintf(host->watcher_interface, len, "org.%s.StatusNotifierWatcher", protocol);
sd_bus_slot *reg_slot = NULL, *unreg_slot = NULL, *watcher_slot = NULL; sd_bus_slot *reg_slot = NULL, *unreg_slot = NULL, *watcher_slot = NULL;
int ret = sd_bus_match_signal(tray->bus, &reg_slot, host->watcher_interface, int ret = sd_bus_match_signal(tray->bus, &reg_slot, host->watcher_interface,
@ -173,13 +172,10 @@ bool init_host(struct swaybar_host *host, char *protocol,
} }
pid_t pid = getpid(); pid_t pid = getpid();
size_t service_len = snprintf(NULL, 0, "org.%s.StatusNotifierHost-%d", host->service = format_str("org.%s.StatusNotifierHost-%d", protocol, pid);
protocol, pid) + 1;
host->service = malloc(service_len);
if (!host->service) { if (!host->service) {
goto error; goto error;
} }
snprintf(host->service, service_len, "org.%s.StatusNotifierHost-%d", protocol, pid);
ret = sd_bus_request_name(tray->bus, host->service, 0); ret = sd_bus_request_name(tray->bus, host->service, 0);
if (ret < 0) { if (ret < 0) {
sway_log(SWAY_DEBUG, "Failed to acquire service name: %s", strerror(-ret)); sway_log(SWAY_DEBUG, "Failed to acquire service name: %s", strerror(-ret));

@ -40,9 +40,7 @@ static list_t *get_basedirs(void) {
data_dirs = strdup(data_dirs); data_dirs = strdup(data_dirs);
char *dir = strtok(data_dirs, ":"); char *dir = strtok(data_dirs, ":");
do { do {
size_t path_len = snprintf(NULL, 0, "%s/icons", dir) + 1; char *path = format_str("%s/icons", dir);
char *path = malloc(path_len);
snprintf(path, path_len, "%s/icons", dir);
list_add(basedirs, path); list_add(basedirs, path);
} while ((dir = strtok(NULL, ":"))); } while ((dir = strtok(NULL, ":")));
free(data_dirs); free(data_dirs);
@ -206,13 +204,7 @@ static const char *entry_handler(char *group, char *key, char *value,
*/ */
static struct icon_theme *read_theme_file(char *basedir, char *theme_name) { static struct icon_theme *read_theme_file(char *basedir, char *theme_name) {
// look for index.theme file // look for index.theme file
size_t path_len = snprintf(NULL, 0, "%s/%s/index.theme", basedir, char *path = format_str("%s/%s/index.theme", basedir, theme_name);
theme_name) + 1;
char *path = malloc(path_len);
if (!path) {
return NULL;
}
snprintf(path, path_len, "%s/%s/index.theme", basedir, theme_name);
FILE *theme_file = fopen(path, "r"); FILE *theme_file = fopen(path, "r");
free(path); free(path);
if (!theme_file) { if (!theme_file) {
@ -416,26 +408,20 @@ static char *find_icon_in_subdir(char *name, char *basedir, char *theme,
#endif #endif
}; };
size_t path_len = snprintf(NULL, 0, "%s/%s/%s/%s.EXT", basedir, theme,
subdir, name) + 1;
char *path = malloc(path_len);
for (size_t i = 0; i < sizeof(extensions) / sizeof(*extensions); ++i) { for (size_t i = 0; i < sizeof(extensions) / sizeof(*extensions); ++i) {
snprintf(path, path_len, "%s/%s/%s/%s.%s", basedir, theme, subdir, char *path = format_str("%s/%s/%s/%s.%s",
name, extensions[i]); basedir, theme, subdir, name, extensions[i]);
if (access(path, R_OK) == 0) { if (access(path, R_OK) == 0) {
return path; return path;
} }
free(path);
} }
free(path);
return NULL; return NULL;
} }
static bool theme_exists_in_basedir(char *theme, char *basedir) { static bool theme_exists_in_basedir(char *theme, char *basedir) {
size_t path_len = snprintf(NULL, 0, "%s/%s", basedir, theme) + 1; char *path = format_str("%s/%s", basedir, theme);
char *path = malloc(path_len);
snprintf(path, path_len, "%s/%s", basedir, theme);
bool ret = dir_exists(path); bool ret = dir_exists(path);
free(path); free(path);
return ret; return ret;

@ -6,6 +6,7 @@
#include <string.h> #include <string.h>
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
#include "stringop.h"
#include "swaybar/tray/watcher.h" #include "swaybar/tray/watcher.h"
static const char *obj_path = "/StatusNotifierWatcher"; static const char *obj_path = "/StatusNotifierWatcher";
@ -76,9 +77,7 @@ static int register_sni(sd_bus_message *msg, void *data, sd_bus_error *error) {
service = service_or_path; service = service_or_path;
path = "/StatusNotifierItem"; path = "/StatusNotifierItem";
} }
size_t id_len = snprintf(NULL, 0, "%s%s", service, path) + 1; id = format_str("%s%s", service, path);
id = malloc(id_len);
snprintf(id, id_len, "%s%s", service, path);
} }
if (list_seq_find(watcher->items, cmp_id, id) == -1) { if (list_seq_find(watcher->items, cmp_id, id) == -1) {
@ -159,9 +158,7 @@ struct swaybar_watcher *create_watcher(char *protocol, sd_bus *bus) {
return NULL; return NULL;
} }
size_t len = snprintf(NULL, 0, "org.%s.StatusNotifierWatcher", protocol) + 1; watcher->interface = format_str("org.%s.StatusNotifierWatcher", protocol);
watcher->interface = malloc(len);
snprintf(watcher->interface, len, "org.%s.StatusNotifierWatcher", protocol);
sd_bus_slot *signal_slot = NULL, *vtable_slot = NULL; sd_bus_slot *signal_slot = NULL, *vtable_slot = NULL;
int ret = sd_bus_add_object_vtable(bus, &vtable_slot, obj_path, int ret = sd_bus_add_object_vtable(bus, &vtable_slot, obj_path,

Loading…
Cancel
Save