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 ']'. */
385 if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') ||
386 *name == '\n' || *name == '\t'))
396 parse_section_start (BusDesktopFileParser *parser, DBusError *error)
398 int line_end, eol_len;
401 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
403 if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
404 line_end = parser->len;
406 if (line_end - parser->pos <= 2 ||
407 _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
409 report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
410 parser_free (parser);
414 section_name = unescape_string (parser,
415 &parser->data, parser->pos + 1, line_end - 1,
418 if (section_name == NULL)
420 parser_free (parser);
424 if (!is_valid_section_name (section_name))
426 report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
427 parser_free (parser);
428 dbus_free (section_name);
432 if (open_section (parser, section_name) == NULL)
434 dbus_free (section_name);
435 parser_free (parser);
440 if (line_end == parser->len)
441 parser->pos = parser->len;
443 parser->pos = line_end + eol_len;
445 parser->line_num += 1;
447 dbus_free (section_name);
453 parse_key_value (BusDesktopFileParser *parser, DBusError *error)
455 int line_end, eol_len;
456 int key_start, key_end;
461 BusDesktopFileLine *line;
463 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
465 if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
466 line_end = parser->len;
470 while (p < line_end &&
471 (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
475 if (key_start == key_end)
477 report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
478 parser_free (parser);
482 /* We ignore locales for now */
483 if (p < line_end && _dbus_string_get_byte (&parser->data, p) == '[')
485 if (line_end == parser->len)
486 parser->pos = parser->len;
488 parser->pos = line_end + eol_len;
490 parser->line_num += 1;
495 /* Skip space before '=' */
496 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
499 if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
501 report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
502 parser_free (parser);
508 report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
509 parser_free (parser);
516 /* Skip space after '=' */
517 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
522 value = unescape_string (parser, &parser->data, value_start, line_end, error);
525 parser_free (parser);
529 line = new_line (parser);
533 parser_free (parser);
538 if (!_dbus_string_init (&key))
541 parser_free (parser);
546 if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
549 _dbus_string_free (&key);
551 parser_free (parser);
556 if (!_dbus_string_steal_data (&key, &tmp))
558 _dbus_string_free (&key);
560 parser_free (parser);
565 _dbus_string_free (&key);
570 if (line_end == parser->len)
571 parser->pos = parser->len;
573 parser->pos = line_end + eol_len;
575 parser->line_num += 1;
581 report_error (BusDesktopFileParser *parser,
583 const char *error_name,
586 const char *section_name = NULL;
588 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
590 if (parser->current_section != -1)
591 section_name = parser->desktop_file->sections[parser->current_section].section_name;
594 dbus_set_error (error, error_name,
595 "Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
597 dbus_set_error (error, error_name,
598 "Error at line %d: %s\n", parser->line_num, message);
603 dump_desktop_file (BusDesktopFile *file)
607 for (i = 0; i < file->n_sections; i++)
611 printf ("[%s]\n", file->sections[i].section_name);
613 for (j = 0; j < file->sections[i].n_lines; j++)
615 printf ("%s=%s\n", file->sections[i].lines[j].key,
616 file->sections[i].lines[j].value);
623 bus_desktop_file_load (DBusString *filename,
627 BusDesktopFileParser parser;
630 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
632 /* Clearly there's a race here, but it's just to make it unlikely
633 * that we do something silly, we still handle doing it below.
635 if (!_dbus_stat (filename, &sb, error))
638 if (sb.size > _DBUS_ONE_KILOBYTE * 128)
640 dbus_set_error (error, DBUS_ERROR_FAILED,
641 "Desktop file size (%ld bytes) is too large", (long) sb.size);
645 if (!_dbus_string_init (&str))
651 if (!_dbus_file_get_contents (&str, filename, error))
653 _dbus_string_free (&str);
657 if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
659 _dbus_string_free (&str);
660 dbus_set_error (error, DBUS_ERROR_FAILED,
665 parser.desktop_file = dbus_new0 (BusDesktopFile, 1);
666 if (parser.desktop_file == NULL)
668 _dbus_string_free (&str);
676 parser.len = _dbus_string_get_length (&parser.data);
677 parser.current_section = -1;
679 while (parser.pos < parser.len)
681 if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
683 if (!parse_section_start (&parser, error))
688 else if (is_blank_line (&parser) ||
689 _dbus_string_get_byte (&parser.data, parser.pos) == '#')
690 parse_comment_or_blank (&parser);
691 else if (parser.current_section < 0)
693 dbus_set_error(error, DBUS_ERROR_FAILED,
694 "invalid service file: key=value before [Section]");
699 if (!parse_key_value (&parser, error))
706 _dbus_string_free (&parser.data);
708 return parser.desktop_file;
711 static BusDesktopFileSection *
712 lookup_section (BusDesktopFile *desktop_file,
713 const char *section_name)
715 BusDesktopFileSection *section;
718 if (section_name == NULL)
721 for (i = 0; i < desktop_file->n_sections; i ++)
723 section = &desktop_file->sections[i];
725 if (strcmp (section->section_name, section_name) == 0)
732 static BusDesktopFileLine *
733 lookup_line (BusDesktopFile *desktop_file,
734 BusDesktopFileSection *section,
737 BusDesktopFileLine *line;
740 for (i = 0; i < section->n_lines; i++)
742 line = §ion->lines[i];
744 if (strcmp (line->key, keyname) == 0)
752 bus_desktop_file_get_raw (BusDesktopFile *desktop_file,
753 const char *section_name,
757 BusDesktopFileSection *section;
758 BusDesktopFileLine *line;
762 section = lookup_section (desktop_file, section_name);
767 line = lookup_line (desktop_file,
780 bus_desktop_file_get_string (BusDesktopFile *desktop_file,
788 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
792 if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
794 dbus_set_error (error, DBUS_ERROR_FAILED,
795 "No \"%s\" key in .service file\n", keyname);
799 *val = _dbus_strdup (raw);