8d9fb200cb54f174ec7ed00d41abde9f08f5bf20
[gpgme.git] / tests / gpg / t-gpgconf.c
1 /* t-gpgconf.c - Regression test.
2    Copyright (C) 2001, 2004, 2007 g10 Code GmbH
3
4    This file is part of GPGME.
5
6    GPGME is free software; you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation; either version 2.1 of
9    the License, or (at your option) any later version.
10    
11    GPGME is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15    
16    You should have received a copy of the GNU Lesser General Public
17    License along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19    02111-1307, USA.  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <unistd.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <locale.h>
29 #include <string.h>
30
31 #ifdef HAVE_W32_SYSTEM
32 #include <windows.h>
33 #endif
34
35 #include <gpgme.h>
36
37
38 #define fail_if_err(err)                                        \
39   do                                                            \
40     {                                                           \
41       if (err)                                                  \
42         {                                                       \
43           fprintf (stderr, "%s:%d: %s: %s\n",                   \
44                    __FILE__, __LINE__, gpgme_strsource (err),   \
45                    gpgme_strerror (err));                       \
46           exit (1);                                             \
47         }                                                       \
48     }                                                           \
49   while (0)
50
51
52 void
53 init_gpgme (gpgme_protocol_t proto)
54 {
55   gpgme_error_t err;
56
57   gpgme_check_version (NULL);
58   setlocale (LC_ALL, "");
59   gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
60 #ifndef HAVE_W32_SYSTEM
61   gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
62 #endif
63
64   err = gpgme_engine_check_version (proto);
65   fail_if_err (err);
66 }
67
68 \f
69 static char *
70 spaces (char *str, int extra)
71 {
72   static char buf[80];
73   int len = str ? strlen (str) : 0;
74   int n;
75
76 #define TABSTOP 30
77   n = TABSTOP - len - extra;
78
79   memset (buf, ' ', sizeof (buf));
80   if (n < 1 || n > (sizeof (buf) - 1))
81     {
82       buf[0] = '\n';
83       n = TABSTOP + 1;
84     }
85
86   buf[n] = '\0';
87   return buf;
88 }
89
90
91 void
92 dump_arg (int type, gpgme_conf_arg_t arg)
93 {
94   if (!arg)
95     {
96       printf ("(none)");
97       return;
98     }
99
100   while (arg)
101     {
102       switch (type)
103         {
104         case GPGME_CONF_STRING:
105         case GPGME_CONF_PATHNAME:
106         case GPGME_CONF_LDAP_SERVER:
107           printf ("%s", arg->value.string);
108           break;
109
110         case GPGME_CONF_UINT32:
111           printf ("%u", arg->value.uint32);
112           break;
113
114         case GPGME_CONF_INT32:
115           printf ("%i", arg->value.int32);
116           break;
117
118         case GPGME_CONF_NONE:
119           printf ("%i (times)", arg->value.count);
120           break;
121
122         default:
123           printf ("(unknown type)");
124         }
125
126       arg = arg->next;
127       if (arg)
128         printf (" ");
129     }
130 }
131
132
133 void
134 dump_opt (gpgme_conf_opt_t opt)
135 {
136   char level;
137   char runtime = (opt->flags & GPGME_CONF_RUNTIME) ? 'r' : ' ';
138
139   switch (opt->level)
140     {
141     case GPGME_CONF_BASIC:
142       level = 'b';
143       break;
144     case GPGME_CONF_ADVANCED:
145       level = 'a';
146       break;
147     case GPGME_CONF_EXPERT:
148       level = 'e';
149       break;
150     case GPGME_CONF_INVISIBLE:
151       level = 'i';
152       break;
153     case GPGME_CONF_INTERNAL:
154       level = '#';
155       break;
156     default:
157       level = '?';
158     }
159
160   if (opt->flags & GPGME_CONF_GROUP)
161     {
162       printf ("\n");
163       printf ("%c%c [%s]%s%s\n", level, runtime, opt->name, spaces (opt->name, 5),
164               opt->description
165               ? opt->description : "");
166     }
167   else
168     {
169       if (opt->argname)
170         {
171           char *more = (opt->flags & GPGME_CONF_LIST) ? "..." : "";
172       
173           if (opt->flags & GPGME_CONF_OPTIONAL)
174             {
175               printf ("%c%c --%s [%s%s] %s", level, runtime, opt->name, opt->argname, more,
176                       spaces (opt->name, 9 + strlen (opt->argname) + strlen (more)));
177             }
178           else
179             {
180               printf ("%c%c --%s %s%s %s", level, runtime, opt->name, opt->argname, more,
181                       spaces (opt->name, 7 + strlen (opt->argname) + strlen (more)));
182             }
183         }
184       else
185         printf ("%c%c --%s%s", level, runtime, opt->name, spaces (opt->name, 5));
186       
187       if (opt->description)
188         printf ("%s", opt->description);
189       printf ("\n");
190
191       if (opt->flags & GPGME_CONF_DEFAULT)
192         {
193           printf ("%s%s = ", spaces (NULL, 0), opt->argname ? opt->argname : "(default)");
194           dump_arg (opt->type, opt->default_value);
195           printf ("\n");
196         }
197       else if (opt->flags & GPGME_CONF_DEFAULT_DESC)
198         printf ("%s%s = %s\n", spaces (NULL, 0), opt->argname ? opt->argname : "(default)",
199                 opt->default_description);
200
201       if (opt->no_arg_value)
202         {
203           printf ("%sNo Arg Def = ", spaces (NULL, 0));
204           dump_arg (opt->type, opt->no_arg_value);
205           printf ("\n");
206         }
207       if (opt->value)
208         {
209           printf ("%sCurrent = ", spaces (NULL, 0));
210           dump_arg (opt->type, opt->value);
211           printf ("\n");
212         }
213     }
214
215 #if 0
216   arg = comp->options;
217   while (opt)
218     {
219       dump_opt (opt);
220       opt = opt->next;
221     }
222 #endif
223 }
224
225
226 void
227 dump_comp (gpgme_conf_comp_t comp)
228 {
229   gpgme_conf_opt_t opt;
230
231   printf ("COMPONENT\n");
232   printf ("=========\n");
233   printf ("  Name: %s\n", comp->name);
234   if (comp->description)
235     printf ("  Desc: %s\n", comp->description);
236   if (comp->program_name)
237     printf ("  Path: %s\n", comp->program_name);
238   printf ("\n");
239
240   opt = comp->options;
241   while (opt)
242     {
243       dump_opt (opt);
244       opt = opt->next;
245     }
246 }
247
248
249 int 
250 main (int argc, char **argv)
251 {
252   gpgme_ctx_t ctx;
253   gpgme_error_t err;
254   gpgme_conf_comp_t conf;
255   gpgme_conf_comp_t comp;
256   int first;
257
258 #ifndef ENABLE_GPGCONF
259   return 0;
260 #endif
261
262   init_gpgme (GPGME_PROTOCOL_GPGCONF);
263
264   err = gpgme_new (&ctx);
265   fail_if_err (err);
266
267   err = gpgme_op_conf_load (ctx, &conf);
268   fail_if_err (err);
269
270   comp = conf;
271   first = 1;
272   while (comp)
273     {
274       if (!first)
275         printf ("\n");
276       else
277         first = 0;
278       dump_comp (comp);
279       comp = comp->next;
280     }
281
282 #if 1
283   /* Now change something.  */
284   {
285     unsigned int count = 1;
286     gpgme_conf_arg_t arg;
287     gpgme_conf_opt_t opt;
288
289     err = gpgme_conf_arg_new (&arg, GPGME_CONF_NONE, &count);
290     fail_if_err (err);
291
292     comp = conf;
293     while (comp && strcmp (comp->name, "dirmngr"))
294       comp = comp->next;
295
296     if (comp)
297       {
298         opt = comp->options;
299         while (opt && strcmp (opt->name, "verbose"))
300           opt = opt->next;
301         
302         /* Allow for the verbose option not to be there.  */
303         if (opt)
304           {
305             err = gpgme_conf_opt_change (opt, 0, arg);
306             fail_if_err (err);
307             
308             err = gpgme_op_conf_save (ctx, comp);
309             fail_if_err (err);
310           }
311       }
312   }
313 #endif
314
315   gpgme_conf_release (conf);
316
317   return 0;
318 }