Browse Source

Add support for command line options.

Implement command line parsing with GLib's parser. Two options
are available for now: -e to encrypt (and/or sign) and -d to
decrypt (and/or verify). Encrypt is the default mode and is the
only one supported for now.
master
Damien Goutte-Gattat 2 years ago
parent
commit
42915a0dfe
  1. 1
      src/errors.h
  2. 48
      src/yki.c

1
src/errors.h

@ -4,3 +4,4 @@ DEFINE_YKI_ERROR(2, GPGME_OPEN, "Cannot open GpgME data buffer")
DEFINE_YKI_ERROR(3, GPGME_CLOSE, "Cannot close GpgME data buffer")
DEFINE_YKI_ERROR(4, GPGME_CRYPTO, "Cannot perform cryptographic operation")
DEFINE_YKI_ERROR(5, GPGME_KEYLIST, "Cannot list keys")
DEFINE_YKI_ERROR(6, INVALID_COMMAND, "Invalid command line")

48
src/yki.c

@ -240,6 +240,24 @@ perform_operation_on_file(yorkie_ctx_t *yki, const char *filename, GError **erro
return ret;
}
static gboolean
check_non_option_args(GOptionContext *parser, GOptionGroup *group, gpointer data, GError **error)
{
char **args = *((char ***)data);
size_t n = 0;
if ( args ) {
while ( *args++ )
n += 1;
}
if ( n != 1 )
g_set_error(error, YKI_ERROR, YKI_ERROR_INVALID_COMMAND, n == 0 ?
"Missing FILE argument" : "Too many arguments");
return n == 1;
}
int
main(int argc, char **argv)
@ -248,12 +266,32 @@ main(int argc, char **argv)
GtkDialog *dlg;
GError *error = NULL;
gboolean encrypt;
gchar **files;
GOptionEntry options[] = {
{ "encrypt", 'e', 0, G_OPTION_ARG_NONE, &encrypt, "Encrypt the file", NULL },
{ "decrupt", 'd', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &encrypt, "Decrypt the file", NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, NULL, "FILE" },
{ NULL }
};
GOptionContext *parser;
GOptionGroup *main_options;
encrypt = TRUE;
files = NULL;
yki.selected_keys = 0;
yki.symmetric = yki.sign = yki.detached = FALSE;
gtk_init(&argc, &argv);
if ( argc != 2 )
return 1;
parser = g_option_context_new("- Perform cryptographic operation on specified file");
main_options = g_option_group_new(NULL, NULL, NULL, (gpointer) &files, NULL);
g_option_group_add_entries(main_options, options);
g_option_group_set_parse_hooks(main_options, NULL, check_non_option_args);
g_option_context_set_main_group(parser, main_options);
g_option_context_add_group(parser, gtk_get_option_group(TRUE));
if ( ! g_option_context_parse(parser, &argc, &argv, &error) ) {
g_printerr("Error: Invalid command line: %s\n", error->message);
exit(EXIT_FAILURE);
}
dlg = create_encrypt_dialog(&yki);
@ -263,7 +301,7 @@ main(int argc, char **argv)
populate_public_key_list(yki.gpgme, yki.public_keys, &error);
if ( ! error && gtk_dialog_run(dlg) == GTK_RESPONSE_OK && can_proceed(yki) )
perform_operation_on_file(&yki, argv[1], &error);
perform_operation_on_file(&yki, files[0], &error);
if ( error ) {
g_printerr("Error: %s: %s\n", yki_error_msg(error->code), error->message);
@ -273,6 +311,8 @@ main(int argc, char **argv)
gtk_tree_model_foreach(GTK_TREE_MODEL(yki.public_keys), free_key_in_list, NULL);
gtk_widget_destroy(GTK_WIDGET(dlg));
gpgme_release(yki.gpgme);
g_option_context_free(parser);
g_strfreev(files);
return error ? EXIT_FAILURE : EXIT_SUCCESS;
}

Loading…
Cancel
Save