
#include "logo.h"
#include <signal.h>

#ifdef NEXT
extern char *ibufptr;
extern const char *homeval;
extern const char *syslibdir;
extern const char *helpdir;
#endif NEXT
extern int nullfn();
extern int errrec();
extern int ehand2(),ehand3();
extern char *token();
extern char *getenv();
extern char titlebuf[],editfile[];
extern int letflag;
#ifndef NOTURTLE
extern int turtdes,textmode;
extern struct display *mydpy;
#endif

chkproc(str,prim,obj)
register char *str;
char *prim;
struct object *obj;
{
	register char ch;

	if (((ch = *str)<'a') || (ch>'z')) ungood(prim,obj);
	if (memb('/',str)) ungood(prim,obj);
	if (strlen(str)>NAMELEN) ungood(prim,obj);
}

stedit(ednobj,flag)
struct object *ednobj;
int flag;
{
	register char *edname;
	register struct object *rest;
	char fnam[200];
	char edline[100];
	FILDES edfd;

	if (ednobj==0) ungood("Edit",ednobj);
	if (flag==0) unlink(editfile);
	switch (ednobj->obtype) {
		case INT:
		case DUB:
			ungood("Edit",ednobj);
		case CONS:
			for (rest=ednobj; rest; rest=rest->obcdr)
				stedit(localize(rest->obcar),1);
			break;
		default: /* STRING */
			edname = token(ednobj->obstr);
			chkproc(edname,"Edit",ednobj);
#ifndef NEXT
			cpystr(fnam,edname,EXTEN,NULL);
#else   NEXT
                        if (homeval == NULL)
			   cpystr(fnam,LOCALLIB,edname,EXTEN,NULL);
                        else
			   cpystr(fnam,homeval,edname,EXTEN,NULL);
#endif  NEXT
			if ((edfd=open(fnam,READ,0))<0) {
#ifdef NEXT
	                if (syslibdir == NULL)
			    cpystr(fnam,LIBLOGO,edname,EXTEN,NULL);
                        else
                            cpystr(fnam,syslibdir,edname,EXTEN,NULL);

#else NEXT
				cpystr(fnam,LIBLOGO,edname,EXTEN,NULL);
#endif NEXT
				if ((edfd=open(fnam,READ,0)) < 0) {
#ifndef NEXT
			                cpystr(fnam,edname,EXTEN,NULL);
#else   NEXT
                        if (homeval == NULL)
			   cpystr(fnam,LOCALLIB,edname,EXTEN,NULL);
                        else
			   cpystr(fnam,homeval,edname,EXTEN,NULL);
#endif  NEXT
					edfd = creat(fnam,0666);
					if (edfd < 0) {
					   printf("Can't write %s.\n",fnam);
					   printf("Can't write %s.\n",edname);
						mfree(ednobj);
						errhand();
					}
					onintr(ehand3,edfd);
					write(edfd,"to ",3);
					write(edfd,edname,strlen(edname));
					write(edfd,"\n\nend\n",6);
				}
			}
			close(edfd);
			onintr(errrec,1);
			sprintf(edline,"cat %s >> %s",fnam,editfile);
			system(edline);
			sprintf(edline,LIBNL,editfile);
			system(edline);
	}
	mfree(ednobj);
	if (flag==0) doedit();
}

doedit() {
	register char ch,*cp;
	FILE *fd,*ofd;
	int pid,status;
	char *name,*envedit;
	char fname[200];
	char line[200];
	static char binname[25] = "";
	static char usrbinname[30];
	static char editname[20];
	static char *editor;

	if (binname[0] == '\0') {
		editor = getenv("EDITOR");
		envedit = editor ? editor : EDT;	/* default editor */
		strcpy(binname,"/bin/");
		strcat(binname,envedit);
		strcpy(usrbinname,"/usr/bin/");
		strcat(usrbinname,envedit);
		strcpy(editname,envedit);
	}

#ifndef NOTURTLE
	if (turtdes<0) {
		(*mydpy->state)('t');
		textmode++;
	}
#endif
	fflush(stdout);
	signal(SIGINT,SIG_IGN);
	signal(SIGQUIT,SIG_IGN);
	switch (pid=fork()) {
		case -1:
			printf("Can't fork to editor.\n");
			errhand();
		case 0:
			/*if (editor) */ execl(editname,editname,editfile,0);
			/* Only try bare name if really user-specified. */
			execl(binname,editname,editfile,0);
			execl(usrbinname,editname,editfile,0);
			printf("Can't find editor.\n");
			exit(2);
		default:
			while (wait(&status) != pid) ;
	}
	if (status&0177400) {
		printf("Redefinition aborted.\n");
		errhand();
	}
	if ((fd=fopen(editfile,"r"))==NULL) {
		printf("Can't reread edits!\n");
		errhand();
	}
	onintr(ehand2,fd);
	while (fgets(line,200,fd)) {
		for (cp = line; (ch = *cp)==' ' || ch=='\t'; cp++) ;
		if (ch == '\n') continue;
		if (strcmp(token(cp),"to")) {
			printf("Edited file includes non-procedure.\n");
			ehand2(fd);
		}
		for (cp += 2; (ch = *cp)==' ' || ch=='\t'; cp++) ;
		name = token(cp);
		printf("Defining %s\n",name);
#ifndef NEXT
		sprintf(fname,"%s%s",name,EXTEN);
#else   NEXT
                if (homeval == NULL)
		   sprintf(fname,"%s%s%s",LOCALLIB,name,EXTEN);
                else
		   sprintf(fname,"%s%s%s",homeval,name,EXTEN);
#endif  NEXT
		ofd = fopen(fname,"w");
		if (ofd==NULL) {
			printf("Can't write %s\n",fname);
			ehand2(fd);
		}
		fprintf(ofd,"%s",line);
		while (fgets(line,200,fd)) {
			fprintf(ofd,"%s",line);
			for (cp = line; (ch = *cp)==' ' || ch=='\t'; cp++) ;
			if (!strcmp(token(cp),"end")) break;
		}
		fclose(ofd);
	}
	fclose(fd);
	onintr(errrec,1);
}

struct object *cmedit(arg)
struct object *arg;
{
	stedit(arg,0);
	return ((struct object *)(-1));
}

struct object *erase(name)	/* delete a procedure from directory */
register struct object *name;
{
	register struct object *rest;
	char temp[200];

	if (name==0) ungood("Erase",name);
	switch(name->obtype) {
		case STRING:
#ifndef NEXT
			cpystr(temp,name->obstr,EXTEN,NULL);
#else   NEXT
                        if (homeval == NULL)
			   cpystr(temp,LOCALLIB,name->obstr,EXTEN,NULL);
                        else
			   cpystr(temp,homeval,name->obstr,EXTEN,NULL);
#endif  NEXT
			if (unlink(temp)<0) {	/* undefined procedure */
				nputs("You haven't defined ");
				puts(name->obstr);
				errhand();
			}
			break;
		case CONS:
			for (rest = name; rest; rest = rest->obcdr)
				erase(localize(rest->obcar));
			break;
		default:	/* number */
			ungood("Erase",name);
	}
	mfree(name);
	return ((struct object *)(-1));
}

addlines(edfd)	/* read text of procedure, simple TO style */
int edfd;
{
	register char *lintem;
	int oldlet;
	static char tstack[IBUFSIZ];
	int brak,brace,ch;	/* BH 1/7/82 */

	oldlet=letflag;
	letflag=1;	/* read rest of line verbatim */
loop:
#ifndef NEXT
	putchar('>');
	fflush(stdout);
#endif  NEXT
	lintem=tstack;
	brace = brak = 0;	/* BH 1/7/82 count square brackets */
	do {
#ifdef NEXT
            if(ibufptr == NULL)
            {
              printf("The 'to' procedure can only be used\n");
              printf("if the whole input command is a\n");
              printf("full to procedure.  To input multiple\n");
              printf("lines in a TextField object, press the\n");
              printf("<alt> and <return> keys together.\n");
              printf("This allows for the entry of a line\n");
              printf("feed into the TextField object.\n\n");
              break;
            }
#endif NEXT
		while ((ch=getchar())!='\n') {
			if (lintem >= &tstack[IBUFSIZ-2]) {
				printf("Line too long.");
				goto loop;
			}
			*lintem++ = ch;
			if (ch == '\\') *lintem++ = getchar();
			else if (ch == '[') brak++;
			else if (ch == ']') --brak;
			else if (brak == 0) {
				if (ch == '{' || ch == '(') brace++;
				else if (ch == '}' || ch == ')') --brace;
			}
		}
		if (brak > 0) {
			*lintem++ = ' ';
			printf("[: ");
			fflush(stdout);
		} else if (brace > 0) {
			*lintem++ = ' ';
			printf("{(: ");
			fflush(stdout);
		}
	} while (brace+brak > 0);
	*lintem++='\n';
	*lintem='\0';
	write(edfd,tstack,lintem-tstack);
	for (lintem = tstack; memb(*lintem++," \t") ; ) ;
	--lintem;
#ifdef NEXT
	if (strcmp(token(lintem),"end") && ibufptr != NULL) goto loop;
	/* write(edfd,"\nend\n",6); */
#else  NEXT
	if (strcmp(token(lintem),"end")) goto loop;
#endif NEXT
	close(edfd);
	letflag=oldlet;
}

struct object *show(nameob)
register struct object *nameob;
{
	register struct object *rest;
	register char *name;
	FILE *sbuf;
	char temp[200];

	if (nameob==0) ungood("Show",nameob);
	switch(nameob->obtype) {
		case STRING:
			name = nameob->obstr;
#ifndef NEXT
			cpystr(temp,name,EXTEN,NULL);
#else   NEXT
                        if (homeval == NULL)
			   cpystr(temp,LOCALLIB,name,EXTEN,NULL);
                        else
			   cpystr(temp,homeval,name,EXTEN,NULL);
#endif  NEXT
			if (!(sbuf=fopen(temp,"r"))) {
#ifdef NEXT
	                if (syslibdir == NULL)
			    cpystr(temp,LIBLOGO,name,EXTEN,NULL);
                        else
                            cpystr(temp,syslibdir,name,EXTEN,NULL);

#else NEXT
				cpystr(temp,LIBLOGO,name,EXTEN,NULL);
#endif NEXT
				if (!(sbuf = fopen(temp,"r"))) {
					printf("You haven't defined %s\n",name);
					errhand();
				}
			}
			onintr(ehand2,sbuf);
			while (putch(getc(sbuf))!=EOF)
				;
			fclose(sbuf);
			onintr(errrec,1);
			break;
		case CONS:
			for (rest = nameob; rest; rest = rest->obcdr) {
				show(localize(rest->obcar));
				putchar('\n');
			}
			break;
		default:	/* number */
			ungood("Show",nameob);
	}
	mfree(nameob);
	return ((struct object *)(-1));
}

