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