Migrate to Guile 2.2
[geda-gaf.git] / gaf / config.c
blob9597a028230ef17e9e3418ea4c9e8de9e8670096
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
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <getopt.h>
28 /* Gettext translation */
29 #include "gettext.h"
31 #include <libgeda/libgeda.h>
33 #define config_short_options "hp::su"
35 static struct option config_long_options[] =
37 {"help", 0, NULL, 'h'},
38 {"project", 2, NULL, 'p'},
39 {"system", 0, NULL, 's'},
40 {"user", 0, NULL, 'u'},
43 static void
44 config_usage (void)
46 printf (_("Usage: gaf config [OPTION] [GROUP KEY [VALUE]]\n"
47 "\n"
48 "View and modify gEDA configuration.\n"
49 "\n"
50 " -p, --project[=PATH] select project configuration [PATH=.]\n"
51 " -u, --user select user configuration\n"
52 " -s, --system select system configuration\n"
53 " -h, --help display usage information and exit\n"
54 "\n"
55 "If GROUP and KEY are specified, retrieves the value of that\n"
56 "configuration parameter. If a VALUE was specified, sets the value of\n"
57 "the parameter. The -p, -u and -s options can be used to select the\n"
58 "configuration store affected (by default, the project configuration\n"
59 "store for the current directory). If no GROUP and KEY were provided,\n"
60 "outputs the filename of the selected configuration store.\n"
61 "\n"
62 "Please report bugs to %s.\n"),
63 PACKAGE_BUGREPORT);
64 exit (0);
67 #define see_help_msg _("\nRun `gaf config --help' for more information.\n")
68 #define multi_store_msg _("ERROR: You may only specify a single configuration store.\n")
70 static void
71 cmd_config_impl (void *data, int argc, char **argv)
73 int c;
74 EdaConfig *cfg = NULL, *parent;
75 const gchar *project_store_path = NULL;
76 const char *group, *key;
78 scm_init_guile ();
79 libgeda_init ();
81 /* Parse command-line arguments */
82 while ((c = getopt_long (argc, argv, config_short_options,
83 config_long_options, NULL)) != -1) {
84 switch (c) {
86 case 0:
87 /* This is a long-form-only flag option, and has already been
88 * dealt with by getopt_long(). */
89 break;
91 case 'p':
92 if (cfg != NULL || project_store_path != NULL) {
93 fprintf (stderr, multi_store_msg);
94 fprintf (stderr, see_help_msg);
95 exit (1);
97 project_store_path = (optarg == NULL) ? "." : optarg;
98 break;
100 case 's':
101 if (cfg != NULL || project_store_path != NULL) {
102 fprintf (stderr, multi_store_msg);
103 fprintf (stderr, see_help_msg);
104 exit (1);
106 cfg = eda_config_get_system_context ();
107 break;
109 case 'u':
110 if (cfg != NULL || project_store_path != NULL) {
111 fprintf (stderr, multi_store_msg);
112 fprintf (stderr, see_help_msg);
113 exit (1);
115 cfg = eda_config_get_user_context ();
116 break;
118 case 'h':
119 config_usage ();
120 break;
122 case '?':
123 /* getopt_long already printed an error message */
124 fprintf (stderr, see_help_msg);
125 exit (1);
126 break;
128 default:
129 g_assert_not_reached ();
133 /* If no configuration is available yet, grab the project
134 * configuration. */
135 if (cfg == NULL) {
136 if (project_store_path == NULL)
137 project_store_path = ".";
138 GFile *project_store = g_file_new_for_commandline_arg (project_store_path);
139 cfg = eda_config_get_context_for_file (project_store);
142 /* If no further arguments were specified, output the configuration
143 * file location. */
144 if (argc == optind) {
145 printf ("%s\n", eda_config_get_filename (cfg));
146 exit (0);
149 /* Attempt to load the file, and all its parents */
150 for (parent = cfg; parent != NULL; parent = eda_config_get_parent (parent)) {
151 GError *err = NULL;
152 if (eda_config_is_loaded (parent) ||
153 eda_config_get_file (parent) == NULL) continue;
155 if (!eda_config_load (parent, &err)) {
156 if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) {
157 fprintf (stderr, _("WARNING: Could not load '%s': %s.\n"),
158 eda_config_get_filename (parent),
159 err->message);
161 g_clear_error (&err);
166 /* Otherwise, we must have group and key */
167 if (argc - optind < 2) {
168 fprintf (stderr,
169 _("ERROR: You must specify both configuration group and key.\n"));
170 fprintf (stderr, see_help_msg);
171 exit (1);
173 group = argv[optind++];
174 key = argv[optind++];
176 /* If no value was specified, output the parameter value. */
177 if (argc == optind) {
178 GError *err = NULL;
179 gchar *value = eda_config_get_string (cfg, group, key, &err);
180 if (value == NULL) {
181 fprintf (stderr, _("ERROR: %s.\n"), err->message);
182 exit (1);
184 printf ("%s\n", value);
185 exit (0);
188 /* If a value was specified, set the value and save the
189 * configuration. */
190 if (argc - optind > 0) {
191 GError *err = NULL;
192 const gchar *value = argv[optind++];
193 eda_config_set_string (cfg, group, key, value);
194 if (!eda_config_save (cfg, &err)) {
195 fprintf (stderr, _("ERROR: %s.\n"), err->message);
196 exit (1);
198 exit (0);
201 g_assert_not_reached ();
204 /* Main function for `gaf config' */
206 cmd_config (int argc, char **argv)
208 scm_boot_guile (argc, argv, cmd_config_impl, NULL); /* Doesn't return */
209 return 0;