xref: /csrg-svn/contrib/ed/e.c (revision 57710)
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.2 (Berkeley) 01/23/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 #include <db.h>
19 #include <fcntl.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 #include "ed.h"
28 #include "extern.h"
29 
30 /*
31  * Places a new file in the buffer to be editted. The current contents
32  * of the buffer are deleted - no undo can be perfomed. A warning is
33  * issued once if no write has occurred since the last buffer
34  * modification (unless 'E' spec'd).
35  */
36 
37 void
38 e(inputt, errnum)
39 	FILE *inputt;
40 	int *errnum;
41 {
42 	int l_which;		/* which is it? 'e' or 'E' */
43 	char *l_temp;
44 
45 	l_which = ss;
46 
47 	l_temp = filename(inputt, errnum);
48 	if (sigint_flag)
49 		SIGINT_ACTION;
50 	if (*errnum == 1) {
51 		free(filename_current);
52 		filename_current = l_temp;
53 	} else
54 		if (*errnum == -2)
55 			while (((ss = getc(inputt)) != '\n') || (ss == EOF));
56 		else
57 			if (*errnum < 0)
58 				return;
59 	*errnum = 0;
60 
61 	/* Note: 'E' will bypass this if stmt., which warns of no save. */
62 	if ((change_flag == 1L) && (l_which == 'e')) {
63 		change_flag = 0L;
64 		strcpy(help_msg, "warning: buffer changes not saved");
65 		*errnum = -1;
66 		ungetc('\n', inputt);
67 		return;
68 	}
69 	start = top;
70 	End = bottom;
71 	start_default = End_default = 0;
72 	if (start == NULL && bottom == NULL);
73 	else {
74 		ungetc(ss, inputt);
75 		d(inputt, errnum);	/* delete the whole buffer */
76 	}
77 	if (sigint_flag)
78 		SIGINT_ACTION;
79 
80 	/* An 'e' clears all traces of last doc'mt, even in 'g'. */
81 	u_clr_stk();
82 	if (*errnum < 0)
83 		return;
84 	*errnum = 0;
85 	if (dbhtmp != NULL) {
86 		(dbhtmp->close) (dbhtmp);
87 		unlink(template);
88 	}
89 
90 	name_set = 1;
91 	e2(inputt, errnum);
92 
93 	*errnum = 1;
94 }
95 
96 /*
97  * This is pulled out of e.c to make the "simulated 'e'" at startup easier to
98  * handle.
99  */
100 void
101 e2(inputt, errnum)
102 	FILE *inputt;
103 	int *errnum;
104 {
105 	RECNOINFO l_dbaccess;
106 
107 	if (template == NULL) {
108 		template = (char *) calloc(FILENAME_LEN, sizeof(char));
109 		if (template == NULL)
110 			ed_exit(4);
111 	}
112 	/* create the buffer using the method favored at compile time */
113 	bcopy("/tmp/_4.4bsd_ed_XXXXXX\0", template, 22);
114 	mktemp(template);
115 	(l_dbaccess.bval) = (u_char) '\0';
116 	(l_dbaccess.cachesize) = 0;
117 	(l_dbaccess.flags) = R_NOKEY;
118 	(l_dbaccess.lorder) = 0;
119 	(l_dbaccess.reclen) = 0;
120 	dbhtmp = dbopen(template, O_CREAT | O_RDWR,
121 	    S_IRUSR | S_IWUSR, (DBTYPE) DB_RECNO, &l_dbaccess);
122 	current = top;
123 	start = top;
124 	End = bottom;
125 
126 	if (sigint_flag)
127 		SIGINT_ACTION;
128 	if (name_set) {
129 		/* So 'r' knows the filename is already read in. */
130 		filename_flag = 1;
131 		r(inputt, errnum);
132 	}
133 	change_flag = 0;
134 }
135