swaybar: fix workspace command

Escape quotes and backslashes, allowing switching to workspace names
like "1" (including quotes) and \
This commit is contained in:
Ian Fan 2019-01-22 11:43:37 +00:00 committed by emersion
parent 04aa41de34
commit d8f3e6e19a

View file

@ -13,10 +13,27 @@
#include "util.h" #include "util.h"
void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { void ipc_send_workspace_command(struct swaybar *bar, const char *ws) {
const char *fmt = "workspace \"%s\""; uint32_t size = strlen("workspace \"\"") + strlen(ws);
uint32_t size = snprintf(NULL, 0, fmt, ws); for (size_t i = 0; i < strlen(ws); ++i) {
char *command = malloc(sizeof(char) * (size + 1)); if (ws[i] == '"' || ws[i] == '\\') {
snprintf(command, size, fmt, ws); ++size;
}
}
char *command = malloc(size) + 1;
if (!command) {
return;
}
strcpy(command, "workspace \"");
strcpy(&command[size - 1], "\"");
for (size_t i = 0, d = strlen("workspace \""); i < strlen(ws); ++i) {
if (ws[i] == '"' || ws[i] == '\\') {
command[d++] = '\\';
}
command[d++] = ws[i];
}
ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, command, &size); ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, command, &size);
free(command); free(command);
} }