1 /* $OpenBSD: io.c,v 1.8 2001/01/16 03:04:45 deraadt Exp $ */ 2 /* $NetBSD: io.c,v 1.2 1995/03/21 09:04:43 cgd Exp $ */ 3 4 /* io.c: This file contains the i/o routines for the ed line editor */ 5 /*- 6 * Copyright (c) 1993 Andrew Moore, Talke Studio. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #ifndef lint 32 #if 0 33 static char *rcsid = "@(#)io.c,v 1.1 1994/02/01 00:34:41 alm Exp"; 34 #else 35 static char rcsid[] = "$OpenBSD: io.c,v 1.8 2001/01/16 03:04:45 deraadt Exp $"; 36 #endif 37 #endif /* not lint */ 38 39 #include "ed.h" 40 41 42 extern int scripted; 43 44 /* read_file: read a named file/pipe into the buffer; return line count */ 45 long 46 read_file(fn, n) 47 char *fn; 48 long n; 49 { 50 FILE *fp; 51 long size; 52 53 54 fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r"); 55 if (fp == NULL) { 56 perror(fn); 57 seterrmsg("cannot open input file"); 58 return ERR; 59 } else if ((size = read_stream(fp, n)) < 0) 60 return ERR; 61 else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) { 62 perror(fn); 63 seterrmsg("cannot close input file"); 64 return ERR; 65 } 66 fprintf(stderr, !scripted ? "%lu\n" : "", size); 67 return current_addr - n; 68 } 69 70 71 extern int des; 72 73 char *sbuf; /* file i/o buffer */ 74 int sbufsz; /* file i/o buffer size */ 75 int newline_added; /* if set, newline appended to input file */ 76 77 /* read_stream: read a stream into the editor buffer; return status */ 78 long 79 read_stream(fp, n) 80 FILE *fp; 81 long n; 82 { 83 line_t *lp = get_addressed_line_node(n); 84 undo_t *up = NULL; 85 unsigned long size = 0; 86 int o_newline_added = newline_added; 87 int o_isbinary = isbinary; 88 int appended = (n == addr_last); 89 int len; 90 91 isbinary = newline_added = 0; 92 if (des) 93 init_des_cipher(); 94 for (current_addr = n; (len = get_stream_line(fp)) > 0; size += len) { 95 SPL1(); 96 if (put_sbuf_line(sbuf) == NULL) { 97 SPL0(); 98 return ERR; 99 } 100 lp = lp->q_forw; 101 if (up) 102 up->t = lp; 103 else if ((up = push_undo_stack(UADD, current_addr, 104 current_addr)) == NULL) { 105 SPL0(); 106 return ERR; 107 } 108 SPL0(); 109 } 110 if (len < 0) 111 return ERR; 112 if (appended && size && o_isbinary && o_newline_added) 113 fputs("newline inserted\n", stderr); 114 else if (newline_added && (!appended || (!isbinary && !o_isbinary))) 115 fputs("newline appended\n", stderr); 116 if (isbinary && newline_added && !appended) 117 size += 1; 118 if (!size) 119 newline_added = 1; 120 newline_added = appended ? newline_added : o_newline_added; 121 isbinary = isbinary | o_isbinary; 122 if (des) 123 size += 8 - size % 8; /* adjust DES size */ 124 return size; 125 } 126 127 128 /* get_stream_line: read a line of text from a stream; return line length */ 129 int 130 get_stream_line(fp) 131 FILE *fp; 132 { 133 register int c; 134 register int i = 0; 135 136 while (((c = des ? get_des_char(fp) : getc(fp)) != EOF || (!feof(fp) && 137 !ferror(fp))) && c != '\n') { 138 REALLOC(sbuf, sbufsz, i + 1, ERR); 139 if (!(sbuf[i++] = c)) 140 isbinary = 1; 141 } 142 REALLOC(sbuf, sbufsz, i + 2, ERR); 143 if (c == '\n') 144 sbuf[i++] = c; 145 else if (ferror(fp)) { 146 perror(NULL); 147 seterrmsg("cannot read input file"); 148 return ERR; 149 } else if (i) { 150 sbuf[i++] = '\n'; 151 newline_added = 1; 152 } 153 sbuf[i] = '\0'; 154 return (isbinary && newline_added && i) ? --i : i; 155 } 156 157 158 /* write_file: write a range of lines to a named file/pipe; return line count */ 159 long 160 write_file(fn, mode, n, m) 161 char *fn; 162 char *mode; 163 long n; 164 long m; 165 { 166 FILE *fp; 167 long size; 168 169 fp = (*fn == '!') ? popen(fn+1, "w") : fopen(strip_escapes(fn), mode); 170 if (fp == NULL) { 171 perror(fn); 172 seterrmsg("cannot open output file"); 173 return ERR; 174 } else if ((size = write_stream(fp, n, m)) < 0) 175 return ERR; 176 else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) { 177 perror(fn); 178 seterrmsg("cannot close output file"); 179 return ERR; 180 } 181 fprintf(stderr, !scripted ? "%lu\n" : "", size); 182 return n ? m - n + 1 : 0; 183 } 184 185 186 /* write_stream: write a range of lines to a stream; return status */ 187 long 188 write_stream(fp, n, m) 189 FILE *fp; 190 long n; 191 long m; 192 { 193 line_t *lp = get_addressed_line_node(n); 194 unsigned long size = 0; 195 char *s; 196 int len; 197 198 if (des) 199 init_des_cipher(); 200 for (; n && n <= m; n++, lp = lp->q_forw) { 201 if ((s = get_sbuf_line(lp)) == NULL) 202 return ERR; 203 len = lp->len; 204 if (n != addr_last || !isbinary || !newline_added) 205 s[len++] = '\n'; 206 if (put_stream_line(fp, s, len) < 0) 207 return ERR; 208 size += len; 209 } 210 if (des) { 211 flush_des_file(fp); /* flush buffer */ 212 size += 8 - size % 8; /* adjust DES size */ 213 } 214 return size; 215 } 216 217 218 /* put_stream_line: write a line of text to a stream; return status */ 219 int 220 put_stream_line(fp, s, len) 221 FILE *fp; 222 char *s; 223 int len; 224 { 225 while (len--) 226 if ((des ? put_des_char(*s++, fp) : fputc(*s++, fp)) < 0) { 227 perror(NULL); 228 seterrmsg("cannot write file"); 229 return ERR; 230 } 231 return 0; 232 } 233 234 /* get_extended_line: get a an extended line from stdin */ 235 char * 236 get_extended_line(sizep, nonl) 237 int *sizep; 238 int nonl; 239 { 240 static char *cvbuf = NULL; /* buffer */ 241 static int cvbufsz = 0; /* buffer size */ 242 243 int l, n; 244 char *t = ibufp; 245 246 while (*t++ != '\n') 247 ; 248 if ((l = t - ibufp) < 2 || !has_trailing_escape(ibufp, ibufp + l - 1)) { 249 *sizep = l; 250 return ibufp; 251 } 252 *sizep = -1; 253 REALLOC(cvbuf, cvbufsz, l, NULL); 254 memcpy(cvbuf, ibufp, l); 255 *(cvbuf + --l - 1) = '\n'; /* strip trailing esc */ 256 if (nonl) l--; /* strip newline */ 257 for (;;) { 258 if ((n = get_tty_line()) < 0) 259 return NULL; 260 else if (n == 0 || ibuf[n - 1] != '\n') { 261 seterrmsg("unexpected end-of-file"); 262 return NULL; 263 } 264 REALLOC(cvbuf, cvbufsz, l + n, NULL); 265 memcpy(cvbuf + l, ibuf, n); 266 l += n; 267 if (n < 2 || !has_trailing_escape(cvbuf, cvbuf + l - 1)) 268 break; 269 *(cvbuf + --l - 1) = '\n'; /* strip trailing esc */ 270 if (nonl) l--; /* strip newline */ 271 } 272 REALLOC(cvbuf, cvbufsz, l + 1, NULL); 273 cvbuf[l] = '\0'; 274 *sizep = l; 275 return cvbuf; 276 } 277 278 279 /* get_tty_line: read a line of text from stdin; return line length */ 280 int 281 get_tty_line() 282 { 283 register int oi = 0; 284 register int i = 0; 285 int c; 286 287 for (;;) 288 switch (c = getchar()) { 289 default: 290 oi = 0; 291 REALLOC(ibuf, ibufsz, i + 2, ERR); 292 if (!(ibuf[i++] = c)) isbinary = 1; 293 if (c != '\n') 294 continue; 295 lineno++; 296 ibuf[i] = '\0'; 297 ibufp = ibuf; 298 return i; 299 case EOF: 300 if (ferror(stdin)) { 301 perror("stdin"); 302 seterrmsg("cannot read stdin"); 303 clearerr(stdin); 304 ibufp = NULL; 305 return ERR; 306 } else { 307 clearerr(stdin); 308 if (i != oi) { 309 oi = i; 310 continue; 311 } else if (i) 312 ibuf[i] = '\0'; 313 ibufp = ibuf; 314 return i; 315 } 316 } 317 } 318 319 320 321 #define ESCAPES "\a\b\f\n\r\t\v\\" 322 #define ESCCHARS "abfnrtv\\" 323 324 extern int rows; 325 extern int cols; 326 327 /* put_tty_line: print text to stdout */ 328 int 329 put_tty_line(s, l, n, gflag) 330 char *s; 331 int l; 332 long n; 333 int gflag; 334 { 335 int col = 0; 336 #ifndef BACKWARDS 337 int lc = 0; 338 #endif 339 char *cp; 340 341 if (gflag & GNP) { 342 printf("%ld\t", n); 343 col = 8; 344 } 345 for (; l--; s++) { 346 if ((gflag & GLS) && ++col > cols) { 347 fputs("\\\n", stdout); 348 col = 1; 349 #ifndef BACKWARDS 350 if (!scripted && !isglobal && ++lc > rows) { 351 lc = 0; 352 fputs("Press <RETURN> to continue... ", stdout); 353 fflush(stdout); 354 if (get_tty_line() < 0) 355 return ERR; 356 } 357 #endif 358 } 359 if (gflag & GLS) { 360 if (31 < *s && *s < 127 && *s != '\\') 361 putchar(*s); 362 else { 363 putchar('\\'); 364 col++; 365 if (*s && (cp = strchr(ESCAPES, *s)) != NULL) 366 putchar(ESCCHARS[cp - ESCAPES]); 367 else { 368 putchar((((unsigned char) *s & 0300) >> 6) + '0'); 369 putchar((((unsigned char) *s & 070) >> 3) + '0'); 370 putchar(((unsigned char) *s & 07) + '0'); 371 col += 2; 372 } 373 } 374 375 } else 376 putchar(*s); 377 } 378 #ifndef BACKWARDS 379 if (gflag & GLS) 380 putchar('$'); 381 #endif 382 putchar('\n'); 383 return 0; 384 } 385