xref: /csrg-svn/contrib/ed/e.c (revision 60663)
157680Sbostic /*-
2*60663Sbostic  * Copyright (c) 1992, 1993
3*60663Sbostic  *	The Regents of the University of California.  All rights reserved.
457680Sbostic  *
557680Sbostic  * This code is derived from software contributed to Berkeley by
657680Sbostic  * Rodney Ruddock of the University of Guelph.
757680Sbostic  *
857680Sbostic  * %sccs.include.redist.c%
957680Sbostic  */
1057680Sbostic 
1157680Sbostic #ifndef lint
12*60663Sbostic static char sccsid[] = "@(#)e.c	8.1 (Berkeley) 05/31/93";
1357680Sbostic #endif /* not lint */
1457680Sbostic 
1557710Sbostic #include <sys/types.h>
1657710Sbostic #include <sys/stat.h>
1757710Sbostic 
1857710Sbostic #include <fcntl.h>
1958315Sbostic #include <limits.h>
2057710Sbostic #include <regex.h>
2157710Sbostic #include <setjmp.h>
2257710Sbostic #include <stdio.h>
2357710Sbostic #include <stdlib.h>
2457710Sbostic #include <string.h>
2557710Sbostic #include <unistd.h>
2657710Sbostic 
2758315Sbostic #ifdef DBI
2858315Sbostic #include <db.h>
2958315Sbostic #endif
3058315Sbostic 
3157680Sbostic #include "ed.h"
3257710Sbostic #include "extern.h"
3357680Sbostic 
3457680Sbostic /*
3557680Sbostic  * Places a new file in the buffer to be editted. The current contents
3657680Sbostic  * of the buffer are deleted - no undo can be perfomed. A warning is
3757680Sbostic  * issued once if no write has occurred since the last buffer
3857680Sbostic  * modification (unless 'E' spec'd).
3957680Sbostic  */
4057680Sbostic 
4157680Sbostic void
e(inputt,errnum)4257680Sbostic e(inputt, errnum)
4357710Sbostic 	FILE *inputt;
4457710Sbostic 	int *errnum;
4557680Sbostic {
4657710Sbostic 	int l_which;		/* which is it? 'e' or 'E' */
4757710Sbostic 	char *l_temp;
4857680Sbostic 
4957710Sbostic 	l_which = ss;
5057680Sbostic 
5157710Sbostic 	l_temp = filename(inputt, errnum);
5257710Sbostic 	if (*errnum == 1) {
5358315Sbostic 		sigspecial++;
5457710Sbostic 		free(filename_current);
5557710Sbostic 		filename_current = l_temp;
5658315Sbostic 		sigspecial--;
5758315Sbostic 		if (sigint_flag && (!sigspecial))
5858315Sbostic 			SIGINT_ACTION;
5957710Sbostic 	} else
6057710Sbostic 		if (*errnum == -2)
6157710Sbostic 			while (((ss = getc(inputt)) != '\n') || (ss == EOF));
6257710Sbostic 		else
6357710Sbostic 			if (*errnum < 0)
6457710Sbostic 				return;
6557710Sbostic 	*errnum = 0;
6657680Sbostic 
6757710Sbostic 	/* Note: 'E' will bypass this if stmt., which warns of no save. */
6857710Sbostic 	if ((change_flag == 1L) && (l_which == 'e')) {
6957710Sbostic 		change_flag = 0L;
7057710Sbostic 		strcpy(help_msg, "warning: buffer changes not saved");
7157710Sbostic 		*errnum = -1;
7257710Sbostic 		ungetc('\n', inputt);
7357710Sbostic 		return;
7457710Sbostic 	}
7558564Sralph 	Start = top;
7657710Sbostic 	End = bottom;
7758564Sralph 	Start_default = End_default = 0;
7858564Sralph 	if (Start == NULL && bottom == NULL);
7957710Sbostic 	else {
8057710Sbostic 		ungetc(ss, inputt);
8157710Sbostic 		d(inputt, errnum);	/* delete the whole buffer */
8257710Sbostic 	}
8357680Sbostic 
8457710Sbostic 	/* An 'e' clears all traces of last doc'mt, even in 'g'. */
8557710Sbostic 	u_clr_stk();
8657710Sbostic 	if (*errnum < 0)
8757710Sbostic 		return;
8857710Sbostic 	*errnum = 0;
8958315Sbostic #ifdef STDIO
9058315Sbostic 	if (fhtmp > NULL) {
9158315Sbostic 		fclose(fhtmp);
9258315Sbostic 		unlink(template);
9358315Sbostic 	}
9458315Sbostic #endif
9558315Sbostic #ifdef DBI
9657710Sbostic 	if (dbhtmp != NULL) {
9757710Sbostic 		(dbhtmp->close) (dbhtmp);
9857710Sbostic 		unlink(template);
9957710Sbostic 	}
10058315Sbostic #endif
10157680Sbostic 
10257710Sbostic 	name_set = 1;
10357710Sbostic 	e2(inputt, errnum);
10457680Sbostic 
10557710Sbostic 	*errnum = 1;
10657710Sbostic }
10757680Sbostic 
10857710Sbostic /*
10957710Sbostic  * This is pulled out of e.c to make the "simulated 'e'" at startup easier to
11057710Sbostic  * handle.
11157710Sbostic  */
11257680Sbostic void
e2(inputt,errnum)11357680Sbostic e2(inputt, errnum)
11457710Sbostic 	FILE *inputt;
11557710Sbostic 	int *errnum;
11657680Sbostic {
11758315Sbostic 	char *tmp_path;
11857680Sbostic 
11958315Sbostic 	sigspecial++;
12058315Sbostic #ifndef MEMORY
12157710Sbostic 	if (template == NULL) {
12257710Sbostic 		template = (char *) calloc(FILENAME_LEN, sizeof(char));
12357710Sbostic 		if (template == NULL)
12457710Sbostic 			ed_exit(4);
12557710Sbostic 	}
12657710Sbostic 	/* create the buffer using the method favored at compile time */
12758315Sbostic 	tmp_path = getenv("TMPDIR");
12858315Sbostic 	sprintf(template, "%s/_4.4bsd_ed_XXXXXX", tmp_path ? tmp_path : "/tmp");
12957710Sbostic 	mktemp(template);
13058315Sbostic #endif
13158315Sbostic #ifdef STDIO
13258315Sbostic 	fhtmp = fopen(template, "w+");
13358315Sbostic 	if (fhtmp == NULL) {
13458315Sbostic 		ed_exit(5); /* unable to create buffer */
13558315Sbostic 	}
13658347Sbostic 	fwrite("R", sizeof(char), 1, fhtmp);
13758315Sbostic 	file_seek = 0;
13858315Sbostic #endif
13958315Sbostic #ifdef DBI
14060655Sbostic 	/* open using btree only, recno will mess things up
14160655Sbostic 	 * because of garbage collection and how recno operates
14260655Sbostic 	 * with delete.
14360655Sbostic 	 */
14457710Sbostic 	dbhtmp = dbopen(template, O_CREAT | O_RDWR,
14560655Sbostic 	    S_IRUSR | S_IWUSR, (DBTYPE) DB_BTREE, NULL);
14658315Sbostic 	if (dbhtmp == NULL) {
14758315Sbostic 		ed_exit(5); /* unable to create buffer */
14858315Sbostic 	}
14958315Sbostic #endif
15057710Sbostic 	current = top;
15158564Sralph 	Start = top;
15257710Sbostic 	End = bottom;
15357680Sbostic 
15458315Sbostic 	sigspecial--;
15558315Sbostic 	if (sigint_flag &&(!sigspecial))
15657710Sbostic 		SIGINT_ACTION;
15758315Sbostic 
15858315Sbostic 	change_flag = 1;
15957710Sbostic 	if (name_set) {
16057710Sbostic 		/* So 'r' knows the filename is already read in. */
16157710Sbostic 		filename_flag = 1;
16257710Sbostic 		r(inputt, errnum);
16359475Sbostic 		gut_num = line_number(bottom) + 512;
16459475Sbostic 		if (gut == NULL) {
16559475Sbostic 			gut = malloc(sizeof(LINE **) * gut_num);
16659475Sbostic 			if (gut == NULL) {
16759475Sbostic 				*errnum = -1;
16859475Sbostic 				strcpy(help_msg, "out of memory error");
16959475Sbostic 				return;
17059475Sbostic 			}
17159475Sbostic 		}
17257710Sbostic 	}
17357710Sbostic 	change_flag = 0;
17459915Sbostic 	*errnum = 1;
17557710Sbostic }
176