Browse Source

Move code out of the main function.

Move the bulk of the code doing the actual job in two separate
functions out of the main function.
master
Damien Goutte-Gattat 2 years ago
parent
commit
c23b91438c
  1. 98
      src/yki.c

98
src/yki.c

@ -21,6 +21,7 @@
#endif
#include <stdio.h>
#include <errno.h>
#include <locale.h>
#include <glib.h>
@ -108,6 +109,42 @@ get_selected_keys(yorkie_ctx_t *yki)
return keys;
}
static gpgme_error_t
perform_operation(yorkie_ctx_t *yki, FILE *in, FILE *out)
{
gpgme_data_t gin, gout;
gpgme_error_t gerr;
if ( (gerr = gpgme_data_new_from_stream(&gin, in)) )
return gerr;
if ( (gerr = gpgme_data_new_from_stream(&gout, out)) ) {
gpgme_data_release(gin);
return gerr;
}
if ( yki->symmetric || yki->selected_keys ) {
gpgme_key_t *keys = get_selected_keys(yki);
gpgme_encrypt_flags_t flags = yki->symmetric ? GPGME_ENCRYPT_SYMMETRIC : 0;
if ( yki->sign )
gerr = gpgme_op_encrypt_sign(yki->gpgme, keys, flags, gin, gout);
else
gerr = gpgme_op_encrypt(yki->gpgme, keys, flags, gin, gout);
g_free(keys);
}
else if ( yki->sign ) {
gpgme_sig_mode_t mode = yki->detached ? GPGME_SIG_MODE_DETACH : GPGME_SIG_MODE_NORMAL;
gerr = gpgme_op_sign(yki->gpgme, gin, gout, mode);
}
gpgme_data_release(gin);
gpgme_data_release(gout);
return gerr;
}
#define can_proceed(yki) ((yki).selected_keys || (yki).symmetric || (yki).sign)
@ -182,6 +219,32 @@ create_encrypt_dialog(yorkie_ctx_t *yki)
return GTK_DIALOG(gtk_builder_get_object(builder, "dlgEncrypt"));
}
static gpgme_error_t
perform_operation_on_file(yorkie_ctx_t *yki, const char *filename)
{
FILE *in, *out;
gchar *outname;
gpgme_error_t gerr;
if ( ! (in = fopen(filename, "r")) )
return gpgme_error_from_errno(errno);
outname = g_strdup_printf("%s.gpg", filename);
out = fopen(outname, "w");
g_free(outname);
if ( ! out ) {
fclose(in);
return gpgme_error_from_errno(errno);
}
gerr = perform_operation(yki, in, out);
fclose(in);
fclose(out);
return gerr;
}
int
main(int argc, char **argv)
@ -203,40 +266,11 @@ main(int argc, char **argv)
g_error("Cannot initialize GPGME: %s", gpgme_strerror(gerr));
if ( (gerr = populate_public_key_list(yki.gpgme, yki.public_keys)) )
g_error("Cannot get public keys; %s", gpgme_strerror(gerr));
g_error("Cannot get public keys: %s", gpgme_strerror(gerr));
if ( gtk_dialog_run(dlg) == GTK_RESPONSE_OK && can_proceed(yki) ) {
FILE *in, *out;
gpgme_data_t gin, gout;
gchar *outname;
if ( ! (in = fopen(argv[1], "r")) || gpgme_data_new_from_stream(&gin, in) )
g_error("cannot open file '%s'", argv[1]);
outname = g_strdup_printf("%s.gpg", argv[1]);
if ( ! (out = fopen(outname, "w")) || gpgme_data_new_from_stream(&gout, out) )
g_error("cannot open file '%s'", outname);
g_free(outname);
if ( yki.symmetric || yki.selected_keys ) {
gpgme_key_t *keys = get_selected_keys(&yki);
if ( yki.sign ) {
gerr = gpgme_op_encrypt_sign(yki.gpgme, keys, yki.symmetric ? GPGME_ENCRYPT_SYMMETRIC : 0, gin, gout);
}
else {
gerr = gpgme_op_encrypt(yki.gpgme, keys, yki.symmetric ? GPGME_ENCRYPT_SYMMETRIC : 0, gin, gout);
}
}
else if ( yki.sign ) {
gerr = gpgme_op_sign(yki.gpgme, gin, gout, yki.detached ? GPGME_SIG_MODE_DETACH : GPGME_SIG_MODE_NORMAL);
}
gpgme_data_release(gin);
gpgme_data_release(gout);
fclose(in);
fclose(out);
if ( (gerr = perform_operation_on_file(&yki, argv[1])) )
g_error("Cannot perform requested operation: %s", gpgme_strerror(gerr));
}
gpgme_release(yki.gpgme);

Loading…
Cancel
Save