1 /* $OpenBSD: io.c,v 1.15 2009/10/28 15:40:47 deraadt Exp $ */
2 /* $NetBSD: io.c,v 1.2 1995/03/21 09:04:43 cgd Exp $ */
4 /* io.c: This file contains the i/o routines for the ed line editor */
6 * Copyright (c) 1993 Andrew Moore, Talke Studio.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 /* read_file: read a named file/pipe into the buffer; return line count */
38 read_file(char *fn
, int n
)
44 fp
= (*fn
== '!') ? popen(fn
+ 1, "r") : fopen(strip_escapes(fn
), "r");
47 seterrmsg("cannot open input file");
49 } else if ((size
= read_stream(fp
, n
)) < 0)
51 else if (((*fn
== '!') ? pclose(fp
) : fclose(fp
)) < 0) {
53 seterrmsg("cannot close input file");
56 fprintf(stderr
, !scripted
? "%d\n" : "", size
);
57 return current_addr
- n
;
63 char *sbuf
; /* file i/o buffer */
64 int sbufsz
; /* file i/o buffer size */
65 int newline_added
; /* if set, newline appended to input file */
67 /* read_stream: read a stream into the editor buffer; return status */
69 read_stream(FILE *fp
, int n
)
71 line_t
*lp
= get_addressed_line_node(n
);
73 unsigned int size
= 0;
74 int o_newline_added
= newline_added
;
75 int o_isbinary
= isbinary
;
76 int appended
= (n
== addr_last
);
79 isbinary
= newline_added
= 0;
84 for (current_addr
= n
; (len
= get_stream_line(fp
)) > 0; size
+= len
) {
86 if (put_sbuf_line(sbuf
) == NULL
) {
93 else if ((up
= push_undo_stack(UADD
, current_addr
,
94 current_addr
)) == NULL
) {
102 if (appended
&& size
&& o_isbinary
&& o_newline_added
)
103 fputs("newline inserted\n", stderr
);
104 else if (newline_added
&& (!appended
|| (!isbinary
&& !o_isbinary
)))
105 fputs("newline appended\n", stderr
);
106 if (isbinary
&& newline_added
&& !appended
)
110 newline_added
= appended
? newline_added
: o_newline_added
;
111 isbinary
= isbinary
| o_isbinary
;
113 size
+= 8 - size
% 8; /* adjust DES size */
118 #define DESGETCHAR(fp) (des ? get_des_char((fp)) : getc((fp)))
120 #define DESGETCHAR(fp) (getc((fp)))
123 /* get_stream_line: read a line of text from a stream; return line length */
125 get_stream_line(FILE *fp
)
130 while (((c
= DESGETCHAR(fp
)) != EOF
|| (!feof(fp
) &&
131 !ferror(fp
))) && c
!= '\n') {
132 REALLOC(sbuf
, sbufsz
, i
+ 1, ERR
);
133 if (!(sbuf
[i
++] = c
))
136 REALLOC(sbuf
, sbufsz
, i
+ 2, ERR
);
139 else if (ferror(fp
)) {
141 seterrmsg("cannot read input file");
148 return (isbinary
&& newline_added
&& i
) ? --i
: i
;
152 /* write_file: write a range of lines to a named file/pipe; return line count */
154 write_file(char *fn
, char *mode
, int n
, int m
)
159 fp
= (*fn
== '!') ? popen(fn
+1, "w") : fopen(strip_escapes(fn
), mode
);
162 seterrmsg("cannot open output file");
164 } else if ((size
= write_stream(fp
, n
, m
)) < 0)
166 else if (((*fn
== '!') ? pclose(fp
) : fclose(fp
)) < 0) {
168 seterrmsg("cannot close output file");
171 fprintf(stderr
, !scripted
? "%d\n" : "", size
);
172 return n
? m
- n
+ 1 : 0;
176 /* write_stream: write a range of lines to a stream; return status */
178 write_stream(FILE *fp
, int n
, int m
)
180 line_t
*lp
= get_addressed_line_node(n
);
181 unsigned int size
= 0;
189 for (; n
&& n
<= m
; n
++, lp
= lp
->q_forw
) {
190 if ((s
= get_sbuf_line(lp
)) == NULL
)
193 if (n
!= addr_last
|| !isbinary
|| !newline_added
)
195 if (put_stream_line(fp
, s
, len
) < 0)
201 flush_des_file(fp
); /* flush buffer */
202 size
+= 8 - size
% 8; /* adjust DES size */
209 #define DESPUTCHAR(c, fp) (des ? put_des_char((c), (fp)) : fputc((c), (fp)))
211 #define DESPUTCHAR(c, fp) (fputc((c), (fp)))
215 /* put_stream_line: write a line of text to a stream; return status */
217 put_stream_line(FILE *fp
, char *s
, int len
)
220 if (DESPUTCHAR(*s
, fp
) < 0) {
222 seterrmsg("cannot write file");
230 /* get_extended_line: get a an extended line from stdin */
232 get_extended_line(int *sizep
, int nonl
)
234 static char *cvbuf
= NULL
; /* buffer */
235 static int cvbufsz
= 0; /* buffer size */
242 if ((l
= t
- ibufp
) < 2 || !has_trailing_escape(ibufp
, ibufp
+ l
- 1)) {
247 REALLOC(cvbuf
, cvbufsz
, l
, NULL
);
248 memcpy(cvbuf
, ibufp
, l
);
249 *(cvbuf
+ --l
- 1) = '\n'; /* strip trailing esc */
251 l
--; /* strip newline */
253 if ((n
= get_tty_line()) < 0)
255 else if (n
== 0 || ibuf
[n
- 1] != '\n') {
256 seterrmsg("unexpected end-of-file");
259 REALLOC(cvbuf
, cvbufsz
, l
+ n
, NULL
);
260 memcpy(cvbuf
+ l
, ibuf
, n
);
262 if (n
< 2 || !has_trailing_escape(cvbuf
, cvbuf
+ l
- 1))
264 *(cvbuf
+ --l
- 1) = '\n'; /* strip trailing esc */
265 if (nonl
) l
--; /* strip newline */
267 REALLOC(cvbuf
, cvbufsz
, l
+ 1, NULL
);
274 /* get_tty_line: read a line of text from stdin; return line length */
283 switch (c
= getchar()) {
286 REALLOC(ibuf
, ibufsz
, i
+ 2, ERR
);
287 if (!(ibuf
[i
++] = c
)) isbinary
= 1;
297 seterrmsg("cannot read stdin");
316 #define ESCAPES "\a\b\f\n\r\t\v\\"
317 #define ESCCHARS "abfnrtv\\"
322 /* put_tty_line: print text to stdout */
324 put_tty_line(char *s
, int l
, int n
, int gflag
)
337 if ((gflag
& GLS
) && ++col
> cols
) {
338 fputs("\\\n", stdout
);
341 if (!scripted
&& !isglobal
&& ++lc
> rows
) {
343 fputs("Press <RETURN> to continue... ", stdout
);
345 if (get_tty_line() < 0)
351 if (31 < *s
&& *s
< 127 && *s
!= '\\')
356 if (*s
&& (cp
= strchr(ESCAPES
, *s
)) != NULL
)
357 putchar(ESCCHARS
[cp
- ESCAPES
]);
359 putchar((((unsigned char) *s
& 0300) >> 6) + '0');
360 putchar((((unsigned char) *s
& 070) >> 3) + '0');
361 putchar(((unsigned char) *s
& 07) + '0');