1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <getopt.h>
#include <sfdo-basedir.h>
#include <sfdo-desktop.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void log_handler(enum sfdo_log_level level, const char *fmt, va_list args, void *data) {
(void)level;
(void)data;
static const char *levels[] = {
[SFDO_LOG_LEVEL_SILENT] = "",
[SFDO_LOG_LEVEL_ERROR] = "error",
[SFDO_LOG_LEVEL_INFO] = "info",
[SFDO_LOG_LEVEL_DEBUG] = "debug",
};
fprintf(stdout, "[%s] ", levels[level]);
vfprintf(stdout, fmt, args);
fprintf(stdout, "\n");
}
static void die_usage(const char *prog) {
printf("Usage: %s [-d] [-e environment] [-l locale]\n", prog);
exit(1);
}
int main(int argc, char **argv) {
bool debug = false;
const char *locale = NULL;
const char *env = NULL;
char *prog = argv[0];
int opt;
while ((opt = getopt(argc, argv, "de:l:")) != -1) {
switch (opt) {
case 'd':
debug = true;
break;
case 'e':
env = optarg;
break;
case 'l':
locale = optarg;
break;
default:
die_usage(prog);
}
}
argv += optind;
argc -= optind;
if (argc > 0) {
die_usage(prog);
}
(void)argv;
struct sfdo_basedir_ctx *basedir_ctx = sfdo_basedir_ctx_create();
struct sfdo_desktop_ctx *ctx = sfdo_desktop_ctx_create(basedir_ctx);
sfdo_desktop_ctx_set_log_handler(
ctx, debug ? SFDO_LOG_LEVEL_DEBUG : SFDO_LOG_LEVEL_ERROR, log_handler, NULL);
struct sfdo_desktop_db *db = sfdo_desktop_db_load(ctx, locale);
if (db == NULL) {
fprintf(stderr, "Failed to load a database\n");
exit(1);
}
size_t n_entries;
struct sfdo_desktop_entry **entries = sfdo_desktop_db_get_entries(db, &n_entries);
size_t env_len = 0;
if (env != NULL) {
env_len = strlen(env);
}
for (size_t i = 0; i < n_entries; i++) {
struct sfdo_desktop_entry *entry = entries[i];
if (sfdo_desktop_entry_get_no_display(entry)) {
continue;
}
if (!sfdo_desktop_entry_show_in(entry, env, env_len)) {
continue;
}
const char *id = sfdo_desktop_entry_get_id(entry, NULL);
const char *name = sfdo_desktop_entry_get_name(entry, NULL);
printf("%s: %s\n", id, name);
}
sfdo_desktop_db_destroy(db);
sfdo_desktop_ctx_destroy(ctx);
sfdo_basedir_ctx_destroy(basedir_ctx);
return 0;
}
|