io.c revision 1.7 1 1.7 xtraeme /* $NetBSD: io.c,v 1.7 2005/02/17 16:29:26 xtraeme Exp $ */
2 1.2 cgd
3 1.1 alm /* io.c: This file contains the i/o routines for the ed line editor */
4 1.1 alm /*-
5 1.1 alm * Copyright (c) 1993 Andrew Moore, Talke Studio.
6 1.1 alm * All rights reserved.
7 1.1 alm *
8 1.1 alm * Redistribution and use in source and binary forms, with or without
9 1.1 alm * modification, are permitted provided that the following conditions
10 1.1 alm * are met:
11 1.1 alm * 1. Redistributions of source code must retain the above copyright
12 1.1 alm * notice, this list of conditions and the following disclaimer.
13 1.1 alm * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 alm * notice, this list of conditions and the following disclaimer in the
15 1.1 alm * documentation and/or other materials provided with the distribution.
16 1.1 alm *
17 1.1 alm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.1 alm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 alm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 alm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 alm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 alm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 alm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 alm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 alm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 alm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 alm * SUCH DAMAGE.
28 1.1 alm */
29 1.1 alm
30 1.4 thorpej #include <sys/cdefs.h>
31 1.1 alm #ifndef lint
32 1.2 cgd #if 0
33 1.1 alm static char *rcsid = "@(#)io.c,v 1.1 1994/02/01 00:34:41 alm Exp";
34 1.2 cgd #else
35 1.7 xtraeme __RCSID("$NetBSD: io.c,v 1.7 2005/02/17 16:29:26 xtraeme Exp $");
36 1.2 cgd #endif
37 1.1 alm #endif /* not lint */
38 1.1 alm
39 1.1 alm #include "ed.h"
40 1.1 alm
41 1.1 alm
42 1.1 alm /* read_file: read a named file/pipe into the buffer; return line count */
43 1.1 alm long
44 1.7 xtraeme read_file(char *fn, long n)
45 1.1 alm {
46 1.1 alm FILE *fp;
47 1.1 alm long size;
48 1.1 alm
49 1.1 alm
50 1.1 alm fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r");
51 1.1 alm if (fp == NULL) {
52 1.1 alm fprintf(stderr, "%s: %s\n", fn, strerror(errno));
53 1.1 alm sprintf(errmsg, "cannot open input file");
54 1.1 alm return ERR;
55 1.1 alm } else if ((size = read_stream(fp, n)) < 0)
56 1.1 alm return ERR;
57 1.1 alm else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) {
58 1.1 alm fprintf(stderr, "%s: %s\n", fn, strerror(errno));
59 1.1 alm sprintf(errmsg, "cannot close input file");
60 1.1 alm return ERR;
61 1.1 alm }
62 1.1 alm fprintf(stderr, !scripted ? "%lu\n" : "", size);
63 1.1 alm return current_addr - n;
64 1.1 alm }
65 1.1 alm
66 1.1 alm
67 1.1 alm char *sbuf; /* file i/o buffer */
68 1.1 alm int sbufsz; /* file i/o buffer size */
69 1.1 alm int newline_added; /* if set, newline appended to input file */
70 1.1 alm
71 1.1 alm /* read_stream: read a stream into the editor buffer; return status */
72 1.1 alm long
73 1.7 xtraeme read_stream(FILE *fp, long n)
74 1.1 alm {
75 1.1 alm line_t *lp = get_addressed_line_node(n);
76 1.1 alm undo_t *up = NULL;
77 1.1 alm unsigned long size = 0;
78 1.1 alm int o_newline_added = newline_added;
79 1.1 alm int o_isbinary = isbinary;
80 1.1 alm int appended = (n == addr_last);
81 1.1 alm int len;
82 1.1 alm
83 1.1 alm isbinary = newline_added = 0;
84 1.1 alm if (des)
85 1.1 alm init_des_cipher();
86 1.1 alm for (current_addr = n; (len = get_stream_line(fp)) > 0; size += len) {
87 1.1 alm SPL1();
88 1.1 alm if (put_sbuf_line(sbuf) == NULL) {
89 1.1 alm SPL0();
90 1.1 alm return ERR;
91 1.1 alm }
92 1.1 alm lp = lp->q_forw;
93 1.1 alm if (up)
94 1.1 alm up->t = lp;
95 1.1 alm else if ((up = push_undo_stack(UADD, current_addr,
96 1.1 alm current_addr)) == NULL) {
97 1.1 alm SPL0();
98 1.1 alm return ERR;
99 1.1 alm }
100 1.1 alm SPL0();
101 1.1 alm }
102 1.1 alm if (len < 0)
103 1.1 alm return ERR;
104 1.1 alm if (appended && size && o_isbinary && o_newline_added)
105 1.1 alm fputs("newline inserted\n", stderr);
106 1.4 thorpej else if (newline_added && (!appended || (!isbinary && !o_isbinary)))
107 1.1 alm fputs("newline appended\n", stderr);
108 1.1 alm if (isbinary && newline_added && !appended)
109 1.1 alm size += 1;
110 1.1 alm if (!size)
111 1.1 alm newline_added = 1;
112 1.1 alm newline_added = appended ? newline_added : o_newline_added;
113 1.1 alm isbinary = isbinary | o_isbinary;
114 1.1 alm if (des)
115 1.1 alm size += 8 - size % 8; /* adjust DES size */
116 1.1 alm return size;
117 1.1 alm }
118 1.1 alm
119 1.1 alm
120 1.1 alm /* get_stream_line: read a line of text from a stream; return line length */
121 1.1 alm int
122 1.7 xtraeme get_stream_line(FILE *fp)
123 1.1 alm {
124 1.3 tls int c;
125 1.3 tls int i = 0;
126 1.1 alm
127 1.4 thorpej while (((c = des ? get_des_char(fp) : getc(fp)) != EOF || (!feof(fp) &&
128 1.4 thorpej !ferror(fp))) && c != '\n') {
129 1.1 alm REALLOC(sbuf, sbufsz, i + 1, ERR);
130 1.1 alm if (!(sbuf[i++] = c))
131 1.1 alm isbinary = 1;
132 1.1 alm }
133 1.1 alm REALLOC(sbuf, sbufsz, i + 2, ERR);
134 1.1 alm if (c == '\n')
135 1.1 alm sbuf[i++] = c;
136 1.1 alm else if (ferror(fp)) {
137 1.1 alm fprintf(stderr, "%s\n", strerror(errno));
138 1.1 alm sprintf(errmsg, "cannot read input file");
139 1.1 alm return ERR;
140 1.1 alm } else if (i) {
141 1.1 alm sbuf[i++] = '\n';
142 1.1 alm newline_added = 1;
143 1.1 alm }
144 1.1 alm sbuf[i] = '\0';
145 1.1 alm return (isbinary && newline_added && i) ? --i : i;
146 1.1 alm }
147 1.1 alm
148 1.1 alm
149 1.1 alm /* write_file: write a range of lines to a named file/pipe; return line count */
150 1.1 alm long
151 1.7 xtraeme write_file(char *fn, char *mode, long n, long m)
152 1.1 alm {
153 1.1 alm FILE *fp;
154 1.1 alm long size;
155 1.1 alm
156 1.1 alm fp = (*fn == '!') ? popen(fn+1, "w") : fopen(strip_escapes(fn), mode);
157 1.1 alm if (fp == NULL) {
158 1.1 alm fprintf(stderr, "%s: %s\n", fn, strerror(errno));
159 1.1 alm sprintf(errmsg, "cannot open output file");
160 1.1 alm return ERR;
161 1.1 alm } else if ((size = write_stream(fp, n, m)) < 0)
162 1.1 alm return ERR;
163 1.1 alm else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) {
164 1.1 alm fprintf(stderr, "%s: %s\n", fn, strerror(errno));
165 1.1 alm sprintf(errmsg, "cannot close output file");
166 1.1 alm return ERR;
167 1.1 alm }
168 1.1 alm fprintf(stderr, !scripted ? "%lu\n" : "", size);
169 1.1 alm return n ? m - n + 1 : 0;
170 1.1 alm }
171 1.1 alm
172 1.1 alm
173 1.1 alm /* write_stream: write a range of lines to a stream; return status */
174 1.1 alm long
175 1.7 xtraeme write_stream(FILE *fp, long n, long m)
176 1.1 alm {
177 1.1 alm line_t *lp = get_addressed_line_node(n);
178 1.1 alm unsigned long size = 0;
179 1.1 alm char *s;
180 1.1 alm int len;
181 1.1 alm
182 1.1 alm if (des)
183 1.1 alm init_des_cipher();
184 1.1 alm for (; n && n <= m; n++, lp = lp->q_forw) {
185 1.1 alm if ((s = get_sbuf_line(lp)) == NULL)
186 1.1 alm return ERR;
187 1.1 alm len = lp->len;
188 1.1 alm if (n != addr_last || !isbinary || !newline_added)
189 1.1 alm s[len++] = '\n';
190 1.1 alm if (put_stream_line(fp, s, len) < 0)
191 1.1 alm return ERR;
192 1.1 alm size += len;
193 1.1 alm }
194 1.1 alm if (des) {
195 1.1 alm flush_des_file(fp); /* flush buffer */
196 1.1 alm size += 8 - size % 8; /* adjust DES size */
197 1.1 alm }
198 1.1 alm return size;
199 1.1 alm }
200 1.1 alm
201 1.1 alm
202 1.1 alm /* put_stream_line: write a line of text to a stream; return status */
203 1.1 alm int
204 1.7 xtraeme put_stream_line(FILE *fp, char *s, int len)
205 1.1 alm {
206 1.1 alm while (len--)
207 1.1 alm if ((des ? put_des_char(*s++, fp) : fputc(*s++, fp)) < 0) {
208 1.1 alm fprintf(stderr, "%s\n", strerror(errno));
209 1.1 alm sprintf(errmsg, "cannot write file");
210 1.1 alm return ERR;
211 1.1 alm }
212 1.1 alm return 0;
213 1.1 alm }
214 1.1 alm
215 1.1 alm /* get_extended_line: get a an extended line from stdin */
216 1.1 alm char *
217 1.7 xtraeme get_extended_line(int *sizep, int nonl)
218 1.1 alm {
219 1.1 alm static char *cvbuf = NULL; /* buffer */
220 1.1 alm static int cvbufsz = 0; /* buffer size */
221 1.1 alm
222 1.1 alm int l, n;
223 1.1 alm char *t = ibufp;
224 1.1 alm
225 1.1 alm while (*t++ != '\n')
226 1.1 alm ;
227 1.1 alm if ((l = t - ibufp) < 2 || !has_trailing_escape(ibufp, ibufp + l - 1)) {
228 1.1 alm *sizep = l;
229 1.1 alm return ibufp;
230 1.1 alm }
231 1.1 alm *sizep = -1;
232 1.1 alm REALLOC(cvbuf, cvbufsz, l, NULL);
233 1.1 alm memcpy(cvbuf, ibufp, l);
234 1.1 alm *(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
235 1.1 alm if (nonl) l--; /* strip newline */
236 1.1 alm for (;;) {
237 1.1 alm if ((n = get_tty_line()) < 0)
238 1.1 alm return NULL;
239 1.1 alm else if (n == 0 || ibuf[n - 1] != '\n') {
240 1.1 alm sprintf(errmsg, "unexpected end-of-file");
241 1.1 alm return NULL;
242 1.1 alm }
243 1.1 alm REALLOC(cvbuf, cvbufsz, l + n, NULL);
244 1.1 alm memcpy(cvbuf + l, ibuf, n);
245 1.1 alm l += n;
246 1.1 alm if (n < 2 || !has_trailing_escape(cvbuf, cvbuf + l - 1))
247 1.1 alm break;
248 1.1 alm *(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
249 1.1 alm if (nonl) l--; /* strip newline */
250 1.1 alm }
251 1.1 alm REALLOC(cvbuf, cvbufsz, l + 1, NULL);
252 1.1 alm cvbuf[l] = '\0';
253 1.1 alm *sizep = l;
254 1.1 alm return cvbuf;
255 1.1 alm }
256 1.1 alm
257 1.1 alm
258 1.1 alm /* get_tty_line: read a line of text from stdin; return line length */
259 1.1 alm int
260 1.7 xtraeme get_tty_line(void)
261 1.1 alm {
262 1.3 tls int oi = 0;
263 1.3 tls int i = 0;
264 1.1 alm int c;
265 1.1 alm
266 1.1 alm for (;;)
267 1.1 alm switch (c = getchar()) {
268 1.1 alm default:
269 1.1 alm oi = 0;
270 1.1 alm REALLOC(ibuf, ibufsz, i + 2, ERR);
271 1.1 alm if (!(ibuf[i++] = c)) isbinary = 1;
272 1.1 alm if (c != '\n')
273 1.1 alm continue;
274 1.1 alm lineno++;
275 1.1 alm ibuf[i] = '\0';
276 1.1 alm ibufp = ibuf;
277 1.1 alm return i;
278 1.1 alm case EOF:
279 1.1 alm if (ferror(stdin)) {
280 1.1 alm fprintf(stderr, "stdin: %s\n", strerror(errno));
281 1.1 alm sprintf(errmsg, "cannot read stdin");
282 1.1 alm clearerr(stdin);
283 1.1 alm ibufp = NULL;
284 1.1 alm return ERR;
285 1.1 alm } else {
286 1.1 alm clearerr(stdin);
287 1.1 alm if (i != oi) {
288 1.1 alm oi = i;
289 1.1 alm continue;
290 1.1 alm } else if (i)
291 1.1 alm ibuf[i] = '\0';
292 1.1 alm ibufp = ibuf;
293 1.1 alm return i;
294 1.1 alm }
295 1.1 alm }
296 1.1 alm }
297 1.1 alm
298 1.1 alm
299 1.1 alm
300 1.1 alm #define ESCAPES "\a\b\f\n\r\t\v\\"
301 1.1 alm #define ESCCHARS "abfnrtv\\"
302 1.1 alm
303 1.1 alm /* put_tty_line: print text to stdout */
304 1.1 alm int
305 1.7 xtraeme put_tty_line(char *s, int l, long n, int gflag)
306 1.1 alm {
307 1.1 alm int col = 0;
308 1.1 alm char *cp;
309 1.6 christos #ifndef BACKWARDS
310 1.6 christos int lc = 0;
311 1.6 christos #endif
312 1.1 alm
313 1.1 alm if (gflag & GNP) {
314 1.1 alm printf("%ld\t", n);
315 1.1 alm col = 8;
316 1.1 alm }
317 1.1 alm for (; l--; s++) {
318 1.1 alm if ((gflag & GLS) && ++col > cols) {
319 1.1 alm fputs("\\\n", stdout);
320 1.1 alm col = 1;
321 1.1 alm #ifndef BACKWARDS
322 1.1 alm if (!scripted && !isglobal && ++lc > rows) {
323 1.1 alm lc = 0;
324 1.1 alm fputs("Press <RETURN> to continue... ", stdout);
325 1.1 alm fflush(stdout);
326 1.1 alm if (get_tty_line() < 0)
327 1.1 alm return ERR;
328 1.1 alm }
329 1.1 alm #endif
330 1.1 alm }
331 1.1 alm if (gflag & GLS) {
332 1.1 alm if (31 < *s && *s < 127 && *s != '\\')
333 1.1 alm putchar(*s);
334 1.1 alm else {
335 1.1 alm putchar('\\');
336 1.1 alm col++;
337 1.1 alm if (*s && (cp = strchr(ESCAPES, *s)) != NULL)
338 1.1 alm putchar(ESCCHARS[cp - ESCAPES]);
339 1.1 alm else {
340 1.1 alm putchar((((unsigned char) *s & 0300) >> 6) + '0');
341 1.1 alm putchar((((unsigned char) *s & 070) >> 3) + '0');
342 1.1 alm putchar(((unsigned char) *s & 07) + '0');
343 1.1 alm col += 2;
344 1.1 alm }
345 1.1 alm }
346 1.1 alm
347 1.1 alm } else
348 1.1 alm putchar(*s);
349 1.1 alm }
350 1.1 alm #ifndef BACKWARDS
351 1.1 alm if (gflag & GLS)
352 1.1 alm putchar('$');
353 1.1 alm #endif
354 1.1 alm putchar('\n');
355 1.1 alm return 0;
356 1.1 alm }
357