#include <sys/cdefs.h>
#ifndef lint
#if 0
static char *rcsid = "@(#)buf.c,v 1.4 1994/02/01 00:34:35 alm Exp";
#else
__RCSID("$NetBSD: buf.c,v 1.28 2025/09/17 20:35:11 rillig Exp $");
#endif
#endif
#include <sys/file.h>
#include <sys/stat.h>
#include <paths.h>
#include <stdio.h>
#include <err.h>
#include "ed.h"
FILE *sfp;
off_t sfseek;
int seek_write;
line_t buffer_head;
char *
get_sbuf_line(line_t *lp)
{
static char *sfbuf = NULL;
static int sfbufsz = 0;
int len, ct;
if (lp == &buffer_head)
return NULL;
seek_write = 1;
if (sfseek != lp->seek) {
sfseek = lp->seek;
if (fseek(sfp, sfseek, SEEK_SET) < 0) {
fprintf(stderr, "%s\n", strerror(errno));
seterrmsg("cannot seek temp file");
return NULL;
}
}
len = lp->len;
REALLOC(sfbuf, sfbufsz, len + 1, NULL);
if ((ct = fread(sfbuf, sizeof(char), len, sfp)) < 0 || ct != len) {
fprintf(stderr, "%s\n", strerror(errno));
seterrmsg("cannot read temp file");
return NULL;
}
sfseek += len;
sfbuf[len] = '\0';
return sfbuf;
}
char *
put_sbuf_line(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));
seterrmsg("out of memory");
return NULL;
}
for (s = cs; *s != '\n'; s++)
;
if (s - cs >= LINECHARS) {
seterrmsg("line too long");
free(lp);
return NULL;
}
len = s - cs;
if (seek_write) {
if (fseek(sfp, 0L, SEEK_END) < 0) {
fprintf(stderr, "%s\n", strerror(errno));
seterrmsg("cannot seek temp file");
free(lp);
return NULL;
}
sfseek = ftell(sfp);
seek_write = 0;
}
if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
sfseek = -1;
fprintf(stderr, "%s\n", strerror(errno));
seterrmsg("cannot write temp file");
free(lp);
return NULL;
}
lp->len = len;
lp->seek = sfseek;
add_line_node(lp);
sfseek += len;
return ++s;
}
void
add_line_node(line_t *lp)
{
line_t *cp;
cp = get_addressed_line_node(current_addr);
INSQUE(lp, cp);
addr_last++;
current_addr++;
}
long
get_line_node_addr(line_t *lp)
{
line_t *cp = &buffer_head;
long n = 0;
while (cp != lp && (cp = cp->q_forw) != &buffer_head)
n++;
if (n && cp == &buffer_head) {
seterrmsg("invalid address");
return ERR;
}
return n;
}
line_t *
get_addressed_line_node(long n)
{
static line_t *lp = &buffer_head;
static long on = 0;
SPL1();
if (n > on) {
if (n <= (on + addr_last) >> 1) {
for (; on < n; on++)
lp = lp->q_forw;
} else {
lp = buffer_head.q_back;
for (on = addr_last; on > n; on--)
lp = lp->q_back;
}
} else {
if (n >= on >> 1) {
for (; on > n; on--)
lp = lp->q_back;
} else {
lp = &buffer_head;
for (on = 0; on < n; on++)
lp = lp->q_forw;
}
}
SPL0();
return lp;
}
char *sfn = NULL;
int
open_sbuf(void)
{
int u, fd;
const char *tmp;
size_t s;
isbinary = newline_added = 0;
fd = -1;
u = umask(077);
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);
if (sfn == NULL) {
warn(NULL);
seterrmsg("could not allocate memory");
umask(u);
return ERR;
}
if ((fd = mkstemp(sfn)) == -1 || (sfp = fdopen(fd, "w+")) == NULL) {
if (fd != -1)
close(fd);
warn("%s", sfn);
seterrmsg("cannot open temp file");
umask(u);
return ERR;
}
umask(u);
return 0;
}
int
close_sbuf(void)
{
if (sfp) {
if (fclose(sfp) < 0) {
fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
seterrmsg("cannot close temp file");
return ERR;
}
sfp = NULL;
if (sfn) {
unlink(sfn);
free(sfn);
sfn = NULL;
}
}
sfseek = seek_write = 0;
return 0;
}
void
quit(int n)
{
if (sfp) {
fclose(sfp);
if (sfn) {
unlink(sfn);
free(sfn);
sfn = NULL;
}
}
exit(n);
}
unsigned char ctab[256];
void
init_buffers(void)
{
int i = 0;
setbuffer(stdin, stdinbuf, 1);
if (open_sbuf() < 0)
quit(2);
REQUE(&buffer_head, &buffer_head);
for (i = 0; i < 256; i++)
ctab[i] = i;
}
char *
translit_text(char *s, int len, int from, int to)
{
static int i = 0;
unsigned char *us;
ctab[i] = i;
ctab[i = from] = to;
for (us = (unsigned char *) s; len-- > 0; us++)
*us = ctab[*us];
return s;
}