1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 obt/ddparse.c for the Openbox window manager
4 Copyright (c) 2009 Dana Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
19 #include "obt/ddparse.h"
28 typedef struct _ObtDDParse ObtDDParse;
30 /* Parses the value and adds it to the group's key_hash, with the given
32 Return TRUE if it is added to the hash table, and FALSE if not.
34 typedef gboolean (*ObtDDParseValueFunc)(gchar *key, const gchar *val,
35 ObtDDParseLangMatch match,
36 ObtDDParse *parse, gboolean *error);
41 DE_TYPE_APPLICATION = 1 << 1,
42 DE_TYPE_LINK = 1 << 2,
49 const gchar *filename;
50 const gchar *language;
52 const gchar *modifier;
55 ObtDDParseGroup *group;
56 /* the key is a group name, the value is a ObtDDParseGroup */
57 GHashTable *group_hash;
60 struct _ObtDDParseGroup {
63 ObtDDParseValueFunc value_func;
64 /* the key is a string (a key inside the group in the .desktop).
65 the value is an ObtDDParseValue */
69 /* Displays a warning message including the file name and line number, and
70 sets the boolean @error to true if it points to a non-NULL address.
72 static void parse_error(const gchar *m, const ObtDDParse *const parse,
76 g_warning("%s at line %lu of input", m, parse->lineno);
78 g_warning("%s at line %lu of file %s",
79 m, parse->lineno, parse->filename);
80 if (error) *error = TRUE;
83 static void parse_value_free(ObtDDParseValue *v)
86 case OBT_DDPARSE_EXEC:
87 case OBT_DDPARSE_STRING:
88 case OBT_DDPARSE_LOCALESTRING:
89 g_free(v->value.string); break;
90 case OBT_DDPARSE_STRINGS:
91 case OBT_DDPARSE_LOCALESTRINGS:
92 g_strfreev(v->value.strings.a);
93 v->value.strings.n = 0;
95 case OBT_DDPARSE_BOOLEAN:
96 case OBT_DDPARSE_NUMERIC:
97 case OBT_DDPARSE_ENUM_TYPE:
98 case OBT_DDPARSE_ENVIRONMENTS:
101 g_assert_not_reached();
103 g_slice_free(ObtDDParseValue, v);
106 static ObtDDParseGroup* parse_group_new(gchar *name, ObtDDParseValueFunc f)
108 ObtDDParseGroup *g = g_slice_new(ObtDDParseGroup);
112 g->key_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
114 (GDestroyNotify)parse_value_free);
118 static void parse_group_free(ObtDDParseGroup *g)
121 g_hash_table_destroy(g->key_hash);
122 g_slice_free(ObtDDParseGroup, g);
125 /*! Reads an input string, strips out invalid stuff, and parses
128 static gchar* parse_value_string(const gchar *in,
130 gboolean semicolonterminate,
132 const ObtDDParse *const parse,
138 const gchar *end, *i;
140 /* find the end/size of the string */
142 for (end = in; *end; ++end) {
143 if (semicolonterminate) {
144 if (backslash) backslash = FALSE;
145 else if (*end == '\\') backslash = TRUE;
146 else if (*end == ';') break;
151 g_return_val_if_fail(in != NULL, NULL);
153 if (locale && !g_utf8_validate(in, bytes, &end)) {
154 parse_error("Invalid bytes in localestring", parse, error);
158 out = g_new(char, bytes + 1);
165 /* find the next character in the string */
166 if (!locale) next = i+1;
167 else if (!(next = g_utf8_find_next_char(i, end))) next = end;
171 case 's': *o++ = ' '; break;
172 case 'n': *o++ = '\n'; break;
173 case 't': *o++ = '\t'; break;
174 case 'r': *o++ = '\r'; break;
175 case ';': *o++ = ';'; break;
176 case '\\': *o++ = '\\'; break;
178 parse_error((locale ?
179 "Invalid escape sequence in localestring" :
180 "Invalid escape sequence in string"),
187 else if ((!locale && (guchar)*i >= 127) || (guchar)*i < 32) {
188 /* avoid ascii control characters */
189 parse_error("Found control character in string", parse, error);
193 const gulong s = next-i;
205 /*! Reads a list of input strings, strips out invalid stuff, and parses
208 static gchar** parse_value_strings(const gchar *in,
211 const ObtDDParse *const parse,
217 out = g_new(gchar*, 1);
226 a = parse_value_string(i, locale, TRUE, &len, parse, error);
231 out = g_renew(gchar*, out, *nstrings+1);
232 out[*nstrings-1] = a;
233 out[*nstrings] = NULL;
236 if (!*i) break; /* no more strings */
242 static guint parse_value_environments(const gchar *in,
243 const ObtDDParse *const parse,
253 if (strcmp(s, "NOME") == 0) {
254 mask |= OBT_LINK_ENV_GNOME;
259 if (strcmp(s, "DE") == 0) {
260 mask |= OBT_LINK_ENV_KDE;
265 if (strcmp(s, "XDE") == 0) {
266 mask |= OBT_LINK_ENV_LXDE;
271 if (strcmp(s, "OX") == 0) {
272 mask |= OBT_LINK_ENV_ROX;
277 if (strcmp(s, "FCE") == 0) {
278 mask |= OBT_LINK_ENV_XFCE;
285 if (strcmp(s, "d") == 0) {
286 mask |= OBT_LINK_ENV_OLD;
291 if (strcmp(s, "ENBOX") == 0) {
292 mask |= OBT_LINK_ENV_OPENBOX;
298 /* find the next string, or the end of the sequence */
299 while (*s && *s != ';') ++s;
304 static gboolean parse_value_boolean(const gchar *in,
305 const ObtDDParse *const parse,
308 if (strcmp(in, "true") == 0)
310 else if (strcmp(in, "false") != 0)
311 parse_error("Invalid boolean value", parse, error);
315 static gfloat parse_value_numeric(const gchar *in,
316 const ObtDDParse *const parse,
320 if (sscanf(in, "%f", &out) == 0)
321 parse_error("Invalid numeric value", parse, error);
325 static gboolean parse_file_line(FILE *f, gchar **buf,
326 gulong *size, gulong *read,
327 ObtDDParse *parse, gboolean *error)
329 const gulong BUFMUL = 80;
334 g_assert(*read == 0);
336 *buf = g_new(char, *size);
339 /* remove everything up to a null zero already in the buffer and shift
340 the rest to the front */
342 for (i = 0; i < *read; ++i) {
344 (*buf)[i-null-1] = (*buf)[i];
345 else if ((*buf)[i] == '\0')
351 /* is there already a newline in the buffer? */
352 for (i = 0; i < *read; ++i)
353 if ((*buf)[i] == '\n') {
354 /* turn it into a null zero and done */
359 /* we need to read some more to find a newline */
364 newread = *buf + *read;
365 ret = fread(newread, sizeof(char), *size-*read, f);
366 if (ret < *size - *read && !feof(f)) {
367 parse_error("Error reading", parse, error);
372 /* strip out null zeros in the input and look for an endofline */
375 for (i = newread-*buf; i < *read; ++i) {
377 (*buf)[i] = (*buf)[i+null];
378 if ((*buf)[i] == '\0') {
383 else if ((*buf)[i] == '\n' && eol == *size) {
385 /* turn it into a null zero */
391 /* found an endofline, done */
393 else if (feof(f) && *read < *size) {
394 /* found the endoffile, done (if there is space) */
396 /* stick a null zero on if there is test on the last line */
397 (*buf)[(*read)++] = '\0';
404 *buf = g_renew(char, *buf, *size);
410 static void parse_group(const gchar *buf, gulong len,
411 ObtDDParse *parse, gboolean *error)
417 /* get the group name */
418 group = g_strndup(buf+1, len-2);
419 for (i = 0; i < len-2; ++i)
420 if ((guchar)group[i] < 32 || (guchar)group[i] >= 127) {
421 /* valid ASCII only */
422 parse_error("Invalid character found", parse, NULL);
423 group[i] = '\0'; /* stopping before this character */
427 /* make sure it's a new group */
428 g = g_hash_table_lookup(parse->group_hash, group);
430 parse_error("Duplicate group found", parse, error);
434 /* if it's the first group, make sure it's named Desktop Entry */
435 else if (!parse->group && strcmp(group, "Desktop Entry") != 0)
437 parse_error("Incorrect group found, "
438 "expected [Desktop Entry]",
445 g = parse_group_new(group, NULL);
446 g_hash_table_insert(parse->group_hash, g->name, g);
453 /* g_print("Found group %s\n", g->name); */
457 static void parse_key_value(const gchar *buf, gulong len,
458 ObtDDParse *parse, gboolean *error)
460 gulong i, keyend, valstart, eq;
461 gulong langstart, langend, countrystart, countryend, modstart, modend;
463 ObtDDParseValue *val;
464 ObtDDParseLangMatch match;
466 /* find the end of the key */
467 for (i = 0; i < len; ++i)
468 if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
469 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z') ||
470 ((guchar)buf[i] >= '0' && (guchar)buf[i] <= '9') ||
471 ((guchar)buf[i] == '-'))) {
472 /* not part of the key */
478 parse_error("Empty key", parse, error);
482 /* is there a language specifier? */
483 langstart = langend = countrystart = countryend = modstart = modend = 0;
484 if ((guchar)buf[i] == '[') {
486 for (i = langstart; i < len; ++i)
487 if ((guchar)buf[i] == '.' || (guchar)buf[i] == '_' ||
488 (guchar)buf[i] == '@' || (guchar)buf[i] == ']')
493 else if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
494 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z'))) {
495 parse_error("Invalid character in language", parse, error);
498 if ((guchar)buf[i] == '_') {
500 for (i = i+1; i < len; ++i)
501 if ((guchar)buf[i] == '.' ||
502 (guchar)buf[i] == '@' || (guchar)buf[i] == ']')
507 else if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
508 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z'))) {
509 parse_error("Invalid character in country", parse, error);
513 if ((guchar)buf[i] == '.') {
514 for (i = i+1; i < len; ++i)
515 if ((guchar)buf[i] == '@' || (guchar)buf[i] == ']')
517 else if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
518 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z'))) {
519 parse_error("Invalid character in encoding", parse, error);
523 if ((guchar)buf[i] == '@') {
525 for (i = i+1; i < len; ++i)
526 if ((guchar)buf[i] == ']') {
530 else if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
531 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z'))) {
532 parse_error("Invalid character in locale modifier",
540 /* find the = character */
541 for (; i < len; ++i) {
546 else if (buf[i] != ' ') {
547 parse_error("Invalid character in key name", parse, error);
552 parse_error("Key without value found", parse, error);
555 /* find the start of the value */
556 for (i = eq+1; i < len; ++i)
562 parse_error("Empty value found", parse, error);
566 if (langend < langstart)
567 match = OBT_DDPARSE_MATCH_FAIL;
569 match = OBT_DDPARSE_MATCH_NONE;
570 if (parse->language && langend >= langstart &&
571 strncmp(parse->language, buf+langstart, langend-langstart+1) == 0)
573 match = OBT_DDPARSE_MATCH_LANG;
574 if (parse->country && countryend >= countrystart &&
575 strncmp(parse->country, buf+countrystart,
576 countryend-countrystart+1) == 0)
577 match = OBT_DDPARSE_MATCH_LANG_COUNTRY;
579 if (parse->modifier && modend >= modstart &&
580 strncmp(parse->modifier, buf+modstart,
581 modend-modstart+1) == 0)
582 match += 1; /* its one up for LANG and for LANG_COUNTY */
585 key = g_strndup(buf, keyend);
586 if ((val = g_hash_table_lookup(parse->group->key_hash, key))) {
587 if (val->language_match >= match) {
588 /* found a better match already */
593 /* g_print("Found key/value %s=%s\n", key, buf+valstart); */
594 if (parse->group->value_func)
595 if (!parse->group->value_func(key, buf+valstart, match, parse, error))
597 /*parse_error("Unknown key", parse, error);*/
602 static gboolean parse_file(FILE *f, ObtDDParse *parse)
605 gulong bytes = 0, read = 0;
606 gboolean error = FALSE;
608 while (!error && parse_file_line(f, &buf, &bytes, &read, parse, &error)) {
609 gulong len = strlen(buf);
610 if (buf[0] == '#' || buf[0] == '\0')
611 ; /* ignore comment lines */
612 else if (buf[0] == '[' && buf[len-1] == ']')
613 parse_group(buf, len, parse, &error);
614 else if (!parse->group)
615 /* just ignore keys outside of groups */
616 parse_error("Key found before group", parse, NULL);
618 /* ignore errors in key-value pairs and continue */
619 parse_key_value(buf, len, parse, NULL);
623 if (buf) g_free(buf);
627 static gboolean parse_desktop_entry_value(gchar *key, const gchar *val,
628 ObtDDParseLangMatch match,
629 ObtDDParse *parse, gboolean *error)
631 ObtDDParseValue v, *pv;
633 v.language_match = match;
638 case 'a': /* Categories */
639 if (strcmp(key+2, "tegories")) return FALSE;
640 v.type = OBT_DDPARSE_STRINGS; break;
641 case 'o': /* Comment */
642 if (strcmp(key+2, "mment")) return FALSE;
643 v.type = OBT_DDPARSE_LOCALESTRING; break;
649 if (strcmp(key+1, "xec")) return FALSE;
650 v.type = OBT_DDPARSE_EXEC; parse->flags |= DE_EXEC; break;
651 case 'G': /* GenericName */
652 if (strcmp(key+1, "enericName")) return FALSE;
653 v.type = OBT_DDPARSE_LOCALESTRING; break;
655 if (strcmp(key+1, "con")) return FALSE;
656 v.type = OBT_DDPARSE_LOCALESTRING; break;
657 case 'H': /* Hidden */
658 if (strcmp(key+1, "idden")) return FALSE;
659 v.type = OBT_DDPARSE_BOOLEAN; break;
660 case 'M': /* MimeType */
661 if (strcmp(key+1, "imeType")) return FALSE;
662 v.type = OBT_DDPARSE_STRINGS; break;
666 if (strcmp(key+2, "me")) return FALSE;
667 v.type = OBT_DDPARSE_LOCALESTRING; parse->flags |= DE_NAME; break;
670 case 'D': /* NoDisplay */
671 if (strcmp(key+3, "isplay")) return FALSE;
672 v.type = OBT_DDPARSE_BOOLEAN; break;
673 case 't': /* NotShowIn */
674 if (strcmp(key+3, "ShowIn")) return FALSE;
675 v.type = OBT_DDPARSE_STRINGS; break;
685 if (strcmp(key+1, "ath")) return FALSE;
686 v.type = OBT_DDPARSE_STRING; break;
688 if (key[1] == 't' && key[2] == 'a' && key[3] == 'r' &&
689 key[4] == 't' && key[5] == 'u' && key[6] == 'p')
691 case 'N': /* StartupNotify */
692 if (strcmp(key+8, "otify")) return FALSE;
693 v.type = OBT_DDPARSE_BOOLEAN; break;
694 case 'W': /* StartupWMClass */
695 if (strcmp(key+8, "MClass")) return FALSE;
696 v.type = OBT_DDPARSE_STRING; break;
705 case 'e': /* Terminal */
706 if (strcmp(key+2, "rminal")) return FALSE;
707 v.type = OBT_DDPARSE_BOOLEAN; break;
708 case 'r': /* TryExec */
709 if (strcmp(key+2, "yExec")) return FALSE;
710 v.type = OBT_DDPARSE_STRING; break;
712 if (strcmp(key+2, "pe")) return FALSE;
713 v.type = OBT_DDPARSE_ENUM_TYPE; parse->flags |= DE_TYPE; break;
719 if (strcmp(key+1, "RL")) return FALSE;
720 v.type = OBT_DDPARSE_STRING; parse->flags |= DE_URL; break;
721 case 'V': /* MimeType */
722 if (strcmp(key+1, "ersion")) return FALSE;
723 v.type = OBT_DDPARSE_STRING; break;
728 if (v.language_match && !(v.type == OBT_DDPARSE_LOCALESTRING ||
729 v.type == OBT_DDPARSE_LOCALESTRINGS))
731 parse_error("Invalid localization on key", parse, error);
735 /* parse the value */
737 case OBT_DDPARSE_EXEC: {
742 v.value.string = parse_value_string(val, FALSE, FALSE, NULL,
744 g_assert(v.value.string);
746 /* an exec string can only contain one of the file/url-opening %'s */
747 percent = found = FALSE;
748 for (c = v.value.string; *c; ++c) {
756 m = g_strdup_printf("Malformed Exec key, "
757 "extraneous %%%c", *c);
758 parse_error(m, parse, error);
769 m = g_strdup_printf("Malformed Exec key, "
770 "uses deprecated %%%c", *c);
771 parse_error(m, parse, NULL); /* just a warning */
780 m = g_strdup_printf("Malformed Exec key, "
781 "uses unknown %%%c", *c);
782 parse_error(m, parse, NULL); /* just a warning */
787 else if (*c == '%') percent = TRUE;
791 case OBT_DDPARSE_STRING:
792 v.value.string = parse_value_string(val, FALSE, FALSE, NULL,
794 g_assert(v.value.string);
796 case OBT_DDPARSE_LOCALESTRING:
797 v.value.string = parse_value_string(val, TRUE, FALSE, NULL,
799 g_assert(v.value.string);
801 case OBT_DDPARSE_STRINGS:
802 v.value.strings.a = parse_value_strings(val, FALSE, &v.value.strings.n,
804 g_assert(v.value.strings.a);
805 g_assert(v.value.strings.n);
807 case OBT_DDPARSE_LOCALESTRINGS:
808 v.value.strings.a = parse_value_strings(val, TRUE, &v.value.strings.n,
810 g_assert(v.value.strings.a);
811 g_assert(v.value.strings.n);
813 case OBT_DDPARSE_BOOLEAN:
814 v.value.boolean = parse_value_boolean(val, parse, error);
816 case OBT_DDPARSE_NUMERIC:
817 v.value.numeric = parse_value_numeric(val, parse, error);
819 case OBT_DDPARSE_ENUM_TYPE:
820 if (val[0] == 'A' && strcmp(val+1, "pplication") == 0) {
821 v.value.enumerable = OBT_LINK_TYPE_APPLICATION;
822 parse->flags |= DE_TYPE_APPLICATION;
824 else if (val[0] == 'L' && strcmp(val+1, "ink") == 0) {
825 v.value.enumerable = OBT_LINK_TYPE_URL;
826 parse->flags |= DE_TYPE_LINK;
828 else if (val[0] == 'D' && strcmp(val+1, "irectory") == 0)
829 v.value.enumerable = OBT_LINK_TYPE_DIRECTORY;
831 parse_error("Unknown Type", parse, error);
835 case OBT_DDPARSE_ENVIRONMENTS:
836 v.value.environments = parse_value_environments(val, parse, error);
839 g_assert_not_reached();
842 pv = g_slice_new(ObtDDParseValue);
844 g_hash_table_insert(parse->group->key_hash, key, pv);
848 GHashTable* obt_ddparse_file(const gchar *filename,
849 const gchar *language,
850 const gchar *country,
851 const gchar *modifier)
854 ObtDDParseGroup *desktop_entry;
858 if (!g_utf8_validate(filename, -1, NULL)) {
859 g_warning("Filename contains bad utf8: %s", filename);
863 parse.filename = filename;
864 parse.language = language;
865 parse.country = country;
866 parse.modifier = modifier;
869 parse.group_hash = g_hash_table_new_full(g_str_hash,
872 (GDestroyNotify)parse_group_free);
874 /* set up the groups (there's only one right now) */
875 desktop_entry = parse_group_new(g_strdup("Desktop Entry"),
876 parse_desktop_entry_value);
877 g_hash_table_insert(parse.group_hash, desktop_entry->name, desktop_entry);
880 if ((f = fopen(parse.filename, "r"))) {
883 if ((success = parse_file(f, &parse))) {
884 /* check that required keys exist */
886 if (!(parse.flags & DE_TYPE)) {
887 g_warning("Missing Type key in %s", parse.filename);
890 if (!(parse.flags & DE_NAME)) {
891 g_warning("Missing Name key in %s", parse.filename);
894 if (parse.flags & DE_TYPE_APPLICATION &&
895 !(parse.flags & DE_EXEC))
897 g_warning("Missing Exec key for Application in %s",
901 else if (parse.flags & DE_TYPE_LINK && !(parse.flags & DE_URL))
903 g_warning("Missing URL key for Link in %s", parse.filename);
910 g_hash_table_destroy(parse.group_hash);
911 parse.group_hash = NULL;
913 return parse.group_hash;
916 GHashTable* obt_ddparse_group_keys(ObtDDParseGroup *g)
921 gchar* obt_ddparse_file_to_id(const gchar *filename)
925 gchar *out, *out_start;
928 if (!g_utf8_validate(filename, -1, NULL)) {
929 g_warning("Filename contains bad utf8: %s", filename);
933 len = strlen(filename) - 8; /* 8 = strlen(".desktop") */
934 g_assert(strcmp(filename+len, ".desktop") == 0);
936 out_start = out = g_new(char, len+1);
938 for (in = filename; in < filename + len; ) {
942 /* path separators becomes dashes */
951 /* everything else is copied as is */
952 next = g_utf8_next_char(in);