#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <err.h>
#include "mdef.h"
#include "stdd.h"
#include "extern.h"
#include "pathnames.h"
char *ep;
static char *strspace;
char *endest;
static size_t strsize = STRSPMAX;
static size_t bufsize = BUFSIZE;
unsigned char *buf;
unsigned char *bufbase;
unsigned char *bbase[MAXINP];
unsigned char *bp;
unsigned char *endpbb;
ptrdiff_t
doindex(const char *s1, const char *s2)
{
char *t;
t = strstr(s1, s2);
if (t == NULL)
return (-1);
else
return (t - s1);
}
void
pushback(int c)
{
if (c == EOF)
return;
if (bp >= endpbb)
enlarge_bufspace();
*bp++ = c;
}
void
pbstr(const char *s)
{
size_t n;
n = strlen(s);
while (endpbb - bp <= (long)n)
enlarge_bufspace();
while (n > 0)
*bp++ = s[--n];
}
void
pbnum(int n)
{
pbnumbase(n, 10, 0);
}
void
pbnumbase(int n, int base, int d)
{
static char digits[36] __nonstring =
"0123456789abcdefghijklmnopqrstuvwxyz";
unsigned int num;
int printed = 0;
if (base > 36)
m4errx(1, "base %d > 36: not supported.", base);
if (base < 2)
m4errx(1, "bad base %d for conversion.", base);
num = (n < 0) ? -n : n;
do {
pushback(digits[num % base]);
printed++;
} while ((num /= base) > 0);
while (printed++ < d)
pushback('0');
if (n < 0)
pushback('-');
}
void
pbunsigned(unsigned long n)
{
do {
pushback(n % 10 + '0');
} while ((n /= 10) > 0);
}
void
initspaces(void)
{
int i;
strspace = xalloc(strsize+1, NULL);
ep = strspace;
endest = strspace+strsize;
buf = xalloc(bufsize, NULL);
bufbase = buf;
bp = buf;
endpbb = buf + bufsize;
for (i = 0; i < MAXINP; i++)
bbase[i] = buf;
}
void
enlarge_strspace(void)
{
char *newstrspace;
int i;
strsize *= 2;
newstrspace = malloc(strsize + 1);
if (!newstrspace)
errx(1, "string space overflow");
memcpy(newstrspace, strspace, strsize/2);
for (i = 0; i <= sp; i++)
if (sstack[i] == STORAGE_STRSPACE)
mstack[i].sstr = (mstack[i].sstr - strspace) +
newstrspace;
ep = (ep - strspace) + newstrspace;
free(strspace);
strspace = newstrspace;
endest = strspace + strsize;
}
void
enlarge_bufspace(void)
{
unsigned char *newbuf;
int i;
bufsize += bufsize/2;
newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
for (i = 0; i < MAXINP; i++)
bbase[i] = (bbase[i]-buf)+newbuf;
bp = (bp-buf)+newbuf;
bufbase = (bufbase-buf)+newbuf;
buf = newbuf;
endpbb = buf+bufsize;
}
void
chrsave(int c)
{
if (ep >= endest)
enlarge_strspace();
*ep++ = c;
}
void
getdiv(int n)
{
int c;
if (active == outfile[n])
m4errx(1, "undivert: diversion still active.");
rewind(outfile[n]);
while ((c = getc(outfile[n])) != EOF)
putc(c, active);
(void) fclose(outfile[n]);
outfile[n] = NULL;
}
void
onintr(int signo __unused)
{
#define intrmessage "m4: interrupted.\n"
write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
_exit(1);
}
void
killdiv(void)
{
int n;
for (n = 0; n < maxout; n++)
if (outfile[n] != NULL) {
(void) fclose(outfile[n]);
}
}
extern char *__progname;
void
m4errx(int eval, const char *fmt, ...)
{
fprintf(stderr, "%s: ", __progname);
fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);
if (fmt != NULL) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
fprintf(stderr, "\n");
exit(eval);
}
void
resizedivs(int n)
{
int i;
outfile = xreallocarray(outfile, n, sizeof(FILE *),
"too many diverts %d", n);
for (i = maxout; i < n; i++)
outfile[i] = NULL;
maxout = n;
}
void *
xalloc(size_t n, const char *fmt, ...)
{
void *p = malloc(n);
if (p == NULL) {
if (fmt == NULL)
err(1, "malloc");
else {
va_list va;
va_start(va, fmt);
verr(1, fmt, va);
va_end(va);
}
}
return p;
}
void *
xcalloc(size_t n, size_t s, const char *fmt, ...)
{
void *p = calloc(n, s);
if (p == NULL) {
if (fmt == NULL)
err(1, "calloc");
else {
va_list va;
va_start(va, fmt);
verr(1, fmt, va);
va_end(va);
}
}
return p;
}
void *
xrealloc(void *old, size_t n, const char *fmt, ...)
{
char *p = realloc(old, n);
if (p == NULL) {
free(old);
if (fmt == NULL)
err(1, "realloc");
else {
va_list va;
va_start(va, fmt);
verr(1, fmt, va);
va_end(va);
}
}
return p;
}
void *
xreallocarray(void *old, size_t s1, size_t s2, const char *fmt, ...)
{
void *p = reallocarray(old, s1, s2);
if (p == NULL) {
free(old);
if (fmt == NULL)
err(1, "reallocarray");
else {
va_list va;
va_start(va, fmt);
verr(1, fmt, va);
va_end(va);
}
}
return p;
}
char *
xstrdup(const char *s)
{
char *p = strdup(s);
if (p == NULL)
err(1, "strdup");
return p;
}
void
usage(void)
{
fprintf(stderr, "usage: m4 [-EgPs] [-Dname[=value]] [-d flags] "
"[-I dirname] [-o filename]\n"
"\t[-t macro] [-Uname] [file ...]\n");
exit(1);
}
int
obtain_char(struct input_file *f)
{
if (f->c == EOF)
return EOF;
f->c = fgetc(f->file);
if (f->c == '\n')
f->lineno++;
return f->c;
}
void
set_input(struct input_file *f, FILE *real, const char *name)
{
f->file = real;
f->lineno = 1;
f->c = 0;
f->name = xstrdup(name);
emit_synchline();
}
void
do_emit_synchline(void)
{
fprintf(active, "#line %lu \"%s\"\n",
infile[ilevel].lineno, infile[ilevel].name);
infile[ilevel].synch_lineno = infile[ilevel].lineno;
}
void
release_input(struct input_file *f)
{
if (ferror(f->file))
errx(1, "Fatal error reading from %s\n", f->name);
if (f->file != stdin)
fclose(f->file);
f->c = EOF;
}
void
doprintlineno(struct input_file *f)
{
pbunsigned(f->lineno);
}
void
doprintfilename(struct input_file *f)
{
pbstr(rquote);
pbstr(f->name);
pbstr(lquote);
}
size_t
buffer_mark(void)
{
return bp - buf;
}
void
dump_buffer(FILE *f, size_t m)
{
unsigned char *s;
for (s = bp; s-buf > (long)m;)
fputc(*--s, f);
}