gaf-config: Use new GFile config sys APIs where appropriate.
[geda-gaf/pcjc2.git] / gaf / config.c
blob75d8ca6bc37ead59c7ff0084c0da50e099fb644e
1 /*
2 * gEDA/gaf command-line utility
3 * Copyright (C) 2012 Peter Brett <peter@peter-b.co.uk>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <version.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <getopt.h>
29 /* Gettext translation */
30 #include "gettext.h"
32 #include <libgeda/libgeda.h>
34 #define config_short_options "hp::su"
36 static struct option config_long_options[] =
38 {"help", 0, NULL, 'h'},
39 {"project", 2, NULL, 'p'},
40 {"system", 0, NULL, 's'},
41 {"user", 0, NULL, 'u'},
44 static void
45 config_usage (void)
47 printf (_("Usage: gaf config [OPTION] [GROUP KEY [VALUE]]\n"
48 "\n"
49 "View and modify gEDA configuration.\n"
50 "\n"
51 " -p, --project[=PATH] select project configuration [PATH=.]\n"
52 " -u, --user select user configuration\n"
53 " -s, --system select system configuration\n"
54 " -h, --help display usage information and exit\n"
55 "\n"
56 "If GROUP and KEY are specified, retrieves the value of that\n"
57 "configuration parameter. If a VALUE was specified, sets the value of\n"
58 "the parameter. The -p, -u and -s options can be used to select the\n"
59 "configuration store affected (by default, the project configuration\n"
60 "store for the current directory). If no GROUP and KEY were provided,\n"
61 "outputs the filename of the selected configuration store.\n"
62 "\n"
63 "Please report bugs to %s.\n"),
64 PACKAGE_BUGREPORT);
65 exit (0);
68 #define see_help_msg _("\nRun `gaf config --help' for more information.\n")
69 #define multi_store_msg _("ERROR: You may only specify a single configuration store.\n")
71 int
72 cmd_config (int argc, char **argv)
74 int c;
75 EdaConfig *cfg = NULL, *parent;
76 const gchar *project_store_path = NULL;
77 const char *group, *key;
79 scm_init_guile ();
80 libgeda_init ();
82 /* Parse command-line arguments */
83 while ((c = getopt_long (argc, argv, config_short_options,
84 config_long_options, NULL)) != -1) {
85 switch (c) {
87 case 0:
88 /* This is a long-form-only flag option, and has already been
89 * dealt with by getopt_long(). */
90 break;
92 case 'p':
93 if (cfg != NULL || project_store_path != NULL) {
94 fprintf (stderr, multi_store_msg);
95 fprintf (stderr, see_help_msg);
96 exit (1);
98 project_store_path = (optarg == NULL) ? "." : optarg;
99 break;
101 case 's':
102 if (cfg != NULL || project_store_path != NULL) {
103 fprintf (stderr, multi_store_msg);
104 fprintf (stderr, see_help_msg);
105 exit (1);
107 cfg = eda_config_get_system_context ();
108 break;
110 case 'u':
111 if (cfg != NULL || project_store_path != NULL) {
112 fprintf (stderr, multi_store_msg);
113 fprintf (stderr, see_help_msg);
114 exit (1);
116 cfg = eda_config_get_user_context ();
117 break;
119 case 'h':
120 config_usage ();
121 break;
123 case '?':
124 /* getopt_long already printed an error message */
125 fprintf (stderr, see_help_msg);
126 exit (1);
127 break;
129 default:
130 g_assert_not_reached ();
134 /* If no configuration is available yet, grab the project
135 * configuration. */
136 if (cfg == NULL) {
137 if (project_store_path == NULL)
138 project_store_path = ".";
139 GFile *project_store = g_file_new_for_commandline_arg (project_store_path);
140 cfg = eda_config_get_context_for_file (project_store);
143 /* If no further arguments were specified, output the configuration
144 * file location. */
145 if (argc == optind) {
146 printf ("%s\n", eda_config_get_filename (cfg));
147 exit (0);
150 /* Attempt to load the file, and all its parents */
151 for (parent = cfg; parent != NULL; parent = eda_config_get_parent (parent)) {
152 GError *err = NULL;
153 if (eda_config_is_loaded (parent) ||
154 eda_config_get_file (parent) == NULL) continue;
156 if (!eda_config_load (parent, &err)) {
157 if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) {
158 fprintf (stderr, _("WARNING: Could not load '%s': %s.\n"),
159 eda_config_get_filename (parent),
160 err->message);
162 g_clear_error (&err);
167 /* Otherwise, we must have group and key */
168 if (argc - optind < 2) {
169 fprintf (stderr,
170 _("ERROR: You must specify both configuration group and key.\n"));
171 fprintf (stderr, see_help_msg);
172 exit (1);
174 group = argv[optind++];
175 key = argv[optind++];
177 /* If no value was specified, output the parameter value. */
178 if (argc == optind) {
179 GError *err = NULL;
180 gchar *value = eda_config_get_string (cfg, group, key, &err);
181 if (value == NULL) {
182 fprintf (stderr, _("ERROR: %s.\n"), err->message);
183 exit (1);
185 printf ("%s\n", value);
186 exit (0);
189 /* If a value was specified, set the value and save the
190 * configuration. */
191 if (argc - optind > 0) {
192 GError *err = NULL;
193 const gchar *value = argv[optind++];
194 eda_config_set_string (cfg, group, key, value);
195 if (!eda_config_save (cfg, &err)) {
196 fprintf (stderr, _("ERROR: %s.\n"), err->message);
197 exit (1);
199 exit (0);
202 g_assert_not_reached ();
204 return 0;