/* io.c: This file contains the i/o routines for the ed line editor */
* Copyright (c) 1993 Andrew Moore, Talke Studio.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char *rcsid
= "@(#)$Id: io.c,v 1.3 1993/12/14 16:19:56 alm Exp $";
/* read_file: read a named file/pipe into the buffer; return line count */
fp
= (*fn
== '!') ? popen(fn
+ 1, "r") : fopen(strip_escapes(fn
), "r");
fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
sprintf(errmsg
, "cannot open input file");
} else if ((size
= read_stream(fp
, n
)) < 0)
else if (((*fn
== '!') ? pclose(fp
) : fclose(fp
)) < 0) {
fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
sprintf(errmsg
, "cannot close input file");
fprintf(stderr
, !scripted
? "%lu\n" : "", size
);
char *sbuf
; /* file i/o buffer */
int sbufsz
; /* file i/o buffer size */
int newline_added
; /* if set, newline appended to input file */
/* read_stream: read a stream into the editor buffer; return status */
line_t
*lp
= get_addressed_line_node(n
);
int o_newline_added
= newline_added
;
int o_isbinary
= isbinary
;
int appended
= (n
== addr_last
);
isbinary
= newline_added
= 0;
for (current_addr
= n
; (len
= get_stream_line(fp
)) > 0; size
+= len
) {
if (put_sbuf_line(sbuf
) == NULL
) {
else if ((up
= push_undo_stack(UADD
, current_addr
,
current_addr
)) == NULL
) {
if (appended
&& size
&& o_isbinary
&& o_newline_added
)
fputs("newline inserted\n", stderr
);
else if (newline_added
&& (!appended
|| !isbinary
&& !o_isbinary
))
fputs("newline appended\n", stderr
);
if (isbinary
&& newline_added
&& !appended
)
newline_added
= appended
? newline_added
: o_newline_added
;
isbinary
= isbinary
| o_isbinary
;
size
+= 8 - size
% 8; /* adjust DES size */
/* get_stream_line: read a line of text from a stream; return line length */
while (((c
= des
? get_des_char(fp
) : getc(fp
)) != EOF
|| !feof(fp
) &&
!ferror(fp
)) && c
!= '\n') {
REALLOC(sbuf
, sbufsz
, i
+ 1, ERR
);
REALLOC(sbuf
, sbufsz
, i
+ 2, ERR
);
fprintf(stderr
, "%s\n", strerror(errno
));
sprintf(errmsg
, "cannot read input file");
return (isbinary
&& newline_added
&& i
) ? --i
: i
;
/* write_file: write a range of lines to a named file/pipe; return line count */
write_file(fn
, mode
, n
, m
)
fp
= (*fn
== '!') ? popen(fn
+1, "w") : fopen(strip_escapes(fn
), mode
);
fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
sprintf(errmsg
, "cannot open output file");
} else if ((size
= write_stream(fp
, n
, m
)) < 0)
else if (((*fn
== '!') ? pclose(fp
) : fclose(fp
)) < 0) {
fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
sprintf(errmsg
, "cannot close output file");
fprintf(stderr
, !scripted
? "%lu\n" : "", size
);
return n
? m
- n
+ 1 : 0;
/* write_stream: write a range of lines to a stream; return status */
line_t
*lp
= get_addressed_line_node(n
);
for (; n
&& n
<= m
; n
++, lp
= lp
->q_forw
) {
if ((s
= get_sbuf_line(lp
)) == NULL
)
if (n
!= addr_last
|| !isbinary
|| !newline_added
)
if (put_stream_line(fp
, s
, len
) < 0)
flush_des_file(fp
); /* flush buffer */
size
+= 8 - size
% 8; /* adjust DES size */
/* put_stream_line: write a line of text to a stream; return status */
put_stream_line(fp
, s
, len
)
if ((des
? put_des_char(*s
++, fp
) : fputc(*s
++, fp
)) < 0) {
fprintf(stderr
, "%s\n", strerror(errno
));
sprintf(errmsg
, "cannot write file");
/* get_extended_line: get a an extended line from stdin */
get_extended_line(sizep
, nonl
)
static char *cvbuf
= NULL
; /* buffer */
static int cvbufsz
= 0; /* buffer size */
if ((l
= t
- ibufp
) < 2 || !has_trailing_escape(ibufp
, ibufp
+ l
- 1)) {
REALLOC(cvbuf
, cvbufsz
, l
, NULL
);
*(cvbuf
+ --l
- 1) = '\n'; /* strip trailing esc */
if (nonl
) l
--; /* strip newline */
if ((n
= get_tty_line()) < 0)
else if (n
== 0 || ibuf
[n
- 1] != '\n') {
sprintf(errmsg
, "unexpected end-of-file");
REALLOC(cvbuf
, cvbufsz
, l
+ n
, NULL
);
memcpy(cvbuf
+ l
, ibuf
, n
);
if (n
< 2 || !has_trailing_escape(cvbuf
, cvbuf
+ l
- 1))
*(cvbuf
+ --l
- 1) = '\n'; /* strip trailing esc */
if (nonl
) l
--; /* strip newline */
REALLOC(cvbuf
, cvbufsz
, l
+ 1, NULL
);
/* get_tty_line: read a line of text from stdin; return line length */
REALLOC(ibuf
, ibufsz
, i
+ 2, ERR
);
if (!(ibuf
[i
++] = c
)) isbinary
= 1;
fprintf(stderr
, "stdin: %s\n", strerror(errno
));
sprintf(errmsg
, "cannot read stdin");
#define ESCAPES "\a\b\f\n\r\t\v\\"
#define ESCCHARS "abfnrtv\\"
/* put_tty_line: print text to stdout */
put_tty_line(s
, l
, n
, gflag
)
if ((gflag
& GLS
) && ++col
> cols
) {
if (!scripted
&& !isglobal
&& ++lc
> rows
) {
fputs("Press <RETURN> to continue... ", stdout
);
if (31 < *s
&& *s
< 127 && *s
!= '\\')
if (*s
&& (cp
= strchr(ESCAPES
, *s
)) != NULL
)
putchar(ESCCHARS
[cp
- ESCAPES
]);
putchar((((unsigned char) *s
& 0300) >> 6) + '0');
putchar((((unsigned char) *s
& 070) >> 3) + '0');
putchar(((unsigned char) *s
& 07) + '0');