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