1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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 2.1
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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;
52 * Parser for service files.
56 DBusString data; /**< The data from the file */
58 BusDesktopFile *desktop_file; /**< The resulting object */
59 int current_section; /**< The current section being parsed */
61 int pos; /**< Current position */
62 int len; /**< Length */
63 int line_num; /**< Current line number */
65 } BusDesktopFileParser;
67 #define VALID_KEY_CHAR 1
68 #define VALID_LOCALE_CHAR 2
69 static unsigned char valid[256] = {
70 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
71 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
72 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x3 , 0x2 , 0x0 ,
73 0x3 , 0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 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
84 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
85 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
88 static void report_error (BusDesktopFileParser *parser,
90 const char *error_name,
94 parser_free (BusDesktopFileParser *parser)
96 bus_desktop_file_free (parser->desktop_file);
98 _dbus_string_free (&parser->data);
102 bus_desktop_file_line_free (BusDesktopFileLine *line)
104 dbus_free (line->key);
105 dbus_free (line->value);
109 bus_desktop_file_section_free (BusDesktopFileSection *section)
113 for (i = 0; i < section->n_lines; i++)
114 bus_desktop_file_line_free (§ion->lines[i]);
116 dbus_free (section->lines);
117 dbus_free (section->section_name);
121 bus_desktop_file_free (BusDesktopFile *desktop_file)
125 for (i = 0; i < desktop_file->n_sections; i++)
126 bus_desktop_file_section_free (&desktop_file->sections[i]);
127 dbus_free (desktop_file->sections);
129 dbus_free (desktop_file);
133 grow_lines_in_section (BusDesktopFileSection *section)
135 BusDesktopFileLine *lines;
139 if (section->n_allocated_lines == 0)
142 new_n_lines = section->n_allocated_lines*2;
144 lines = dbus_realloc (section->lines,
145 sizeof (BusDesktopFileLine) * new_n_lines);
150 section->lines = lines;
151 section->n_allocated_lines = new_n_lines;
157 grow_sections (BusDesktopFile *desktop_file)
160 BusDesktopFileSection *sections;
162 if (desktop_file->n_allocated_sections == 0)
165 new_n_sections = desktop_file->n_allocated_sections*2;
167 sections = dbus_realloc (desktop_file->sections,
168 sizeof (BusDesktopFileSection) * new_n_sections);
169 if (sections == NULL)
172 desktop_file->sections = sections;
174 desktop_file->n_allocated_sections = new_n_sections;
180 unescape_string (BusDesktopFileParser *parser,
181 const DBusString *str,
188 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
190 /* len + 1 is enough, because unescaping never makes the
193 retval = dbus_malloc (end_pos - pos + 1);
202 while (pos < end_pos)
204 if (_dbus_string_get_byte (str, pos) == 0)
206 /* Found an embedded null */
208 report_error (parser, "Text to be unescaped contains embedded nul",
209 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
213 if (_dbus_string_get_byte (str, pos) == '\\')
219 /* Escape at end of string */
221 report_error (parser, "Text to be unescaped ended in \\",
222 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
226 switch (_dbus_string_get_byte (str, pos))
244 /* Invalid escape code */
246 report_error (parser, "Text to be unescaped had invalid escape sequence",
247 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
254 *q++ =_dbus_string_get_byte (str, pos);
265 static BusDesktopFileSection*
266 new_section (BusDesktopFile *desktop_file,
272 if (desktop_file->n_allocated_sections == desktop_file->n_sections)
274 if (!grow_sections (desktop_file))
278 name_copy = _dbus_strdup (name);
279 if (name_copy == NULL)
282 n = desktop_file->n_sections;
283 desktop_file->sections[n].section_name = name_copy;
285 desktop_file->sections[n].n_lines = 0;
286 desktop_file->sections[n].lines = NULL;
287 desktop_file->sections[n].n_allocated_lines = 0;
289 if (!grow_lines_in_section (&desktop_file->sections[n]))
291 dbus_free (desktop_file->sections[n].section_name);
292 desktop_file->sections[n].section_name = NULL;
296 desktop_file->n_sections += 1;
298 return &desktop_file->sections[n];
301 static BusDesktopFileSection*
302 open_section (BusDesktopFileParser *parser,
305 BusDesktopFileSection *section;
307 section = new_section (parser->desktop_file, name);
311 parser->current_section = parser->desktop_file->n_sections - 1;
312 _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section);
317 static BusDesktopFileLine *
318 new_line (BusDesktopFileParser *parser)
320 BusDesktopFileSection *section;
321 BusDesktopFileLine *line;
323 section = &parser->desktop_file->sections[parser->current_section];
325 if (section->n_allocated_lines == section->n_lines)
327 if (!grow_lines_in_section (section))
331 line = §ion->lines[section->n_lines++];
339 is_blank_line (BusDesktopFileParser *parser)
346 c = _dbus_string_get_byte (&parser->data, p);
348 while (c && c != '\n')
350 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'))
354 c = _dbus_string_get_byte (&parser->data, p);
361 parse_comment_or_blank (BusDesktopFileParser *parser)
363 int line_end, eol_len;
365 if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
366 line_end = parser->len;
368 if (line_end == parser->len)
369 parser->pos = parser->len;
371 parser->pos = line_end + eol_len;
373 parser->line_num += 1;
377 is_valid_section_name (const char *name)
379 /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'. */
383 if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') ||
384 *name == '\n' || *name == '\t'))
394 parse_section_start (BusDesktopFileParser *parser, DBusError *error)
396 int line_end, eol_len;
399 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
401 if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
402 line_end = parser->len;
404 if (line_end - parser->pos <= 2 ||
405 _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
407 report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
408 parser_free (parser);
412 section_name = unescape_string (parser,
413 &parser->data, parser->pos + 1, line_end - 1,
416 if (section_name == NULL)
418 parser_free (parser);
422 if (!is_valid_section_name (section_name))
424 report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
425 parser_free (parser);
426 dbus_free (section_name);
430 if (open_section (parser, section_name) == NULL)
432 dbus_free (section_name);
433 parser_free (parser);
438 if (line_end == parser->len)
439 parser->pos = parser->len;
441 parser->pos = line_end + eol_len;
443 parser->line_num += 1;
445 dbus_free (section_name);
451 parse_key_value (BusDesktopFileParser *parser, DBusError *error)
453 int line_end, eol_len;
454 int key_start, key_end;
459 BusDesktopFileLine *line;
461 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
463 if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
464 line_end = parser->len;
468 while (p < line_end &&
469 (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
473 if (key_start == key_end)
475 report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
476 parser_free (parser);
480 /* We ignore locales for now */
481 if (p < line_end && _dbus_string_get_byte (&parser->data, p) == '[')
483 if (line_end == parser->len)
484 parser->pos = parser->len;
486 parser->pos = line_end + eol_len;
488 parser->line_num += 1;
493 /* Skip space before '=' */
494 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
497 if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
499 report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
500 parser_free (parser);
506 report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
507 parser_free (parser);
514 /* Skip space after '=' */
515 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
520 value = unescape_string (parser, &parser->data, value_start, line_end, error);
523 parser_free (parser);
527 line = new_line (parser);
531 parser_free (parser);
536 if (!_dbus_string_init (&key))
539 parser_free (parser);
544 if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
547 _dbus_string_free (&key);
549 parser_free (parser);
554 if (!_dbus_string_steal_data (&key, &tmp))
556 _dbus_string_free (&key);
558 parser_free (parser);
563 _dbus_string_free (&key);
568 if (line_end == parser->len)
569 parser->pos = parser->len;
571 parser->pos = line_end + eol_len;
573 parser->line_num += 1;
579 report_error (BusDesktopFileParser *parser,
581 const char *error_name,
584 const char *section_name = NULL;
586 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
588 if (parser->current_section != -1)
589 section_name = parser->desktop_file->sections[parser->current_section].section_name;
592 dbus_set_error (error, error_name,
593 "Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
595 dbus_set_error (error, error_name,
596 "Error at line %d: %s\n", parser->line_num, message);
601 dump_desktop_file (BusDesktopFile *file)
605 for (i = 0; i < file->n_sections; i++)
609 printf ("[%s]\n", file->sections[i].section_name);
611 for (j = 0; j < file->sections[i].n_lines; j++)
613 printf ("%s=%s\n", file->sections[i].lines[j].key,
614 file->sections[i].lines[j].value);
621 bus_desktop_file_load (DBusString *filename,
625 BusDesktopFileParser parser;
628 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
630 /* Clearly there's a race here, but it's just to make it unlikely
631 * that we do something silly, we still handle doing it below.
633 if (!_dbus_stat (filename, &sb, error))
636 if (sb.size > _DBUS_ONE_KILOBYTE * 128)
638 dbus_set_error (error, DBUS_ERROR_FAILED,
639 "Desktop file size (%ld bytes) is too large", (long) sb.size);
643 if (!_dbus_string_init (&str))
649 if (!_dbus_file_get_contents (&str, filename, error))
651 _dbus_string_free (&str);
655 if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
657 _dbus_string_free (&str);
658 dbus_set_error (error, DBUS_ERROR_FAILED,
663 parser.desktop_file = dbus_new0 (BusDesktopFile, 1);
664 if (parser.desktop_file == NULL)
666 _dbus_string_free (&str);
674 parser.len = _dbus_string_get_length (&parser.data);
675 parser.current_section = -1;
677 while (parser.pos < parser.len)
679 if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
681 if (!parse_section_start (&parser, error))
686 else if (is_blank_line (&parser) ||
687 _dbus_string_get_byte (&parser.data, parser.pos) == '#')
688 parse_comment_or_blank (&parser);
691 if (!parse_key_value (&parser, error))
698 _dbus_string_free (&parser.data);
700 return parser.desktop_file;
703 static BusDesktopFileSection *
704 lookup_section (BusDesktopFile *desktop_file,
705 const char *section_name)
707 BusDesktopFileSection *section;
710 if (section_name == NULL)
713 for (i = 0; i < desktop_file->n_sections; i ++)
715 section = &desktop_file->sections[i];
717 if (strcmp (section->section_name, section_name) == 0)
724 static BusDesktopFileLine *
725 lookup_line (BusDesktopFile *desktop_file,
726 BusDesktopFileSection *section,
729 BusDesktopFileLine *line;
732 for (i = 0; i < section->n_lines; i++)
734 line = §ion->lines[i];
736 if (strcmp (line->key, keyname) == 0)
744 bus_desktop_file_get_raw (BusDesktopFile *desktop_file,
745 const char *section_name,
749 BusDesktopFileSection *section;
750 BusDesktopFileLine *line;
754 section = lookup_section (desktop_file, section_name);
759 line = lookup_line (desktop_file,
772 bus_desktop_file_get_string (BusDesktopFile *desktop_file,
780 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
784 if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
786 dbus_set_error (error, DBUS_ERROR_FAILED,
787 "No \"%s\" key in .service file\n", keyname);
791 *val = _dbus_strdup (raw);