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