io.c revision 1.6 1 1.6 christos /* $NetBSD: io.c,v 1.6 2000/04/17 23:37:30 christos 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.6 christos __RCSID("$NetBSD: io.c,v 1.6 2000/04/17 23:37:30 christos 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.1 alm read_file(fn, n)
45 1.1 alm char *fn;
46 1.1 alm long n;
47 1.1 alm {
48 1.1 alm FILE *fp;
49 1.1 alm long size;
50 1.1 alm
51 1.1 alm
52 1.1 alm fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r");
53 1.1 alm if (fp == NULL) {
54 1.1 alm fprintf(stderr, "%s: %s\n", fn, strerror(errno));
55 1.1 alm sprintf(errmsg, "cannot open input file");
56 1.1 alm return ERR;
57 1.1 alm } else if ((size = read_stream(fp, n)) < 0)
58 1.1 alm return ERR;
59 1.1 alm else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) {
60 1.1 alm fprintf(stderr, "%s: %s\n", fn, strerror(errno));
61 1.1 alm sprintf(errmsg, "cannot close input file");
62 1.1 alm return ERR;
63 1.1 alm }
64 1.1 alm fprintf(stderr, !scripted ? "%lu\n" : "", size);
65 1.1 alm return current_addr - n;
66 1.1 alm }
67 1.1 alm
68 1.1 alm
69 1.1 alm char *sbuf; /* file i/o buffer */
70 1.1 alm int sbufsz; /* file i/o buffer size */
71 1.1 alm int newline_added; /* if set, newline appended to input file */
72 1.1 alm
73 1.1 alm /* read_stream: read a stream into the editor buffer; return status */
74 1.1 alm long
75 1.1 alm read_stream(fp, n)
76 1.1 alm FILE *fp;
77 1.1 alm long n;
78 1.1 alm {
79 1.1 alm line_t *lp = get_addressed_line_node(n);
80 1.1 alm undo_t *up = NULL;
81 1.1 alm unsigned long size = 0;
82 1.1 alm int o_newline_added = newline_added;
83 1.1 alm int o_isbinary = isbinary;
84 1.1 alm int appended = (n == addr_last);
85 1.1 alm int len;
86 1.1 alm
87 1.1 alm isbinary = newline_added = 0;
88 1.1 alm if (des)
89 1.1 alm init_des_cipher();
90 1.1 alm for (current_addr = n; (len = get_stream_line(fp)) > 0; size += len) {
91 1.1 alm SPL1();
92 1.1 alm if (put_sbuf_line(sbuf) == NULL) {
93 1.1 alm SPL0();
94 1.1 alm return ERR;
95 1.1 alm }
96 1.1 alm lp = lp->q_forw;
97 1.1 alm if (up)
98 1.1 alm up->t = lp;
99 1.1 alm else if ((up = push_undo_stack(UADD, current_addr,
100 1.1 alm current_addr)) == NULL) {
101 1.1 alm SPL0();
102 1.1 alm return ERR;
103 1.1 alm }
104 1.1 alm SPL0();
105 1.1 alm }
106 1.1 alm if (len < 0)
107 1.1 alm return ERR;
108 1.1 alm if (appended && size && o_isbinary && o_newline_added)
109 1.1 alm fputs("newline inserted\n", stderr);
110 1.4 thorpej else if (newline_added && (!appended || (!isbinary && !o_isbinary)))
111 1.1 alm fputs("newline appended\n", stderr);
112 1.1 alm if (isbinary && newline_added && !appended)
113 1.1 alm size += 1;
114 1.1 alm if (!size)
115 1.1 alm newline_added = 1;
116 1.1 alm newline_added = appended ? newline_added : o_newline_added;
117 1.1 alm isbinary = isbinary | o_isbinary;
118 1.1 alm if (des)
119 1.1 alm size += 8 - size % 8; /* adjust DES size */
120 1.1 alm return size;
121 1.1 alm }
122 1.1 alm
123 1.1 alm
124 1.1 alm /* get_stream_line: read a line of text from a stream; return line length */
125 1.1 alm int
126 1.1 alm get_stream_line(fp)
127 1.1 alm FILE *fp;
128 1.1 alm {
129 1.3 tls int c;
130 1.3 tls int i = 0;
131 1.1 alm
132 1.4 thorpej while (((c = des ? get_des_char(fp) : getc(fp)) != EOF || (!feof(fp) &&
133 1.4 thorpej !ferror(fp))) && c != '\n') {
134 1.1 alm REALLOC(sbuf, sbufsz, i + 1, ERR);
135 1.1 alm if (!(sbuf[i++] = c))
136 1.1 alm isbinary = 1;
137 1.1 alm }
138 1.1 alm REALLOC(sbuf, sbufsz, i + 2, ERR);
139 1.1 alm if (c == '\n')
140 1.1 alm sbuf[i++] = c;
141 1.1 alm else if (ferror(fp)) {
142 1.1 alm fprintf(stderr, "%s\n", strerror(errno));
143 1.1 alm sprintf(errmsg, "cannot read input file");
144 1.1 alm return ERR;
145 1.1 alm } else if (i) {
146 1.1 alm sbuf[i++] = '\n';
147 1.1 alm newline_added = 1;
148 1.1 alm }
149 1.1 alm sbuf[i] = '\0';
150 1.1 alm return (isbinary && newline_added && i) ? --i : i;
151 1.1 alm }
152 1.1 alm
153 1.1 alm
154 1.1 alm /* write_file: write a range of lines to a named file/pipe; return line count */
155 1.1 alm long
156 1.1 alm write_file(fn, mode, n, m)
157 1.1 alm char *fn;
158 1.1 alm char *mode;
159 1.1 alm long n;
160 1.1 alm long m;
161 1.1 alm {
162 1.1 alm FILE *fp;
163 1.1 alm long size;
164 1.1 alm
165 1.1 alm fp = (*fn == '!') ? popen(fn+1, "w") : fopen(strip_escapes(fn), mode);
166 1.1 alm if (fp == NULL) {
167 1.1 alm fprintf(stderr, "%s: %s\n", fn, strerror(errno));
168 1.1 alm sprintf(errmsg, "cannot open output file");
169 1.1 alm return ERR;
170 1.1 alm } else if ((size = write_stream(fp, n, m)) < 0)
171 1.1 alm return ERR;
172 1.1 alm else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) {
173 1.1 alm fprintf(stderr, "%s: %s\n", fn, strerror(errno));
174 1.1 alm sprintf(errmsg, "cannot close output file");
175 1.1 alm return ERR;
176 1.1 alm }
177 1.1 alm fprintf(stderr, !scripted ? "%lu\n" : "", size);
178 1.1 alm return n ? m - n + 1 : 0;
179 1.1 alm }
180 1.1 alm
181 1.1 alm
182 1.1 alm /* write_stream: write a range of lines to a stream; return status */
183 1.1 alm long
184 1.1 alm write_stream(fp, n, m)
185 1.1 alm FILE *fp;
186 1.1 alm long n;
187 1.1 alm long m;
188 1.1 alm {
189 1.1 alm line_t *lp = get_addressed_line_node(n);
190 1.1 alm unsigned long size = 0;
191 1.1 alm char *s;
192 1.1 alm int len;
193 1.1 alm
194 1.1 alm if (des)
195 1.1 alm init_des_cipher();
196 1.1 alm for (; n && n <= m; n++, lp = lp->q_forw) {
197 1.1 alm if ((s = get_sbuf_line(lp)) == NULL)
198 1.1 alm return ERR;
199 1.1 alm len = lp->len;
200 1.1 alm if (n != addr_last || !isbinary || !newline_added)
201 1.1 alm s[len++] = '\n';
202 1.1 alm if (put_stream_line(fp, s, len) < 0)
203 1.1 alm return ERR;
204 1.1 alm size += len;
205 1.1 alm }
206 1.1 alm if (des) {
207 1.1 alm flush_des_file(fp); /* flush buffer */
208 1.1 alm size += 8 - size % 8; /* adjust DES size */
209 1.1 alm }
210 1.1 alm return size;
211 1.1 alm }
212 1.1 alm
213 1.1 alm
214 1.1 alm /* put_stream_line: write a line of text to a stream; return status */
215 1.1 alm int
216 1.1 alm put_stream_line(fp, s, len)
217 1.1 alm FILE *fp;
218 1.1 alm char *s;
219 1.1 alm int len;
220 1.1 alm {
221 1.1 alm while (len--)
222 1.1 alm if ((des ? put_des_char(*s++, fp) : fputc(*s++, fp)) < 0) {
223 1.1 alm fprintf(stderr, "%s\n", strerror(errno));
224 1.1 alm sprintf(errmsg, "cannot write file");
225 1.1 alm return ERR;
226 1.1 alm }
227 1.1 alm return 0;
228 1.1 alm }
229 1.1 alm
230 1.1 alm /* get_extended_line: get a an extended line from stdin */
231 1.1 alm char *
232 1.1 alm get_extended_line(sizep, nonl)
233 1.1 alm int *sizep;
234 1.1 alm int nonl;
235 1.1 alm {
236 1.1 alm static char *cvbuf = NULL; /* buffer */
237 1.1 alm static int cvbufsz = 0; /* buffer size */
238 1.1 alm
239 1.1 alm int l, n;
240 1.1 alm char *t = ibufp;
241 1.1 alm
242 1.1 alm while (*t++ != '\n')
243 1.1 alm ;
244 1.1 alm if ((l = t - ibufp) < 2 || !has_trailing_escape(ibufp, ibufp + l - 1)) {
245 1.1 alm *sizep = l;
246 1.1 alm return ibufp;
247 1.1 alm }
248 1.1 alm *sizep = -1;
249 1.1 alm REALLOC(cvbuf, cvbufsz, l, NULL);
250 1.1 alm memcpy(cvbuf, ibufp, l);
251 1.1 alm *(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
252 1.1 alm if (nonl) l--; /* strip newline */
253 1.1 alm for (;;) {
254 1.1 alm if ((n = get_tty_line()) < 0)
255 1.1 alm return NULL;
256 1.1 alm else if (n == 0 || ibuf[n - 1] != '\n') {
257 1.1 alm sprintf(errmsg, "unexpected end-of-file");
258 1.1 alm return NULL;
259 1.1 alm }
260 1.1 alm REALLOC(cvbuf, cvbufsz, l + n, NULL);
261 1.1 alm memcpy(cvbuf + l, ibuf, n);
262 1.1 alm l += n;
263 1.1 alm if (n < 2 || !has_trailing_escape(cvbuf, cvbuf + l - 1))
264 1.1 alm break;
265 1.1 alm *(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
266 1.1 alm if (nonl) l--; /* strip newline */
267 1.1 alm }
268 1.1 alm REALLOC(cvbuf, cvbufsz, l + 1, NULL);
269 1.1 alm cvbuf[l] = '\0';
270 1.1 alm *sizep = l;
271 1.1 alm return cvbuf;
272 1.1 alm }
273 1.1 alm
274 1.1 alm
275 1.1 alm /* get_tty_line: read a line of text from stdin; return line length */
276 1.1 alm int
277 1.1 alm get_tty_line()
278 1.1 alm {
279 1.3 tls int oi = 0;
280 1.3 tls int i = 0;
281 1.1 alm int c;
282 1.1 alm
283 1.1 alm for (;;)
284 1.1 alm switch (c = getchar()) {
285 1.1 alm default:
286 1.1 alm oi = 0;
287 1.1 alm REALLOC(ibuf, ibufsz, i + 2, ERR);
288 1.1 alm if (!(ibuf[i++] = c)) isbinary = 1;
289 1.1 alm if (c != '\n')
290 1.1 alm continue;
291 1.1 alm lineno++;
292 1.1 alm ibuf[i] = '\0';
293 1.1 alm ibufp = ibuf;
294 1.1 alm return i;
295 1.1 alm case EOF:
296 1.1 alm if (ferror(stdin)) {
297 1.1 alm fprintf(stderr, "stdin: %s\n", strerror(errno));
298 1.1 alm sprintf(errmsg, "cannot read stdin");
299 1.1 alm clearerr(stdin);
300 1.1 alm ibufp = NULL;
301 1.1 alm return ERR;
302 1.1 alm } else {
303 1.1 alm clearerr(stdin);
304 1.1 alm if (i != oi) {
305 1.1 alm oi = i;
306 1.1 alm continue;
307 1.1 alm } else if (i)
308 1.1 alm ibuf[i] = '\0';
309 1.1 alm ibufp = ibuf;
310 1.1 alm return i;
311 1.1 alm }
312 1.1 alm }
313 1.1 alm }
314 1.1 alm
315 1.1 alm
316 1.1 alm
317 1.1 alm #define ESCAPES "\a\b\f\n\r\t\v\\"
318 1.1 alm #define ESCCHARS "abfnrtv\\"
319 1.1 alm
320 1.1 alm /* put_tty_line: print text to stdout */
321 1.1 alm int
322 1.1 alm put_tty_line(s, l, n, gflag)
323 1.1 alm char *s;
324 1.1 alm int l;
325 1.1 alm long n;
326 1.1 alm int gflag;
327 1.1 alm {
328 1.1 alm int col = 0;
329 1.1 alm char *cp;
330 1.6 christos #ifndef BACKWARDS
331 1.6 christos int lc = 0;
332 1.6 christos #endif
333 1.1 alm
334 1.1 alm if (gflag & GNP) {
335 1.1 alm printf("%ld\t", n);
336 1.1 alm col = 8;
337 1.1 alm }
338 1.1 alm for (; l--; s++) {
339 1.1 alm if ((gflag & GLS) && ++col > cols) {
340 1.1 alm fputs("\\\n", stdout);
341 1.1 alm col = 1;
342 1.1 alm #ifndef BACKWARDS
343 1.1 alm if (!scripted && !isglobal && ++lc > rows) {
344 1.1 alm lc = 0;
345 1.1 alm fputs("Press <RETURN> to continue... ", stdout);
346 1.1 alm fflush(stdout);
347 1.1 alm if (get_tty_line() < 0)
348 1.1 alm return ERR;
349 1.1 alm }
350 1.1 alm #endif
351 1.1 alm }
352 1.1 alm if (gflag & GLS) {
353 1.1 alm if (31 < *s && *s < 127 && *s != '\\')
354 1.1 alm putchar(*s);
355 1.1 alm else {
356 1.1 alm putchar('\\');
357 1.1 alm col++;
358 1.1 alm if (*s && (cp = strchr(ESCAPES, *s)) != NULL)
359 1.1 alm putchar(ESCCHARS[cp - ESCAPES]);
360 1.1 alm else {
361 1.1 alm putchar((((unsigned char) *s & 0300) >> 6) + '0');
362 1.1 alm putchar((((unsigned char) *s & 070) >> 3) + '0');
363 1.1 alm putchar(((unsigned char) *s & 07) + '0');
364 1.1 alm col += 2;
365 1.1 alm }
366 1.1 alm }
367 1.1 alm
368 1.1 alm } else
369 1.1 alm putchar(*s);
370 1.1 alm }
371 1.1 alm #ifndef BACKWARDS
372 1.1 alm if (gflag & GLS)
373 1.1 alm putchar('$');
374 1.1 alm #endif
375 1.1 alm putchar('\n');
376 1.1 alm return 0;
377 1.1 alm }
378