]>
git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/ed/io.c
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: src/bin/ed/io.c,v 1.14 2003/01/01 18:48:39 schweikh Exp $");
36 /* read_file: read a named file/pipe into the buffer; return line count */
38 read_file(char *fn
, long n
)
44 fp
= (*fn
== '!') ? popen(fn
+ 1, "r") : fopen(strip_escapes(fn
), "r");
46 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
47 errmsg
= "cannot open input file";
49 } else if ((size
= read_stream(fp
, n
)) < 0)
51 else if (((*fn
== '!') ? pclose(fp
) : fclose(fp
)) < 0) {
52 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
53 errmsg
= "cannot close input file";
56 fprintf(stdout
, !scripted
? "%lu\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
, long n
)
71 line_t
*lp
= get_addressed_line_node(n
);
73 unsigned long 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;
82 for (current_addr
= n
; (len
= get_stream_line(fp
)) > 0; size
+= len
) {
84 if (put_sbuf_line(sbuf
) == NULL
) {
91 else if ((up
= push_undo_stack(UADD
, current_addr
,
92 current_addr
)) == NULL
) {
100 if (appended
&& size
&& o_isbinary
&& o_newline_added
)
101 fputs("newline inserted\n", stderr
);
102 else if (newline_added
&& (!appended
|| (!isbinary
&& !o_isbinary
)))
103 fputs("newline appended\n", stderr
);
104 if (isbinary
&& newline_added
&& !appended
)
108 newline_added
= appended
? newline_added
: o_newline_added
;
109 isbinary
= isbinary
| o_isbinary
;
111 size
+= 8 - size
% 8; /* adjust DES size */
116 /* get_stream_line: read a line of text from a stream; return line length */
118 get_stream_line(FILE *fp
)
123 while (((c
= des
? get_des_char(fp
) : getc(fp
)) != EOF
|| (!feof(fp
) &&
124 !ferror(fp
))) && c
!= '\n') {
125 REALLOC(sbuf
, sbufsz
, i
+ 1, ERR
);
126 if (!(sbuf
[i
++] = c
))
129 REALLOC(sbuf
, sbufsz
, i
+ 2, ERR
);
132 else if (ferror(fp
)) {
133 fprintf(stderr
, "%s\n", strerror(errno
));
134 errmsg
= "cannot read input file";
141 return (isbinary
&& newline_added
&& i
) ? --i
: i
;
145 /* write_file: write a range of lines to a named file/pipe; return line count */
147 write_file(char *fn
, const char *mode
, long n
, long m
)
152 fp
= (*fn
== '!') ? popen(fn
+1, "w") : fopen(strip_escapes(fn
), mode
);
154 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
155 errmsg
= "cannot open output file";
157 } else if ((size
= write_stream(fp
, n
, m
)) < 0)
159 else if (((*fn
== '!') ? pclose(fp
) : fclose(fp
)) < 0) {
160 fprintf(stderr
, "%s: %s\n", fn
, strerror(errno
));
161 errmsg
= "cannot close output file";
164 fprintf(stdout
, !scripted
? "%lu\n" : "", size
);
165 return n
? m
- n
+ 1 : 0;
169 /* write_stream: write a range of lines to a stream; return status */
171 write_stream(FILE *fp
, long n
, long m
)
173 line_t
*lp
= get_addressed_line_node(n
);
174 unsigned long size
= 0;
180 for (; n
&& n
<= m
; n
++, lp
= lp
->q_forw
) {
181 if ((s
= get_sbuf_line(lp
)) == NULL
)
184 if (n
!= addr_last
|| !isbinary
|| !newline_added
)
186 if (put_stream_line(fp
, s
, len
) < 0)
191 flush_des_file(fp
); /* flush buffer */
192 size
+= 8 - size
% 8; /* adjust DES size */
198 /* put_stream_line: write a line of text to a stream; return status */
200 put_stream_line(FILE *fp
, const char *s
, int len
)
203 if ((des
? put_des_char(*s
++, fp
) : fputc(*s
++, fp
)) < 0) {
204 fprintf(stderr
, "%s\n", strerror(errno
));
205 errmsg
= "cannot write file";
211 /* get_extended_line: get an extended line from stdin */
213 get_extended_line(int *sizep
, int nonl
)
215 static char *cvbuf
= NULL
; /* buffer */
216 static int cvbufsz
= 0; /* buffer size */
223 if ((l
= t
- ibufp
) < 2 || !has_trailing_escape(ibufp
, ibufp
+ l
- 1)) {
228 REALLOC(cvbuf
, cvbufsz
, l
, NULL
);
229 memcpy(cvbuf
, ibufp
, l
);
230 *(cvbuf
+ --l
- 1) = '\n'; /* strip trailing esc */
231 if (nonl
) l
--; /* strip newline */
233 if ((n
= get_tty_line()) < 0)
235 else if (n
== 0 || ibuf
[n
- 1] != '\n') {
236 errmsg
= "unexpected end-of-file";
239 REALLOC(cvbuf
, cvbufsz
, l
+ n
, NULL
);
240 memcpy(cvbuf
+ l
, ibuf
, n
);
242 if (n
< 2 || !has_trailing_escape(cvbuf
, cvbuf
+ l
- 1))
244 *(cvbuf
+ --l
- 1) = '\n'; /* strip trailing esc */
245 if (nonl
) l
--; /* strip newline */
247 REALLOC(cvbuf
, cvbufsz
, l
+ 1, NULL
);
254 /* get_tty_line: read a line of text from stdin; return line length */
263 switch (c
= getchar()) {
266 REALLOC(ibuf
, ibufsz
, i
+ 2, ERR
);
267 if (!(ibuf
[i
++] = c
)) isbinary
= 1;
276 fprintf(stderr
, "stdin: %s\n", strerror(errno
));
277 errmsg
= "cannot read stdin";
296 #define ESCAPES "\a\b\f\n\r\t\v\\"
297 #define ESCCHARS "abfnrtv\\"
302 /* put_tty_line: print text to stdout */
304 put_tty_line(const char *s
, int l
, long n
, int gflag
)
309 if ((gflag
& GNP
) && !(gflag
& GINT
)) {
314 if ((gflag
& GLS
) && ++col
> cols
) {
315 fputs("\\\n", stdout
);
319 if (31 < *s
&& *s
< 127 && *s
!= '\\')
324 if (*s
&& (cp
= strchr(ESCAPES
, *s
)) != NULL
)
325 putchar(ESCCHARS
[cp
- ESCAPES
]);
327 putchar((((unsigned char) *s
& 0300) >> 6) + '0');
328 putchar((((unsigned char) *s
& 070) >> 3) + '0');
329 putchar(((unsigned char) *s
& 07) + '0');
337 if (posixly_correct
&& (gflag
& GLS
) && !(gflag
& GINT
))