]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - bin/ed/io.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "ed.h"
32
33
34 extern int scripted;
35
36 /* read_file: read a named file/pipe into the buffer; return line count */
37 long
38 read_file(char *fn, long n)
39 {
40         FILE *fp;
41         long size;
42
43
44         fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r");
45         if (fp == NULL) {
46                 fprintf(stderr, "%s: %s\n", fn, strerror(errno));
47                 errmsg = "cannot open input file";
48                 return ERR;
49         } else if ((size = read_stream(fp, n)) < 0)
50                 return ERR;
51          else if (((*fn == '!') ?  pclose(fp) : fclose(fp)) < 0) {
52                 fprintf(stderr, "%s: %s\n", fn, strerror(errno));
53                 errmsg = "cannot close input file";
54                 return ERR;
55         }
56         if (!scripted)
57                 fprintf(stdout, "%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(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         if (!scripted)
166                 fprintf(stdout, "%lu\n", size);
167         return n ? m - n + 1 : 0;
168 }
169
170
171 /* write_stream: write a range of lines to a stream; return status */
172 long
173 write_stream(FILE *fp, long n, long m)
174 {
175         line_t *lp = get_addressed_line_node(n);
176         unsigned long size = 0;
177         char *s;
178         int len;
179
180         if (des)
181                 init_des_cipher();
182         for (; n && n <= m; n++, lp = lp->q_forw) {
183                 if ((s = get_sbuf_line(lp)) == NULL)
184                         return ERR;
185                 len = lp->len;
186                 if (n != addr_last || !isbinary || !newline_added)
187                         s[len++] = '\n';
188                 if (put_stream_line(fp, s, len) < 0)
189                         return ERR;
190                 size += len;
191         }
192         if (des) {
193                 flush_des_file(fp);                     /* flush buffer */
194                 size += 8 - size % 8;                   /* adjust DES size */
195         }
196         return size;
197 }
198
199
200 /* put_stream_line: write a line of text to a stream; return status */
201 int
202 put_stream_line(FILE *fp, const char *s, int len)
203 {
204         while (len--)
205                 if ((des ? put_des_char(*s++, fp) : fputc(*s++, fp)) < 0) {
206                         fprintf(stderr, "%s\n", strerror(errno));
207                         errmsg = "cannot write file";
208                         return ERR;
209                 }
210         return 0;
211 }
212
213 /* get_extended_line: get an extended line from stdin */
214 char *
215 get_extended_line(int *sizep, int nonl)
216 {
217         static char *cvbuf = NULL;              /* buffer */
218         static int cvbufsz = 0;                 /* buffer size */
219
220         int l, n;
221         char *t = ibufp;
222
223         while (*t++ != '\n')
224                 ;
225         if ((l = t - ibufp) < 2 || !has_trailing_escape(ibufp, ibufp + l - 1)) {
226                 *sizep = l;
227                 return ibufp;
228         }
229         *sizep = -1;
230         REALLOC(cvbuf, cvbufsz, l, NULL);
231         memcpy(cvbuf, ibufp, l);
232         *(cvbuf + --l - 1) = '\n';      /* strip trailing esc */
233         if (nonl) l--;                  /* strip newline */
234         for (;;) {
235                 if ((n = get_tty_line()) < 0)
236                         return NULL;
237                 else if (n == 0 || ibuf[n - 1] != '\n') {
238                         errmsg = "unexpected end-of-file";
239                         return NULL;
240                 }
241                 REALLOC(cvbuf, cvbufsz, l + n, NULL);
242                 memcpy(cvbuf + l, ibuf, n);
243                 l += n;
244                 if (n < 2 || !has_trailing_escape(cvbuf, cvbuf + l - 1))
245                         break;
246                 *(cvbuf + --l - 1) = '\n';      /* strip trailing esc */
247                 if (nonl) l--;                  /* strip newline */
248         }
249         REALLOC(cvbuf, cvbufsz, l + 1, NULL);
250         cvbuf[l] = '\0';
251         *sizep = l;
252         return cvbuf;
253 }
254
255
256 /* get_tty_line: read a line of text from stdin; return line length */
257 int
258 get_tty_line(void)
259 {
260         int oi = 0;
261         int i = 0;
262         int c;
263
264         for (;;)
265                 switch (c = getchar()) {
266                 default:
267                         oi = 0;
268                         REALLOC(ibuf, ibufsz, i + 2, ERR);
269                         if (!(ibuf[i++] = c)) isbinary = 1;
270                         if (c != '\n')
271                                 continue;
272                         lineno++;
273                         ibuf[i] = '\0';
274                         ibufp = ibuf;
275                         return i;
276                 case EOF:
277                         if (ferror(stdin)) {
278                                 fprintf(stderr, "stdin: %s\n", strerror(errno));
279                                 errmsg = "cannot read stdin";
280                                 clearerr(stdin);
281                                 ibufp = NULL;
282                                 return ERR;
283                         } else {
284                                 clearerr(stdin);
285                                 if (i != oi) {
286                                         oi = i;
287                                         continue;
288                                 } else if (i)
289                                         ibuf[i] = '\0';
290                                 ibufp = ibuf;
291                                 return i;
292                         }
293                 }
294 }
295
296
297
298 #define ESCAPES "\a\b\f\n\r\t\v\\"
299 #define ESCCHARS "abfnrtv\\"
300
301 extern int rows;
302 extern int cols;
303
304 /* put_tty_line: print text to stdout */
305 int
306 put_tty_line(const char *s, int l, long n, int gflag)
307 {
308         int col = 0;
309         int lc = 0;
310         char *cp;
311
312         if (gflag & GNP) {
313                 printf("%ld\t", n);
314                 col = 8;
315         }
316         for (; l--; s++) {
317                 if ((gflag & GLS) && ++col > cols) {
318                         fputs("\\\n", stdout);
319                         col = 1;
320 #ifndef BACKWARDS
321                         if (!scripted && !isglobal && ++lc > rows) {
322                                 lc = 0;
323                                 fputs("Press <RETURN> to continue... ", stdout);
324                                 fflush(stdout);
325                                 if (get_tty_line() < 0)
326                                         return ERR;
327                         }
328 #endif
329                 }
330                 if (gflag & GLS) {
331                         if (31 < *s && *s < 127 && *s != '\\')
332                                 putchar(*s);
333                         else {
334                                 putchar('\\');
335                                 col++;
336                                 if (*s && (cp = strchr(ESCAPES, *s)) != NULL)
337                                         putchar(ESCCHARS[cp - ESCAPES]);
338                                 else {
339                                         putchar((((unsigned char) *s & 0300) >> 6) + '0');
340                                         putchar((((unsigned char) *s & 070) >> 3) + '0');
341                                         putchar(((unsigned char) *s & 07) + '0');
342                                         col += 2;
343                                 }
344                         }
345
346                 } else
347                         putchar(*s);
348         }
349 #ifndef BACKWARDS
350         if (gflag & GLS)
351                 putchar('$');
352 #endif
353         putchar('\n');
354         return 0;
355 }