1999-05-08 10:22:15 +00:00
|
|
|
/* buf.c: This file contains the scratch-file buffer routines for the
|
1993-06-18 13:00:14 +00:00
|
|
|
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
|
|
|
|
2002-06-30 05:13:54 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1993-06-18 13:00:14 +00:00
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
|
2012-10-19 05:43:38 +00:00
|
|
|
static FILE *sfp; /* scratch file pointer */
|
|
|
|
static off_t sfseek; /* scratch file position */
|
|
|
|
static int seek_write; /* seek before writing */
|
|
|
|
static 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 *
|
2002-02-02 06:36:49 +00:00
|
|
|
get_sbuf_line(line_t *lp)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
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;
|
2001-09-01 23:26:40 +00:00
|
|
|
if (fseeko(sfp, sfseek, SEEK_SET) < 0) {
|
1993-06-18 13:00:14 +00:00
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "cannot seek temp file";
|
1993-06-18 13:00:14 +00:00
|
|
|
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));
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "cannot read temp file";
|
1993-06-18 13:00:14 +00:00
|
|
|
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 */
|
2001-08-06 22:01:31 +00:00
|
|
|
const char *
|
2002-02-02 06:36:49 +00:00
|
|
|
put_sbuf_line(const char *cs)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
|
|
|
line_t *lp;
|
|
|
|
int len, ct;
|
2001-08-06 22:01:31 +00:00
|
|
|
const char *s;
|
1993-06-18 13:00:14 +00:00
|
|
|
|
|
|
|
if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "out of memory";
|
1993-06-18 13:00:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* assert: cs is '\n' terminated */
|
|
|
|
for (s = cs; *s != '\n'; s++)
|
|
|
|
;
|
|
|
|
if (s - cs >= LINECHARS) {
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "line too long";
|
2011-08-27 15:28:57 +00:00
|
|
|
free(lp);
|
1993-06-18 13:00:14 +00:00
|
|
|
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) {
|
2001-09-01 23:26:40 +00:00
|
|
|
if (fseeko(sfp, (off_t)0, SEEK_END) < 0) {
|
1993-06-18 13:00:14 +00:00
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "cannot seek temp file";
|
2011-08-27 15:28:57 +00:00
|
|
|
free(lp);
|
1993-06-18 13:00:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-01 23:26:40 +00:00
|
|
|
sfseek = ftello(sfp);
|
1993-06-18 13:00:14 +00:00
|
|
|
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));
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "cannot write temp file";
|
2011-08-27 15:28:57 +00:00
|
|
|
free(lp);
|
1993-06-18 13:00:14 +00:00
|
|
|
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
|
2002-02-02 06:36:49 +00:00
|
|
|
add_line_node(line_t *lp)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
|
|
|
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
|
2002-02-02 06:36:49 +00:00
|
|
|
get_line_node_addr(line_t *lp)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
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) {
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "invalid address";
|
1993-06-26 06:47:21 +00:00
|
|
|
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 *
|
2002-02-02 06:36:49 +00:00
|
|
|
get_addressed_line_node(long n)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-19 05:43:38 +00:00
|
|
|
static char sfn[15] = ""; /* scratch file name */
|
1993-06-18 13:00:14 +00:00
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* open_sbuf: open scratch file */
|
|
|
|
int
|
2002-02-02 06:36:49 +00:00
|
|
|
open_sbuf(void)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
2002-06-20 07:15:55 +00:00
|
|
|
int fd;
|
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-09 11:05:16 +00:00
|
|
|
if ((fd = mkstemp(sfn)) == -1 ||
|
1997-03-24 05:45:29 +00:00
|
|
|
(sfp = fdopen(fd, "w+")) == NULL) {
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
perror(sfn);
|
2001-08-06 22:01:31 +00:00
|
|
|
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
|
2002-02-02 06:36:49 +00:00
|
|
|
close_sbuf(void)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
|
|
|
if (sfp) {
|
|
|
|
if (fclose(sfp) < 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "cannot close temp file";
|
1993-06-18 13:00:14 +00:00
|
|
|
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
|
2002-02-02 06:36:49 +00:00
|
|
|
quit(int n)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
|
|
|
if (sfp) {
|
|
|
|
fclose(sfp);
|
|
|
|
unlink(sfn);
|
|
|
|
}
|
|
|
|
exit(n);
|
|
|
|
}
|
1993-07-02 06:16:28 +00:00
|
|
|
|
|
|
|
|
2012-10-19 05:43:38 +00:00
|
|
|
static unsigned char ctab[256]; /* character translation table */
|
1993-07-02 06:16:28 +00:00
|
|
|
|
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
|
2002-02-02 06:36:49 +00:00
|
|
|
init_buffers(void)
|
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);
|
1997-12-31 12:25:35 +00:00
|
|
|
|
|
|
|
/* Ensure stdout is line buffered. This avoids bogus delays
|
|
|
|
of output if stdout is piped through utilities to a terminal. */
|
|
|
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
1994-02-01 00:36:28 +00:00
|
|
|
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 *
|
2002-02-02 06:36:49 +00:00
|
|
|
translit_text(char *s, int len, int from, int to)
|
1993-07-02 06:16:28 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|