1 /* $NetBSD: io.c,v 1.7 2005/02/17 16:29:26 xtraeme Exp $ */
3 /* io.c: This file contains the i/o routines for the ed line editor */
5 * Copyright (c) 1993 Andrew Moore, Talke Studio.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
33 static char *rcsid
= "@(#)io.c,v 1.1 1994/02/01 00:34:41 alm Exp";
35 __RCSID("$NetBSD: io.c,v 1.7 2005/02/17 16:29:26 xtraeme Exp $");
42 /* read_file: read a named file/pipe into the buffer; return line count */
44 read_file(char *fn
, long n
)
50 fp
= (*fn
== '!') ? popen(fn
+ 1, "r") : fopen(strip_escapes(fn
), "r");
52 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
53 sprintf(errmsg
, "cannot open input file");
55 } else if ((size
= read_stream(fp
, n
)) < 0)
57 else if (((*fn
== '!') ? pclose(fp
) : fclose(fp
)) < 0) {
58 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
59 sprintf(errmsg
, "cannot close input file");
62 fprintf(stderr
, !scripted
? "%lu\n" : "", size
);
63 return current_addr
- n
;
67 char *sbuf
; /* file i/o buffer */
68 int sbufsz
; /* file i/o buffer size */
69 int newline_added
; /* if set, newline appended to input file */
71 /* read_stream: read a stream into the editor buffer; return status */
73 read_stream(FILE *fp
, long n
)
75 line_t
*lp
= get_addressed_line_node(n
);
77 unsigned long size
= 0;
78 int o_newline_added
= newline_added
;
79 int o_isbinary
= isbinary
;
80 int appended
= (n
== addr_last
);
83 isbinary
= newline_added
= 0;
86 for (current_addr
= n
; (len
= get_stream_line(fp
)) > 0; size
+= len
) {
88 if (put_sbuf_line(sbuf
) == NULL
) {
95 else if ((up
= push_undo_stack(UADD
, current_addr
,
96 current_addr
)) == NULL
) {
104 if (appended
&& size
&& o_isbinary
&& o_newline_added
)
105 fputs("newline inserted\n", stderr
);
106 else if (newline_added
&& (!appended
|| (!isbinary
&& !o_isbinary
)))
107 fputs("newline appended\n", stderr
);
108 if (isbinary
&& newline_added
&& !appended
)
112 newline_added
= appended
? newline_added
: o_newline_added
;
113 isbinary
= isbinary
| o_isbinary
;
115 size
+= 8 - size
% 8; /* adjust DES size */
120 /* get_stream_line: read a line of text from a stream; return line length */
122 get_stream_line(FILE *fp
)
127 while (((c
= des
? get_des_char(fp
) : getc(fp
)) != EOF
|| (!feof(fp
) &&
128 !ferror(fp
))) && c
!= '\n') {
129 REALLOC(sbuf
, sbufsz
, i
+ 1, ERR
);
130 if (!(sbuf
[i
++] = c
))
133 REALLOC(sbuf
, sbufsz
, i
+ 2, ERR
);
136 else if (ferror(fp
)) {
137 fprintf(stderr
, "%s\n", strerror(errno
));
138 sprintf(errmsg
, "cannot read input file");
145 return (isbinary
&& newline_added
&& i
) ? --i
: i
;
149 /* write_file: write a range of lines to a named file/pipe; return line count */
151 write_file(const char *fn
, const char *mode
, long n
, long m
)
156 fp
= (*fn
== '!') ? popen(fn
+1, "w") : fopen(strip_escapes(fn
), mode
);
158 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
159 sprintf(errmsg
, "cannot open output file");
161 } else if ((size
= write_stream(fp
, n
, m
)) < 0)
163 else if (((*fn
== '!') ? pclose(fp
) : fclose(fp
)) < 0) {
164 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
165 sprintf(errmsg
, "cannot close output file");
168 fprintf(stderr
, !scripted
? "%lu\n" : "", size
);
169 return n
? m
- n
+ 1 : 0;
173 /* write_stream: write a range of lines to a stream; return status */
175 write_stream(FILE *fp
, long n
, long m
)
177 line_t
*lp
= get_addressed_line_node(n
);
178 unsigned long size
= 0;
184 for (; n
&& n
<= m
; n
++, lp
= lp
->q_forw
) {
185 if ((s
= get_sbuf_line(lp
)) == NULL
)
188 if (n
!= addr_last
|| !isbinary
|| !newline_added
)
190 if (put_stream_line(fp
, s
, len
) < 0)
195 flush_des_file(fp
); /* flush buffer */
196 size
+= 8 - size
% 8; /* adjust DES size */
202 /* put_stream_line: write a line of text to a stream; return status */
204 put_stream_line(FILE *fp
, char *s
, int len
)
207 if ((des
? put_des_char(*s
++, fp
) : fputc(*s
++, fp
)) < 0) {
208 fprintf(stderr
, "%s\n", strerror(errno
));
209 sprintf(errmsg
, "cannot write file");
215 /* get_extended_line: get a an extended line from stdin */
217 get_extended_line(int *sizep
, int nonl
)
219 static char *cvbuf
= NULL
; /* buffer */
220 static int cvbufsz
= 0; /* buffer size */
227 if ((l
= t
- ibufp
) < 2 || !has_trailing_escape(ibufp
, ibufp
+ l
- 1)) {
232 REALLOC(cvbuf
, cvbufsz
, l
, NULL
);
233 memcpy(cvbuf
, ibufp
, l
);
234 *(cvbuf
+ --l
- 1) = '\n'; /* strip trailing esc */
235 if (nonl
) l
--; /* strip newline */
237 if ((n
= get_tty_line()) < 0)
239 else if (n
== 0 || ibuf
[n
- 1] != '\n') {
240 sprintf(errmsg
, "unexpected end-of-file");
243 REALLOC(cvbuf
, cvbufsz
, l
+ n
, NULL
);
244 memcpy(cvbuf
+ l
, ibuf
, n
);
246 if (n
< 2 || !has_trailing_escape(cvbuf
, cvbuf
+ l
- 1))
248 *(cvbuf
+ --l
- 1) = '\n'; /* strip trailing esc */
249 if (nonl
) l
--; /* strip newline */
251 REALLOC(cvbuf
, cvbufsz
, l
+ 1, NULL
);
258 /* get_tty_line: read a line of text from stdin; return line length */
267 switch (c
= getchar()) {
270 REALLOC(ibuf
, ibufsz
, i
+ 2, ERR
);
271 if (!(ibuf
[i
++] = c
)) isbinary
= 1;
280 fprintf(stderr
, "stdin: %s\n", strerror(errno
));
281 sprintf(errmsg
, "cannot read stdin");
300 #define ESCAPES "\a\b\f\n\r\t\v\\"
301 #define ESCCHARS "abfnrtv\\"
303 /* put_tty_line: print text to stdout */
305 put_tty_line(char *s
, int l
, long n
, int gflag
)
318 if ((gflag
& GLS
) && ++col
> cols
) {
319 fputs("\\\n", stdout
);
322 if (!scripted
&& !isglobal
&& ++lc
> rows
) {
324 fputs("Press <RETURN> to continue... ", stdout
);
326 if (get_tty_line() < 0)
332 if (31 < *s
&& *s
< 127 && *s
!= '\\')
337 if (*s
&& (cp
= strchr(ESCAPES
, *s
)) != NULL
)
338 putchar(ESCCHARS
[cp
- ESCAPES
]);
340 putchar((((unsigned char) *s
& 0300) >> 6) + '0');
341 putchar((((unsigned char) *s
& 070) >> 3) + '0');
342 putchar(((unsigned char) *s
& 07) + '0');