1139969Simp/* io.c: This file contains the i/o routines for the ed line editor */
21556Srgrimes/*-
31556Srgrimes * Copyright (c) 1993 Andrew Moore, Talke Studio.
41556Srgrimes * All rights reserved.
51556Srgrimes *
61556Srgrimes * Redistribution and use in source and binary forms, with or without
71556Srgrimes * modification, are permitted provided that the following conditions
81556Srgrimes * are met:
91556Srgrimes * 1. Redistributions of source code must retain the above copyright
101556Srgrimes *    notice, this list of conditions and the following disclaimer.
111556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer in the
131556Srgrimes *    documentation and/or other materials provided with the distribution.
141556Srgrimes *
151556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251556Srgrimes * SUCH DAMAGE.
261556Srgrimes */
271556Srgrimes
281556Srgrimes#include <sys/cdefs.h>
291556Srgrimes__FBSDID("$FreeBSD$");
3050471Speter
311556Srgrimes#include "ed.h"
32320646Sallanjude
331556Srgrimes/* read_file: read a named file/pipe into the buffer; return line count */
3479526Srulong
351556Srgrimesread_file(char *fn, long n)
361556Srgrimes{
371556Srgrimes	FILE *fp;
381556Srgrimes	long size;
3968935Sru	int cs;
40140563Sru
411556Srgrimes	fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r");
4268935Sru	if (fp == NULL) {
43140563Sru		fprintf(stderr, "%s: %s\n", fn, strerror(errno));
4472432Sru		errmsg = "cannot open input file";
451556Srgrimes		return ERR;
46320646Sallanjude	}
47320646Sallanjude	if ((size = read_stream(fp, n)) < 0) {
48320646Sallanjude		fprintf(stderr, "%s: %s\n", fn, strerror(errno));
49320646Sallanjude		errmsg = "error reading input file";
50320646Sallanjude	}
51320646Sallanjude	if ((cs = (*fn == '!') ?  pclose(fp) : fclose(fp)) < 0) {
52320646Sallanjude		fprintf(stderr, "%s: %s\n", fn, strerror(errno));
53320646Sallanjude		errmsg = "cannot close input file";
54320646Sallanjude	}
551556Srgrimes	if (size < 0 || cs < 0)
5694869Scharnier		return ERR;
5794869Scharnier	if (!scripted)
58131484Sru		fprintf(stdout, "%lu\n", size);
59131484Sru	return current_addr - n;
601556Srgrimes}
611556Srgrimes
621556Srgrimesstatic char *sbuf;		/* file i/o buffer */
631556Srgrimesstatic int sbufsz;		/* file i/o buffer size */
6455562Sphantomint newline_added;		/* if set, newline appended to input file */
65167063Sru
66167063Sru/* read_stream: read a stream into the editor buffer; return status */
67167063Srulong
68167063Sruread_stream(FILE *fp, long n)
69167063Sru{
70167063Sru	line_t *lp = get_addressed_line_node(n);
71167063Sru	undo_t *up = NULL;
72167063Sru	unsigned long size = 0;
73167063Sru	int o_newline_added = newline_added;
741556Srgrimes	int o_isbinary = isbinary;
7536149Scharnier	int appended = (n == addr_last);
7636149Scharnier	int len;
77101591Sume
78101591Sume	isbinary = newline_added = 0;
79101591Sume	if (des)
80101591Sume		init_des_cipher();
811556Srgrimes	for (current_addr = n; (len = get_stream_line(fp)) > 0; size += len) {
8236149Scharnier		SPL1();
8336149Scharnier		if (put_sbuf_line(sbuf) == NULL) {
841556Srgrimes			SPL0();
851556Srgrimes			return ERR;
86167063Sru		}
871556Srgrimes		lp = lp->q_forw;
881556Srgrimes		if (up)
891556Srgrimes			up->t = lp;
901556Srgrimes		else if ((up = push_undo_stack(UADD, current_addr,
91131505Sru		    current_addr)) == NULL) {
921556Srgrimes			SPL0();
931556Srgrimes			return ERR;
941556Srgrimes		}
9536149Scharnier		SPL0();
961556Srgrimes	}
971556Srgrimes	if (len < 0)
981556Srgrimes		return ERR;
991556Srgrimes	if (appended && size && o_isbinary && o_newline_added)
1001556Srgrimes		fputs("newline inserted\n", stderr);
1011556Srgrimes	else if (newline_added && (!appended || (!isbinary && !o_isbinary)))
1021556Srgrimes		fputs("newline appended\n", stderr);
1031556Srgrimes	if (isbinary && newline_added && !appended)
1041556Srgrimes	    	size += 1;
1051556Srgrimes	if (!size)
106131505Sru		newline_added = 1;
1071556Srgrimes	newline_added = appended ? newline_added : o_newline_added;
1081556Srgrimes	isbinary = isbinary | o_isbinary;
1091556Srgrimes	if (des)
110167063Sru		size += 8 - size % 8;			/* adjust DES size */
111167063Sru	return size;
112167063Sru}
113167063Sru
114167063Sru
1151556Srgrimes/* get_stream_line: read a line of text from a stream; return line length */
1161556Srgrimesint
11794869Scharnierget_stream_line(FILE *fp)
11894869Scharnier{
11994869Scharnier	int c;
1201556Srgrimes	int i = 0;
121131505Sru
1221556Srgrimes	while (((c = des ? get_des_char(fp) : getc(fp)) != EOF || (!feof(fp) &&
1231556Srgrimes	    !ferror(fp))) && c != '\n') {
12494869Scharnier		REALLOC(sbuf, sbufsz, i + 1, ERR);
12594869Scharnier		if (!(sbuf[i++] = c))
12694869Scharnier			isbinary = 1;
1271556Srgrimes	}
1281556Srgrimes	REALLOC(sbuf, sbufsz, i + 2, ERR);
1291556Srgrimes	if (c == '\n')
1301556Srgrimes		sbuf[i++] = c;
13121635Swosch	else if (ferror(fp)) {
13255562Sphantom		fprintf(stderr, "%s\n", strerror(errno));
13368754Sben		errmsg = "cannot read input file";
1341556Srgrimes		return ERR;
1351556Srgrimes	} else if (i) {
13636149Scharnier		sbuf[i++] = '\n';
1371556Srgrimes		newline_added = 1;
1381556Srgrimes	}
1391556Srgrimes	sbuf[i] = '\0';
14036149Scharnier	return (isbinary && newline_added && i) ? --i : i;
1411556Srgrimes}
1421556Srgrimes
1431556Srgrimes
1441556Srgrimes/* write_file: write a range of lines to a named file/pipe; return line count */
145141846Srulong
1461556Srgrimeswrite_file(char *fn, const char *mode, long n, long m)
1471556Srgrimes{
1481556Srgrimes	FILE *fp;
149167063Sru	long size;
150167063Sru	int cs;
1511556Srgrimes
152167063Sru	fp = (*fn == '!') ? popen(fn+1, "w") : fopen(strip_escapes(fn), mode);
1531556Srgrimes	if (fp == NULL) {
1541556Srgrimes		fprintf(stderr, "%s: %s\n", fn, strerror(errno));
1551556Srgrimes		errmsg = "cannot open output file";
156167063Sru		return ERR;
15755562Sphantom	}
1581556Srgrimes	if ((size = write_stream(fp, n, m)) < 0) {
1591556Srgrimes		fprintf(stderr, "%s: %s\n", fn, strerror(errno));
16068935Sru		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 */
175long
176write_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 */
204int
205put_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 */
217char *
218get_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 */
260int
261get_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 */
305int
306put_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