1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/* This file is part of GDBM, the GNU data base manager.
Copyright (C) 2024-2025 Free Software Foundation, Inc.
GDBM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GDBM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
#include "autoconf.h"
#include <stdio.h>
#include <stdarg.h>
#include "gdbmtool.h"
int
pager_flush (struct pagerfile *pfp)
{
if (pfp->bufsize > 0)
{
if (fwrite (pfp->bufbase, pfp->bufsize, 1, pfp->stream) != 1)
return -1;
pfp->bufsize = 0;
}
if (fflush (pfp->stream))
return -1;
return 0;
}
static int
pager_checklines (struct pagerfile *pfp)
{
if (pfp->nlines > pfp->maxlines)
{
FILE *pagfp = popen (pfp->pager, "w");
if (!pagfp)
{
terror (_("cannot run pager `%s': %s"), pfp->pager, strerror (errno));
pfp->mode = mode_transparent;
}
else
{
pfp->mode = mode_pager;
pfp->stream = pagfp;
}
return pager_flush (pfp);
}
return 0;
}
static ssize_t
memccount (char const *str, int c, size_t len)
{
ssize_t n = 0;
for (; len > 0; len--, str++)
if (*str == c)
n++;
return n;
}
ssize_t
pager_write (struct pagerfile *pfp, const char *buffer, size_t size)
{
if (pfp->mode != mode_initial)
{
return fwrite (buffer, 1, size, pfp->stream);
}
while (pfp->bufsize + size > pfp->bufmax)
pfp->bufbase = e2nrealloc (pfp->bufbase, &pfp->bufmax, 1);
memcpy (pfp->bufbase + pfp->bufsize, buffer, size);
pfp->bufsize += size;
pfp->nlines += memccount (buffer, '\n', size);
if (pager_checklines (pfp))
return -1;
return size;
}
ssize_t
pager_writez (struct pagerfile *pfp, const char *str)
{
return pager_write (pfp, str, strlen (str));
}
int
pager_putc (struct pagerfile *pfp, int c)
{
char buf[1];
buf[0] = c;
return pager_write (pfp, buf, 1);
}
ssize_t
pager_writeln (struct pagerfile *pfp, const char *str)
{
ssize_t ret = pager_writez (pfp, str);
if (ret > 0)
{
if (pager_write (pfp, "\n", 1) == 1)
ret++;
}
return ret;
}
int
pager_vprintf (struct pagerfile *pfp, const char *fmt, va_list ap)
{
ssize_t ret;
if (!pfp->bufbase)
{
if (pfp->bufmax == 0)
pfp->bufmax = 512;
pfp->bufbase = ecalloc (1, pfp->bufmax);
}
for (;;)
{
va_list aq;
ssize_t n;
char *bufptr = pfp->bufbase + pfp->bufsize;
size_t buflen = pfp->bufmax - pfp->bufsize;
va_copy (aq, ap);
n = vsnprintf (bufptr, buflen, fmt, aq);
va_end (aq);
if (n < 0 || n >= buflen || !memchr (bufptr, '\0', n + 1))
{
pfp->bufbase = e2nrealloc (pfp->bufbase, &pfp->bufmax, 1);
}
else
{
if (pfp->mode == mode_initial)
pfp->nlines += memccount (bufptr, '\n', n);
ret = n;
pfp->bufsize += n;
break;
}
}
if (pfp->mode == mode_initial)
ret = pager_checklines (pfp);
else
ret = pager_flush (pfp);
return ret;
}
int
pager_printf (struct pagerfile *pfp, const char *fmt, ...)
{
va_list ap;
int ret;
va_start (ap, fmt);
ret = pager_vprintf (pfp, fmt, ap);
va_end (ap);
return ret;
}
void
pager_close (struct pagerfile *pfp)
{
if (pfp->bufsize)
pager_flush (pfp);
if (pfp->mode == mode_pager)
pclose (pfp->stream);
free (pfp->bufbase);
free (pfp->pager);
free (pfp);
}
int
pager_error (struct pagerfile *pfp)
{
return ferror (pfp->stream);
}
PAGERFILE *
pager_open (FILE *stream, size_t maxlines, char const *pager)
{
struct pagerfile *pfp;
pfp = ecalloc (1, sizeof (*pfp));
pfp->stream = stream;
if (maxlines > 0 && pager)
{
pfp->pager = estrdup (pager);
pfp->mode = mode_initial;
pfp->maxlines = maxlines;
}
else
pfp->mode = mode_transparent;
return pfp;
}
PAGERFILE *
pager_create (char const *pager)
{
struct pagerfile *pfp;
FILE *pagfp = popen (pager, "w");
if (!pagfp)
{
terror (_("cannot run command `%s': %s"), pager, strerror (errno));
return NULL;
}
pfp = ecalloc (1, sizeof (*pfp));
pfp->pager = estrdup (pager);
pfp->mode = mode_pager;
pfp->stream = pagfp;
return pfp;
}
|