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 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., 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;
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 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 , 0x3 , 0x3 , 0x3 , 0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 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++];
333 memset (line, 0, sizeof (BusDesktopFileLine));
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)
365 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
366 line_end = parser->len;
368 if (line_end == parser->len)
369 parser->pos = parser->len;
371 parser->pos = line_end + 1;
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)
399 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
401 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
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);
436 if (line_end == parser->len)
437 parser->pos = parser->len;
439 parser->pos = line_end + 1;
441 parser->line_num += 1;
443 dbus_free (section_name);
449 parse_key_value (BusDesktopFileParser *parser, DBusError *error)
452 int key_start, key_end;
457 BusDesktopFileLine *line;
459 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
461 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
462 line_end = parser->len;
466 while (p < line_end &&
467 (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
471 if (key_start == key_end)
473 report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
474 parser_free (parser);
478 /* We ignore locales for now */
480 /* Skip space before '=' */
481 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
484 if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
486 report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
487 parser_free (parser);
493 report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
494 parser_free (parser);
501 /* Skip space after '=' */
502 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
507 value = unescape_string (parser, &parser->data, value_start, line_end, error);
510 parser_free (parser);
514 line = new_line (parser);
517 parser_free (parser);
521 if (!_dbus_string_init (&key))
523 parser_free (parser);
527 if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
530 parser_free (parser);
534 if (!_dbus_string_steal_data (&key, &tmp))
536 parser_free (parser);
540 _dbus_string_free (&key);
545 if (line_end == parser->len)
546 parser->pos = parser->len;
548 parser->pos = line_end + 1;
550 parser->line_num += 1;
556 report_error (BusDesktopFileParser *parser,
558 const char *error_name,
561 const char *section_name = NULL;
563 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
565 if (parser->current_section != -1)
566 section_name = parser->desktop_file->sections[parser->current_section].section_name;
569 dbus_set_error (error, error_name,
570 "Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
572 dbus_set_error (error, error_name,
573 "Error at line %d: %s\n", parser->line_num, message);
578 dump_desktop_file (BusDesktopFile *file)
582 for (i = 0; i < file->n_sections; i++)
586 printf ("[%s]\n", file->sections[i].section_name);
588 for (j = 0; j < file->sections[i].n_lines; j++)
590 printf ("%s=%s\n", file->sections[i].lines[j].key,
591 file->sections[i].lines[j].value);
598 bus_desktop_file_load (DBusString *filename,
602 BusDesktopFileParser parser;
605 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
607 /* Clearly there's a race here, but it's just to make it unlikely
608 * that we do something silly, we still handle doing it below.
610 if (!_dbus_stat (filename, &sb, error))
613 if (sb.size > _DBUS_ONE_KILOBYTE * 128)
615 dbus_set_error (error, DBUS_ERROR_FAILED,
616 "Desktop file size (%ld bytes) is too large", (long) sb.size);
620 if (!_dbus_string_init (&str))
623 if (!_dbus_file_get_contents (&str, filename, error))
625 _dbus_string_free (&str);
629 if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
631 _dbus_string_free (&str);
632 dbus_set_error (error, DBUS_ERROR_FAILED,
637 parser.desktop_file = dbus_new0 (BusDesktopFile, 1);
638 if (parser.desktop_file == NULL)
640 _dbus_string_free (&str);
648 parser.len = _dbus_string_get_length (&parser.data);
649 parser.current_section = -1;
651 while (parser.pos < parser.len)
653 if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
655 if (!parse_section_start (&parser, error))
657 _dbus_string_free (&parser.data);
661 else if (is_blank_line (&parser) ||
662 _dbus_string_get_byte (&parser.data, parser.pos) == '#')
663 parse_comment_or_blank (&parser);
666 if (!parse_key_value (&parser, error))
668 _dbus_string_free (&parser.data);
674 _dbus_string_free (&parser.data);
676 return parser.desktop_file;
679 static BusDesktopFileSection *
680 lookup_section (BusDesktopFile *desktop_file,
681 const char *section_name)
683 BusDesktopFileSection *section;
686 if (section_name == NULL)
689 for (i = 0; i < desktop_file->n_sections; i ++)
691 section = &desktop_file->sections[i];
693 if (strcmp (section->section_name, section_name) == 0)
700 static BusDesktopFileLine *
701 lookup_line (BusDesktopFile *desktop_file,
702 BusDesktopFileSection *section,
705 BusDesktopFileLine *line;
708 for (i = 0; i < section->n_lines; i++)
710 line = §ion->lines[i];
712 if (strcmp (line->key, keyname) == 0)
720 bus_desktop_file_get_raw (BusDesktopFile *desktop_file,
721 const char *section_name,
725 BusDesktopFileSection *section;
726 BusDesktopFileLine *line;
730 section = lookup_section (desktop_file, section_name);
735 line = lookup_line (desktop_file,
748 bus_desktop_file_get_string (BusDesktopFile *desktop_file,
757 if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
760 *val = _dbus_strdup (raw);
762 /* FIXME we don't distinguish "key not found" from "out of memory" here,