2008-05-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / tools / gpgconf.c
blob9c1c77a2d0747dad4b6e4a74328f6cca50413e8d
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 aApplyDefaults,
47 aListConfig,
48 aCheckConfig
53 /* The list of commands and options. */
54 static ARGPARSE_OPTS opts[] =
56 { 300, NULL, 0, N_("@Commands:\n ") },
58 { aListComponents, "list-components", 256, N_("list all components") },
59 { aCheckPrograms, "check-programs", 256, N_("check all programs") },
60 { aListOptions, "list-options", 256, N_("|COMPONENT|list options") },
61 { aChangeOptions, "change-options", 256, N_("|COMPONENT|change options") },
62 { aApplyDefaults, "apply-defaults", 256,
63 N_("apply global default values") },
64 { aListConfig, "list-config", 256,
65 N_("list global configuration file") },
66 { aCheckConfig, "check-config", 256,
67 N_("check global configuration file") },
69 { 301, NULL, 0, N_("@\nOptions:\n ") },
71 { oOutput, "output", 2, N_("use as output file") },
72 { oVerbose, "verbose", 0, N_("verbose") },
73 { oQuiet, "quiet", 0, N_("quiet") },
74 { oDryRun, "dry-run", 0, N_("do not make any changes") },
75 { oRuntime, "runtime", 0, N_("activate changes at runtime, if possible") },
76 /* hidden options */
77 { oNoVerbose, "no-verbose", 0, "@"},
78 {0}
82 /* Print usage information and and provide strings for help. */
83 static const char *
84 my_strusage( int level )
86 const char *p;
88 switch (level)
90 case 11: p = "gpgconf (GnuPG)";
91 break;
92 case 13: p = VERSION; break;
93 case 17: p = PRINTABLE_OS_NAME; break;
94 case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n");
95 break;
96 case 1:
97 case 40: p = _("Usage: gpgconf [options] (-h for help)");
98 break;
99 case 41:
100 p = _("Syntax: gpgconf [options]\n"
101 "Manage configuration options for tools of the GnuPG system\n");
102 break;
104 default: p = NULL; break;
106 return p;
110 /* Return the fp for the output. This is usually stdout unless
111 --output has been used. In the latter case this function opens
112 that file. */
113 static FILE *
114 get_outfp (FILE **fp)
116 if (!*fp)
118 if (opt.outfile)
120 *fp = fopen (opt.outfile, "w");
121 if (!*fp)
122 gc_error (1, errno, "can not open `%s'", opt.outfile);
124 else
125 *fp = stdout;
127 return *fp;
131 /* gpgconf main. */
133 main (int argc, char **argv)
135 ARGPARSE_ARGS pargs;
136 const char *fname;
137 int no_more_options = 0;
138 enum cmd_and_opt_values cmd = 0;
139 FILE *outfp = NULL;
141 set_strusage (my_strusage);
142 log_set_prefix ("gpgconf", 1);
144 /* Make sure that our subsystems are ready. */
145 init_common_subsystems ();
147 i18n_init();
149 /* Parse the command line. */
150 pargs.argc = &argc;
151 pargs.argv = &argv;
152 pargs.flags = 1; /* Do not remove the args. */
153 while (!no_more_options && optfile_parse (NULL, NULL, NULL, &pargs, opts))
155 switch (pargs.r_opt)
157 case oOutput: opt.outfile = pargs.r.ret_str; break;
158 case oQuiet: opt.quiet = 1; break;
159 case oDryRun: opt.dry_run = 1; break;
160 case oRuntime:
161 opt.runtime = 1;
162 break;
163 case oVerbose: opt.verbose++; break;
164 case oNoVerbose: opt.verbose = 0; break;
166 case aListComponents:
167 case aCheckPrograms:
168 case aListOptions:
169 case aChangeOptions:
170 case aApplyDefaults:
171 case aListConfig:
172 case aCheckConfig:
173 cmd = pargs.r_opt;
174 break;
176 default: pargs.err = 2; break;
180 if (log_get_errorcount (0))
181 exit (2);
183 fname = argc ? *argv : NULL;
185 switch (cmd)
187 case aListComponents:
188 default:
189 /* List all components. */
190 gc_component_list_components (get_outfp (&outfp));
191 break;
193 case aCheckPrograms:
194 /* Check all programs. */
195 gc_component_check_programs (get_outfp (&outfp));
196 break;
198 case aListOptions:
199 case aChangeOptions:
200 if (!fname)
202 fputs (_("usage: gpgconf [options] "), stderr);
203 putc ('\n',stderr);
204 fputs (_("Need one component argument"), stderr);
205 putc ('\n',stderr);
206 exit (2);
208 else
210 int idx = gc_component_find (fname);
211 if (idx < 0)
213 fputs (_("Component not found"), stderr);
214 putc ('\n', stderr);
215 exit (1);
217 gc_component_retrieve_options (idx);
218 if (gc_process_gpgconf_conf (NULL, 1, 0, NULL))
219 exit (1);
220 if (cmd == aListOptions)
221 gc_component_list_options (idx, get_outfp (&outfp));
222 else
223 gc_component_change_options (idx, stdin);
225 break;
227 case aListConfig:
228 if (gc_process_gpgconf_conf (fname, 0, 0, get_outfp (&outfp)))
229 exit (1);
230 break;
232 case aCheckConfig:
233 if (gc_process_gpgconf_conf (fname, 0, 0, NULL))
234 exit (1);
235 break;
237 case aApplyDefaults:
238 if (fname)
240 fputs (_("usage: gpgconf [options] "), stderr);
241 putc ('\n',stderr);
242 fputs (_("No argument allowed"), stderr);
243 putc ('\n',stderr);
244 exit (2);
246 gc_component_retrieve_options (-1);
247 if (gc_process_gpgconf_conf (NULL, 1, 1, NULL))
248 exit (1);
249 break;
252 if (outfp && outfp != stdout)
253 if (fclose (outfp))
254 gc_error (1, errno, "error closing `%s'", opt.outfile);
256 return 0;