1993-04-08 05:07:16 +04:00
|
|
|
/* buf.c: This file contains the scratch-file buffer rountines for the
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
#ifndef lint
|
1994-02-20 09:56:16 +03:00
|
|
|
static char *rcsid = "@(#)buf.c,v 1.4 1994/02/01 00:34:35 alm Exp";
|
1993-04-08 05:07:16 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/file.h>
|
|
|
|
|
|
|
|
#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 *
|
1993-11-23 07:41:44 +03:00
|
|
|
get_sbuf_line(lp)
|
1993-04-08 05:07:16 +04:00
|
|
|
line_t *lp;
|
|
|
|
{
|
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 *
|
1993-11-23 07:41:44 +03:00
|
|
|
put_sbuf_line(cs)
|
1993-04-08 05:07:16 +04:00
|
|
|
char *cs;
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
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");
|
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");
|
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
|
1993-11-23 07:41:44 +03:00
|
|
|
add_line_node(lp)
|
1993-04-08 05:07:16 +04:00
|
|
|
line_t *lp;
|
|
|
|
{
|
|
|
|
line_t *cp;
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
cp = get_addressed_line_node(current_addr); /* this get_addressed_line_node last! */
|
1994-02-20 09:56:16 +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
|
1993-11-23 07:41:44 +03:00
|
|
|
get_line_node_addr(lp)
|
1993-04-14 16:22:14 +04:00
|
|
|
line_t *lp;
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
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 *
|
1993-11-23 07:41:44 +03:00
|
|
|
get_addressed_line_node(n)
|
1993-04-08 05:07:16 +04:00
|
|
|
long n;
|
|
|
|
{
|
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();
|
1993-04-08 05:07:16 +04:00
|
|
|
if (n > on)
|
1993-11-23 07:41:44 +03:00
|
|
|
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;
|
1993-04-10 15:46:38 +04: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
|
|
|
}
|
|
|
|
else
|
1993-04-10 15:46:38 +04:00
|
|
|
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;
|
1993-04-10 15:46:38 +04: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
|
|
|
}
|
1993-11-23 07:41:44 +03:00
|
|
|
SPL0();
|
1993-04-08 05:07:16 +04:00
|
|
|
return lp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-20 09:56:16 +03:00
|
|
|
extern int newline_added;
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
char sfn[15] = ""; /* scratch file name */
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* open_sbuf: open scratch file */
|
|
|
|
int
|
|
|
|
open_sbuf()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1994-02-20 09:56:16 +03:00
|
|
|
isbinary = newline_added = 0;
|
1993-04-08 05:07:16 +04:00
|
|
|
strcpy(sfn, "/tmp/ed.XXXXXX");
|
|
|
|
if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "cannot open temp file");
|
1993-04-08 05:07:16 +04:00
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-04-10 15:46:38 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* close_sbuf: close scratch file */
|
|
|
|
int
|
|
|
|
close_sbuf()
|
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;
|
1993-04-10 15:46:38 +04:00
|
|
|
unlink(sfn);
|
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
|
|
|
|
quit(n)
|
|
|
|
int n;
|
|
|
|
{
|
|
|
|
if (sfp) {
|
|
|
|
fclose(sfp);
|
1993-05-08 14:49:52 +04:00
|
|
|
unlink(sfn);
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
exit(n);
|
|
|
|
}
|
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
|
1993-11-23 07:41:44 +03:00
|
|
|
init_buffers()
|
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 *
|
1993-11-23 07:41:44 +03:00
|
|
|
translit_text(s, len, from, to)
|
1993-07-02 14:02:26 +04: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;
|
|
|
|
}
|