Article 13132 of alt.sources:
Path: nntpd.lkg.dec.com!lead.zk3.dec.com!crl.dec.com!crl.dec.com!bloom-beacon.mit.edu!newsfeed.internetmci.com!newsxfer2.itd.umich.edu!agate!mconst
From: mconst@OCF.Berkeley.EDU (Michael Constant)
Newsgroups: alt.sources
Subject: y - another pipe fitting
Followup-To: alt.sources.d
Date: 28 Jan 1996 07:13:17 GMT
Organization: Society for the Prevention of Cruelty to Vegetables, UC Berkeley
Lines: 192
Message-ID: <4ef7md$nd4@agate.berkeley.edu>
NNTP-Posting-Host: maelstrom-ether.berkeley.edu

Archive-name: y
Submitted-by: mconst@csua.berkeley.edu

This is a very simple utility that I have found quite useful.  By way
of explaining what it does, I have started this post with some of the
(formatted) manpage; the entire nroff source is in the sharchive below.

Any comments or suggestions would be greatly appreciated; please send
them to mconst@csua.berkeley.edu.

NAME
       y - another pipe fitting

SYNOPSIS
       `subcommand | y`

DESCRIPTION
       The y utility is used to create systems of pipes and fifos
       (named pipes) more complicated than standard shell  syntax
       allows.   A  single  invocation  of  y  has  the following
       effect: a temporary fifo is created; the name of this fifo
       is  printed  on  standard  output;  and y exits, leaving a
       child process in the background to copy any data  received
       on  standard input to the fifo.  The net effect of this is
       that you can use the expression `subcommand | y`  anywhere
       on  a  command line that you would use a filename, and the
       output of subcommand will be piped appropriately to create
       the  illusion  that `subcommand | y` is a file, containing
       the output of subcommand.

EXAMPLES
       Since y is a simple command, I  have  given  it  a  simple
       description;  however,  some  examples  are  certainly  in
       order.  To print all lines of  the  standard  input  which
       contain the name of a file in the current directory:

            fgrep -f `ls | y`

       To show which users log on or off in the next minute:

            diff `who | y` `sleep 60; who | y`

       To change the names of all the files in the current direc-
       tory to lowercase:

            paste `ls | y` `ls | tr A-Z a-z | y` | xargs -n 2 mv


======== cut here for y.c and y.1 ========

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	y.c
#	y.1
#
echo x - y.c
sed 's/^X//' >y.c << 'END-of-y.c'
X/* y.c, written by Michael Constant (mconst@csua.berkeley.edu) and placed
X * in the public domain.  $Id: y.c,v 1.3 1996/01/28 06:16:44 mconst Exp $ */
X
X#include <stdio.h>
X#include <unistd.h>
X#include <signal.h>
X#include <stdlib.h>
X#include <sys/stat.h>
X#include <sys/errno.h>
X
X#define TEMPMASK "/tmp/y.XXXXXX"	/* the name of our tempfile */
X
Xvoid cleanup(int sig);
X
Xchar tempfile[]=TEMPMASK;
X
Xint main()
X{
X	int ch;
X	FILE *fp;
X
X	/* create the fifo.  if we get EEXIST, then someone else created a
X	 * file with our intended name right before we did; we should pick
X	 * a new name and try again.  see the source to mkstemp(3). */
X	while (mkfifo(mktemp(tempfile), 0600) == -1) {
X		if (errno != EEXIST) {
X			perror("y: couldn't create fifo");
X			exit(1);
X		}
X		strcpy(tempfile, TEMPMASK);
X	}
X
X	/* remove our tempfile if we die */
X	signal(SIGHUP, cleanup);
X	signal(SIGINT, cleanup);
X	signal(SIGQUIT, cleanup);
X	signal(SIGTERM, cleanup);
X
X	/* fork a child to do the work */
X	switch (fork()) {
X	case -1:
X		perror("y: couldn't fork");
X		exit(1);
X
X	case 0:
X		fclose(stdout);		/* this is necessary for `cmd | y` */
X
X		/* this will block until someone else opens the fifo */
X		if ((fp = fopen(tempfile, "w")) == NULL) {
X			perror("y: couldn't open fifo");
X			exit(1);	/* not as if anyone cares */
X		}
X
X		unlink(tempfile);	/* so it disappears when we're done */
X		while ((ch = getchar()) != EOF) putc(ch, fp);
X		exit(0);
X	}
X
X	/* output the name of the fifo */
X	printf("%s\n", tempfile);
X	exit(0);
X}
X
Xvoid cleanup(int sig)
X{
X	unlink(tempfile);
X	exit(2);
X}
END-of-y.c
echo x - y.1
sed 's/^X//' >y.1 << 'END-of-y.1'
X.\" $Id: y.1,v 1.3 1996/01/28 06:33:18 mconst Exp $
X.TH Y 1
X.SH NAME
X\fCy\fR \- another pipe fitting
X.SH SYNOPSIS
X\fC`\fIsubcommand\fP | y`\fR
X.SH DESCRIPTION
XThe \fCy\fR utility is used to create systems of pipes and fifos
X(named pipes) more complicated than standard shell syntax allows.
XA single invocation of \fCy\fR has the following effect: a temporary
Xfifo is created; the name of this fifo is printed on standard output;
Xand \fCy\fR exits, leaving a child process in the background to copy
Xany data received on standard input to the fifo.  The net effect of
Xthis is that you can use the expression \fC`\fIsubcommand\fP | y`\fR
Xanywhere on a command line that you would use a filename, and the output of
X\fIsubcommand\fP will be piped appropriately to create the illusion that
X\fC`\fIsubcommand\fP | y`\fR is a file, containing the output of
X\fIsubcommand\fR.
X.SH EXAMPLES
XSince \fCy\fR is a simple command, I have given it a simple
Xdescription; however, some examples are certainly in order.
XTo print all lines of the standard input which contain the name
Xof a file in the current directory:
X.nf
X
X	\fCfgrep \-f `ls | y`\fR
X
X.fi
XTo show which users log on or off in the next minute:
X.nf
X
X	\fCdiff `who | y` `sleep 60; who | y`\fR
X
X.fi
XTo change the names of all the files in the current directory to lowercase:
X.nf
X
X	\fCpaste `ls | y` `ls | tr A\-Z a\-z | y` | xargs \-n 2 mv\fR
X.fi
X.SH FILES
X\fC/tmp/y.??????\fR	temporary fifos
X.SH BUGS
XIf the temporary fifo is never opened by another program, the background
X\fCy\fR process will stick around indefinitely.
X.PP
XThere is no equivalent of \fCmkstemp\fR(\fC3\fR) for fifos, so we
Xmust provide the same functionality ourselves with \fCmktemp\fR(\fC3\fR)
Xand \fCmkfifo\fR(\fC2\fR), repeating the calls if
X.SM EEXIST
Xis returned.
X.SH AUTHOR
XMichael Constant (mconst@csua.berkeley.edu).
X.SH "SEE ALSO"
X\fCmktemp\fR(\fC3\fR), \fCmkstemp\fR(\fC3\fR), \fCmkfifo\fR(\fC2\fR),
X\fCtee\fR(\fC1\fR)
END-of-y.1
exit
--
            Michael Constant (mconst@soda.csua.berkeley.edu)


