From 4a41d4296adac6a7ac8b820491cfedcf70def1cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Gibrowski=20Fa=C3=A9?= Date: Tue, 10 May 2022 22:04:26 -0300 Subject: [PATCH] Implement '&&' and '||' operators on map '-' is a valid character for tags. Commit 03e1c7d (module/network: Add link stats, 2022-04-30) introduced two new tags for the network module: `ul-speed` and `dl-speed`. These use the `-` character, that was previously never used in any tag. We had two options: either change those tags to use `_` instead, or just accept `-`s as a valid character. Going forward, I can see many people deciding to name their tags with `-` instead of `_`, so I believe it is better to just accept it once and for all. Note that `-` cannot be used as the first character of a tag (e.g. `-tag1`) since the `-` has a special meaning in `.yml` files. I don't believe this will happen often, however, and should be easy to both detect and correct if it does. --- .woodpecker.yml | 2 + CHANGELOG.md | 14 ++ doc/yambar-particles.5.scd | 39 +++- examples/configurations/laptop.conf | 8 +- examples/configurations/river-tags.conf | 18 +- particles/map.c | 264 +++++------------------- particles/map.h | 37 ++++ particles/map.l | 25 +++ particles/map.y | 125 +++++++++++ particles/meson.build | 24 ++- 10 files changed, 330 insertions(+), 226 deletions(-) create mode 100644 particles/map.h create mode 100644 particles/map.l create mode 100644 particles/map.y diff --git a/.woodpecker.yml b/.woodpecker.yml index 4ea84f1..aa7ce65 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -40,6 +40,7 @@ pipeline: - apk add json-c-dev libmpdclient-dev alsa-lib-dev - apk add ttf-dejavu - apk add git + - apk add flex bison # Debug - apk add gcovr @@ -97,6 +98,7 @@ pipeline: - apk add json-c-dev libmpdclient-dev alsa-lib-dev - apk add ttf-dejavu - apk add git + - apk add flex bison # Debug - mkdir -p bld/debug-x86 diff --git a/CHANGELOG.md b/CHANGELOG.md index 08ebdc4..a6ae5e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,20 @@ ... ``` + Note that if `` contains any non-alphanumerical characters, + it **must** be surrounded by `""`: + + `State == "very confused!!!"` + + Finally, you can mix and match conditions using the boolean + operators `&&` and `||`: + + ``` + && + && ( || ) # parenthesis work + ~( && ) # '~' can be applied to any condition + ``` + For a more thorough explanation, see the updated map section in the man page for yambar-particles([#137][137] and [#175][175]). diff --git a/doc/yambar-particles.5.scd b/doc/yambar-particles.5.scd index 1b6059d..c04bf93 100644 --- a/doc/yambar-particles.5.scd +++ b/doc/yambar-particles.5.scd @@ -218,11 +218,15 @@ content: This particle maps the values of a specific tag to different particles based on conditions. A condition takes either the form of: +``` +``` Or, for boolean tags: +``` +``` Where is the tag you would like to map, is one of: @@ -233,15 +237,46 @@ Where is the tag you would like to map, is one of: :- <= :- < -and is the value you would like to compare it to. +and is the value you would like to compare it to. *If the +value contains any non-alphanumerical characters, you must +surround it with ' " ' *: -For boolean tags, negation is done with a preceding '~': +``` +"hello world" +"@#$%" +``` +Negation is done with a preceding '~': + +``` ~ +~ +``` To match for empty strings, use ' "" ': +``` == "" +``` + +Furthermore, you may use the boolean operators: + +[- && +:- || + +in order to create more complex conditions: + +``` + && +``` + +You may surround with parenthesis for clarity or +specifying precedence: + +``` +() + && ( || ) +``` In addition to explicit tag values, you can also specify a default/fallback particle. diff --git a/examples/configurations/laptop.conf b/examples/configurations/laptop.conf index 85f43ad..96383ca 100644 --- a/examples/configurations/laptop.conf +++ b/examples/configurations/laptop.conf @@ -178,11 +178,7 @@ bar: map: default: {string: {text: , font: *awesome, foreground: ffffff66}} conditions: - state == up: - map: - default: {string: {text: , font: *awesome}} - conditions: - ipv4 == "": {string: {text: , font: *awesome, foreground: ffffff66}} + state == up && ipv4 != "": {string: {text: , font: *awesome}} - network: name: wlp2s0 poll-interval: 1 @@ -257,7 +253,7 @@ bar: state == full: - string: {text: , foreground: 00ff00ff, font: *awesome} - string: {text: "{capacity}% full"} - state == not charging: + state == "not charging": - ramp: tag: capacity items: diff --git a/examples/configurations/river-tags.conf b/examples/configurations/river-tags.conf index 462a329..54289e6 100644 --- a/examples/configurations/river-tags.conf +++ b/examples/configurations/river-tags.conf @@ -41,16 +41,18 @@ bar: map: <<: *river_base deco: *bg_default - state == visible: + state == visible && ~occupied: map: - conditions: - ~occupied: {map: {<<: *river_base}} - occupied: {map: {<<: *river_base, deco: *bg_default}} + <<: *river_base + state == visible && occupied: + map: + <<: *river_base + deco: *bg_default state == unfocused: map: <<: *river_base - state == invisible: + state == invisible && ~occupied: {empty: {}} + state == invisible && occupied: map: - conditions: - ~occupied: {empty: {}} - occupied: {map: {<<: *river_base, deco: {underline: {size: 3, color: ea6962ff}}}} + <<: *river_base + deco: {underline: {size: 3, color: ea6962ff}} diff --git a/particles/map.c b/particles/map.c index c748bea..82a1ee0 100644 --- a/particles/map.c +++ b/particles/map.c @@ -11,38 +11,7 @@ #include "../plugin.h" #include "dynlist.h" -enum map_op{ - MAP_OP_EQ, - MAP_OP_NE, - MAP_OP_LE, - MAP_OP_LT, - MAP_OP_GE, - MAP_OP_GT, - /* The next two are for bool types */ - MAP_OP_SELF, - MAP_OP_NOT, -}; - -struct map_condition { - char *tag; - enum map_op op; - char *value; -}; - -static char * -trim(char *s) -{ - while (*s == ' ') - s++; - - char *end = s + strlen(s) - 1; - while (*end == ' ') { - *end = '\0'; - end--; - } - - return s; -} +#include "map.h" bool int_condition(const long tag_value, const long cond_value, enum map_op op) @@ -54,6 +23,7 @@ int_condition(const long tag_value, const long cond_value, enum map_op op) case MAP_OP_LT: return tag_value < cond_value; case MAP_OP_GE: return tag_value >= cond_value; case MAP_OP_GT: return tag_value > cond_value; + case MAP_OP_SELF: LOG_WARN("using int tag as bool"); default: return false; } } @@ -68,6 +38,7 @@ float_condition(const double tag_value, const double cond_value, enum map_op op) case MAP_OP_LT: return tag_value < cond_value; case MAP_OP_GE: return tag_value >= cond_value; case MAP_OP_GT: return tag_value > cond_value; + case MAP_OP_SELF: LOG_WARN("using float tag as bool"); default: return false; } } @@ -82,12 +53,13 @@ str_condition(const char* tag_value, const char* cond_value, enum map_op op) case MAP_OP_LT: return strcmp(tag_value, cond_value) < 0; case MAP_OP_GE: return strcmp(tag_value, cond_value) >= 0; case MAP_OP_GT: return strcmp(tag_value, cond_value) > 0; + case MAP_OP_SELF: LOG_WARN("using String tag as bool"); default: return false; } } bool -eval_map_condition(const struct map_condition* map_cond, const struct tag_set *tags) +eval_comparison(const struct map_condition* map_cond, const struct tag_set *tags) { const struct tag *tag = tag_for_name(tags, map_cond->tag); if (tag == NULL) { @@ -131,8 +103,6 @@ eval_map_condition(const struct map_condition* map_cond, const struct tag_set *t case TAG_TYPE_BOOL: if (map_cond->op == MAP_OP_SELF) return tag->as_bool(tag); - else if (map_cond->op == MAP_OP_NOT) - return !tag->as_bool(tag); else { LOG_WARN("boolean tag '%s' should be used directly", map_cond->tag); return false; @@ -145,86 +115,43 @@ eval_map_condition(const struct map_condition* map_cond, const struct tag_set *t return false; } -struct map_condition* -map_condition_from_str(const char *str) +bool +eval_map_condition(const struct map_condition* map_cond, const struct tag_set *tags) { - struct map_condition *cond = malloc(sizeof(*cond)); - - /* This strdup is just to discard the 'const' qualifier */ - char *str_cpy = strdup(str); - char *op_str = strpbrk(str_cpy, "=!<>~"); - - /* we've already checked things in the verify functions, so these should all work */ - char *tag = str_cpy; - char *value = NULL; - - if (op_str == NULL) { - cond->tag = strdup(trim(tag)); - cond->op = MAP_OP_SELF; - cond->value = NULL; - free(str_cpy); - return cond; + switch(map_cond->op) + { + case MAP_OP_NOT: return !eval_map_condition(map_cond->cond1, tags); + case MAP_OP_AND: return eval_map_condition(map_cond->cond1, tags) && eval_map_condition(map_cond->cond2, tags); + case MAP_OP_OR: return eval_map_condition(map_cond->cond1, tags) || eval_map_condition(map_cond->cond2, tags); + default: return eval_comparison(map_cond, tags); } - - switch (op_str[0]) { - case '=': - cond->op = MAP_OP_EQ; - value = op_str + 2; - break; - case '!': - cond->op = MAP_OP_NE; - value = op_str + 2; - break; - case '<': - if (op_str[1] == '=') { - cond->op = MAP_OP_LE; - value = op_str + 2; - } else { - cond->op = MAP_OP_LT; - value = op_str + 1; - } - break; - case '>': - if (op_str[1] == '=') { - cond->op = MAP_OP_GE; - value = op_str + 2; - } else { - cond->op = MAP_OP_GT; - value = op_str + 1; - } - break; - case '~': - tag = op_str + 1; - cond->op = MAP_OP_NOT; - break; - } - - /* NULL terminate the tag value */ - op_str[0] = '\0'; - - cond->tag = strdup(trim(tag)); - - cond->value = NULL; - if (value != NULL) { - value = trim(value); - const size_t value_len = strlen(value); - if (value[0] == '"' && value[value_len - 1] == '"') { - value[value_len - 1] = '\0'; - ++value; - } - cond->value = strdup(value); - } - - free(str_cpy); - return cond; } void -free_map_condition(struct map_condition* mc) +free_map_condition(struct map_condition* c) { - free(mc->tag); - free(mc->value); - free(mc); + switch (c->op) + { + case MAP_OP_EQ: + case MAP_OP_NE: + case MAP_OP_LE: + case MAP_OP_LT: + case MAP_OP_GE: + case MAP_OP_GT: + free(c->value); + /* FALLTHROUGH */ + case MAP_OP_SELF: + free(c->tag); + break; + case MAP_OP_AND: + case MAP_OP_OR: + free_map_condition(c->cond2); + /* FALLTHROUGH */ + case MAP_OP_NOT: + free_map_condition(c->cond1); + break; + } + free(c); } struct particle_map { @@ -379,101 +306,6 @@ map_new(struct particle *common, const struct particle_map particle_map[], return common; } -static bool -verify_map_condition_syntax(keychain_t *chain, const struct yml_node *node, - const char *line) -{ - /* We need this strdup to discard the 'const' qualifier */ - char *cond = strdup(line); - char *op_str = strpbrk(cond, " =!<>~"); - if (op_str == NULL) { - char *tag = trim(cond); - if (tag[0] == '\0') - goto syntax_fail_missing_tag; - - free(cond); - return true; - } - op_str = trim(op_str); - - char *tag = cond; - char *value = NULL; - - switch (op_str[0]) { - case '=': - if (op_str[1] != '=') - goto syntax_fail_invalid_op; - - value = op_str + 2; - break; - - case '!': - if (op_str[1] != '=') - goto syntax_fail_invalid_op; - - value = op_str + 2; - break; - - case '<': - if (op_str[1] == '=') - value = op_str + 2; - else - value = op_str + 1; - break; - - case '>': - if (op_str[1] == '=') - value = op_str + 2; - else - value = op_str + 1; - break; - - case '~': - tag = op_str + 1; - if (strpbrk(tag, " =!<>~") != NULL) - goto syntax_fail_bad_not; - value = NULL; - break; - default: - goto syntax_fail_invalid_op; - } - - /* NULL terminate the tag value */ - op_str[0] = '\0'; - - tag = trim(tag); - if (tag[0] == '\0') - goto syntax_fail_missing_tag; - - value = value != NULL ? trim(value) : NULL; - - if (value != NULL && value[0] == '\0') - goto syntax_fail_missing_value; - - free(cond); - return true; - -syntax_fail_invalid_op: - LOG_ERR("%s: \"%s\" invalid operator", conf_err_prefix(chain, node), line); - goto err; - -syntax_fail_missing_tag: - LOG_ERR("%s: \"%s\" missing tag", conf_err_prefix(chain, node), line); - goto err; - -syntax_fail_missing_value: - LOG_ERR("%s: \"%s\" missing value", conf_err_prefix(chain, node), line); - goto err; - -syntax_fail_bad_not: - LOG_ERR("%s: \"%s\" '~' cannot be used with other operators", - conf_err_prefix(chain, node), line); - goto err; - -err: - free(cond); - return false; -} static bool verify_map_conditions(keychain_t *chain, const struct yml_node *node) @@ -485,6 +317,7 @@ verify_map_conditions(keychain_t *chain, const struct yml_node *node) return false; } + bool result = true; for (struct yml_dict_iter it = yml_dict_iter(node); it.key != NULL; yml_dict_next(&it)) @@ -494,9 +327,16 @@ verify_map_conditions(keychain_t *chain, const struct yml_node *node) LOG_ERR("%s: key must be a string", conf_err_prefix(chain, it.key)); return false; } - - if (!verify_map_condition_syntax(chain, it.key, key)) - return false; + char *key_clone = strdup(key); + YY_BUFFER_STATE buffer = yy_scan_string(key_clone); + if (yyparse() != 0) { + LOG_ERR("%s: %s", conf_err_prefix(chain, it.key), MAP_PARSER_ERROR_MSG); + free(MAP_PARSER_ERROR_MSG); + result = false; + } else + free_map_condition(MAP_CONDITION_PARSE_RESULT); + yy_delete_buffer(buffer); + free(key_clone); if (!conf_verify_particle(chain_push(chain, key), it.value)) return false; @@ -504,7 +344,7 @@ verify_map_conditions(keychain_t *chain, const struct yml_node *node) chain_pop(chain); } - return true; + return result; } static struct particle * @@ -526,7 +366,13 @@ from_conf(const struct yml_node *node, struct particle *common) it.key != NULL; yml_dict_next(&it), idx++) { - particle_map[idx].condition = map_condition_from_str(yml_value_as_string(it.key)); + /* Note we can skip the error checking here */ + char *key_clone = strdup(yml_value_as_string(it.key)); + YY_BUFFER_STATE buffer = yy_scan_string(key_clone); + yyparse(); + particle_map[idx].condition = MAP_CONDITION_PARSE_RESULT; + yy_delete_buffer(buffer); + free(key_clone); particle_map[idx].particle = conf_to_particle(it.value, inherited); } diff --git a/particles/map.h b/particles/map.h new file mode 100644 index 0000000..a6d35b4 --- /dev/null +++ b/particles/map.h @@ -0,0 +1,37 @@ +#pragma once + +enum map_op { + MAP_OP_EQ, + MAP_OP_NE, + MAP_OP_LE, + MAP_OP_LT, + MAP_OP_GE, + MAP_OP_GT, + MAP_OP_SELF, + MAP_OP_NOT, + + MAP_OP_AND, + MAP_OP_OR, +}; + +struct map_condition { + union { + char *tag; + struct map_condition *cond1; + }; + enum map_op op; + union { + char *value; + struct map_condition *cond2; + }; +}; + +void free_map_condition(struct map_condition *c); + +typedef struct yy_buffer_state* YY_BUFFER_STATE; +YY_BUFFER_STATE yy_scan_string(const char *str); +int yyparse(); +void yy_delete_buffer(YY_BUFFER_STATE buffer); + +extern struct map_condition *MAP_CONDITION_PARSE_RESULT; +extern char *MAP_PARSER_ERROR_MSG; diff --git a/particles/map.l b/particles/map.l new file mode 100644 index 0000000..7a5ebc8 --- /dev/null +++ b/particles/map.l @@ -0,0 +1,25 @@ +%{ +#include +#include "map.h" +#include "map.tab.h" +%} + +%option warn nodefault nounput noinput noyywrap + +%% +[[:alnum:]_-]+ yylval.str = strdup(yytext); return WORD; +\".*\" yylval.str = strndup(yytext + 1, strlen(yytext) - 2); return STRING; +== yylval.op = MAP_OP_EQ; return CMP_OP; +!= yylval.op = MAP_OP_NE; return CMP_OP; +\<= yylval.op = MAP_OP_LE; return CMP_OP; +\< yylval.op = MAP_OP_LT; return CMP_OP; +>= yylval.op = MAP_OP_GE; return CMP_OP; +> yylval.op = MAP_OP_GT; return CMP_OP; +&& yylval.op = MAP_OP_AND; return BOOL_OP; +\|\| yylval.op = MAP_OP_OR; return BOOL_OP; +~ return NOT; +\( return L_PAR; +\) return R_PAR; +[ \t\n] ; +. yylval.str = strdup(yytext); return STRING; +%% diff --git a/particles/map.y b/particles/map.y new file mode 100644 index 0000000..ee426da --- /dev/null +++ b/particles/map.y @@ -0,0 +1,125 @@ +%{ +#include +#include + +#include "map.h" + +struct map_condition *MAP_CONDITION_PARSE_RESULT; +char *MAP_PARSER_ERROR_MSG; + +static const int NUM_TOKENS = 7; +int yylex(); +void yyerror(const char *str); +%} + +%define parse.lac full +%define parse.error custom + +%union { + char *str; + struct map_condition *condition; + enum map_op op; +} + +%token WORD STRING CMP_OP L_PAR R_PAR +%left BOOL_OP +%precedence NOT + +%destructor { free_map_condition($$); } condition +%destructor { free($$); } WORD +%destructor { free($$); } STRING + +%% +result: condition { MAP_CONDITION_PARSE_RESULT = $1; }; + +condition: + WORD { + $$ = malloc(sizeof(struct map_condition)); + $$->tag = $1; + $$->op = MAP_OP_SELF; + } + | + WORD CMP_OP WORD { + $$ = malloc(sizeof(struct map_condition)); + $$->tag = $1; + $$->op = $2; + $$->value = $3; + } + | + WORD CMP_OP STRING { + $$ = malloc(sizeof(struct map_condition)); + $$->tag = $1; + $$->op = $2; + $$->value = $3; + } + | + L_PAR condition R_PAR { $$ = $2; } + | + NOT condition { + $$ = malloc(sizeof(struct map_condition)); + $$->cond1 = $2; + $$->op = MAP_OP_NOT; + } + | + condition BOOL_OP condition { + $$ = malloc(sizeof(struct map_condition)); + $$->cond1 = $1; + $$->op = $2; + $$->cond2 = $3; + } + ; +%% + +void yyerror(const char *str) +{ + fprintf(stderr, "error: %s\n", str); +} + +static char const* +token_to_str(yysymbol_kind_t tkn) +{ + switch (tkn) { + case YYSYMBOL_CMP_OP: return "==, !=, <=, <, >=, >"; + case YYSYMBOL_BOOL_OP: return "||, &&"; + case YYSYMBOL_L_PAR: return "("; + case YYSYMBOL_R_PAR: return ")"; + case YYSYMBOL_NOT: return "~"; + default: return yysymbol_name(tkn); + } +} + +static int +yyreport_syntax_error (const yypcontext_t *ctx) +{ + int res = 0; + char *errmsg = malloc(1024); + errmsg[0] = '\0'; + + // Report the tokens expected at this point. + yysymbol_kind_t expected[NUM_TOKENS]; + int n = yypcontext_expected_tokens(ctx, expected, NUM_TOKENS); + if (n < 0) + res = n; // Forward errors to yyparse. + else { + for (int i = 0; i < n; ++i) { + strcat(errmsg, i == 0 ? "expected [" : ", "); + strcat(errmsg, token_to_str(expected[i])); + } + strcat(errmsg, "]"); + } + + // Report the unexpected token. + yysymbol_kind_t lookahead = yypcontext_token(ctx); + if (lookahead != YYSYMBOL_YYEMPTY) { + strcat(errmsg, ", found "); + if (!(lookahead == YYSYMBOL_STRING || lookahead == YYSYMBOL_WORD)) + strcat(errmsg, yysymbol_name(lookahead)); + else if (yylval.str != NULL) + strcat(errmsg, yylval.str); + else + strcat(errmsg, "nothing"); + } + + MAP_PARSER_ERROR_MSG = errmsg; + return res; +} diff --git a/particles/meson.build b/particles/meson.build index 6b31081..091f551 100644 --- a/particles/meson.build +++ b/particles/meson.build @@ -1,3 +1,25 @@ +flex = find_program('flex', required: true) +bison = find_program('bison', required: true) + +lgen = generator( + flex, + output : '@BASENAME@.yy.c', + arguments : ['-o', '@OUTPUT@', '@INPUT@'] +) +lfiles = lgen.process('map.l') + +pgen = generator( + bison, + output : ['@BASENAME@.tab.c', '@BASENAME@.tab.h'], + arguments : ['-Wall', + '-Wcounterexamples', + '--defines=@OUTPUT1@', + '--output=@OUTPUT0@', '@INPUT@'] +) +pfiles = pgen.process('map.y') + +map_parser = declare_dependency(sources: [pfiles, lfiles], include_directories: '.') + particle_sdk = declare_dependency(dependencies: [pixman, tllist, fcft]) dynlist_lib = build_target( @@ -14,7 +36,7 @@ dynlist = declare_dependency(link_with: dynlist_lib) deps = { 'empty': [], 'list': [], - 'map': [dynlist], + 'map': [dynlist, map_parser], 'progress-bar': [], 'ramp': [], 'string': [],