/*
 *  Copyright (C) 1984, 1985  S.R.I. International
 *  Copyright (C) 1992	TGV, Inc.
 *
 *	Invoke editors
 *
 */
#include <signal.h>
#include <stdio.h>
#include <descrip.h>
#include "vax-mm.h"

static int Edit_Read_Only_Flag = 0;


/*
 *	Use editor to view messages
 */
CMD_Edit()
{
	register int Message;
	register struct Msg *Msg;

	/*
	 *	Message List
	 */
	Parse_Message_List("CURRENT");
	/*
	 *	Confirm
	 */
	Confirm();
	/*
	 *	Do it
	 */
	while(1) {
		Message = Next_Message(0);
		if (Message <= 0) break;
		Current_Message = Message;
		Msg = &Messages[Message-1];
		Edit_Buffer_Read_Only("mm-read-message",
				      MESSAGE_HEADER(Message) +
							Msg->Header_Size,
				      Msg->Real_Size - Msg->Header_Size);
	}
}


/*
 *	Edit a buffer Read-Only
 */
Edit_Buffer_Read_Only(Fancy_Command,Buffer,Current_Size)
char *Fancy_Command;
char *Buffer;
{
	Edit_Read_Only_Flag = 1;
	Edit_Buffer(Fancy_Command,Buffer,Current_Size,0);
	Edit_Read_Only_Flag = 0;
}


/*
 *	Edit a buffer
 */
Edit_Buffer(Fancy_Command,Buffer,Current_Size,Maximum_Size)
char *Fancy_Command;
char *Buffer;
{
	register char *cp,*cp1;
	int fd,i;
	char Filename[32];
	int pid;
	int mypid = getpid() & 0xffff;
	char Local_Argv_Characters[sizeof(Editor_Invocation_Command)+32];
	static int Our_Tty = -1;
	static int Our_Pgroup;
	static int Editor_Pgroup;

	/*
	 *	Write the buffer to a temporary file
	 */
	sprintf(Filename,"sys$scratch:mm%d.tmp",mypid);
	fd = Xcreat(Filename,0600,"txt");
	if (!Simple_Editor) {
		/*
		 *	The 1st line in the buffer is a fancy editor/MM command
		 */
		write(fd,Fancy_Command,strlen(Fancy_Command));
		write(fd,"\n",1);
		/*
		 *	If reply mode, add the original message
		 */
		if (strcmp(Fancy_Command,"mm-reply-edit") == 0) {
			write(fd,
			      MESSAGE_HEADER(Current_Message),
			      Messages[Current_Message-1].Real_Size);
			write(fd,"\03***END-OF-MESSAGE***\03\n",23);
		}
	}
	write(fd,Buffer,Current_Size);
	close(fd);
	/*
	 *	If Fancy editor and the editor is saved, continue it
	 */
	{
		/*
		 *	Attempt to attach a previously spawned subprocess
		 */
		if (Saved_Editor_PID) {
    	    	    	if (!(lib$attach(&Saved_Editor_PID) & 1))
    	    	    	    Saved_Editor_PID = 0;
		}
		/*
		 *	If not attached, we spawn a NEW editor
		 */
		if (Saved_Editor_PID == 0) {
    	    	    	struct dsc$descriptor dsc;
			dsc.dsc$w_length = sprintf(Local_Argv_Characters,
				Editor_Invocation_Command,
				Filename);
    	    	    	dsc.dsc$b_dtype = DSC$K_DTYPE_T;
    	    	    	dsc.dsc$b_class = DSC$K_CLASS_S;
    	    	    	dsc.dsc$a_pointer = Local_Argv_Characters;
			i = lib$spawn(&dsc, 0, 0, 0, 0, &Saved_Editor_PID);
			if (!(i & 1)) {
				/*
				 *	Spawn failed: print status
				 */
				printf("SPAWN failed, status = %x\n",i);
			}
		}
	}
	/*
	 *	Done, read the temp file back in!
	 */
Done:	if (Edit_Read_Only_Flag) {
		i = Current_Size;
	} else {
		fd = open(Filename,0);
		i= read(fd,Buffer,Maximum_Size);
		if (i == Maximum_Size)
			printf("The editor buffer was too large, it has been truncated\n");
		close(fd);
	}
	/*
	 *	Delete it
	 */
	while(unlink(Filename) >= 0) ;
	/*
	 *	Done
	 */
	return(i);

}
