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