xref: /openbsd/bin/ed/buf.c (revision 7b36286a)
1 /*	$OpenBSD: buf.c,v 1.18 2008/02/12 07:57:29 otto Exp $	*/
2 /*	$NetBSD: buf.c,v 1.15 1995/04/23 10:07:28 cgd Exp $	*/
3 
4 /* buf.c: This file contains the scratch-file buffer routines for the
5    ed line editor. */
6 /*-
7  * Copyright (c) 1993 Andrew Moore, Talke Studio.
8  * All rights reserved.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 #if 0
34 static char *rcsid = "@(#)buf.c,v 1.4 1994/02/01 00:34:35 alm Exp";
35 #else
36 static char rcsid[] = "$OpenBSD: buf.c,v 1.18 2008/02/12 07:57:29 otto Exp $";
37 #endif
38 #endif /* not lint */
39 
40 #include <sys/file.h>
41 #include <sys/stat.h>
42 
43 #include "ed.h"
44 
45 
46 FILE *sfp;				/* scratch file pointer */
47 off_t sfseek;				/* scratch file position */
48 int seek_write;				/* seek before writing */
49 line_t buffer_head;			/* incore buffer */
50 
51 /* get_sbuf_line: get a line of text from the scratch file; return pointer
52    to the text */
53 char *
54 get_sbuf_line(line_t *lp)
55 {
56 	static char *sfbuf = NULL;	/* buffer */
57 	static int sfbufsz = 0;		/* buffer size */
58 
59 	int len, ct;
60 
61 	if (lp == &buffer_head)
62 		return NULL;
63 	seek_write = 1;				/* force seek on write */
64 	/* out of position */
65 	if (sfseek != lp->seek) {
66 		sfseek = lp->seek;
67 		if (fseeko(sfp, sfseek, SEEK_SET) < 0) {
68 			perror(NULL);
69 			seterrmsg("cannot seek temp file");
70 			return NULL;
71 		}
72 	}
73 	len = lp->len;
74 	REALLOC(sfbuf, sfbufsz, len + 1, NULL);
75 	if ((ct = fread(sfbuf, sizeof(char), len, sfp)) <  0 || ct != len) {
76 		perror(NULL);
77 		seterrmsg("cannot read temp file");
78 		return NULL;
79 	}
80 	sfseek += len;				/* update file position */
81 	sfbuf[len] = '\0';
82 	return sfbuf;
83 }
84 
85 
86 /* put_sbuf_line: write a line of text to the scratch file and add a line node
87    to the editor buffer;  return a pointer to the end of the text */
88 char *
89 put_sbuf_line(char *cs)
90 {
91 	line_t *lp;
92 	int len, ct;
93 	char *s;
94 
95 	if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
96 		perror(NULL);
97 		seterrmsg("out of memory");
98 		return NULL;
99 	}
100 	/* assert: cs is '\n' terminated */
101 	for (s = cs; *s != '\n'; s++)
102 		;
103 	if (s - cs >= LINECHARS) {
104 		seterrmsg("line too long");
105 		free(lp);
106 		return NULL;
107 	}
108 	len = s - cs;
109 	/* out of position */
110 	if (seek_write) {
111 		if (fseek(sfp, 0L, SEEK_END) < 0) {
112 			perror(NULL);
113 			seterrmsg("cannot seek temp file");
114 			free(lp);
115 			return NULL;
116 		}
117 		sfseek = ftello(sfp);
118 		seek_write = 0;
119 	}
120 	/* assert: SPL1() */
121 	if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
122 		sfseek = -1;
123 		perror(NULL);
124 		seterrmsg("cannot write temp file");
125 		free(lp);
126 		return NULL;
127 	}
128 	lp->len = len;
129 	lp->seek  = sfseek;
130 	add_line_node(lp);
131 	sfseek += len;			/* update file position */
132 	return ++s;
133 }
134 
135 
136 /* add_line_node: add a line node in the editor buffer after the current line */
137 void
138 add_line_node(line_t *lp)
139 {
140 	line_t *cp;
141 
142 	/* this get_addressed_line_node last! */
143 	cp = get_addressed_line_node(current_addr);
144 	INSQUE(lp, cp);
145 	addr_last++;
146 	current_addr++;
147 }
148 
149 
150 /* get_line_node_addr: return line number of pointer */
151 int
152 get_line_node_addr(line_t *lp)
153 {
154 	line_t *cp = &buffer_head;
155 	int n = 0;
156 
157 	while (cp != lp && (cp = cp->q_forw) != &buffer_head)
158 		n++;
159 	if (n && cp == &buffer_head) {
160 		seterrmsg("invalid address");
161 		return ERR;
162 	 }
163 	 return n;
164 }
165 
166 
167 /* get_addressed_line_node: return pointer to a line node in the editor buffer */
168 line_t *
169 get_addressed_line_node(int n)
170 {
171 	static line_t *lp = &buffer_head;
172 	static int on = 0;
173 
174 	SPL1();
175 	if (n > on) {
176 		if (n <= (on + addr_last) >> 1)
177 			for (; on < n; on++)
178 				lp = lp->q_forw;
179 		else {
180 			lp = buffer_head.q_back;
181 			for (on = addr_last; on > n; on--)
182 				lp = lp->q_back;
183 		}
184 	} else {
185 		if (n >= on >> 1)
186 			for (; on > n; on--)
187 				lp = lp->q_back;
188 		else {
189 			lp = &buffer_head;
190 			for (on = 0; on < n; on++)
191 				lp = lp->q_forw;
192 		}
193 	}
194 	SPL0();
195 	return lp;
196 }
197 
198 
199 extern int newline_added;
200 
201 #define SCRATCH_TEMPLATE      "/tmp/ed.XXXXXXXXXX"
202 char	sfn[sizeof(SCRATCH_TEMPLATE)+1] = "";	/* scratch file name */
203 
204 /* open_sbuf: open scratch file */
205 int
206 open_sbuf(void)
207 {
208 	int fd = -1;
209 
210 	isbinary = newline_added = 0;
211 	strlcpy(sfn, SCRATCH_TEMPLATE, sizeof sfn);
212 	if ((fd = mkstemp(sfn)) == -1 ||
213 	    (sfp = fdopen(fd, "w+")) == NULL) {
214 		if (fd != -1)
215 			close(fd);
216 		perror(sfn);
217 		seterrmsg("cannot open temp file");
218 		return ERR;
219 	}
220 	return 0;
221 }
222 
223 
224 /* close_sbuf: close scratch file */
225 int
226 close_sbuf(void)
227 {
228 	if (sfp) {
229 		if (fclose(sfp) < 0) {
230 			perror(sfn);
231 			seterrmsg("cannot close temp file");
232 			return ERR;
233 		}
234 		sfp = NULL;
235 		unlink(sfn);
236 	}
237 	sfseek = seek_write = 0;
238 	return 0;
239 }
240 
241 
242 /* quit: remove_lines scratch file and exit */
243 void
244 quit(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_buffers: open scratch buffer; initialize line queue */
257 void
258 init_buffers(void)
259 {
260 	int i = 0;
261 
262 	/* Read stdin one character at a time to avoid i/o contention
263 	   with shell escapes invoked by nonterminal input, e.g.,
264 	   ed - <<EOF
265 	   !cat
266 	   hello, world
267 	   EOF */
268 	setbuffer(stdin, stdinbuf, 1);
269 	if (open_sbuf() < 0)
270 		quit(2);
271 	REQUE(&buffer_head, &buffer_head);
272 	for (i = 0; i < 256; i++)
273 		ctab[i] = i;
274 }
275 
276 
277 /* translit_text: translate characters in a string */
278 char *
279 translit_text(char *s, int len, int from, int to)
280 {
281 	static int i = 0;
282 
283 	unsigned char *us;
284 
285 	ctab[i] = i;			/* restore table to initial state */
286 	ctab[i = from] = to;
287 	for (us = (unsigned char *) s; len-- > 0; us++)
288 		*us = ctab[*us];
289 	return s;
290 }
291