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"
40 BusDesktopFileLine *lines;
41 int n_allocated_lines;
42 } BusDesktopFileSection;
47 BusDesktopFileSection *sections;
48 int n_allocated_sections;
55 BusDesktopFile *desktop_file;
61 } BusDesktopFileParser;
63 #define VALID_KEY_CHAR 1
64 #define VALID_LOCALE_CHAR 2
65 unsigned char valid[256] = {
66 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
67 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
68 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x3 , 0x2 , 0x0 ,
69 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
70 0x0 , 0x3 , 0x3 , 0x3 , 0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 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
81 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
84 static void report_error (BusDesktopFileParser *parser,
86 const char *error_name,
90 parser_free (BusDesktopFileParser *parser)
92 bus_desktop_file_free (parser->desktop_file);
94 _dbus_string_free (&parser->data);
98 bus_desktop_file_line_free (BusDesktopFileLine *line)
100 dbus_free (line->key);
101 dbus_free (line->value);
105 bus_desktop_file_section_free (BusDesktopFileSection *section)
109 for (i = 0; i < section->n_lines; i++)
110 bus_desktop_file_line_free (§ion->lines[i]);
112 dbus_free (section->lines);
113 dbus_free (section->section_name);
117 bus_desktop_file_free (BusDesktopFile *desktop_file)
121 for (i = 0; i < desktop_file->n_sections; i++)
122 bus_desktop_file_section_free (&desktop_file->sections[i]);
123 dbus_free (desktop_file->sections);
125 dbus_free (desktop_file);
129 grow_lines_in_section (BusDesktopFileSection *section)
131 BusDesktopFileLine *lines;
135 if (section->n_allocated_lines == 0)
138 new_n_lines = section->n_allocated_lines*2;
140 lines = dbus_realloc (section->lines,
141 sizeof (BusDesktopFileLine) * new_n_lines);
146 section->lines = lines;
147 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 sections = dbus_realloc (desktop_file->sections,
164 sizeof (BusDesktopFileSection) * new_n_sections);
165 if (sections == NULL)
168 desktop_file->sections = sections;
170 desktop_file->n_allocated_sections = new_n_sections;
176 unescape_string (BusDesktopFileParser *parser,
177 const DBusString *str,
184 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
186 /* len + 1 is enough, because unescaping never makes the
189 retval = dbus_malloc (end_pos - pos + 1);
198 while (pos < end_pos)
200 if (_dbus_string_get_byte (str, pos) == 0)
202 /* Found an embedded null */
204 report_error (parser, "Text to be unescaped contains embedded nul",
205 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
209 if (_dbus_string_get_byte (str, pos) == '\\')
215 /* Escape at end of string */
217 report_error (parser, "Text to be unescaped ended in \\",
218 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
222 switch (_dbus_string_get_byte (str, pos))
240 /* Invalid escape code */
242 report_error (parser, "Text to be unescaped had invalid escape sequence",
243 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
250 *q++ =_dbus_string_get_byte (str, pos);
261 static BusDesktopFileSection*
262 new_section (BusDesktopFile *desktop_file,
268 if (desktop_file->n_allocated_sections == desktop_file->n_sections)
270 if (!grow_sections (desktop_file))
274 name_copy = _dbus_strdup (name);
275 if (name_copy == NULL)
278 n = desktop_file->n_sections;
279 desktop_file->sections[n].section_name = name_copy;
281 desktop_file->sections[n].n_lines = 0;
282 desktop_file->sections[n].lines = NULL;
283 desktop_file->sections[n].n_allocated_lines = 0;
285 if (!grow_lines_in_section (&desktop_file->sections[n]))
287 dbus_free (desktop_file->sections[n].section_name);
288 desktop_file->sections[n].section_name = NULL;
292 desktop_file->n_sections += 1;
294 return &desktop_file->sections[n];
297 static BusDesktopFileSection*
298 open_section (BusDesktopFileParser *parser,
301 BusDesktopFileSection *section;
303 section = new_section (parser->desktop_file, name);
307 parser->current_section = parser->desktop_file->n_sections - 1;
308 _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section);
313 static BusDesktopFileLine *
314 new_line (BusDesktopFileParser *parser)
316 BusDesktopFileSection *section;
317 BusDesktopFileLine *line;
319 section = &parser->desktop_file->sections[parser->current_section];
321 if (section->n_allocated_lines == section->n_lines)
323 if (!grow_lines_in_section (section))
327 line = §ion->lines[section->n_lines++];
329 memset (line, 0, sizeof (BusDesktopFileLine));
335 is_blank_line (BusDesktopFileParser *parser)
342 c = _dbus_string_get_byte (&parser->data, p);
344 while (c && c != '\n')
346 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'))
350 c = _dbus_string_get_byte (&parser->data, p);
357 parse_comment_or_blank (BusDesktopFileParser *parser)
361 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
362 line_end = parser->len;
364 if (line_end == parser->len)
365 parser->pos = parser->len;
367 parser->pos = line_end + 1;
369 parser->line_num += 1;
373 is_valid_section_name (const char *name)
375 /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'. */
379 if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') ||
380 *name == '\n' || *name == '\t'))
390 parse_section_start (BusDesktopFileParser *parser, DBusError *error)
395 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
397 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
398 line_end = parser->len;
400 if (line_end - parser->pos <= 2 ||
401 _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
403 report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
404 parser_free (parser);
408 section_name = unescape_string (parser,
409 &parser->data, parser->pos + 1, line_end - 1,
412 if (section_name == NULL)
414 parser_free (parser);
418 if (!is_valid_section_name (section_name))
420 report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
421 parser_free (parser);
422 dbus_free (section_name);
426 if (open_section (parser, section_name) == NULL)
428 dbus_free (section_name);
432 if (line_end == parser->len)
433 parser->pos = parser->len;
435 parser->pos = line_end + 1;
437 parser->line_num += 1;
439 dbus_free (section_name);
445 parse_key_value (BusDesktopFileParser *parser, DBusError *error)
448 int key_start, key_end;
453 BusDesktopFileLine *line;
455 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
457 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
458 line_end = parser->len;
462 while (p < line_end &&
463 (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
467 if (key_start == key_end)
469 report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
470 parser_free (parser);
474 /* We ignore locales for now */
476 /* Skip space before '=' */
477 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
480 if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
482 report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
483 parser_free (parser);
489 report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
490 parser_free (parser);
497 /* Skip space after '=' */
498 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
503 value = unescape_string (parser, &parser->data, value_start, line_end, error);
506 parser_free (parser);
510 line = new_line (parser);
513 parser_free (parser);
517 if (!_dbus_string_init (&key, key_end - key_start))
519 parser_free (parser);
523 if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
526 parser_free (parser);
530 if (!_dbus_string_steal_data (&key, &tmp))
532 parser_free (parser);
536 _dbus_string_free (&key);
541 if (line_end == parser->len)
542 parser->pos = parser->len;
544 parser->pos = line_end + 1;
546 parser->line_num += 1;
552 report_error (BusDesktopFileParser *parser,
554 const char *error_name,
557 const char *section_name = NULL;
559 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
561 if (parser->current_section != -1)
562 section_name = parser->desktop_file->sections[parser->current_section].section_name;
565 dbus_set_error (error, error_name,
566 "Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
568 dbus_set_error (error, error_name,
569 "Error at line %d: %s\n", parser->line_num, message);
574 dump_desktop_file (BusDesktopFile *file)
578 for (i = 0; i < file->n_sections; i++)
582 printf ("[%s]\n", file->sections[i].section_name);
584 for (j = 0; j < file->sections[i].n_lines; j++)
586 printf ("%s=%s\n", file->sections[i].lines[j].key,
587 file->sections[i].lines[j].value);
594 bus_desktop_file_load (DBusString *filename,
598 BusDesktopFileParser parser;
601 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
603 /* Clearly there's a race here, but it's just to make it unlikely
604 * that we do something silly, we still handle doing it below.
606 if (!_dbus_stat (filename, &sb, error))
609 if (sb.size > _DBUS_ONE_KILOBYTE * 128)
611 dbus_set_error (error, DBUS_ERROR_FAILED,
612 "Desktop file size (%ld bytes) is too large", (long) sb.size);
616 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
619 if (!_dbus_file_get_contents (&str, filename, error))
621 _dbus_string_free (&str);
625 if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
627 _dbus_string_free (&str);
628 dbus_set_error (error, DBUS_ERROR_FAILED,
633 parser.desktop_file = dbus_new0 (BusDesktopFile, 1);
634 if (parser.desktop_file == NULL)
636 _dbus_string_free (&str);
644 parser.len = _dbus_string_get_length (&parser.data);
645 parser.current_section = -1;
647 while (parser.pos < parser.len)
649 if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
651 if (!parse_section_start (&parser, error))
653 _dbus_string_free (&parser.data);
657 else if (is_blank_line (&parser) ||
658 _dbus_string_get_byte (&parser.data, parser.pos) == '#')
659 parse_comment_or_blank (&parser);
662 if (!parse_key_value (&parser, error))
664 _dbus_string_free (&parser.data);
670 _dbus_string_free (&parser.data);
672 return parser.desktop_file;
675 static BusDesktopFileSection *
676 lookup_section (BusDesktopFile *desktop_file,
677 const char *section_name)
679 BusDesktopFileSection *section;
682 if (section_name == NULL)
685 for (i = 0; i < desktop_file->n_sections; i ++)
687 section = &desktop_file->sections[i];
689 if (strcmp (section->section_name, section_name) == 0)
696 static BusDesktopFileLine *
697 lookup_line (BusDesktopFile *desktop_file,
698 BusDesktopFileSection *section,
701 BusDesktopFileLine *line;
704 for (i = 0; i < section->n_lines; i++)
706 line = §ion->lines[i];
708 if (strcmp (line->key, keyname) == 0)
716 bus_desktop_file_get_raw (BusDesktopFile *desktop_file,
717 const char *section_name,
721 BusDesktopFileSection *section;
722 BusDesktopFileLine *line;
726 section = lookup_section (desktop_file, section_name);
731 line = lookup_line (desktop_file,
744 bus_desktop_file_get_string (BusDesktopFile *desktop_file,
753 if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
756 *val = _dbus_strdup (raw);
758 /* FIXME we don't distinguish "key not found" from "out of memory" here,