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"
27 typedef struct _ObtDDParse ObtDDParse;
29 /* Parses the value and adds it to the group's key_hash, with the given
31 Return TRUE if it is added to the hash table, and FALSE if not.
33 typedef gboolean (*ObtDDParseValueFunc)(gchar *key, const gchar *val,
34 ObtDDParse *parse, gboolean *error);
40 ObtDDParseGroup *group;
41 /* the key is a group name, the value is a ObtDDParseGroup */
42 GHashTable *group_hash;
45 struct _ObtDDParseGroup {
48 ObtDDParseValueFunc value_func;
49 /* the key is a string (a key inside the group in the .desktop).
50 the value is an ObtDDParseValue */
54 /* Displays a warning message including the file name and line number, and
55 sets the boolean @error to true if it points to a non-NULL address.
57 static void parse_error(const gchar *m, const ObtDDParse *const parse,
61 g_warning("%s at line %lu of input", m, parse->lineno);
63 g_warning("%s at line %lu of file %s",
64 m, parse->lineno, parse->filename);
65 if (error) *error = TRUE;
68 static void parse_value_free(ObtDDParseValue *v)
71 case OBT_DDPARSE_STRING:
72 case OBT_DDPARSE_LOCALESTRING:
73 g_free(v->value.string); break;
74 case OBT_DDPARSE_STRINGS:
75 case OBT_DDPARSE_LOCALESTRINGS:
76 g_free(v->value.strings.s);
77 v->value.strings.n = 0;
79 case OBT_DDPARSE_BOOLEAN:
81 case OBT_DDPARSE_NUMERIC:
84 g_assert_not_reached();
86 g_slice_free(ObtDDParseValue, v);
89 static ObtDDParseGroup* parse_group_new(gchar *name, ObtDDParseValueFunc f)
91 ObtDDParseGroup *g = g_slice_new(ObtDDParseGroup);
95 g->key_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
97 (GDestroyNotify)parse_value_free);
101 static void parse_group_free(ObtDDParseGroup *g)
104 g_hash_table_destroy(g->key_hash);
105 g_slice_free(ObtDDParseGroup, g);
108 /*! Reads an input string, strips out invalid stuff, and parses
110 If @nstrings is not NULL, then it splits the output string at ';'
111 characters. They are all returned in the same string with null zeros
112 between them, @nstrings is set to the number of such strings.
114 static gchar* parse_value_string(const gchar *in,
117 const ObtDDParse *const parse,
120 const gint bytes = strlen(in);
123 const gchar *end, *i;
125 g_return_val_if_fail(in != NULL, NULL);
129 for (i = in; i < end; ++i) {
130 if ((guchar)*i >= 127 || (guchar)*i < 32) {
131 /* non-control character ascii */
133 parse_error("Invalid bytes in string", parse, error);
138 else if (!g_utf8_validate(in, bytes, &end))
139 parse_error("Invalid bytes in localestring", parse, error);
141 if (nstrings) *nstrings = 1;
143 out = g_new(char, bytes + 1);
147 const gchar *next = locale ? g_utf8_find_next_char(i, end) : i+1;
150 case 's': *o++ = ' '; break;
151 case 'n': *o++ = '\n'; break;
152 case 't': *o++ = '\t'; break;
153 case 'r': *o++ = '\r'; break;
154 case ';': *o++ = ';'; break;
155 case '\\': *o++ = '\\'; break;
157 parse_error((locale ?
158 "Invalid escape sequence in localestring" :
159 "Invalid escape sequence in string"),
166 else if (*i == ';' && nstrings) {
170 else if ((guchar)*i == 127 || (guchar)*i < 32) {
171 /* avoid ascii control characters */
172 parse_error("Found control character in string", parse, error);
176 memcpy(o, i, next-i);
185 static gboolean parse_value_boolean(const gchar *in,
186 const ObtDDParse *const parse,
189 if (strcmp(in, "true") == 0)
191 else if (strcmp(in, "false") != 0)
192 parse_error("Invalid boolean value", parse, error);
196 static gfloat parse_value_numeric(const gchar *in,
197 const ObtDDParse *const parse,
201 if (sscanf(in, "%f", &out) == 0)
202 parse_error("Invalid numeric value", parse, error);
206 static gboolean parse_file_line(FILE *f, gchar **buf,
207 gulong *size, gulong *read,
208 ObtDDParse *parse, gboolean *error)
210 const gulong BUFMUL = 80;
215 g_assert(*read == 0);
217 *buf = g_new(char, *size);
220 /* remove everything up to a null zero already in the buffer and shift
221 the rest to the front */
223 for (i = 0; i < *read; ++i) {
225 (*buf)[i-null-1] = (*buf)[i];
226 else if ((*buf)[i] == '\0')
232 /* is there already a newline in the buffer? */
233 for (i = 0; i < *read; ++i)
234 if ((*buf)[i] == '\n') {
235 /* turn it into a null zero and done */
240 /* we need to read some more to find a newline */
245 newread = *buf + *read;
246 ret = fread(newread, sizeof(char), *size-*read, f);
247 if (ret < *size - *read && !feof(f)) {
248 parse_error("Error reading", parse, error);
253 /* strip out null zeros in the input and look for an endofline */
256 for (i = newread-*buf; i < *read; ++i) {
258 (*buf)[i] = (*buf)[i+null];
259 if ((*buf)[i] == '\0') {
264 else if ((*buf)[i] == '\n' && eol == *size) {
266 /* turn it into a null zero */
272 /* found an endofline, done */
274 else if (feof(f) && *read < *size) {
275 /* found the endoffile, done (if there is space) */
277 /* stick a null zero on if there is test on the last line */
278 (*buf)[(*read)++] = '\0';
285 *buf = g_renew(char, *buf, *size);
291 static void parse_group(const gchar *buf, gulong len,
292 ObtDDParse *parse, gboolean *error)
298 /* get the group name */
299 group = g_strndup(buf+1, len-2);
300 for (i = 0; i < len-2; ++i)
301 if ((guchar)group[i] < 32 || (guchar)group[i] >= 127) {
302 /* valid ASCII only */
303 parse_error("Invalid character found", parse, NULL);
304 group[i] = '\0'; /* stopping before this character */
308 /* make sure it's a new group */
309 g = g_hash_table_lookup(parse->group_hash, group);
311 parse_error("Duplicate group found", parse, error);
315 /* if it's the first group, make sure it's named Desktop Entry */
316 else if (!parse->group && strcmp(group, "Desktop Entry") != 0)
318 parse_error("Incorrect group found, "
319 "expected [Desktop Entry]",
326 g = parse_group_new(group, NULL);
327 g_hash_table_insert(parse->group_hash, g->name, g);
334 g_print("Found group %s\n", g->name);
338 static void parse_key_value(const gchar *buf, gulong len,
339 ObtDDParse *parse, gboolean *error)
341 gulong i, keyend, valstart, eq;
344 /* find the end of the key */
345 for (i = 0; i < len; ++i)
346 if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
347 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z') ||
348 ((guchar)buf[i] >= '0' && (guchar)buf[i] <= '9') ||
349 ((guchar)buf[i] == '-'))) {
350 /* not part of the key */
355 parse_error("Empty key", parse, error);
358 /* find the = character */
359 for (i = keyend; i < len; ++i) {
364 else if (buf[i] != ' ') {
365 parse_error("Invalid character in key name", parse, error);
370 parse_error("Key without value found", parse, error);
373 /* find the start of the value */
374 for (i = eq+1; i < len; ++i)
380 parse_error("Empty value found", parse, error);
384 key = g_strndup(buf, keyend);
385 if (g_hash_table_lookup(parse->group->key_hash, key)) {
386 parse_error("Duplicate key found", parse, error);
390 g_print("Found key/value %s=%s.\n", key, buf+valstart);
391 if (parse->group->value_func)
392 if (!parse->group->value_func(key, buf+valstart, parse, error)) {
393 parse_error("Unknown key", parse, error);
398 static gboolean parse_file(FILE *f, ObtDDParse *parse)
401 gulong bytes = 0, read = 0;
402 gboolean error = FALSE;
404 while (!error && parse_file_line(f, &buf, &bytes, &read, parse, &error)) {
405 gulong len = strlen(buf);
406 if (buf[0] == '#' || buf[0] == '\0')
407 ; /* ignore comment lines */
408 else if (buf[0] == '[' && buf[len-1] == ']')
409 parse_group(buf, len, parse, &error);
410 else if (!parse->group)
411 /* just ignore keys outside of groups */
412 parse_error("Key found before group", parse, NULL);
414 /* ignore errors in key-value pairs and continue */
415 parse_key_value(buf, len, parse, NULL);
419 if (buf) g_free(buf);
423 static gboolean parse_desktop_entry_value(gchar *key, const gchar *val,
424 ObtDDParse *parse, gboolean *error)
426 ObtDDParseValue v, *pv;
431 case 'a': /* Categories */
432 if (strcmp(key+2, "tegories")) return FALSE;
433 v.type = OBT_DDPARSE_STRINGS; break;
434 case 'o': /* Comment */
435 if (strcmp(key+2, "mment")) return FALSE;
436 v.type = OBT_DDPARSE_LOCALESTRING; break;
442 if (strcmp(key+1, "xec")) return FALSE;
443 v.type = OBT_DDPARSE_STRING; break;
444 case 'G': /* GenericName */
445 if (strcmp(key+1, "enericName")) return FALSE;
446 v.type = OBT_DDPARSE_LOCALESTRING; break;
448 if (strcmp(key+1, "con")) return FALSE;
449 v.type = OBT_DDPARSE_LOCALESTRING; break;
450 case 'H': /* Hidden */
451 if (strcmp(key+1, "idden")) return FALSE;
452 v.type = OBT_DDPARSE_BOOLEAN; break;
453 case 'M': /* MimeType */
454 if (strcmp(key+1, "imeType")) return FALSE;
455 v.type = OBT_DDPARSE_STRINGS; break;
459 if (strcmp(key+2, "me")) return FALSE;
460 v.type = OBT_DDPARSE_LOCALESTRING; break;
463 case 'D': /* NoDisplay */
464 if (strcmp(key+3, "isplay")) return FALSE;
465 v.type = OBT_DDPARSE_BOOLEAN; break;
466 case 't': /* NotShowIn */
467 if (strcmp(key+3, "ShowIn")) return FALSE;
468 v.type = OBT_DDPARSE_STRINGS; break;
478 if (strcmp(key+1, "ath")) return FALSE;
479 v.type = OBT_DDPARSE_STRING; break;
481 if (key[1] == 't' && key[2] == 'a' && key[3] == 'r' &&
482 key[4] == 't' && key[5] == 'u' && key[6] == 'p')
484 case 'N': /* StartupNotify */
485 if (strcmp(key+8, "otify")) return FALSE;
486 v.type = OBT_DDPARSE_BOOLEAN; break;
487 case 'W': /* StartupWMClass */
488 if (strcmp(key+8, "MClass")) return FALSE;
489 v.type = OBT_DDPARSE_STRING; break;
498 case 'e': /* Terminal */
499 if (strcmp(key+2, "rminal")) return FALSE;
500 v.type = OBT_DDPARSE_BOOLEAN; break;
501 case 'r': /* TryExec */
502 if (strcmp(key+2, "yExec")) return FALSE;
503 v.type = OBT_DDPARSE_STRING; break;
505 if (strcmp(key+2, "pe")) return FALSE;
506 v.type = OBT_DDPARSE_STRING; break;
512 if (strcmp(key+1, "RL")) return FALSE;
513 v.type = OBT_DDPARSE_STRING; break;
514 case 'V': /* MimeType */
515 if (strcmp(key+1, "ersion")) return FALSE;
516 v.type = OBT_DDPARSE_STRING; break;
521 /* parse the value */
523 case OBT_DDPARSE_STRING:
524 v.value.string = parse_value_string(val, FALSE, NULL, parse, error);
525 g_assert(v.value.string);
527 case OBT_DDPARSE_LOCALESTRING:
528 v.value.string = parse_value_string(val, TRUE, NULL, parse, error);
529 g_assert(v.value.string);
531 case OBT_DDPARSE_STRINGS:
532 v.value.strings.s = parse_value_string(val, FALSE, &v.value.strings.n,
534 g_assert(v.value.strings.s);
535 g_assert(v.value.strings.n);
537 case OBT_DDPARSE_LOCALESTRINGS:
538 v.value.strings.s = parse_value_string(val, TRUE, &v.value.strings.n,
540 g_assert(v.value.strings.s);
541 g_assert(v.value.strings.n);
543 case OBT_DDPARSE_BOOLEAN:
544 v.value.boolean = parse_value_boolean(val, parse, error);
546 case OBT_DDPARSE_NUMERIC:
547 v.value.numeric = parse_value_numeric(val, parse, error);
550 g_assert_not_reached();
553 pv = g_slice_new(ObtDDParseValue);
555 g_hash_table_insert(parse->group->key_hash, key, pv);
559 GHashTable* obt_ddparse_file(const gchar *name, GSList *paths)
562 ObtDDParseGroup *desktop_entry;
567 parse.filename = NULL;
570 parse.group_hash = g_hash_table_new_full(g_str_hash,
573 (GDestroyNotify)parse_group_free);
575 /* set up the groups (there's only one right now) */
576 desktop_entry = parse_group_new(g_strdup("Desktop Entry"),
577 parse_desktop_entry_value);
578 g_hash_table_insert(parse.group_hash, desktop_entry->name, desktop_entry);
581 for (it = paths; it && !success; it = g_slist_next(it)) {
582 gchar *path = g_strdup_printf("%s/%s", (char*)it->data, name);
583 if ((f = fopen(path, "r"))) {
584 parse.filename = path;
586 success = parse_file(f, &parse);
592 g_hash_table_destroy(parse.group_hash);
596 return parse.group_hash;
599 GHashTable* obt_ddparse_group_keys(ObtDDParseGroup *g)