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 BUS_HANDLE_OOM (lines = dbus_realloc (section->lines,
141 sizeof (BusDesktopFileLine) * new_n_lines));
142 section->lines = lines;
144 section->n_allocated_lines = new_n_lines;
148 grow_sections (BusDesktopFile *desktop_file)
151 BusDesktopFileSection *sections;
153 if (desktop_file->n_allocated_sections == 0)
156 new_n_sections = desktop_file->n_allocated_sections*2;
158 BUS_HANDLE_OOM (sections = dbus_realloc (desktop_file->sections,
159 sizeof (BusDesktopFileSection) * new_n_sections));
160 desktop_file->sections = sections;
162 desktop_file->n_allocated_sections = new_n_sections;
166 unescape_string (const DBusString *str, int pos, int end_pos)
170 /* len + 1 is enough, because unescaping never makes the
172 BUS_HANDLE_OOM (retval = dbus_malloc (end_pos - pos + 1));
176 while (pos < end_pos)
178 if (_dbus_string_get_byte (str, pos) == 0)
180 /* Found an embedded null */
185 if (_dbus_string_get_byte (str, pos) == '\\')
191 /* Escape at end of string */
196 switch (_dbus_string_get_byte (str, pos))
214 /* Invalid escape code */
222 *q++ =_dbus_string_get_byte (str, pos);
233 static BusDesktopFileSection*
234 new_section (BusDesktopFile *desktop_file,
239 if (desktop_file->n_allocated_sections == desktop_file->n_sections)
240 grow_sections (desktop_file);
242 n = desktop_file->n_sections++;
244 BUS_HANDLE_OOM (desktop_file->sections[n].section_name = _dbus_strdup (name));
246 desktop_file->sections[n].n_lines = 0;
247 desktop_file->sections[n].lines = NULL;
248 desktop_file->sections[n].n_allocated_lines = 0;
250 grow_lines_in_section (&desktop_file->sections[n]);
252 return &desktop_file->sections[n];
255 static BusDesktopFileSection*
256 open_section (BusDesktopFileParser *parser,
259 BusDesktopFileSection *section;
261 section = new_section (parser->desktop_file, name);
265 parser->current_section = parser->desktop_file->n_sections - 1;
266 _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section);
271 static BusDesktopFileLine *
272 new_line (BusDesktopFileParser *parser)
274 BusDesktopFileSection *section;
275 BusDesktopFileLine *line;
277 section = &parser->desktop_file->sections[parser->current_section];
279 if (section->n_allocated_lines == section->n_lines)
280 grow_lines_in_section (section);
282 line = §ion->lines[section->n_lines++];
284 memset (line, 0, sizeof (BusDesktopFileLine));
290 is_blank_line (BusDesktopFileParser *parser)
297 c = _dbus_string_get_byte (&parser->data, p);
299 while (c && c != '\n')
301 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'))
305 c = _dbus_string_get_byte (&parser->data, p);
312 parse_comment_or_blank (BusDesktopFileParser *parser)
316 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
317 line_end = parser->len;
319 if (line_end == parser->len)
320 parser->pos = parser->len;
322 parser->pos = line_end + 1;
324 parser->line_num += 1;
328 is_valid_section_name (const char *name)
330 /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'. */
334 if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') ||
335 *name == '\n' || *name == '\t'))
345 parse_section_start (BusDesktopFileParser *parser, DBusError *error)
350 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
351 line_end = parser->len;
353 if (line_end - parser->pos <= 2 ||
354 _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
356 report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
357 parser_free (parser);
361 section_name = unescape_string (&parser->data, parser->pos + 1, line_end - 1);
363 if (section_name == NULL)
365 report_error (parser, "Invalid escaping in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
366 parser_free (parser);
370 if (!is_valid_section_name (section_name))
372 report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
373 parser_free (parser);
374 dbus_free (section_name);
378 if (open_section (parser, section_name) == NULL)
380 dbus_free (section_name);
384 if (line_end == parser->len)
385 parser->pos = parser->len;
387 parser->pos = line_end + 1;
389 parser->line_num += 1;
391 dbus_free (section_name);
397 parse_key_value (BusDesktopFileParser *parser, DBusError *error)
400 int key_start, key_end;
405 BusDesktopFileLine *line;
407 if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
408 line_end = parser->len;
412 while (p < line_end &&
413 (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
417 if (key_start == key_end)
419 report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
420 parser_free (parser);
424 /* We ignore locales for now */
426 /* Skip space before '=' */
427 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
430 if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
432 report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
433 parser_free (parser);
439 report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
440 parser_free (parser);
447 /* Skip space after '=' */
448 while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
453 value = unescape_string (&parser->data, value_start, line_end);
456 report_error (parser, "Invalid escaping in value", BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
457 parser_free (parser);
461 line = new_line (parser);
463 BUS_HANDLE_OOM (_dbus_string_init (&key, key_end - key_start));
464 BUS_HANDLE_OOM (_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
466 BUS_HANDLE_OOM (_dbus_string_steal_data (&key, &tmp));
467 _dbus_string_free (&key);
472 if (line_end == parser->len)
473 parser->pos = parser->len;
475 parser->pos = line_end + 1;
477 parser->line_num += 1;
483 report_error (BusDesktopFileParser *parser,
485 const char *error_name,
488 const char *section_name = NULL;
490 if (parser->current_section != -1)
491 section_name = parser->desktop_file->sections[parser->current_section].section_name;
494 BUS_HANDLE_OOM (dbus_set_error (error, error_name,
495 "Error in section %s at line %d: %s\n", section_name, parser->line_num, message));
497 BUS_HANDLE_OOM (dbus_set_error (error, error_name,
498 "Error at line %d: %s\n", parser->line_num, message));
503 dump_desktop_file (BusDesktopFile *file)
507 for (i = 0; i < file->n_sections; i++)
511 printf ("[%s]\n", file->sections[i].section_name);
513 for (j = 0; j < file->sections[i].n_lines; j++)
515 printf ("%s=%s\n", file->sections[i].lines[j].key,
516 file->sections[i].lines[j].value);
523 bus_desktop_file_load (DBusString *filename,
527 DBusResultCode result_code;
528 BusDesktopFileParser parser;
530 /* FIXME: Check file size so we don't try to load a ridicously large file. */
532 BUS_HANDLE_OOM (_dbus_string_init (&str, _DBUS_INT_MAX));
534 BUS_HANDLE_OOM ((result_code = _dbus_file_get_contents (&str, filename)) !=
535 DBUS_RESULT_NO_MEMORY);
537 if (result_code != DBUS_RESULT_SUCCESS)
539 _dbus_string_free (&str);
541 /* FIXME: Set error */
545 if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
547 _dbus_string_free (&str);
549 /* FIXME: Set error */
553 BUS_HANDLE_OOM (parser.desktop_file = dbus_malloc0 (sizeof (BusDesktopFile)));
558 parser.len = _dbus_string_get_length (&parser.data);
559 parser.current_section = -1;
561 while (parser.pos < parser.len)
563 if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
565 if (!parse_section_start (&parser, error))
568 else if (is_blank_line (&parser) ||
569 _dbus_string_get_byte (&parser.data, parser.pos) == '#')
570 parse_comment_or_blank (&parser);
573 if (!parse_key_value (&parser, error))
578 _dbus_string_free (&parser.data);
580 return parser.desktop_file;
583 static BusDesktopFileSection *
584 lookup_section (BusDesktopFile *desktop_file,
585 const char *section_name)
587 BusDesktopFileSection *section;
590 if (section_name == NULL)
593 for (i = 0; i < desktop_file->n_sections; i ++)
595 section = &desktop_file->sections[i];
597 if (strcmp (section->section_name, section_name) == 0)
604 static BusDesktopFileLine *
605 lookup_line (BusDesktopFile *desktop_file,
606 BusDesktopFileSection *section,
609 BusDesktopFileLine *line;
612 for (i = 0; i < section->n_lines; i++)
614 line = §ion->lines[i];
616 if (strcmp (line->key, keyname) == 0)
624 bus_desktop_file_get_raw (BusDesktopFile *desktop_file,
625 const char *section_name,
629 BusDesktopFileSection *section;
630 BusDesktopFileLine *line;
634 section = lookup_section (desktop_file, section_name);
639 line = lookup_line (desktop_file,
652 bus_desktop_file_get_string (BusDesktopFile *desktop_file,
661 if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
664 BUS_HANDLE_OOM (*val = _dbus_strdup (raw));