xref: /original-bsd/contrib/ed/e.c (revision 0b318d59)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rodney Ruddock of the University of Guelph.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)e.c	5.5 (Berkeley) 03/08/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 #include <fcntl.h>
19 #include <limits.h>
20 #include <regex.h>
21 #include <setjmp.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #ifdef DBI
28 #include <db.h>
29 #endif
30 
31 #include "ed.h"
32 #include "extern.h"
33 
34 /*
35  * Places a new file in the buffer to be editted. The current contents
36  * of the buffer are deleted - no undo can be perfomed. A warning is
37  * issued once if no write has occurred since the last buffer
38  * modification (unless 'E' spec'd).
39  */
40 
41 void
42 e(inputt, errnum)
43 	FILE *inputt;
44 	int *errnum;
45 {
46 	int l_which;		/* which is it? 'e' or 'E' */
47 	char *l_temp;
48 
49 	l_which = ss;
50 
51 	l_temp = filename(inputt, errnum);
52 	if (*errnum == 1) {
53 		sigspecial++;
54 		free(filename_current);
55 		filename_current = l_temp;
56 		sigspecial--;
57 		if (sigint_flag && (!sigspecial))
58 			SIGINT_ACTION;
59 	} else
60 		if (*errnum == -2)
61 			while (((ss = getc(inputt)) != '\n') || (ss == EOF));
62 		else
63 			if (*errnum < 0)
64 				return;
65 	*errnum = 0;
66 
67 	/* Note: 'E' will bypass this if stmt., which warns of no save. */
68 	if ((change_flag == 1L) && (l_which == 'e')) {
69 		change_flag = 0L;
70 		strcpy(help_msg, "warning: buffer changes not saved");
71 		*errnum = -1;
72 		ungetc('\n', inputt);
73 		return;
74 	}
75 	Start = top;
76 	End = bottom;
77 	Start_default = End_default = 0;
78 	if (Start == NULL && bottom == NULL);
79 	else {
80 		ungetc(ss, inputt);
81 		d(inputt, errnum);	/* delete the whole buffer */
82 	}
83 
84 	/* An 'e' clears all traces of last doc'mt, even in 'g'. */
85 	u_clr_stk();
86 	if (*errnum < 0)
87 		return;
88 	*errnum = 0;
89 #ifdef STDIO
90 	if (fhtmp > NULL) {
91 		fclose(fhtmp);
92 		unlink(template);
93 	}
94 #endif
95 #ifdef DBI
96 	if (dbhtmp != NULL) {
97 		(dbhtmp->close) (dbhtmp);
98 		unlink(template);
99 	}
100 #endif
101 
102 	name_set = 1;
103 	e2(inputt, errnum);
104 
105 	*errnum = 1;
106 }
107 
108 /*
109  * This is pulled out of e.c to make the "simulated 'e'" at startup easier to
110  * handle.
111  */
112 void
113 e2(inputt, errnum)
114 	FILE *inputt;
115 	int *errnum;
116 {
117 	char *tmp_path;
118 #ifdef DBI
119 	RECNOINFO l_dbaccess;
120 #endif
121 
122 	sigspecial++;
123 #ifndef MEMORY
124 	if (template == NULL) {
125 		template = (char *) calloc(FILENAME_LEN, sizeof(char));
126 		if (template == NULL)
127 			ed_exit(4);
128 	}
129 	/* create the buffer using the method favored at compile time */
130 	tmp_path = getenv("TMPDIR");
131 	sprintf(template, "%s/_4.4bsd_ed_XXXXXX", tmp_path ? tmp_path : "/tmp");
132 	mktemp(template);
133 #endif
134 #ifdef STDIO
135 	fhtmp = fopen(template, "w+");
136 	if (fhtmp == NULL) {
137 		ed_exit(5); /* unable to create buffer */
138 	}
139 	fwrite("R", sizeof(char), 1, fhtmp);
140 	file_seek = 0;
141 #endif
142 #ifdef DBI
143 	(l_dbaccess.bval) = (u_char) '\0';
144 	(l_dbaccess.cachesize) = 0;
145 	(l_dbaccess.flags) = R_NOKEY;
146 	(l_dbaccess.lorder) = 0;
147 	(l_dbaccess.reclen) = 0;
148 	dbhtmp = dbopen(template, O_CREAT | O_RDWR,
149 	    S_IRUSR | S_IWUSR, (DBTYPE) DB_RECNO, &l_dbaccess);
150 	if (dbhtmp == NULL) {
151 		ed_exit(5); /* unable to create buffer */
152 	}
153 #endif
154 	current = top;
155 	Start = top;
156 	End = bottom;
157 
158 	sigspecial--;
159 	if (sigint_flag &&(!sigspecial))
160 		SIGINT_ACTION;
161 
162 	change_flag = 1;
163 	if (name_set) {
164 		/* So 'r' knows the filename is already read in. */
165 		filename_flag = 1;
166 		r(inputt, errnum);
167 	}
168 	change_flag = 0;
169 }
170