buf.c revision 1.8 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[] = "from: @(#)buf.c 5.5 (Berkeley) 3/28/93";*/
41 static char rcsid[] = "$Id: buf.c,v 1.8 1993/08/01 18:59:50 mycroft Exp $";
42 #endif /* not lint */
43
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
54 FILE *sfp; /* scratch file pointer */
55 char *sfbuf = NULL; /* scratch file input buffer */
56 int sfbufsz = 0; /* scratch file input buffer size */
57 off_t sfseek; /* scratch file position */
58 int seek_write; /* seek before writing */
59 line_t line0; /* initial node of line queue */
60
61 /* gettxt: get a line of text from the scratch file; return pointer
62 to the text */
63 char *
64 gettxt(lp)
65 line_t *lp;
66 {
67 int len, ct;
68
69 if (lp == &line0)
70 return NULL;
71 seek_write = 1; /* force seek on write */
72 /* out of position */
73 if (sfseek != lp->seek) {
74 sfseek = lp->seek;
75 if (fseek(sfp, sfseek, SEEK_SET) < 0) {
76 fprintf(stderr, "%s\n", strerror(errno));
77 sprintf(errmsg, "cannot seek temp file");
78 return NULL;
79 }
80 }
81 len = lp->len & ~ACTV;
82 CKBUF(sfbuf, sfbufsz, len + 1, NULL);
83 if ((ct = fread(sfbuf, sizeof(char), len, sfp)) < 0 || ct != len) {
84 fprintf(stderr, "%s\n", strerror(errno));
85 sprintf(errmsg, "cannot read temp file");
86 return NULL;
87 }
88 sfseek += len; /* update file position */
89 sfbuf[len] = '\0';
90 return sfbuf;
91 }
92
93
94 extern long curln;
95 extern long lastln;
96
97 /* puttxt: write a line of text to the scratch file and add a line node
98 to the editor buffer; return a pointer to the end of the text */
99 char *
100 puttxt(cs)
101 char *cs;
102 {
103 line_t *lp;
104 int len, ct;
105 char *s;
106
107 if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
108 fprintf(stderr, "%s\n", strerror(errno));
109 sprintf(errmsg, "out of memory");
110 return NULL;
111 }
112 /* assert: cs is '\n' terminated */
113 for (s = cs; *s != '\n'; s++)
114 ;
115 if (s - cs >= LINECHARS) {
116 sprintf(errmsg, "line too long");
117 return NULL;
118 }
119 len = (s - cs) & ~ACTV;
120 /* out of position */
121 if (seek_write) {
122 if (fseek(sfp, 0L, SEEK_END) < 0) {
123 fprintf(stderr, "%s\n", strerror(errno));
124 sprintf(errmsg, "cannot seek temp file");
125 return NULL;
126 }
127 sfseek = ftell(sfp);
128 seek_write = 0;
129 }
130 /* assert: spl1() */
131 if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
132 sfseek = -1;
133 fprintf(stderr, "%s\n", strerror(errno));
134 sprintf(errmsg, "cannot write temp file");
135 return NULL;
136 }
137 lp->len = len;
138 lp->seek = sfseek;
139 lpqueue(lp);
140 sfseek += len; /* update file position */
141 return ++s;
142 }
143
144
145 /* lpqueue: add a line node in the editor buffer after the current line */
146 void
147 lpqueue(lp)
148 line_t *lp;
149 {
150 line_t *cp;
151
152 cp = getlp(curln); /* this getlp last! */
153 insqueue(lp, cp);
154 lastln++;
155 curln++;
156 }
157
158
159 /* getaddr: return line number of pointer */
160 long
161 getaddr(lp)
162 line_t *lp;
163 {
164 line_t *cp = &line0;
165 long n = 0;
166
167 while (cp != lp && (cp = cp->next) != &line0)
168 n++;
169 if (n && cp == &line0) {
170 sprintf(errmsg, "invalid address");
171 return ERR;
172 }
173 return n;
174 }
175
176
177 /* getlp: return pointer to a line node in the editor buffer */
178 line_t *
179 getlp(n)
180 long n;
181 {
182 static line_t *lp = &line0;
183 static long on = 0;
184
185 spl1();
186 if (n > on)
187 if (n <= (on + lastln) >> 1)
188 for (; on < n; on++)
189 lp = lp->next;
190 else {
191 lp = line0.prev;
192 for (on = lastln; on > n; on--)
193 lp = lp->prev;
194 }
195 else
196 if (n >= on >> 1)
197 for (; on > n; on--)
198 lp = lp->prev;
199 else {
200 lp = &line0;
201 for (on = 0; on < n; on++)
202 lp = lp->next;
203 }
204 spl0();
205 return lp;
206 }
207
208
209 char sfn[15] = ""; /* scratch file name */
210
211 /* sbopen: open scratch file */
212 sbopen()
213 {
214 strcpy(sfn, "/tmp/ed.XXXXXX");
215 if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
216 fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
217 sprintf(errmsg, "cannot open temp file");
218 return ERR;
219 }
220 return 0;
221 }
222
223
224 /* sbclose: close scratch file */
225 sbclose()
226 {
227 if (sfp) {
228 if (fclose(sfp) < 0) {
229 fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
230 sprintf(errmsg, "cannot close temp file");
231 return ERR;
232 }
233 sfp = NULL;
234 unlink(sfn);
235 }
236 sfseek = seek_write = 0;
237 return 0;
238 }
239
240
241 /* quit: remove scratch file and exit */
242 void
243 quit(n)
244 int n;
245 {
246 if (sfp) {
247 fclose(sfp);
248 unlink(sfn);
249 }
250 exit(n);
251 }
252
253
254 unsigned char ctab[256]; /* character translation table */
255
256 /* init_buf: open scratch buffer; initialize line queue */
257 void
258 init_buf()
259 {
260 int i = 0;
261
262 if (sbopen() < 0)
263 quit(2);
264 requeue(&line0, &line0);
265 for (i = 0; i < 256; i++)
266 ctab[i] = i;
267 }
268
269
270 /* translit: translate characters in a string */
271 char *
272 translit(s, len, from, to)
273 char *s;
274 int len;
275 int from;
276 int to;
277 {
278 static int i = 0;
279
280 unsigned char *us;
281
282 ctab[i] = i; /* restore table to initial state */
283 ctab[i = from] = to;
284 for (us = (unsigned char *) s; len-- > 0; us++)
285 *us = ctab[*us];
286 return s;
287 }
288
289
290