Silence a bunch of -Wunused-but-set-variable warnings
[platform/upstream/glib.git] / gio / glib-compile-schemas.c
index 95770a7..d2c3f77 100644 (file)
@@ -107,6 +107,12 @@ enum_state_add_value (EnumState    *state,
       return;
     }
 
+  /* Silently drop the null case if it is mentioned.
+   * It is properly denoted with an empty array.
+   */
+  if (state->is_flags && value == 0)
+    return;
+
   if (state->is_flags && (value & (value - 1)))
     {
       g_set_error (error, G_MARKUP_ERROR,
@@ -121,7 +127,6 @@ enum_state_add_value (EnumState    *state,
    * If we loosen the one-bit-set restriction we need an overlap check.
    */
 
-
   strinfo_builder_append_item (state->strinfo, nick, value);
 }
 
@@ -135,9 +140,10 @@ enum_state_end (EnumState **state_ptr,
   *state_ptr = NULL;
 
   if (state->strinfo->len == 0)
-    g_set_error_literal (error,
-                         G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
-                         "<enum> must contain at least one <value>");
+    g_set_error (error,
+                 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
+                 "<%s> must contain at least one <value>",
+                 state->is_flags ? "flags" : "enum");
 }
 
 /* Handling of <key> {{{1 */
@@ -322,6 +328,23 @@ key_state_set_range (KeyState     *state,
                      const gchar  *max_str,
                      GError      **error)
 {
+  const struct {
+    const gchar  type;
+    const gchar *min;
+    const gchar *max;
+  } table[] = {
+    { 'y',                    "0",                  "255" },
+    { 'n',               "-32768",                "32767" },
+    { 'q',                    "0",                "65535" },
+    { 'i',          "-2147483648",           "2147483647" },
+    { 'u',                    "0",           "4294967295" },
+    { 'x', "-9223372036854775808",  "9223372036854775807" },
+    { 't',                    "0", "18446744073709551615" },
+    { 'd',                 "-inf",                  "inf" },
+  };
+  gboolean type_ok = FALSE;
+  gint i;
+
   if (state->minimum)
     {
       g_set_error_literal (error, G_MARKUP_ERROR,
@@ -330,7 +353,16 @@ key_state_set_range (KeyState     *state,
       return;
     }
 
-  if (strchr ("ynqiuxtd", *(char *) state->type) == NULL)
+  for (i = 0; i < G_N_ELEMENTS (table); i++)
+    if (*(char *) state->type == table[i].type)
+      {
+        min_str = min_str ? min_str : table[i].min;
+        max_str = max_str ? max_str : table[i].max;
+        type_ok = TRUE;
+        break;
+      }
+
+  if (!type_ok)
     {
       gchar *type = g_variant_type_dup_string (state->type);
       g_set_error (error, G_MARKUP_ERROR,
@@ -496,8 +528,7 @@ key_state_start_aliases (KeyState  *state,
     g_set_error_literal (error, G_MARKUP_ERROR,
                          G_MARKUP_ERROR_INVALID_CONTENT,
                          "<aliases> already specified for this key");
-
-  if (!state->is_flags && !state->is_enum && !state->has_choices)
+  else if (!state->is_flags && !state->is_enum && !state->has_choices)
     g_set_error_literal (error, G_MARKUP_ERROR,
                          G_MARKUP_ERROR_INVALID_CONTENT,
                          "<aliases> can only be specified for keys with "
@@ -623,12 +654,18 @@ key_state_serialise (KeyState *state)
           if (state->strinfo->len)
             {
               GVariant *array;
+              guint32 *words;
               gpointer data;
               gsize size;
+              gint i;
 
               data = state->strinfo->str;
               size = state->strinfo->len;
 
+              words = data;
+              for (i = 0; i < size / sizeof (guint32); i++)
+                words[i] = GUINT32_TO_LE (words[i]);
+
               array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
                                                data, size, TRUE,
                                                g_free, data);
@@ -1217,7 +1254,9 @@ start_element (GMarkupParseContext  *context,
                        OPTIONAL | STRING, "gettext-domain", &gettext_domain,
                        OPTIONAL | STRING, "extends", &extends,
                        OPTIONAL | STRING, "list-of", &list_of))
-            parse_state_start_schema (state, id, path, gettext_domain,
+            parse_state_start_schema (state, id, path,
+                                      gettext_domain ? gettext_domain
+                                                     : state->schemalist_domain,
                                       extends, list_of, error);
           return;
         }
@@ -1307,7 +1346,8 @@ start_element (GMarkupParseContext  *context,
       else if (strcmp (element_name, "range") == 0)
         {
           const gchar *min, *max;
-          if (COLLECT (STRING, "min", &min, STRING, "max", &max))
+          if (COLLECT (STRING | OPTIONAL, "min", &min,
+                       STRING | OPTIONAL, "max", &max))
             key_state_set_range (state->key_state, min, max, error);
           return;
         }
@@ -1402,9 +1442,6 @@ static void
 schema_state_end (SchemaState **state_ptr,
                   GError      **error)
 {
-  SchemaState *state;
-
-  state = *state_ptr;
   *state_ptr = NULL;
 }
 
@@ -1492,6 +1529,13 @@ gvdb_pair_init (GvdbPair *pair)
 
 typedef struct
 {
+  GHashTable *schema_table;
+  GvdbPair root_pair;
+} WriteToFileData;
+
+typedef struct
+{
+  GHashTable *schema_table;
   GvdbPair pair;
   gboolean l10n;
 } OutputSchemaData;
@@ -1516,6 +1560,11 @@ output_key (gpointer key,
 
   if (state->l10n)
     data->l10n = TRUE;
+
+  if (state->child_schema &&
+      !g_hash_table_lookup (data->schema_table, state->child_schema))
+    g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
+                state->child_schema);
 }
 
 static void
@@ -1523,6 +1572,7 @@ output_schema (gpointer key,
                gpointer value,
                gpointer user_data)
 {
+  WriteToFileData *wtf_data = user_data;
   OutputSchemaData data;
   GvdbPair *root_pair;
   SchemaState *state;
@@ -1531,8 +1581,9 @@ output_schema (gpointer key,
 
   id = key;
   state = value;
-  root_pair = user_data;
+  root_pair = &wtf_data->root_pair;
 
+  data.schema_table = wtf_data->schema_table;
   gvdb_pair_init (&data.pair);
   data.l10n = FALSE;
 
@@ -1564,17 +1615,19 @@ write_to_file (GHashTable   *schema_table,
                const gchar  *filename,
                GError      **error)
 {
+  WriteToFileData data;
   gboolean success;
-  GvdbPair pair;
 
-  gvdb_pair_init (&pair);
+  data.schema_table = schema_table;
+
+  gvdb_pair_init (&data.root_pair);
 
-  g_hash_table_foreach (schema_table, output_schema, &pair);
+  g_hash_table_foreach (schema_table, output_schema, &data);
 
-  success = gvdb_table_write_contents (pair.table, filename,
+  success = gvdb_table_write_contents (data.root_pair.table, filename,
                                        G_BYTE_ORDER != G_LITTLE_ENDIAN,
                                        error);
-  g_hash_table_unref (pair.table);
+  g_hash_table_unref (data.root_pair.table);
 
   return success;
 }
@@ -1633,6 +1686,7 @@ parse_gschema_files (gchar    **files,
 
           /* let them know */
           fprintf (stderr, "%s: %s.  ", filename, error->message);
+          g_clear_error (&error);
 
           if (strict)
             {
@@ -1684,9 +1738,10 @@ compare_strings (gconstpointer a,
 static gboolean
 set_overrides (GHashTable  *schema_table,
                gchar      **files,
-               GError     **error)
+               gboolean     strict)
 {
   const gchar *filename;
+  GError *error = NULL;
 
   while ((filename = *files++))
     {
@@ -1695,10 +1750,19 @@ set_overrides (GHashTable  *schema_table,
       gint i;
 
       key_file = g_key_file_new ();
-      if (!g_key_file_load_from_file (key_file, filename, 0, error))
+      if (!g_key_file_load_from_file (key_file, filename, 0, &error))
         {
+          fprintf (stderr, "%s: %s.  ", filename, error->message);
           g_key_file_free (key_file);
+          g_clear_error (&error);
+
+          if (!strict)
+            {
+              fprintf (stderr, _("Ignoring this file.\n"));
+              continue;
+            }
 
+          fprintf (stderr, _("--strict was specified; exiting.\n"));
           return FALSE;
         }
 
@@ -1714,16 +1778,11 @@ set_overrides (GHashTable  *schema_table,
           schema = g_hash_table_lookup (schema_table, group);
 
           if (schema == NULL)
-            {
-              g_set_error (error, G_KEY_FILE_ERROR,
-                           G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
-                           _("No such schema `%s' specified in "
-                             "override file `%s'"), group, filename);
-              g_key_file_free (key_file);
-              g_strfreev (groups);
-
-              return FALSE;
-            }
+            /* Having the schema not be installed is expected to be a
+             * common case.  Don't even emit an error message about
+             * that.
+             */
+            continue;
 
           keys = g_key_file_get_keys (key_file, group, NULL, NULL);
           g_assert (keys != NULL);
@@ -1739,11 +1798,17 @@ set_overrides (GHashTable  *schema_table,
 
               if (state == NULL)
                 {
-                  g_set_error (error, G_KEY_FILE_ERROR,
-                               G_KEY_FILE_ERROR_KEY_NOT_FOUND,
-                               _("No such key `%s' in schema `%s' as "
-                                 "specified in override file `%s'"),
-                               key, group, filename);
+                  fprintf (stderr, _("No such key `%s' in schema `%s' as "
+                                     "specified in override file `%s'"),
+                           key, group, filename);
+
+                  if (!strict)
+                    {
+                      fprintf (stderr, _("; ignoring override for this key.\n"));
+                      continue;
+                    }
+
+                  fprintf (stderr, _(" and --strict was specified; exiting.\n"));
                   g_key_file_free (key_file);
                   g_strfreev (groups);
                   g_strfreev (keys);
@@ -1755,14 +1820,28 @@ set_overrides (GHashTable  *schema_table,
               g_assert (string != NULL);
 
               value = g_variant_parse (state->type, string,
-                                       NULL, NULL, error);
+                                       NULL, NULL, &error);
 
               if (value == NULL)
                 {
+                  fprintf (stderr, _("error parsing key `%s' in schema `%s' "
+                                     "as specified in override file `%s': "
+                                     "%s.  "),
+                           key, group, filename, error->message);
+
+                  g_clear_error (&error);
+                  g_free (string);
+
+                  if (!strict)
+                    {
+                      fprintf (stderr, _("Ignoring override for this key.\n"));
+                      continue;
+                    }
+
+                  fprintf (stderr, _("--strict was specified; exiting.\n"));
                   g_key_file_free (key_file);
                   g_strfreev (groups);
                   g_strfreev (keys);
-                  g_free (string);
 
                   return FALSE;
                 }
@@ -1772,18 +1851,25 @@ set_overrides (GHashTable  *schema_table,
                   if (g_variant_compare (value, state->minimum) < 0 ||
                       g_variant_compare (value, state->maximum) > 0)
                     {
-                      g_set_error (error, G_MARKUP_ERROR,
-                                   G_MARKUP_ERROR_INVALID_CONTENT,
-                                   _("override for key `%s' in schema `%s' in "
-                                     "override file `%s' is out of the range "
-                                     "given in the schema"),
-                                   key, group, filename);
+                      fprintf (stderr,
+                               _("override for key `%s' in schema `%s' in "
+                                 "override file `%s' is out of the range "
+                                 "given in the schema"),
+                               key, group, filename);
 
-                      g_key_file_free (key_file);
                       g_variant_unref (value);
+                      g_free (string);
+
+                      if (!strict)
+                        {
+                          fprintf (stderr, _("; ignoring override for this key.\n"));
+                          continue;
+                        }
+
+                      fprintf (stderr, _(" and --strict was specified; exiting.\n"));
+                      g_key_file_free (key_file);
                       g_strfreev (groups);
                       g_strfreev (keys);
-                      g_free (string);
 
                       return FALSE;
                     }
@@ -1793,18 +1879,25 @@ set_overrides (GHashTable  *schema_table,
                 {
                   if (!is_valid_choices (value, state->strinfo))
                     {
-                      g_set_error (error, G_MARKUP_ERROR,
-                                   G_MARKUP_ERROR_INVALID_CONTENT,
-                                   _("override for key `%s' in schema `%s' in "
-                                     "override file `%s' is not in the list "
-                                     "of valid choices"),
-                                   key, group, filename);
+                      fprintf (stderr,
+                               _("override for key `%s' in schema `%s' in "
+                                 "override file `%s' is not in the list "
+                                 "of valid choices"),
+                               key, group, filename);
 
-                      g_key_file_free (key_file);
                       g_variant_unref (value);
+                      g_free (string);
+
+                      if (!strict)
+                        {
+                          fprintf (stderr, _("; ignoring override for this key.\n"));
+                          continue;
+                        }
+
+                      fprintf (stderr, _(" and --strict was specified; exiting.\n"));
+                      g_key_file_free (key_file);
                       g_strfreev (groups);
                       g_strfreev (keys);
-                      g_free (string);
 
                       return FALSE;
                     }
@@ -1812,9 +1905,9 @@ set_overrides (GHashTable  *schema_table,
 
               g_variant_unref (state->default_value);
               state->default_value = value;
+              g_free (string);
             }
 
-
           g_strfreev (keys);
         }
 
@@ -1853,6 +1946,19 @@ main (int argc, char **argv)
   };
 
   setlocale (LC_ALL, "");
+  textdomain (GETTEXT_PACKAGE);
+#ifdef G_OS_WIN32
+  extern gchar *_glib_get_locale_dir (void);
+  gchar *tmp = _glib_get_locale_dir ();
+  bindtextdomain (GETTEXT_PACKAGE, tmp);
+  g_free (tmp);
+#else
+  bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
+#endif
+
+#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+#endif
 
   context = g_option_context_new (N_("DIRECTORY"));
   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
@@ -1938,8 +2044,14 @@ main (int argc, char **argv)
       return 1;
     }
 
-  if ((override_files != NULL && !set_overrides (table, override_files, &error)) ||
-      (!dry_run && !write_to_file (table, target, &error)))
+  if (override_files != NULL &&
+      !set_overrides (table, override_files, strict))
+    {
+      g_free (target);
+      return 1;
+    }
+
+  if (!dry_run && !write_to_file (table, target, &error))
     {
       fprintf (stderr, "%s\n", error->message);
       g_free (target);