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: src/bin/ed/io.c,v 1.14 2003/01/01 18:48:39 schweikh Exp $
29 * $DragonFly: src/bin/ed/io.c,v 1.6 2007/04/06 23:36:54 pavalos Exp $
37 /* read_file: read a named file/pipe into the buffer; return line count */
39 read_file(char *fn
, long n
)
45 fp
= (*fn
== '!') ? popen(fn
+ 1, "r") : fopen(strip_escapes(fn
), "r");
47 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
48 errmsg
= "cannot open input file";
50 } else if ((size
= read_stream(fp
, n
)) < 0)
52 else if (((*fn
== '!') ? pclose(fp
) : fclose(fp
)) < 0) {
53 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
54 errmsg
= "cannot close input file";
57 fprintf(stdout
, !scripted
? "%lu\n" : "", size
);
58 return current_addr
- n
;
64 char *sbuf
; /* file i/o buffer */
65 int sbufsz
; /* file i/o buffer size */
66 int newline_added
; /* if set, newline appended to input file */
68 /* read_stream: read a stream into the editor buffer; return status */
70 read_stream(FILE *fp
, long n
)
72 line_t
*lp
= get_addressed_line_node(n
);
74 unsigned long size
= 0;
75 int o_newline_added
= newline_added
;
76 int o_isbinary
= isbinary
;
77 int appended
= (n
== addr_last
);
80 isbinary
= newline_added
= 0;
83 for (current_addr
= n
; (len
= get_stream_line(fp
)) > 0; size
+= len
) {
85 if (put_sbuf_line(sbuf
) == NULL
) {
92 else if ((up
= push_undo_stack(UADD
, current_addr
,
93 current_addr
)) == NULL
) {
101 if (appended
&& size
&& o_isbinary
&& o_newline_added
)
102 fputs("newline inserted\n", stderr
);
103 else if (newline_added
&& (!appended
|| (!isbinary
&& !o_isbinary
)))
104 fputs("newline appended\n", stderr
);
105 if (isbinary
&& newline_added
&& !appended
)
109 newline_added
= appended
? newline_added
: o_newline_added
;
110 isbinary
= isbinary
| o_isbinary
;
112 size
+= 8 - size
% 8; /* adjust DES size */
117 /* get_stream_line: read a line of text from a stream; return line length */
119 get_stream_line(FILE *fp
)
124 while (((c
= des
? get_des_char(fp
) : getc(fp
)) != EOF
|| (!feof(fp
) &&
125 !ferror(fp
))) && c
!= '\n') {
126 REALLOC(sbuf
, sbufsz
, i
+ 1, ERR
);
127 if (!(sbuf
[i
++] = c
))
130 REALLOC(sbuf
, sbufsz
, i
+ 2, ERR
);
133 else if (ferror(fp
)) {
134 fprintf(stderr
, "%s\n", strerror(errno
));
135 errmsg
= "cannot read input file";
142 return (isbinary
&& newline_added
&& i
) ? --i
: i
;
146 /* write_file: write a range of lines to a named file/pipe; return line count */
148 write_file(const char *fn
, const char *mode
, long n
, long m
)
153 fp
= (*fn
== '!') ? popen(fn
+1, "w") : fopen(strip_escapes(fn
), mode
);
155 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
156 errmsg
= "cannot open output file";
158 } else if ((size
= write_stream(fp
, n
, m
)) < 0)
160 else if (((*fn
== '!') ? pclose(fp
) : fclose(fp
)) < 0) {
161 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
162 errmsg
= "cannot close output file";
165 fprintf(stdout
, !scripted
? "%lu\n" : "", size
);
166 return n
? m
- n
+ 1 : 0;
170 /* write_stream: write a range of lines to a stream; return status */
172 write_stream(FILE *fp
, long n
, long m
)
174 line_t
*lp
= get_addressed_line_node(n
);
175 unsigned long size
= 0;
181 for (; n
&& n
<= m
; n
++, lp
= lp
->q_forw
) {
182 if ((s
= get_sbuf_line(lp
)) == NULL
)
185 if (n
!= addr_last
|| !isbinary
|| !newline_added
)
187 if (put_stream_line(fp
, s
, len
) < 0)
192 flush_des_file(fp
); /* flush buffer */
193 size
+= 8 - size
% 8; /* adjust DES size */
199 /* put_stream_line: write a line of text to a stream; return status */
201 put_stream_line(FILE *fp
, const char *s
, int len
)
204 if ((des
? put_des_char(*s
++, fp
) : fputc(*s
++, fp
)) < 0) {
205 fprintf(stderr
, "%s\n", strerror(errno
));
206 errmsg
= "cannot write file";
212 /* get_extended_line: get an extended line from stdin */
214 get_extended_line(int *sizep
, int nonl
)
216 static char *cvbuf
= NULL
; /* buffer */
217 static int cvbufsz
= 0; /* buffer size */
224 if ((l
= t
- ibufp
) < 2 || !has_trailing_escape(ibufp
, ibufp
+ l
- 1)) {
229 REALLOC(cvbuf
, cvbufsz
, l
, NULL
);
230 memcpy(cvbuf
, ibufp
, l
);
231 *(cvbuf
+ --l
- 1) = '\n'; /* strip trailing esc */
232 if (nonl
) l
--; /* strip newline */
234 if ((n
= get_tty_line()) < 0)
236 else if (n
== 0 || ibuf
[n
- 1] != '\n') {
237 errmsg
= "unexpected end-of-file";
240 REALLOC(cvbuf
, cvbufsz
, l
+ n
, NULL
);
241 memcpy(cvbuf
+ l
, ibuf
, n
);
243 if (n
< 2 || !has_trailing_escape(cvbuf
, cvbuf
+ l
- 1))
245 *(cvbuf
+ --l
- 1) = '\n'; /* strip trailing esc */
246 if (nonl
) l
--; /* strip newline */
248 REALLOC(cvbuf
, cvbufsz
, l
+ 1, NULL
);
255 /* get_tty_line: read a line of text from stdin; return line length */
264 switch (c
= getchar()) {
267 REALLOC(ibuf
, ibufsz
, i
+ 2, ERR
);
268 if (!(ibuf
[i
++] = c
)) isbinary
= 1;
277 fprintf(stderr
, "stdin: %s\n", strerror(errno
));
278 errmsg
= "cannot read stdin";
297 #define ESCAPES "\a\b\f\n\r\t\v\\"
298 #define ESCCHARS "abfnrtv\\"
303 /* put_tty_line: print text to stdout */
305 put_tty_line(const char *s
, int l
, long n
, int gflag
)
316 if ((gflag
& GLS
) && ++col
> cols
) {
317 fputs("\\\n", stdout
);
320 if (!scripted
&& !isglobal
&& ++lc
> rows
) {
322 fputs("Press <RETURN> to continue... ", stdout
);
324 if (get_tty_line() < 0)
330 if (31 < *s
&& *s
< 127 && *s
!= '\\')
335 if (*s
&& (cp
= strchr(ESCAPES
, *s
)) != NULL
)
336 putchar(ESCCHARS
[cp
- ESCAPES
]);
338 putchar((((unsigned char) *s
& 0300) >> 6) + '0');
339 putchar((((unsigned char) *s
& 070) >> 3) + '0');
340 putchar(((unsigned char) *s
& 07) + '0');