buf.c revision 1.1 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 line_t line0;
53
54 FILE *sfp; /* scratch file pointer */
55 off_t sfseek; /* scratch file position */
56 int seek_write; /* seek before writing */
57
58 /* gettxt: get a line of text from the scratch file; return pointer
59 to the text */
60 char *
61 gettxt(lp)
62 line_t *lp;
63 {
64 static char txtbuf[MAXLINE];
65 int len, ct;
66
67 if (lp == &line0)
68 return NULL;
69 seek_write = 1; /* force seek on write */
70 /* out of position */
71 if (sfseek != lp->seek) {
72 sfseek = lp->seek;
73 if (fseek(sfp, sfseek, SEEK_SET) < 0) {
74 fprintf(stderr, "cannot seek temp file\n");
75 return (char *) ERR;
76 }
77 }
78 len = lp->len & ~ACTV;
79 if ((ct = fread(txtbuf, sizeof(char), len, sfp)) < 0 || ct != len) {
80 fprintf(stderr, "cannot read temp file\n");
81 return (char *) ERR;
82 }
83 sfseek += len; /* update file position */
84 txtbuf[len] = '\0';
85 return txtbuf;
86 }
87
88
89 extern long curln;
90 extern long lastln;
91
92 /* puttxt: write a line of text to the scratch file and add a line node
93 to the editor buffer; return a pointer to the end of the text */
94 char *
95 puttxt(cs)
96 char *cs;
97 {
98 line_t *lp;
99 int len, ct;
100 char *s;
101
102 if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
103 fprintf(stderr, "out of memory\n");
104 return (char *) ERR;
105 }
106 /* assert: cs is '\n' terminated */
107 for (s = cs; *s != '\n'; s++)
108 ;
109 len = (s - cs) & ~ACTV;
110 /* out of position */
111 if (seek_write) {
112 if (fseek(sfp, 0L, SEEK_END) < 0) {
113 fprintf(stderr, "cannot seek temp file\n");
114 return (char *) ERR;
115 }
116 sfseek = ftell(sfp);
117 seek_write = 0;
118 }
119 /* assert: spl1() */
120 if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
121 sfseek = -1;
122 fprintf(stderr, "cannot write temp file\n");
123 return (char *) ERR;
124 }
125 lp->len = len;
126 lp->seek = sfseek;
127 lpqueue(lp);
128 sfseek += len; /* update file position */
129 return (*++s == '\0') ? NULL : s;
130 }
131
132
133 /* lpqueue: add a line node in the editor buffer after the current line */
134 void
135 lpqueue(lp)
136 line_t *lp;
137 {
138 line_t *cp;
139
140 cp = getptr(curln); /* this getptr last! */
141 insqueue(lp, cp);
142 lastln++;
143 curln++;
144 }
145
146
147 extern int mutex;
148 extern int sigflags;
149
150 /* getptr: return pointer to a line node in the editor buffer */
151 line_t *
152 getptr(n)
153 long n;
154 {
155 static line_t *lp = &line0;
156 static long on = 0;
157
158 spl1();
159 if (n > on)
160 if (n <= (on + lastln) >> 1) {
161 for (; on < n; on++)
162 lp = lp->next;
163 } else {
164 lp = line0.prev;
165 for (on = lastln; on > n; on--)
166 lp = lp->prev;
167 }
168 else
169 if (n >= on >> 1) {
170 for (; on > n; on--)
171 lp = lp->prev;
172 } else {
173 lp = &line0;
174 for (on = 0; on < n; on++)
175 lp = lp->next;
176 }
177 spl0();
178 return lp;
179 }
180
181
182 char sfn[15] = ""; /* scratch file name */
183
184 /* sbopen: open scratch file */
185 sbopen()
186 {
187 strcpy(sfn, "/tmp/ed.XXXXXX");
188 if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
189 fprintf(stderr, "cannot open temp file\n");
190 return ERR;
191 }
192 return 0;
193 }
194
195 /* sbclose: close scratch file */
196 sbclose()
197 {
198 if (sfp) {
199 unlink(sfn);
200 fclose(sfp);
201 sfp = NULL;
202 }
203 sfseek = seek_write = 0;
204 }
205
206
207 /* quit: remove scratch file and exit */
208 void
209 quit(n)
210 int n;
211 {
212 if (sfp) {
213 unlink(sfn);
214 fclose(sfp);
215 }
216 exit(n);
217 }
218