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 struct _ObtDDParse {
43 gchar *name; /*!< Specific name for the object (eg Firefox) */
44 gchar *generic; /*!< Generic name for the object (eg Web Browser) */
45 gchar *comment; /*!< Comment/description to display for the object */
46 gchar *icon; /*!< Name/path for an icon for the object */
48 union _ObtDDFileData {
50 gchar *exec; /*!< Executable to run for the app */
51 gchar *wdir; /*!< Working dir to run the app in */
52 gboolean term; /*!< Run the app in a terminal or not */
53 ObtDDFileAppOpen open;
55 /* XXX gchar**? or something better, a mime struct.. maybe
56 glib has something i can use. */
57 gchar **mime; /*!< Mime types the app can open */
59 ObtDDFileAppStartup startup;
60 gchar *startup_wmclass;
70 static void parse_error(const gchar *m, const ObtDDParse *const parse,
74 g_warning("%s at line %lud of input\n", m, parse->lineno);
76 g_warning("%s at line %lud of file %s\n",
77 m, parse->lineno, parse->filename);
78 if (error) *error = TRUE;
81 /* reads an input string, strips out invalid stuff, and parses
83 static gchar* parse_string(const gchar *in, gboolean locale,
84 const ObtDDParse *const parse,
87 const gint bytes = strlen(in);
92 g_return_val_if_fail(in != NULL, NULL);
96 for (i = in; i < end; ++i) {
99 parse_error("Invalid bytes in string", parse, error);
104 else if (!g_utf8_validate(in, bytes, &end))
105 parse_error("Invalid bytes in localestring", parse, error);
107 out = g_new(char, bytes + 1);
111 const gchar *next = locale ? g_utf8_find_next_char(i, end) : i+1;
114 case 's': *o++ = ' '; break;
115 case 'n': *o++ = '\n'; break;
116 case 't': *o++ = '\t'; break;
117 case 'r': *o++ = '\r'; break;
118 case '\\': *o++ = '\\'; break;
120 parse_error((locale ?
121 "Invalid escape sequence in localestring" :
122 "Invalid escape sequence in string"),
130 memcpy(o, i, next-i);
139 static gboolean parse_bool(const gchar *in, const ObtDDParse *const parse,
142 if (strcmp(in, "true") == 0)
144 else if (strcmp(in, "false") != 0)
145 parse_error("Invalid boolean value", parse, error);
149 static float parse_numeric(const gchar *in, const ObtDDParse *const parse,
153 if (sscanf(in, "%f", &out) == 0)
154 parse_error("Invalid numeric value", parse, error);