Move gpg test programs to the top test directory.
[gpgme.git] / tests / run-import.c
1 /* pgp-import.c  - Helper to run an import command
2    Copyright (C) 2008, 2009 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* We need to include config.h so that we know whether we are building
21    with large file system (LFS) support. */
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <gpgme.h>
31
32 #define PGM "run-import"
33
34 #include "run-support.h"
35
36
37 static int verbose;
38
39
40 static int
41 show_usage (int ex)
42 {
43   fputs ("usage: " PGM " [options] FILENAMEs\n\n"
44          "Options:\n"
45          "  --verbose        run in verbose mode\n"
46          "  --url            import from given URLs\n"
47          "  -0               URLs are delimited by a nul\n"
48          , stderr);
49   exit (ex);
50 }
51
52 int 
53 main (int argc, char **argv)
54 {
55   int last_argc = -1;
56   gpgme_error_t err;
57   gpgme_ctx_t ctx;
58   int url_mode = 0;
59   int nul_mode = 0;
60   gpgme_import_result_t impres;
61   gpgme_data_t data;
62
63   if (argc)
64     { argc--; argv++; }
65   while (argc && last_argc != argc )
66     {
67       last_argc = argc;
68       if (!strcmp (*argv, "--"))
69         {
70           argc--; argv++;
71           break;
72         }
73       else if (!strcmp (*argv, "--help"))
74         show_usage (0);
75       else if (!strcmp (*argv, "--verbose"))
76         {
77           verbose = 1;
78           argc--; argv++;
79         }
80       else if (!strcmp (*argv, "--url"))
81         {
82           url_mode = 1;
83           argc--; argv++;
84         }
85       else if (!strcmp (*argv, "-0"))
86         {
87           nul_mode = 1;
88           argc--; argv++;
89         }
90       else if (!strncmp (*argv, "--", 2))
91         show_usage (1);
92       
93     }          
94  
95   if (!argc)
96     show_usage (1);
97
98   init_gpgme (GPGME_PROTOCOL_OpenPGP);
99
100   err = gpgme_new (&ctx);
101   fail_if_err (err);
102   gpgme_set_protocol (ctx, GPGME_PROTOCOL_OpenPGP);
103
104   for (; argc; argc--, argv++)
105     {
106       printf ("reading file `%s'\n", *argv);
107       err = gpgme_data_new_from_file (&data, *argv, 1);
108       fail_if_err (err);
109
110       if (url_mode)
111         gpgme_data_set_encoding (data, (nul_mode? GPGME_DATA_ENCODING_URL0
112                                         : GPGME_DATA_ENCODING_URL));
113       
114       err = gpgme_op_import (ctx, data);
115       fail_if_err (err);
116       impres = gpgme_op_import_result (ctx);
117       if (!impres)
118         {
119           fprintf (stderr, PGM ": no import result returned\n");
120           exit (1);
121         }
122       print_import_result (impres);
123       
124       gpgme_data_release (data);
125     }
126
127   gpgme_release (ctx);
128   return 0;
129 }