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 ObtDDParse *parse, gboolean *error);
41 ObtDDParseGroup *group;
42 /* the key is a group name, the value is a ObtDDParseGroup */
43 GHashTable *group_hash;
46 struct _ObtDDParseGroup {
49 ObtDDParseValueFunc value_func;
50 /* the key is a string (a key inside the group in the .desktop).
51 the value is an ObtDDParseValue */
55 /* Displays a warning message including the file name and line number, and
56 sets the boolean @error to true if it points to a non-NULL address.
58 static void parse_error(const gchar *m, const ObtDDParse *const parse,
62 g_warning("%s at line %lu of input", m, parse->lineno);
64 g_warning("%s at line %lu of file %s",
65 m, parse->lineno, parse->filename);
66 if (error) *error = TRUE;
69 static void parse_value_free(ObtDDParseValue *v)
72 case OBT_DDPARSE_STRING:
73 case OBT_DDPARSE_LOCALESTRING:
74 g_free(v->value.string); break;
75 case OBT_DDPARSE_STRINGS:
76 case OBT_DDPARSE_LOCALESTRINGS:
77 g_free(v->value.strings.s);
78 v->value.strings.n = 0;
80 case OBT_DDPARSE_BOOLEAN:
81 case OBT_DDPARSE_NUMERIC:
82 case OBT_DDPARSE_ENUM_APPLICATION:
83 case OBT_DDPARSE_ENVIRONMENTS:
86 g_assert_not_reached();
88 g_slice_free(ObtDDParseValue, v);
91 static ObtDDParseGroup* parse_group_new(gchar *name, ObtDDParseValueFunc f)
93 ObtDDParseGroup *g = g_slice_new(ObtDDParseGroup);
97 g->key_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
99 (GDestroyNotify)parse_value_free);
103 static void parse_group_free(ObtDDParseGroup *g)
106 g_hash_table_destroy(g->key_hash);
107 g_slice_free(ObtDDParseGroup, g);
110 /*! Reads an input string, strips out invalid stuff, and parses
112 If @nstrings is not NULL, then it splits the output string at ';'
113 characters. They are all returned in the same string with null zeros
114 between them, @nstrings is set to the number of such strings.
116 static gchar* parse_value_string(const gchar *in,
119 const ObtDDParse *const parse,
122 const gint bytes = strlen(in);
125 const gchar *end, *i;
127 g_return_val_if_fail(in != NULL, NULL);
131 for (i = in; i < end; ++i) {
132 if ((guchar)*i >= 127 || (guchar)*i < 32) {
133 /* non-control character ascii */
135 parse_error("Invalid bytes in string", parse, error);
140 else if (!g_utf8_validate(in, bytes, &end))
141 parse_error("Invalid bytes in localestring", parse, error);
143 if (nstrings) *nstrings = 1;
145 out = g_new(char, bytes + 1);
149 const gchar *next = locale ? g_utf8_find_next_char(i, end) : i+1;
152 case 's': *o++ = ' '; break;
153 case 'n': *o++ = '\n'; break;
154 case 't': *o++ = '\t'; break;
155 case 'r': *o++ = '\r'; break;
156 case ';': *o++ = ';'; break;
157 case '\\': *o++ = '\\'; break;
159 parse_error((locale ?
160 "Invalid escape sequence in localestring" :
161 "Invalid escape sequence in string"),
168 else if (*i == ';' && nstrings) {
172 else if ((guchar)*i == 127 || (guchar)*i < 32) {
173 /* avoid ascii control characters */
174 parse_error("Found control character in string", parse, error);
178 memcpy(o, i, next-i);
187 static guint parse_value_environments(const gchar *in,
188 const ObtDDParse *const parse,
199 if (strcmp(s, "NOME") == 0) {
200 mask |= OBT_LINK_ENV_GNOME;
205 if (strcmp(s, "DE") == 0) {
206 mask |= OBT_LINK_ENV_KDE;
211 if (strcmp(s, "XDE") == 0) {
212 mask |= OBT_LINK_ENV_LXDE;
217 if (strcmp(s, "OX") == 0) {
218 mask |= OBT_LINK_ENV_ROX;
223 if (strcmp(s, "FCE") == 0) {
224 mask |= OBT_LINK_ENV_XFCE;
231 if (strcmp(s, "d") == 0) {
232 mask |= OBT_LINK_ENV_OLD;
237 if (strcmp(s, "ENBOX") == 0) {
238 mask |= OBT_LINK_ENV_OPENBOX;
244 /* find the next string, or the end of the sequence */
245 while (*s && *s != ';') ++s;
250 static gboolean parse_value_boolean(const gchar *in,
251 const ObtDDParse *const parse,
254 if (strcmp(in, "true") == 0)
256 else if (strcmp(in, "false") != 0)
257 parse_error("Invalid boolean value", parse, error);
261 static gfloat parse_value_numeric(const gchar *in,
262 const ObtDDParse *const parse,
266 if (sscanf(in, "%f", &out) == 0)
267 parse_error("Invalid numeric value", parse, error);
271 static gboolean parse_file_line(FILE *f, gchar **buf,
272 gulong *size, gulong *read,
273 ObtDDParse *parse, gboolean *error)
275 const gulong BUFMUL = 80;
280 g_assert(*read == 0);
282 *buf = g_new(char, *size);
285 /* remove everything up to a null zero already in the buffer and shift
286 the rest to the front */
288 for (i = 0; i < *read; ++i) {
290 (*buf)[i-null-1] = (*buf)[i];
291 else if ((*buf)[i] == '\0')
297 /* is there already a newline in the buffer? */
298 for (i = 0; i < *read; ++i)
299 if ((*buf)[i] == '\n') {
300 /* turn it into a null zero and done */
305 /* we need to read some more to find a newline */
310 newread = *buf + *read;
311 ret = fread(newread, sizeof(char), *size-*read, f);
312 if (ret < *size - *read && !feof(f)) {
313 parse_error("Error reading", parse, error);
318 /* strip out null zeros in the input and look for an endofline */
321 for (i = newread-*buf; i < *read; ++i) {
323 (*buf)[i] = (*buf)[i+null];
324 if ((*buf)[i] == '\0') {
329 else if ((*buf)[i] == '\n' && eol == *size) {
331 /* turn it into a null zero */
337 /* found an endofline, done */
339 else if (feof(f) && *read < *size) {
340 /* found the endoffile, done (if there is space) */
342 /* stick a null zero on if there is test on the last line */
343 (*buf)[(*read)++] = '\0';
350 *buf = g_renew(char, *buf, *size);
356 static void parse_group(const gchar *buf, gulong len,
357 ObtDDParse *parse, gboolean *error)
363 /* get the group name */
364 group = g_strndup(buf+1, len-2);
365 for (i = 0; i < len-2; ++i)
366 if ((guchar)group[i] < 32 || (guchar)group[i] >= 127) {
367 /* valid ASCII only */
368 parse_error("Invalid character found", parse, NULL);
369 group[i] = '\0'; /* stopping before this character */
373 /* make sure it's a new group */
374 g = g_hash_table_lookup(parse->group_hash, group);
376 parse_error("Duplicate group found", parse, error);
380 /* if it's the first group, make sure it's named Desktop Entry */
381 else if (!parse->group && strcmp(group, "Desktop Entry") != 0)
383 parse_error("Incorrect group found, "
384 "expected [Desktop Entry]",
391 g = parse_group_new(group, NULL);
392 g_hash_table_insert(parse->group_hash, g->name, g);
399 g_print("Found group %s\n", g->name);
403 static void parse_key_value(const gchar *buf, gulong len,
404 ObtDDParse *parse, gboolean *error)
406 gulong i, keyend, valstart, eq;
409 /* find the end of the key */
410 for (i = 0; i < len; ++i)
411 if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
412 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z') ||
413 ((guchar)buf[i] >= '0' && (guchar)buf[i] <= '9') ||
414 ((guchar)buf[i] == '-'))) {
415 /* not part of the key */
420 parse_error("Empty key", parse, error);
423 /* find the = character */
424 for (i = keyend; i < len; ++i) {
429 else if (buf[i] != ' ') {
430 parse_error("Invalid character in key name", parse, error);
435 parse_error("Key without value found", parse, error);
438 /* find the start of the value */
439 for (i = eq+1; i < len; ++i)
445 parse_error("Empty value found", parse, error);
449 key = g_strndup(buf, keyend);
450 if (g_hash_table_lookup(parse->group->key_hash, key)) {
451 parse_error("Duplicate key found", parse, error);
455 g_print("Found key/value %s=%s.\n", key, buf+valstart);
456 if (parse->group->value_func)
457 if (!parse->group->value_func(key, buf+valstart, parse, error)) {
458 parse_error("Unknown key", parse, error);
463 static gboolean parse_file(FILE *f, ObtDDParse *parse)
466 gulong bytes = 0, read = 0;
467 gboolean error = FALSE;
469 while (!error && parse_file_line(f, &buf, &bytes, &read, parse, &error)) {
470 gulong len = strlen(buf);
471 if (buf[0] == '#' || buf[0] == '\0')
472 ; /* ignore comment lines */
473 else if (buf[0] == '[' && buf[len-1] == ']')
474 parse_group(buf, len, parse, &error);
475 else if (!parse->group)
476 /* just ignore keys outside of groups */
477 parse_error("Key found before group", parse, NULL);
479 /* ignore errors in key-value pairs and continue */
480 parse_key_value(buf, len, parse, NULL);
484 if (buf) g_free(buf);
488 static gboolean parse_desktop_entry_value(gchar *key, const gchar *val,
489 ObtDDParse *parse, gboolean *error)
491 ObtDDParseValue v, *pv;
496 case 'a': /* Categories */
497 if (strcmp(key+2, "tegories")) return FALSE;
498 v.type = OBT_DDPARSE_STRINGS; break;
499 case 'o': /* Comment */
500 if (strcmp(key+2, "mment")) return FALSE;
501 v.type = OBT_DDPARSE_LOCALESTRING; break;
507 if (strcmp(key+1, "xec")) return FALSE;
508 v.type = OBT_DDPARSE_STRING; break;
509 case 'G': /* GenericName */
510 if (strcmp(key+1, "enericName")) return FALSE;
511 v.type = OBT_DDPARSE_LOCALESTRING; break;
513 if (strcmp(key+1, "con")) return FALSE;
514 v.type = OBT_DDPARSE_LOCALESTRING; break;
515 case 'H': /* Hidden */
516 if (strcmp(key+1, "idden")) return FALSE;
517 v.type = OBT_DDPARSE_BOOLEAN; break;
518 case 'M': /* MimeType */
519 if (strcmp(key+1, "imeType")) return FALSE;
520 v.type = OBT_DDPARSE_STRINGS; break;
524 if (strcmp(key+2, "me")) return FALSE;
525 v.type = OBT_DDPARSE_LOCALESTRING; break;
528 case 'D': /* NoDisplay */
529 if (strcmp(key+3, "isplay")) return FALSE;
530 v.type = OBT_DDPARSE_BOOLEAN; break;
531 case 't': /* NotShowIn */
532 if (strcmp(key+3, "ShowIn")) return FALSE;
533 v.type = OBT_DDPARSE_STRINGS; break;
543 if (strcmp(key+1, "ath")) return FALSE;
544 v.type = OBT_DDPARSE_STRING; break;
546 if (key[1] == 't' && key[2] == 'a' && key[3] == 'r' &&
547 key[4] == 't' && key[5] == 'u' && key[6] == 'p')
549 case 'N': /* StartupNotify */
550 if (strcmp(key+8, "otify")) return FALSE;
551 v.type = OBT_DDPARSE_BOOLEAN; break;
552 case 'W': /* StartupWMClass */
553 if (strcmp(key+8, "MClass")) return FALSE;
554 v.type = OBT_DDPARSE_STRING; break;
563 case 'e': /* Terminal */
564 if (strcmp(key+2, "rminal")) return FALSE;
565 v.type = OBT_DDPARSE_BOOLEAN; break;
566 case 'r': /* TryExec */
567 if (strcmp(key+2, "yExec")) return FALSE;
568 v.type = OBT_DDPARSE_STRING; break;
570 if (strcmp(key+2, "pe")) return FALSE;
571 v.type = OBT_DDPARSE_STRING; break;
577 if (strcmp(key+1, "RL")) return FALSE;
578 v.type = OBT_DDPARSE_STRING; break;
579 case 'V': /* MimeType */
580 if (strcmp(key+1, "ersion")) return FALSE;
581 v.type = OBT_DDPARSE_STRING; break;
586 /* parse the value */
588 case OBT_DDPARSE_STRING:
589 v.value.string = parse_value_string(val, FALSE, NULL, parse, error);
590 g_assert(v.value.string);
592 case OBT_DDPARSE_LOCALESTRING:
593 v.value.string = parse_value_string(val, TRUE, NULL, parse, error);
594 g_assert(v.value.string);
596 case OBT_DDPARSE_STRINGS:
597 v.value.strings.s = parse_value_string(val, FALSE, &v.value.strings.n,
599 g_assert(v.value.strings.s);
600 g_assert(v.value.strings.n);
602 case OBT_DDPARSE_LOCALESTRINGS:
603 v.value.strings.s = parse_value_string(val, TRUE, &v.value.strings.n,
605 g_assert(v.value.strings.s);
606 g_assert(v.value.strings.n);
608 case OBT_DDPARSE_BOOLEAN:
609 v.value.boolean = parse_value_boolean(val, parse, error);
611 case OBT_DDPARSE_NUMERIC:
612 v.value.numeric = parse_value_numeric(val, parse, error);
614 case OBT_DDPARSE_ENUM_APPLICATION:
615 if (val[0] == 'A' && strcmp(val+1, "pplication") == 0)
616 v.value.enumerable = OBT_LINK_TYPE_APPLICATION;
617 else if (val[0] == 'L' && strcmp(val+1, "ink") == 0)
618 v.value.enumerable = OBT_LINK_TYPE_URL;
619 else if (val[0] == 'D' && strcmp(val+1, "irectory") == 0)
620 v.value.enumerable = OBT_LINK_TYPE_DIRECTORY;
622 parse_error("Unknown Type", parse, error);
626 case OBT_DDPARSE_ENVIRONMENTS:
627 v.value.environments = parse_value_environments(val, parse, error);
630 g_assert_not_reached();
633 pv = g_slice_new(ObtDDParseValue);
635 g_hash_table_insert(parse->group->key_hash, key, pv);
639 GHashTable* obt_ddparse_file(const gchar *name, GSList *paths)
642 ObtDDParseGroup *desktop_entry;
647 parse.filename = NULL;
650 parse.group_hash = g_hash_table_new_full(g_str_hash,
653 (GDestroyNotify)parse_group_free);
655 /* set up the groups (there's only one right now) */
656 desktop_entry = parse_group_new(g_strdup("Desktop Entry"),
657 parse_desktop_entry_value);
658 g_hash_table_insert(parse.group_hash, desktop_entry->name, desktop_entry);
661 for (it = paths; it && !success; it = g_slist_next(it)) {
662 gchar *path = g_strdup_printf("%s/%s", (char*)it->data, name);
663 if ((f = fopen(path, "r"))) {
664 parse.filename = path;
666 success = parse_file(f, &parse);
672 g_hash_table_destroy(parse.group_hash);
676 return parse.group_hash;
679 GHashTable* obt_ddparse_group_keys(ObtDDParseGroup *g)