+/**
+ * Test if we are finished yet.
+ */
+static void
+test_finished ()
+{
+ if ( (NULL == create_op) &&
+ (NULL == delete_op) &&
+ (NULL == set_op) &&
+ (NULL == set_ego) &&
+ (! list) &&
+ (! monitor) )
+ {
+ if (TIMEOUT_STATUS_CODE == global_ret)
+ global_ret = 0;
+ GNUNET_SCHEDULER_shutdown ();
+ }
+}
+
+
+/**
+ * Deletion operation finished.
+ *
+ * @param cls pointer to operation handle
+ * @param emsg NULL on success, otherwise an error message
+ */
+static void
+delete_finished (void *cls,
+ const char *emsg)
+{
+ struct GNUNET_IDENTITY_Operation **op = cls;
+
+ *op = NULL;
+ if (NULL != emsg)
+ fprintf (stderr,
+ "%s\n",
+ gettext (emsg));
+ test_finished ();
+}
+
+
+/**
+ * Creation operation finished.
+ *
+ * @param cls pointer to operation handle
+ * @param emsg error message, NULL on success
+ */
+static void
+create_finished (void *cls,
+ const char *emsg)
+{
+ struct GNUNET_IDENTITY_Operation **op = cls;
+
+ *op = NULL;
+ if (NULL != emsg)
+ {
+ fprintf (stderr,
+ _("Failed to create ego: %s\n"),
+ emsg);
+ global_ret = 1;
+ }
+ test_finished ();
+}
+
+
+/**
+ * Function called by #GNUNET_IDENTITY_set up on completion.
+ *
+ * @param cls NULL
+ * @param emsg error message (NULL on success)
+ */
+static void
+set_done (void *cls,
+ const char *emsg)
+{
+ set_op = NULL;
+ if (NULL != emsg)
+ {
+ fprintf (stderr,
+ _("Failed to set default ego: %s\n"),
+ emsg);
+ global_ret = 1;
+ }
+ test_finished ();
+}
+
+
+/**
+ * If listing is enabled, prints information about the egos.
+ *
+ * This function is initially called for all egos and then again
+ * whenever a ego's identifier changes or if it is deleted. At the
+ * end of the initial pass over all egos, the function is once called
+ * with 'NULL' for 'ego'. That does NOT mean that the callback won't
+ * be invoked in the future or that there was an error.
+ *
+ * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
+ * this function is only called ONCE, and 'NULL' being passed in
+ * 'ego' does indicate an error (i.e. name is taken or no default
+ * value is known). If 'ego' is non-NULL and if '*ctx'
+ * is set in those callbacks, the value WILL be passed to a subsequent
+ * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
+ * that one was not NULL).
+ *
+ * When an identity is renamed, this function is called with the
+ * (known) ego but the NEW identifier.
+ *
+ * When an identity is deleted, this function is called with the
+ * (known) ego and "NULL" for the 'identifier'. In this case,
+ * the 'ego' is henceforth invalid (and the 'ctx' should also be
+ * cleaned up).
+ *
+ * @param cls closure
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ * (during the lifetime of this process, initially NULL)
+ * @param identifier identifier assigned by the user for this ego,
+ * NULL if the user just deleted the ego and it
+ * must thus no longer be used
+*/
+static void
+print_ego (void *cls,
+ struct GNUNET_IDENTITY_Ego *ego,
+ void **ctx,
+ const char *identifier)
+{
+ struct GNUNET_CRYPTO_EcdsaPublicKey pk;
+ char *s;
+
+ if ( (NULL != set_ego) &&
+ (NULL != ego) &&
+ (NULL != identifier) &&
+ (0 == strcmp (identifier,
+ set_ego)) )
+ {
+ set_op = GNUNET_IDENTITY_set (sh,
+ set_subsystem,
+ ego,
+ &set_done,
+ NULL);
+ GNUNET_free (set_subsystem);
+ set_subsystem = NULL;
+ GNUNET_free (set_ego);
+ set_ego = NULL;
+ }
+ if ( (NULL == ego) &&
+ (NULL != set_ego) )
+ {
+ fprintf (stderr,
+ "Could not set ego to `%s' for subsystem `%s', ego not known\n",
+ set_ego,
+ set_subsystem);
+ GNUNET_free (set_subsystem);
+ set_subsystem = NULL;
+ GNUNET_free (set_ego);
+ set_ego = NULL;
+ }
+ if ( (NULL == ego) && (! monitor) )
+ {
+ list = 0;
+ test_finished ();
+ return;
+ }
+ if (! (list | monitor))
+ return;
+ if (NULL == ego)
+ return;
+ GNUNET_IDENTITY_ego_get_public_key (ego,
+ &pk);
+ s = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
+ if ( (monitor) ||
+ (NULL != identifier) )
+ fprintf (stdout,
+ "%s - %s\n",
+ identifier,
+ s);
+ GNUNET_free (s);
+}
+
+