1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/* io.c: This file contains the i/o routines for the ed line editor */
/* ed line editor.
Copyright (C) 1993, 1994 Andrew Moore, Talke Studio
All Rights Reserved
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef lint
static char *rcsid = "@(#)$Id: io.c,v 1.11 1994/11/13 04:25:44 alm Exp $";
#endif /* not lint */
#include "ed.h"
extern int scripted;
/* read_file: read a named file/pipe into the buffer; return line count */
long
read_file (fn, n)
char *fn;
long n;
{
FILE *fp;
long size;
fp = (*fn == '!') ? popen (fn + 1, "r") : fopen (strip_escapes (fn), "r");
if (fp == NULL)
{
fprintf (stderr, "%s: %s\n", fn, strerror (errno));
sprintf (errmsg, "Cannot open input file");
return ERR;
}
else if ((size = read_stream (fp, n)) < 0)
return ERR;
else if (((*fn == '!') ? pclose (fp) : fclose (fp)) < 0)
{
fprintf (stderr, "%s: %s\n", fn, strerror (errno));
sprintf (errmsg, "Cannot close input file");
return ERR;
}
fprintf (stderr, !scripted ? "%lu\n" : "", size);
return current_addr - n;
}
char *sbuf; /* file i/o buffer */
int sbufsz; /* file i/o buffer size */
int newline_added; /* if set, newline appended to input file */
/* read_stream: read a stream into the editor buffer; return status */
long
read_stream (fp, n)
FILE *fp;
long n;
{
line_t *lp = get_addressed_line_node (n);
undo_t *up = NULL;
unsigned long size = 0;
int o_newline_added = newline_added;
int o_isbinary = isbinary;
int o_n = n;
int appended = n == addr_last;
int len;
isbinary = newline_added = 0;
for (current_addr = n; (len = get_stream_line (fp)) > 0; size += len)
{
SPL1 ();
if (put_sbuf_line (sbuf) == NULL)
{
SPL0 ();
return ERR;
}
lp = lp->q_forw;
if (up)
up->t = lp;
else if ((up = push_undo_stack (UADD, current_addr,
current_addr)) == NULL)
{
SPL0 ();
return ERR;
}
SPL0 ();
}
if (len < 0)
return ERR;
if (o_n && appended && size && o_isbinary && o_newline_added)
fputs ("Newline inserted\n", stderr);
else if (newline_added && (!appended || (!isbinary && !o_isbinary)))
fputs ("Newline appended\n", stderr);
if (isbinary && newline_added && !appended)
size += 1;
if (!size)
newline_added = 1;
newline_added = appended ? newline_added : o_newline_added;
isbinary = isbinary | o_isbinary;
return size;
}
/* get_stream_line: read a line of text from a stream; return line length */
int
get_stream_line (fp)
FILE *fp;
{
register int c;
register int i = 0;
while (((c = getc (fp)) != EOF || (!feof (fp) &&
!ferror (fp))) && c != '\n')
{
REALLOC (sbuf, sbufsz, i + 1, ERR);
if (!(sbuf[i++] = c))
isbinary = 1;
}
REALLOC (sbuf, sbufsz, i + 2, ERR);
if (c == '\n')
sbuf[i++] = c;
else if (ferror (fp))
{
fprintf (stderr, "%s\n", strerror (errno));
sprintf (errmsg, "Cannot read input file");
return ERR;
}
else if (i)
{
sbuf[i++] = '\n';
newline_added = 1;
}
sbuf[i] = '\0';
return (isbinary && newline_added && i) ? --i : i;
}
/* write_file: write a range of lines to a named file/pipe; return line count */
long
write_file (fn, mode, n, m)
char *fn;
char *mode;
long n;
long m;
{
FILE *fp;
long size;
fp = (*fn == '!') ? popen (fn + 1, "w") : fopen (strip_escapes (fn), mode);
if (fp == NULL)
{
fprintf (stderr, "%s: %s\n", fn, strerror (errno));
sprintf (errmsg, "Cannot open output file");
return ERR;
}
else if ((size = write_stream (fp, n, m)) < 0)
return ERR;
else if (((*fn == '!') ? pclose (fp) : fclose (fp)) < 0)
{
fprintf (stderr, "%s: %s\n", fn, strerror (errno));
sprintf (errmsg, "Cannot close output file");
return ERR;
}
fprintf (stderr, !scripted ? "%lu\n" : "", size);
return n ? m - n + 1 : 0;
}
/* write_stream: write a range of lines to a stream; return status */
long
write_stream (fp, n, m)
FILE *fp;
long n;
long m;
{
line_t *lp = get_addressed_line_node (n);
unsigned long size = 0;
char *s;
int len;
for (; n && n <= m; n++, lp = lp->q_forw)
{
if ((s = get_sbuf_line (lp)) == NULL)
return ERR;
len = lp->len;
if (n != addr_last || !isbinary || !newline_added)
s[len++] = '\n';
if (put_stream_line (fp, s, len) < 0)
return ERR;
size += len;
}
return size;
}
/* put_stream_line: write a line of text to a stream; return status */
int
put_stream_line (fp, s, len)
FILE *fp;
char *s;
int len;
{
while (len--)
if (fputc (*s++, fp) < 0)
{
fprintf (stderr, "%s\n", strerror (errno));
sprintf (errmsg, "Cannot write file");
return ERR;
}
return 0;
}
/* get_extended_line: get an extended line from stdin */
char *
get_extended_line (sizep, nonl)
int *sizep;
int nonl;
{
static char *cvbuf = NULL; /* buffer */
static int cvbufsz = 0; /* buffer size */
int l, n;
char *t = ibufp;
while (*t++ != '\n')
;
if ((l = t - ibufp) < 2 || !has_trailing_escape (ibufp, ibufp + l - 1))
{
*sizep = l;
return ibufp;
}
*sizep = -1;
REALLOC (cvbuf, cvbufsz, l, NULL);
memcpy (cvbuf, ibufp, l);
*(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
if (nonl)
l--; /* strip newline */
for (;;)
{
if ((n = get_tty_line ()) < 0)
return NULL;
else if (n == 0 || ibuf[n - 1] != '\n')
{
sprintf (errmsg, "Unexpected end-of-file");
return NULL;
}
REALLOC (cvbuf, cvbufsz, l + n, NULL);
memcpy (cvbuf + l, ibuf, n);
l += n;
if (n < 2 || !has_trailing_escape (cvbuf, cvbuf + l - 1))
break;
*(cvbuf + --l - 1) = '\n'; /* strip trailing esc */
if (nonl)
l--; /* strip newline */
}
REALLOC (cvbuf, cvbufsz, l + 1, NULL);
cvbuf[l] = '\0';
*sizep = l;
return cvbuf;
}
/* get_tty_line: read a line of text from stdin; return line length */
int
get_tty_line ()
{
register int oi = 0;
register int i = 0;
int c;
/* Read stdin one character at a time to avoid i/o contention
with shell escapes invoked by nonterminal input, e.g.,
ed - <<EOF
!cat
hello, world
EOF */
for (;;)
switch (c = getchar ())
{
default:
oi = 0;
REALLOC (ibuf, ibufsz, i + 2, ERR);
if (!(ibuf[i++] = c))
isbinary = 1;
if (c != '\n')
continue;
lineno++;
ibuf[i] = '\0';
ibufp = ibuf;
return i;
case EOF:
if (ferror (stdin))
{
fprintf (stderr, "stdin: %s\n", strerror (errno));
sprintf (errmsg, "Cannot read stdin");
clearerr (stdin);
ibufp = NULL;
return ERR;
}
else
{
clearerr (stdin);
if (i != oi)
{
oi = i;
continue;
}
else if (i)
ibuf[i] = '\0';
ibufp = ibuf;
return i;
}
}
}
#define ESCAPES "\a\b\f\n\r\t\v\\"
#define ESCCHARS "abfnrtv\\"
extern long rows;
extern int cols;
extern int dlcnt;
/* put_tty_line: print text to stdout */
int
put_tty_line (s, l, n, gflag)
char *s;
int l;
long n;
int gflag;
{
int col = 0;
char *cp;
if (gflag & GNP)
{
printf ("%ld\t", n);
col = 8;
}
for (; l--; s++)
{
if ((gflag & GLS) && ++col > cols)
{
fputs ("\\\n", stdout);
col = 1;
if (!traditional && !scripted && !isglobal && ++dlcnt > rows)
{
dlcnt = 0;
fputs ("Press <RETURN> to continue... ", stdout);
fflush (stdout);
if (get_tty_line () < 0)
return ERR;
}
}
if (gflag & GLS)
{
if (31 < *s && *s < 127 && *s != '\\')
putchar (*s);
else
{
putchar ('\\');
col++;
if (*s && (cp = strchr (ESCAPES, *s)) != NULL)
putchar (ESCCHARS[cp - ESCAPES]);
else
{
putchar ((((unsigned char) *s & 0300) >> 6) + '0');
putchar ((((unsigned char) *s & 070) >> 3) + '0');
putchar (((unsigned char) *s & 07) + '0');
col += 2;
}
}
}
else
putchar (*s);
}
if (!traditional && (gflag & GLS))
putchar ('$');
putchar ('\n');
return 0;
}
|