shithub: mq

Download patch

ref: 770d1b37880702b4de54fbad44c0726737b17b1a
parent: 21b7055755ea046a61f462adf5d731bfb8505d1e
author: kvik <[email protected]>
date: Sat Sep 12 10:44:00 EDT 2020

mq-cat: redesign the command line interface

Streams to read must be specified explicitly:

	mq-cat mq stream ...

Previously we opened all the streams in a group, which isn't a very
useful behaviour for most situations.  It was also a lot of code to do
something easily done in the shell.

--- a/src/mq-cat.c
+++ b/src/mq-cat.c
@@ -10,61 +10,15 @@
 	int fd;
 };
 
-int nstreams;
-Stream *streams;
-
 char buf[8192];
 
 void
 usage(void)
 {
-	fprint(2, "usage: %s mq\n", argv0);
+	fprint(2, "usage: %s mq stream ...\n", argv0);
 	exits("usage");
 }
 
-int
-eopen(char *s, int m)
-{
-	int fd;
-
-	if((fd = open(s, m)) < 0)
-		sysfatal("open: %r");
-	return fd;
-}
-
-int
-openmq(char *name)
-{
-	int mqfd, n, ismq;
-	Dir *dirs, *d;
-	Stream *s;
-
-	mqfd = eopen(name, OREAD);
-	if((n = dirreadall(mqfd, &dirs)) == -1)
-		sysfatal("dirread: %r");
-	if(n == 0)
-		return -1;
-	close(mqfd);
-
-	ismq = 0;
-	nstreams = n - 2;
-	streams = s = emalloc(nstreams*sizeof(Stream));
-	for(d = dirs; n--; d++){
-		if(strncmp(d->name, "ctl", 3) == 0
-		|| strncmp(d->name, "order", 5) == 0){
-			ismq++;
-			continue;
-		}
-		s->name = estrdup(d->name);
-		s->fd = eopen(d->name, OREAD);
-		s++;
-	}
-	free(dirs);
-	if(ismq != 2)
-		return -1;
-	return eopen("order", OREAD);
-}
-
 long
 rdwr(int fd0, int fd1)
 {
@@ -82,24 +36,32 @@
 void
 main(int argc, char *argv[])
 {
-	int orderfd, n, i;
+	int orderfd, n, ns, i;
 	char name[512+1];
-	Stream *s;
+	Stream *streams, *s;
 
 	ARGBEGIN{
 	default: usage();
 	}ARGEND;
-	if(argc != 1) usage();
+	if(argc < 2) usage();
 
 	if(chdir(argv[0]) == -1)
 		sysfatal("chdir: %r");
-	if((orderfd = openmq(".")) == -1)
-		sysfatal("not mq");
+	argv++, argc--;
+	ns = argc;
+	streams = s = emalloc(ns*sizeof(Stream));
+	for(int i = 0; i < ns; i++, s++){
+		s->name = argv[i];
+		if((s->fd = open(argv[i], OREAD)) == -1)
+			sysfatal("open: %r");
+	}
+	if((orderfd = open("order", OREAD)) == -1)
+		sysfatal("open: %r");
 	for(;;){
 		if((n = read(orderfd, name, sizeof(name)-1)) == 0)
 			break;
 		name[n] = 0;
-		for(i = 0, s = streams; i < nstreams; i++, s++){
+		for(i = 0, s = streams; i < ns; i++, s++){
 			if(strcmp(s->name, name) != 0 || s->fd == -1)
 				continue;
 			if(rdwr(s->fd, 1) == 0)