Commit 456bb88c authored by Stefan Walter's avatar Stefan Walter

Use GOption for parsing command line arguments. Fixes bug #336146

    * agent/seahorse-agent-main.c:
    * daemon/seahorse-daemon.c:
    * plugins/nautilus/seahorse-pgp-preferences.c:
    * plugins/nautilus/seahorse-tool.c: Use GOption for parsing command
    line arguments. Fixes bug #336146
parent 5fa62aad
2006-12-21 Nate Nielsen <nielsen@memberwebs.com>
* agent/seahorse-agent-main.c:
* daemon/seahorse-daemon.c:
* plugins/nautilus/seahorse-pgp-preferences.c:
* plugins/nautilus/seahorse-tool.c: Use GOption for parsing command
line arguments. Fixes bug #336146
2006-12-21 Nate Nielsen <nielsen@memberwebs.com>
* agent/seahorse-agent-cache.c:
......
......@@ -40,33 +40,35 @@
#include "seahorse-unix-signal.h"
#include "seahorse-secure-memory.h"
static gboolean agent_daemonize = TRUE;
static gboolean agent_no_daemonize = FALSE;
static gboolean agent_running = FALSE;
static gboolean agent_quit = FALSE;
static gchar **agent_exec_args = NULL;
static const struct poptOption options[] = {
{ "no-daemonize", 'd', POPT_ARG_NONE | POPT_ARG_VAL, &agent_daemonize, FALSE,
N_("Do not daemonize seahorse-agent"), NULL },
static const GOptionEntry options[] = {
{ "no-daemonize", 'd', 0, G_OPTION_ARG_NONE, &agent_no_daemonize,
N_("Do not daemonize seahorse-agent"), NULL },
{ "cshell", 'c', POPT_ARG_NONE | POPT_ARG_VAL, &seahorse_agent_cshell, TRUE,
N_("Print variables in for a C type shell"), NULL },
{ "cshell", 'c', 0, G_OPTION_ARG_NONE, &seahorse_agent_cshell,
N_("Print variables in for a C type shell"), NULL },
{ "variables", 'v', POPT_ARG_NONE | POPT_ARG_VAL, &seahorse_agent_displayvars, TRUE,
N_("Display variables instead of editing conf files (gpg.conf, ssh agent socket)"), NULL },
{ "variables", 'v', 0, G_OPTION_ARG_NONE, &seahorse_agent_displayvars,
N_("Display variables instead of editing conf files (gpg.conf, ssh agent socket)"), NULL },
{ "execute", 'x', POPT_ARG_NONE | POPT_ARG_VAL, &seahorse_agent_execvars, TRUE,
N_("Execute other arguments on the command line"), NULL },
{ "execute", 'x', 0, G_OPTION_ARG_NONE, &seahorse_agent_execvars,
N_("Execute other arguments on the command line"), NULL },
{ "any-display", 'A', POPT_ARG_NONE | POPT_ARG_VAL, &seahorse_agent_any_display, TRUE,
N_("Allow GPG agent request from any display"), NULL },
{ "any-display", 'A', 0, G_OPTION_ARG_NONE, &seahorse_agent_any_display,
N_("Allow GPG agent request from any display"), NULL },
POPT_AUTOHELP
POPT_TABLEEND
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &agent_exec_args,
NULL, N_("command ...") },
{ NULL }
};
static void
daemonize (const gchar **exec)
daemonize (gchar **exec)
{
/*
* We can't use the normal daemon call, because we have
......@@ -76,7 +78,10 @@ daemonize (const gchar **exec)
pid_t pid;
int i;
if (agent_daemonize) {
if (agent_no_daemonize) {
pid = getpid ();
} else {
switch ((pid = fork ())) {
case -1:
err (1, _("couldn't fork process"));
......@@ -101,18 +106,19 @@ daemonize (const gchar **exec)
};
}
/* Not daemonizing */
else {
pid = getpid ();
}
/* The parent process or not daemonizing ... */
/* Let the agent do it's thing */
seahorse_agent_postfork (pid);
seahorse_agent_ssh_postfork (pid);
if (agent_daemonize) {
if (agent_no_daemonize) {
/* We can't overlay our process with the exec one if not daemonizing */
if (exec && exec[0])
g_warning ("cannot execute process when not daemonizing: %s", exec[0]);
} else {
/* If we were asked to exec another program, do that here */
if (!exec || !exec[0])
......@@ -122,12 +128,6 @@ daemonize (const gchar **exec)
g_critical ("couldn't exec %s: %s\n", exec[0], strerror (errno));
exit (1);
} else {
/* We can't overlay our process with the exec one if not daemonizing */
if (exec && exec[0])
g_warning ("cannot execute process when not daemonizing: %s", exec[0]);
}
}
......@@ -205,11 +205,8 @@ client_die ()
int main(int argc, char* argv[])
{
SeahorseOperation *op;
GnomeProgram *program = NULL;
GnomeClient *client = NULL;
const char **args = NULL;
poptContext pctx;
GValue value = { 0, };
GOptionContext *octx = NULL;
seahorse_secure_memory_init (65536);
......@@ -226,38 +223,37 @@ int main(int argc, char* argv[])
if (setuid (getuid ()) == -1 || setgid (getgid ()) == -1)
#endif
err (1, _("couldn't drop privileges properly"));
octx = g_option_context_new ("");
g_option_context_add_main_entries (octx, options, GETTEXT_PACKAGE);
program = gnome_program_init("seahorse-agent", VERSION, LIBGNOMEUI_MODULE, argc, argv,
GNOME_PARAM_POPT_TABLE, options,
gnome_program_init("seahorse-agent", VERSION, LIBGNOMEUI_MODULE, argc, argv,
GNOME_PARAM_GOPTION_CONTEXT, octx,
GNOME_PARAM_HUMAN_READABLE_NAME, _("Encryption Key Agent (Seahorse)"),
GNOME_PARAM_APP_DATADIR, DATA_DIR, NULL);
seahorse_agent_prefork ();
seahorse_agent_ssh_prefork ();
/* If we need to run another program, then prepare that */
if (seahorse_agent_execvars) {
g_value_init (&value, G_TYPE_POINTER);
g_object_get_property (G_OBJECT (program), GNOME_PARAM_POPT_CONTEXT, &value);
pctx = g_value_get_pointer (&value);
g_value_unset (&value);
args = poptGetArgs(pctx);
}
if (seahorse_agent_execvars &&
(!agent_exec_args || !agent_exec_args[0]))
errx (2, _("no command specified to execute"));
/*
* All functions after this point have to print messages
* nicely and not just called exit()
*/
daemonize (args);
daemonize (seahorse_agent_execvars ? agent_exec_args : NULL);
g_strfreev (agent_exec_args);
agent_exec_args = NULL;
/* Handle some signals */
seahorse_unix_signal_register (SIGINT, unix_signal);
seahorse_unix_signal_register (SIGTERM, unix_signal);
/* Force gconf to reconnect after daemonizing */
if (agent_daemonize)
if (!agent_no_daemonize)
seahorse_gconf_disconnect ();
client = gnome_master_client();
......
......@@ -40,7 +40,7 @@
#include "seahorse-context.h"
#include "seahorse-unix-signal.h"
static gboolean daemon_daemonize = TRUE;
static gboolean daemon_no_daemonize = FALSE;
static gboolean daemon_running = FALSE;
static gboolean daemon_quit = FALSE;
......@@ -49,18 +49,14 @@ static const gchar *daemon_icons[] = {
NULL
};
static const struct poptOption options[] = {
{ "no-daemonize", 'd', POPT_ARG_NONE | POPT_ARG_VAL, &daemon_daemonize, FALSE,
N_("Do not daemonize seahorse-daemon"), NULL },
POPT_AUTOHELP
POPT_TABLEEND
static const GOptionEntry options[] = {
{ "no-daemonize", 'd', 0, G_OPTION_ARG_NONE, &daemon_no_daemonize,
N_("Do not daemonize seahorse-daemon"), NULL },
{ NULL }
};
static void
daemonize (const gchar **exec)
daemonize ()
{
/*
* We can't use the normal daemon call, because we have
......@@ -70,7 +66,7 @@ daemonize (const gchar **exec)
pid_t pid;
int i;
if (daemon_daemonize) {
if (!daemon_no_daemonize) {
switch ((pid = fork ())) {
case -1:
err (1, _("couldn't fork process"));
......@@ -101,24 +97,8 @@ daemonize (const gchar **exec)
}
/* The parent process or not daemonizing ... */
if (daemon_daemonize) {
/* If we were asked to exec another program, do that here */
if (!exec || !exec[0])
exit (0);
execvp (exec[0], (char**)exec);
g_critical ("couldn't exec %s: %s\n", exec[0], strerror (errno));
exit (1);
} else {
/* We can't overlay our process with the exec one if not daemonizing */
if (exec && exec[0])
g_warning ("cannot execute process when not daemonizing: %s", exec[0]);
}
if (!daemon_no_daemonize)
exit (0);
}
static void
......@@ -195,9 +175,8 @@ client_die ()
int main(int argc, char* argv[])
{
SeahorseOperation *op;
GnomeProgram *program = NULL;
GnomeClient *client = NULL;
const char **args = NULL;
GOptionContext *octx = NULL;
seahorse_secure_memory_init (65536);
......@@ -215,8 +194,11 @@ int main(int argc, char* argv[])
#endif
err (1, _("couldn't drop privileges properly"));
program = gnome_program_init("seahorse-daemon", VERSION, LIBGNOMEUI_MODULE, argc, argv,
GNOME_PARAM_POPT_TABLE, options,
octx = g_option_context_new ("");
g_option_context_add_main_entries (octx, options, GETTEXT_PACKAGE);
gnome_program_init ("seahorse-daemon", VERSION, LIBGNOMEUI_MODULE, argc, argv,
GNOME_PARAM_GOPTION_CONTEXT, octx,
GNOME_PARAM_HUMAN_READABLE_NAME, _("Encryption Daemon (Seahorse)"),
GNOME_PARAM_APP_DATADIR, DATA_DIR, NULL);
......@@ -224,14 +206,14 @@ int main(int argc, char* argv[])
* All functions after this point have to print messages
* nicely and not just called exit()
*/
daemonize (args);
daemonize ();
/* Handle some signals */
seahorse_unix_signal_register (SIGINT, unix_signal);
seahorse_unix_signal_register (SIGTERM, unix_signal);
/* Force gconf to reconnect after daemonizing */
if (daemon_daemonize)
if (!daemon_no_daemonize)
seahorse_gconf_disconnect ();
client = gnome_master_client();
......
......@@ -38,22 +38,20 @@ static gboolean show_sharing = FALSE;
static gboolean show_applet = FALSE;
#endif
static const struct poptOption options[] = {
static const GOptionEntry options[] = {
#ifdef WITH_AGENT
{ "cache", 'c', POPT_ARG_NONE | POPT_ARG_VAL, &show_cache, TRUE,
{ "cache", 'c', 0, G_OPTION_ARG_NONE, &show_cache,
N_("For internal use"), NULL },
#endif
#ifdef WITH_SHARING
{ "sharing", 's', POPT_ARG_NONE | POPT_ARG_VAL, &show_sharing, TRUE,
{ "sharing", 's', 0, G_OPTION_ARG_NONE, &show_sharing,
N_("For internal use"), NULL },
#endif
#ifdef WITH_APPLET
{ "applet", 's', POPT_ARG_NONE | POPT_ARG_VAL, &show_applet, TRUE,
{ "applet", 's', 0, G_OPTION_ARG_NONE, &show_applet,
N_("For internal use"), NULL },
#endif
POPT_AUTOHELP
POPT_TABLEEND
{ NULL }
};
static void
......@@ -65,11 +63,15 @@ destroyed (GtkObject *object, gpointer data)
int
main (int argc, char **argv)
{
SeahorseWidget *swidget;
SeahorseWidget *swidget;
SeahorseOperation *op;
GOptionContext *octx = NULL;
octx = g_option_context_new ("");
g_option_context_add_main_entries (octx, options, GETTEXT_PACKAGE);
gnome_program_init ("seahorse-preferences", VERSION, LIBGNOMEUI_MODULE, argc, argv,
GNOME_PARAM_POPT_TABLE, options,
GNOME_PARAM_GOPTION_CONTEXT, octx,
GNOME_PARAM_HUMAN_READABLE_NAME, _("Encryption Preferences"),
GNOME_PARAM_APP_DATADIR, DATA_DIR, NULL);
......
......@@ -38,42 +38,38 @@
* ARGUMENT PARSING
*/
typedef enum _CmdLineMode {
MODE_NONE,
MODE_IMPORT,
MODE_ENCRYPT,
MODE_SIGN,
MODE_ENCRYPT_SIGN,
MODE_DECRYPT,
MODE_VERIFY
} CmdLineMode;
static gchar **arg_uris = NULL;
static gboolean read_uris = FALSE;
static CmdLineMode cmd_mode = MODE_NONE;
static const struct poptOption options[] = {
{ "import", 'i', POPT_ARG_NONE | POPT_ARG_VAL, &cmd_mode, MODE_IMPORT,
N_("Import keys from the file"), NULL },
{ "encrypt", 'e', POPT_ARG_NONE | POPT_ARG_VAL, &cmd_mode, MODE_ENCRYPT,
N_("Encrypt file"), NULL },
{ "sign", 's', POPT_ARG_NONE | POPT_ARG_VAL, &cmd_mode, MODE_SIGN,
N_("Sign file with default key"), NULL },
{ "encrypt-sign", 'n', POPT_ARG_NONE | POPT_ARG_VAL, &cmd_mode, MODE_ENCRYPT_SIGN,
N_("Encrypt and sign file with default key"), NULL },
{ "decrypt", 'd', POPT_ARG_NONE | POPT_ARG_VAL, &cmd_mode, MODE_DECRYPT,
N_("Decrypt encrypted file"), NULL },
{ "verify", 'v', POPT_ARG_NONE | POPT_ARG_VAL, &cmd_mode, MODE_VERIFY,
N_("Verify signature file"), NULL },
{ "uri-list", 'T', POPT_ARG_NONE | POPT_ARG_VAL, &read_uris, TRUE,
N_("Read list of URIs on standard in"), NULL },
POPT_AUTOHELP
POPT_TABLEEND
static gboolean mode_import = FALSE;
static gboolean mode_encrypt = FALSE;
static gboolean mode_sign = FALSE;
static gboolean mode_encrypt_sign = FALSE;
static gboolean mode_decrypt = FALSE;
static gboolean mode_verify = FALSE;
static const GOptionEntry options[] = {
{ "import", 'i', 0, G_OPTION_ARG_NONE, &mode_import,
N_("Import keys from the file"), NULL },
{ "encrypt", 'e', 0, G_OPTION_ARG_NONE, &mode_encrypt,
N_("Encrypt file"), NULL },
{ "sign", 's', 0, G_OPTION_ARG_NONE, &mode_sign,
N_("Sign file with default key"), NULL },
{ "encrypt-sign", 'n', 0, G_OPTION_ARG_NONE, &mode_encrypt_sign,
N_("Encrypt and sign file with default key"), NULL },
{ "decrypt", 'd', 0, G_OPTION_ARG_NONE, &mode_decrypt,
N_("Decrypt encrypted file"), NULL },
{ "verify", 'v', 0, G_OPTION_ARG_NONE, &mode_verify,
N_("Verify signature file"), NULL },
{ "uri-list", 'T', 0, G_OPTION_ARG_NONE, &read_uris,
N_("Read list of URIs on standard in"), NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &arg_uris,
NULL, N_("file ...") },
{ NULL }
};
/* Returns a null terminated array of uris, use g_strfreev to free */
static gchar**
read_uri_arguments (poptContext pctx)
read_uri_arguments ()
{
/* Read uris from stdin */
if (read_uris) {
......@@ -104,7 +100,10 @@ read_uri_arguments (poptContext pctx)
/* Multiple arguments on command line */
} else {
return seahorse_util_strvec_dup (poptGetArgs(pctx));
gchar **args = arg_uris;
arg_uris = NULL;
return args;
}
}
......@@ -545,14 +544,11 @@ check_dialogs (gpointer dummy)
int
main (int argc, char **argv)
{
GOptionContext *octx = NULL;
SeahorseToolMode mode;
GnomeProgram *program = NULL;
SeahorseContext *sctx;
gchar **uris = NULL;
int ret = 0;
poptContext pctx;
GValue value = { 0, };
seahorse_secure_memory_init (65536);
......@@ -582,31 +578,21 @@ main (int argc, char **argv)
* operations, the other shows the progress window, handles cancel.
*/
seahorse_tool_progress_init (argc, argv);
/* Main operation process */
program = gnome_program_init("seahorse-tool", VERSION, LIBGNOMEUI_MODULE, argc, argv,
GNOME_PARAM_POPT_TABLE, options,
GNOME_PARAM_HUMAN_READABLE_NAME, _("File Encryption Tool"),
GNOME_PARAM_APP_DATADIR, DATA_DIR, NULL);
g_value_init (&value, G_TYPE_POINTER);
g_object_get_property (G_OBJECT (program), GNOME_PARAM_POPT_CONTEXT, &value);
pctx = g_value_get_pointer (&value);
g_value_unset (&value);
octx = g_option_context_new ("");
g_option_context_add_main_entries (octx, options, GETTEXT_PACKAGE);
if (cmd_mode == MODE_NONE) {
fprintf (stderr, "seahorse-tool: must specify an operation");
poptPrintHelp (pctx, stdout, 0);
return 2;
}
/* Main operation process */
gnome_program_init("seahorse-tool", VERSION, LIBGNOMEUI_MODULE, argc, argv,
GNOME_PARAM_GOPTION_CONTEXT, octx,
GNOME_PARAM_HUMAN_READABLE_NAME, _("File Encryption Tool"),
GNOME_PARAM_APP_DATADIR, DATA_DIR, NULL);
/* Load up all our arguments */
uris = read_uri_arguments(pctx);
uris = read_uri_arguments ();
if(!uris || !uris[0]) {
fprintf (stderr, "seahorse-tool: must specify files");
poptPrintHelp (pctx, stdout, 0);
return 2;
}
......@@ -619,9 +605,7 @@ main (int argc, char **argv)
/* The basic settings for the operation */
memset (&mode, 0, sizeof (mode));
switch (cmd_mode) {
case MODE_ENCRYPT_SIGN:
case MODE_ENCRYPT:
if (mode_encrypt_sign || mode_encrypt) {
mode.recipients = prompt_recipients (&mode.signer);
if (mode.recipients) {
mode.title = _("Encrypting");
......@@ -629,38 +613,38 @@ main (int argc, char **argv)
mode.startcb = encrypt_sign_start;
mode.package = TRUE;
}
break;
case MODE_SIGN:
} else if (mode_sign) {
mode.signer = prompt_signer ();
if (mode.signer) {
mode.title = _("Signing");
mode.errmsg = _("Couldn't sign file: %s");
mode.startcb = sign_start;
}
break;
case MODE_IMPORT:
} else if (mode_import) {
mode.title = _("Importing");
mode.errmsg = _("Couldn't import keys from file: %s");
mode.startcb = import_start;
mode.donecb = import_done;
mode.imports = 0;
break;
case MODE_DECRYPT:
} else if (mode_decrypt) {
mode.title = _("Decrypting");
mode.errmsg = _("Couldn't decrypt file: %s");
mode.startcb = decrypt_start;
mode.donecb = decrypt_done;
break;
case MODE_VERIFY:
} else if (mode_verify) {
mode.title = _("Verifying");
mode.errmsg = _("Couldn't verify file: %s");
mode.startcb = verify_start;
mode.donecb = verify_done;
break;
default:
g_assert_not_reached ();
break;
};
} else {
fprintf (stderr, "seahorse-tool: must specify an operation");
return 2;
}
/* Must at least have a start cb to do something */
if (mode.startcb) {
......@@ -669,15 +653,8 @@ main (int argc, char **argv)
/* Any results necessary */
if (ret == 0) {
switch (cmd_mode) {
case MODE_IMPORT:
if (mode_import)
import_show (&mode);
break;
default:
break;
};
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment