suwako / UnixMenu アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
UnixMenu / src / parser.c
3.4 KB、138行 生 Blame 履歴
1#include "parser.h"
2#include "utils.h"
3
4#include <stdio.h>
5#include <string.h>
6#include <stdbool.h>
7#include <stdlib.h>
8#include <dirent.h>
9#include <unistd.h>
10
11bool foundName = false;
12bool foundKey = false;
13bool foundExec = false;
14bool isDesktopEntry = false;
15
16void parse_name(char *line, Program *program) {
17 if (foundName) return;
18 if (strncmp(line, "Name[", 5) == 0) {
19 char *locale = line + 5;
20 char *end = strchr(locale, ']');
21 if (!end) return;
22 *end = '\0';
23 if (strncmp(locale, getenv("LANG"), 2) != 0) return;
24 if (!isdup(end + 2)) {
25 strncpy(program->name, end + 2, MAX_NAME_LEN - 1);
26 program->name[strcspn(program->name, "\n")] = '\0';
27 }
28 add_to_dup(end + 2);
29 foundName = true;
30 } else {
31 if (!isdup(line + 5)) {
32 strncpy(program->name, line + 5, MAX_NAME_LEN - 1);
33 program->name[strcspn(program->name, "\n")] = '\0';
34 }
35 add_to_dup(line + 5);
36 foundName = true;
37 }
38}
39
40void parse_keywords(char *line, Program *program) {
41 if (foundKey) return;
42 size_t len = strnlen(line + 9, MAX_NAME_LEN - 1);
43 strncpy(program->keys, line + 9, len);
44 program->keys[len] = '\0';
45
46 program->keys[strcspn(program->keys, "\n")] = '\0';
47 foundKey = true;
48}
49
50void parse_exec(char *line, Program *program) {
51 if (foundExec) return;
52 size_t len = strnlen(line + 5, MAX_NAME_LEN - 1);
53 strncpy(program->exec, line + 5, len);
54 program->exec[strcspn(program->exec, "\n")] = '\0';
55
56 char *p = program->exec;
57 while ((p = strpbrk(p, "%")) != NULL) {
58 *p = '\0';
59
60 size_t rspace = MAX_NAME_LEN - strlen(program->exec) - 1;
61 if (rspace > 0) {
62 strncat(program->exec, p + 2, rspace);
63 }
64 }
65 foundExec = true;
66}
67
68void parse_term(char *line, Program *program) {
69 size_t len = strnlen(line + 9, MAX_NAME_LEN - 1);
70 strncpy(program->term, line + 9, len);
71 program->term[strcspn(program->term, "\n")] = '\0';
72
73 char *p = program->term;
74
75 while ((p = strpbrk(p, "%")) != NULL) {
76 *p = '\0';
77
78 size_t rspace = MAX_NAME_LEN - strlen(program->term) - 1;
79 if (rspace > 0) {
80 strncat(program->term, p + 2, rspace);
81 }
82 }
83}
84
85void parse_desktop_file(const char *filepath) {
86 FILE *file = fopen(filepath, "r");
87 if (!file) return;
88
89 char line[MAX_NAME_LEN * 2];
90 Program program = {0};
91
92 while (fgets(line, sizeof(line), file)) {
93 if (strncmp(line, "Name=", 5) == 0 || strncmp(line, "Name[", 5) == 0) {
94 parse_name(line, &program);
95 } else if (strncmp(line, "Keywords=", 9) == 0) {
96 parse_keywords(line, &program);
97 } else if (strncmp(line, "Exec=", 5) == 0) {
98 parse_exec(line, &program);
99 } else if (strncmp(line, "Terminal=", 9) == 0) {
100 parse_term(line, &program);
101 }
102 }
103
104 fclose(file);
105
106 if (program.name[0] && program.exec[0]) {
107 programs[programcount++] = program;
108 }
109}
110
111void scan_desktop_files(const char *directory) {
112 DIR *dir = opendir(directory);
113 if (!dir) return;
114
115 struct dirent *entry;
116 while ((entry = readdir(dir)) != NULL) {
117 if (strcasestr(entry->d_name, ".desktop")) {
118 size_t filepath_len = strlen(directory) + strlen(entry->d_name) + 2;
119 char *filepath = malloc(filepath_len);
120 if (!filepath) {
121 closedir(dir);
122 return;
123 }
124
125 snprintf(filepath, filepath_len, "%s/%s", directory, entry->d_name);
126 if (isdis(filepath)) parse_desktop_file(filepath);
127
128 isDesktopEntry = false;
129 foundName = false;
130 foundKey = false;
131 foundExec = false;
132
133 free(filepath);
134 }
135 }
136
137 closedir(dir);
138}