xref: /freebsd/bin/ed/io.c (revision 3ff369fed2a08f32dda232c10470b949bef9489f)
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 #ifndef lint
29 static const char rcsid[] =
30   "$FreeBSD$";
31 #endif /* not lint */
32 
33 #include "ed.h"
34 
35 
36 extern int scripted;
37 
38 /* read_file: read a named file/pipe into the buffer; return line count */
39 long
40 read_file(char *fn, long n)
41 {
42 	FILE *fp;
43 	long size;
44 
45 
46 	fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r");
47 	if (fp == NULL) {
48 		fprintf(stderr, "%s: %s\n", fn, strerror(errno));
49 		errmsg = "cannot open input file";
50 		return ERR;
51 	} else if ((size = read_stream(fp, n)) < 0)
52 		return ERR;
53 	 else if (((*fn == '!') ?  pclose(fp) : fclose(fp)) < 0) {
54 		fprintf(stderr, "%s: %s\n", fn, strerror(errno));
55 		errmsg = "cannot close input file";
56 		return ERR;
57 	}
58 	fprintf(stdout, !scripted ? "%lu\n" : "", size);
59 	return current_addr - n;
60 }
61 
62 
63 extern int des;
64 
65 char *sbuf;			/* file i/o buffer */
66 int sbufsz;			/* file i/o buffer size */
67 int newline_added;		/* if set, newline appended to input file */
68 
69 /* read_stream: read a stream into the editor buffer; return status */
70 long
71 read_stream(FILE *fp, long n)
72 {
73 	line_t *lp = get_addressed_line_node(n);
74 	undo_t *up = NULL;
75 	unsigned long size = 0;
76 	int o_newline_added = newline_added;
77 	int o_isbinary = isbinary;
78 	int appended = (n == addr_last);
79 	int len;
80 
81 	isbinary = newline_added = 0;
82 	if (des)
83 		init_des_cipher();
84 	for (current_addr = n; (len = get_stream_line(fp)) > 0; size += len) {
85 		SPL1();
86 		if (put_sbuf_line(sbuf) == NULL) {
87 			SPL0();
88 			return ERR;
89 		}
90 		lp = lp->q_forw;
91 		if (up)
92 			up->t = lp;
93 		else if ((up = push_undo_stack(UADD, current_addr,
94 		    current_addr)) == NULL) {
95 			SPL0();
96 			return ERR;
97 		}
98 		SPL0();
99 	}
100 	if (len < 0)
101 		return ERR;
102 	if (appended && size && o_isbinary && o_newline_added)
103 		fputs("newline inserted\n", stderr);
104 	else if (newline_added && (!appended || (!isbinary && !o_isbinary)))
105 		fputs("newline appended\n", stderr);
106 	if (isbinary && newline_added && !appended)
107 	    	size += 1;
108 	if (!size)
109 		newline_added = 1;
110 	newline_added = appended ? newline_added : o_newline_added;
111 	isbinary = isbinary | o_isbinary;
112 	if (des)
113 		size += 8 - size % 8;			/* adjust DES size */
114 	return size;
115 }
116 
117 
118 /* get_stream_line: read a line of text from a stream; return line length */
119 int
120 get_stream_line(FILE *fp)
121 {
122 	int c;
123 	int i = 0;
124 
125 	while (((c = des ? get_des_char(fp) : getc(fp)) != EOF || (!feof(fp) &&
126 	    !ferror(fp))) && c != '\n') {
127 		REALLOC(sbuf, sbufsz, i + 1, ERR);
128 		if (!(sbuf[i++] = c))
129 			isbinary = 1;
130 	}
131 	REALLOC(sbuf, sbufsz, i + 2, ERR);
132 	if (c == '\n')
133 		sbuf[i++] = c;
134 	else if (ferror(fp)) {
135 		fprintf(stderr, "%s\n", strerror(errno));
136 		errmsg = "cannot read input file";
137 		return ERR;
138 	} else if (i) {
139 		sbuf[i++] = '\n';
140 		newline_added = 1;
141 	}
142 	sbuf[i] = '\0';
143 	return (isbinary && newline_added && i) ? --i : i;
144 }
145 
146 
147 /* write_file: write a range of lines to a named file/pipe; return line count */
148 long
149 write_file(char *fn, const char *mode, long n, long m)
150 {
151 	FILE *fp;
152 	long size;
153 
154 	fp = (*fn == '!') ? popen(fn+1, "w") : fopen(strip_escapes(fn), mode);
155 	if (fp == NULL) {
156 		fprintf(stderr, "%s: %s\n", fn, strerror(errno));
157 		errmsg = "cannot open output file";
158 		return ERR;
159 	} else if ((size = write_stream(fp, n, m)) < 0)
160 		return ERR;
161 	 else if (((*fn == '!') ?  pclose(fp) : fclose(fp)) < 0) {
162 		fprintf(stderr, "%s: %s\n", fn, strerror(errno));
163 		errmsg = "cannot close output file";
164 		return ERR;
165 	}
166 	fprintf(stdout, !scripted ? "%lu\n" : "", size);
167 	return n ? m - n + 1 : 0;
168 }
169 
170 
171 /* write_stream: write a range of lines to a stream; return status */
172 long
173 write_stream(FILE *fp, long n, long m)
174 {
175 	line_t *lp = get_addressed_line_node(n);
176 	unsigned long size = 0;
177 	char *s;
178 	int len;
179 
180 	if (des)
181 		init_des_cipher();
182 	for (; n && n <= m; n++, lp = lp->q_forw) {
183 		if ((s = get_sbuf_line(lp)) == NULL)
184 			return ERR;
185 		len = lp->len;
186 		if (n != addr_last || !isbinary || !newline_added)
187 			s[len++] = '\n';
188 		if (put_stream_line(fp, s, len) < 0)
189 			return ERR;
190 		size += len;
191 	}
192 	if (des) {
193 		flush_des_file(fp);			/* flush buffer */
194 		size += 8 - size % 8;			/* adjust DES size */
195 	}
196 	return size;
197 }
198 
199 
200 /* put_stream_line: write a line of text to a stream; return status */
201 int
202 put_stream_line(FILE *fp, const char *s, int len)
203 {
204 	while (len--)
205 		if ((des ? put_des_char(*s++, fp) : fputc(*s++, fp)) < 0) {
206 			fprintf(stderr, "%s\n", strerror(errno));
207 			errmsg = "cannot write file";
208 			return ERR;
209 		}
210 	return 0;
211 }
212 
213 /* get_extended_line: get a an extended line from stdin */
214 char *
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) 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 			errmsg = "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 */
257 int
258 get_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 				fprintf(stderr, "stdin: %s\n", strerror(errno));
279 				errmsg = "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 
301 extern int rows;
302 extern int cols;
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