2008-06-18 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / tools / gpgconf.c
blobbf976714661209fcf5a5806a0d8c34db271bd5aa
1 /* gpgconf.c - Configuration utility for GnuPG
2 * Copyright (C) 2003, 2007 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "gpgconf.h"
27 #include "i18n.h"
29 /* Constants to identify the commands and options. */
30 enum cmd_and_opt_values
32 aNull = 0,
33 oDryRun = 'n',
34 oOutput = 'o',
35 oQuiet = 'q',
36 oVerbose = 'v',
37 oRuntime = 'r',
38 oComponent = 'c',
39 oNoVerbose = 500,
40 oHomedir,
42 aListComponents,
43 aCheckPrograms,
44 aListOptions,
45 aChangeOptions,
46 aCheckOptions,
47 aApplyDefaults,
48 aListConfig,
49 aCheckConfig,
50 aListDirs
55 /* The list of commands and options. */
56 static ARGPARSE_OPTS opts[] =
58 { 300, NULL, 0, N_("@Commands:\n ") },
60 { aListComponents, "list-components", 256, N_("list all components") },
61 { aCheckPrograms, "check-programs", 256, N_("check all programs") },
62 { aListOptions, "list-options", 256, N_("|COMPONENT|list options") },
63 { aChangeOptions, "change-options", 256, N_("|COMPONENT|change options") },
64 { aCheckOptions, "check-options", 256, N_("|COMPONENT|check options") },
65 { aApplyDefaults, "apply-defaults", 256,
66 N_("apply global default values") },
67 { aListDirs, "list-dirs", 256,
68 N_("get the configuration directories for gpgconf") },
69 { aListConfig, "list-config", 256,
70 N_("list global configuration file") },
71 { aCheckConfig, "check-config", 256,
72 N_("check global configuration file") },
74 { 301, NULL, 0, N_("@\nOptions:\n ") },
76 { oOutput, "output", 2, N_("use as output file") },
77 { oVerbose, "verbose", 0, N_("verbose") },
78 { oQuiet, "quiet", 0, N_("quiet") },
79 { oDryRun, "dry-run", 0, N_("do not make any changes") },
80 { oRuntime, "runtime", 0, N_("activate changes at runtime, if possible") },
81 /* hidden options */
82 { oNoVerbose, "no-verbose", 0, "@"},
83 {0}
87 /* Print usage information and and provide strings for help. */
88 static const char *
89 my_strusage( int level )
91 const char *p;
93 switch (level)
95 case 11: p = "gpgconf (GnuPG)";
96 break;
97 case 13: p = VERSION; break;
98 case 17: p = PRINTABLE_OS_NAME; break;
99 case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n");
100 break;
101 case 1:
102 case 40: p = _("Usage: gpgconf [options] (-h for help)");
103 break;
104 case 41:
105 p = _("Syntax: gpgconf [options]\n"
106 "Manage configuration options for tools of the GnuPG system\n");
107 break;
109 default: p = NULL; break;
111 return p;
115 /* Return the fp for the output. This is usually stdout unless
116 --output has been used. In the latter case this function opens
117 that file. */
118 static FILE *
119 get_outfp (FILE **fp)
121 if (!*fp)
123 if (opt.outfile)
125 *fp = fopen (opt.outfile, "w");
126 if (!*fp)
127 gc_error (1, errno, "can not open `%s'", opt.outfile);
129 else
130 *fp = stdout;
132 return *fp;
136 /* gpgconf main. */
138 main (int argc, char **argv)
140 ARGPARSE_ARGS pargs;
141 const char *fname;
142 int no_more_options = 0;
143 enum cmd_and_opt_values cmd = 0;
144 FILE *outfp = NULL;
146 set_strusage (my_strusage);
147 log_set_prefix ("gpgconf", 1);
149 /* Make sure that our subsystems are ready. */
150 init_common_subsystems ();
152 i18n_init();
154 /* Parse the command line. */
155 pargs.argc = &argc;
156 pargs.argv = &argv;
157 pargs.flags = 1; /* Do not remove the args. */
158 while (!no_more_options && optfile_parse (NULL, NULL, NULL, &pargs, opts))
160 switch (pargs.r_opt)
162 case oOutput: opt.outfile = pargs.r.ret_str; break;
163 case oQuiet: opt.quiet = 1; break;
164 case oDryRun: opt.dry_run = 1; break;
165 case oRuntime:
166 opt.runtime = 1;
167 break;
168 case oVerbose: opt.verbose++; break;
169 case oNoVerbose: opt.verbose = 0; break;
171 case aListDirs:
172 case aListComponents:
173 case aCheckPrograms:
174 case aListOptions:
175 case aChangeOptions:
176 case aCheckOptions:
177 case aApplyDefaults:
178 case aListConfig:
179 case aCheckConfig:
180 cmd = pargs.r_opt;
181 break;
183 default: pargs.err = 2; break;
187 if (log_get_errorcount (0))
188 exit (2);
190 fname = argc ? *argv : NULL;
192 switch (cmd)
194 case aListComponents:
195 default:
196 /* List all components. */
197 gc_component_list_components (get_outfp (&outfp));
198 break;
200 case aCheckPrograms:
201 /* Check all programs. */
202 gc_check_programs (get_outfp (&outfp));
203 break;
205 case aListOptions:
206 case aChangeOptions:
207 case aCheckOptions:
208 if (!fname)
210 fputs (_("usage: gpgconf [options] "), stderr);
211 putc ('\n',stderr);
212 fputs (_("Need one component argument"), stderr);
213 putc ('\n',stderr);
214 exit (2);
216 else
218 int idx = gc_component_find (fname);
219 if (idx < 0)
221 fputs (_("Component not found"), stderr);
222 putc ('\n', stderr);
223 exit (1);
225 gc_component_retrieve_options (idx);
226 if (gc_process_gpgconf_conf (NULL, 1, 0, NULL))
227 exit (1);
228 if (cmd == aListOptions)
229 gc_component_list_options (idx, get_outfp (&outfp));
230 else if (cmd == aChangeOptions)
231 gc_component_change_options (idx, stdin, get_outfp (&outfp));
232 else
233 gc_component_check_options (idx, get_outfp (&outfp), NULL);
235 break;
237 case aListConfig:
238 if (gc_process_gpgconf_conf (fname, 0, 0, get_outfp (&outfp)))
239 exit (1);
240 break;
242 case aCheckConfig:
243 if (gc_process_gpgconf_conf (fname, 0, 0, NULL))
244 exit (1);
245 break;
247 case aApplyDefaults:
248 if (fname)
250 fputs (_("usage: gpgconf [options] "), stderr);
251 putc ('\n',stderr);
252 fputs (_("No argument allowed"), stderr);
253 putc ('\n',stderr);
254 exit (2);
256 gc_component_retrieve_options (-1);
257 if (gc_process_gpgconf_conf (NULL, 1, 1, NULL))
258 exit (1);
259 break;
261 case aListDirs:
262 /* Show the system configuration directory for gpgconf. */
263 get_outfp (&outfp);
264 fprintf (outfp, "sysconfdir:%s\n",
265 gc_percent_escape (gnupg_sysconfdir ()));
266 break;
269 if (outfp && outfp != stdout)
270 if (fclose (outfp))
271 gc_error (1, errno, "error closing `%s'", opt.outfile);
273 return 0;