[net.sources.bugs] pipe input to vi

david@sun.uucp (David DiGiacomo) (09/16/86)

In article <3115@pixar> brighton@pixar.UUCP writes:
>/*
>I wrote this late one night, to facilitate saving files from rn ( s | svi )
...
>Improvements welcome.  Enjoy!

Well, you asked for it...

>	sprintf (file, "/tmp/svi.%d", getpid());

Why not use mktemp(3) here?

>	fd = open (file, O_CREAT|O_RDWR, 0644);

Use mode 0666, let your umask work.

>char buf[10240];
 ...
>	while (write(fd,buf,read(0,buf,1024)))

Why use a 10K buffer to hold 1K?

>	tty_in = open ("/dev/tty", O_RDONLY);
 ...
>	tty_out = open ("/dev/tty", O_WRONLY);

Looks like a job for dup(2) !

>	strcpy (editor, getenv ("EDITOR"));
>	if (editor[0] == 0) {
>		strcpy (editor, getenv ("VISUAL"));

Oops!  Passing a null pointer to strcpy means instant core dump on 
many machines.

Finally, here's a shell script to do the same thing:

#! /bin/sh
# pedit -- pipeline editor

edit=${VISUAL-${EDITOR-vi}}
tmp=/tmp/pedit.$$

trap 'rm -f $tmp*; exit 1' 1 2 15

# workaround for SysV/SunOS sh "$@" bug
cat ${1+"$@"} > $tmp

# /dev/tty could be named explicitly here, but that's less flexible
$edit $tmp 0<&2 1>&2

# svi.c doesn't do this, but I find it useful
cat $tmp

# * is to catch editor backup files
rm -f $tmp*

-- 
David DiGiacomo  {decvax, ihnp4, ucbvax}!sun!david  david@sun.arpa
Sun Microsystems, Mt. View, CA  (415) 691-7495