corrections for echoing the last command when "!!" is given,
[unix-history] / usr / src / contrib / ed / e.c
CommitLineData
0fcca29a
KB
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
afdef93a 12static char sccsid[] = "@(#)e.c 5.6 (Berkeley) %G%";
0fcca29a
KB
13#endif /* not lint */
14
ecbf4ad0
KB
15#include <sys/types.h>
16#include <sys/stat.h>
17
ecbf4ad0 18#include <fcntl.h>
e692f66f 19#include <limits.h>
ecbf4ad0
KB
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
e692f66f
KB
27#ifdef DBI
28#include <db.h>
29#endif
30
0fcca29a 31#include "ed.h"
ecbf4ad0 32#include "extern.h"
0fcca29a
KB
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
41void
42e(inputt, errnum)
ecbf4ad0
KB
43 FILE *inputt;
44 int *errnum;
0fcca29a 45{
ecbf4ad0
KB
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);
ecbf4ad0 52 if (*errnum == 1) {
e692f66f 53 sigspecial++;
ecbf4ad0
KB
54 free(filename_current);
55 filename_current = l_temp;
e692f66f
KB
56 sigspecial--;
57 if (sigint_flag && (!sigspecial))
58 SIGINT_ACTION;
ecbf4ad0
KB
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 }
035e94f8 75 Start = top;
ecbf4ad0 76 End = bottom;
035e94f8
RC
77 Start_default = End_default = 0;
78 if (Start == NULL && bottom == NULL);
ecbf4ad0
KB
79 else {
80 ungetc(ss, inputt);
81 d(inputt, errnum); /* delete the whole buffer */
82 }
ecbf4ad0
KB
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;
e692f66f
KB
89#ifdef STDIO
90 if (fhtmp > NULL) {
91 fclose(fhtmp);
92 unlink(template);
93 }
94#endif
95#ifdef DBI
ecbf4ad0
KB
96 if (dbhtmp != NULL) {
97 (dbhtmp->close) (dbhtmp);
98 unlink(template);
99 }
e692f66f 100#endif
ecbf4ad0
KB
101
102 name_set = 1;
103 e2(inputt, errnum);
104
105 *errnum = 1;
106}
0fcca29a 107
ecbf4ad0
KB
108/*
109 * This is pulled out of e.c to make the "simulated 'e'" at startup easier to
110 * handle.
111 */
0fcca29a
KB
112void
113e2(inputt, errnum)
ecbf4ad0
KB
114 FILE *inputt;
115 int *errnum;
0fcca29a 116{
e692f66f
KB
117 char *tmp_path;
118#ifdef DBI
ecbf4ad0 119 RECNOINFO l_dbaccess;
e692f66f 120#endif
ecbf4ad0 121
e692f66f
KB
122 sigspecial++;
123#ifndef MEMORY
ecbf4ad0
KB
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 */
e692f66f
KB
130 tmp_path = getenv("TMPDIR");
131 sprintf(template, "%s/_4.4bsd_ed_XXXXXX", tmp_path ? tmp_path : "/tmp");
ecbf4ad0 132 mktemp(template);
e692f66f
KB
133#endif
134#ifdef STDIO
135 fhtmp = fopen(template, "w+");
136 if (fhtmp == NULL) {
137 ed_exit(5); /* unable to create buffer */
138 }
2718f34d 139 fwrite("R", sizeof(char), 1, fhtmp);
e692f66f
KB
140 file_seek = 0;
141#endif
142#ifdef DBI
afdef93a 143/*
ecbf4ad0
KB
144 (l_dbaccess.bval) = (u_char) '\0';
145 (l_dbaccess.cachesize) = 0;
146 (l_dbaccess.flags) = R_NOKEY;
147 (l_dbaccess.lorder) = 0;
148 (l_dbaccess.reclen) = 0;
149 dbhtmp = dbopen(template, O_CREAT | O_RDWR,
150 S_IRUSR | S_IWUSR, (DBTYPE) DB_RECNO, &l_dbaccess);
afdef93a
KB
151*/
152 dbhtmp = dbopen(template, O_CREAT | O_RDWR,
153 S_IRUSR | S_IWUSR, (DBTYPE) DB_RECNO, NULL);
e692f66f
KB
154 if (dbhtmp == NULL) {
155 ed_exit(5); /* unable to create buffer */
156 }
157#endif
ecbf4ad0 158 current = top;
035e94f8 159 Start = top;
ecbf4ad0
KB
160 End = bottom;
161
e692f66f
KB
162 sigspecial--;
163 if (sigint_flag &&(!sigspecial))
ecbf4ad0 164 SIGINT_ACTION;
e692f66f
KB
165
166 change_flag = 1;
ecbf4ad0
KB
167 if (name_set) {
168 /* So 'r' knows the filename is already read in. */
169 filename_flag = 1;
170 r(inputt, errnum);
afdef93a
KB
171 gut_num = line_number(bottom) + 512;
172 if (gut == NULL) {
173 gut = malloc(sizeof(LINE **) * gut_num);
174 if (gut == NULL) {
175 *errnum = -1;
176 strcpy(help_msg, "out of memory error");
177 return;
178 }
179 }
ecbf4ad0
KB
180 }
181 change_flag = 0;
182}