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