1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2010 Red Hat, Inc.
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 License, or (at your option) any later version.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Matthias Clasen
31 pick_word_at (const gchar *s,
33 gint *out_word_begins_at)
40 if (out_word_begins_at != NULL)
41 *out_word_begins_at = -1;
45 if (g_ascii_isspace (s[cursor]) &&
46 ((cursor > 0 && g_ascii_isspace (s[cursor-1])) || cursor == 0))
48 if (out_word_begins_at != NULL)
49 *out_word_begins_at = cursor;
52 while (!g_ascii_isspace (s[cursor - 1]) && cursor > 0)
57 while (!g_ascii_isspace (s[end]) && s[end] != '\0')
60 if (out_word_begins_at != NULL)
61 *out_word_begins_at = begin;
63 return g_strndup (s + begin, end - begin);
71 GOptionContext *context;
74 g_set_prgname (g_path_get_basename ((*argv)[0]));
76 context = g_option_context_new (_("COMMAND"));
77 g_option_context_set_help_enabled (context, FALSE);
80 " help Show this information\n"
81 " get Get the value of a key\n"
82 " set Set the value of a key\n"
83 " reset Reset the value of a key\n"
84 " monitor Monitor a key for changes\n"
85 " writable Check if a key is writable\n"
87 "Use '%s COMMAND --help' to get help for individual commands.\n"),
89 g_option_context_set_description (context, s);
91 s = g_option_context_get_help (context, FALSE, NULL);
97 g_option_context_free (context);
99 return use_stdout ? 0 : 1;
103 remove_arg (gint num, gint *argc, gchar **argv[])
107 g_assert (num <= (*argc));
109 for (n = num; (*argv)[n] != NULL; n++)
110 (*argv)[n] = (*argv)[n+1];
112 (*argc) = (*argc) - 1;
117 modify_argv0_for_command (gint *argc,
119 const gchar *command)
123 g_assert (g_strcmp0 ((*argv)[1], command) == 0);
124 remove_arg (1, argc, argv);
126 s = g_strdup_printf ("%s %s", (*argv)[0], command);
131 schema_exists (const gchar *name)
133 const gchar * const *schemas;
136 schemas = g_settings_list_schemas ();
137 for (i = 0; schemas[i]; i++)
138 if (g_strcmp0 (name, schemas[i]) == 0)
145 list_schemas (const gchar *prefix)
147 const gchar * const *schemas;
150 schemas = g_settings_list_schemas ();
151 for (i = 0; schemas[i]; i++)
152 if (prefix == NULL || g_str_has_prefix (schemas[i], prefix))
153 g_print ("%s \n", schemas[i]);
157 key_exists (GSettings *settings,
166 keys = g_settings_list_keys (settings);
167 for (i = 0; keys[i]; i++)
168 if (g_strcmp0 (keys[i], name) == 0)
179 list_keys (GSettings *settings,
185 keys = g_settings_list_keys (settings);
186 for (i = 0; keys[i]; i++)
188 if (prefix == NULL || g_str_has_prefix (keys[i], prefix))
189 g_print ("%s \n", keys[i]);
195 list_options (GOptionContext *context,
198 /* FIXME extract options from context */
199 const gchar *options[] = { "--help", "--path", NULL };
201 for (i = 0; options[i]; i++)
202 if (g_str_has_prefix (options[i], prefix))
203 g_print ("%s \n", options[i]);
207 handle_get (gint *argc,
209 gboolean request_completion,
210 gchar *completion_cur,
211 gchar *completion_prev)
218 GOptionContext *context;
219 GOptionEntry entries[] = {
220 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
226 modify_argv0_for_command (argc, argv, "get");
228 context = g_option_context_new (_("SCHEMA KEY"));
229 g_option_context_set_help_enabled (context, FALSE);
230 g_option_context_set_summary (context, _("Get the value of KEY"));
231 g_option_context_set_description (context,
233 " SCHEMA The id of the schema\n"
234 " KEY The name of the key\n"));
235 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
243 if (!g_option_context_parse (context, argc, argv, NULL))
245 if (!request_completion)
248 s = g_option_context_get_help (context, FALSE, NULL);
249 g_printerr ("%s", s);
261 if (request_completion && completion_cur[0] == '-')
263 list_options (context, completion_cur);
268 if (request_completion && !schema_exists (schema))
270 list_schemas (schema);
276 settings = g_settings_new_with_path (schema, path);
278 settings = g_settings_new (schema);
280 if (request_completion && !key_exists (settings, key))
282 list_keys (settings, key);
287 if (!request_completion)
289 v = g_settings_get_value (settings, key);
290 g_print ("%s\n", g_variant_print (v, FALSE));
297 g_object_unref (settings);
299 g_option_context_free (context);
305 handle_set (gint *argc,
307 gboolean request_completion,
308 gchar *completion_cur,
309 gchar *completion_prev)
316 GVariant *v, *default_v;
317 const GVariantType *type;
318 GOptionContext *context;
319 GOptionEntry entries[] = {
320 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
326 modify_argv0_for_command (argc, argv, "set");
328 context = g_option_context_new (_("SCHEMA KEY VALUE"));
329 g_option_context_set_help_enabled (context, FALSE);
330 g_option_context_set_summary (context, _("Set the value of KEY"));
331 g_option_context_set_description (context,
333 " SCHEMA The id of the schema\n"
334 " KEY The name of the key\n"
335 " VALUE The value to set key to, as a serialized GVariant\n"));
336 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
344 if (!g_option_context_parse (context, argc, argv, NULL))
346 if (!request_completion)
349 s = g_option_context_get_help (context, FALSE, NULL);
350 g_printerr ("%s", s);
363 if (request_completion && completion_cur[0] == '-')
365 list_options (context, completion_cur);
370 if (request_completion && !schema_exists (schema))
372 list_schemas (schema);
378 settings = g_settings_new_with_path (schema, path);
380 settings = g_settings_new (schema);
382 if (request_completion && !key_exists (settings, key))
384 list_keys (settings, key);
389 if (!request_completion)
391 default_v = g_settings_get_value (settings, key);
392 type = g_variant_get_type (default_v);
395 v = g_variant_parse (type, value, NULL, NULL, &error);
396 g_variant_unref (default_v);
399 g_printerr ("%s\n", error->message);
403 if (!g_settings_set_value (settings, key, v))
405 g_printerr (_("Key %s is not writable\n"), key);
415 g_object_unref (settings);
417 g_option_context_free (context);
424 handle_reset (gint *argc,
426 gboolean request_completion,
427 gchar *completion_cur,
428 gchar *completion_prev)
434 GOptionContext *context;
435 GOptionEntry entries[] = {
436 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
442 modify_argv0_for_command (argc, argv, "reset");
444 context = g_option_context_new (_("SCHEMA KEY VALUE"));
445 g_option_context_set_help_enabled (context, FALSE);
446 g_option_context_set_summary (context, _("Sets KEY to its default value"));
447 g_option_context_set_description (context,
449 " SCHEMA The id of the schema\n"
450 " KEY The name of the key\n"));
451 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
459 if (!g_option_context_parse (context, argc, argv, NULL))
461 if (!request_completion)
464 s = g_option_context_get_help (context, FALSE, NULL);
465 g_printerr ("%s", s);
476 if (request_completion && completion_cur[0] == '-')
478 list_options (context, completion_cur);
483 if (request_completion && !schema_exists (schema))
485 list_schemas (schema);
491 settings = g_settings_new_with_path (schema, path);
493 settings = g_settings_new (schema);
495 if (request_completion && !key_exists (settings, key))
497 list_keys (settings, key);
502 if (!request_completion)
504 g_settings_reset (settings, key);
511 g_object_unref (settings);
513 g_option_context_free (context);
519 handle_writable (gint *argc,
521 gboolean request_completion,
522 gchar *completion_cur,
523 gchar *completion_prev)
529 GOptionContext *context;
530 GOptionEntry entries[] = {
531 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
537 modify_argv0_for_command (argc, argv, "writable");
539 context = g_option_context_new (_("SCHEMA KEY"));
540 g_option_context_set_help_enabled (context, FALSE);
541 g_option_context_set_summary (context, _("Find out whether KEY is writable"));
542 g_option_context_set_description (context,
544 " SCHEMA The id of the schema\n"
545 " KEY The name of the key\n"));
546 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
554 if (!g_option_context_parse (context, argc, argv, NULL))
556 if (!request_completion)
559 s = g_option_context_get_help (context, FALSE, NULL);
560 g_printerr ("%s", s);
571 if (request_completion && completion_cur[0] == '-')
573 list_options (context, completion_cur);
578 if (request_completion && !schema_exists (schema))
580 list_schemas (schema);
586 settings = g_settings_new_with_path (schema, path);
588 settings = g_settings_new (schema);
590 if (request_completion && !key_exists (settings, key))
592 list_keys (settings, key);
597 if (!request_completion)
599 if (g_settings_is_writable (settings, key))
608 g_object_unref (settings);
610 g_option_context_free (context);
616 key_changed (GSettings *settings,
622 v = g_settings_get_value (settings, key);
623 value = g_variant_print (v, FALSE);
624 g_print ("%s\n", value);
630 handle_monitor (gint *argc,
632 gboolean request_completion,
633 gchar *completion_cur,
634 gchar *completion_prev)
640 gchar *detailed_signal;
642 GOptionContext *context;
643 GOptionEntry entries[] = {
644 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
650 modify_argv0_for_command (argc, argv, "monitor");
652 context = g_option_context_new (_("SCHEMA KEY"));
653 g_option_context_set_help_enabled (context, FALSE);
654 g_option_context_set_summary (context,
655 _("Monitor KEY for changes and print the changed values.\n"
656 "Monitoring will continue until the process is terminated."));
658 g_option_context_set_description (context,
660 " SCHEMA The id of the schema\n"
661 " KEY The name of the key\n"));
662 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
670 if (!g_option_context_parse (context, argc, argv, NULL))
672 if (!request_completion)
675 s = g_option_context_get_help (context, FALSE, NULL);
676 g_printerr ("%s", s);
687 if (request_completion && completion_cur[0] == '-')
689 list_options (context, completion_cur);
694 if (request_completion && !schema_exists (schema))
696 list_schemas (schema);
702 settings = g_settings_new_with_path (schema, path);
704 settings = g_settings_new (schema);
706 if (request_completion && !key_exists (settings, key))
708 list_keys (settings, key);
713 if (!request_completion)
715 detailed_signal = g_strdup_printf ("changed::%s", key);
716 g_signal_connect (settings, detailed_signal,
717 G_CALLBACK (key_changed), NULL);
719 loop = g_main_loop_new (NULL, FALSE);
720 g_main_loop_run (loop);
721 g_main_loop_unref (loop);
727 g_object_unref (settings);
729 g_option_context_free (context);
735 main (int argc, char *argv[])
739 gboolean request_completion;
740 gchar *completion_cur;
741 gchar *completion_prev;
743 setlocale (LC_ALL, "");
748 completion_cur = NULL;
749 completion_prev = NULL;
750 request_completion = FALSE;
754 ret = usage (&argc, &argv, FALSE);
761 if (g_strcmp0 (command, "help") == 0)
763 if (!request_completion)
764 ret = usage (&argc, &argv, TRUE);
766 else if (g_strcmp0 (command, "get") == 0)
767 ret = handle_get (&argc, &argv, request_completion, completion_cur, completion_prev);
768 else if (g_strcmp0 (command, "set") == 0)
769 ret = handle_set (&argc, &argv, request_completion, completion_cur, completion_prev);
770 else if (g_strcmp0 (command, "reset") == 0)
771 ret = handle_reset (&argc, &argv, request_completion, completion_cur, completion_prev);
772 else if (g_strcmp0 (command, "monitor") == 0)
773 ret = handle_monitor (&argc, &argv, request_completion, completion_cur, completion_prev);
774 else if (g_strcmp0 (command, "writable") == 0)
775 ret = handle_writable (&argc, &argv, request_completion, completion_cur, completion_prev);
776 else if (g_strcmp0 (command, "complete") == 0 && argc == 4 && !request_completion)
778 gchar *completion_line;
779 gint completion_point;
781 gchar **completion_argv;
782 gint completion_argc;
785 request_completion = TRUE;
787 completion_line = argv[2];
788 completion_point = strtol (argv[3], &endp, 10);
789 if (endp == argv[3] || *endp != '\0')
792 if (!g_shell_parse_argv (completion_line,
797 /* can't parse partical cmdline, don't attempt completion */
801 completion_prev = NULL;
802 completion_cur = pick_word_at (completion_line, completion_point, &cur_begin);
806 for (prev_end = cur_begin - 1; prev_end >= 0; prev_end--)
808 if (!g_ascii_isspace (completion_line[prev_end]))
810 completion_prev = pick_word_at (completion_line, prev_end, NULL);
816 argc = completion_argc;
817 argv = completion_argv;
824 if (request_completion)
826 g_print ("help \nget \nmonitor \nwritable \nset \nreset \n");
831 g_printerr (_("Unknown command '%s'\n"), argv[1]);
832 ret = usage (&argc, &argv, FALSE);
837 g_free (completion_cur);
838 g_free (completion_prev);