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,
93 DBusResultCode *result);
96 parser_free (BusDesktopFileParser *parser)
98 bus_desktop_file_free (parser->desktop_file);
100 _dbus_string_free (&parser->data);
104 bus_desktop_file_line_free (BusDesktopFileLine *line)
106 dbus_free (line->key);
107 dbus_free (line->value);
111 bus_desktop_file_section_free (BusDesktopFileSection *section)
115 for (i = 0; i < section->n_lines; i++)
116 bus_desktop_file_line_free (§ion->lines[i]);
118 dbus_free (section->lines);
119 dbus_free (section->section_name);
123 bus_desktop_file_free (BusDesktopFile *desktop_file)
127 for (i = 0; i < desktop_file->n_sections; i++)
128 bus_desktop_file_section_free (&desktop_file->sections[i]);
129 dbus_free (desktop_file->sections);
131 dbus_free (desktop_file);
135 grow_lines_in_section (BusDesktopFileSection *section)
137 BusDesktopFileLine *lines;
141 if (section->n_allocated_lines == 0)
144 new_n_lines = section->n_allocated_lines*2;
146 _DBUS_HANDLE_OOM (lines = dbus_realloc (section->lines,
147 sizeof (BusDesktopFileLine) * new_n_lines));
148 section->lines = lines;
150 section->n_allocated_lines = new_n_lines;
154 grow_sections (BusDesktopFile *desktop_file)
157 BusDesktopFileSection *sections;
159 if (desktop_file->n_allocated_sections == 0)
162 new_n_sections = desktop_file->n_allocated_sections*2;
164 _DBUS_HANDLE_OOM (sections = dbus_realloc (desktop_file->sections,
165 sizeof (BusDesktopFileSection) * new_n_sections));
166 desktop_file->sections = sections;
168 desktop_file->n_allocated_sections = new_n_sections;
172 unescape_string (const DBusString *str, int pos, int end_pos)
176 /* len + 1 is enough, because unescaping never makes the
178 _DBUS_HANDLE_OOM (retval = dbus_malloc (end_pos - pos + 1));
182 while (pos < end_pos)
184 if (_dbus_string_get_byte (str, pos) == 0)
186 /* Found an embedded null */
191 if (_dbus_string_get_byte (str, pos) == '\\')
197 /* Escape at end of string */
202 switch (_dbus_string_get_byte (str, pos))
220 /* Invalid escape code */
228 *q++ =_dbus_string_get_byte (str, pos);
239 static BusDesktopFileSection*
240 new_section (BusDesktopFile *desktop_file,
245 if (desktop_file->n_allocated_sections == desktop_file->n_sections)
246 grow_sections (desktop_file);
248 n = desktop_file->n_sections++;
250 _DBUS_HANDLE_OOM (desktop_file->sections[n].section_name = _dbus_strdup (name));
252 desktop_file->sections[n].n_lines = 0;
253 desktop_file->sections[n].lines = NULL;
254 desktop_file->sections[n].n_allocated_lines = 0;
256 grow_lines_in_section (&desktop_file->sections[n]);
258 return &desktop_file->sections[n];
261 static BusDesktopFileSection*
262 open_section (BusDesktopFileParser *parser,
265 BusDesktopFileSection *section;
267 section = new_section (parser->desktop_file, name);
271 parser->current_section = parser->desktop_file->n_sections - 1;
272 _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section);
277 static BusDesktopFileLine *
278 new_line (BusDesktopFileParser *parser)
280 BusDesktopFileSection *section;
281 BusDesktopFileLine *line;
283 section = &parser->desktop_file->sections[parser->current_section];
285 if (section->n_allocated_lines == section->n_lines)
286 grow_lines_in_section (section);
288 line = §ion->lines[section->n_lines++];
290 memset (line, 0, sizeof (BusDesktopFileLine));
296 is_blank_line (BusDesktopFileParser *parser)
303 c = _dbus_string_get_byte (&parser->data, p);
305 while (c && c != '\n')
307 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'))
311 c = _dbus_string_get_byte (&parser->data, p);
318 parse_comment_or_blank (BusDesktopFileParser *parser)
322 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
323 line_end = parser->len;
325 if (line_end == parser->len)
326 parser->pos = parser->len;
328 parser->pos = line_end + 1;
330 parser->line_num += 1;
334 is_valid_section_name (const char *name)
336 /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'. */
340 if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') ||
341 *name == '\n' || *name == '\t'))
351 parse_section_start (BusDesktopFileParser *parser, DBusResultCode *result)
356 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
357 line_end = parser->len;
359 if (line_end - parser->pos <= 2 ||
360 _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
362 report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, result);
363 parser_free (parser);
367 section_name = unescape_string (&parser->data, parser->pos + 1, line_end - 1);
369 if (section_name == NULL)
371 report_error (parser, "Invalid escaping in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, result);
372 parser_free (parser);
376 if (!is_valid_section_name (section_name))
378 report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, result);
379 parser_free (parser);
380 dbus_free (section_name);
384 if (open_section (parser, section_name) == NULL)
386 dbus_free (section_name);
390 if (line_end == parser->len)
391 parser->pos = parser->len;
393 parser->pos = line_end + 1;
395 parser->line_num += 1;
397 dbus_free (section_name);
403 parse_key_value (BusDesktopFileParser *parser, DBusResultCode *result)
406 int key_start, key_end;
411 BusDesktopFileLine *line;
413 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
414 line_end = parser->len;
418 while (p < line_end &&
419 (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
423 if (key_start == key_end)
425 report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, result);
426 parser_free (parser);
430 /* We ignore locales for now */
432 /* Skip space before '=' */
433 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
436 if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
438 report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, result);
439 parser_free (parser);
445 report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, result);
446 parser_free (parser);
453 /* Skip space after '=' */
454 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
459 value = unescape_string (&parser->data, value_start, line_end);
462 report_error (parser, "Invalid escaping in value", BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, result);
463 parser_free (parser);
467 line = new_line (parser);
469 _DBUS_HANDLE_OOM (_dbus_string_init (&key, key_end - key_start));
470 _DBUS_HANDLE_OOM (_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
472 _DBUS_HANDLE_OOM (_dbus_string_steal_data (&key, &tmp));
473 _dbus_string_free (&key);
478 if (line_end == parser->len)
479 parser->pos = parser->len;
481 parser->pos = line_end + 1;
483 parser->line_num += 1;
489 report_error (BusDesktopFileParser *parser,
491 BusDesktopParseError error_code,
492 DBusResultCode *result)
494 const char *section_name = NULL;
496 if (parser->current_section != -1)
497 section_name = parser->desktop_file->sections[parser->current_section].section_name;
500 _dbus_verbose ("Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
502 _dbus_verbose ("Error at line %d: %s\n", parser->line_num, message);
504 dbus_set_result (result, DBUS_RESULT_FAILED);
509 dump_desktop_file (BusDesktopFile *file)
513 for (i = 0; i < file->n_sections; i++)
517 printf ("[%s]\n", file->sections[i].section_name);
519 for (j = 0; j < file->sections[i].n_lines; j++)
521 printf ("%s=%s\n", file->sections[i].lines[j].key,
522 file->sections[i].lines[j].value);
529 bus_desktop_file_load (DBusString *filename,
530 DBusResultCode *result)
533 DBusResultCode result_code;
534 BusDesktopFileParser parser;
536 /* FIXME: Check file size so we don't try to load a ridicously large file. */
538 _DBUS_HANDLE_OOM (_dbus_string_init (&str, _DBUS_INT_MAX));
540 _DBUS_HANDLE_OOM ((result_code = _dbus_file_get_contents (&str, filename)) !=
541 DBUS_RESULT_NO_MEMORY);
543 if (result_code != DBUS_RESULT_SUCCESS)
545 _dbus_string_free (&str);
546 dbus_set_result (result, result_code);
550 if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
552 _dbus_string_free (&str);
554 /* FIXME: Use a better error code */
555 dbus_set_result (result, DBUS_RESULT_FAILED);
559 _DBUS_HANDLE_OOM (parser.desktop_file = dbus_malloc0 (sizeof (BusDesktopFile)));
564 parser.len = _dbus_string_get_length (&parser.data);
565 parser.current_section = -1;
567 while (parser.pos < parser.len)
569 if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
571 if (!parse_section_start (&parser, result))
574 else if (is_blank_line (&parser) ||
575 _dbus_string_get_byte (&parser.data, parser.pos) == '#')
576 parse_comment_or_blank (&parser);
579 if (!parse_key_value (&parser, result))
584 _dbus_string_free (&parser.data);
586 dbus_set_result (result, DBUS_RESULT_SUCCESS);
588 return parser.desktop_file;
591 static BusDesktopFileSection *
592 lookup_section (BusDesktopFile *desktop_file,
593 const char *section_name)
595 BusDesktopFileSection *section;
598 if (section_name == NULL)
601 for (i = 0; i < desktop_file->n_sections; i ++)
603 section = &desktop_file->sections[i];
605 if (strcmp (section->section_name, section_name) == 0)
612 static BusDesktopFileLine *
613 lookup_line (BusDesktopFile *desktop_file,
614 BusDesktopFileSection *section,
617 BusDesktopFileLine *line;
620 for (i = 0; i < section->n_lines; i++)
622 line = §ion->lines[i];
624 if (strcmp (line->key, keyname) == 0)
632 bus_desktop_file_get_raw (BusDesktopFile *desktop_file,
633 const char *section_name,
637 BusDesktopFileSection *section;
638 BusDesktopFileLine *line;
642 section = lookup_section (desktop_file, section_name);
647 line = lookup_line (desktop_file,
660 bus_desktop_file_get_string (BusDesktopFile *desktop_file,
669 if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
672 _DBUS_HANDLE_OOM (*val = _dbus_strdup (raw));