1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. 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 8.1 (Berkeley) 05/31/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
e(inputt,errnum)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
e2(inputt,errnum)113 e2(inputt, errnum)
114 FILE *inputt;
115 int *errnum;
116 {
117 char *tmp_path;
118
119 sigspecial++;
120 #ifndef MEMORY
121 if (template == NULL) {
122 template = (char *) calloc(FILENAME_LEN, sizeof(char));
123 if (template == NULL)
124 ed_exit(4);
125 }
126 /* create the buffer using the method favored at compile time */
127 tmp_path = getenv("TMPDIR");
128 sprintf(template, "%s/_4.4bsd_ed_XXXXXX", tmp_path ? tmp_path : "/tmp");
129 mktemp(template);
130 #endif
131 #ifdef STDIO
132 fhtmp = fopen(template, "w+");
133 if (fhtmp == NULL) {
134 ed_exit(5); /* unable to create buffer */
135 }
136 fwrite("R", sizeof(char), 1, fhtmp);
137 file_seek = 0;
138 #endif
139 #ifdef DBI
140 /* open using btree only, recno will mess things up
141 * because of garbage collection and how recno operates
142 * with delete.
143 */
144 dbhtmp = dbopen(template, O_CREAT | O_RDWR,
145 S_IRUSR | S_IWUSR, (DBTYPE) DB_BTREE, NULL);
146 if (dbhtmp == NULL) {
147 ed_exit(5); /* unable to create buffer */
148 }
149 #endif
150 current = top;
151 Start = top;
152 End = bottom;
153
154 sigspecial--;
155 if (sigint_flag &&(!sigspecial))
156 SIGINT_ACTION;
157
158 change_flag = 1;
159 if (name_set) {
160 /* So 'r' knows the filename is already read in. */
161 filename_flag = 1;
162 r(inputt, errnum);
163 gut_num = line_number(bottom) + 512;
164 if (gut == NULL) {
165 gut = malloc(sizeof(LINE **) * gut_num);
166 if (gut == NULL) {
167 *errnum = -1;
168 strcpy(help_msg, "out of memory error");
169 return;
170 }
171 }
172 }
173 change_flag = 0;
174 *errnum = 1;
175 }
176