buf.c revision 1.7 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
53 1.1 cgd FILE *sfp; /* scratch file pointer */
54 1.6 alm char *sfbuf = NULL; /* scratch file input buffer */
55 1.6 alm int sfbufsz = 0; /* scratch file input buffer size */
56 1.1 cgd off_t sfseek; /* scratch file position */
57 1.1 cgd int seek_write; /* seek before writing */
58 1.7 alm line_t line0; /* initial node of line queue */
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.7 alm if (n && cp == &line0) {
169 1.7 alm sprintf(errmsg, "invalid address");
170 1.7 alm return ERR;
171 1.7 alm }
172 1.7 alm return n;
173 1.3 alm }
174 1.3 alm
175 1.3 alm
176 1.6 alm /* getlp: return pointer to a line node in the editor buffer */
177 1.1 cgd line_t *
178 1.6 alm getlp(n)
179 1.1 cgd long n;
180 1.1 cgd {
181 1.1 cgd static line_t *lp = &line0;
182 1.1 cgd static long on = 0;
183 1.1 cgd
184 1.1 cgd spl1();
185 1.1 cgd if (n > on)
186 1.2 cgd if (n <= (on + lastln) >> 1)
187 1.1 cgd for (; on < n; on++)
188 1.1 cgd lp = lp->next;
189 1.2 cgd else {
190 1.1 cgd lp = line0.prev;
191 1.1 cgd for (on = lastln; on > n; on--)
192 1.1 cgd lp = lp->prev;
193 1.1 cgd }
194 1.1 cgd else
195 1.2 cgd if (n >= on >> 1)
196 1.1 cgd for (; on > n; on--)
197 1.1 cgd lp = lp->prev;
198 1.2 cgd else {
199 1.1 cgd lp = &line0;
200 1.1 cgd for (on = 0; on < n; on++)
201 1.1 cgd lp = lp->next;
202 1.1 cgd }
203 1.1 cgd spl0();
204 1.1 cgd return lp;
205 1.1 cgd }
206 1.1 cgd
207 1.1 cgd
208 1.1 cgd char sfn[15] = ""; /* scratch file name */
209 1.1 cgd
210 1.1 cgd /* sbopen: open scratch file */
211 1.1 cgd sbopen()
212 1.1 cgd {
213 1.1 cgd strcpy(sfn, "/tmp/ed.XXXXXX");
214 1.1 cgd if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
215 1.3 alm fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
216 1.2 cgd sprintf(errmsg, "cannot open temp file");
217 1.1 cgd return ERR;
218 1.1 cgd }
219 1.1 cgd return 0;
220 1.1 cgd }
221 1.1 cgd
222 1.2 cgd
223 1.1 cgd /* sbclose: close scratch file */
224 1.1 cgd sbclose()
225 1.1 cgd {
226 1.1 cgd if (sfp) {
227 1.3 alm if (fclose(sfp) < 0) {
228 1.3 alm fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
229 1.3 alm sprintf(errmsg, "cannot close temp file");
230 1.3 alm return ERR;
231 1.3 alm }
232 1.1 cgd sfp = NULL;
233 1.2 cgd unlink(sfn);
234 1.1 cgd }
235 1.1 cgd sfseek = seek_write = 0;
236 1.4 alm return 0;
237 1.1 cgd }
238 1.1 cgd
239 1.1 cgd
240 1.1 cgd /* quit: remove scratch file and exit */
241 1.1 cgd void
242 1.1 cgd quit(n)
243 1.1 cgd int n;
244 1.1 cgd {
245 1.1 cgd if (sfp) {
246 1.6 alm fclose(sfp);
247 1.1 cgd unlink(sfn);
248 1.1 cgd }
249 1.1 cgd exit(n);
250 1.1 cgd }
251 1.7 alm
252 1.7 alm
253 1.7 alm unsigned char ctab[256]; /* character translation table */
254 1.7 alm
255 1.7 alm /* init_buf: open scratch buffer; initialize line queue */
256 1.7 alm void
257 1.7 alm init_buf()
258 1.7 alm {
259 1.7 alm int i = 0;
260 1.7 alm
261 1.7 alm if (sbopen() < 0)
262 1.7 alm quit(2);
263 1.7 alm requeue(&line0, &line0);
264 1.7 alm for (i = 0; i < 256; i++)
265 1.7 alm ctab[i] = i;
266 1.7 alm }
267 1.7 alm
268 1.7 alm
269 1.7 alm /* translit: translate characters in a string */
270 1.7 alm char *
271 1.7 alm translit(s, len, from, to)
272 1.7 alm char *s;
273 1.7 alm int len;
274 1.7 alm int from;
275 1.7 alm int to;
276 1.7 alm {
277 1.7 alm static int i = 0;
278 1.7 alm
279 1.7 alm unsigned char *us;
280 1.7 alm
281 1.7 alm ctab[i] = i; /* restore table to initial state */
282 1.7 alm ctab[i = from] = to;
283 1.7 alm for (us = (unsigned char *) s; len-- > 0; us++)
284 1.7 alm *us = ctab[*us];
285 1.7 alm return s;
286 1.7 alm }
287 1.7 alm
288 1.7 alm
289