Update Armenian translation
[wmaker-crm.git] / util / wmmenugen_parse_xdg.c
blob1d8975dad915a6dce525494f358598dc5885ed99
1 /*
2 * wmmenugen - Window Maker PropList menu generator
4 * Desktop Entry Specification parser functions
6 * Copyright (c) 2010. Tamas Tevesz <ice@extreme.hu>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.1.html
25 * http://standards.freedesktop.org/menu-spec/menu-spec-1.1.html
27 * We will only deal with Type == "Application" entries in [Desktop Entry]
28 * groups. Since there is no passing of file name arguments or anything of
29 * the sort to applications from the menu, execname is determined as follows:
30 * - If `TryExec' is present, use that;
31 * - else use `Exec' with any switches stripped
33 * Only the (first, though there should not be more than one) `Main Category'
34 * is used to place the entry in a submenu.
36 * Basic validation of the .desktop file is done.
39 #include <sys/types.h>
40 #include <sys/stat.h>
42 #include <ctype.h>
43 #include <ftw.h>
44 #if DEBUG
45 #include <errno.h>
46 #endif
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include "wmmenugen.h"
53 /* LocaleString match levels */
54 enum {
55 MATCH_DEFAULT,
56 MATCH_LANG,
57 MATCH_LANG_MODIFIER,
58 MATCH_LANG_COUNTRY,
59 MATCH_LANG_COUNTRY_MODIFIER
62 typedef struct {
63 char *Name; /* Name */ /* localestring */
64 int MatchLevel; /* LocaleString match type */ /* int */
65 char *TryExec; /* TryExec */ /* string */
66 char *Exec; /* Exec */ /* string */
67 char *Path; /* Path */ /* string */
68 int Flags; /* Flags */
69 char *Category; /* Categories (first item only) */ /* string */
70 } XDGMenuEntry;
72 static void getKey(char **target, const char *line);
73 static void getStringValue(char **target, const char *line);
74 static void getLocalizedStringValue(char **target, const char *line, int *match_level);
75 static int getBooleanValue(const char *line);
76 static void getMenuHierarchyFor(char **xdgmenuspec);
77 static int compare_matchlevel(int *current_level, const char *found_locale);
78 static Bool xdg_to_wm(XDGMenuEntry *xdg, WMMenuEntry *wmentry);
79 static char *parse_xdg_exec(char *exec);
80 static void init_xdg_storage(XDGMenuEntry *xdg);
81 static void init_wm_storage(WMMenuEntry *wm);
84 void parse_xdg(const char *file, cb_add_menu_entry *addWMMenuEntryCallback)
86 FILE *fp;
87 char buf[1024];
88 char *p, *tmp, *key;
89 WMMenuEntry *wm;
90 XDGMenuEntry *xdg;
91 int InGroup;
93 fp = fopen(file, "r");
94 if (!fp) {
95 #if DEBUG
96 fprintf(stderr, "Error opening file %s: %s\n", file, strerror(errno));
97 #endif
98 return;
101 xdg = (XDGMenuEntry *)wmalloc(sizeof(XDGMenuEntry));
102 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
103 InGroup = 0;
104 memset(buf, 0, sizeof(buf));
106 while (fgets(buf, sizeof(buf), fp)) {
108 p = buf;
110 /* skip whitespaces */
111 while (isspace(*p))
112 p++;
113 /* skip comments, empty lines */
114 if (*p == '\r' || *p == '\n' || *p == '#') {
115 memset(buf, 0, sizeof(buf));
116 continue;
118 /* trim crlf */
119 buf[strcspn(buf, "\r\n")] = '\0';
120 if (strlen(buf) == 0)
121 continue;
123 if (strcmp(p, "[Desktop Entry]") == 0) {
124 /* if currently processing a group, we've just hit the
125 * end of its definition, try processing it
127 if (InGroup && xdg_to_wm(xdg, wm)) {
128 (*addWMMenuEntryCallback)(wm);
130 init_xdg_storage(xdg);
131 init_wm_storage(wm);
132 InGroup = 1;
133 /* start processing group */
134 memset(buf, 0, sizeof(buf));
135 continue;
136 } else if (p[0] == '[') {
137 /* If we find a new group and the previous group was the main one,
138 * we stop all further processing
140 if (InGroup) break;
143 if (!InGroup) {
144 memset(buf, 0, sizeof(buf));
145 continue;
148 getKey(&key, p);
149 if (key == NULL) { /* not `key' = `value' */
150 memset(buf, 0, sizeof(buf));
151 continue;
154 if (strcmp(key, "Type") == 0) {
155 getStringValue(&tmp, p);
156 if (strcmp(tmp, "Application") != 0)
157 InGroup = 0; /* if not application, skip current group */
158 wfree(tmp);
159 tmp = NULL;
160 } else if (strcmp(key, "Name") == 0) {
161 getLocalizedStringValue(&xdg->Name, p, &xdg->MatchLevel);
162 } else if (strcmp(key, "NoDisplay") == 0) {
163 if (getBooleanValue(p)) /* if nodisplay, skip current group */
164 InGroup = 0;
165 } else if (strcmp(key, "Hidden") == 0) {
166 if (getBooleanValue(p))
167 InGroup = 0; /* if hidden, skip current group */
168 } else if (strcmp(key, "TryExec") == 0) {
169 getStringValue(&xdg->TryExec, p);
170 } else if (strcmp(key, "Exec") == 0) {
171 getStringValue(&xdg->Exec, p);
172 } else if (strcmp(key, "Path") == 0) {
173 getStringValue(&xdg->Path, p);
174 } else if (strcmp(key, "Terminal") == 0) {
175 if (getBooleanValue(p))
176 xdg->Flags |= F_TERMINAL;
177 } else if (strcmp(key, "Categories") == 0) {
178 getStringValue(&xdg->Category, p);
179 getMenuHierarchyFor(&xdg->Category);
182 if (xdg->Category == NULL)
183 xdg->Category = wstrdup(_("Other"));
185 wfree(key);
186 key = NULL;
189 fclose(fp);
191 /* at the end of the file, might as well try to menuize what we have
192 * unless there was no group at all or it was marked as hidden
194 if (InGroup && xdg_to_wm(xdg, wm))
195 (*addWMMenuEntryCallback)(wm);
200 /* coerce an xdg entry type into a wm entry type
202 static Bool xdg_to_wm(XDGMenuEntry *xdg, WMMenuEntry *wm)
204 char *p;
206 /* Exec or TryExec is mandatory */
207 if (!(xdg->Exec || xdg->TryExec))
208 return False;
210 /* if there's no Name, use the first word of Exec or TryExec
212 if (xdg->Name) {
213 wm->Name = xdg->Name;
214 } else {
215 if (xdg->TryExec)
216 wm->Name = wstrdup(xdg->TryExec);
217 else /* xdg->Exec */
218 wm->Name = wstrdup(xdg->Exec);
220 p = strchr(wm->Name, ' ');
221 if (p)
222 *p = '\0';
225 if (xdg->TryExec)
226 wm->CmdLine = xdg->TryExec;
227 else { /* xdg->Exec */
228 wm->CmdLine = parse_xdg_exec(xdg->Exec);
229 if (!wm->CmdLine)
230 return False;
233 wm->SubMenu = xdg->Category;
234 wm->Flags = xdg->Flags;
235 if (wm->CmdLine != xdg->TryExec)
236 wm->Flags |= F_FREE_CMD_LINE;
238 return True;
241 static char *parse_xdg_exec(char *exec)
243 char *cmd_line, *dst, *src;
244 Bool quoted = False;
246 cmd_line = wstrdup(exec);
248 for (dst = src = cmd_line; *src; src++) {
249 if (quoted) {
250 if (*src == '"')
251 quoted = False;
252 else if (*src == '\\')
253 switch (*++src) {
254 case '"':
255 case '`':
256 case '$':
257 case '\\':
258 *dst++ = *src;
259 break;
260 default:
261 goto err_out;
263 else
264 *dst++ = *src;
265 } else {
266 if (*src == '"')
267 quoted = True;
268 else if (*src == '%') {
269 src++;
270 if (*src == '%')
271 *dst++ = *src;
272 else if (strchr ("fFuUdDnNickvm", *src))
274 * Skip valid field-code.
277 else
279 * Invalid field-code.
281 goto err_out;
282 } else
283 *dst++ = *src;
287 if (quoted)
288 goto err_out;
291 *dst = '\0';
292 while (dst > cmd_line && isspace(*--dst));
294 return cmd_line;
296 err_out:
297 wfree(cmd_line);
298 return NULL;
301 /* (re-)initialize a XDGMenuEntry storage
303 static void init_xdg_storage(XDGMenuEntry *xdg)
306 if (xdg->Name)
307 wfree(xdg->Name);
308 if (xdg->TryExec)
309 wfree(xdg->TryExec);
310 if (xdg->Exec)
311 wfree(xdg->Exec);
312 if (xdg->Category)
313 wfree(xdg->Category);
314 if (xdg->Path)
315 wfree(xdg->Path);
317 xdg->Name = NULL;
318 xdg->TryExec = NULL;
319 xdg->Exec = NULL;
320 xdg->Category = NULL;
321 xdg->Path = NULL;
322 xdg->Flags = 0;
323 xdg->MatchLevel = -1;
326 /* (re-)initialize a WMMenuEntry storage
328 static void init_wm_storage(WMMenuEntry *wm)
330 if (wm->Flags & F_FREE_CMD_LINE)
331 wfree(wm->CmdLine);
333 wm->Name = NULL;
334 wm->CmdLine = NULL;
335 wm->Flags = 0;
338 /* get a key from line. allocates target, which must be wfreed later */
339 static void getKey(char **target, const char *line)
341 const char *p;
342 int kstart, kend;
344 p = line;
346 if (strchr(p, '=') == NULL) { /* not `key' = `value' */
347 *target = NULL;
348 return;
351 kstart = 0;
353 /* skip whitespace */
354 while (isspace(*(p + kstart)))
355 kstart++;
357 /* skip up until first whitespace or '[' (localestring) or '=' */
358 kend = kstart + 1;
359 while (*(p + kend) && !isspace(*(p + kend)) && *(p + kend) != '=' && *(p + kend) != '[')
360 kend++;
362 *target = wstrndup(p + kstart, kend - kstart);
365 /* get a string value from line. allocates target, which must be wfreed later. */
366 static void getStringValue(char **target, const char *line)
368 const char *p;
369 char *q;
370 int kstart;
372 p = line;
373 kstart = 0;
375 /* skip until after '=' */
376 while (*(p + kstart) && *(p + kstart) != '=')
377 kstart++;
378 kstart++;
380 /* skip whitespace */
381 while (*(p + kstart) && isspace(*(p + kstart)))
382 kstart++;
384 *target = wstrdup(p + kstart);
386 for (p = q = *target; *p; p++) {
387 if (*p != '\\') {
388 *q++ = *p;
389 } else {
390 switch (*++p) {
391 case 's': *q++ = ' '; break;
392 case 'n': *q++ = '\n'; break;
393 case 't': *q++ = '\t'; break;
394 case 'r': *q++ = '\r'; break;
395 case '\\': *q++ = '\\'; break;
396 default:
398 * Skip invalid escape.
400 break;
404 *q = '\0';
407 /* get a localized string value from line. allocates target, which must be wfreed later.
408 * matching is dependent on the current value of target as well as on the
409 * level the current value is matched on. guts matching algorithm is in
410 * compare_matchlevel().
412 static void getLocalizedStringValue(char **target, const char *line, int *match_level)
414 const char *p;
415 char *locale;
416 int kstart;
417 int sqbstart, sqbend;
419 p = line;
420 kstart = 0;
421 sqbstart = 0;
422 sqbend = 0;
423 locale = NULL;
425 /* skip until after '=', mark if '[' and ']' is found */
426 while (*(p + kstart) && *(p + kstart) != '=') {
427 switch (*(p + kstart)) {
428 case '[': sqbstart = kstart + 1;break;
429 case ']': sqbend = kstart; break;
430 default : break;
432 kstart++;
434 kstart++;
436 /* skip whitespace */
437 while (isspace(*(p + kstart)))
438 kstart++;
440 if (sqbstart > 0 && sqbend > sqbstart)
441 locale = wstrndup(p + sqbstart, sqbend - sqbstart);
443 /* if there is no value yet and this is the default key, return */
444 if (!*target && !locale) {
445 *match_level = MATCH_DEFAULT;
446 *target = wstrdup(p + kstart);
447 return;
450 if (compare_matchlevel(match_level, locale)) {
451 wfree(locale);
452 *target = wstrdup(p + kstart);
453 return;
456 return;
459 /* get a boolean value from line */
460 static Bool getBooleanValue(const char *line)
462 char *p;
463 int ret;
465 getStringValue(&p, line);
466 ret = strcmp(p, "true") == 0 ? True : False;
467 wfree(p);
469 return ret;
472 /* perform locale matching by implementing the algorithm specified in
473 * xdg desktop entry specification, section "localized values for keys".
475 static Bool compare_matchlevel(int *current_level, const char *found_locale)
477 /* current key locale */
478 char *key_lang, *key_ctry, *key_enc, *key_mod;
480 parse_locale(found_locale, &key_lang, &key_ctry, &key_enc, &key_mod);
482 if (env_lang && key_lang && /* Shortcut: if key and env languages don't match, */
483 strcmp(env_lang, key_lang) != 0) /* don't even bother. This takes care of the great */
484 return False; /* majority of the cases without having to go through */
485 /* the more theoretical parts of the spec'd algo. */
487 if (!env_mod && key_mod) /* If LC_MESSAGES does not have a MODIFIER field, */
488 return False; /* then no key with a modifier will be matched. */
490 if (!env_ctry && key_ctry) /* Similarly, if LC_MESSAGES does not have a COUNTRY field, */
491 return False; /* then no key with a country specified will be matched. */
493 /* LC_MESSAGES value: lang_COUNTRY@MODIFIER */
494 if (env_lang && env_ctry && env_mod) { /* lang_COUNTRY@MODIFIER */
495 if (key_lang && key_ctry && key_mod &&
496 strcmp(env_lang, key_lang) == 0 &&
497 strcmp(env_ctry, key_ctry) == 0 &&
498 strcmp(env_mod, key_mod) == 0) {
499 *current_level = MATCH_LANG_COUNTRY_MODIFIER;
500 return True;
501 } else if (key_lang && key_ctry && /* lang_COUNTRY */
502 strcmp(env_lang, key_lang) == 0 &&
503 strcmp(env_ctry, key_ctry) == 0 &&
504 *current_level < MATCH_LANG_COUNTRY) {
505 *current_level = MATCH_LANG_COUNTRY;
506 return True;
507 } else if (key_lang && key_mod && /* lang@MODIFIER */
508 strcmp(env_lang, key_lang) == 0 &&
509 strcmp(env_mod, key_mod) == 0 &&
510 *current_level < MATCH_LANG_MODIFIER) {
511 *current_level = MATCH_LANG_MODIFIER;
512 return True;
513 } else if (key_lang && /* lang */
514 strcmp(env_lang, key_lang) == 0 &&
515 *current_level < MATCH_LANG) {
516 *current_level = MATCH_LANG;
517 return True;
518 } else {
519 return False;
523 /* LC_MESSAGES value: lang_COUNTRY */
524 if (env_lang && env_ctry) { /* lang_COUNTRY */
525 if (key_lang && key_ctry &&
526 strcmp(env_lang, key_lang) == 0 &&
527 strcmp(env_ctry, key_ctry) == 0 &&
528 *current_level < MATCH_LANG_COUNTRY) {
529 *current_level = MATCH_LANG_COUNTRY;
530 return True;
531 } else if (key_lang && /* lang */
532 strcmp(env_lang, key_lang) == 0 &&
533 *current_level < MATCH_LANG) {
534 *current_level = MATCH_LANG;
535 return True;
536 } else {
537 return False;
541 /* LC_MESSAGES value: lang@MODIFIER */
542 if (env_lang && env_mod) { /* lang@MODIFIER */
543 if (key_lang && key_mod &&
544 strcmp(env_lang, key_lang) == 0 &&
545 strcmp(env_mod, key_mod) == 0 &&
546 *current_level < MATCH_LANG_MODIFIER) {
547 *current_level = MATCH_LANG_MODIFIER;
548 return True;
549 } else if (key_lang && /* lang */
550 strcmp(env_lang, key_lang) == 0 &&
551 *current_level < MATCH_LANG) {
552 *current_level = MATCH_LANG;
553 return True;
554 } else {
555 return False;
559 /* LC_MESSAGES value: lang */
560 if (env_lang) { /* lang */
561 if (key_lang &&
562 strcmp(env_lang, key_lang) == 0 &&
563 *current_level < MATCH_LANG) {
564 *current_level = MATCH_LANG;
565 return True;
566 } else {
567 return False;
571 /* MATCH_DEFAULT is handled in getLocalizedStringValue */
573 return False;
576 /* get the (first) xdg main category from a list of categories
578 static void getMenuHierarchyFor(char **xdgmenuspec)
580 char *category, *p;
581 char buf[1024];
583 if (!*xdgmenuspec || !**xdgmenuspec)
584 return;
586 category = wstrdup(*xdgmenuspec);
587 wfree(*xdgmenuspec);
588 memset(buf, 0, sizeof(buf));
590 p = strtok(category, ";");
591 while (p) { /* get a known category */
592 if (strcmp(p, "AudioVideo") == 0) {
593 snprintf(buf, sizeof(buf), "%s", _("Audio & Video"));
594 break;
595 } else if (strcmp(p, "Audio") == 0) {
596 snprintf(buf, sizeof(buf), "%s", _("Audio"));
597 break;
598 } else if (strcmp(p, "Video") == 0) {
599 snprintf(buf, sizeof(buf), "%s", _("Video"));
600 break;
601 } else if (strcmp(p, "Development") == 0) {
602 snprintf(buf, sizeof(buf), "%s", _("Development"));
603 break;
604 } else if (strcmp(p, "Education") == 0) {
605 snprintf(buf, sizeof(buf), "%s", _("Education"));
606 break;
607 } else if (strcmp(p, "Game") == 0) {
608 snprintf(buf, sizeof(buf), "%s", _("Game"));
609 break;
610 } else if (strcmp(p, "Graphics") == 0) {
611 snprintf(buf, sizeof(buf), "%s", _("Graphics"));
612 break;
613 } else if (strcmp(p, "Network") == 0) {
614 snprintf(buf, sizeof(buf), "%s", _("Network"));
615 break;
616 } else if (strcmp(p, "Office") == 0) {
617 snprintf(buf, sizeof(buf), "%s", _("Office"));
618 break;
619 } else if (strcmp(p, "Science") == 0) {
620 snprintf(buf, sizeof(buf), "%s", _("Science"));
621 break;
622 } else if (strcmp(p, "Settings") == 0) {
623 snprintf(buf, sizeof(buf), "%s", _("Settings"));
624 break;
625 } else if (strcmp(p, "System") == 0) {
626 snprintf(buf, sizeof(buf), "%s", _("System"));
627 break;
628 } else if (strcmp(p, "Utility") == 0) {
629 snprintf(buf, sizeof(buf), "%s", _("Utility"));
630 break;
631 /* reserved categories */
632 } else if (strcmp(p, "Screensaver") == 0) {
633 snprintf(buf, sizeof(buf), "%s", _("Screensaver"));
634 break;
635 } else if (strcmp(p, "TrayIcon") == 0) {
636 snprintf(buf, sizeof(buf), "%s", _("Tray Icon"));
637 break;
638 } else if (strcmp(p, "Applet") == 0) {
639 snprintf(buf, sizeof(buf), "%s", _("Applet"));
640 break;
641 } else if (strcmp(p, "Shell") == 0) {
642 snprintf(buf, sizeof(buf), "%s", _("Shell"));
643 break;
645 p = strtok(NULL, ";");
649 if (!*buf) /* come up with something if nothing found */
650 snprintf(buf, sizeof(buf), "%s", _("Other"));
652 *xdgmenuspec = wstrdup(buf);