[gnome-keyring/gnome-3-0] gcr: Implement tool for playing with the parser
- From: Stefan Walter <stefw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-keyring/gnome-3-0] gcr: Implement tool for playing with the parser
- Date: Thu, 21 Apr 2011 08:09:42 +0000 (UTC)
commit fab2b464b3cc73bc3c2f42ac34fb5a2bf03ece2b
Author: Stef Walter <stefw collabora co uk>
Date: Fri Apr 15 16:31:24 2011 +0200
gcr: Implement tool for playing with the parser
.gitignore | 1 +
gcr/tests/Makefile.am | 1 +
gcr/tests/frob-parser.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 136 insertions(+), 0 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 528a4c8..4c39497 100644
--- a/.gitignore
+++ b/.gitignore
@@ -108,6 +108,7 @@ run-tests
/gcr/tests/frob-certificate
/gcr/tests/frob-key
/gcr/tests/frob-unlock-options
+/gcr/tests/frob-parser
/gcr/tests/test-certificate
/gcr/tests/test-certificate-chain
/gcr/tests/test-parser
diff --git a/gcr/tests/Makefile.am b/gcr/tests/Makefile.am
index 92bf3b5..3a5290d 100644
--- a/gcr/tests/Makefile.am
+++ b/gcr/tests/Makefile.am
@@ -44,5 +44,6 @@ EXTRA_DIST = \
noinst_PROGRAMS = \
frob-certificate \
+ frob-parser \
frob-key \
frob-unlock-options
diff --git a/gcr/tests/frob-parser.c b/gcr/tests/frob-parser.c
new file mode 100644
index 0000000..287e914
--- /dev/null
+++ b/gcr/tests/frob-parser.c
@@ -0,0 +1,134 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2011 Collabora Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Author: Stef Walter <stefw collabora co uk>
+ */
+
+#include "config.h"
+
+#include "gcr/gcr.h"
+
+#include <gtk/gtk.h>
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+static gboolean
+dump_certificate (GckAttributes *attrs, const gchar *filename)
+{
+ GckAttribute *attr;
+ GcrCertificate *cert;
+ gchar *subject;
+ gulong klass;
+
+ if (!gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) ||
+ klass != CKO_CERTIFICATE)
+ return FALSE;
+
+ attr = gck_attributes_find (attrs, CKA_VALUE);
+ if (!attr)
+ return FALSE;
+
+ cert = gcr_simple_certificate_new_static (attr->value, attr->length);
+ subject = gcr_certificate_get_subject_dn (cert);
+ g_print ("%s: parsed certificate: %s\n", filename, subject);
+ g_free (subject);
+ g_object_unref (cert);
+
+ return TRUE;
+}
+
+static void
+on_parser_parsed (GcrParser *parser, gpointer user_data)
+{
+ GckAttributes *attrs;
+ const gchar **pointer_filename = (const gchar**)user_data;
+
+ attrs = gcr_parser_get_parsed_attributes (parser);
+ g_return_if_fail (attrs);
+
+ if (!dump_certificate (attrs, *pointer_filename))
+ g_print ("%s: parsed %s\n", *pointer_filename,
+ gcr_parser_get_parsed_description (parser));
+}
+
+int
+main(int argc, char *argv[])
+{
+ GcrParser *parser;
+ GError *error = NULL;
+ gchar *contents;
+ gsize len;
+ GDir *dir;
+ const gchar *filename;
+ gchar *path;
+
+ gtk_init (&argc, &argv);
+ g_set_prgname ("frob-parser");
+
+ if (argc != 2) {
+ g_printerr ("usage: frob-parser directory\n");
+ return 2;
+ }
+
+ dir = g_dir_open (argv[1], 0, &error);
+ if (!dir) {
+ g_printerr ("couldn't list directory: %s: %s\n", argv[1], error->message);
+ g_clear_error (&error);
+ return 1;
+ }
+
+ parser = gcr_parser_new ();
+ g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), &filename);
+
+ for (;;) {
+ filename = g_dir_read_name (dir);
+ if (!filename)
+ break;
+ if (filename[0] == '.')
+ continue;
+
+ path = g_build_filename (argv[1], filename, NULL);
+ g_file_get_contents (path, &contents, &len, &error);
+ g_free (path);
+
+ if (error) {
+ g_printerr ("%s: couldn't read file: %s\n", filename, error->message);
+ g_clear_error (&error);
+ continue;
+ }
+
+ gcr_parser_parse_data (parser, contents, len, &error);
+ g_free (contents);
+
+ if (error) {
+ g_warning ("%s: couldn't parse: %s\n", filename, error->message);
+ g_clear_error (&error);
+ }
+
+ g_print ("%s: finished parsing\n", filename);
+ }
+
+ g_dir_close (dir);
+ g_object_unref (parser);
+
+ return 0;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]