From 71af5b7ddebe6b714f3be65bf8c810c952a7e0af Mon Sep 17 00:00:00 2001 From: taiyu Date: Mon, 7 Sep 2015 14:29:40 -0700 Subject: config modes --- sway/stringop.c | 151 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 108 insertions(+), 43 deletions(-) (limited to 'sway/stringop.c') diff --git a/sway/stringop.c b/sway/stringop.c index c39e2c34..d2b74655 100644 --- a/sway/stringop.c +++ b/sway/stringop.c @@ -1,34 +1,26 @@ -#include "stringop.h" #include #include +#include +#include +#include "stringop.h" +#include "log.h" #include "string.h" #include "list.h" -#include -#include + +const char *whitespace = " \f\n\r\t\v"; /* Note: This returns 8 characters for trimmed_start per tab character. */ -char *strip_whitespace(char *_str, int *trimmed_start) { - *trimmed_start = 0; - if (*_str == '\0') - return _str; - char *strold = _str; - while (*_str == ' ' || *_str == '\t') { - if (*_str == '\t') { - *trimmed_start += 8; - } else { - *trimmed_start += 1; - } - _str++; - } - char *str = malloc(strlen(_str) + 1); - strcpy(str, _str); - free(strold); - int i; - for (i = 0; str[i] != '\0'; ++i); +char *strip_whitespace(char *_str) { + int ws = strspn(_str, whitespace); + int len = strlen(_str) - ws + 1; + sway_log(L_DEBUG,"%d, %d..",ws,len); + char *str = malloc(len); + strcpy(str, _str+ws); + free(_str); do { - i--; - } while (i >= 0 && (str[i] == ' ' || str[i] == '\t')); - str[i + 1] = '\0'; + len--; + } while (len >= 0 && (str[len] == ' ' || str[len] == '\t')); + str[len + 1] = '\0'; return str; } @@ -41,7 +33,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] == '#' && i == 0) { + if (str[i] == '#') { str[i] = '\0'; break; } @@ -51,26 +43,45 @@ char *strip_comments(char *str) { return str; } +void strip_quotes(char *str) { + bool in_str = false; + bool in_chr = false; + bool escaped = false; + char *end = strchr(str,0); + while (*str) { + if (*str == '\'' && !in_str && !escaped) { + in_chr = !in_chr; + goto shift_over; + } else if (*str == '\"' && !in_chr && !escaped) { + in_str = !in_str; + goto shift_over; + } else if (*str == '\\') { + escaped = !escaped; + ++str; + continue; + } + escaped = false; + ++str; + continue; + shift_over: + memmove(str, str+1, end-- - str); + } + *end = '\0'; +} + list_t *split_string(const char *str, const char *delims) { list_t *res = create_list(); - int i, j; - int len = strlen(str); - for (i = 0, j = 0; i < len + 1; ++i) { - if (strchr(delims, str[i]) || i == len) { - if (i - j == 0) { - continue; - } - char *left = malloc(i - j + 1); - memcpy(left, str + j, i - j); - left[i - j] = 0; - list_add(res, left); - j = i + 1; - while (j <= len && str[j] && strchr(delims, str[j])) { - j++; - i++; - } - } + char *copy = malloc(strlen(str) + 1); + char *token; + strcpy(copy, str); + + token = strtok(copy, delims); + while(token) { + token = strdup(token); + list_add(res, token); + token = strtok(NULL, delims); } + free(copy); return res; } @@ -82,6 +93,60 @@ void free_flat_list(list_t *list) { list_free(list); } +char **split_args(const char *start, int *argc) { + *argc = 0; + int alloc = 2; + char **parts = malloc(sizeof(char *) * alloc); + bool in_token = false; + bool in_string = false; + bool in_char = false; + bool escaped = false; + const char *end = start; + while (*start) { + if (!in_token) { + start = (end += strspn(end, whitespace)); + in_token = true; + } + if (*end == '"' && !in_char && !escaped) { + in_string = !in_string; + } else if (*end == '\'' && !in_string && !escaped) { + in_char = !in_char; + } else if (*end == '\\') { + escaped = !escaped; + } else if (*end == '\0' || (!in_string && !in_char && !escaped + && strchr(whitespace, *end))) { + goto add_part; + } + if (*end != '\\') { + escaped = false; + } + ++end; + continue; + add_part: + if (end - start > 0) { + char *token = malloc(end - start + 1); + strncpy(token, start, end - start + 1); + token[end - start] = '\0'; + strip_quotes(token); + unescape_string(token); + parts[*argc] = token; + if (++*argc == alloc) { + parts = realloc(parts, (alloc *= 2) * sizeof(char *)); + } + } + in_token = false; + escaped = false; + } + return parts; +} + +void free_argv(int argc, char **argv) { + while (--argc) { + free(argv[argc]); + } + free(argv); +} + char *code_strstr(const char *haystack, const char *needle) { /* TODO */ return strstr(haystack, needle); @@ -177,7 +242,7 @@ int unescape_string(char *string) { string[i - 1] = c; } } - memmove(string + i, string + i + shift, len - i); + memmove(string + i, string + i + shift, len - i + 1); } } return len; -- cgit v1.2.3 From 47ff0006975a2fcab2178b729deec675c2283363 Mon Sep 17 00:00:00 2001 From: taiyu Date: Mon, 7 Sep 2015 14:40:23 -0700 Subject: put strip_whitespace back --- sway/config.c | 1 + sway/stringop.c | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) (limited to 'sway/stringop.c') diff --git a/sway/config.c b/sway/config.c index 119730ca..95cd8b0d 100644 --- a/sway/config.c +++ b/sway/config.c @@ -230,6 +230,7 @@ bool read_config(FILE *file, bool is_active) { char *line; while (!feof(file)) { line = read_line(file); + line = strip_whitespace(line); line = strip_comments(line); if (line[0] == '\0') { goto _continue; diff --git a/sway/stringop.c b/sway/stringop.c index d2b74655..77faf3f1 100644 --- a/sway/stringop.c +++ b/sway/stringop.c @@ -11,16 +11,21 @@ const char *whitespace = " \f\n\r\t\v"; /* Note: This returns 8 characters for trimmed_start per tab character. */ char *strip_whitespace(char *_str) { - int ws = strspn(_str, whitespace); - int len = strlen(_str) - ws + 1; - sway_log(L_DEBUG,"%d, %d..",ws,len); - char *str = malloc(len); - strcpy(str, _str+ws); - free(_str); + if (*_str == '\0') + return _str; + char *strold = _str; + while (*_str == ' ' || *_str == '\t') { + _str++; + } + char *str = malloc(strlen(_str) + 1); + strcpy(str, _str); + free(strold); + int i; + for (i = 0; str[i] != '\0'; ++i); do { - len--; - } while (len >= 0 && (str[len] == ' ' || str[len] == '\t')); - str[len + 1] = '\0'; + i--; + } while (i >= 0 && (str[i] == ' ' || str[i] == '\t')); + str[i + 1] = '\0'; return str; } -- cgit v1.2.3 From 3eb29ea7364724af99e4f4a5f7a6f633e17baf8d Mon Sep 17 00:00:00 2001 From: taiyu Date: Mon, 7 Sep 2015 15:03:04 -0700 Subject: strdup + style --- sway/commands.c | 9 +++------ sway/config.c | 3 +-- sway/stringop.c | 8 +++----- 3 files changed, 7 insertions(+), 13 deletions(-) (limited to 'sway/stringop.c') diff --git a/sway/commands.c b/sway/commands.c index 72d53ff0..e7ddfa71 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -383,9 +383,8 @@ static bool cmd_mode(int argc, char **argv) { // Create mode if it doesnt exist if (!mode && argc >= 2 && strncmp(argv[1],"{",1) == 0) { mode = malloc(sizeof*mode); - mode->name = malloc(strlen(mode_name) + 1); + mode->name = strdup(mode_name); mode->bindings = create_list(); - strcpy(mode->name, mode_name); list_add(config->modes, mode); } if (!mode) { @@ -834,10 +833,8 @@ static bool cmd_set(int argc, char **argv) { return false; } 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]); + var->name = strdup(argv[0]); + var->value = strdup(argv[1]); list_add(config->symbols, var); return true; } diff --git a/sway/config.c b/sway/config.c index 95cd8b0d..5ece2810 100644 --- a/sway/config.c +++ b/sway/config.c @@ -249,8 +249,7 @@ bool read_config(FILE *file, bool is_active) { sway_log(L_ERROR, "Invalid command during config ``%s''", line); } else if (handler->config_type == CMD_COMPOSITOR_READY && !is_active) { sway_log(L_DEBUG, "Deferring command ``%s''", line); - char *cmd = malloc(strlen(line) + 1); - strcpy(cmd, line); + char *cmd = strdup(line); list_add(config->cmd_queue, cmd); } else if (!handle_command(line)) { sway_log(L_DEBUG, "Config load failed for line ``%s''", line); diff --git a/sway/stringop.c b/sway/stringop.c index 77faf3f1..1ba54ec6 100644 --- a/sway/stringop.c +++ b/sway/stringop.c @@ -17,14 +17,13 @@ char *strip_whitespace(char *_str) { while (*_str == ' ' || *_str == '\t') { _str++; } - char *str = malloc(strlen(_str) + 1); - strcpy(str, _str); + char *str = strdup(_str); free(strold); int i; for (i = 0; str[i] != '\0'; ++i); do { i--; - } while (i >= 0 && (str[i] == ' ' || str[i] == '\t')); + } while (i >= 0 && (str[i] == ' ' || str[i] == '\t')); str[i + 1] = '\0'; return str; } @@ -76,9 +75,8 @@ void strip_quotes(char *str) { list_t *split_string(const char *str, const char *delims) { list_t *res = create_list(); - char *copy = malloc(strlen(str) + 1); + char *copy = strdup(str); char *token; - strcpy(copy, str); token = strtok(copy, delims); while(token) { -- cgit v1.2.3