Blame tests/run-import.c

Packit Service 672cf4
/* pgp-import.c  - Helper to run an import command
Packit Service 6c01f9
   Copyright (C) 2008, 2009 g10 Code GmbH
Packit Service 6c01f9
Packit Service 6c01f9
   This file is part of GPGME.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is free software; you can redistribute it and/or modify it
Packit Service 6c01f9
   under the terms of the GNU Lesser General Public License as
Packit Service 6c01f9
   published by the Free Software Foundation; either version 2.1 of
Packit Service 6c01f9
   the License, or (at your option) any later version.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is distributed in the hope that it will be useful, but
Packit Service 6c01f9
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 6c01f9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 6c01f9
   Lesser General Public License for more details.
Packit Service 6c01f9
Packit Service 6c01f9
   You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
   License along with this program; if not, see <https://www.gnu.org/licenses/>.
Packit Service 6c01f9
*/
Packit Service 672cf4
Packit Service 672cf4
/* We need to include config.h so that we know whether we are building
Packit Service 672cf4
   with large file system (LFS) support. */
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
#include <config.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include <stdlib.h>
Packit Service 672cf4
#include <stdio.h>
Packit Service 672cf4
#include <string.h>
Packit Service 672cf4
Packit Service 672cf4
#include <gpgme.h>
Packit Service 672cf4
Packit Service 672cf4
#define PGM "run-import"
Packit Service 672cf4
Packit Service 672cf4
#include "run-support.h"
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static int verbose;
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static int
Packit Service 672cf4
show_usage (int ex)
Packit Service 672cf4
{
Packit Service 672cf4
  fputs ("usage: " PGM " [options] FILENAMEs\n\n"
Packit Service 672cf4
         "Options:\n"
Packit Service 672cf4
         "  --verbose        run in verbose mode\n"
Packit Service 672cf4
         "  --url            import from given URLs\n"
Packit Service 672cf4
         "  -0               URLs are delimited by a nul\n"
Packit Service 672cf4
         , stderr);
Packit Service 672cf4
  exit (ex);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
int
Packit Service 672cf4
main (int argc, char **argv)
Packit Service 672cf4
{
Packit Service 672cf4
  int last_argc = -1;
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  gpgme_ctx_t ctx;
Packit Service 672cf4
  int url_mode = 0;
Packit Service 672cf4
  int nul_mode = 0;
Packit Service 672cf4
  gpgme_import_result_t impres;
Packit Service 672cf4
  gpgme_data_t data;
Packit Service 672cf4
Packit Service 672cf4
  if (argc)
Packit Service 672cf4
    { argc--; argv++; }
Packit Service 672cf4
  while (argc && last_argc != argc )
Packit Service 672cf4
    {
Packit Service 672cf4
      last_argc = argc;
Packit Service 672cf4
      if (!strcmp (*argv, "--"))
Packit Service 672cf4
        {
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
          break;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--help"))
Packit Service 672cf4
        show_usage (0);
Packit Service 672cf4
      else if (!strcmp (*argv, "--verbose"))
Packit Service 672cf4
        {
Packit Service 672cf4
          verbose = 1;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--url"))
Packit Service 672cf4
        {
Packit Service 672cf4
          url_mode = 1;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "-0"))
Packit Service 672cf4
        {
Packit Service 672cf4
          nul_mode = 1;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strncmp (*argv, "--", 2))
Packit Service 672cf4
        show_usage (1);
Packit Service 672cf4
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  if (!argc)
Packit Service 672cf4
    show_usage (1);
Packit Service 672cf4
Packit Service 6c01f9
  init_gpgme (GPGME_PROTOCOL_OpenPGP);
Packit Service 672cf4
Packit Service 672cf4
  err = gpgme_new (&ctx;;
Packit Service 672cf4
  fail_if_err (err);
Packit Service 6c01f9
  gpgme_set_protocol (ctx, GPGME_PROTOCOL_OpenPGP);
Packit Service 672cf4
Packit Service 672cf4
  for (; argc; argc--, argv++)
Packit Service 672cf4
    {
Packit Service 672cf4
      printf ("reading file `%s'\n", *argv);
Packit Service 672cf4
      err = gpgme_data_new_from_file (&data, *argv, 1);
Packit Service 672cf4
      fail_if_err (err);
Packit Service 672cf4
Packit Service 672cf4
      if (url_mode)
Packit Service 672cf4
        gpgme_data_set_encoding (data, (nul_mode? GPGME_DATA_ENCODING_URL0
Packit Service 672cf4
                                        : GPGME_DATA_ENCODING_URL));
Packit Service 672cf4
Packit Service 672cf4
      err = gpgme_op_import (ctx, data);
Packit Service 672cf4
      fail_if_err (err);
Packit Service 672cf4
      impres = gpgme_op_import_result (ctx);
Packit Service 672cf4
      if (!impres)
Packit Service 672cf4
        {
Packit Service 672cf4
          fprintf (stderr, PGM ": no import result returned\n");
Packit Service 672cf4
          exit (1);
Packit Service 672cf4
        }
Packit Service 672cf4
      print_import_result (impres);
Packit Service 672cf4
Packit Service 672cf4
      gpgme_data_release (data);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  gpgme_release (ctx);
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}