diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2022-12-28 00:27:22 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2022-12-28 00:27:22 -0500 |
| commit | 3398d028c61165c219bf9361f5b81d3a54e968c8 (patch) | |
| tree | fcf6bbb62c7d0d6bb86a58e0db4eb3d74356c9e1 /util2.c | |
| parent | f2d52d23f74c065f288d9d4b399404fe50f38fc3 (diff) | |
| download | dwm-3398d028c61165c219bf9361f5b81d3a54e968c8.tar.gz dwm-3398d028c61165c219bf9361f5b81d3a54e968c8.tar.xz | |
Use regular expressions for tagging windows by class and name
Diffstat (limited to 'util2.c')
| -rw-r--r-- | util2.c | 53 |
1 files changed, 53 insertions, 0 deletions
@@ -0,0 +1,53 @@ +#include <stdio.h> +#include <fcntl.h> +#include <err.h> +#include <unistd.h> +#include <stdlib.h> +#include <regex.h> + +int +readEnv(FILE *fh) +{ + int ret; + char name[1024], value[8192]; + + while (1) { + if ((ret = fscanf(fh, "%1023[^=]=%8191[^\n]\n", &name, &value)) == EOF) + break; + else if (ret == 0) + break; + + fprintf(stderr, "Setting: '%s' = '%s'\n", name, value); + setenv(name, value, 1); + } + + return 0; +} + +int +reMatch(const char *regex, const char *str) { + char reErr[1024] = {0}; + regex_t *re = calloc(1, sizeof(regex_t)); + int rc = 0; + + if (!re) + return -1; + + rc = regcomp(re, regex, REG_EXTENDED|REG_ICASE|REG_NOSUB); + if (rc != 0) { + regerror(rc, re, reErr, 1024); + fprintf(stderr, "Regex compile err: %s %s\n", regex, reErr); + return -1; + } + + rc = regexec(re, str, 0, NULL, 0); + regfree(re); + if (rc != 0) { + regerror(rc, re, reErr, 1024); + fprintf(stderr, "Regex match error: %s -> %s : %s\n", + regex, str, reErr); + return -1; + } + + return rc; +} |
