Create libmapper subtree function and call it in event loop

Call a new mapper subtree interface in an event loop.
Create an enum to specify the mapper action, in this case
wait for an interface to be removed. This allows to later
add other operations like wait for an interface to be added
since the logic would be different.

Change-Id: I8330852d47185c3c4e40e994c6e4719054a9fc06
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/libmapper/app.c b/libmapper/app.c
index 501a191..3bdd0c6 100644
--- a/libmapper/app.c
+++ b/libmapper/app.c
@@ -140,10 +140,14 @@
 static int subtree_main(int argc, char *argv[])
 {
 	int r = 0;
+	int op = 0;
 	static const char* token = ":";
 	char* tmp = NULL;
 	char* namespace = NULL;
 	char* interface = NULL;
+	sd_bus *conn = NULL;
+	sd_event *loop = NULL;
+	mapper_async_subtree *subtree = NULL;
 
 	if (argc != 3) {
 		fprintf(stderr, "Usage: %s subtree-remove "
@@ -151,6 +155,8 @@
 		exit(EXIT_FAILURE);
 	}
 
+	op = MAPPER_OP_REMOVE;
+
 	namespace = strtok_r(argv[2], token, &tmp);
 	interface = strtok_r(NULL, token, &tmp);
 	if ((namespace == NULL) || (interface == NULL)) {
@@ -158,6 +164,43 @@
 		exit(EXIT_FAILURE);
 	}
 
+	r = sd_bus_default_system(&conn);
+	if(r < 0) {
+		fprintf(stderr, "Error connecting to system bus: %s\n",
+				strerror(-r));
+		goto finish;
+	}
+
+	r = sd_event_default(&loop);
+	if (r < 0) {
+		fprintf(stderr, "Error obtaining event loop: %s\n",
+				strerror(-r));
+		goto finish;
+	}
+
+	r = sd_bus_attach_event(conn, loop, SD_EVENT_PRIORITY_NORMAL);
+	if (r < 0) {
+		fprintf(stderr, "Failed to attach system bus to event loop: %s\n",
+				strerror(-r));
+		goto finish;
+	}
+
+	r = mapper_subtree_async(conn, loop, namespace, interface, quit, loop,
+				&subtree, op);
+	if(r < 0) {
+		fprintf(stderr, "Error configuring subtree list: %s\n",
+				strerror(-r));
+		goto finish;
+	}
+
+	r = sd_event_loop(loop);
+	if(r < 0) {
+		fprintf(stderr, "Error starting event loop: %s\n",
+				strerror(-r));
+		goto finish;
+	}
+
+finish:
 	exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
 }