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
26 #include <dbus/dbus-sysdeps.h>
27 #include <dbus/dbus-internals.h>
28 #include "desktop-file.h"
42 BusDesktopFileLine *lines;
43 int n_allocated_lines;
44 } BusDesktopFileSection;
49 BusDesktopFileSection *sections;
50 int n_allocated_sections;
54 * Parser for service files.
58 DBusString data; /**< The data from the file */
60 BusDesktopFile *desktop_file; /**< The resulting object */
61 int current_section; /**< The current section being parsed */
63 int pos; /**< Current position */
64 int len; /**< Length */
65 int line_num; /**< Current line number */
67 } BusDesktopFileParser;
69 #define VALID_KEY_CHAR 1
70 #define VALID_LOCALE_CHAR 2
71 static 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 , 0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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 const char *error_name,
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 lines = dbus_realloc (section->lines,
147 sizeof (BusDesktopFileLine) * new_n_lines);
152 section->lines = lines;
153 section->n_allocated_lines = new_n_lines;
159 grow_sections (BusDesktopFile *desktop_file)
162 BusDesktopFileSection *sections;
164 if (desktop_file->n_allocated_sections == 0)
167 new_n_sections = desktop_file->n_allocated_sections*2;
169 sections = dbus_realloc (desktop_file->sections,
170 sizeof (BusDesktopFileSection) * new_n_sections);
171 if (sections == NULL)
174 desktop_file->sections = sections;
176 desktop_file->n_allocated_sections = new_n_sections;
182 unescape_string (BusDesktopFileParser *parser,
183 const DBusString *str,
190 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
192 /* len + 1 is enough, because unescaping never makes the
195 retval = dbus_malloc (end_pos - pos + 1);
204 while (pos < end_pos)
206 if (_dbus_string_get_byte (str, pos) == 0)
208 /* Found an embedded null */
210 report_error (parser, "Text to be unescaped contains embedded nul",
211 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
215 if (_dbus_string_get_byte (str, pos) == '\\')
221 /* Escape at end of string */
223 report_error (parser, "Text to be unescaped ended in \\",
224 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
228 switch (_dbus_string_get_byte (str, pos))
246 /* Invalid escape code */
248 report_error (parser, "Text to be unescaped had invalid escape sequence",
249 BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
256 *q++ =_dbus_string_get_byte (str, pos);
267 static BusDesktopFileSection*
268 new_section (BusDesktopFile *desktop_file,
274 if (desktop_file->n_allocated_sections == desktop_file->n_sections)
276 if (!grow_sections (desktop_file))
280 name_copy = _dbus_strdup (name);
281 if (name_copy == NULL)
284 n = desktop_file->n_sections;
285 desktop_file->sections[n].section_name = name_copy;
287 desktop_file->sections[n].n_lines = 0;
288 desktop_file->sections[n].lines = NULL;
289 desktop_file->sections[n].n_allocated_lines = 0;
291 if (!grow_lines_in_section (&desktop_file->sections[n]))
293 dbus_free (desktop_file->sections[n].section_name);
294 desktop_file->sections[n].section_name = NULL;
298 desktop_file->n_sections += 1;
300 return &desktop_file->sections[n];
303 static BusDesktopFileSection*
304 open_section (BusDesktopFileParser *parser,
307 BusDesktopFileSection *section;
309 section = new_section (parser->desktop_file, name);
313 parser->current_section = parser->desktop_file->n_sections - 1;
314 _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section);
319 static BusDesktopFileLine *
320 new_line (BusDesktopFileParser *parser)
322 BusDesktopFileSection *section;
323 BusDesktopFileLine *line;
325 section = &parser->desktop_file->sections[parser->current_section];
327 if (section->n_allocated_lines == section->n_lines)
329 if (!grow_lines_in_section (section))
333 line = §ion->lines[section->n_lines++];
341 is_blank_line (BusDesktopFileParser *parser)
348 c = _dbus_string_get_byte (&parser->data, p);
350 while (c && c != '\n')
352 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'))
356 c = _dbus_string_get_byte (&parser->data, p);
363 parse_comment_or_blank (BusDesktopFileParser *parser)
365 int line_end, eol_len;
367 if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
368 line_end = parser->len;
370 if (line_end == parser->len)
371 parser->pos = parser->len;
373 parser->pos = line_end + eol_len;
375 parser->line_num += 1;
379 is_valid_section_name (const char *name)
381 /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'.
383 * We don't use isprint() here because it's locale-dependent. ASCII
384 * characters <= 0x1f and 0x7f are control characters, and bytes with
385 * values >= 0x80 aren't ASCII. 0x20 is a space, which we must allow,
386 * not least because DBUS_SERVICE_SECTION contains one. */
390 if (*name <= 0x1f || *name >= 0x7f || *name == '[' || *name == ']')
400 parse_section_start (BusDesktopFileParser *parser, DBusError *error)
402 int line_end, eol_len;
405 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
407 if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
408 line_end = parser->len;
410 if (line_end - parser->pos <= 2 ||
411 _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
413 report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
414 parser_free (parser);
418 section_name = unescape_string (parser,
419 &parser->data, parser->pos + 1, line_end - 1,
422 if (section_name == NULL)
424 parser_free (parser);
428 if (!is_valid_section_name (section_name))
430 report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
431 parser_free (parser);
432 dbus_free (section_name);
436 if (open_section (parser, section_name) == NULL)
438 dbus_free (section_name);
439 parser_free (parser);
444 if (line_end == parser->len)
445 parser->pos = parser->len;
447 parser->pos = line_end + eol_len;
449 parser->line_num += 1;
451 dbus_free (section_name);
457 parse_key_value (BusDesktopFileParser *parser, DBusError *error)
459 int line_end, eol_len;
460 int key_start, key_end;
465 BusDesktopFileLine *line;
467 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
469 if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
470 line_end = parser->len;
474 while (p < line_end &&
475 (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
479 if (key_start == key_end)
481 report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
482 parser_free (parser);
486 /* We ignore locales for now */
487 if (p < line_end && _dbus_string_get_byte (&parser->data, p) == '[')
489 if (line_end == parser->len)
490 parser->pos = parser->len;
492 parser->pos = line_end + eol_len;
494 parser->line_num += 1;
499 /* Skip space before '=' */
500 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
503 if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
505 report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
506 parser_free (parser);
512 report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
513 parser_free (parser);
520 /* Skip space after '=' */
521 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
526 value = unescape_string (parser, &parser->data, value_start, line_end, error);
529 parser_free (parser);
533 line = new_line (parser);
537 parser_free (parser);
542 if (!_dbus_string_init (&key))
545 parser_free (parser);
550 if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
553 _dbus_string_free (&key);
555 parser_free (parser);
560 if (!_dbus_string_steal_data (&key, &tmp))
562 _dbus_string_free (&key);
564 parser_free (parser);
569 _dbus_string_free (&key);
574 if (line_end == parser->len)
575 parser->pos = parser->len;
577 parser->pos = line_end + eol_len;
579 parser->line_num += 1;
585 report_error (BusDesktopFileParser *parser,
587 const char *error_name,
590 const char *section_name = NULL;
592 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
594 if (parser->current_section != -1)
595 section_name = parser->desktop_file->sections[parser->current_section].section_name;
598 dbus_set_error (error, error_name,
599 "Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
601 dbus_set_error (error, error_name,
602 "Error at line %d: %s\n", parser->line_num, message);
607 dump_desktop_file (BusDesktopFile *file)
611 for (i = 0; i < file->n_sections; i++)
615 printf ("[%s]\n", file->sections[i].section_name);
617 for (j = 0; j < file->sections[i].n_lines; j++)
619 printf ("%s=%s\n", file->sections[i].lines[j].key,
620 file->sections[i].lines[j].value);
627 bus_desktop_file_load (DBusString *filename,
631 BusDesktopFileParser parser;
634 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
636 /* Clearly there's a race here, but it's just to make it unlikely
637 * that we do something silly, we still handle doing it below.
639 if (!_dbus_stat (filename, &sb, error))
642 if (sb.size > _DBUS_ONE_KILOBYTE * 128)
644 dbus_set_error (error, DBUS_ERROR_FAILED,
645 "Desktop file size (%ld bytes) is too large", (long) sb.size);
649 if (!_dbus_string_init (&str))
655 if (!_dbus_file_get_contents (&str, filename, error))
657 _dbus_string_free (&str);
661 if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
663 _dbus_string_free (&str);
664 dbus_set_error (error, DBUS_ERROR_FAILED,
669 parser.desktop_file = dbus_new0 (BusDesktopFile, 1);
670 if (parser.desktop_file == NULL)
672 _dbus_string_free (&str);
680 parser.len = _dbus_string_get_length (&parser.data);
681 parser.current_section = -1;
683 while (parser.pos < parser.len)
685 if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
687 if (!parse_section_start (&parser, error))
692 else if (is_blank_line (&parser) ||
693 _dbus_string_get_byte (&parser.data, parser.pos) == '#')
694 parse_comment_or_blank (&parser);
695 else if (parser.current_section < 0)
697 dbus_set_error(error, DBUS_ERROR_FAILED,
698 "invalid service file: key=value before [Section]");
703 if (!parse_key_value (&parser, error))
710 _dbus_string_free (&parser.data);
712 return parser.desktop_file;
715 static BusDesktopFileSection *
716 lookup_section (BusDesktopFile *desktop_file,
717 const char *section_name)
719 BusDesktopFileSection *section;
722 if (section_name == NULL)
725 for (i = 0; i < desktop_file->n_sections; i ++)
727 section = &desktop_file->sections[i];
729 if (strcmp (section->section_name, section_name) == 0)
736 static BusDesktopFileLine *
737 lookup_line (BusDesktopFile *desktop_file,
738 BusDesktopFileSection *section,
741 BusDesktopFileLine *line;
744 for (i = 0; i < section->n_lines; i++)
746 line = §ion->lines[i];
748 if (strcmp (line->key, keyname) == 0)
756 bus_desktop_file_get_raw (BusDesktopFile *desktop_file,
757 const char *section_name,
761 BusDesktopFileSection *section;
762 BusDesktopFileLine *line;
766 section = lookup_section (desktop_file, section_name);
771 line = lookup_line (desktop_file,
784 bus_desktop_file_get_string (BusDesktopFile *desktop_file,
792 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
796 if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
798 dbus_set_error (error, DBUS_ERROR_FAILED,
799 "No \"%s\" key in .service file\n", keyname);
803 *val = _dbus_strdup (raw);