summaryrefslogtreecommitdiff
path: root/tool/gkr-tool.c
blob: 663fc65db93f0f5e1d76b59279868dcac65e7c0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

#include "config.h"

#include "gkr-tool.h"

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>

#include <locale.h>
#include <string.h>

/* -----------------------------------------------------------------------------
 * GENERAL HELPERS
 */

gboolean gkr_tool_mode_quiet = FALSE;

int
gkr_tool_parse_options (int *argc, char** argv[], GOptionEntry *options)
{
	GError *err = NULL;
	GOptionContext *context;
	int ret = 0;
	
	context = g_option_context_new ("- Gnome Keyring Tool");
	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
	
	if (!g_option_context_parse (context, argc, argv, &err)) {
		gkr_tool_handle_error (&err, NULL);
		ret = 2;
	}
	
	g_option_context_free (context);
	return ret;
}

void gkr_tool_handle_error (GError **error, const gchar *format, ...)
{
	gchar *message = NULL;
	GError *err = error ? *error : NULL;
	va_list va;
	
	if (format) {
		va_start(va, format);
		message = g_strdup_vprintf(format, va);
		va_end(va);
	}
	
	g_printerr ("gnome-keyring: %s%s%s",
	            message ? message : "",
	            message && err && err->message ? ": " : "",
	            err && err->message ? err->message : "");
	
	g_free (message);
	g_clear_error (error);
}

/* -----------------------------------------------------------------------------
 * COMMAND LINE
 */

typedef struct _CommandInfo {
	const char *name;
	int (*handler) (int argc, char *argv[]);
} CommandInfo;

static CommandInfo command_info[] = {
	{ "import", gkr_tool_import },
	{ NULL, NULL }
};

static void
print_general_usage (void)
{
	CommandInfo *cmd;
	const gchar *prefix;
	
	g_printerr ("usage: gnome-keyring command [options]\n");
	
	prefix = "commands: ";
	for (cmd = command_info; cmd->name; ++cmd) {
		g_printerr ("%s%s\n", prefix, cmd->name);
		prefix = "          ";
	}
}

int
main (int argc, char *argv[])
{
	CommandInfo *cmd;
	int ret = -1;
	
	g_type_init ();
	g_thread_init (NULL);
	
#ifdef HAVE_LOCALE_H
	/* internationalisation */
	setlocale (LC_ALL, "");
#endif

#ifdef HAVE_GETTEXT
	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	textdomain (GETTEXT_PACKAGE);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif

	/* The first argument is the command */
	if (argc < 2) {
		print_general_usage ();
		return 2;
	}
	
	/* Find the command to run */
	for (cmd = command_info; cmd->name; ++cmd) {
		g_return_val_if_fail (argv[1], 1);
		if (strcmp (cmd->name, argv[1]) == 0) {
			/* Remove the command and replace with executable command */
			argv[1] = argv[0];
			ret = (cmd->handler) (argc - 1, argv + 1);
			break;
		}
	}
	
	if (ret == -1) {
		print_general_usage ();
		ret = 2;
	}
	
	return ret;
}