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
28 /* Gettext translation */
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'},
46 printf (_("Usage: gaf config [OPTION] [GROUP KEY [VALUE]]\n"
48 "View and modify gEDA configuration.\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"
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"
62 "Please report bugs to %s.\n"),
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")
71 cmd_config_impl (void *data
, int argc
, char **argv
)
74 EdaConfig
*cfg
= NULL
, *parent
;
75 const gchar
*project_store_path
= NULL
;
76 const char *group
, *key
;
81 /* Parse command-line arguments */
82 while ((c
= getopt_long (argc
, argv
, config_short_options
,
83 config_long_options
, NULL
)) != -1) {
87 /* This is a long-form-only flag option, and has already been
88 * dealt with by getopt_long(). */
92 if (cfg
!= NULL
|| project_store_path
!= NULL
) {
93 fprintf (stderr
, multi_store_msg
);
94 fprintf (stderr
, see_help_msg
);
97 project_store_path
= (optarg
== NULL
) ? "." : optarg
;
101 if (cfg
!= NULL
|| project_store_path
!= NULL
) {
102 fprintf (stderr
, multi_store_msg
);
103 fprintf (stderr
, see_help_msg
);
106 cfg
= eda_config_get_system_context ();
110 if (cfg
!= NULL
|| project_store_path
!= NULL
) {
111 fprintf (stderr
, multi_store_msg
);
112 fprintf (stderr
, see_help_msg
);
115 cfg
= eda_config_get_user_context ();
123 /* getopt_long already printed an error message */
124 fprintf (stderr
, see_help_msg
);
129 g_assert_not_reached ();
133 /* If no configuration is available yet, grab the project
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
);
140 g_object_unref (project_store
);
143 /* If no further arguments were specified, output the configuration
145 if (argc
== optind
) {
146 printf ("%s\n", eda_config_get_filename (cfg
));
150 /* Attempt to load the file, and all its parents */
151 for (parent
= cfg
; parent
!= NULL
; parent
= eda_config_get_parent (parent
)) {
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
),
162 g_clear_error (&err
);
167 /* Otherwise, we must have group and key */
168 if (argc
- optind
< 2) {
170 _("ERROR: You must specify both configuration group and key.\n"));
171 fprintf (stderr
, see_help_msg
);
174 group
= argv
[optind
++];
175 key
= argv
[optind
++];
177 /* If no value was specified, output the parameter value. */
178 if (argc
== optind
) {
180 gchar
*value
= eda_config_get_string (cfg
, group
, key
, &err
);
182 fprintf (stderr
, _("ERROR: %s.\n"), err
->message
);
185 printf ("%s\n", value
);
189 /* If a value was specified, set the value and save the
191 if (argc
- optind
> 0) {
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
);
202 g_assert_not_reached ();
205 /* Main function for `gaf config' */
207 cmd_config (int argc
, char **argv
)
209 scm_boot_guile (argc
, argv
, cmd_config_impl
, NULL
); /* Doesn't return */