1/*	$OpenBSD: io.c,v 1.25 2022/11/18 14:52:03 millert 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 <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37#include "ed.h"
38
39static int read_stream(FILE *, int);
40static int get_stream_line(FILE *);
41static int write_stream(FILE *, int, int);
42static int put_stream_line(FILE *, char *, int);
43
44extern int scripted;
45
46/* read_file: read a named file/pipe into the buffer; return line count */
47int
48read_file(char *fn, int n)
49{
50	FILE *fp;
51	int size;
52
53
54	fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r");
55	if (fp == NULL) {
56		perror(fn);
57		seterrmsg("cannot open input file");
58		return ERR;
59	} else if ((size = read_stream(fp, n)) < 0)
60		return ERR;
61	 else if ((*fn == '!') ?  pclose(fp) == -1 : fclose(fp) == EOF) {
62		perror(fn);
63		seterrmsg("cannot close input file");
64		return ERR;
65	}
66	if (!scripted)
67		printf("%d\n", size);
68	return current_addr - n;
69}
70
71
72static char *sbuf;		/* file i/o buffer */
73static int sbufsz;		/* file i/o buffer size */
74int newline_added;		/* if set, newline appended to input file */
75
76/* read_stream: read a stream into the editor buffer; return status */
77static int
78read_stream(FILE *fp, int n)
79{
80	line_t *lp = get_addressed_line_node(n);
81	undo_t *up = NULL;
82	unsigned int size = 0;
83	int o_newline_added = newline_added;
84	int o_isbinary = isbinary;
85	int appended = (n == addr_last);
86	int len;
87
88	isbinary = newline_added = 0;
89	for (current_addr = n; (len = get_stream_line(fp)) > 0; size += len) {
90		SPL1();
91		if (put_sbuf_line(sbuf) == NULL) {
92			SPL0();
93			return ERR;
94		}
95		lp = lp->q_forw;
96		if (up)
97			up->t = lp;
98		else if ((up = push_undo_stack(UADD, current_addr,
99		    current_addr)) == NULL) {
100			SPL0();
101			return ERR;
102		}
103		SPL0();
104	}
105	if (len < 0)
106		return ERR;
107	if (appended && size && o_isbinary && o_newline_added)
108		fputs("newline inserted\n", stderr);
109	else if (newline_added && (!appended || (!isbinary && !o_isbinary)))
110		fputs("newline appended\n", stderr);
111	if (isbinary && newline_added && !appended)
112	    	size += 1;
113	if (!size)
114		newline_added = 1;
115	newline_added = appended ? newline_added : o_newline_added;
116	isbinary = isbinary | o_isbinary;
117	return size;
118}
119
120/* get_stream_line: read a line of text from a stream; return line length */
121static int
122get_stream_line(FILE *fp)
123{
124	int c;
125	int i = 0;
126
127	while (((c = getc(fp)) != EOF || (!feof(fp) &&
128	    !ferror(fp))) && c != '\n') {
129		REALLOC(sbuf, sbufsz, i + 1, ERR);
130		if (!(sbuf[i++] = c))
131			isbinary = 1;
132	}
133	REALLOC(sbuf, sbufsz, i + 2, ERR);
134	if (c == '\n')
135		sbuf[i++] = c;
136	else if (ferror(fp)) {
137		perror(NULL);
138		seterrmsg("cannot read input file");
139		return ERR;
140	} else if (i) {
141		sbuf[i++] = '\n';
142		newline_added = 1;
143	}
144	sbuf[i] = '\0';
145	return (isbinary && newline_added && i) ? --i : i;
146}
147
148
149/* write_file: write a range of lines to a named file/pipe; return line count */
150int
151write_file(char *fn, char *mode, int n, int m)
152{
153	FILE *fp;
154	int size;
155
156	fp = (*fn == '!') ? popen(fn+1, "w") : fopen(strip_escapes(fn), mode);
157	if (fp == NULL) {
158		perror(fn);
159		seterrmsg("cannot open output file");
160		return ERR;
161	} else if ((size = write_stream(fp, n, m)) < 0)
162		return ERR;
163	 else if ((*fn == '!') ?  pclose(fp) == -1 : fclose(fp) == EOF) {
164		perror(fn);
165		seterrmsg("cannot close output file");
166		return ERR;
167	}
168	if (!scripted)
169		printf("%d\n", size);
170	return n ? m - n + 1 : 0;
171}
172
173
174/* write_stream: write a range of lines to a stream; return status */
175static int
176write_stream(FILE *fp, int n, int m)
177{
178	line_t *lp = get_addressed_line_node(n);
179	unsigned int size = 0;
180	char *s;
181	int len;
182
183	for (; n && n <= m; n++, lp = lp->q_forw) {
184		if ((s = get_sbuf_line(lp)) == NULL)
185			return ERR;
186		len = lp->len;
187		if (n != addr_last || !isbinary || !newline_added)
188			s[len++] = '\n';
189		if (put_stream_line(fp, s, len) < 0)
190			return ERR;
191		size += len;
192	}
193	return size;
194}
195
196
197/* put_stream_line: write a line of text to a stream; return status */
198static int
199put_stream_line(FILE *fp, char *s, int len)
200{
201	while (len--) {
202		if (fputc(*s, fp) == EOF) {
203			perror(NULL);
204			seterrmsg("cannot write file");
205			return ERR;
206		}
207		s++;
208	}
209	return 0;
210}
211
212/* get_extended_line: get a an extended line from stdin */
213char *
214get_extended_line(int *sizep, int nonl)
215{
216	static char *cvbuf = NULL;		/* buffer */
217	static int cvbufsz = 0;			/* buffer size */
218
219	int l, n;
220	char *t = ibufp;
221
222	while (*t++ != '\n')
223		;
224	if ((l = t - ibufp) < 2 || !has_trailing_escape(ibufp, ibufp + l - 1)) {
225		*sizep = l;
226		return ibufp;
227	}
228	*sizep = -1;
229	REALLOC(cvbuf, cvbufsz, l, NULL);
230	memcpy(cvbuf, ibufp, l);
231	*(cvbuf + --l - 1) = '\n'; 	/* strip trailing esc */
232	if (nonl)
233		l--; 			/* strip newline */
234	for (;;) {
235		if ((n = get_tty_line()) < 0)
236			return NULL;
237		else if (n == 0 || ibuf[n - 1] != '\n') {
238			seterrmsg("unexpected end-of-file");
239			return NULL;
240		}
241		REALLOC(cvbuf, cvbufsz, l + n, NULL);
242		memcpy(cvbuf + l, ibuf, n);
243		l += n;
244		if (n < 2 || !has_trailing_escape(cvbuf, cvbuf + l - 1))
245			break;
246		*(cvbuf + --l - 1) = '\n'; 	/* strip trailing esc */
247		if (nonl) l--; 			/* strip newline */
248	}
249	REALLOC(cvbuf, cvbufsz, l + 1, NULL);
250	cvbuf[l] = '\0';
251	*sizep = l;
252	return cvbuf;
253}
254
255
256/* get_tty_line: read a line of text from stdin; return line length */
257int
258get_tty_line(void)
259{
260	int oi = 0;
261	int i = 0;
262	int c;
263
264	for (;;)
265		switch (c = getchar()) {
266		default:
267			oi = 0;
268			REALLOC(ibuf, ibufsz, i + 2, ERR);
269			if (!(ibuf[i++] = c)) isbinary = 1;
270			if (c != '\n')
271				continue;
272			lineno++;
273			ibuf[i] = '\0';
274			ibufp = ibuf;
275			return i;
276		case EOF:
277			if (ferror(stdin)) {
278				perror("stdin");
279				seterrmsg("cannot read stdin");
280				clearerr(stdin);
281				ibufp = NULL;
282				return ERR;
283			} else {
284				clearerr(stdin);
285				if (i != oi) {
286					oi = i;
287					continue;
288				} else if (i)
289					ibuf[i] = '\0';
290				ibufp = ibuf;
291				return i;
292			}
293		}
294}
295
296
297
298#define ESCAPES "\a\b\f\n\r\t\v\\$"
299#define ESCCHARS "abfnrtv\\$"
300
301extern int rows;
302extern int cols;
303
304/* put_tty_line: print text to stdout */
305int
306put_tty_line(char *s, int l, int n, int gflag)
307{
308	int col = 0;
309	char *cp;
310
311	if (gflag & GNP) {
312		printf("%d\t", n);
313		col = 8;
314	}
315	for (; l--; s++) {
316		if ((gflag & GLS) && ++col > cols) {
317			fputs("\\\n", stdout);
318			col = 1;
319		}
320		if (gflag & GLS) {
321			if (31 < *s && *s < 127 && *s != '\\' && *s != '$')
322				putchar(*s);
323			else {
324				putchar('\\');
325				col++;
326				if (*s && (cp = strchr(ESCAPES, *s)) != NULL)
327					putchar(ESCCHARS[cp - ESCAPES]);
328				else {
329					putchar((((unsigned char) *s & 0300) >> 6) + '0');
330					putchar((((unsigned char) *s & 070) >> 3) + '0');
331					putchar(((unsigned char) *s & 07) + '0');
332					col += 2;
333				}
334			}
335
336		} else
337			putchar(*s);
338	}
339	if (gflag & GLS)
340		putchar('$');
341	putchar('\n');
342	return 0;
343}
344