1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* desktop-file.c .desktop file parser
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat Inc.
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <dbus/dbus-sysdeps.h>
25 #include <dbus/dbus-internals.h>
26 #include "desktop-file.h"
30 BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX,
31 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES,
32 BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS
33 } BusDesktopParseError;
46 BusDesktopFileLine *lines;
47 int n_allocated_lines;
48 } BusDesktopFileSection;
53 BusDesktopFileSection *sections;
54 int n_allocated_sections;
61 BusDesktopFile *desktop_file;
67 } BusDesktopFileParser;
69 #define VALID_KEY_CHAR 1
70 #define VALID_LOCALE_CHAR 2
71 unsigned char valid[256] = {
72 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
73 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
74 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x3 , 0x2 , 0x0 ,
75 0x3 ,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 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
86 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
87 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
90 static void report_error (BusDesktopFileParser *parser,
92 BusDesktopParseError error_code);
95 parser_free (BusDesktopFileParser *parser)
97 bus_desktop_file_free (parser->desktop_file);
99 _dbus_string_free (&parser->data);
103 bus_desktop_file_line_free (BusDesktopFileLine *line)
105 dbus_free (line->key);
106 dbus_free (line->value);
110 bus_desktop_file_section_free (BusDesktopFileSection *section)
114 for (i = 0; i < section->n_lines; i++)
115 bus_desktop_file_line_free (§ion->lines[i]);
117 dbus_free (section->lines);
118 dbus_free (section->section_name);
122 bus_desktop_file_free (BusDesktopFile *desktop_file)
126 for (i = 0; i < desktop_file->n_sections; i++)
127 bus_desktop_file_section_free (&desktop_file->sections[i]);
128 dbus_free (desktop_file->sections);
130 dbus_free (desktop_file);
134 grow_lines_in_section (BusDesktopFileSection *section)
136 BusDesktopFileLine *lines;
140 if (section->n_allocated_lines == 0)
143 new_n_lines = section->n_allocated_lines*2;
145 _DBUS_HANDLE_OOM (lines = dbus_realloc (section->lines,
146 sizeof (BusDesktopFileLine) * new_n_lines));
147 section->lines = lines;
149 section->n_allocated_lines = new_n_lines;
153 grow_sections (BusDesktopFile *desktop_file)
156 BusDesktopFileSection *sections;
158 if (desktop_file->n_allocated_sections == 0)
161 new_n_sections = desktop_file->n_allocated_sections*2;
163 _DBUS_HANDLE_OOM (sections = dbus_realloc (desktop_file->sections,
164 sizeof (BusDesktopFileSection) * new_n_sections));
165 desktop_file->sections = sections;
167 desktop_file->n_allocated_sections = new_n_sections;
171 unescape_string (const DBusString *str, int pos, int end_pos)
175 /* len + 1 is enough, because unescaping never makes the
177 _DBUS_HANDLE_OOM (retval = dbus_malloc (end_pos - pos + 1));
181 while (pos < end_pos)
183 if (_dbus_string_get_byte (str, pos) == 0)
185 /* Found an embedded null */
190 if (_dbus_string_get_byte (str, pos) == '\\')
196 /* Escape at end of string */
201 switch (_dbus_string_get_byte (str, pos))
219 /* Invalid escape code */
227 *q++ =_dbus_string_get_byte (str, pos);
238 static BusDesktopFileSection*
239 new_section (BusDesktopFile *desktop_file,
244 if (desktop_file->n_allocated_sections == desktop_file->n_sections)
245 grow_sections (desktop_file);
247 n = desktop_file->n_sections++;
249 _DBUS_HANDLE_OOM (desktop_file->sections[n].section_name = _dbus_strdup (name));
251 desktop_file->sections[n].n_lines = 0;
252 desktop_file->sections[n].lines = NULL;
253 desktop_file->sections[n].n_allocated_lines = 0;
255 grow_lines_in_section (&desktop_file->sections[n]);
257 return &desktop_file->sections[n];
260 static BusDesktopFileSection*
261 open_section (BusDesktopFileParser *parser,
264 BusDesktopFileSection *section;
266 section = new_section (parser->desktop_file, name);
270 parser->current_section = parser->desktop_file->n_sections - 1;
271 _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section);
276 static BusDesktopFileLine *
277 new_line (BusDesktopFileParser *parser)
279 BusDesktopFileSection *section;
280 BusDesktopFileLine *line;
282 section = &parser->desktop_file->sections[parser->current_section];
284 if (section->n_allocated_lines == section->n_lines)
285 grow_lines_in_section (section);
287 line = §ion->lines[section->n_lines++];
289 memset (line, 0, sizeof (BusDesktopFileLine));
295 is_blank_line (BusDesktopFileParser *parser)
302 c = _dbus_string_get_byte (&parser->data, p);
304 while (c && c != '\n')
306 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'))
310 c = _dbus_string_get_byte (&parser->data, p);
317 parse_comment_or_blank (BusDesktopFileParser *parser)
321 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
322 line_end = parser->len;
324 if (line_end == parser->len)
325 parser->pos = parser->len;
327 parser->pos = line_end + 1;
329 parser->line_num += 1;
333 is_valid_section_name (const char *name)
335 /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'. */
339 if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') ||
340 *name == '\n' || *name == '\t'))
350 parse_section_start (BusDesktopFileParser *parser, DBusResultCode *result)
355 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
356 line_end = parser->len;
358 if (line_end - parser->pos <= 2 ||
359 _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
361 report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX);
362 parser_free (parser);
366 section_name = unescape_string (&parser->data, parser->pos + 1, line_end - 1);
368 if (section_name == NULL)
370 report_error (parser, "Invalid escaping in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES);
371 parser_free (parser);
375 if (!is_valid_section_name (section_name))
377 report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS);
378 parser_free (parser);
379 dbus_free (section_name);
383 if (open_section (parser, section_name) == NULL)
385 dbus_free (section_name);
389 if (line_end == parser->len)
390 parser->pos = parser->len;
392 parser->pos = line_end + 1;
394 parser->line_num += 1;
396 dbus_free (section_name);
402 parse_key_value (BusDesktopFileParser *parser, DBusResultCode *result)
405 int key_start, key_end;
410 BusDesktopFileLine *line;
412 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
413 line_end = parser->len;
417 while (p < line_end &&
418 (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
422 if (key_start == key_end)
424 report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX);
425 parser_free (parser);
429 /* We ignore locales for now */
431 /* Skip space before '=' */
432 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
435 if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
437 report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS);
438 parser_free (parser);
444 report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX);
445 parser_free (parser);
452 /* Skip space after '=' */
453 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
458 value = unescape_string (&parser->data, value_start, line_end);
461 report_error (parser, "Invalid escaping in value", BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES);
462 parser_free (parser);
466 line = new_line (parser);
468 _DBUS_HANDLE_OOM (_dbus_string_init (&key, key_end - key_start));
469 _DBUS_HANDLE_OOM (_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
471 _DBUS_HANDLE_OOM (_dbus_string_steal_data (&key, &tmp));
472 _dbus_string_free (&key);
477 if (line_end == parser->len)
478 parser->pos = parser->len;
480 parser->pos = line_end + 1;
482 parser->line_num += 1;
488 report_error (BusDesktopFileParser *parser,
490 BusDesktopParseError error_code)
492 char *section_name = NULL;
498 _dbus_verbose ("Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
500 _dbus_verbose ("Error at line %d: %s\n", parser->line_num, message);
505 dump_desktop_file (BusDesktopFile *file)
509 for (i = 0; i < file->n_sections; i++)
513 printf ("[%s]\n", file->sections[i].section_name);
515 for (j = 0; j < file->sections[i].n_lines; j++)
517 printf ("%s=%s\n", file->sections[i].lines[j].key,
518 file->sections[i].lines[j].value);
525 bus_desktop_file_load (const char *filename,
526 DBusResultCode *result)
528 DBusString str, filename_str;
529 DBusResultCode result_code;
530 BusDesktopFileParser parser;
532 /* FIXME: Check file size so we don't try to load a ridicously large file. */
534 _dbus_string_init_const (&filename_str, filename);
536 _DBUS_HANDLE_OOM (_dbus_string_init (&str, _DBUS_INT_MAX));
538 _DBUS_HANDLE_OOM ((result_code = _dbus_file_get_contents (&str, &filename_str)) !=
539 DBUS_RESULT_NO_MEMORY);
541 if (result_code != DBUS_RESULT_SUCCESS)
543 _dbus_string_free (&str);
544 dbus_set_result (result, result_code);
548 if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
550 _dbus_string_free (&str);
551 /* FIXME: Use a better error code */
552 dbus_set_result (result, result_code);
556 _DBUS_HANDLE_OOM (parser.desktop_file = dbus_malloc (sizeof (BusDesktopFile)));
558 memset (parser.desktop_file, 0, sizeof (BusDesktopFile));
563 parser.len = _dbus_string_get_length (&parser.data);
565 while (parser.pos < parser.len)
567 if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
569 if (!parse_section_start (&parser, result))
572 else if (is_blank_line (&parser) ||
573 _dbus_string_get_byte (&parser.data, parser.pos) == '#')
574 parse_comment_or_blank (&parser);
577 if (!parse_key_value (&parser, result))
582 _dbus_string_free (&parser.data);
584 dbus_set_result (result, DBUS_RESULT_SUCCESS);
586 return parser.desktop_file;