1993-06-18 13:00:14 +00:00
|
|
|
/* buf.c: This file contains the scratch-file buffer rountines for the
|
|
|
|
ed line editor. */
|
|
|
|
/*-
|
1994-02-01 00:36:28 +00:00
|
|
|
* Copyright (c) 1993 Andrew Moore, Talke Studio.
|
1993-06-18 13:00:14 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
1994-02-01 00:36:28 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
1993-06-18 13:00:14 +00:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1994-02-01 00:36:28 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1993-06-18 13:00:14 +00:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
1997-08-07 21:33:47 +00:00
|
|
|
|
1993-06-18 13:00:14 +00:00
|
|
|
#ifndef lint
|
1997-08-07 21:33:47 +00:00
|
|
|
#if 0
|
1996-12-14 06:08:03 +00:00
|
|
|
static char * const rcsid = "@(#)buf.c,v 1.4 1994/02/01 00:34:35 alm Exp";
|
1997-08-07 21:33:47 +00:00
|
|
|
#else
|
|
|
|
static char * const rcsid =
|
1997-10-08 13:46:39 +00:00
|
|
|
"$Id: buf.c,v 1.12 1997/08/07 21:33:39 steve Exp $";
|
1997-08-07 21:33:47 +00:00
|
|
|
#endif
|
1993-06-18 13:00:14 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/file.h>
|
1994-03-23 04:36:59 +00:00
|
|
|
#include <sys/stat.h>
|
1993-06-18 13:00:14 +00:00
|
|
|
|
|
|
|
#include "ed.h"
|
|
|
|
|
|
|
|
|
|
|
|
FILE *sfp; /* scratch file pointer */
|
|
|
|
off_t sfseek; /* scratch file position */
|
|
|
|
int seek_write; /* seek before writing */
|
1994-02-01 00:36:28 +00:00
|
|
|
line_t buffer_head; /* incore buffer */
|
1993-06-18 13:00:14 +00:00
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* get_sbuf_line: get a line of text from the scratch file; return pointer
|
1993-06-18 13:00:14 +00:00
|
|
|
to the text */
|
|
|
|
char *
|
1994-02-01 00:36:28 +00:00
|
|
|
get_sbuf_line(lp)
|
1993-06-18 13:00:14 +00:00
|
|
|
line_t *lp;
|
|
|
|
{
|
1994-02-01 00:36:28 +00:00
|
|
|
static char *sfbuf = NULL; /* buffer */
|
|
|
|
static int sfbufsz = 0; /* buffer size */
|
|
|
|
|
1993-06-18 13:00:14 +00:00
|
|
|
int len, ct;
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
if (lp == &buffer_head)
|
1993-06-18 13:00:14 +00:00
|
|
|
return NULL;
|
|
|
|
seek_write = 1; /* force seek on write */
|
|
|
|
/* out of position */
|
|
|
|
if (sfseek != lp->seek) {
|
|
|
|
sfseek = lp->seek;
|
|
|
|
if (fseek(sfp, sfseek, SEEK_SET) < 0) {
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot seek temp file");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1994-02-01 00:36:28 +00:00
|
|
|
len = lp->len;
|
|
|
|
REALLOC(sfbuf, sfbufsz, len + 1, NULL);
|
1993-06-18 13:00:14 +00:00
|
|
|
if ((ct = fread(sfbuf, sizeof(char), len, sfp)) < 0 || ct != len) {
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot read temp file");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sfseek += len; /* update file position */
|
|
|
|
sfbuf[len] = '\0';
|
|
|
|
return sfbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* put_sbuf_line: write a line of text to the scratch file and add a line node
|
1993-06-18 13:00:14 +00:00
|
|
|
to the editor buffer; return a pointer to the end of the text */
|
|
|
|
char *
|
1994-02-01 00:36:28 +00:00
|
|
|
put_sbuf_line(cs)
|
1993-06-18 13:00:14 +00:00
|
|
|
char *cs;
|
|
|
|
{
|
|
|
|
line_t *lp;
|
|
|
|
int len, ct;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* assert: cs is '\n' terminated */
|
|
|
|
for (s = cs; *s != '\n'; s++)
|
|
|
|
;
|
|
|
|
if (s - cs >= LINECHARS) {
|
|
|
|
sprintf(errmsg, "line too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-02-01 00:36:28 +00:00
|
|
|
len = s - cs;
|
1993-06-18 13:00:14 +00:00
|
|
|
/* out of position */
|
|
|
|
if (seek_write) {
|
|
|
|
if (fseek(sfp, 0L, SEEK_END) < 0) {
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot seek temp file");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sfseek = ftell(sfp);
|
|
|
|
seek_write = 0;
|
|
|
|
}
|
1994-02-01 00:36:28 +00:00
|
|
|
/* assert: SPL1() */
|
1993-06-18 13:00:14 +00:00
|
|
|
if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
|
|
|
|
sfseek = -1;
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot write temp file");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
lp->len = len;
|
|
|
|
lp->seek = sfseek;
|
1994-02-01 00:36:28 +00:00
|
|
|
add_line_node(lp);
|
1993-06-18 13:00:14 +00:00
|
|
|
sfseek += len; /* update file position */
|
|
|
|
return ++s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* add_line_node: add a line node in the editor buffer after the current line */
|
1993-06-18 13:00:14 +00:00
|
|
|
void
|
1994-02-01 00:36:28 +00:00
|
|
|
add_line_node(lp)
|
1993-06-18 13:00:14 +00:00
|
|
|
line_t *lp;
|
|
|
|
{
|
|
|
|
line_t *cp;
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
cp = get_addressed_line_node(current_addr); /* this get_addressed_line_node last! */
|
1994-03-23 04:36:59 +00:00
|
|
|
INSQUE(lp, cp);
|
1994-02-01 00:36:28 +00:00
|
|
|
addr_last++;
|
|
|
|
current_addr++;
|
1993-06-18 13:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* get_line_node_addr: return line number of pointer */
|
1993-06-18 13:00:14 +00:00
|
|
|
long
|
1994-02-01 00:36:28 +00:00
|
|
|
get_line_node_addr(lp)
|
1993-06-18 13:00:14 +00:00
|
|
|
line_t *lp;
|
|
|
|
{
|
1994-02-01 00:36:28 +00:00
|
|
|
line_t *cp = &buffer_head;
|
1993-06-18 13:00:14 +00:00
|
|
|
long n = 0;
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
while (cp != lp && (cp = cp->q_forw) != &buffer_head)
|
1993-06-18 13:00:14 +00:00
|
|
|
n++;
|
1994-02-01 00:36:28 +00:00
|
|
|
if (n && cp == &buffer_head) {
|
1993-06-26 06:47:21 +00:00
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
return n;
|
1993-06-18 13:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* get_addressed_line_node: return pointer to a line node in the editor buffer */
|
1993-06-18 13:00:14 +00:00
|
|
|
line_t *
|
1994-02-01 00:36:28 +00:00
|
|
|
get_addressed_line_node(n)
|
1993-06-18 13:00:14 +00:00
|
|
|
long n;
|
|
|
|
{
|
1994-02-01 00:36:28 +00:00
|
|
|
static line_t *lp = &buffer_head;
|
1993-06-18 13:00:14 +00:00
|
|
|
static long on = 0;
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
SPL1();
|
1993-06-18 13:00:14 +00:00
|
|
|
if (n > on)
|
1994-02-01 00:36:28 +00:00
|
|
|
if (n <= (on + addr_last) >> 1)
|
1993-06-18 13:00:14 +00:00
|
|
|
for (; on < n; on++)
|
1994-02-01 00:36:28 +00:00
|
|
|
lp = lp->q_forw;
|
1993-06-18 13:00:14 +00:00
|
|
|
else {
|
1994-02-01 00:36:28 +00:00
|
|
|
lp = buffer_head.q_back;
|
|
|
|
for (on = addr_last; on > n; on--)
|
|
|
|
lp = lp->q_back;
|
1993-06-18 13:00:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (n >= on >> 1)
|
|
|
|
for (; on > n; on--)
|
1994-02-01 00:36:28 +00:00
|
|
|
lp = lp->q_back;
|
1993-06-18 13:00:14 +00:00
|
|
|
else {
|
1994-02-01 00:36:28 +00:00
|
|
|
lp = &buffer_head;
|
1993-06-18 13:00:14 +00:00
|
|
|
for (on = 0; on < n; on++)
|
1994-02-01 00:36:28 +00:00
|
|
|
lp = lp->q_forw;
|
1993-06-18 13:00:14 +00:00
|
|
|
}
|
1994-02-01 00:36:28 +00:00
|
|
|
SPL0();
|
1993-06-18 13:00:14 +00:00
|
|
|
return lp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
extern int newline_added;
|
|
|
|
|
1993-06-18 13:00:14 +00:00
|
|
|
char sfn[15] = ""; /* scratch file name */
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* open_sbuf: open scratch file */
|
|
|
|
int
|
|
|
|
open_sbuf()
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
1997-03-24 05:45:29 +00:00
|
|
|
int fd = -1;
|
1994-03-23 04:36:59 +00:00
|
|
|
int u;
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
isbinary = newline_added = 0;
|
1994-03-23 04:36:59 +00:00
|
|
|
u = umask(077);
|
1993-06-18 13:00:14 +00:00
|
|
|
strcpy(sfn, "/tmp/ed.XXXXXX");
|
1997-10-08 13:46:39 +00:00
|
|
|
if (mktemp(sfn) == NULL ||
|
|
|
|
(fd = open(sfn, O_RDWR|O_CREAT|O_EXCL, 0666)) == -1 ||
|
1997-03-24 05:45:29 +00:00
|
|
|
(sfp = fdopen(fd, "w+")) == NULL) {
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
perror(sfn);
|
|
|
|
strcpy(errmsg, "cannot open temp file");
|
1994-03-23 04:36:59 +00:00
|
|
|
umask(u);
|
1993-06-18 13:00:14 +00:00
|
|
|
return ERR;
|
|
|
|
}
|
1994-03-23 04:36:59 +00:00
|
|
|
umask(u);
|
1993-06-18 13:00:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* close_sbuf: close scratch file */
|
|
|
|
int
|
|
|
|
close_sbuf()
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
|
|
|
if (sfp) {
|
|
|
|
if (fclose(sfp) < 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot close temp file");
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
sfp = NULL;
|
|
|
|
unlink(sfn);
|
|
|
|
}
|
|
|
|
sfseek = seek_write = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* quit: remove_lines scratch file and exit */
|
1993-06-18 13:00:14 +00:00
|
|
|
void
|
|
|
|
quit(n)
|
|
|
|
int n;
|
|
|
|
{
|
|
|
|
if (sfp) {
|
|
|
|
fclose(sfp);
|
|
|
|
unlink(sfn);
|
|
|
|
}
|
|
|
|
exit(n);
|
|
|
|
}
|
1993-07-02 06:16:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
unsigned char ctab[256]; /* character translation table */
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* init_buffers: open scratch buffer; initialize line queue */
|
1993-07-02 06:16:28 +00:00
|
|
|
void
|
1994-02-01 00:36:28 +00:00
|
|
|
init_buffers()
|
1993-07-02 06:16:28 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
1995-05-30 00:07:29 +00:00
|
|
|
/* Read stdin one character at a time to avoid i/o contention
|
1994-02-01 00:36:28 +00:00
|
|
|
with shell escapes invoked by nonterminal input, e.g.,
|
|
|
|
ed - <<EOF
|
|
|
|
!cat
|
|
|
|
hello, world
|
|
|
|
EOF */
|
|
|
|
setbuffer(stdin, stdinbuf, 1);
|
|
|
|
if (open_sbuf() < 0)
|
1993-07-02 06:16:28 +00:00
|
|
|
quit(2);
|
1994-02-01 00:36:28 +00:00
|
|
|
REQUE(&buffer_head, &buffer_head);
|
1993-07-02 06:16:28 +00:00
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
ctab[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* translit_text: translate characters in a string */
|
1993-07-02 06:16:28 +00:00
|
|
|
char *
|
1994-02-01 00:36:28 +00:00
|
|
|
translit_text(s, len, from, to)
|
1993-07-02 06:16:28 +00:00
|
|
|
char *s;
|
|
|
|
int len;
|
|
|
|
int from;
|
|
|
|
int to;
|
|
|
|
{
|
|
|
|
static int i = 0;
|
|
|
|
|
|
|
|
unsigned char *us;
|
|
|
|
|
|
|
|
ctab[i] = i; /* restore table to initial state */
|
|
|
|
ctab[i = from] = to;
|
|
|
|
for (us = (unsigned char *) s; len-- > 0; us++)
|
|
|
|
*us = ctab[*us];
|
|
|
|
return s;
|
|
|
|
}
|