mirror of
https://github.com/swaywm/sway.git
synced 2024-11-25 09:21:28 +00:00
Flesh out some command parsing
This implements the `set` command from i3
This commit is contained in:
parent
e07c77fbb7
commit
d0f1fb71d1
|
@ -1,3 +1,4 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
@ -5,16 +6,36 @@
|
|||
#include "commands.h"
|
||||
|
||||
int cmd_set(struct sway_config *config, int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Invalid set command (expected 2 arguments, got %d)\n", argc);
|
||||
return 1;
|
||||
}
|
||||
struct sway_variable *var = malloc(sizeof(struct sway_variable));
|
||||
var->name = malloc(strlen(argv[0]) + 1);
|
||||
strcpy(var->name, argv[0]);
|
||||
var->value = malloc(strlen(argv[1]) + 1);
|
||||
strcpy(var->value, argv[1]);
|
||||
list_add(config->symbols, var);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cmd_bindsym(struct sway_config *config, int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Invalid bindsym command (expected 2 arguments, got %d)\n", argc);
|
||||
return 1;
|
||||
}
|
||||
// TODO: parse out keybindings
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Keep alphabetized */
|
||||
struct cmd_handler handlers[] = {
|
||||
{ "bindsym", cmd_bindsym }
|
||||
{ "set", cmd_set }
|
||||
};
|
||||
|
||||
char **split_directive(char *line, int *argc) {
|
||||
const char *delimiters = ",";
|
||||
const char *delimiters = " ";
|
||||
*argc = 0;
|
||||
while (isspace(*line) && *line) ++line;
|
||||
|
||||
|
@ -94,5 +115,11 @@ int handle_command(struct sway_config *config, char *exec) {
|
|||
}
|
||||
int argc;
|
||||
char **argv = split_directive(exec + strlen(handler->command), &argc);
|
||||
return handler->handle(config, argc, argv);
|
||||
int ret = handler->handle(config, argc, argv);
|
||||
int i;
|
||||
for (i = 0; i < argc; ++i) {
|
||||
free(argv[i]);
|
||||
}
|
||||
free(argv);
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -11,8 +11,14 @@ struct sway_config *read_config(FILE *file) {
|
|||
struct sway_config *config = malloc(sizeof(struct sway_config));
|
||||
config->symbols = create_list();
|
||||
config->modes = create_list();
|
||||
config->current_mode = malloc(sizeof(struct sway_mode));
|
||||
config->current_mode->name = NULL;
|
||||
config->current_mode->bindings = create_list();
|
||||
list_add(config->modes, config->current_mode);
|
||||
|
||||
int temp_braces = 0; // Temporary: skip all config sections with braces
|
||||
bool success = true;
|
||||
|
||||
int temp_depth = 0; // Temporary: skip all config sections with depth
|
||||
|
||||
while (!feof(file)) {
|
||||
int _;
|
||||
|
@ -22,19 +28,25 @@ struct sway_config *read_config(FILE *file) {
|
|||
if (!line[0]) {
|
||||
goto _continue;
|
||||
}
|
||||
if (temp_braces && line[0] == '}') {
|
||||
temp_braces--;
|
||||
if (temp_depth && line[0] == '}') {
|
||||
temp_depth--;
|
||||
goto _continue;
|
||||
}
|
||||
|
||||
handle_command(config, line);
|
||||
if (!handle_command(config, line)) {
|
||||
success = false;
|
||||
}
|
||||
|
||||
_continue:
|
||||
if (line && line[strlen(line) - 1] == '{') {
|
||||
temp_braces++;
|
||||
temp_depth++;
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,11 @@
|
|||
#include <wlc/wlc.h>
|
||||
#include "list.h"
|
||||
|
||||
struct sway_variable {
|
||||
char *name;
|
||||
char *value;
|
||||
};
|
||||
|
||||
struct sway_binding {
|
||||
list_t *keys;
|
||||
struct wlc_modifiers modifiers;
|
||||
|
@ -19,6 +24,7 @@ struct sway_mode {
|
|||
struct sway_config {
|
||||
list_t *symbols;
|
||||
list_t *modes;
|
||||
struct sway_mode *current_mode;
|
||||
};
|
||||
|
||||
struct sway_config *read_config(FILE *file);
|
||||
|
|
|
@ -40,7 +40,7 @@ char *strip_comments(char *str) {
|
|||
} else if (str[i] == '\'' && !in_string) {
|
||||
in_character = !in_character;
|
||||
} else if (!in_character && !in_string) {
|
||||
if (str[i] == '#') {
|
||||
if (str[i] == '#' && i == 0) {
|
||||
str[i] = '\0';
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue