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