xref: /csrg-svn/contrib/ed/e.c (revision 59475)
157680Sbostic /*-
257680Sbostic  * Copyright (c) 1992 The Regents of the University of California.
357680Sbostic  * 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*59475Sbostic static char sccsid[] = "@(#)e.c	5.6 (Berkeley) 04/28/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
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
11357680Sbostic e2(inputt, errnum)
11457710Sbostic 	FILE *inputt;
11557710Sbostic 	int *errnum;
11657680Sbostic {
11758315Sbostic 	char *tmp_path;
11858315Sbostic #ifdef DBI
11957710Sbostic 	RECNOINFO l_dbaccess;
12058315Sbostic #endif
12157680Sbostic 
12258315Sbostic 	sigspecial++;
12358315Sbostic #ifndef MEMORY
12457710Sbostic 	if (template == NULL) {
12557710Sbostic 		template = (char *) calloc(FILENAME_LEN, sizeof(char));
12657710Sbostic 		if (template == NULL)
12757710Sbostic 			ed_exit(4);
12857710Sbostic 	}
12957710Sbostic 	/* create the buffer using the method favored at compile time */
13058315Sbostic 	tmp_path = getenv("TMPDIR");
13158315Sbostic 	sprintf(template, "%s/_4.4bsd_ed_XXXXXX", tmp_path ? tmp_path : "/tmp");
13257710Sbostic 	mktemp(template);
13358315Sbostic #endif
13458315Sbostic #ifdef STDIO
13558315Sbostic 	fhtmp = fopen(template, "w+");
13658315Sbostic 	if (fhtmp == NULL) {
13758315Sbostic 		ed_exit(5); /* unable to create buffer */
13858315Sbostic 	}
13958347Sbostic 	fwrite("R", sizeof(char), 1, fhtmp);
14058315Sbostic 	file_seek = 0;
14158315Sbostic #endif
14258315Sbostic #ifdef DBI
143*59475Sbostic /*
14457710Sbostic 	(l_dbaccess.bval) = (u_char) '\0';
14557710Sbostic 	(l_dbaccess.cachesize) = 0;
14657710Sbostic 	(l_dbaccess.flags) = R_NOKEY;
14757710Sbostic 	(l_dbaccess.lorder) = 0;
14857710Sbostic 	(l_dbaccess.reclen) = 0;
14957710Sbostic 	dbhtmp = dbopen(template, O_CREAT | O_RDWR,
15057710Sbostic 	    S_IRUSR | S_IWUSR, (DBTYPE) DB_RECNO, &l_dbaccess);
151*59475Sbostic */
152*59475Sbostic 	dbhtmp = dbopen(template, O_CREAT | O_RDWR,
153*59475Sbostic 	    S_IRUSR | S_IWUSR, (DBTYPE) DB_RECNO, NULL);
15458315Sbostic 	if (dbhtmp == NULL) {
15558315Sbostic 		ed_exit(5); /* unable to create buffer */
15658315Sbostic 	}
15758315Sbostic #endif
15857710Sbostic 	current = top;
15958564Sralph 	Start = top;
16057710Sbostic 	End = bottom;
16157680Sbostic 
16258315Sbostic 	sigspecial--;
16358315Sbostic 	if (sigint_flag &&(!sigspecial))
16457710Sbostic 		SIGINT_ACTION;
16558315Sbostic 
16658315Sbostic 	change_flag = 1;
16757710Sbostic 	if (name_set) {
16857710Sbostic 		/* So 'r' knows the filename is already read in. */
16957710Sbostic 		filename_flag = 1;
17057710Sbostic 		r(inputt, errnum);
171*59475Sbostic 		gut_num = line_number(bottom) + 512;
172*59475Sbostic 		if (gut == NULL) {
173*59475Sbostic 			gut = malloc(sizeof(LINE **) * gut_num);
174*59475Sbostic 			if (gut == NULL) {
175*59475Sbostic 				*errnum = -1;
176*59475Sbostic 				strcpy(help_msg, "out of memory error");
177*59475Sbostic 				return;
178*59475Sbostic 			}
179*59475Sbostic 		}
18057710Sbostic 	}
18157710Sbostic 	change_flag = 0;
18257710Sbostic }
183