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
30 static char * const rcsid = "@(#)io.c,v 1.1 1994/02/01 00:34:41 alm Exp";
32 static char * const rcsid =
33 "$FreeBSD: src/bin/ed/io.c,v 1.10 1999/08/27 23:14:14 peter Exp $";
42 /* read_file: read a named file/pipe into the buffer; return line count */
52 fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r");
54 fprintf(stderr, "%s: %s\n", fn, strerror(errno));
55 sprintf(errmsg, "cannot open input file");
57 } else if ((size = read_stream(fp, n)) < 0)
59 else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) {
60 fprintf(stderr, "%s: %s\n", fn, strerror(errno));
61 sprintf(errmsg, "cannot close input file");
64 fprintf(stdout, !scripted ? "%lu\n" : "", size);
65 return current_addr - n;
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 */
75 /* read_stream: read a stream into the editor buffer; return status */
81 line_t *lp = get_addressed_line_node(n);
83 unsigned long size = 0;
84 int o_newline_added = newline_added;
85 int o_isbinary = isbinary;
86 int appended = (n == addr_last);
89 isbinary = newline_added = 0;
92 for (current_addr = n; (len = get_stream_line(fp)) > 0; size += len) {
94 if (put_sbuf_line(sbuf) == NULL) {
101 else if ((up = push_undo_stack(UADD, current_addr,
102 current_addr)) == NULL) {
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)
118 newline_added = appended ? newline_added : o_newline_added;
119 isbinary = isbinary | o_isbinary;
121 size += 8 - size % 8; /* adjust DES size */
126 /* get_stream_line: read a line of text from a stream; return line length */
134 while (((c = des ? get_des_char(fp) : getc(fp)) != EOF || (!feof(fp) &&
135 !ferror(fp))) && c != '\n') {
136 REALLOC(sbuf, sbufsz, i + 1, ERR);
137 if (!(sbuf[i++] = c))
140 REALLOC(sbuf, sbufsz, i + 2, ERR);
143 else if (ferror(fp)) {
144 fprintf(stderr, "%s\n", strerror(errno));
145 sprintf(errmsg, "cannot read input file");
152 return (isbinary && newline_added && i) ? --i : i;
156 /* write_file: write a range of lines to a named file/pipe; return line count */
158 write_file(fn, mode, n, m)
167 fp = (*fn == '!') ? popen(fn+1, "w") : fopen(strip_escapes(fn), mode);
169 fprintf(stderr, "%s: %s\n", fn, strerror(errno));
170 sprintf(errmsg, "cannot open output file");
172 } else if ((size = write_stream(fp, n, m)) < 0)
174 else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) {
175 fprintf(stderr, "%s: %s\n", fn, strerror(errno));
176 sprintf(errmsg, "cannot close output file");
179 fprintf(stdout, !scripted ? "%lu\n" : "", size);
180 return n ? m - n + 1 : 0;
184 /* write_stream: write a range of lines to a stream; return status */
186 write_stream(fp, n, m)
191 line_t *lp = get_addressed_line_node(n);
192 unsigned long size = 0;
198 for (; n && n <= m; n++, lp = lp->q_forw) {
199 if ((s = get_sbuf_line(lp)) == NULL)
202 if (n != addr_last || !isbinary || !newline_added)
204 if (put_stream_line(fp, s, len) < 0)
209 flush_des_file(fp); /* flush buffer */
210 size += 8 - size % 8; /* adjust DES size */
216 /* put_stream_line: write a line of text to a stream; return status */
218 put_stream_line(fp, s, len)
224 if ((des ? put_des_char(*s++, fp) : fputc(*s++, fp)) < 0) {
225 fprintf(stderr, "%s\n", strerror(errno));
226 sprintf(errmsg, "cannot write file");
232 /* get_extended_line: get a an extended line from stdin */
234 get_extended_line(sizep, nonl)
238 static char *cvbuf = NULL; /* buffer */
239 static int cvbufsz = 0; /* buffer size */
246 if ((l = t - ibufp) < 2 || !has_trailing_escape(ibufp, ibufp + l - 1)) {
251 REALLOC(cvbuf, cvbufsz, l, NULL);
252 memcpy(cvbuf, ibufp, l);
253 *(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
254 if (nonl) l--; /* strip newline */
256 if ((n = get_tty_line()) < 0)
258 else if (n == 0 || ibuf[n - 1] != '\n') {
259 sprintf(errmsg, "unexpected end-of-file");
262 REALLOC(cvbuf, cvbufsz, l + n, NULL);
263 memcpy(cvbuf + l, ibuf, n);
265 if (n < 2 || !has_trailing_escape(cvbuf, cvbuf + l - 1))
267 *(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
268 if (nonl) l--; /* strip newline */
270 REALLOC(cvbuf, cvbufsz, l + 1, NULL);
277 /* get_tty_line: read a line of text from stdin; return line length */
286 switch (c = getchar()) {
289 REALLOC(ibuf, ibufsz, i + 2, ERR);
290 if (!(ibuf[i++] = c)) isbinary = 1;
299 fprintf(stderr, "stdin: %s\n", strerror(errno));
300 sprintf(errmsg, "cannot read stdin");
319 #define ESCAPES "\a\b\f\n\r\t\v\\"
320 #define ESCCHARS "abfnrtv\\"
325 /* put_tty_line: print text to stdout */
327 put_tty_line(s, l, n, gflag)
342 if ((gflag & GLS) && ++col > cols) {
343 fputs("\\\n", stdout);
346 if (!scripted && !isglobal && ++lc > rows) {
348 fputs("Press <RETURN> to continue... ", stdout);
350 if (get_tty_line() < 0)
356 if (31 < *s && *s < 127 && *s != '\\')
361 if (*s && (cp = strchr(ESCAPES, *s)) != NULL)
362 putchar(ESCCHARS[cp - ESCAPES]);
364 putchar((((unsigned char) *s & 0300) >> 6) + '0');
365 putchar((((unsigned char) *s & 070) >> 3) + '0');
366 putchar(((unsigned char) *s & 07) + '0');