xref: /openbsd-src/bin/ed/io.c (revision 2bde29b351c80904b1c32bd5386a056013b27f45)
1 /*	$OpenBSD: io.c,v 1.26 2024/07/16 05:01:10 deraadt Exp $	*/
2 /*	$NetBSD: io.c,v 1.2 1995/03/21 09:04:43 cgd Exp $	*/
3 
4 /* io.c: This file contains the i/o routines for the ed line editor */
5 /*-
6  * Copyright (c) 1993 Andrew Moore, Talke Studio.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <regex.h>
32 #include <signal.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #include "ed.h"
39 
40 static int read_stream(FILE *, int);
41 static int get_stream_line(FILE *);
42 static int write_stream(FILE *, int, int);
43 static int put_stream_line(FILE *, char *, int);
44 
45 extern int scripted;
46 
47 /* read_file: read a named file/pipe into the buffer; return line count */
48 int
read_file(char * fn,int n)49 read_file(char *fn, int n)
50 {
51 	FILE *fp;
52 	int size;
53 
54 
55 	fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r");
56 	if (fp == NULL) {
57 		perror(fn);
58 		seterrmsg("cannot open input file");
59 		return ERR;
60 	} else if ((size = read_stream(fp, n)) < 0)
61 		return ERR;
62 	 else if ((*fn == '!') ?  pclose(fp) == -1 : fclose(fp) == EOF) {
63 		perror(fn);
64 		seterrmsg("cannot close input file");
65 		return ERR;
66 	}
67 	if (!scripted)
68 		printf("%d\n", size);
69 	return current_addr - n;
70 }
71 
72 
73 static char *sbuf;		/* file i/o buffer */
74 static int sbufsz;		/* file i/o buffer size */
75 int newline_added;		/* if set, newline appended to input file */
76 
77 /* read_stream: read a stream into the editor buffer; return status */
78 static int
read_stream(FILE * fp,int n)79 read_stream(FILE *fp, int n)
80 {
81 	line_t *lp = get_addressed_line_node(n);
82 	undo_t *up = NULL;
83 	unsigned int size = 0;
84 	int o_newline_added = newline_added;
85 	int o_isbinary = isbinary;
86 	int appended = (n == addr_last);
87 	int len;
88 
89 	isbinary = newline_added = 0;
90 	for (current_addr = n; (len = get_stream_line(fp)) > 0; size += len) {
91 		SPL1();
92 		if (put_sbuf_line(sbuf) == NULL) {
93 			SPL0();
94 			return ERR;
95 		}
96 		lp = lp->q_forw;
97 		if (up)
98 			up->t = lp;
99 		else if ((up = push_undo_stack(UADD, current_addr,
100 		    current_addr)) == NULL) {
101 			SPL0();
102 			return ERR;
103 		}
104 		SPL0();
105 	}
106 	if (len < 0)
107 		return ERR;
108 	if (appended && size && o_isbinary && o_newline_added)
109 		fputs("newline inserted\n", stderr);
110 	else if (newline_added && (!appended || (!isbinary && !o_isbinary)))
111 		fputs("newline appended\n", stderr);
112 	if (isbinary && newline_added && !appended)
113 	    	size += 1;
114 	if (!size)
115 		newline_added = 1;
116 	newline_added = appended ? newline_added : o_newline_added;
117 	isbinary = isbinary | o_isbinary;
118 	return size;
119 }
120 
121 /* get_stream_line: read a line of text from a stream; return line length */
122 static int
get_stream_line(FILE * fp)123 get_stream_line(FILE *fp)
124 {
125 	int c;
126 	int i = 0;
127 
128 	while (((c = getc(fp)) != EOF || (!feof(fp) &&
129 	    !ferror(fp))) && c != '\n') {
130 		REALLOC(sbuf, sbufsz, i + 1, ERR);
131 		if (!(sbuf[i++] = c))
132 			isbinary = 1;
133 	}
134 	REALLOC(sbuf, sbufsz, i + 2, ERR);
135 	if (c == '\n')
136 		sbuf[i++] = c;
137 	else if (ferror(fp)) {
138 		perror(NULL);
139 		seterrmsg("cannot read input file");
140 		return ERR;
141 	} else if (i) {
142 		sbuf[i++] = '\n';
143 		newline_added = 1;
144 	}
145 	sbuf[i] = '\0';
146 	return (isbinary && newline_added && i) ? --i : i;
147 }
148 
149 
150 /* write_file: write a range of lines to a named file/pipe; return line count */
151 int
write_file(char * fn,char * mode,int n,int m)152 write_file(char *fn, char *mode, int n, int m)
153 {
154 	FILE *fp;
155 	int size;
156 
157 	fp = (*fn == '!') ? popen(fn+1, "w") : fopen(strip_escapes(fn), mode);
158 	if (fp == NULL) {
159 		perror(fn);
160 		seterrmsg("cannot open output file");
161 		return ERR;
162 	} else if ((size = write_stream(fp, n, m)) < 0)
163 		return ERR;
164 	 else if ((*fn == '!') ?  pclose(fp) == -1 : fclose(fp) == EOF) {
165 		perror(fn);
166 		seterrmsg("cannot close output file");
167 		return ERR;
168 	}
169 	if (!scripted)
170 		printf("%d\n", size);
171 	return n ? m - n + 1 : 0;
172 }
173 
174 
175 /* write_stream: write a range of lines to a stream; return status */
176 static int
write_stream(FILE * fp,int n,int m)177 write_stream(FILE *fp, int n, int m)
178 {
179 	line_t *lp = get_addressed_line_node(n);
180 	unsigned int size = 0;
181 	char *s;
182 	int len;
183 
184 	for (; n && n <= m; n++, lp = lp->q_forw) {
185 		if ((s = get_sbuf_line(lp)) == NULL)
186 			return ERR;
187 		len = lp->len;
188 		if (n != addr_last || !isbinary || !newline_added)
189 			s[len++] = '\n';
190 		if (put_stream_line(fp, s, len) < 0)
191 			return ERR;
192 		size += len;
193 	}
194 	return size;
195 }
196 
197 
198 /* put_stream_line: write a line of text to a stream; return status */
199 static int
put_stream_line(FILE * fp,char * s,int len)200 put_stream_line(FILE *fp, char *s, int len)
201 {
202 	while (len--) {
203 		if (fputc(*s, fp) == EOF) {
204 			perror(NULL);
205 			seterrmsg("cannot write file");
206 			return ERR;
207 		}
208 		s++;
209 	}
210 	return 0;
211 }
212 
213 /* get_extended_line: get a an extended line from stdin */
214 char *
get_extended_line(int * sizep,int nonl)215 get_extended_line(int *sizep, int nonl)
216 {
217 	static char *cvbuf = NULL;		/* buffer */
218 	static int cvbufsz = 0;			/* buffer size */
219 
220 	int l, n;
221 	char *t = ibufp;
222 
223 	while (*t++ != '\n')
224 		;
225 	if ((l = t - ibufp) < 2 || !has_trailing_escape(ibufp, ibufp + l - 1)) {
226 		*sizep = l;
227 		return ibufp;
228 	}
229 	*sizep = -1;
230 	REALLOC(cvbuf, cvbufsz, l, NULL);
231 	memcpy(cvbuf, ibufp, l);
232 	*(cvbuf + --l - 1) = '\n'; 	/* strip trailing esc */
233 	if (nonl)
234 		l--; 			/* strip newline */
235 	for (;;) {
236 		if ((n = get_tty_line()) < 0)
237 			return NULL;
238 		else if (n == 0 || ibuf[n - 1] != '\n') {
239 			seterrmsg("unexpected end-of-file");
240 			return NULL;
241 		}
242 		REALLOC(cvbuf, cvbufsz, l + n, NULL);
243 		memcpy(cvbuf + l, ibuf, n);
244 		l += n;
245 		if (n < 2 || !has_trailing_escape(cvbuf, cvbuf + l - 1))
246 			break;
247 		*(cvbuf + --l - 1) = '\n'; 	/* strip trailing esc */
248 		if (nonl) l--; 			/* strip newline */
249 	}
250 	REALLOC(cvbuf, cvbufsz, l + 1, NULL);
251 	cvbuf[l] = '\0';
252 	*sizep = l;
253 	return cvbuf;
254 }
255 
256 
257 /* get_tty_line: read a line of text from stdin; return line length */
258 int
get_tty_line(void)259 get_tty_line(void)
260 {
261 	int oi = 0;
262 	int i = 0;
263 	int c;
264 
265 	for (;;) {
266 		if (sighup)
267 			handle_hup();
268 		switch (c = getchar()) {
269 		default:
270 			oi = 0;
271 			REALLOC(ibuf, ibufsz, i + 2, ERR);
272 			if (!(ibuf[i++] = c)) isbinary = 1;
273 			if (c != '\n')
274 				continue;
275 			lineno++;
276 			ibuf[i] = '\0';
277 			ibufp = ibuf;
278 			return i;
279 		case EOF:
280 			if (sighup)
281 				handle_hup();
282 			if (ferror(stdin)) {
283 				perror("stdin");
284 				seterrmsg("cannot read stdin");
285 				clearerr(stdin);
286 				ibufp = NULL;
287 				return ERR;
288 			} else {
289 				clearerr(stdin);
290 				if (i != oi) {
291 					oi = i;
292 					continue;
293 				} else if (i)
294 					ibuf[i] = '\0';
295 				ibufp = ibuf;
296 				return i;
297 			}
298 		}
299 	}
300 }
301 
302 
303 
304 #define ESCAPES "\a\b\f\n\r\t\v\\$"
305 #define ESCCHARS "abfnrtv\\$"
306 
307 extern int rows;
308 extern int cols;
309 
310 /* put_tty_line: print text to stdout */
311 int
put_tty_line(char * s,int l,int n,int gflag)312 put_tty_line(char *s, int l, int n, int gflag)
313 {
314 	int col = 0;
315 	char *cp;
316 
317 	if (gflag & GNP) {
318 		printf("%d\t", n);
319 		col = 8;
320 	}
321 	for (; l--; s++) {
322 		if ((gflag & GLS) && ++col > cols) {
323 			fputs("\\\n", stdout);
324 			col = 1;
325 		}
326 		if (gflag & GLS) {
327 			if (31 < *s && *s < 127 && *s != '\\' && *s != '$')
328 				putchar(*s);
329 			else {
330 				putchar('\\');
331 				col++;
332 				if (*s && (cp = strchr(ESCAPES, *s)) != NULL)
333 					putchar(ESCCHARS[cp - ESCAPES]);
334 				else {
335 					putchar((((unsigned char) *s & 0300) >> 6) + '0');
336 					putchar((((unsigned char) *s & 070) >> 3) + '0');
337 					putchar(((unsigned char) *s & 07) + '0');
338 					col += 2;
339 				}
340 			}
341 
342 		} else
343 			putchar(*s);
344 	}
345 	if (gflag & GLS)
346 		putchar('$');
347 	putchar('\n');
348 	return 0;
349 }
350