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