1 /*	$OpenBSD: buf.c,v 1.19 2009/10/27 23:59:21 deraadt 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 #include <sys/file.h>
33 #include <sys/stat.h>
34 
35 #include "ed.h"
36 
37 __RCSID("$MirOS: src/bin/ed/buf.c,v 1.4 2012/01/04 21:38:46 tg Exp $");
38 
39 FILE *sfp;				/* scratch file pointer */
40 tp_ftell sfpos;				/* scratch file position */
41 int seek_write;				/* seek before writing */
42 line_t buffer_head;			/* incore buffer */
43 
44 /* get_sbuf_line: get a line of text from the scratch file; return pointer
45    to the text */
46 char *
get_sbuf_line(line_t * lp)47 get_sbuf_line(line_t *lp)
48 {
49 	static char *sfbuf = NULL;	/* buffer */
50 	static int sfbufsz = 0;		/* buffer size */
51 
52 	int len, ct;
53 
54 	if (lp == &buffer_head)
55 		return NULL;
56 	seek_write = 1;				/* force seek on write */
57 	/* out of position */
58 	if (sfpos != lp->adr) {
59 		sfpos = lp->adr;
60 		if (do_fseek(sfp, sfpos, SEEK_SET) < 0) {
61 			perror(NULL);
62 			seterrmsg("cannot seek temp file");
63 			return NULL;
64 		}
65 	}
66 	len = lp->len;
67 	REALLOC(sfbuf, sfbufsz, len + 1, NULL);
68 	if ((ct = fread(sfbuf, sizeof(char), len, sfp)) <  0 || ct != len) {
69 		perror(NULL);
70 		seterrmsg("cannot read temp file");
71 		return NULL;
72 	}
73 	sfpos += len;				/* update file position */
74 	sfbuf[len] = '\0';
75 	return sfbuf;
76 }
77 
78 
79 /* put_sbuf_line: write a line of text to the scratch file and add a line node
80    to the editor buffer;  return a pointer to the end of the text */
81 char *
put_sbuf_line(char * cs)82 put_sbuf_line(char *cs)
83 {
84 	line_t *lp;
85 	int len, ct;
86 	char *s;
87 
88 	if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
89 		perror(NULL);
90 		seterrmsg("out of memory");
91 		return NULL;
92 	}
93 	/* assert: cs is '\n' terminated */
94 	for (s = cs; *s != '\n'; s++)
95 		;
96 	if (s - cs >= LINECHARS) {
97 		seterrmsg("line too long");
98 		free(lp);
99 		return NULL;
100 	}
101 	len = s - cs;
102 	/* out of position */
103 	if (seek_write) {
104 		if (do_fseek(sfp, (tp_ftell)0, SEEK_END) < 0) {
105 			perror(NULL);
106 			seterrmsg("cannot seek temp file");
107 			free(lp);
108 			return NULL;
109 		}
110 		sfpos = do_ftell(sfp);
111 		seek_write = 0;
112 	}
113 	/* assert: SPL1() */
114 	if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
115 		sfpos = -1;
116 		perror(NULL);
117 		seterrmsg("cannot write temp file");
118 		free(lp);
119 		return NULL;
120 	}
121 	lp->len = len;
122 	lp->adr = sfpos;
123 	add_line_node(lp);
124 	sfpos += len;			/* update file position */
125 	return ++s;
126 }
127 
128 
129 /* add_line_node: add a line node in the editor buffer after the current line */
130 void
add_line_node(line_t * lp)131 add_line_node(line_t *lp)
132 {
133 	line_t *cp;
134 
135 	/* this get_addressed_line_node last! */
136 	cp = get_addressed_line_node(current_addr);
137 	INSQUE(lp, cp);
138 	addr_last++;
139 	current_addr++;
140 }
141 
142 
143 /* get_line_node_addr: return line number of pointer */
144 int
get_line_node_addr(line_t * lp)145 get_line_node_addr(line_t *lp)
146 {
147 	line_t *cp = &buffer_head;
148 	int n = 0;
149 
150 	while (cp != lp && (cp = cp->q_forw) != &buffer_head)
151 		n++;
152 	if (n && cp == &buffer_head) {
153 		seterrmsg("invalid address");
154 		return ERR;
155 	 }
156 	 return n;
157 }
158 
159 
160 /* get_addressed_line_node: return pointer to a line node in the editor buffer */
161 line_t *
get_addressed_line_node(int n)162 get_addressed_line_node(int n)
163 {
164 	static line_t *lp = &buffer_head;
165 	static int on = 0;
166 
167 	SPL1();
168 	if (n > on) {
169 		if (n <= (on + addr_last) >> 1)
170 			for (; on < n; on++)
171 				lp = lp->q_forw;
172 		else {
173 			lp = buffer_head.q_back;
174 			for (on = addr_last; on > n; on--)
175 				lp = lp->q_back;
176 		}
177 	} else {
178 		if (n >= on >> 1)
179 			for (; on > n; on--)
180 				lp = lp->q_back;
181 		else {
182 			lp = &buffer_head;
183 			for (on = 0; on < n; on++)
184 				lp = lp->q_forw;
185 		}
186 	}
187 	SPL0();
188 	return lp;
189 }
190 
191 
192 extern int newline_added;
193 
194 #define SCRATCH_TEMPLATE      "/tmp/ed.XXXXXXXXXX"
195 char	sfn[sizeof(SCRATCH_TEMPLATE)+1] = "";	/* scratch file name */
196 
197 /* open_sbuf: open scratch file */
198 int
open_sbuf(void)199 open_sbuf(void)
200 {
201 	int fd = -1;
202 
203 	isbinary = newline_added = 0;
204 	strlcpy(sfn, SCRATCH_TEMPLATE, sizeof sfn);
205 	if ((fd = mkstemp(sfn)) == -1 ||
206 	    (sfp = fdopen(fd, "w+")) == NULL) {
207 		if (fd != -1)
208 			close(fd);
209 		perror(sfn);
210 		seterrmsg("cannot open temp file");
211 		return ERR;
212 	}
213 	return 0;
214 }
215 
216 
217 /* close_sbuf: close scratch file */
218 int
close_sbuf(void)219 close_sbuf(void)
220 {
221 	if (sfp) {
222 		if (fclose(sfp) < 0) {
223 			perror(sfn);
224 			seterrmsg("cannot close temp file");
225 			return ERR;
226 		}
227 		sfp = NULL;
228 		unlink(sfn);
229 	}
230 	sfpos = seek_write = 0;
231 	return 0;
232 }
233 
234 
235 /* quit: remove_lines scratch file and exit */
236 void
quit(int n)237 quit(int n)
238 {
239 	if (sfp) {
240 		fclose(sfp);
241 		unlink(sfn);
242 	}
243 	exit(n);
244 }
245 
246 
247 unsigned char ctab[256];		/* character translation table */
248 
249 /* init_buffers: open scratch buffer; initialize line queue */
250 void
init_buffers(void)251 init_buffers(void)
252 {
253 	int i = 0;
254 
255 	/* Read stdin one character at a time to avoid i/o contention
256 	   with shell escapes invoked by nonterminal input, e.g.,
257 	   ed - <<EOF
258 	   !cat
259 	   hello, world
260 	   EOF */
261 #ifdef _IOFBF
262 	setvbuf(stdin, stdinbuf, _IOFBF, 1);
263 #else
264 	setbuffer(stdin, stdinbuf, 1);
265 #endif
266 	if (open_sbuf() < 0)
267 		quit(2);
268 	REQUE(&buffer_head, &buffer_head);
269 	for (i = 0; i < 256; i++)
270 		ctab[i] = i;
271 }
272 
273 
274 /* translit_text: translate characters in a string */
275 char *
translit_text(char * s,int len,int from,int to)276 translit_text(char *s, int len, int from, 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