1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 obt/ddfile.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/ddfile.h"
28 typedef void (*ObtDDParseGroupFunc)(const gchar *group,
29 GHashTable *key_hash);
31 typedef struct _ObtDDParseGroup {
34 ObtDDParseGroupFunc func;
35 /* the key is a string (a key in the .desktop).
36 the value is a strings (a value in the .desktop) */
40 typedef struct _ObtDDParse {
43 ObtDDParseGroup *group;
44 /* the key is a group name, the value is a ObtDDParseGroup */
45 GHashTable *group_hash;
60 gchar *name; /*!< Specific name for the object (eg Firefox) */
61 gchar *generic; /*!< Generic name for the object (eg Web Browser) */
62 gchar *comment; /*!< Comment/description to display for the object */
63 gchar *icon; /*!< Name/path for an icon for the object */
65 union _ObtDDFileData {
67 gchar *exec; /*!< Executable to run for the app */
68 gchar *wdir; /*!< Working dir to run the app in */
69 gboolean term; /*!< Run the app in a terminal or not */
70 ObtDDFileAppOpen open;
72 /* XXX gchar**? or something better, a mime struct.. maybe
73 glib has something i can use. */
74 gchar **mime; /*!< Mime types the app can open */
76 ObtDDFileAppStartup startup;
77 gchar *startup_wmclass;
87 static void group_free(ObtDDParseGroup *g)
90 g_hash_table_destroy(g->key_hash);
91 g_slice_free(ObtDDParseGroup, g);
94 /* Displays a warning message including the file name and line number, and
95 sets the boolean @error to true if it points to a non-NULL address.
97 static void parse_error(const gchar *m, const ObtDDParse *const parse,
100 if (!parse->filename)
101 g_warning("%s at line %lu of input", m, parse->lineno);
103 g_warning("%s at line %lu of file %s",
104 m, parse->lineno, parse->filename);
105 if (error) *error = TRUE;
108 /* reads an input string, strips out invalid stuff, and parses
110 static gchar* parse_string(const gchar *in, gboolean locale,
111 const ObtDDParse *const parse,
114 const gint bytes = strlen(in);
117 const gchar *end, *i;
119 g_return_val_if_fail(in != NULL, NULL);
123 for (i = in; i < end; ++i) {
124 if ((guchar)*i > 126 || (guchar)*i < 32) {
125 /* non-control character ascii */
127 parse_error("Invalid bytes in string", parse, error);
132 else if (!g_utf8_validate(in, bytes, &end))
133 parse_error("Invalid bytes in localestring", parse, error);
135 out = g_new(char, bytes + 1);
139 const gchar *next = locale ? g_utf8_find_next_char(i, end) : i+1;
142 case 's': *o++ = ' '; break;
143 case 'n': *o++ = '\n'; break;
144 case 't': *o++ = '\t'; break;
145 case 'r': *o++ = '\r'; break;
146 case '\\': *o++ = '\\'; break;
148 parse_error((locale ?
149 "Invalid escape sequence in localestring" :
150 "Invalid escape sequence in string"),
157 else if ((guchar)*i >= 127 || (guchar)*i < 32) {
158 /* avoid ascii control characters */
159 parse_error("Found control character in string", parse, error);
163 memcpy(o, i, next-i);
172 static gboolean parse_bool(const gchar *in, const ObtDDParse *const parse,
175 if (strcmp(in, "true") == 0)
177 else if (strcmp(in, "false") != 0)
178 parse_error("Invalid boolean value", parse, error);
182 static float parse_numeric(const gchar *in, const ObtDDParse *const parse,
186 if (sscanf(in, "%f", &out) == 0)
187 parse_error("Invalid numeric value", parse, error);
191 gboolean parse_file_line(FILE *f, gchar **buf, gulong *size, gulong *read,
192 ObtDDParse *parse, gboolean *error)
194 const gulong BUFMUL = 80;
199 g_assert(*read == 0);
201 *buf = g_new(char, *size);
204 /* remove everything up to a null zero already in the buffer and shift
205 the rest to the front */
207 for (i = 0; i < *read; ++i) {
209 (*buf)[i-null-1] = (*buf)[i];
210 else if ((*buf)[i] == '\0')
216 /* is there already a newline in the buffer? */
217 for (i = 0; i < *read; ++i)
218 if ((*buf)[i] == '\n') {
219 /* turn it into a null zero and done */
224 /* we need to read some more to find a newline */
229 newread = *buf + *read;
230 ret = fread(newread, sizeof(char), *size-*read, f);
231 if (ret < *size - *read && !feof(f)) {
232 parse_error("Error reading", parse, error);
237 /* strip out null zeros in the input and look for an endofline */
240 for (i = newread-*buf; i < *read; ++i) {
242 (*buf)[i] = (*buf)[i+null];
243 if ((*buf)[i] == '\0') {
248 else if ((*buf)[i] == '\n' && eol == *size) {
250 /* turn it into a null zero */
256 /* found an endofline, done */
258 else if (feof(f) && *read < *size) {
259 /* found the endoffile, done (if there is space) */
261 /* stick a null zero on if there is test on the last line */
262 (*buf)[(*read)++] = '\0';
269 *buf = g_renew(char, *buf, *size);
275 static void parse_group(const gchar *buf, gulong len,
276 ObtDDParse *parse, gboolean *error)
282 /* get the group name */
283 group = g_strndup(buf+1, len-2);
284 for (i = 0; i < len-2; ++i)
285 if ((guchar)group[i] < 32 || (guchar)group[i] >= 127) {
286 /* valid ASCII only */
287 parse_error("Invalid character found", parse, NULL);
288 group[i] = '\0'; /* stopping before this character */
292 /* make sure it's a new group */
293 g = g_hash_table_lookup(parse->group_hash, group);
295 parse_error("Duplicate group found", parse, error);
299 /* if it's the first group, make sure it's named Desktop Entry */
300 else if (!parse->group && strcmp(group, "Desktop Entry") != 0)
302 parse_error("Incorrect group found, "
303 "expected [Desktop Entry]",
310 g = g_slice_new(ObtDDParseGroup);
313 g->key_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
315 g_hash_table_insert(parse->group_hash, group, g);
322 g_print("Found group %s\n", g->name);
326 static void parse_key_value(const gchar *buf, gulong len,
327 ObtDDParse *parse, gboolean *error)
329 gulong i, keyend, valstart, eq;
332 /* find the end of the key */
333 for (i = 0; i < len; ++i)
334 if (!(((guchar)buf[i] >= 'A' && (guchar)buf[i] <= 'Z') ||
335 ((guchar)buf[i] >= 'a' && (guchar)buf[i] <= 'z') ||
336 ((guchar)buf[i] >= '0' && (guchar)buf[i] <= '9') ||
337 ((guchar)buf[i] == '-'))) {
338 /* not part of the key */
343 parse_error("Empty key", parse, error);
346 /* find the = character */
347 for (i = keyend; i < len; ++i) {
352 else if (buf[i] != ' ') {
353 parse_error("Invalid character in key name", parse, error);
358 parse_error("Key without value found", parse, error);
361 /* find the start of the value */
362 for (i = eq+1; i < len; ++i)
368 parse_error("Empty value found", parse, error);
372 key = g_strndup(buf, keyend);
373 val = g_strndup(buf+valstart, len-valstart);
374 if (g_hash_table_lookup(parse->group->key_hash, key)) {
375 parse_error("Duplicate key found", parse, error);
380 g_hash_table_insert(parse->group->key_hash, key, val);
381 g_print("Found key/value %s=%s.\n", key, val);
384 static gboolean parse_file(ObtDDFile *dd, FILE *f, ObtDDParse *parse)
387 gulong bytes = 0, read = 0;
388 gboolean error = FALSE;
390 while (!error && parse_file_line(f, &buf, &bytes, &read, parse, &error)) {
391 /* XXX use the string in buf */
392 gulong len = strlen(buf);
393 if (buf[0] == '#' || buf[0] == '\0')
394 ; /* ignore comment lines */
395 else if (buf[0] == '[' && buf[len-1] == ']')
396 parse_group(buf, len, parse, &error);
397 else if (!parse->group)
398 /* just ignore keys outside of groups */
399 parse_error("Key found before group", parse, NULL);
401 /* ignore errors in key-value pairs and continue */
402 parse_key_value(buf, len, parse, NULL);
406 if (buf) g_free(buf);
410 ObtDDFile* obt_ddfile_new_from_file(const gchar *name, GSList *paths)
418 dd = g_slice_new(ObtDDFile);
421 parse.filename = NULL;
424 parse.group_hash = g_hash_table_new_full(g_str_hash,
427 (GDestroyNotify)group_free);
430 for (it = paths; it && !success; it = g_slist_next(it)) {
431 gchar *path = g_strdup_printf("%s/%s", (char*)it->data, name);
432 if ((f = fopen(path, "r"))) {
433 parse.filename = path;
435 success = parse_file(dd, f, &parse);
441 obt_ddfile_unref(dd);
445 g_hash_table_destroy(parse.group_hash);
450 void obt_ddfile_ref(ObtDDFile *dd)
455 void obt_ddfile_unref(ObtDDFile *dd)
458 g_slice_free(ObtDDFile, dd);