shithub: neindaw

Download patch

ref: 8291e90f0700652c6f3a3dce408244a67600ad46
parent: fd3f0be9dc1524bf79cb6f7cb6938aec7e934ffd
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun May 10 20:33:10 EDT 2020

piper: instance allocation

--- a/piper/piper.c
+++ b/piper/piper.c
@@ -7,9 +7,11 @@
 typedef struct Group Group;
 
 struct Group {
+	char *path;
 	Synth *synth;
 	void **aux;
 	int numaux;
+	int clone;
 };
 
 static Group *groups;
@@ -27,13 +29,49 @@
 	threadexitsall("usage");
 }
 
+static char *
+parse(char *s, Group *g)
+{
+	Cmd c;
+	char *e, *path, tmp[8];
+	void *aux;
+	int i, n;
+
+	for (i = 1; s[i] != 0 && s[i] != ';'; i++);
+	e = s[i] == 0 ? nil : s + i + 1;
+	s[i] = 0;
+
+	if (g != nil) {
+		s++;
+		i = i36(*s);
+		while (i >= g->numaux) { /* instance needs to be created */
+			seek(g->clone, 0, 0);
+			if ((n = read(g->clone, tmp, sizeof(tmp))) < 1)
+				sysfatal("clone failed");
+			tmp[n] = 0;
+			if ((path = smprint("%s/%s/%s", g->path, tmp, g->synth->name)) == nil)
+				sysfatal("memory");
+			if ((aux = g->synth->alloc(path)) == nil)
+				return e;
+			if ((g->aux = realloc(g->aux, sizeof(void*)*(g->numaux+1))) == nil)
+				sysfatal("memory");
+			g->aux[g->numaux++] = aux;
+		}
+		aux = g->aux[i];
+		memset(&c, 0, sizeof(c));
+		g->synth->cmd(aux, &c);
+	}
+
+	return e;
+}
+
 void
 threadmain(int argc, char **argv)
 {
-	char *s;
+	char *s, t[256];
 	Synth *synth;
 	Biobuf *b;
-	int i, j;
+	int i, j, n;
 
 	ARGBEGIN{
 	default:
@@ -63,7 +101,13 @@
 					sysfatal("memory");
 				synth = synths[j];
 				memset(&groups[numgroups], 0, sizeof(Group));
-				groups[numgroups].synth = synth;
+				if ((s = smprint("%s/clone", argv[i])) == nil)
+					sysfatal("memory");
+				if ((groups[numgroups].clone = open(s, OREAD)) < 0)
+					sysfatal("%r");
+				free(s);
+				groups[numgroups].path = argv[i];
+				groups[numgroups++].synth = synth;
 				break;
 			}
 		}
@@ -70,6 +114,16 @@
 		Bterm(b);
 		if (synth == nil)
 			sysfatal("no name set in %s/metadata", argv[i]);
+	}
+
+	for (;;) {
+		if ((n = read(0, t, sizeof(t)-1)) < 1)
+			break;
+		t[n] = 0;
+		for (s = t; s != nil && *s;) {
+			i = i36(s[0]);
+			s = parse(s, i < numgroups ? &groups[i] : nil);
+		}
 	}
 
 	threadexitsall(nil);