#include <X11/Xlib.h>
+#include "libi3.h"
+
extern Display *dpy;
struct context {
char *str = XKeysymToString(sym);
char *modifiers = modifier_to_string($<number>3);
// TODO: modifier to string
- asprintf(&(context->result), "bindsym %s%s %s\n", modifiers, str, $<string>6);
+ sasprintf(&(context->result), "bindsym %s%s %s\n", modifiers, str, $<string>6);
free(modifiers);
}
;
/* This option is deprecated, but will still work in i3 v4.1, 4.2 and 4.3 */
fprintf(stderr, "i3-input: WARNING: the -p option is DEPRECATED in favor of the -F (format) option\n");
FREE(format);
- asprintf(&format, "%s%%s", optarg);
+ sasprintf(&format, "%s%%s", optarg);
break;
case 'l':
limit = atoi(optarg);
echo "[i3bar] LINK"
$(CC) $(LDFLAGS) -o $@ $(filter-out libi3/libi3.a,$^) $(LIBS)
-$(TOPDIR)/libi3/%.a:
+$(TOPDIR)/libi3/%.a: $(TOPDIR)/libi3/*.c
$(MAKE) -C $(TOPDIR)/libi3
doc:
* Therefore we save the command in 'config' and access it later in
* got_bar_config() */
DLOG("command = %.*s\n", len, val);
- asprintf(&config.command, "%.*s", len, val);
+ sasprintf(&config.command, "%.*s", len, val);
return 1;
}
if (!strcmp(cur_key, "font")) {
DLOG("font = %.*s\n", len, val);
- asprintf(&config.fontname, "%.*s", len, val);
+ sasprintf(&config.fontname, "%.*s", len, val);
return 1;
}
DLOG("+output %.*s\n", len, val);
int new_num_outputs = config.num_outputs + 1;
config.outputs = srealloc(config.outputs, sizeof(char*) * new_num_outputs);
- asprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
+ sasprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
config.num_outputs = new_num_outputs;
return 1;
}
if (!strcmp(cur_key, "tray_output")) {
DLOG("tray_output %.*s\n", len, val);
FREE(config.tray_output);
- asprintf(&config.tray_output, "%.*s", len, val);
+ sasprintf(&config.tray_output, "%.*s", len, val);
return 1;
}
do { \
if (!strcmp(cur_key, #json_name)) { \
DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
- asprintf(&(config.colors.struct_name), "%.*s", len, val); \
+ sasprintf(&(config.colors.struct_name), "%.*s", len, val); \
return 1; \
} \
} while (0)
#ifndef _LIBI3_H
#define _LIBI3_H
+#include <stdarg.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
*/
char *sstrdup(const char *str);
+/**
+ * Safe-wrapper around asprintf which exits if it returns -1 (meaning that
+ * there is no more memory available)
+ *
+ */
+int sasprintf(char **strp, const char *fmt, ...);
+
/**
* Formats a message (payload) of the given size and type and sends it to i3 via
* the given socket file descriptor.
*/
#include <string.h>
#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
#include <err.h>
err(EXIT_FAILURE, "strdup()");
return result;
}
+
+int sasprintf(char **strp, const char *fmt, ...) {
+ va_list args;
+ int result;
+
+ va_start(args, fmt);
+ if ((result = vasprintf(strp, fmt, args)) == -1)
+ err(EXIT_FAILURE, "asprintf(%s)", fmt);
+ va_end(args);
+ return result;
+}
if (current->type == A_COMMAND) {
DLOG("execute command %s\n", current->dest.command);
char *full_command;
- asprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command);
+ sasprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command);
char *json_result = parse_cmd(full_command);
FREE(full_command);
FREE(json_result);
if (configerror_pid == 0) {
char *editaction,
*pageraction;
- if (asprintf(&editaction, "i3-sensible-terminal -e sh -c \"i3-sensible-editor \\\"%s\\\" && i3-msg reload\"", config_path) == -1)
- exit(1);
- if (asprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename) == -1)
- exit(1);
+ sasprintf(&editaction, "i3-sensible-terminal -e sh -c \"i3-sensible-editor \\\"%s\\\" && i3-msg reload\"", config_path);
+ sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
char *argv[] = {
NULL, /* will be replaced by the executable path */
"-t",
WORD
| NUMBER
{
- asprintf(&$$, "%d", $1);
+ sasprintf(&$$, "%d", $1);
}
;
} else {
char *ws_name = NULL;
if ($5 == NULL) {
- asprintf(&ws_name, "%d", ws_num);
+ sasprintf(&ws_name, "%d", ws_num);
} else {
ws_name = $5;
}
if ((separator = strchr(criteria, '/')) != NULL) {
*(separator++) = '\0';
char *pattern;
- if (asprintf(&pattern, "(?i)%s", separator) == -1) {
- ELOG("asprintf failed\n");
- break;
- }
+ sasprintf(&pattern, "(?i)%s", separator);
match->title = regex_new(pattern);
free(pattern);
printf(" title = %s\n", separator);
}
if (*criteria != '\0') {
char *pattern;
- if (asprintf(&pattern, "(?i)%s", criteria) == -1) {
- ELOG("asprintf failed\n");
- break;
- }
+ sasprintf(&pattern, "(?i)%s", criteria);
match->class = regex_new(pattern);
free(pattern);
printf(" class = %s\n", criteria);
context->filename = "cmd";
if (cmdyyparse() != 0) {
fprintf(stderr, "Could not parse command\n");
- asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
- context->compact_error, context->first_column);
+ sasprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
+ context->compact_error, context->first_column);
FREE(context->line_copy);
FREE(context->compact_error);
free(context);
ELOG("You have to specify which window/container should be focused.\n");
ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
- asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
- "specify which window/container should be focused\"}");
+ sasprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
+ "specify which window/container should be focused\"}");
break;
}
printf("opening new container\n");
Con *con = tree_open_con(NULL, NULL);
con_focus(con);
- asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
+ sasprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
tree_render();
}
xdg_config_home = "~/.config";
xdg_config_home = resolve_tilde(xdg_config_home);
- if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
- die("asprintf() failed");
+ sasprintf(&config_path, "%s/i3/config", xdg_config_home);
free(xdg_config_home);
if (path_exists(config_path))
char *tok = strtok(buf, ":");
while (tok != NULL) {
tok = resolve_tilde(tok);
- if (asprintf(&config_path, "%s/i3/config", tok) == -1)
- die("asprintf() failed");
+ sasprintf(&config_path, "%s/i3/config", tok);
free(tok);
if (path_exists(config_path)) {
free(buf);
}
char *name;
- asprintf(&name, "[i3 con] floatingcon around %p", con);
+ sasprintf(&name, "[i3 con] floatingcon around %p", con);
x_set_name(nc, name);
free(name);
LOG("sticky_group of this container is %s\n", json_node->sticky_group);
} else if (strcasecmp(last_key, "orientation") == 0) {
char *buf = NULL;
- asprintf(&buf, "%.*s", (int)len, val);
+ sasprintf(&buf, "%.*s", (int)len, val);
if (strcasecmp(buf, "none") == 0)
json_node->orientation = NO_ORIENTATION;
else if (strcasecmp(buf, "horizontal") == 0)
free(buf);
} else if (strcasecmp(last_key, "border") == 0) {
char *buf = NULL;
- asprintf(&buf, "%.*s", (int)len, val);
+ sasprintf(&buf, "%.*s", (int)len, val);
if (strcasecmp(buf, "none") == 0)
json_node->border_style = BS_NONE;
else if (strcasecmp(buf, "1pixel") == 0)
free(buf);
} else if (strcasecmp(last_key, "layout") == 0) {
char *buf = NULL;
- asprintf(&buf, "%.*s", (int)len, val);
+ sasprintf(&buf, "%.*s", (int)len, val);
if (strcasecmp(buf, "default") == 0)
json_node->layout = L_DEFAULT;
else if (strcasecmp(buf, "stacked") == 0)
free(buf);
} else if (strcasecmp(last_key, "mark") == 0) {
char *buf = NULL;
- asprintf(&buf, "%.*s", (int)len, val);
+ sasprintf(&buf, "%.*s", (int)len, val);
json_node->mark = buf;
}
}
payload = sstrdup(argv[optind]);
} else {
char *both;
- if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
- err(EXIT_FAILURE, "asprintf");
+ sasprintf(&both, "%s %s", payload, argv[optind]);
free(payload);
payload = both;
}
Barconfig *barconfig;
TAILQ_FOREACH(barconfig, &barconfigs, configs) {
char *command = NULL;
- asprintf(&command, "i3bar --bar_id=%s --socket=\"%s\"",
- barconfig->id, current_socketpath);
+ sasprintf(&command, "i3bar --bar_id=%s --socket=\"%s\"",
+ barconfig->id, current_socketpath);
LOG("Starting bar process: %s\n", command);
start_application(command);
free(command);
nc->border_width = geom->border_width;
char *name;
- asprintf(&name, "[i3 con] container around %p", cwindow);
+ sasprintf(&name, "[i3 con] container around %p", cwindow);
x_set_name(nc, name);
free(name);
output->con = con;
char *name;
- asprintf(&name, "[i3 con] output %s", con->name);
+ sasprintf(&name, "[i3 con] output %s", con->name);
x_set_name(con, name);
FREE(name);
FREE(topdock->name);
topdock->name = sstrdup("topdock");
- asprintf(&name, "[i3 con] top dockarea %s", con->name);
+ sasprintf(&name, "[i3 con] top dockarea %s", con->name);
x_set_name(topdock, name);
FREE(name);
DLOG("attaching\n");
FREE(content->name);
content->name = sstrdup("content");
- asprintf(&name, "[i3 con] content %s", con->name);
+ sasprintf(&name, "[i3 con] content %s", con->name);
x_set_name(content, name);
FREE(name);
con_attach(content, con, false);
FREE(bottomdock->name);
bottomdock->name = sstrdup("bottomdock");
- asprintf(&name, "[i3 con] bottom dockarea %s", con->name);
+ sasprintf(&name, "[i3 con] bottom dockarea %s", con->name);
x_set_name(bottomdock, name);
FREE(name);
DLOG("attaching\n");
c++;
FREE(ws->name);
- asprintf(&(ws->name), "%d", c);
+ sasprintf(&(ws->name), "%d", c);
current = NULL;
TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
}
con_attach(ws, content, false);
- asprintf(&name, "[i3 con] workspace %s", ws->name);
+ sasprintf(&name, "[i3 con] workspace %s", ws->name);
x_set_name(ws, name);
free(name);
new->id = id;
new->primary = (primary && primary->output == id);
FREE(new->name);
- asprintf(&new->name, "%.*s",
+ sasprintf(&new->name, "%.*s",
xcb_randr_get_output_info_name_length(output),
xcb_randr_get_output_info_name(output));
* argv[0]’s dirname */
char *pathbuf = strdup(start_argv[0]);
char *dir = dirname(pathbuf);
- asprintf(&migratepath, "%s/%s", dir, name);
+ sasprintf(&migratepath, "%s/%s", dir, name);
argv[0] = migratepath;
execvp(migratepath, argv);
exit(1);
}
dir = dirname(buffer);
- asprintf(&migratepath, "%s/%s", dir, name);
+ sasprintf(&migratepath, "%s/%s", dir, name);
argv[0] = migratepath;
execvp(migratepath, argv);
#endif
if (dir == NULL) {
struct passwd *pw = getpwuid(getuid());
const char *username = pw ? pw->pw_name : "unknown";
- if (asprintf(&dir, "/tmp/i3-%s", username) == -1) {
- perror("asprintf()");
- return NULL;
- }
+ sasprintf(&dir, "/tmp/i3-%s", username);
} else {
char *tmp;
- if (asprintf(&tmp, "%s/i3", dir) == -1) {
- perror("asprintf()");
- return NULL;
- }
+ sasprintf(&tmp, "%s/i3", dir);
dir = tmp;
}
if (!path_exists(dir)) {
}
}
char *filename;
- if (asprintf(&filename, "%s/%s.%d", dir, prefix, getpid()) == -1) {
- perror("asprintf()");
- filename = NULL;
- }
-
+ sasprintf(&filename, "%s/%s.%d", dir, prefix, getpid());
free(dir);
return filename;
}
* will handle CT_WORKSPACEs differently */
workspace = con_new(NULL, NULL);
char *name;
- asprintf(&name, "[i3 con] workspace %s", num);
+ sasprintf(&name, "[i3 con] workspace %s", num);
x_set_name(workspace, name);
free(name);
workspace->type = CT_WORKSPACE;
s->rect.height = min(s->rect.height, screen_info[screen].height);
} else {
s = scalloc(sizeof(Output));
- asprintf(&(s->name), "xinerama-%d", num_screens);
+ sasprintf(&(s->name), "xinerama-%d", num_screens);
DLOG("Created new Xinerama screen %s (%p)\n", s->name, s);
s->active = true;
s->rect.x = screen_info[screen].x_org;