Merge "Optional autogen.sh flag --enable-kdbus-transport added allowing to compile...
[platform/upstream/dbus.git] / bus / desktop-file.c
index 138095d..bfeb72e 100644 (file)
@@ -1,10 +1,10 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* desktop-file.c  .desktop file parser
  *
  * Copyright (C) 2003  CodeFactory AB
  * Copyright (C) 2003  Red Hat Inc.
  *
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
+
+#include <config.h>
 #include <dbus/dbus-sysdeps.h>
 #include <dbus/dbus-internals.h>
 #include "desktop-file.h"
-
-typedef enum 
-{
-  BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX,
-  BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES,
-  BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS
-} BusDesktopParseError;
+#include "utils.h"
 
 typedef struct
 {
@@ -54,21 +50,25 @@ struct BusDesktopFile
   int n_allocated_sections;
 };
 
+/**
+ * Parser for service files.
+ */
 typedef struct
 {
-  DBusString data;
+  DBusString data; /**< The data from the file */
 
-  BusDesktopFile *desktop_file;
-  int current_section;
+  BusDesktopFile *desktop_file; /**< The resulting object */
+  int current_section;    /**< The current section being parsed */
   
-  int pos, len;
-  int line_num;
+  int pos;          /**< Current position */
+  int len;          /**< Length */
+  int line_num;     /**< Current line number */
   
 } BusDesktopFileParser;
 
 #define VALID_KEY_CHAR 1
 #define VALID_LOCALE_CHAR 2
-unsigned char valid[256] = { 
+static unsigned char valid[256] = { 
    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x3 , 0x2 , 0x0 , 
@@ -87,10 +87,10 @@ unsigned char valid[256] = {
    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
 };
 
-static void report_error (BusDesktopFileParser   *parser,
-                         char                   *message,
-                         BusDesktopParseError    error_code,
-                         DBusResultCode         *result);
+static void report_error (BusDesktopFileParser *parser,
+                         char                 *message,
+                         const char           *error_name,
+                         DBusError            *error);
 
 static void
 parser_free (BusDesktopFileParser *parser)
@@ -131,7 +131,7 @@ bus_desktop_file_free (BusDesktopFile *desktop_file)
   dbus_free (desktop_file);
 }
 
-static void
+static dbus_bool_t
 grow_lines_in_section (BusDesktopFileSection *section)
 {
   BusDesktopFileLine *lines;
@@ -143,14 +143,19 @@ grow_lines_in_section (BusDesktopFileSection *section)
   else
     new_n_lines = section->n_allocated_lines*2;
 
-  _DBUS_HANDLE_OOM (lines = dbus_realloc (section->lines,
-                                         sizeof (BusDesktopFileLine) * new_n_lines));
-  section->lines = lines;
+  lines = dbus_realloc (section->lines,
+                        sizeof (BusDesktopFileLine) * new_n_lines);
+
+  if (lines == NULL)
+    return FALSE;
   
+  section->lines = lines;
   section->n_allocated_lines = new_n_lines;
+
+  return TRUE;
 }
 
-static void
+static dbus_bool_t
 grow_sections (BusDesktopFile *desktop_file)
 {
   int new_n_sections;
@@ -161,21 +166,38 @@ grow_sections (BusDesktopFile *desktop_file)
   else
     new_n_sections = desktop_file->n_allocated_sections*2;
 
-  _DBUS_HANDLE_OOM (sections = dbus_realloc (desktop_file->sections,
-                                            sizeof (BusDesktopFileSection) * new_n_sections));
+  sections = dbus_realloc (desktop_file->sections,
+                           sizeof (BusDesktopFileSection) * new_n_sections);
+  if (sections == NULL)
+    return FALSE;
+  
   desktop_file->sections = sections;
   
   desktop_file->n_allocated_sections = new_n_sections;
+
+  return TRUE;
 }
 
 static char *
-unescape_string (const DBusString *str, int pos, int end_pos)
+unescape_string (BusDesktopFileParser *parser,
+                 const DBusString     *str,
+                 int                   pos,
+                 int                   end_pos,
+                 DBusError            *error)
 {
   char *retval, *q;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   
   /* len + 1 is enough, because unescaping never makes the
-   * string longer */
-  _DBUS_HANDLE_OOM (retval = dbus_malloc (end_pos - pos + 1));
+   * string longer
+   */
+  retval = dbus_malloc (end_pos - pos + 1);
+  if (retval == NULL)
+    {
+      BUS_SET_OOM (error);
+      return NULL;
+    }
 
   q = retval;
   
@@ -185,6 +207,8 @@ unescape_string (const DBusString *str, int pos, int end_pos)
        {
          /* Found an embedded null */
          dbus_free (retval);
+          report_error (parser, "Text to be unescaped contains embedded nul",
+                        BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
          return NULL;
        }
 
@@ -196,6 +220,8 @@ unescape_string (const DBusString *str, int pos, int end_pos)
            {
              /* Escape at end of string */
              dbus_free (retval);
+              report_error (parser, "Text to be unescaped ended in \\",
+                            BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
              return NULL;
            }
 
@@ -219,7 +245,9 @@ unescape_string (const DBusString *str, int pos, int end_pos)
            default:
             /* Invalid escape code */
             dbus_free (retval);
-            return NULL;
+             report_error (parser, "Text to be unescaped had invalid escape sequence",
+                           BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
+             return NULL;
            }
          pos++;
        }
@@ -241,20 +269,34 @@ new_section (BusDesktopFile *desktop_file,
              const char     *name)
 {
   int n;
+  char *name_copy;
   
   if (desktop_file->n_allocated_sections == desktop_file->n_sections)
-    grow_sections (desktop_file);
+    {
+      if (!grow_sections (desktop_file))
+        return NULL;
+    }
 
-  n = desktop_file->n_sections++;
+  name_copy = _dbus_strdup (name);
+  if (name_copy == NULL)
+    return NULL;
 
-  _DBUS_HANDLE_OOM (desktop_file->sections[n].section_name = _dbus_strdup (name));
+  n = desktop_file->n_sections;
+  desktop_file->sections[n].section_name = name_copy;
 
   desktop_file->sections[n].n_lines = 0;
   desktop_file->sections[n].lines = NULL;
   desktop_file->sections[n].n_allocated_lines = 0;
 
-  grow_lines_in_section (&desktop_file->sections[n]);
+  if (!grow_lines_in_section (&desktop_file->sections[n]))
+    {
+      dbus_free (desktop_file->sections[n].section_name);
+      desktop_file->sections[n].section_name = NULL;
+      return NULL;
+    }
 
+  desktop_file->n_sections += 1;
+  
   return &desktop_file->sections[n];  
 }
 
@@ -283,11 +325,14 @@ new_line (BusDesktopFileParser *parser)
   section = &parser->desktop_file->sections[parser->current_section];
 
   if (section->n_allocated_lines == section->n_lines)
-    grow_lines_in_section (section);
+    {
+      if (!grow_lines_in_section (section))
+        return NULL;
+    }
 
   line = &section->lines[section->n_lines++];
 
-  memset (line, 0, sizeof (BusDesktopFileLine));
+  _DBUS_ZERO(*line);
     
   return line;
 }
@@ -317,15 +362,15 @@ is_blank_line (BusDesktopFileParser *parser)
 static void
 parse_comment_or_blank (BusDesktopFileParser *parser)
 {
-  int line_end;
+  int line_end, eol_len;
   
-  if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
+  if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
     line_end = parser->len;
 
   if (line_end == parser->len)
     parser->pos = parser->len;
   else
-    parser->pos = line_end + 1;
+    parser->pos = line_end + eol_len;
   
   parser->line_num += 1;
 }
@@ -348,34 +393,37 @@ is_valid_section_name (const char *name)
 }
 
 static dbus_bool_t
-parse_section_start (BusDesktopFileParser *parser, DBusResultCode *result)
+parse_section_start (BusDesktopFileParser *parser, DBusError *error)
 {
-  int line_end;
+  int line_end, eol_len;
   char *section_name;
-  
-  if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+    
+  if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
     line_end = parser->len;
   
   if (line_end - parser->pos <= 2 ||
       _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
     {
-      report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, result);
+      report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
       parser_free (parser);
       return FALSE;
     }
 
-  section_name = unescape_string (&parser->data, parser->pos + 1, line_end - 1);
+  section_name = unescape_string (parser,
+                                  &parser->data, parser->pos + 1, line_end - 1,
+                                  error);
 
   if (section_name == NULL)
     {
-      report_error (parser, "Invalid escaping in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, result);
       parser_free (parser);
       return FALSE;
     }
 
   if (!is_valid_section_name (section_name))
     {
-      report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, result);
+      report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
       parser_free (parser);
       dbus_free (section_name);
       return FALSE;
@@ -384,13 +432,15 @@ parse_section_start (BusDesktopFileParser *parser, DBusResultCode *result)
   if (open_section (parser, section_name) == NULL)
     {
       dbus_free (section_name);
+      parser_free (parser);
+      BUS_SET_OOM (error);
       return FALSE;
     }
 
   if (line_end == parser->len)
     parser->pos = parser->len;
   else
-    parser->pos = line_end + 1;
+    parser->pos = line_end + eol_len;
   
   parser->line_num += 1;
 
@@ -400,19 +450,21 @@ parse_section_start (BusDesktopFileParser *parser, DBusResultCode *result)
 }
 
 static dbus_bool_t
-parse_key_value (BusDesktopFileParser *parser, DBusResultCode *result)
+parse_key_value (BusDesktopFileParser *parser, DBusError *error)
 {
-  int line_end;
+  int line_end, eol_len;
   int key_start, key_end;
   int value_start;
   int p;
   char *value, *tmp;
   DBusString key;
   BusDesktopFileLine *line;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   
-  if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
+  if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
     line_end = parser->len;
-
+  
   p = parser->pos;
   key_start = p;
   while (p < line_end &&
@@ -422,12 +474,23 @@ parse_key_value (BusDesktopFileParser *parser, DBusResultCode *result)
   
   if (key_start == key_end)
     {
-      report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, result);
+      report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
       parser_free (parser);
       return FALSE;
     }
 
   /* We ignore locales for now */
+  if (p < line_end && _dbus_string_get_byte (&parser->data, p) == '[')
+    {
+      if (line_end == parser->len)
+       parser->pos = parser->len;
+      else
+       parser->pos = line_end + eol_len;
+         
+      parser->line_num += 1;
+
+      return TRUE;
+    }
   
   /* Skip space before '=' */
   while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
@@ -435,14 +498,14 @@ parse_key_value (BusDesktopFileParser *parser, DBusResultCode *result)
 
   if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
     {
-      report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, result);
+      report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
       parser_free (parser);
       return FALSE;
     }
 
   if (p == line_end)
     {
-      report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, result);
+      report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
       parser_free (parser);
       return FALSE;
     }
@@ -456,20 +519,49 @@ parse_key_value (BusDesktopFileParser *parser, DBusResultCode *result)
 
   value_start = p;
   
-  value = unescape_string (&parser->data, value_start, line_end);
+  value = unescape_string (parser, &parser->data, value_start, line_end, error);
   if (value == NULL)
     {
-      report_error (parser, "Invalid escaping in value", BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, result);
       parser_free (parser);
       return FALSE;
     }
 
   line = new_line (parser);
-
-  _DBUS_HANDLE_OOM (_dbus_string_init (&key, key_end - key_start));
-  _DBUS_HANDLE_OOM (_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
-                                          &key, 0));
-  _DBUS_HANDLE_OOM (_dbus_string_steal_data (&key, &tmp));
+  if (line == NULL)
+    {
+      dbus_free (value);
+      parser_free (parser);
+      BUS_SET_OOM (error);
+      return FALSE;
+    }
+  
+  if (!_dbus_string_init (&key))
+    {
+      dbus_free (value);
+      parser_free (parser);
+      BUS_SET_OOM (error);
+      return FALSE;
+    }
+  
+  if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
+                              &key, 0))
+    {
+      _dbus_string_free (&key);
+      dbus_free (value);
+      parser_free (parser);
+      BUS_SET_OOM (error);
+      return FALSE;
+    }
+  
+  if (!_dbus_string_steal_data (&key, &tmp))
+    {
+      _dbus_string_free (&key);
+      dbus_free (value);
+      parser_free (parser);
+      BUS_SET_OOM (error);
+      return FALSE;
+    }
+  
   _dbus_string_free (&key);
   
   line->key = tmp;
@@ -478,7 +570,7 @@ parse_key_value (BusDesktopFileParser *parser, DBusResultCode *result)
   if (line_end == parser->len)
     parser->pos = parser->len;
   else
-    parser->pos = line_end + 1;
+    parser->pos = line_end + eol_len;
   
   parser->line_num += 1;
 
@@ -486,22 +578,24 @@ parse_key_value (BusDesktopFileParser *parser, DBusResultCode *result)
 }
 
 static void
-report_error (BusDesktopFileParser   *parser,
-             char                   *message,
-             BusDesktopParseError    error_code,
-             DBusResultCode         *result)
+report_error (BusDesktopFileParser *parser,
+             char                 *message,
+             const char           *error_name,
+             DBusError            *error)
 {
   const char *section_name = NULL;
-    
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+  
   if (parser->current_section != -1)
     section_name = parser->desktop_file->sections[parser->current_section].section_name;
 
   if (section_name)
-    _dbus_verbose ("Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
+    dbus_set_error (error, error_name,
+                    "Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
   else
-    _dbus_verbose ("Error at line %d: %s\n", parser->line_num, message);
-
-  dbus_set_result (result, DBUS_RESULT_FAILED);
+    dbus_set_error (error, error_name,
+                    "Error at line %d: %s\n", parser->line_num, message);
 }
 
 #if 0
@@ -525,66 +619,92 @@ dump_desktop_file (BusDesktopFile *file)
 }
 #endif
 
-BusDesktopFile *
-bus_desktop_file_load (DBusString     *filename,
-                      DBusResultCode *result)
+BusDesktopFile*
+bus_desktop_file_load (DBusString *filename,
+                      DBusError  *error)
 {
   DBusString str;
-  DBusResultCode result_code;
   BusDesktopFileParser parser;
-  
-  /* FIXME: Check file size so we don't try to load a ridicously large file. */
+  DBusStat sb;
 
-  _DBUS_HANDLE_OOM (_dbus_string_init (&str, _DBUS_INT_MAX));
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   
-  _DBUS_HANDLE_OOM ((result_code = _dbus_file_get_contents (&str, filename)) !=
-                   DBUS_RESULT_NO_MEMORY);
+  /* Clearly there's a race here, but it's just to make it unlikely
+   * that we do something silly, we still handle doing it below.
+   */
+  if (!_dbus_stat (filename, &sb, error))
+    return NULL;
 
-  if (result_code != DBUS_RESULT_SUCCESS)
+  if (sb.size > _DBUS_ONE_KILOBYTE * 128)
+    {
+      dbus_set_error (error, DBUS_ERROR_FAILED,
+                      "Desktop file size (%ld bytes) is too large", (long) sb.size);
+      return NULL;
+    }
+  
+  if (!_dbus_string_init (&str))
+    {
+      BUS_SET_OOM (error);
+      return NULL;
+    }
+  
+  if (!_dbus_file_get_contents (&str, filename, error))
     {
       _dbus_string_free (&str);
-      dbus_set_result (result, result_code);
       return NULL;
     }
 
   if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
     {
       _dbus_string_free (&str);
-      
-      /* FIXME: Use a better error code */
-      dbus_set_result (result, DBUS_RESULT_FAILED);
+      dbus_set_error (error, DBUS_ERROR_FAILED,
+                      "invalid UTF-8");   
+      return NULL;
+    }
+  
+  parser.desktop_file = dbus_new0 (BusDesktopFile, 1);
+  if (parser.desktop_file == NULL)
+    {
+      _dbus_string_free (&str);
+      BUS_SET_OOM (error);
       return NULL;
     }
   
-  _DBUS_HANDLE_OOM (parser.desktop_file = dbus_malloc0 (sizeof (BusDesktopFile)));
-
   parser.data = str;
   parser.line_num = 1;
   parser.pos = 0;
   parser.len = _dbus_string_get_length (&parser.data);
   parser.current_section = -1;
-  
+
   while (parser.pos < parser.len)
     {
       if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
        {
-         if (!parse_section_start (&parser, result))
-           return NULL;
+         if (!parse_section_start (&parser, error))
+            {
+              return NULL;
+            }
        }
       else if (is_blank_line (&parser) ||
               _dbus_string_get_byte (&parser.data, parser.pos) == '#')
        parse_comment_or_blank (&parser);
+      else if (parser.current_section < 0)
+       {
+           dbus_set_error(error, DBUS_ERROR_FAILED,
+                          "invalid service file: key=value before [Section]");
+           return NULL;
+       }
       else
        {
-         if (!parse_key_value (&parser, result))
-           return NULL;
+         if (!parse_key_value (&parser, error))
+            {
+              return NULL;
+            }
        }
     }
 
   _dbus_string_free (&parser.data);
 
-  dbus_set_result (result, DBUS_RESULT_SUCCESS);
-  
   return parser.desktop_file;
 }
 
@@ -660,16 +780,29 @@ dbus_bool_t
 bus_desktop_file_get_string (BusDesktopFile  *desktop_file,
                             const char      *section,
                             const char      *keyname,
-                            char           **val)
+                            char           **val,
+                            DBusError       *error)
 {
   const char *raw;
-  
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+
   *val = NULL;
   
   if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
-    return FALSE;
+    {
+      dbus_set_error (error, DBUS_ERROR_FAILED,
+                      "No \"%s\" key in .service file\n", keyname);
+      return FALSE;
+    }
+
+  *val = _dbus_strdup (raw);
 
-  _DBUS_HANDLE_OOM (*val = _dbus_strdup (raw));
+  if (*val == NULL)
+    {
+      BUS_SET_OOM (error);
+      return FALSE;
+    }
   
   return TRUE;
 }