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
{
45 gchar
*name
; /*!< Specific name for the object (eg Firefox) */
46 gchar
*generic
; /*!< Generic name for the object (eg Web Browser) */
47 gchar
*comment
; /*!< Comment/description to display for the object */
48 gchar
*icon
; /*!< Name/path for an icon for the object */
50 union _ObtDDFileData
{
52 gchar
*exec
; /*!< Executable to run for the app */
53 gchar
*wdir
; /*!< Working dir to run the app in */
54 gboolean term
; /*!< Run the app in a terminal or not */
55 ObtDDFileAppOpen open
;
57 /* XXX gchar**? or something better, a mime struct.. maybe
58 glib has something i can use. */
59 gchar
**mime
; /*!< Mime types the app can open */
61 ObtDDFileAppStartup startup
;
62 gchar
*startup_wmclass
;
72 static void parse_error(const gchar
*m
, const ObtDDParse
*const parse
,
76 g_warning("%s at line %lu of input\n", m
, parse
->lineno
);
78 g_warning("%s at line %lu of file %s\n",
79 m
, parse
->lineno
, parse
->filename
);
80 if (error
) *error
= TRUE
;
83 /* reads an input string, strips out invalid stuff, and parses
85 static gchar
* parse_string(const gchar
*in
, gboolean locale
,
86 const ObtDDParse
*const parse
,
89 const gint bytes
= strlen(in
);
94 g_return_val_if_fail(in
!= NULL
, NULL
);
98 for (i
= in
; i
< end
; ++i
) {
101 parse_error("Invalid bytes in string", parse
, error
);
106 else if (!g_utf8_validate(in
, bytes
, &end
))
107 parse_error("Invalid bytes in localestring", parse
, error
);
109 out
= g_new(char, bytes
+ 1);
113 const gchar
*next
= locale
? g_utf8_find_next_char(i
, end
) : i
+1;
116 case 's': *o
++ = ' '; break;
117 case 'n': *o
++ = '\n'; break;
118 case 't': *o
++ = '\t'; break;
119 case 'r': *o
++ = '\r'; break;
120 case '\\': *o
++ = '\\'; break;
122 parse_error((locale
?
123 "Invalid escape sequence in localestring" :
124 "Invalid escape sequence in string"),
132 memcpy(o
, i
, next
-i
);
141 static gboolean
parse_bool(const gchar
*in
, const ObtDDParse
*const parse
,
144 if (strcmp(in
, "true") == 0)
146 else if (strcmp(in
, "false") != 0)
147 parse_error("Invalid boolean value", parse
, error
);
151 static float parse_numeric(const gchar
*in
, const ObtDDParse
*const parse
,
155 if (sscanf(in
, "%f", &out
) == 0)
156 parse_error("Invalid numeric value", parse
, error
);
160 gboolean
parse_file_line(FILE *f
, gchar
**buf
, gulong
*size
, gulong
*read
,
161 ObtDDParse
*parse
, gboolean
*error
)
163 const gulong BUFMUL
= 80;
168 g_assert(*read
== 0);
170 *buf
= g_new(char, *size
);
173 /* remove everything up to a null zero already in the buffer and shift
174 the rest to the front */
176 for (i
= 0; i
< *read
; ++i
) {
178 (*buf
)[i
-null
-1] = (*buf
)[i
];
179 else if ((*buf
)[i
] == '\0')
185 /* is there already a newline in the buffer? */
186 for (i
= 0; i
< *read
; ++i
)
187 if ((*buf
)[i
] == '\n') {
188 /* turn it into a null zero and done */
193 /* we need to read some more to find a newline */
198 newread
= *buf
+ *read
;
199 ret
= fread(newread
, sizeof(char), *size
-*read
, f
);
200 if (ret
< *size
- *read
&& !feof(f
)) {
201 parse_error("Error reading", parse
, error
);
206 /* strip out null zeros in the input and look for an endofline */
209 for (i
= newread
-*buf
; i
< *read
; ++i
) {
211 (*buf
)[i
] = (*buf
)[i
+null
];
212 if ((*buf
)[i
] == '\0') {
217 else if ((*buf
)[i
] == '\n' && eol
== *size
) {
219 /* turn it into a null zero */
225 /* found an endofline, done */
227 else if (feof(f
) && *read
< *size
) {
228 /* found the endoffile, done (if there is space) */
230 /* stick a null zero on if there is test on the last line */
231 (*buf
)[(*read
)++] = '\0';
238 *buf
= g_renew(char, *buf
, *size
);
244 static gboolean
parse_file(ObtDDFile
*dd
, FILE *f
, ObtDDParse
*parse
)
247 gulong bytes
= 0, read
= 0;
248 gboolean error
= FALSE
;
250 while (parse_file_line(f
, &buf
, &bytes
, &read
, parse
, &error
)) {
251 /* XXX use the string in buf */
255 if (buf
) g_free(buf
);
259 ObtDDFile
* obt_ddfile_new_from_file(const gchar
*name
, GSList
*paths
)
266 dd
= g_slice_new(ObtDDFile
);
270 for (it
= paths
; it
&& !f
; it
= g_slist_next(it
)) {
271 gchar
*path
= g_strdup_printf("%s/%s", (char*)it
->data
, name
);
272 if ((f
= fopen(path
, "r"))) {
273 parse
.filename
= path
;
275 if (!parse_file(dd
, f
, &parse
)) f
= NULL
;
279 obt_ddfile_unref(dd
);
285 void obt_ddfile_ref(ObtDDFile
*dd
)
290 void obt_ddfile_unref(ObtDDFile
*dd
)
293 g_slice_free(ObtDDFile
, dd
);