1 /* io.c: This file contains the i/o routines for the ed line editor */
3 * Copyright (c) 1993 Andrew Moore, Talke Studio.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * @(#)io.c,v 1.1 1994/02/01 00:34:41 alm Exp
28 * $FreeBSD: head/bin/ed/io.c 241737 2012-10-19 14:49:42Z ed $
33 /* read_file: read a named file/pipe into the buffer; return line count */
35 read_file(char *fn, long n)
41 fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r");
43 fprintf(stderr, "%s: %s\n", fn, strerror(errno));
44 errmsg = "cannot open input file";
46 } else if ((size = read_stream(fp, n)) < 0)
48 else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) {
49 fprintf(stderr, "%s: %s\n", fn, strerror(errno));
50 errmsg = "cannot close input file";
54 fprintf(stdout, "%lu\n", size);
55 return current_addr - n;
58 static char *sbuf; /* file i/o buffer */
59 static int sbufsz; /* file i/o buffer size */
60 int newline_added; /* if set, newline appended to input file */
62 /* read_stream: read a stream into the editor buffer; return status */
64 read_stream(FILE *fp, long n)
66 line_t *lp = get_addressed_line_node(n);
68 unsigned long size = 0;
69 int o_newline_added = newline_added;
70 int o_isbinary = isbinary;
71 int appended = (n == addr_last);
74 isbinary = newline_added = 0;
77 for (current_addr = n; (len = get_stream_line(fp)) > 0; size += len) {
79 if (put_sbuf_line(sbuf) == NULL) {
86 else if ((up = push_undo_stack(UADD, current_addr,
87 current_addr)) == NULL) {
95 if (appended && size && o_isbinary && o_newline_added)
96 fputs("newline inserted\n", stderr);
97 else if (newline_added && (!appended || (!isbinary && !o_isbinary)))
98 fputs("newline appended\n", stderr);
99 if (isbinary && newline_added && !appended)
103 newline_added = appended ? newline_added : o_newline_added;
104 isbinary = isbinary | o_isbinary;
106 size += 8 - size % 8; /* adjust DES size */
111 /* get_stream_line: read a line of text from a stream; return line length */
113 get_stream_line(FILE *fp)
118 while (((c = des ? get_des_char(fp) : getc(fp)) != EOF || (!feof(fp) &&
119 !ferror(fp))) && c != '\n') {
120 REALLOC(sbuf, sbufsz, i + 1, ERR);
121 if (!(sbuf[i++] = c))
124 REALLOC(sbuf, sbufsz, i + 2, ERR);
127 else if (ferror(fp)) {
128 fprintf(stderr, "%s\n", strerror(errno));
129 errmsg = "cannot read input file";
136 return (isbinary && newline_added && i) ? --i : i;
140 /* write_file: write a range of lines to a named file/pipe; return line count */
142 write_file(char *fn, const char *mode, long n, long m)
147 fp = (*fn == '!') ? popen(fn+1, "w") : fopen(strip_escapes(fn), mode);
149 fprintf(stderr, "%s: %s\n", fn, strerror(errno));
150 errmsg = "cannot open output file";
152 } else if ((size = write_stream(fp, n, m)) < 0)
154 else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) {
155 fprintf(stderr, "%s: %s\n", fn, strerror(errno));
156 errmsg = "cannot close output file";
160 fprintf(stdout, "%lu\n", size);
161 return n ? m - n + 1 : 0;
165 /* write_stream: write a range of lines to a stream; return status */
167 write_stream(FILE *fp, long n, long m)
169 line_t *lp = get_addressed_line_node(n);
170 unsigned long size = 0;
176 for (; n && n <= m; n++, lp = lp->q_forw) {
177 if ((s = get_sbuf_line(lp)) == NULL)
180 if (n != addr_last || !isbinary || !newline_added)
182 if (put_stream_line(fp, s, len) < 0)
187 flush_des_file(fp); /* flush buffer */
188 size += 8 - size % 8; /* adjust DES size */
194 /* put_stream_line: write a line of text to a stream; return status */
196 put_stream_line(FILE *fp, const char *s, int len)
199 if ((des ? put_des_char(*s++, fp) : fputc(*s++, fp)) < 0) {
200 fprintf(stderr, "%s\n", strerror(errno));
201 errmsg = "cannot write file";
207 /* get_extended_line: get an extended line from stdin */
209 get_extended_line(int *sizep, int nonl)
211 static char *cvbuf = NULL; /* buffer */
212 static int cvbufsz = 0; /* buffer size */
219 if ((l = t - ibufp) < 2 || !has_trailing_escape(ibufp, ibufp + l - 1)) {
224 REALLOC(cvbuf, cvbufsz, l, NULL);
225 memcpy(cvbuf, ibufp, l);
226 *(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
227 if (nonl) l--; /* strip newline */
229 if ((n = get_tty_line()) < 0)
231 else if (n == 0 || ibuf[n - 1] != '\n') {
232 errmsg = "unexpected end-of-file";
235 REALLOC(cvbuf, cvbufsz, l + n, NULL);
236 memcpy(cvbuf + l, ibuf, n);
238 if (n < 2 || !has_trailing_escape(cvbuf, cvbuf + l - 1))
240 *(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
241 if (nonl) l--; /* strip newline */
243 REALLOC(cvbuf, cvbufsz, l + 1, NULL);
250 /* get_tty_line: read a line of text from stdin; return line length */
259 switch (c = getchar()) {
262 REALLOC(ibuf, ibufsz, i + 2, ERR);
263 if (!(ibuf[i++] = c)) isbinary = 1;
272 fprintf(stderr, "stdin: %s\n", strerror(errno));
273 errmsg = "cannot read stdin";
292 #define ESCAPES "\a\b\f\n\r\t\v\\"
293 #define ESCCHARS "abfnrtv\\"
295 /* put_tty_line: print text to stdout */
297 put_tty_line(const char *s, int l, long n, int gflag)
308 if ((gflag & GLS) && ++col > cols) {
309 fputs("\\\n", stdout);
312 if (!scripted && !isglobal && ++lc > rows) {
314 fputs("Press <RETURN> to continue... ", stdout);
316 if (get_tty_line() < 0)
322 if (31 < *s && *s < 127 && *s != '\\')
327 if (*s && (cp = strchr(ESCAPES, *s)) != NULL)
328 putchar(ESCCHARS[cp - ESCAPES]);
330 putchar((((unsigned char) *s & 0300) >> 6) + '0');
331 putchar((((unsigned char) *s & 070) >> 3) + '0');
332 putchar(((unsigned char) *s & 07) + '0');