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