gthreadedresolver: Unref unexpected address
[platform/upstream/glib.git] / gio / gsettings-tool.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the licence, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  *
17  * Author: Ryan Lortie <desrt@desrt.ca>
18  */
19
20 #include "config.h"
21
22 #include <gio/gio.h>
23 #include <gi18n.h>
24 #include <locale.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #ifdef G_OS_WIN32
29 #include "glib/glib-private.h"
30 #endif
31
32 static GSettingsSchemaSource   *global_schema_source;
33 static GSettings               *global_settings;
34 static GSettingsSchema         *global_schema;
35 static GSettingsSchemaKey      *global_schema_key;
36 const gchar                    *global_key;
37 const gchar                    *global_value;
38
39 static gboolean
40 is_relocatable_schema (GSettingsSchema *schema)
41 {
42   return g_settings_schema_get_path (schema) == NULL;
43 }
44
45 static gboolean
46 check_relocatable_schema (GSettingsSchema *schema,
47                           const gchar     *schema_id)
48 {
49   if (schema == NULL)
50     {
51       g_printerr (_("No such schema '%s'\n"), schema_id);
52       return FALSE;
53     }
54
55   if (!is_relocatable_schema (schema))
56     {
57       g_printerr (_("Schema '%s' is not relocatable "
58                     "(path must not be specified)\n"),
59                   schema_id);
60       return FALSE;
61     }
62
63   return TRUE;
64 }
65
66 static gboolean
67 check_schema (GSettingsSchema *schema,
68               const gchar *schema_id)
69 {
70   if (schema == NULL)
71     {
72       g_printerr (_("No such schema '%s'\n"), schema_id);
73       return FALSE;
74     }
75
76   if (is_relocatable_schema (schema))
77     {
78       g_printerr (_("Schema '%s' is relocatable "
79                     "(path must be specified)\n"),
80                   schema_id);
81       return FALSE;
82     }
83
84   return TRUE;
85 }
86
87 static gboolean
88 check_path (const gchar *path)
89 {
90   if (path[0] == '\0')
91     {
92       g_printerr (_("Empty path given.\n"));
93       return FALSE;
94     }
95
96   if (path[0] != '/')
97     {
98       g_printerr (_("Path must begin with a slash (/)\n"));
99       return FALSE;
100     }
101
102   if (!g_str_has_suffix (path, "/"))
103     {
104       g_printerr (_("Path must end with a slash (/)\n"));
105       return FALSE;
106     }
107
108   if (strstr (path, "//"))
109     {
110       g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
111       return FALSE;
112     }
113
114   return TRUE;
115 }
116
117 static void
118 output_list (gchar **list)
119 {
120   gint i;
121
122   for (i = 0; list[i]; i++)
123     g_print ("%s\n", list[i]);
124 }
125
126 static void
127 gsettings_print_version (void)
128 {
129   g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
130            glib_micro_version);
131 }
132
133 static void
134 gsettings_list_schemas (void)
135 {
136   gchar **schemas;
137
138   g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
139   output_list (schemas);
140   g_strfreev (schemas);
141 }
142
143 static void
144 gsettings_list_relocatable_schemas (void)
145 {
146   gchar **schemas;
147
148   g_settings_schema_source_list_schemas (global_schema_source, TRUE, NULL, &schemas);
149   output_list (schemas);
150   g_strfreev (schemas);
151 }
152
153 static void
154 gsettings_list_keys (void)
155 {
156   gchar **keys;
157
158   keys = g_settings_list_keys (global_settings);
159   output_list (keys);
160   g_strfreev (keys);
161 }
162
163 static void
164 gsettings_list_children (void)
165 {
166   gchar **children;
167   gint max = 0;
168   gint i;
169
170   children = g_settings_list_children (global_settings);
171   for (i = 0; children[i]; i++)
172     if (strlen (children[i]) > max)
173       max = strlen (children[i]);
174
175   for (i = 0; children[i]; i++)
176     {
177       GSettings *child;
178       GSettingsSchema *schema;
179       gchar *path;
180
181       child = g_settings_get_child (global_settings, children[i]);
182       g_object_get (child,
183                     "settings-schema", &schema,
184                     "path", &path,
185                     NULL);
186
187       if (g_settings_schema_get_path (schema) != NULL)
188         g_print ("%-*s   %s\n", max, children[i], g_settings_schema_get_id (schema));
189       else
190         g_print ("%-*s   %s:%s\n", max, children[i], g_settings_schema_get_id (schema), path);
191
192       g_object_unref (child);
193       g_settings_schema_unref (schema);
194       g_free (path);
195     }
196
197   g_strfreev (children);
198 }
199
200 static void
201 enumerate (GSettings *settings)
202 {
203   gchar **keys;
204   gchar *schema;
205   gint i;
206
207   g_object_get (settings, "schema-id", &schema, NULL);
208
209   keys = g_settings_list_keys (settings);
210   for (i = 0; keys[i]; i++)
211     {
212       GVariant *value;
213       gchar *printed;
214
215       value = g_settings_get_value (settings, keys[i]);
216       printed = g_variant_print (value, TRUE);
217       g_print ("%s %s %s\n", schema, keys[i], printed);
218       g_variant_unref (value);
219       g_free (printed);
220     }
221
222   g_free (schema);
223   g_strfreev (keys);
224 }
225
226 static void
227 list_recursively (GSettings *settings)
228 {
229   gchar **children;
230   gint i;
231
232   enumerate (settings);
233   children = g_settings_list_children (settings);
234   for (i = 0; children[i]; i++)
235     {
236       GSettings *child;
237
238       child = g_settings_get_child (settings, children[i]);
239       list_recursively (child);
240       g_object_unref (child);
241     }
242
243   g_strfreev (children);
244 }
245
246 static void
247 gsettings_list_recursively (void)
248 {
249   if (global_settings)
250     {
251       list_recursively (global_settings);
252     }
253   else
254     {
255       gchar **schemas;
256       gint i;
257
258       g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
259
260       for (i = 0; schemas[i]; i++)
261         {
262           GSettings *settings;
263
264           settings = g_settings_new (schemas[i]);
265           list_recursively (settings);
266           g_object_unref (settings);
267         }
268
269       g_strfreev (schemas);
270     }
271 }
272
273 static void
274 gsettings_range (void)
275 {
276   GVariant *range, *detail;
277   const gchar *type;
278
279   range = g_settings_schema_key_get_range (global_schema_key);
280   g_variant_get (range, "(&sv)", &type, &detail);
281
282   if (strcmp (type, "type") == 0)
283     g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
284
285   else if (strcmp (type, "range") == 0)
286     {
287       GVariant *min, *max;
288       gchar *smin, *smax;
289
290       g_variant_get (detail, "(**)", &min, &max);
291       smin = g_variant_print (min, FALSE);
292       smax = g_variant_print (max, FALSE);
293
294       g_print ("range %s %s %s\n",
295                g_variant_get_type_string (min), smin, smax);
296       g_variant_unref (min);
297       g_variant_unref (max);
298       g_free (smin);
299       g_free (smax);
300     }
301
302   else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
303     {
304       GVariantIter iter;
305       GVariant *item;
306
307       g_print ("%s\n", type);
308
309       g_variant_iter_init (&iter, detail);
310       while (g_variant_iter_loop (&iter, "*", &item))
311         {
312           gchar *printed;
313
314           printed = g_variant_print (item, FALSE);
315           g_print ("%s\n", printed);
316           g_free (printed);
317         }
318     }
319
320   g_variant_unref (detail);
321   g_variant_unref (range);
322 }
323
324 static void
325 gsettings_get (void)
326 {
327   GVariant *value;
328   gchar *printed;
329
330   value = g_settings_get_value (global_settings, global_key);
331   printed = g_variant_print (value, TRUE);
332   g_print ("%s\n", printed);
333   g_variant_unref (value);
334   g_free (printed);
335 }
336
337 static void
338 gsettings_reset (void)
339 {
340   g_settings_reset (global_settings, global_key);
341   g_settings_sync ();
342 }
343
344 static void
345 reset_all_keys (GSettings *settings)
346 {
347   gchar **keys;
348   gint i;
349
350   keys = g_settings_list_keys (settings);
351   for (i = 0; keys[i]; i++)
352     {
353       g_settings_reset (settings, keys[i]);
354     }
355
356   g_strfreev (keys);
357 }
358
359 static void
360 gsettings_reset_recursively (void)
361 {
362   gchar **children;
363   gint i;
364
365   g_settings_delay (global_settings);
366
367   reset_all_keys (global_settings);
368   children = g_settings_list_children (global_settings);
369   for (i = 0; children[i]; i++)
370     {
371       GSettings *child;
372       child = g_settings_get_child (global_settings, children[i]);
373
374       reset_all_keys (child);
375
376       g_object_unref (child);
377     }
378
379   g_strfreev (children);
380
381   g_settings_apply (global_settings);
382   g_settings_sync ();
383 }
384
385 static void
386 gsettings_writable (void)
387 {
388   g_print ("%s\n",
389            g_settings_is_writable (global_settings, global_key) ?
390            "true" : "false");
391 }
392
393 static void
394 value_changed (GSettings   *settings,
395                const gchar *key,
396                gpointer     user_data)
397 {
398   GVariant *value;
399   gchar *printed;
400
401   value = g_settings_get_value (settings, key);
402   printed = g_variant_print (value, TRUE);
403   g_print ("%s: %s\n", key, printed);
404   g_variant_unref (value);
405   g_free (printed);
406 }
407
408 static void
409 gsettings_monitor (void)
410 {
411   if (global_key)
412     {
413       gchar *name;
414
415       name = g_strdup_printf ("changed::%s", global_key);
416       g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
417     }
418   else
419     g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL);
420
421   for (;;)
422     g_main_context_iteration (NULL, TRUE);
423 }
424
425 static void
426 gsettings_set (void)
427 {
428   const GVariantType *type;
429   GError *error = NULL;
430   GVariant *new;
431   gchar *freeme = NULL;
432
433   type = g_settings_schema_key_get_value_type (global_schema_key);
434
435   new = g_variant_parse (type, global_value, NULL, NULL, &error);
436
437   /* If that didn't work and the type is string then we should assume
438    * that the user is just trying to set a string directly and forgot
439    * the quotes (or had them consumed by the shell).
440    *
441    * If the user started with a quote then we assume that some deeper
442    * problem is at play and we want the failure in that case.
443    *
444    * Consider:
445    *
446    *   gsettings set x.y.z key "'i don't expect this to work'"
447    *
448    * Note that we should not just add quotes and try parsing again, but
449    * rather assume that the user is providing us with a bare string.
450    * Assume we added single quotes, then consider this case:
451    *
452    *   gsettings set x.y.z key "i'd expect this to work"
453    *
454    * A similar example could be given for double quotes.
455    *
456    * Avoid that whole mess by just using g_variant_new_string().
457    */
458   if (new == NULL &&
459       g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
460       global_value[0] != '\'' && global_value[0] != '"')
461     {
462       g_clear_error (&error);
463       new = g_variant_new_string (global_value);
464     }
465
466   if (new == NULL)
467     {
468       gchar *context;
469
470       context = g_variant_parse_error_print_context (error, global_value);
471       g_printerr ("%s", context);
472       exit (1);
473     }
474
475   if (!g_settings_schema_key_range_check (global_schema_key, new))
476     {
477       g_printerr (_("The provided value is outside of the valid range\n"));
478       g_variant_unref (new);
479       exit (1);
480     }
481
482   if (!g_settings_set_value (global_settings, global_key, new))
483     {
484       g_printerr (_("The key is not writable\n"));
485       exit (1);
486     }
487
488   g_settings_sync ();
489
490   g_free (freeme);
491 }
492
493 static int
494 gsettings_help (gboolean     requested,
495                 const gchar *command)
496 {
497   const gchar *description;
498   const gchar *synopsis;
499   GString *string;
500
501   string = g_string_new (NULL);
502
503   if (command == NULL)
504     ;
505
506   else if (strcmp (command, "help") == 0)
507     {
508       description = _("Print help");
509       synopsis = "[COMMAND]";
510     }
511
512   else if (strcmp (command, "--version") == 0)
513     {
514       description = _("Print version information and exit");
515       synopsis = "";
516     }
517
518   else if (strcmp (command, "list-schemas") == 0)
519     {
520       description = _("List the installed (non-relocatable) schemas");
521       synopsis = "";
522     }
523
524   else if (strcmp (command, "list-relocatable-schemas") == 0)
525     {
526       description = _("List the installed relocatable schemas");
527       synopsis = "";
528     }
529
530   else if (strcmp (command, "list-keys") == 0)
531     {
532       description = _("List the keys in SCHEMA");
533       synopsis = N_("SCHEMA[:PATH]");
534     }
535
536   else if (strcmp (command, "list-children") == 0)
537     {
538       description = _("List the children of SCHEMA");
539       synopsis = N_("SCHEMA[:PATH]");
540     }
541
542   else if (strcmp (command, "list-recursively") == 0)
543     {
544       description = _("List keys and values, recursively\n"
545                       "If no SCHEMA is given, list all keys\n");
546       synopsis = N_("[SCHEMA[:PATH]]");
547     }
548
549   else if (strcmp (command, "get") == 0)
550     {
551       description = _("Get the value of KEY");
552       synopsis = N_("SCHEMA[:PATH] KEY");
553     }
554
555   else if (strcmp (command, "range") == 0)
556     {
557       description = _("Query the range of valid values for KEY");
558       synopsis = N_("SCHEMA[:PATH] KEY");
559     }
560
561   else if (strcmp (command, "set") == 0)
562     {
563       description = _("Set the value of KEY to VALUE");
564       synopsis = N_("SCHEMA[:PATH] KEY VALUE");
565     }
566
567   else if (strcmp (command, "reset") == 0)
568     {
569       description = _("Reset KEY to its default value");
570       synopsis = N_("SCHEMA[:PATH] KEY");
571     }
572
573   else if (strcmp (command, "reset-recursively") == 0)
574     {
575       description = _("Reset all keys in SCHEMA to their defaults");
576       synopsis = N_("SCHEMA[:PATH]");
577     }
578
579   else if (strcmp (command, "writable") == 0)
580     {
581       description = _("Check if KEY is writable");
582       synopsis = N_("SCHEMA[:PATH] KEY");
583     }
584
585   else if (strcmp (command, "monitor") == 0)
586     {
587       description = _("Monitor KEY for changes.\n"
588                     "If no KEY is specified, monitor all keys in SCHEMA.\n"
589                     "Use ^C to stop monitoring.\n");
590       synopsis = N_("SCHEMA[:PATH] [KEY]");
591     }
592   else
593     {
594       g_string_printf (string, _("Unknown command %s\n\n"), command);
595       requested = FALSE;
596       command = NULL;
597     }
598
599   if (command == NULL)
600     {
601       g_string_append (string,
602       _("Usage:\n"
603         "  gsettings --version\n"
604         "  gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
605         "\n"
606         "Commands:\n"
607         "  help                      Show this information\n"
608         "  list-schemas              List installed schemas\n"
609         "  list-relocatable-schemas  List relocatable schemas\n"
610         "  list-keys                 List keys in a schema\n"
611         "  list-children             List children of a schema\n"
612         "  list-recursively          List keys and values, recursively\n"
613         "  range                     Queries the range of a key\n"
614         "  get                       Get the value of a key\n"
615         "  set                       Set the value of a key\n"
616         "  reset                     Reset the value of a key\n"
617         "  reset-recursively         Reset all values in a given schema\n"
618         "  writable                  Check if a key is writable\n"
619         "  monitor                   Watch for changes\n"
620         "\n"
621         "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
622     }
623   else
624     {
625       g_string_append_printf (string, _("Usage:\n  gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
626                               command, synopsis[0] ? _(synopsis) : "", description);
627
628       g_string_append (string, _("Arguments:\n"));
629
630       g_string_append (string,
631                        _("  SCHEMADIR A directory to search for additional schemas\n"));
632
633       if (strstr (synopsis, "[COMMAND]"))
634         g_string_append (string,
635                        _("  COMMAND   The (optional) command to explain\n"));
636
637       else if (strstr (synopsis, "SCHEMA"))
638         g_string_append (string,
639                        _("  SCHEMA    The name of the schema\n"
640                          "  PATH      The path, for relocatable schemas\n"));
641
642       if (strstr (synopsis, "[KEY]"))
643         g_string_append (string,
644                        _("  KEY       The (optional) key within the schema\n"));
645
646       else if (strstr (synopsis, "KEY"))
647         g_string_append (string,
648                        _("  KEY       The key within the schema\n"));
649
650       if (strstr (synopsis, "VALUE"))
651         g_string_append (string,
652                        _("  VALUE     The value to set\n"));
653
654       g_string_append (string, "\n");
655     }
656
657   if (requested)
658     g_print ("%s", string->str);
659   else
660     g_printerr ("%s\n", string->str);
661
662   g_string_free (string, TRUE);
663
664   return requested ? 0 : 1;
665 }
666
667
668 int
669 main (int argc, char **argv)
670 {
671   void (* function) (void);
672
673 #ifdef G_OS_WIN32
674   gchar *tmp;
675 #endif
676
677   setlocale (LC_ALL, "");
678   textdomain (GETTEXT_PACKAGE);
679
680 #ifdef G_OS_WIN32
681   tmp = _glib_get_locale_dir ();
682   bindtextdomain (GETTEXT_PACKAGE, tmp);
683   g_free (tmp);
684 #else
685   bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
686 #endif
687
688 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
689   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
690 #endif
691
692   if (argc < 2)
693     return gsettings_help (FALSE, NULL);
694
695   global_schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
696
697   if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
698     {
699       GSettingsSchemaSource *parent = global_schema_source;
700       GError *error = NULL;
701
702       global_schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
703       g_settings_schema_source_unref (parent);
704
705       if (global_schema_source == NULL)
706         {
707           g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
708           g_clear_error (&error);
709
710           return 1;
711         }
712
713       /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
714       argv = argv + 2;
715       argc -= 2;
716     }
717
718   if (strcmp (argv[1], "help") == 0)
719     return gsettings_help (TRUE, argv[2]);
720
721   else if (argc == 2 && strcmp (argv[1], "--version") == 0)
722     function = gsettings_print_version;
723
724   else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
725     function = gsettings_list_schemas;
726
727   else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
728     function = gsettings_list_relocatable_schemas;
729
730   else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
731     function = gsettings_list_keys;
732
733   else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
734     function = gsettings_list_children;
735
736   else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
737     function = gsettings_list_recursively;
738
739   else if (argc == 4 && strcmp (argv[1], "range") == 0)
740     function = gsettings_range;
741
742   else if (argc == 4 && strcmp (argv[1], "get") == 0)
743     function = gsettings_get;
744
745   else if (argc == 5 && strcmp (argv[1], "set") == 0)
746     function = gsettings_set;
747
748   else if (argc == 4 && strcmp (argv[1], "reset") == 0)
749     function = gsettings_reset;
750
751   else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
752     function = gsettings_reset_recursively;
753
754   else if (argc == 4 && strcmp (argv[1], "writable") == 0)
755     function = gsettings_writable;
756
757   else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
758     function = gsettings_monitor;
759
760   else
761     return gsettings_help (FALSE, argv[1]);
762
763   if (argc > 2)
764     {
765       gchar **parts;
766
767       if (argv[2][0] == '\0')
768         {
769           g_printerr (_("Empty schema name given\n"));
770           return 1;
771         }
772
773       parts = g_strsplit (argv[2], ":", 2);
774
775       global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
776       if (parts[1])
777         {
778           if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
779             return 1;
780
781           global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
782         }
783       else
784         {
785           if (!check_schema (global_schema, parts[0]))
786             return 1;
787
788           global_settings = g_settings_new_full (global_schema, NULL, NULL);
789         }
790
791       g_strfreev (parts);
792     }
793
794   if (argc > 3)
795     {
796       if (!g_settings_schema_has_key (global_schema, argv[3]))
797         {
798           g_printerr (_("No such key '%s'\n"), argv[3]);
799           return 1;
800         }
801
802       global_key = argv[3];
803       global_schema_key = g_settings_schema_get_key (global_schema, global_key);
804     }
805
806   if (argc > 4)
807     global_value = argv[4];
808
809   (* function) ();
810
811
812   g_clear_pointer (&global_schema_source, g_settings_schema_source_unref);
813   g_clear_pointer (&global_schema_key, g_settings_schema_key_unref);
814   g_clear_pointer (&global_schema, g_settings_schema_unref);
815   g_clear_object (&global_settings);
816
817   return 0;
818 }