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