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 /* len + 1 is enough, because unescaping never makes the
187 retval = dbus_malloc (end_pos - pos + 1);
196 while (pos < end_pos)
198 if (_dbus_string_get_byte (str, pos) == 0)
200 /* Found an embedded null */
202 report_error (parser, "Text to be unescaped contains embedded nul",
203 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
207 if (_dbus_string_get_byte (str, pos) == '\\')
213 /* Escape at end of string */
215 report_error (parser, "Text to be unescaped ended in \\",
216 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
220 switch (_dbus_string_get_byte (str, pos))
238 /* Invalid escape code */
240 report_error (parser, "Text to be unescaped had invalid escape sequence",
241 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
248 *q++ =_dbus_string_get_byte (str, pos);
259 static BusDesktopFileSection*
260 new_section (BusDesktopFile *desktop_file,
266 if (desktop_file->n_allocated_sections == desktop_file->n_sections)
268 if (!grow_sections (desktop_file))
272 name_copy = _dbus_strdup (name);
273 if (name_copy == NULL)
276 n = desktop_file->n_sections;
277 desktop_file->sections[n].section_name = name_copy;
279 desktop_file->sections[n].n_lines = 0;
280 desktop_file->sections[n].lines = NULL;
281 desktop_file->sections[n].n_allocated_lines = 0;
283 if (!grow_lines_in_section (&desktop_file->sections[n]))
285 dbus_free (desktop_file->sections[n].section_name);
286 desktop_file->sections[n].section_name = NULL;
290 desktop_file->n_sections += 1;
292 return &desktop_file->sections[n];
295 static BusDesktopFileSection*
296 open_section (BusDesktopFileParser *parser,
299 BusDesktopFileSection *section;
301 section = new_section (parser->desktop_file, name);
305 parser->current_section = parser->desktop_file->n_sections - 1;
306 _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section);
311 static BusDesktopFileLine *
312 new_line (BusDesktopFileParser *parser)
314 BusDesktopFileSection *section;
315 BusDesktopFileLine *line;
317 section = &parser->desktop_file->sections[parser->current_section];
319 if (section->n_allocated_lines == section->n_lines)
321 if (!grow_lines_in_section (section))
325 line = §ion->lines[section->n_lines++];
327 memset (line, 0, sizeof (BusDesktopFileLine));
333 is_blank_line (BusDesktopFileParser *parser)
340 c = _dbus_string_get_byte (&parser->data, p);
342 while (c && c != '\n')
344 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'))
348 c = _dbus_string_get_byte (&parser->data, p);
355 parse_comment_or_blank (BusDesktopFileParser *parser)
359 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
360 line_end = parser->len;
362 if (line_end == parser->len)
363 parser->pos = parser->len;
365 parser->pos = line_end + 1;
367 parser->line_num += 1;
371 is_valid_section_name (const char *name)
373 /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'. */
377 if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') ||
378 *name == '\n' || *name == '\t'))
388 parse_section_start (BusDesktopFileParser *parser, DBusError *error)
393 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
394 line_end = parser->len;
396 if (line_end - parser->pos <= 2 ||
397 _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
399 report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
400 parser_free (parser);
404 section_name = unescape_string (parser,
405 &parser->data, parser->pos + 1, line_end - 1,
408 if (section_name == NULL)
410 parser_free (parser);
414 if (!is_valid_section_name (section_name))
416 report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
417 parser_free (parser);
418 dbus_free (section_name);
422 if (open_section (parser, section_name) == NULL)
424 dbus_free (section_name);
428 if (line_end == parser->len)
429 parser->pos = parser->len;
431 parser->pos = line_end + 1;
433 parser->line_num += 1;
435 dbus_free (section_name);
441 parse_key_value (BusDesktopFileParser *parser, DBusError *error)
444 int key_start, key_end;
449 BusDesktopFileLine *line;
451 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
452 line_end = parser->len;
456 while (p < line_end &&
457 (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
461 if (key_start == key_end)
463 report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
464 parser_free (parser);
468 /* We ignore locales for now */
470 /* Skip space before '=' */
471 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
474 if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
476 report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
477 parser_free (parser);
483 report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
484 parser_free (parser);
491 /* Skip space after '=' */
492 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
497 value = unescape_string (parser, &parser->data, value_start, line_end, error);
500 parser_free (parser);
504 line = new_line (parser);
507 parser_free (parser);
511 if (!_dbus_string_init (&key, key_end - key_start))
513 parser_free (parser);
517 if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
520 parser_free (parser);
524 if (!_dbus_string_steal_data (&key, &tmp))
526 parser_free (parser);
530 _dbus_string_free (&key);
535 if (line_end == parser->len)
536 parser->pos = parser->len;
538 parser->pos = line_end + 1;
540 parser->line_num += 1;
546 report_error (BusDesktopFileParser *parser,
548 const char *error_name,
551 const char *section_name = NULL;
553 if (parser->current_section != -1)
554 section_name = parser->desktop_file->sections[parser->current_section].section_name;
557 dbus_set_error (error, error_name,
558 "Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
560 dbus_set_error (error, error_name,
561 "Error at line %d: %s\n", parser->line_num, message);
566 dump_desktop_file (BusDesktopFile *file)
570 for (i = 0; i < file->n_sections; i++)
574 printf ("[%s]\n", file->sections[i].section_name);
576 for (j = 0; j < file->sections[i].n_lines; j++)
578 printf ("%s=%s\n", file->sections[i].lines[j].key,
579 file->sections[i].lines[j].value);
586 bus_desktop_file_load (DBusString *filename,
590 BusDesktopFileParser parser;
593 /* Clearly there's a race here, but it's just to make it unlikely
594 * that we do something silly, we still handle doing it below.
596 if (!_dbus_stat (filename, &sb, error))
599 if (sb.size > _DBUS_ONE_KILOBYTE * 128)
601 dbus_set_error (error, DBUS_ERROR_FAILED,
602 "Desktop file size (%ld bytes) is too large", (long) sb.size);
606 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
609 if (!_dbus_file_get_contents (&str, filename, error))
611 _dbus_string_free (&str);
615 if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
617 _dbus_string_free (&str);
618 dbus_set_error (error, DBUS_ERROR_FAILED,
623 parser.desktop_file = dbus_new0 (BusDesktopFile, 1);
624 if (parser.desktop_file == NULL)
626 _dbus_string_free (&str);
634 parser.len = _dbus_string_get_length (&parser.data);
635 parser.current_section = -1;
637 while (parser.pos < parser.len)
639 if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
641 if (!parse_section_start (&parser, error))
643 _dbus_string_free (&parser.data);
647 else if (is_blank_line (&parser) ||
648 _dbus_string_get_byte (&parser.data, parser.pos) == '#')
649 parse_comment_or_blank (&parser);
652 if (!parse_key_value (&parser, error))
654 _dbus_string_free (&parser.data);
660 _dbus_string_free (&parser.data);
662 return parser.desktop_file;
665 static BusDesktopFileSection *
666 lookup_section (BusDesktopFile *desktop_file,
667 const char *section_name)
669 BusDesktopFileSection *section;
672 if (section_name == NULL)
675 for (i = 0; i < desktop_file->n_sections; i ++)
677 section = &desktop_file->sections[i];
679 if (strcmp (section->section_name, section_name) == 0)
686 static BusDesktopFileLine *
687 lookup_line (BusDesktopFile *desktop_file,
688 BusDesktopFileSection *section,
691 BusDesktopFileLine *line;
694 for (i = 0; i < section->n_lines; i++)
696 line = §ion->lines[i];
698 if (strcmp (line->key, keyname) == 0)
706 bus_desktop_file_get_raw (BusDesktopFile *desktop_file,
707 const char *section_name,
711 BusDesktopFileSection *section;
712 BusDesktopFileLine *line;
716 section = lookup_section (desktop_file, section_name);
721 line = lookup_line (desktop_file,
734 bus_desktop_file_get_string (BusDesktopFile *desktop_file,
743 if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
746 *val = _dbus_strdup (raw);
748 /* FIXME we don't distinguish "key not found" from "out of memory" here,