2005-06-26 23:10:48 +04:00
|
|
|
/* $NetBSD: buf.c,v 1.25 2005/06/26 19:10:49 christos Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
2003-10-19 05:52:45 +04:00
|
|
|
/* buf.c: This file contains the scratch-file buffer routines for the
|
1993-04-08 05:07:16 +04:00
|
|
|
ed line editor. */
|
|
|
|
/*-
|
1993-11-12 13:48:43 +03:00
|
|
|
* Copyright (c) 1993 Andrew Moore, Talke Studio.
|
1993-04-08 05:07:16 +04: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.
|
|
|
|
*
|
1993-11-12 13:48:43 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
1993-04-08 05:07:16 +04:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1993-11-12 13:48:43 +03:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1993-04-08 05:07:16 +04: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.
|
|
|
|
*/
|
1995-04-23 14:07:18 +04:00
|
|
|
|
1997-07-20 10:35:35 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-04-08 05:07:16 +04:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
1994-02-20 09:56:16 +03:00
|
|
|
static char *rcsid = "@(#)buf.c,v 1.4 1994/02/01 00:34:35 alm Exp";
|
1995-03-21 12:01:59 +03:00
|
|
|
#else
|
2005-06-26 23:10:48 +04:00
|
|
|
__RCSID("$NetBSD: buf.c,v 1.25 2005/06/26 19:10:49 christos Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-04-08 05:07:16 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/file.h>
|
1994-03-23 07:04:19 +03:00
|
|
|
#include <sys/stat.h>
|
1993-04-08 05:07:16 +04:00
|
|
|
|
2000-04-18 03:37:50 +04:00
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
1997-07-20 10:35:35 +04:00
|
|
|
#include <err.h>
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
#include "ed.h"
|
|
|
|
|
|
|
|
|
|
|
|
FILE *sfp; /* scratch file pointer */
|
|
|
|
off_t sfseek; /* scratch file position */
|
|
|
|
int seek_write; /* seek before writing */
|
1994-02-20 09:56:16 +03:00
|
|
|
line_t buffer_head; /* incore buffer */
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* get_sbuf_line: get a line of text from the scratch file; return pointer
|
1993-04-08 05:07:16 +04:00
|
|
|
to the text */
|
|
|
|
char *
|
2005-02-17 19:29:26 +03:00
|
|
|
get_sbuf_line(line_t *lp)
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1994-02-20 09:56:16 +03:00
|
|
|
static char *sfbuf = NULL; /* buffer */
|
|
|
|
static int sfbufsz = 0; /* buffer size */
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
int len, ct;
|
|
|
|
|
1994-02-20 09:56:16 +03:00
|
|
|
if (lp == &buffer_head)
|
1993-04-08 05:07:16 +04: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) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "cannot seek temp file");
|
1993-05-08 14:49:52 +04:00
|
|
|
return NULL;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
}
|
1993-11-12 13:48:43 +03:00
|
|
|
len = lp->len;
|
1994-02-20 09:56:16 +03:00
|
|
|
REALLOC(sfbuf, sfbufsz, len + 1, NULL);
|
1993-05-08 14:49:52 +04:00
|
|
|
if ((ct = fread(sfbuf, sizeof(char), len, sfp)) < 0 || ct != len) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "cannot read temp file");
|
1993-05-08 14:49:52 +04:00
|
|
|
return NULL;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
sfseek += len; /* update file position */
|
1993-05-08 14:49:52 +04:00
|
|
|
sfbuf[len] = '\0';
|
|
|
|
return sfbuf;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* put_sbuf_line: write a line of text to the scratch file and add a line node
|
1993-04-08 05:07:16 +04:00
|
|
|
to the editor buffer; return a pointer to the end of the text */
|
|
|
|
char *
|
2005-02-17 19:29:26 +03:00
|
|
|
put_sbuf_line(char *cs)
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
line_t *lp;
|
|
|
|
int len, ct;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "out of memory");
|
1993-05-08 14:49:52 +04:00
|
|
|
return NULL;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
/* assert: cs is '\n' terminated */
|
|
|
|
for (s = cs; *s != '\n'; s++)
|
|
|
|
;
|
1993-05-08 14:49:52 +04:00
|
|
|
if (s - cs >= LINECHARS) {
|
|
|
|
sprintf(errmsg, "line too long");
|
2003-09-06 00:16:50 +04:00
|
|
|
free(lp);
|
1993-05-08 14:49:52 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
len = s - cs;
|
1993-04-08 05:07:16 +04:00
|
|
|
/* out of position */
|
|
|
|
if (seek_write) {
|
|
|
|
if (fseek(sfp, 0L, SEEK_END) < 0) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "cannot seek temp file");
|
2003-09-06 00:16:50 +04:00
|
|
|
free(lp);
|
1993-05-08 14:49:52 +04:00
|
|
|
return NULL;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
sfseek = ftell(sfp);
|
|
|
|
seek_write = 0;
|
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
/* assert: SPL1() */
|
1993-04-08 05:07:16 +04:00
|
|
|
if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
|
|
|
|
sfseek = -1;
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "cannot write temp file");
|
2003-09-06 00:16:50 +04:00
|
|
|
free(lp);
|
1993-05-08 14:49:52 +04:00
|
|
|
return NULL;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
lp->len = len;
|
|
|
|
lp->seek = sfseek;
|
1993-11-23 07:41:44 +03:00
|
|
|
add_line_node(lp);
|
1993-04-08 05:07:16 +04:00
|
|
|
sfseek += len; /* update file position */
|
1993-05-08 14:49:52 +04:00
|
|
|
return ++s;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* add_line_node: add a line node in the editor buffer after the current line */
|
1993-04-08 05:07:16 +04:00
|
|
|
void
|
2005-02-17 19:29:26 +03:00
|
|
|
add_line_node(line_t *lp)
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
line_t *cp;
|
|
|
|
|
2005-02-17 19:29:26 +03:00
|
|
|
cp = get_addressed_line_node(current_addr); /* this get_addressed_line_node last! */
|
1994-03-23 07:22:08 +03:00
|
|
|
INSQUE(lp, cp);
|
1993-11-23 07:41:44 +03:00
|
|
|
addr_last++;
|
|
|
|
current_addr++;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* get_line_node_addr: return line number of pointer */
|
1993-04-14 16:22:14 +04:00
|
|
|
long
|
2005-02-17 19:29:26 +03:00
|
|
|
get_line_node_addr(line_t *lp)
|
1993-04-14 16:22:14 +04:00
|
|
|
{
|
1994-02-20 09:56:16 +03:00
|
|
|
line_t *cp = &buffer_head;
|
1993-04-14 16:22:14 +04:00
|
|
|
long n = 0;
|
|
|
|
|
1994-02-20 09:56:16 +03:00
|
|
|
while (cp != lp && (cp = cp->q_forw) != &buffer_head)
|
1993-04-14 16:22:14 +04:00
|
|
|
n++;
|
1994-02-20 09:56:16 +03:00
|
|
|
if (n && cp == &buffer_head) {
|
1993-07-02 14:02:26 +04:00
|
|
|
sprintf(errmsg, "invalid address");
|
|
|
|
return ERR;
|
1998-11-04 16:41:32 +03:00
|
|
|
}
|
|
|
|
return n;
|
1993-04-14 16:22:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* get_addressed_line_node: return pointer to a line node in the editor buffer */
|
1993-04-08 05:07:16 +04:00
|
|
|
line_t *
|
2005-02-17 19:29:26 +03:00
|
|
|
get_addressed_line_node(long n)
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1994-02-20 09:56:16 +03:00
|
|
|
static line_t *lp = &buffer_head;
|
1993-04-08 05:07:16 +04:00
|
|
|
static long on = 0;
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL1();
|
1998-11-04 16:41:32 +03:00
|
|
|
if (n > on) {
|
|
|
|
if (n <= (on + addr_last) >> 1) {
|
1993-04-08 05:07:16 +04:00
|
|
|
for (; on < n; on++)
|
1994-02-20 09:56:16 +03:00
|
|
|
lp = lp->q_forw;
|
1998-11-04 16:41:32 +03:00
|
|
|
} else {
|
1994-02-20 09:56:16 +03:00
|
|
|
lp = buffer_head.q_back;
|
1993-11-23 07:41:44 +03:00
|
|
|
for (on = addr_last; on > n; on--)
|
1994-02-20 09:56:16 +03:00
|
|
|
lp = lp->q_back;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1998-11-04 16:41:32 +03:00
|
|
|
} else {
|
|
|
|
if (n >= on >> 1) {
|
1993-04-08 05:07:16 +04:00
|
|
|
for (; on > n; on--)
|
1994-02-20 09:56:16 +03:00
|
|
|
lp = lp->q_back;
|
1998-11-04 16:41:32 +03:00
|
|
|
} else {
|
1994-02-20 09:56:16 +03:00
|
|
|
lp = &buffer_head;
|
1993-04-08 05:07:16 +04:00
|
|
|
for (on = 0; on < n; on++)
|
1994-02-20 09:56:16 +03:00
|
|
|
lp = lp->q_forw;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1998-11-04 16:41:32 +03:00
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
return lp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-18 03:37:50 +04:00
|
|
|
char *sfn = NULL; /* scratch file name */
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* open_sbuf: open scratch file */
|
|
|
|
int
|
2005-02-17 19:29:26 +03:00
|
|
|
open_sbuf(void)
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1997-03-22 06:02:17 +03:00
|
|
|
int u, fd;
|
2005-06-26 23:10:48 +04:00
|
|
|
const char *tmp;
|
2000-04-18 03:37:50 +04:00
|
|
|
size_t s;
|
1994-03-23 07:04:19 +03:00
|
|
|
|
1994-02-20 09:56:16 +03:00
|
|
|
isbinary = newline_added = 0;
|
1997-03-22 06:02:17 +03:00
|
|
|
fd = -1;
|
1994-03-23 07:04:19 +03:00
|
|
|
u = umask(077);
|
2000-04-18 03:37:50 +04:00
|
|
|
|
|
|
|
if ((tmp = getenv("TMPDIR")) == NULL)
|
|
|
|
tmp = _PATH_TMP;
|
|
|
|
|
|
|
|
if ((s = strlen(tmp)) == 0 || tmp[s - 1] == '/')
|
|
|
|
(void)asprintf(&sfn, "%sed.XXXXXX", tmp);
|
|
|
|
else
|
|
|
|
(void)asprintf(&sfn, "%s/ed.XXXXXX", tmp);
|
|
|
|
|
1997-03-22 06:02:17 +03:00
|
|
|
if ((fd = mkstemp(sfn)) == -1 || (sfp = fdopen(fd, "w+")) == NULL) {
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
warn("%s", sfn);
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "cannot open temp file");
|
1994-03-23 07:04:19 +03:00
|
|
|
umask(u);
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
}
|
1994-03-23 07:04:19 +03:00
|
|
|
umask(u);
|
1993-04-08 05:07:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-04-10 15:46:38 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* close_sbuf: close scratch file */
|
|
|
|
int
|
2005-02-17 19:29:26 +03:00
|
|
|
close_sbuf(void)
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
if (sfp) {
|
1993-04-14 16:22:14 +04:00
|
|
|
if (fclose(sfp) < 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
|
|
|
|
sprintf(errmsg, "cannot close temp file");
|
|
|
|
return ERR;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
sfp = NULL;
|
2000-04-18 03:37:50 +04:00
|
|
|
if (sfn) {
|
|
|
|
unlink(sfn);
|
|
|
|
free(sfn);
|
|
|
|
sfn = NULL;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
sfseek = seek_write = 0;
|
1993-04-15 08:58:32 +04:00
|
|
|
return 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* quit: remove_lines scratch file and exit */
|
1993-04-08 05:07:16 +04:00
|
|
|
void
|
2005-02-17 19:29:26 +03:00
|
|
|
quit(int n)
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
if (sfp) {
|
|
|
|
fclose(sfp);
|
2000-04-18 03:37:50 +04:00
|
|
|
if (sfn) {
|
|
|
|
unlink(sfn);
|
|
|
|
free(sfn);
|
|
|
|
sfn = NULL;
|
|
|
|
}
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
exit(n);
|
1998-07-28 09:31:22 +04:00
|
|
|
/* NOTREACHED */
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
1993-07-02 14:02:26 +04:00
|
|
|
|
|
|
|
|
|
|
|
unsigned char ctab[256]; /* character translation table */
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* init_buffers: open scratch buffer; initialize line queue */
|
1993-07-02 14:02:26 +04:00
|
|
|
void
|
2005-02-17 19:29:26 +03:00
|
|
|
init_buffers(void)
|
1993-07-02 14:02:26 +04:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
1994-02-20 09:56:16 +03:00
|
|
|
/* 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 */
|
|
|
|
setbuffer(stdin, stdinbuf, 1);
|
1993-11-23 07:41:44 +03:00
|
|
|
if (open_sbuf() < 0)
|
1993-07-02 14:02:26 +04:00
|
|
|
quit(2);
|
1994-02-20 09:56:16 +03:00
|
|
|
REQUE(&buffer_head, &buffer_head);
|
1993-07-02 14:02:26 +04:00
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
ctab[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* translit_text: translate characters in a string */
|
1993-07-02 14:02:26 +04:00
|
|
|
char *
|
2005-02-17 19:29:26 +03:00
|
|
|
translit_text(char *s, int len, int from, int to)
|
1993-07-02 14:02:26 +04: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;
|
|
|
|
}
|