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