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