buf.c revision 1.4 1 /* buf.c: This file contains the scratch-file buffer rountines for the
2 ed line editor. */
3 /*-
4 * Copyright (c) 1992 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rodney Ruddock of the University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifndef lint
40 static char sccsid[] = "@(#)buf.c 5.5 (Berkeley) 3/28/93";
41 #endif /* not lint */
42
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/file.h>
48 #include <unistd.h>
49
50 #include "ed.h"
51
52 extern char errmsg[];
53 extern line_t line0;
54
55 FILE *sfp; /* scratch file pointer */
56 off_t sfseek; /* scratch file position */
57 int seek_write; /* seek before writing */
58
59 /* gettxt: get a line of text from the scratch file; return pointer
60 to the text */
61 char *
62 gettxt(lp)
63 line_t *lp;
64 {
65 static char txtbuf[MAXLINE];
66 int len, ct;
67
68 if (lp == &line0)
69 return NULL;
70 seek_write = 1; /* force seek on write */
71 /* out of position */
72 if (sfseek != lp->seek) {
73 sfseek = lp->seek;
74 if (fseek(sfp, sfseek, SEEK_SET) < 0) {
75 fprintf(stderr, "%s\n", strerror(errno));
76 sprintf(errmsg, "cannot seek temp file");
77 return (char *) ERR;
78 }
79 }
80 len = lp->len & ~ACTV;
81 if ((ct = fread(txtbuf, sizeof(char), len, sfp)) < 0 || ct != len) {
82 fprintf(stderr, "%s\n", strerror(errno));
83 sprintf(errmsg, "cannot read temp file");
84 return (char *) ERR;
85 }
86 sfseek += len; /* update file position */
87 txtbuf[len] = '\0';
88 return txtbuf;
89 }
90
91
92 extern long curln;
93 extern long lastln;
94
95 /* puttxt: write a line of text to the scratch file and add a line node
96 to the editor buffer; return a pointer to the end of the text */
97 char *
98 puttxt(cs)
99 char *cs;
100 {
101 line_t *lp;
102 int len, ct;
103 char *s;
104
105 if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
106 fprintf(stderr, "%s\n", strerror(errno));
107 sprintf(errmsg, "out of memory");
108 return (char *) ERR;
109 }
110 /* assert: cs is '\n' terminated */
111 for (s = cs; *s != '\n'; s++)
112 ;
113 len = (s - cs) & ~ACTV;
114 /* out of position */
115 if (seek_write) {
116 if (fseek(sfp, 0L, SEEK_END) < 0) {
117 fprintf(stderr, "%s\n", strerror(errno));
118 sprintf(errmsg, "cannot seek temp file");
119 return (char *) ERR;
120 }
121 sfseek = ftell(sfp);
122 seek_write = 0;
123 }
124 /* assert: spl1() */
125 if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
126 sfseek = -1;
127 fprintf(stderr, "%s\n", strerror(errno));
128 sprintf(errmsg, "cannot write temp file");
129 return (char *) ERR;
130 }
131 lp->len = len;
132 lp->seek = sfseek;
133 lpqueue(lp);
134 sfseek += len; /* update file position */
135 return (*++s == '\0') ? NULL : s;
136 }
137
138
139 /* lpqueue: add a line node in the editor buffer after the current line */
140 void
141 lpqueue(lp)
142 line_t *lp;
143 {
144 line_t *cp;
145
146 cp = getptr(curln); /* this getptr last! */
147 insqueue(lp, cp);
148 lastln++;
149 curln++;
150 }
151
152
153 /* getaddr: return line number of pointer */
154 long
155 getaddr(lp)
156 line_t *lp;
157 {
158 line_t *cp = &line0;
159 long n = 0;
160
161 while (cp != lp && (cp = cp->next) != &line0)
162 n++;
163 return (cp != &line0) ? n : 0;
164 }
165
166
167 extern int mutex;
168 extern int sigflags;
169
170 /* getptr: return pointer to a line node in the editor buffer */
171 line_t *
172 getptr(n)
173 long n;
174 {
175 static line_t *lp = &line0;
176 static long on = 0;
177
178 spl1();
179 if (n > on)
180 if (n <= (on + lastln) >> 1)
181 for (; on < n; on++)
182 lp = lp->next;
183 else {
184 lp = line0.prev;
185 for (on = lastln; on > n; on--)
186 lp = lp->prev;
187 }
188 else
189 if (n >= on >> 1)
190 for (; on > n; on--)
191 lp = lp->prev;
192 else {
193 lp = &line0;
194 for (on = 0; on < n; on++)
195 lp = lp->next;
196 }
197 spl0();
198 return lp;
199 }
200
201
202 char sfn[15] = ""; /* scratch file name */
203
204 /* sbopen: open scratch file */
205 sbopen()
206 {
207 strcpy(sfn, "/tmp/ed.XXXXXX");
208 if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
209 fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
210 sprintf(errmsg, "cannot open temp file");
211 return ERR;
212 }
213 return 0;
214 }
215
216
217 /* sbclose: close scratch file */
218 sbclose()
219 {
220 if (sfp) {
221 if (fclose(sfp) < 0) {
222 fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
223 sprintf(errmsg, "cannot close temp file");
224 return ERR;
225 }
226 sfp = NULL;
227 unlink(sfn);
228 }
229 sfseek = seek_write = 0;
230 return 0;
231 }
232
233
234 /* quit: remove scratch file and exit */
235 void
236 quit(n)
237 int n;
238 {
239 if (sfp) {
240 unlink(sfn);
241 fclose(sfp);
242 }
243 exit(n);
244 }
245