aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/stringop.c115
-rw-r--r--include/stringop.h13
-rw-r--r--swaybar/tray/icon.c47
3 files changed, 38 insertions, 137 deletions
diff --git a/common/stringop.c b/common/stringop.c
index 709be684..b9c9a193 100644
--- a/common/stringop.c
+++ b/common/stringop.c
@@ -150,29 +150,6 @@ void free_argv(int argc, char **argv) {
150 free(argv); 150 free(argv);
151} 151}
152 152
153char *code_strstr(const char *haystack, const char *needle) {
154 /* TODO */
155 return strstr(haystack, needle);
156}
157
158char *code_strchr(const char *str, char delimiter) {
159 int in_string = 0, in_character = 0;
160 int i = 0;
161 while (str[i] != '\0') {
162 if (str[i] == '"' && !in_character) {
163 in_string = !in_string;
164 } else if (str[i] == '\'' && !in_string) {
165 in_character = !in_character;
166 } else if (!in_character && !in_string) {
167 if (str[i] == delimiter) {
168 return (char *)str + i;
169 }
170 }
171 ++i;
172 }
173 return NULL;
174}
175
176int unescape_string(char *string) { 153int unescape_string(char *string) {
177 /* TODO: More C string escapes */ 154 /* TODO: More C string escapes */
178 int len = strlen(string); 155 int len = strlen(string);
@@ -276,84 +253,6 @@ char *join_args(char **argv, int argc) {
276 return res; 253 return res;
277} 254}
278 255
279static bool has_whitespace(const char *str) {
280 while (*str) {
281 if (isspace(*str)) {
282 return true;
283 }
284 ++str;
285 }
286 return false;
287}
288
289/**
290 * Add quotes around any argv with whitespaces.
291 */
292void add_quotes(char **argv, int argc) {
293 int i;
294 for (i = 0; i < argc; ++i) {
295 if (has_whitespace(argv[i])) {
296 int len = strlen(argv[i]) + 3;
297 char *tmp = argv[i];
298 argv[i] = malloc(len * sizeof(char));
299 snprintf(argv[i], len, "\"%s\"", tmp);
300 free(tmp);
301 }
302 }
303}
304
305/*
306 * Join a list of strings, adding separator in between. Separator can be NULL.
307 */
308char *join_list(list_t *list, char *separator) {
309 if (!sway_assert(list != NULL, "list != NULL") || list->length == 0) {
310 return NULL;
311 }
312
313 size_t len = 1; // NULL terminator
314 size_t sep_len = 0;
315 if (separator != NULL) {
316 sep_len = strlen(separator);
317 len += (list->length - 1) * sep_len;
318 }
319
320 for (int i = 0; i < list->length; i++) {
321 len += strlen(list->items[i]);
322 }
323
324 char *res = malloc(len);
325
326 char *p = res + strlen(list->items[0]);
327 strcpy(res, list->items[0]);
328
329 for (int i = 1; i < list->length; i++) {
330 if (sep_len) {
331 memcpy(p, separator, sep_len);
332 p += sep_len;
333 }
334 strcpy(p, list->items[i]);
335 p += strlen(list->items[i]);
336 }
337
338 *p = '\0';
339
340 return res;
341}
342
343char *cmdsep(char **stringp, const char *delim) {
344 // skip over leading delims
345 char *head = *stringp + strspn(*stringp, delim);
346 // Find end token
347 char *tail = *stringp += strcspn(*stringp, delim);
348 // Set stringp to beginning of next token
349 *stringp += strspn(*stringp, delim);
350 // Set stringp to null if last token
351 if (!**stringp) *stringp = NULL;
352 // Nullify end of first token
353 *tail = 0;
354 return head;
355}
356
357char *argsep(char **stringp, const char *delim) { 256char *argsep(char **stringp, const char *delim) {
358 char *start = *stringp; 257 char *start = *stringp;
359 char *end = start; 258 char *end = start;
@@ -389,17 +288,3 @@ char *argsep(char **stringp, const char *delim) {
389 found: 288 found:
390 return start; 289 return start;
391} 290}
392
393const char *strcasestr(const char *haystack, const char *needle) {
394 size_t needle_len = strlen(needle);
395 const char *pos = haystack;
396 const char *end = pos + strlen(haystack) - needle_len;
397
398 while (pos <= end) {
399 if (strncasecmp(pos, needle, needle_len) == 0) {
400 return pos;
401 }
402 ++pos;
403 }
404 return NULL;
405}
diff --git a/include/stringop.h b/include/stringop.h
index f7ca60a5..6f920999 100644
--- a/include/stringop.h
+++ b/include/stringop.h
@@ -4,7 +4,6 @@
4#include "list.h" 4#include "list.h"
5 5
6void strip_whitespace(char *str); 6void strip_whitespace(char *str);
7char *strip_comments(char *str);
8void strip_quotes(char *str); 7void strip_quotes(char *str);
9 8
10// strcat that does nothing if dest or src is NULL 9// strcat that does nothing if dest or src is NULL
@@ -21,22 +20,10 @@ list_t *split_string(const char *str, const char *delims);
21char **split_args(const char *str, int *argc); 20char **split_args(const char *str, int *argc);
22void free_argv(int argc, char **argv); 21void free_argv(int argc, char **argv);
23 22
24char *code_strchr(const char *string, char delimiter);
25char *code_strstr(const char *haystack, const char *needle);
26int unescape_string(char *string); 23int unescape_string(char *string);
27char *join_args(char **argv, int argc); 24char *join_args(char **argv, int argc);
28char *join_list(list_t *list, char *separator);
29 25
30/**
31 * Add quotes around any argv with whitespaces.
32 */
33void add_quotes(char **argv, int argc);
34
35// split string into 2 by delim.
36char *cmdsep(char **stringp, const char *delim);
37// Split string into 2 by delim, handle quotes 26// Split string into 2 by delim, handle quotes
38char *argsep(char **stringp, const char *delim); 27char *argsep(char **stringp, const char *delim);
39 28
40const char *strcasestr(const char *haystack, const char *needle);
41
42#endif 29#endif
diff --git a/swaybar/tray/icon.c b/swaybar/tray/icon.c
index bf2736c2..c7ce20b4 100644
--- a/swaybar/tray/icon.c
+++ b/swaybar/tray/icon.c
@@ -301,6 +301,43 @@ static list_t *load_themes_in_dir(char *basedir) {
301 return themes; 301 return themes;
302} 302}
303 303
304static void log_loaded_themes(list_t *themes) {
305 if (themes->length == 0) {
306 sway_log(SWAY_INFO, "Warning: no icon themes loaded");
307 return;
308 }
309
310 const char *sep = ", ";
311 size_t sep_len = strlen(sep);
312
313 size_t len = 1 - sep_len;
314 for (int i = 0; i < themes->length; ++i) {
315 struct icon_theme *theme = themes->items[i];
316 len += strlen(theme->name) + sep_len;
317 }
318
319 char *str = malloc(len);
320 if (!str) {
321 return;
322 }
323 char *p = str;
324 for (int i = 0; i < themes->length; ++i) {
325 if (i > 0) {
326 memcpy(p, sep, sep_len);
327 p += sep_len;
328 }
329
330 struct icon_theme *theme = themes->items[i];
331 size_t name_len = strlen(theme->name);
332 memcpy(p, theme->name, name_len);
333 p += name_len;
334 }
335 *p = '\0';
336
337 sway_log(SWAY_DEBUG, "Loaded icon themes: %s", str);
338 free(str);
339}
340
304void init_themes(list_t **themes, list_t **basedirs) { 341void init_themes(list_t **themes, list_t **basedirs) {
305 *basedirs = get_basedirs(); 342 *basedirs = get_basedirs();
306 343
@@ -311,15 +348,7 @@ void init_themes(list_t **themes, list_t **basedirs) {
311 list_free(dir_themes); 348 list_free(dir_themes);
312 } 349 }
313 350
314 list_t *theme_names = create_list(); 351 log_loaded_themes(*themes);
315 for (int i = 0; i < (*themes)->length; ++i) {
316 struct icon_theme *theme = (*themes)->items[i];
317 list_add(theme_names, theme->name);
318 }
319 char *theme_list = join_list(theme_names, ", ");
320 sway_log(SWAY_DEBUG, "Loaded themes: %s", theme_list);
321 free(theme_list);
322 list_free(theme_names);
323} 352}
324 353
325void finish_themes(list_t *themes, list_t *basedirs) { 354void finish_themes(list_t *themes, list_t *basedirs) {