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
);
142 /* If no further arguments were specified, output the configuration
144 if (argc
== optind
) {
145 printf ("%s\n", eda_config_get_filename (cfg
));
149 /* Attempt to load the file, and all its parents */
150 for (parent
= cfg
; parent
!= NULL
; parent
= eda_config_get_parent (parent
)) {
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
),
161 g_clear_error (&err
);
166 /* Otherwise, we must have group and key */
167 if (argc
- optind
< 2) {
169 _("ERROR: You must specify both configuration group and key.\n"));
170 fprintf (stderr
, see_help_msg
);
173 group
= argv
[optind
++];
174 key
= argv
[optind
++];
176 /* If no value was specified, output the parameter value. */
177 if (argc
== optind
) {
179 gchar
*value
= eda_config_get_string (cfg
, group
, key
, &err
);
181 fprintf (stderr
, _("ERROR: %s.\n"), err
->message
);
184 printf ("%s\n", value
);
188 /* If a value was specified, set the value and save the
190 if (argc
- optind
> 0) {
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
);
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 */