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_debug("%s at line %lu of input", m, parse->lineno);
78 g_debug("%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 (strncmp(s, "NOME", 4) == 0 ||
254 strncmp(s, "NOME;", 5 == 0))
256 mask |= OBT_LINK_ENV_GNOME;
261 if (strncmp(s, "DE", 2) == 0 ||
262 strncmp(s, "DE;", 3) == 0)
264 mask |= OBT_LINK_ENV_KDE;
269 if (strncmp(s, "XDE", 3) == 0 ||
270 strncmp(s, "XDE;", 4) == 0)
272 mask |= OBT_LINK_ENV_LXDE;
277 if (strncmp(s, "OX", 2) == 0 ||
278 strncmp(s, "OX;", 3) == 0)
280 mask |= OBT_LINK_ENV_ROX;
285 if (strncmp(s, "FCE", 3) == 0 ||
286 strncmp(s, "FCE;", 4) == 0)
288 mask |= OBT_LINK_ENV_XFCE;
295 if (strncmp(s, "d", 1) == 0 ||
296 strncmp(s, "d;", 2) == 0)
298 mask |= OBT_LINK_ENV_OLD;
303 if (strncmp(s, "ENBOX", 5) == 0 ||
304 strncmp(s, "ENBOX;", 6) == 0)
306 mask |= OBT_LINK_ENV_OPENBOX;
312 /* find the next string, or the end of the sequence */
313 while (*s && *s != ';') ++s;
318 static gboolean parse_value_boolean(const gchar *in,
319 const ObtDDParse *const parse,
322 if (strcmp(in, "true") == 0)
324 else if (strcmp(in, "false") != 0)
325 parse_error("Invalid boolean value", parse, error);
329 static gfloat parse_value_numeric(const gchar *in,
330 const ObtDDParse *const parse,
334 if (sscanf(in, "%f", &out) == 0)
335 parse_error("Invalid numeric value", parse, error);
339 static gboolean parse_file_line(FILE *f, gchar **buf,
340 gulong *size, gulong *read,
341 ObtDDParse *parse, gboolean *error)
343 const gulong BUFMUL = 80;
348 g_assert(*read == 0);
350 *buf = g_new(char, *size);
353 /* remove everything up to a null zero already in the buffer and shift
354 the rest to the front */
356 for (i = 0; i < *read; ++i) {
358 (*buf)[i-null-1] = (*buf)[i];
359 else if ((*buf)[i] == '\0')
365 /* is there already a newline in the buffer? */
366 for (i = 0; i < *read; ++i)
367 if ((*buf)[i] == '\n') {
368 /* turn it into a null zero and done */
373 /* we need to read some more to find a newline */
378 newread = *buf + *read;
379 ret = fread(newread, sizeof(char), *size-*read, f);
380 if (ret < *size - *read && !feof(f)) {
381 parse_error("Error reading", parse, error);
386 /* strip out null zeros in the input and look for an endofline */
389 for (i = newread-*buf; i < *read; ++i) {
391 (*buf)[i] = (*buf)[i+null];
392 if ((*buf)[i] == '\0') {
397 else if ((*buf)[i] == '\n' && eol == *size) {
399 /* turn it into a null zero */
405 /* found an endofline, done */
407 else if (feof(f) && *read < *size) {
408 /* found the endoffile, done (if there is space) */
410 /* stick a null zero on if there is test on the last line */
411 (*buf)[(*read)++] = '\0';
418 *buf = g_renew(char, *buf, *size);
424 static void parse_group(const gchar *buf, gulong len,
425 ObtDDParse *parse, gboolean *error)
431 /* get the group name */
432 group = g_strndup(buf+1, len-2);
433 for (i = 0; i < len-2; ++i)
434 if ((guchar)group[i] < 32 || (guchar)group[i] >= 127) {
435 /* valid ASCII only */
436 parse_error("Invalid character found", parse, NULL);
437 group[i] = '\0'; /* stopping before this character */
441 /* make sure it's a new group */
442 g = g_hash_table_lookup(parse->group_hash, group);
444 parse_error("Duplicate group found", parse, error);
448 /* if it's the first group, make sure it's named Desktop Entry */
449 else if (!parse->group && strcmp(group, "Desktop Entry") != 0)
451 parse_error("Incorrect group found, "
452 "expected [Desktop Entry]",
459 g = parse_group_new(group, NULL);
460 g_hash_table_insert(parse->group_hash, g->name, g);
467 /* g_print("Found group %s\n", g->name); */
471 static void parse_key_value(const gchar *buf, gulong len,
472 ObtDDParse *parse, gboolean *error)
474 gulong i, keyend, valstart, eq;
475 gulong langstart, langend, countrystart, countryend, modstart, modend;
477 ObtDDParseValue *val;
478 ObtDDParseLangMatch match;
480 /* find the end of the key */
481 for (i = 0; i < len; ++i)
482 if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
483 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z') ||
484 ((guchar)buf[i] >= '0' && (guchar)buf[i] <= '9') ||
485 ((guchar)buf[i] == '-'))) {
486 /* not part of the key */
492 parse_error("Empty key", parse, error);
496 /* is there a language specifier? */
497 langstart = langend = countrystart = countryend = modstart = modend = 0;
498 if ((guchar)buf[i] == '[') {
500 for (i = langstart; i < len; ++i)
501 if ((guchar)buf[i] == '.' || (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 language", parse, error);
512 if ((guchar)buf[i] == '_') {
514 for (i = i+1; i < len; ++i)
515 if ((guchar)buf[i] == '.' ||
516 (guchar)buf[i] == '@' || (guchar)buf[i] == ']')
521 else if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
522 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z'))) {
523 parse_error("Invalid character in country", parse, error);
527 if ((guchar)buf[i] == '.') {
528 for (i = i+1; i < len; ++i)
529 if ((guchar)buf[i] == '@' || (guchar)buf[i] == ']')
531 else if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
532 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z'))) {
533 parse_error("Invalid character in encoding", parse, error);
537 if ((guchar)buf[i] == '@') {
539 for (i = i+1; i < len; ++i)
540 if ((guchar)buf[i] == ']') {
544 else if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
545 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z'))) {
546 parse_error("Invalid character in locale modifier",
554 /* find the = character */
555 for (; i < len; ++i) {
560 else if (buf[i] != ' ') {
561 parse_error("Invalid character in key name", parse, error);
566 parse_error("Key without value found", parse, error);
569 /* find the start of the value */
570 for (i = eq+1; i < len; ++i)
576 parse_error("Empty value found", parse, error);
580 if (langend < langstart)
581 match = OBT_DDPARSE_MATCH_FAIL;
583 match = OBT_DDPARSE_MATCH_NONE;
584 if (parse->language && langend >= langstart &&
585 strncmp(parse->language, buf+langstart, langend-langstart+1) == 0)
587 match = OBT_DDPARSE_MATCH_LANG;
588 if (parse->country && countryend >= countrystart &&
589 strncmp(parse->country, buf+countrystart,
590 countryend-countrystart+1) == 0)
591 match = OBT_DDPARSE_MATCH_LANG_COUNTRY;
593 if (parse->modifier && modend >= modstart &&
594 strncmp(parse->modifier, buf+modstart,
595 modend-modstart+1) == 0)
596 match += 1; /* its one up for LANG and for LANG_COUNTY */
599 key = g_strndup(buf, keyend);
600 if ((val = g_hash_table_lookup(parse->group->key_hash, key))) {
601 if (val->language_match >= match) {
602 /* found a better match already */
607 /* g_print("Found key/value %s=%s\n", key, buf+valstart); */
608 if (parse->group->value_func)
609 if (!parse->group->value_func(key, buf+valstart, match, parse, error))
611 /*parse_error("Unknown key", parse, error);*/
616 static gboolean parse_file(FILE *f, ObtDDParse *parse)
619 gulong bytes = 0, read = 0;
620 gboolean error = FALSE;
622 while (!error && parse_file_line(f, &buf, &bytes, &read, parse, &error)) {
623 gulong len = strlen(buf);
624 if (buf[0] == '#' || buf[0] == '\0')
625 ; /* ignore comment lines */
626 else if (buf[0] == '[' && buf[len-1] == ']')
627 parse_group(buf, len, parse, &error);
628 else if (!parse->group)
629 /* just ignore keys outside of groups */
630 parse_error("Key found before group", parse, NULL);
632 /* ignore errors in key-value pairs and continue */
633 parse_key_value(buf, len, parse, NULL);
637 if (buf) g_free(buf);
641 static gboolean parse_desktop_entry_value(gchar *key, const gchar *val,
642 ObtDDParseLangMatch match,
643 ObtDDParse *parse, gboolean *error)
645 ObtDDParseValue v, *pv;
647 v.language_match = match;
652 case 'a': /* Categories */
653 if (strcmp(key+2, "tegories")) return FALSE;
654 v.type = OBT_DDPARSE_STRINGS; break;
655 case 'o': /* Comment */
656 if (strcmp(key+2, "mment")) return FALSE;
657 v.type = OBT_DDPARSE_LOCALESTRING; break;
663 if (strcmp(key+1, "xec")) return FALSE;
664 v.type = OBT_DDPARSE_EXEC; parse->flags |= DE_EXEC; break;
665 case 'G': /* GenericName */
666 if (strcmp(key+1, "enericName")) return FALSE;
667 v.type = OBT_DDPARSE_LOCALESTRING; break;
669 if (strcmp(key+1, "con")) return FALSE;
670 v.type = OBT_DDPARSE_LOCALESTRING; break;
671 case 'H': /* Hidden */
672 if (strcmp(key+1, "idden")) return FALSE;
673 v.type = OBT_DDPARSE_BOOLEAN; break;
674 case 'M': /* MimeType */
675 if (strcmp(key+1, "imeType")) return FALSE;
676 v.type = OBT_DDPARSE_STRINGS; break;
680 if (strcmp(key+2, "me")) return FALSE;
681 v.type = OBT_DDPARSE_LOCALESTRING; parse->flags |= DE_NAME; break;
684 case 'D': /* NoDisplay */
685 if (strcmp(key+3, "isplay")) return FALSE;
686 v.type = OBT_DDPARSE_BOOLEAN; break;
687 case 't': /* NotShowIn */
688 if (strcmp(key+3, "ShowIn")) return FALSE;
689 v.type = OBT_DDPARSE_ENVIRONMENTS; break;
698 case 'O': /* OnlyShowIn */
699 if (strcmp(key+1, "nlyShowIn")) return FALSE;
700 v.type = OBT_DDPARSE_ENVIRONMENTS; break;
702 if (strcmp(key+1, "ath")) return FALSE;
703 v.type = OBT_DDPARSE_STRING; break;
705 if (key[1] == 't' && key[2] == 'a' && key[3] == 'r' &&
706 key[4] == 't' && key[5] == 'u' && key[6] == 'p')
708 case 'N': /* StartupNotify */
709 if (strcmp(key+8, "otify")) return FALSE;
710 v.type = OBT_DDPARSE_BOOLEAN; break;
711 case 'W': /* StartupWMClass */
712 if (strcmp(key+8, "MClass")) return FALSE;
713 v.type = OBT_DDPARSE_STRING; break;
722 case 'e': /* Terminal */
723 if (strcmp(key+2, "rminal")) return FALSE;
724 v.type = OBT_DDPARSE_BOOLEAN; break;
725 case 'r': /* TryExec */
726 if (strcmp(key+2, "yExec")) return FALSE;
727 v.type = OBT_DDPARSE_STRING; break;
729 if (strcmp(key+2, "pe")) return FALSE;
730 v.type = OBT_DDPARSE_ENUM_TYPE; parse->flags |= DE_TYPE; break;
736 if (strcmp(key+1, "RL")) return FALSE;
737 v.type = OBT_DDPARSE_STRING; parse->flags |= DE_URL; break;
738 case 'V': /* MimeType */
739 if (strcmp(key+1, "ersion")) return FALSE;
740 v.type = OBT_DDPARSE_STRING; break;
745 if (v.language_match && !(v.type == OBT_DDPARSE_LOCALESTRING ||
746 v.type == OBT_DDPARSE_LOCALESTRINGS))
748 parse_error("Invalid localization on key", parse, error);
752 /* parse the value */
754 case OBT_DDPARSE_EXEC: {
759 v.value.string = parse_value_string(val, FALSE, FALSE, NULL,
761 g_assert(v.value.string);
763 /* an exec string can only contain one of the file/url-opening %'s */
764 percent = found = FALSE;
765 for (c = v.value.string; *c; ++c) {
773 m = g_strdup_printf("Malformed Exec key, "
774 "extraneous %%%c", *c);
775 parse_error(m, parse, error);
786 m = g_strdup_printf("Malformed Exec key, "
787 "uses deprecated %%%c", *c);
788 parse_error(m, parse, NULL); /* just a warning */
797 m = g_strdup_printf("Malformed Exec key, "
798 "uses unknown %%%c", *c);
799 parse_error(m, parse, NULL); /* just a warning */
804 else if (*c == '%') percent = TRUE;
808 case OBT_DDPARSE_STRING:
809 v.value.string = parse_value_string(val, FALSE, FALSE, NULL,
811 g_assert(v.value.string);
813 case OBT_DDPARSE_LOCALESTRING:
814 v.value.string = parse_value_string(val, TRUE, FALSE, NULL,
816 g_assert(v.value.string);
818 case OBT_DDPARSE_STRINGS:
819 v.value.strings.a = parse_value_strings(val, FALSE, &v.value.strings.n,
821 g_assert(v.value.strings.a);
822 g_assert(v.value.strings.n);
824 case OBT_DDPARSE_LOCALESTRINGS:
825 v.value.strings.a = parse_value_strings(val, TRUE, &v.value.strings.n,
827 g_assert(v.value.strings.a);
828 g_assert(v.value.strings.n);
830 case OBT_DDPARSE_BOOLEAN:
831 v.value.boolean = parse_value_boolean(val, parse, error);
833 case OBT_DDPARSE_NUMERIC:
834 v.value.numeric = parse_value_numeric(val, parse, error);
836 case OBT_DDPARSE_ENUM_TYPE:
837 if (val[0] == 'A' && strcmp(val+1, "pplication") == 0) {
838 v.value.enumerable = OBT_LINK_TYPE_APPLICATION;
839 parse->flags |= DE_TYPE_APPLICATION;
841 else if (val[0] == 'L' && strcmp(val+1, "ink") == 0) {
842 v.value.enumerable = OBT_LINK_TYPE_URL;
843 parse->flags |= DE_TYPE_LINK;
845 else if (val[0] == 'D' && strcmp(val+1, "irectory") == 0)
846 v.value.enumerable = OBT_LINK_TYPE_DIRECTORY;
848 parse_error("Unknown Type", parse, error);
852 case OBT_DDPARSE_ENVIRONMENTS:
853 v.value.environments = parse_value_environments(val, parse, error);
856 g_assert_not_reached();
859 pv = g_slice_new(ObtDDParseValue);
861 g_hash_table_insert(parse->group->key_hash, key, pv);
865 GHashTable* obt_ddparse_file(const gchar *filename,
866 const gchar *language,
867 const gchar *country,
868 const gchar *modifier)
871 ObtDDParseGroup *desktop_entry;
876 if (!g_utf8_validate(filename, -1, NULL)) {
877 g_warning("Filename contains bad utf8: %s", filename);
881 parse.filename = filename;
882 parse.language = language;
883 parse.country = country;
884 parse.modifier = modifier;
887 parse.group_hash = g_hash_table_new_full(g_str_hash,
890 (GDestroyNotify)parse_group_free);
892 /* set up the groups (there's only one right now) */
893 desktop_entry = parse_group_new(g_strdup("Desktop Entry"),
894 parse_desktop_entry_value);
895 g_hash_table_insert(parse.group_hash, desktop_entry->name, desktop_entry);
898 fs_filename = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
899 if ((f = fopen(fs_filename, "r"))) {
902 if ((success = parse_file(f, &parse))) {
903 /* check that required keys exist */
905 if (!(parse.flags & DE_TYPE)) {
906 g_warning("Missing Type key in %s", parse.filename);
909 if (!(parse.flags & DE_NAME)) {
910 g_warning("Missing Name key in %s", parse.filename);
913 if (parse.flags & DE_TYPE_APPLICATION &&
914 !(parse.flags & DE_EXEC))
916 g_warning("Missing Exec key for Application in %s",
920 else if (parse.flags & DE_TYPE_LINK && !(parse.flags & DE_URL))
922 g_warning("Missing URL key for Link in %s", parse.filename);
930 g_hash_table_destroy(parse.group_hash);
931 parse.group_hash = NULL;
933 return parse.group_hash;
936 GHashTable* obt_ddparse_group_keys(ObtDDParseGroup *g)
941 gchar* obt_ddparse_file_to_id(const gchar *filename)
945 gchar *out, *out_start;
948 if (!g_utf8_validate(filename, -1, NULL)) {
949 g_warning("Filename contains bad utf8: %s", filename);
953 len = strlen(filename) - 8; /* 8 = strlen(".desktop") */
954 g_assert(strcmp(filename+len, ".desktop") == 0);
956 out_start = out = g_new(char, len+1);
958 for (in = filename; in < filename + len; ) {
962 /* path separators becomes dashes */
971 /* everything else is copied as is */
972 next = g_utf8_next_char(in);