xdg.c
This file
Path
xdg.c
930 lines · 30.7 KiB · C
Last changed by
xdg: add support for TryExecDaniel Eklöfbefore adoption38e1c6e
History of this fileTake it
The walk stopped after 200 commits: lines older than 6a4c416 point at it.
changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1–2why, one step back1#include "xdg.h"2✨ Add {cmd} in --terminal command, like Rofi. (#600)Mark Stosbergbefore adoption08ef4de
line 3why, one step back3#include <assert.h>changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 4–61why, one step back4#include <ctype.h>5#include <errno.h>6#include <limits.h>7#include <locale.h>8#include <stdbool.h>9#include <stdio.h>10#include <stdlib.h>11#include <string.h>12#include <unistd.h>1314#include <sys/types.h>15#include <sys/stat.h>16#include <dirent.h>17#include <fcntl.h>18#include <pwd.h>1920#define LOG_MODULE "xdg"21#define LOG_ENABLE_DBG 022#include "log.h"23#include "char32.h"24#include "icon.h"25#include "macros.h"26#include "xmalloc.h"27#include "xsnprintf.h"2829typedef tll(struct application *) application_llist_t;3031struct locale_variants {32 char *lang_country_modifier;33 char *lang_country;34 char *lang_modifier;35 char *lang;36};3738struct action {39 char32_t *name;40 char32_t *generic_name;41 char *app_id;42 char32_t *comment;43 char32_list_t keywords;44 char32_list_t categories;45 char_list_t onlyshowin;46 char_list_t notshowin;4748 int name_locale_score;49 int generic_name_locale_score;50 int comment_locale_score;51 int keywords_locale_score;52 int categories_locale_score;5354 char *icon;55 char *exec;56 char32_t *wexec;5758 char *path;59 bool visible;60 bool use_terminal;61 bool no_startup_notify;launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
line 62why, one step back62 char *action_id;changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 63–65why, one step back63};6465static boolxdg: add support for TryExecDaniel Eklöfbefore adoption38e1c6e
lines 66–94why, one step back66is_executable(const char *bin)67{68 if (bin[0] == '/')69 return access(bin, X_OK) == 0;7071 const char *path = getenv("PATH");72 if (path == NULL)73 return false;7475 char *copy = xstrdup(path);76 for (char *ctx = NULL, *p = strtok_r(copy, ":", &ctx);77 p != NULL;78 p = strtok_r(NULL, ":", &ctx))79 {80 char *full_path = xstrjoin3(p, "/", bin);81 bool ok = access(full_path, X_OK) == 0;82 free(full_path);8384 if (ok) {85 free(copy);86 return true;87 }88 }8990 free(copy);91 return false;92}9394static boolchangelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 95–121why, one step back95filter_desktop_entry(const struct action *act, const char_list_t *desktops)96{97 /* If a matching entry is found in OnlyShowIn then the desktop file is98 * shown. If an entry is found in NotShowIn then the desktop file is not99 * shown. */100 tll_foreach(*desktops, current) {101 tll_foreach(act->onlyshowin, desktop) {102 if (strcmp(current->item, desktop->item) == 0)103 return true;104 }105106 tll_foreach(act->notshowin, desktop) {107 if (strcmp(current->item, desktop->item) == 0)108 return false;109 }110 }111112 /* By default, a desktop file should be shown, unless an OnlyShowIn key is113 * present, in which case, the default is for the file not to be shown. */114 return tll_length(act->onlyshowin) == 0;115}116117static void118parse_desktop_file(int fd, char *id, const char32_t *file_basename_lowercase,119 const char *terminal, bool include_actions,120 bool filter_desktops, char_list_t *desktops,121 const struct locale_variants *lc_messages,launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
line 122why, one step back122 application_llist_t *applications, const char *desktop_file_path)changelog: add missing refDaniel Eklöfbefore adoption6a4c416
line 123why, one step back123{Yet more O_CLOEXECDaniel Eklöfbefore adoption43742d4
line 124why, one step back124 FILE *f = fdopen(fd, "re");xdg: don't double close .desktop FDDaniel Eklöfbefore adoption8e5ba71
lines 125–126why, one step back125 if (f == NULL) {126 close(fd);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
line 127why, one step back127 return;xdg: don't double close .desktop FDDaniel Eklöfbefore adoption8e5ba71
line 128why, one step back128 }changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 129–134why, one step back129130 bool is_desktop_entry = false;131132 tll(char *) action_names = tll_init();133134 tll(struct action) actions = tll_init();launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
line 135why, one step back135 tll_push_back(actions, ((struct action){.visible = true, .action_id = NULL}));changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 136–198why, one step back136 struct action *action = &tll_back(actions);137 struct action *default_action = action;138139 while (true) {140 char *raw_line = NULL;141 size_t sz = 0;142 ssize_t len = getline(&raw_line, &sz, f);143144 if (len == -1) {145 free(raw_line);146 break;147 }148149 if (len == 0) {150 free(raw_line);151 continue;152 }153154 if (raw_line[len - 1] == '\n') {155 raw_line[len - 1] = '\0';156 len--;157 }158159 char *line = raw_line;160161 /* Strip leading whitespace */162 while (len > 0 && isspace(line[0])) {163 line++;164 len--;165 }166167 /* Strip trailing whitespace */168 while (len > 0 && isspace(line[len - 1])) {169 line[len - 1] = '\0';170 len--;171 }172173 if (len == 0) {174 free(raw_line);175 continue;176 }177178 if (line[0] == '[' && line[len - 1] == ']') {179 if (strncasecmp(&line[1], "desktop entry", len - 2) == 0) {180 is_desktop_entry = true;181 free(raw_line);182 continue;183 }184185 else if (include_actions &&186 strncasecmp(&line[1], "desktop action ", 15) == 0)187 {188 const char *action_name = &line[16];189 const size_t name_len = len - 1 - 16;190191 bool action_is_valid = false;192 tll_foreach(action_names, it) {193 if (strlen(it->item) == name_len &&194 strncmp(it->item, action_name, name_len) == 0)195 {196 tll_push_back(actions, ((struct action){0}));197 action = &tll_back(actions);198launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
lines 199–200why, one step back199200 action->action_id = xstrndup(action_name, name_len);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 201–300why, one step back201 action->generic_name =202 default_action->generic_name != NULL203 ? xc32dup(default_action->generic_name) : NULL;204 action->comment = default_action->comment != NULL205 ? xc32dup(default_action->comment) : NULL;206 action->path = default_action->path != NULL207 ? xstrdup(default_action->path) : NULL;208 action->icon = default_action->icon != NULL209 ? xstrdup(default_action->icon) : NULL;210 action->visible = default_action->visible;211 action->use_terminal = default_action->use_terminal;212213 tll_foreach(default_action->keywords, it)214 tll_push_back(action->keywords, xc32dup(it->item));215 tll_foreach(default_action->categories, it)216 tll_push_back(action->categories, xc32dup(it->item));217218 action_is_valid = true;219 break;220 }221 }222223 free(raw_line);224 if (action_is_valid)225 continue;226 else227 break;228 }229230 else {231 free(raw_line);232 break;233 }234 }235236 char *ctx;237 char *key = strtok_r(line, "=", &ctx);238 char *value = strtok_r(NULL, "\n", &ctx);239 int locale_score = 1; /* Default, locale not specified in key */240241 if (key != NULL && value != NULL) {242 size_t key_len = strlen(key);243244 /* Strip trailing whitespace from the key name */245 while (key_len > 1 && isspace(key[key_len - 1])) {246 key[key_len - 1] = '\0';247 key_len--;248 }249250 /* Strip leading whitespace from value */251 while (value[0] != '\0' && isspace(value[0]))252 value++;253254 if (key[key_len - 1] == ']') {255 char *locale = strchr(key, '[');256 if (locale != NULL) {257 /* NULL terminate key */258 *locale = '\0';259260 /* NULL terminate locale */261 key[key_len - 1] = '\0';262 locale++; /* Skip past ‘[’ */263264 if (lc_messages->lang_country_modifier != NULL &&265 strcmp(locale, lc_messages->lang_country_modifier) == 0)266 {267 locale_score = 5;268 } else if (lc_messages->lang_country != NULL &&269 strcmp(locale, lc_messages->lang_country) == 0)270 {271 locale_score = 4;272 } else if (lc_messages->lang_modifier != NULL &&273 strcmp(locale, lc_messages->lang_modifier) == 0)274 {275 locale_score = 3;276 } else if (lc_messages->lang != NULL &&277 strcmp(locale, lc_messages->lang) == 0)278 {279 locale_score = 2;280 } else {281 /* Key has locale, but didn’t match. Make sure282 * we don’t use its value */283 locale_score = -1;284 }285 }286 }287288 if (strcmp(key, "Name") == 0) {289 if (locale_score > action->name_locale_score) {290 free(action->name);291 action->name = ambstoc32(value);292 action->name_locale_score = locale_score;293 }294 }295296 else if (strcmp(key, "Exec") == 0) {297 free(action->exec);298 free(action->wexec);299 action->exec = xstrdup(value);300 action->wexec = ambstoc32(value);xdg: add support for TryExecDaniel Eklöfbefore adoption38e1c6e
lines 301–306why, one step back301 }302303 else if (strcmp(key, "TryExec") == 0) {304 if (!is_executable(value)) {305 action->visible = false;306 }changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 307–443why, one step back307 }308309 else if (strcmp(key, "Path") == 0) {310 free(action->path);311 action->path = xstrdup(value);312 }313314 else if (strcmp(key, "GenericName") == 0) {315 if (locale_score > action->generic_name_locale_score) {316 free(action->generic_name);317 action->generic_name = ambstoc32(value);318 action->generic_name_locale_score = locale_score;319 }320 }321322 else if (strcmp(key, "StartupNotify") == 0) {323 if (strcmp(value, "false"))324 action->no_startup_notify = true;325 }326327 else if (strcmp(key, "StartupWMClass") == 0) {328 free(action->app_id);329 action->app_id = xstrdup(value);330 }331332 else if (strcmp(key, "Comment") == 0) {333 if (locale_score > action->comment_locale_score) {334 free(action->comment);335 action->comment = ambstoc32(value);336 action->comment_locale_score = locale_score;337 }338 }339340 else if (strcmp(key, "Keywords") == 0) {341 if (locale_score > action->keywords_locale_score) {342 for (const char *kw = strtok_r(value, ";", &ctx);343 kw != NULL;344 kw = strtok_r(NULL, ";", &ctx))345 {346 char32_t *wide_kw = ambstoc32(kw);347 if (wide_kw != NULL)348 tll_push_back(action->keywords, wide_kw);349 }350351 action->keywords_locale_score = locale_score;352 }353 }354355 else if (strcmp(key, "Categories") == 0) {356 if (locale_score > action->categories_locale_score) {357 for (const char *category = strtok_r(value, ";", &ctx);358 category != NULL;359 category = strtok_r(NULL, ";", &ctx))360 {361 char32_t *wide_category = ambstoc32(category);362 if (wide_category != NULL)363 tll_push_back(action->categories, wide_category);364 }365366 action->categories_locale_score = locale_score;367 }368 }369370 else if (strcmp(key, "Actions") == 0) {371 for (const char *action = strtok_r(value, ";", &ctx);372 action != NULL;373 action = strtok_r(NULL, ";", &ctx))374 {375 tll_push_back(action_names, xstrdup(action));376 }377 }378379 else if (strcmp(key, "OnlyShowIn") == 0) {380 for (const char *desktop = strtok_r(value, ";", &ctx);381 desktop != NULL;382 desktop = strtok_r(NULL, ";", &ctx))383 {384 tll_push_back(action->onlyshowin, xstrdup(desktop));385 }386 }387388 else if (strcmp(key, "NotShowIn") == 0) {389 for (const char *desktop = strtok_r(value, ";", &ctx);390 desktop != NULL;391 desktop = strtok_r(NULL, ";", &ctx))392 {393 tll_push_back(action->notshowin, xstrdup(desktop));394 }395 }396397 else if (strcmp(key, "Icon") == 0) {398 free(action->icon);399 action->icon = xstrdup(value);400 }401402 else if (strcmp(key, "Hidden") == 0 ||403 strcmp(key, "NoDisplay") == 0)404 {405 if (strcmp(value, "true") == 0)406 action->visible = false;407 }408409 else if (strcmp(key, "Terminal") == 0) {410 if (strcmp(value, "true") == 0)411 action->use_terminal = true;412 }413 }414 free(raw_line);415 }416417 fclose(f);418419 tll_foreach(actions, it) {420 struct action *a = &it->item;421422 if (!is_desktop_entry) {423 free(a->name);424 free(a->generic_name);425 free(a->app_id);426 free(a->comment);427 free(a->icon);428 free(a->exec);429 free(a->wexec);430 free(a->path);431432 tll_free_and_free(a->keywords, free);433 tll_free_and_free(a->categories, free);434 tll_free_and_free(a->onlyshowin, free);435 tll_free_and_free(a->notshowin, free);436437 continue;438 }439440 if (a->name == NULL)441 a->name = ambstoc32("<no title>");442443 if (a->use_terminal && terminal != NULL && a->exec != NULL) {✨ Add {cmd} in --terminal command, like Rofi. (#600)Mark Stosbergbefore adoption08ef4de
lines 444–490why, one step back444 char *exec_with_terminal;445446 // Check if terminal command contains {cmd} placeholder447 if (strstr(terminal, "{cmd}") != NULL) {448 // Replace all occurrences of {cmd} with the actual command449 const char *placeholder = "{cmd}";450 const size_t placeholder_len = 5; // strlen("{cmd}")451 const size_t exec_len = strlen(a->exec);452 const size_t terminal_len = strlen(terminal);453454 // Count occurrences of {cmd}455 size_t occurrences = 0;456 const char *pos = terminal;457 while ((pos = strstr(pos, placeholder)) != NULL) {458 occurrences++;459 pos += placeholder_len;460 }461462 // Calculate final string size463 assert(terminal_len >= occurrences * placeholder_len); // should be true, so let's assert it is464 size_t final_len = terminal_len - occurrences * placeholder_len + occurrences * exec_len + 1;465 exec_with_terminal = xmalloc(final_len);466467 // Replace all {cmd} occurrences468 const char *src = terminal;469 char *dst = exec_with_terminal;470 while ((pos = strstr(src, placeholder)) != NULL) {471 // Copy part before {cmd}472 size_t prefix_len = pos - src;473 memcpy(dst, src, prefix_len);474 dst += prefix_len;475476 // Copy the exec command477 memcpy(dst, a->exec, exec_len);478 dst += exec_len;479480 // Move past {cmd}481 src = pos + placeholder_len;482 }483484 // Copy remaining part485 strcpy(dst, src);486 } else {487 // Use the old behavior: "terminal command exec"488 exec_with_terminal = xstrjoin3(terminal, " ", a->exec);489 }490changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 491–494why, one step back491 free(a->exec);492 a->exec = exec_with_terminal;493 }494launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
lines 495–502why, one step back495 /* Save original action name before modifying title */496 char32_t *saved_action_name = a->name ? xc32dup(a->name) : NULL;497 char32_t *saved_default_name = default_action->name ? xc32dup(default_action->name) : NULL;498499 /* Save original generic name before converting to lowercase */500 char32_t *saved_generic_name = a->generic_name ? xc32dup(a->generic_name) : NULL;501 char32_t *saved_default_generic_name = default_action->generic_name ? xc32dup(default_action->generic_name) : NULL;502changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 503–565why, one step back503 char32_t *title = a->name;504 if (a != default_action) {505 title = xc32join3(default_action->name, U" — ", a->name);506 free(a->name);507 }508509 const size_t title_len = c32len(title);510 const size_t basename_len = file_basename_lowercase != NULL511 ? c32len(file_basename_lowercase)512 : 0;513 const size_t wexec_len = action->wexec != NULL514 ? c32len(action->wexec)515 : 0;516 const size_t generic_name_len = action->generic_name != NULL517 ? c32len(action->generic_name)518 : 0;519 const size_t comment_len = action->comment != NULL520 ? c32len(action->comment)521 : 0;522523 char32_t *title_lowercase = xc32dup(title);524 for (size_t i = 0; i < title_len; i++)525 title_lowercase[i] = toc32lower(title_lowercase[i]);526527 for (size_t i = 0; i < wexec_len; i++)528 action->wexec[i] = toc32lower(action->wexec[i]);529 for (size_t i = 0; i < generic_name_len; i++)530 action->generic_name[i] = toc32lower(action->generic_name[i]);531 for (size_t i = 0; i < comment_len; i++)532 action->comment[i] = toc32lower(action->comment[i]);533 tll_foreach(a->keywords, it) {534 for (size_t i = 0; i < c32len(it->item); i++)535 it->item[i] = toc32lower(it->item[i]);536 }537 tll_foreach(a->categories, it) {538 for (size_t i = 0; i < c32len(it->item); i++)539 it->item[i] = toc32lower(it->item[i]);540 }541542 struct application *app = xmalloc(sizeof(*app));543 *app = (struct application){544 .id = xstrdup(id),545 .index = 0, /* Not used in application mode */546 .path = a->path,547 .exec = a->exec,548 .app_id = a->app_id,549 .title = title,550 .title_lowercase = title_lowercase,551 .basename = xc32dup(file_basename_lowercase),552 .wexec = a->wexec,553 .generic_name = a->generic_name,554 .comment = a->comment,555 .keywords = a->keywords,556 .categories = a->categories,557 .title_len = title_len,558 .basename_len = basename_len,559 .wexec_len = wexec_len,560 .generic_name_len = generic_name_len,561 .comment_len = comment_len,562 .icon = {.name = a->icon},563 .visible = a->visible && (!filter_desktops || filter_desktop_entry(a, desktops)),564 .startup_notify = !a->no_startup_notify,565 .count = 0,launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
lines 566–569why, one step back566 /* Additional metadata */567 .desktop_file_path = xstrdup(desktop_file_path),568 .action_id = a->action_id ? xstrdup(a->action_id) : NULL,569 .original_name = saved_default_name,xdg: plug memory leak; saved_action_name is already strdup:ed onceDaniel Eklöfbefore adoption28c5a05
line 570why, one step back570 .localized_name = saved_action_name,launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
lines 571–574why, one step back571 .action_name = (a->action_id && saved_action_name) ? xc32dup(saved_action_name) : NULL,572 .localized_action_name = (a->action_id && saved_action_name) ? xc32dup(saved_action_name) : NULL,573 .original_generic_name = saved_default_generic_name,574 .localized_generic_name = saved_generic_name,changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 575–581why, one step back575 };576 tll_push_back(*applications, app);577 tll_free_and_free(a->onlyshowin, free);578 tll_free_and_free(a->notshowin, free);579 }580581 free(id);launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
lines 582–584why, one step back582 tll_foreach(actions, it) {583 free(it->item.action_id);584 }changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 585–600why, one step back585 tll_free(actions);586 tll_free_and_free(action_names, free);587}588589static char *590new_id(const char *base_id, const char *new_part)591{592 if (base_id == NULL)593 return xstrdup(new_part);594595 return xstrjoin3(base_id, "-", new_part);596}597598static void599scan_dir(int base_fd, const char *terminal, bool include_actions,600 bool filter_desktop, char_list_t *desktops,launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
line 601why, one step back601 application_llist_t *applications, const char *base_id, const char *base_path)changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 602–655why, one step back602{603 DIR *d = fdopendir(base_fd);604 if (d == NULL) {605 LOG_ERRNO("failed to open directory");606 return;607 }608609 const char *locale = setlocale(LC_MESSAGES, NULL);610 struct locale_variants lc_messages = {NULL};611612 if (locale != NULL) {613 char *lang = xstrdup(locale);614 char *country_start = strchr(lang, '_');615 char *encoding_start = strchr(lang, '.');616 char *modifier_start = strchr(lang, '@');617618 // Replace found delimiters with null terminators and skip past them619 if (country_start != NULL)620 *country_start++ = '\0';621 if (encoding_start != NULL)622 *encoding_start++ = '\0';623 if (modifier_start != NULL)624 *modifier_start++ = '\0';625626 lc_messages.lang = lang;627 if (country_start != NULL)628 lc_messages.lang_country = xstrjoin3(lang, "_", country_start);629630 if (modifier_start != NULL)631 lc_messages.lang_modifier = xstrjoin3(lang, "@", modifier_start);632633 if (country_start != NULL && modifier_start != NULL)634 lc_messages.lang_country_modifier = xstrjoin3(635 lc_messages.lang_country, "@", modifier_start);636637 LOG_DBG("lang=%s, lang_country=%s, lang@modifier=%s, lang_country@modifier=%s",638 lc_messages.lang,639 loggable_str(lc_messages.lang_country),640 loggable_str(lc_messages.lang_modifier),641 loggable_str(lc_messages.lang_country_modifier));642 }643644645 for (const struct dirent *e = readdir(d); e != NULL; e = readdir(d)) {646 if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)647 continue;648649 struct stat st;650 if (fstatat(base_fd, e->d_name, &st, 0) == -1) {651 LOG_WARN("%s: failed to stat: %s", e->d_name, strerror(errno));652 continue;653 }654655 if (S_ISDIR(st.st_mode)) {Yet more O_CLOEXECDaniel Eklöfbefore adoption43742d4
line 656why, one step back656 int dir_fd = openat(base_fd, e->d_name, O_RDONLY | O_CLOEXEC);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 657–662why, one step back657 if (dir_fd == -1) {658 LOG_ERRNO("%s: failed to open", e->d_name);659 continue;660 }661662 char *nested_base_id = new_id(base_id, e->d_name);launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
lines 663–664why, one step back663 char nested_base_path[PATH_MAX];664 xsnprintf(nested_base_path, sizeof(nested_base_path), "%s/%s", base_path, e->d_name);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 665–666why, one step back665 scan_dir(dir_fd, terminal, include_actions,666 filter_desktop, desktops,launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
line 667why, one step back667 applications, nested_base_id, nested_base_path);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 668–680why, one step back668 free(nested_base_id);669 close(dir_fd);670 } else if (S_ISREG(st.st_mode)) {671 /* Skip files not ending with ".desktop" */672 const size_t desktop_len = strlen(".desktop");673 const size_t name_len = strlen(e->d_name);674 if (name_len < desktop_len)675 continue;676677 if (strcmp(&e->d_name[name_len - desktop_len], ".desktop") != 0)678 continue;679680 //LOG_DBG("%s", e->d_name);Yet more O_CLOEXECDaniel Eklöfbefore adoption43742d4
line 681why, one step back681 int fd = openat(base_fd, e->d_name, O_RDONLY | O_CLOEXEC);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 682–715why, one step back682 if (fd == -1)683 LOG_WARN("%s: failed to open: %s", e->d_name, strerror(errno));684 else {685 const char *file_basename = strrchr(e->d_name, '/');686 if (file_basename == NULL)687 file_basename = e->d_name;688 else689 file_basename++;690691 const char *extension = strrchr(file_basename, '.');692 if (extension == NULL)693 extension = file_basename + strlen(file_basename);694695 int chars = mbsntoc32(NULL, file_basename, extension - file_basename, 0);696 assert(chars >= 0);697698 char32_t wfile_basename[chars + 1];699 mbsntoc32(wfile_basename, file_basename, extension - file_basename, chars);700 wfile_basename[chars] = U'\0';701702 for (size_t i = 0; i < chars; i++)703 wfile_basename[i] = toc32lower(wfile_basename[i]);704705 char *id = new_id(base_id, e->d_name);706707 bool already_added = false;708 tll_foreach(*applications, it) {709 if (strcmp(it->item->id, id) == 0) {710 already_added = true;711 break;712 }713 }714715 if (!already_added) {launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
lines 716–719why, one step back716 /* Construct full desktop file path */717 char desktop_file_path[PATH_MAX];718 xsnprintf(desktop_file_path, sizeof(desktop_file_path), "%s/%s", base_path, e->d_name);719changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 720–722why, one step back720 parse_desktop_file(721 fd, id, wfile_basename, terminal, include_actions,722 filter_desktop, desktops,launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
line 723why, one step back723 &lc_messages, applications, desktop_file_path);xdg: don't double close .desktop FDDaniel Eklöfbefore adoption8e5ba71
lines 724–725why, one step back724 /* fd closed by parse_desktop_file() */725 } else {changelog: add missing refDaniel Eklöfbefore adoption6a4c416
line 726why, one step back726 free(id);xdg: don't double close .desktop FDDaniel Eklöfbefore adoption8e5ba71
lines 727–728why, one step back727 close(fd);728 }changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 729–759why, one step back729 }730 }731 }732733 free(lc_messages.lang);734 free(lc_messages.lang_country);735 free(lc_messages.lang_modifier);736 free(lc_messages.lang_country_modifier);737 closedir(d);738}739740static int741sort_application_by_title(const void *_a, const void *_b)742{743 const struct application *const *a = _a;744 const struct application *const *b = _b;745 return c32casecmp((*a)->title, (*b)->title);746}747748void749xdg_find_programs(const char *terminal, bool include_actions,750 bool filter_desktop, char_list_t *desktops,751 struct application_list *applications)752{753 application_llist_t apps = tll_init();754755 xdg_data_dirs_t dirs = xdg_data_dirs();756 tll_foreach(dirs, it) {757 char path[strlen(it->item.path) + 1 + strlen("applications") + 1];758 sprintf(path, "%s/applications", it->item.path);759Yet more O_CLOEXECDaniel Eklöfbefore adoption43742d4
line 760why, one step back760 int fd = open(path, O_RDONLY | O_CLOEXEC);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
line 761why, one step back761 if (fd != -1) {launch-prefix: Pass more environment variablesMark Stosbergbefore adoption72f6b2f
line 762why, one step back762 scan_dir(fd, terminal, include_actions, filter_desktop, desktops, &apps, NULL, path);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 763–768why, one step back763 close(fd);764 }765 }766767 mtx_lock(&applications->lock);768 applications->count = tll_length(apps);logs: warn when no apps are found.Mark Stosbergbefore adoptionea49370
line 769why, one step back769 if (applications->count > 0) {xdg: don't crash when no applications were foundDaniel Eklöfbefore adoptionf7c632d
line 770why, one step back770 applications->v = xmalloc(tll_length(apps) * sizeof(applications->v[0]));logs: warn when no apps are found.Mark Stosbergbefore adoptionea49370
lines 771–772why, one step back771 } else {772 LOG_WARN("No applications found. See SEARCH PATHS in `man fuzzel` for details.");xdg: don't crash when no applications were foundDaniel Eklöfbefore adoptionf7c632d
line 773why, one step back773 applications->v = NULL;logs: warn when no apps are found.Mark Stosbergbefore adoptionea49370
line 774why, one step back774 }changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 775–852why, one step back775776 size_t i = 0;777 tll_foreach(apps, it) {778 applications->v[i++] = it->item;779 if (it->item->visible)780 applications->visible_count++;781 tll_remove(apps, it);782 }783 tll_free(apps);784785 qsort(applications->v, applications->count, sizeof(applications->v[0]),786 &sort_application_by_title);787 mtx_unlock(&applications->lock);788789 xdg_data_dirs_destroy(dirs);790791#if defined(_DEBUG) && LOG_ENABLE_DBG && 0792 for (size_t i = 0; i < applications->count; i++) {793 const struct application *app = &applications->v[i];794795 char32_t keywords[1024];796 char32_t categories[1024];797798 int idx = 0;799 tll_foreach(app->keywords, it) {800 idx += swprintf(&keywords[idx],801 ALEN(keywords) - idx,802 L"%ls, ", (const wchar_t *)it->item);803 }804805 if (idx > 0)806 keywords[idx - 2] = U'\0';807808 idx = 0;809 tll_foreach(app->categories, it) {810 idx += swprintf(&categories[idx],811 ALEN(categories) - idx,812 L"%ls, ", (const wchar_t *)it->item);813 }814815 if (idx > 0)816 categories[idx - 2] = U'\0';817818 LOG_DBG("%s:\n"819 " name/title: %ls\n"820 " path: %s\n"821 " exec: %s\n"822 " basename: %ls\n"823 " generic-name: %ls\n"824 " comment: %ls\n"825 " keywords: %ls\n"826 " categories: %ls\n"827 " icon:\n"828 " name: %s\n"829 " type: %s",830 app->id, (const wchar_t *)app->title, app->path, app->exec,831 (const wchar_t *)app->basename,832 (const wchar_t *)app->generic_name,833 (const wchar_t *)app->comment,834 (const wchar_t *)keywords,835 (const wchar_t *)categories,836 app->icon.name,837 app->icon.type == ICON_PNG ? "PNG" :838 app->icon.type == ICON_SVG ? "SVG" : "<none>");839840841 }842#endif843}844845xdg_data_dirs_t846xdg_data_dirs(void)847{848 xdg_data_dirs_t ret = tll_init();849850 const char *xdg_data_home = getenv("XDG_DATA_HOME");851 const char *home;852 if (xdg_data_home != NULL && xdg_data_home[0] != '\0') {Yet more O_CLOEXECDaniel Eklöfbefore adoption43742d4
line 853why, one step back853 int fd = open(xdg_data_home, O_RDONLY | O_DIRECTORY | O_CLOEXEC);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 854–859why, one step back854 if (fd >= 0) {855 struct xdg_data_dir d = {.fd = fd, .path = xstrdup(xdg_data_home)};856 tll_push_back(ret, d);857 }858 } else if ((home = getenv("HOME")) != NULL && home[0] != '\0') {859 char *path = xstrjoin(home, "/.local/share");Yet more O_CLOEXECDaniel Eklöfbefore adoption43742d4
line 860why, one step back860 int fd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 861–878why, one step back861 if (fd >= 0) {862 struct xdg_data_dir d = {.fd = fd, .path = path};863 tll_push_back(ret, d);864 } else865 free(path);866 }867868 const char *_xdg_data_dirs = getenv("XDG_DATA_DIRS");869870 if (_xdg_data_dirs != NULL) {871872 char *ctx = NULL;873 char *copy = xstrdup(_xdg_data_dirs);874875 for (const char *tok = strtok_r(copy, ":", &ctx);876 tok != NULL;877 tok = strtok_r(NULL, ":", &ctx))878 {Yet more O_CLOEXECDaniel Eklöfbefore adoption43742d4
line 879why, one step back879 int fd = open(tok, O_RDONLY | O_DIRECTORY | O_CLOEXEC);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 880–887why, one step back880 if (fd >= 0) {881 struct xdg_data_dir d = {.fd = fd, .path = xstrdup(tok)};882 tll_push_back(ret, d);883 }884 }885886 free(copy);887 } else {Yet more O_CLOEXECDaniel Eklöfbefore adoption43742d4
lines 888–889why, one step back888 int fd1 = open("/usr/local/share", O_RDONLY | O_DIRECTORY | O_CLOEXEC);889 int fd2 = open("/usr/share", O_RDONLY | O_DIRECTORY | O_CLOEXEC);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 890–930why, one step back890891 if (fd1 >= 0) {892 struct xdg_data_dir d = {.fd = fd1, .path = xstrdup("/usr/local/share")};893 tll_push_back(ret, d);894 }895896 if (fd2 >= 0) {897 struct xdg_data_dir d = {.fd = fd2, .path = xstrdup("/usr/share")};898 tll_push_back(ret, d);;899 }900 }901902 return ret;903}904905void906xdg_data_dirs_destroy(xdg_data_dirs_t dirs)907{908 tll_foreach(dirs, it) {909 close(it->item.fd);910 free(it->item.path);911 tll_remove(dirs, it);912 }913}914915const char *916xdg_cache_dir(void)917{918 const char *xdg_cache_home = getenv("XDG_CACHE_HOME");919 if (xdg_cache_home != NULL && xdg_cache_home[0] != '\0')920 return xdg_cache_home;921922 const char *home = getenv("HOME");923 if (home != NULL && home[0] != '\0') {924 static char path[PATH_MAX];925 xsnprintf(path, sizeof(path), "%s/.cache", home);926 return path;927 }928929 return NULL;930}