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