[net.sources] "redo" stuff

crh (09/03/82)

Here is the "redo" stuff I promised in net.unix-wizards.

First the alias:

alias ! '/usr/local/redo \!* ~/.cmd ~/.cmd1 ; source ~/.cmd1 '

Here is redo.c. I could have used a script, but didnt.

#include <stdio.h>

/* redo -- outputs a command file (last arg )used to edit and 
   re-ex a command.  Next to last arg is dest file of the command. 
   First arg is the history ref . To use:
    alias ! 'redo \!* ~/cmd ~/cmd1 ; source ~/cmd1 '
	 ! 25    to edit & re-execute !25
	 ! ! or !  to edit & re-execute !!
	 ! v     to edit & re-execute !v ,etc. 
	 
*/

main(argc, argv)
	int argc;
	char *argv[];
{
	FILE *fp;

        fp=fopen(argv[argc-1],"w");
	fputs("echo \"!",fp);
	if ((argc<4)|| (strcmp(argv[1],"!")==0))  fputs("-2",fp);
        else fputs(argv[1],fp);
	fputs(":q\" >! ",fp);
	fputs(argv[argc-2],fp);
	putc('\n',fp);
	fputs("ex +open ",fp);
	fputs(argv[argc-2],fp);
	putc('\n',fp);
	fputs ("/usr/local/typein2 < ",fp);
	fputs (argv[argc-2],fp);
	putc('\n',fp);
	fclose(fp);
	exit(0);
}


Here is typein2.c, a modified version of typein.c, which takes its std input
(assumed to be small (<=256 chars) ) and places it in the tty INPUT :


#include <stdio.h>
#include <sgtty.h>

main(argc, argv)
	int argc;
	char **argv;
{
	register char *cp;
	struct sgttyb stb, stb2;
	int pendin = LPENDIN;
	int c,i,j;
	char buff[2];
	char buff2[256];

	i=0;
	while ((c=getchar()) != EOF) {
			buff2[i++]=c;
	}
	ioctl(2, TIOCGETP, &stb);
	stb2 = stb;
	stb.sg_flags &= ~ECHO;
	ioctl(2, TIOCSETN, &stb);
	for (j=0; j<i; j++) {
			ioctl(2, TIOCSTI, &buff2[j]);
			putchar(buff2[j]);
	}
	ioctl(2, TIOCSETN, &stb2);
	ioctl(2, TIOCLBIS, &pendin);
	exit(0);
}

Note redo uses args for the files it creates so that each user can name
them as he chooses when he sets up the alias.

Improvements, comments, etc are welcome.

Charlie Hill
Philips Labs
{cmcl2,sdcsvax,floyd}!philabs!crh