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