#include "expect_cf.h"
#include <stdio.h>
#include "tclInt.h"
#include "expect_comm.h"
#include "exp_int.h"
#include "exp_rename.h"
#include "exp_log.h"
int loguser = TRUE;
int logfile_all = FALSE;
FILE *logfile = 0;
FILE *debugfile = 0;
int exp_is_debugging = FALSE;
#define LOGUSER (loguser || force_stdout)
void
exp_log TCL_VARARGS_DEF(int,arg1)
{
int force_stdout;
char *fmt;
va_list args;
force_stdout = TCL_VARARGS_START(int,arg1,args);
fmt = va_arg(args,char *);
if (debugfile) vfprintf(debugfile,fmt,args);
if (logfile_all || (LOGUSER && logfile)) vfprintf(logfile,fmt,args);
if (LOGUSER) vfprintf(stdout,fmt,args);
va_end(args);
}
void
exp_nflog(buf,force_stdout)
char *buf;
int force_stdout;
{
int length = strlen(buf);
if (debugfile) fwrite(buf,1,length,debugfile);
if (logfile_all || (LOGUSER && logfile)) fwrite(buf,1,length,logfile);
if (LOGUSER) fwrite(buf,1,length,stdout);
#if 0
if (logfile_all || (LOGUSER && logfile)) {
int newlength = exp_copy_out(length);
fwrite(exp_out_buffer,1,newlength,logfile);
}
#endif
}
#undef LOGUSER
void
debuglog TCL_VARARGS_DEF(char *,arg1)
{
char *fmt;
va_list args;
fmt = TCL_VARARGS_START(char *,arg1,args);
if (debugfile) vfprintf(debugfile,fmt,args);
if (is_debugging) {
vfprintf(stderr,fmt,args);
if (logfile) vfprintf(logfile,fmt,args);
}
va_end(args);
}
void
exp_errorlog TCL_VARARGS_DEF(char *,arg1)
{
char *fmt;
va_list args;
fmt = TCL_VARARGS_START(char *,arg1,args);
vfprintf(stderr,fmt,args);
if (debugfile) vfprintf(debugfile,fmt,args);
if (logfile) vfprintf(logfile,fmt,args);
va_end(args);
}
void
exp_nferrorlog(buf,force_stdout)
char *buf;
int force_stdout;
{
int length = strlen(buf);
fwrite(buf,1,length,stderr);
if (debugfile) fwrite(buf,1,length,debugfile);
if (logfile) fwrite(buf,1,length,logfile);
}
#if 0
static int out_buffer_size;
static char *outp_last;
static char *out_buffer;
static char *outp;
void
exp_init_log()
{
out_buffer = ckalloc(BUFSIZ);
out_buffer_size = BUFSIZ;
outp_last = out_buffer + BUFSIZ - 1;
}
char *
enlarge_out_buffer()
{
int offset = outp - out_buffer;
int new_out_buffer_size = out_buffer_size = BUFSIZ;
realloc(out_buffer,new_out_buffer_size);
out_buffer_size = new_out_buffer_size;
outp = out_buffer + offset;
outp_last = out_buffer + out_buffer_size - 1;
return(out_buffer);
}
void
exp_sprintf TCL_VARARGS_DEF(char *,arg1)
{
char *fmt;
va_list args;
char int_literal[20];
char *int_litp;
char *width;
char *string_arg;
int int_arg;
char *int_fmt;
fmt = TCL_VARARGS_START(char *,arg1,args);
while (*fmt != '\0') {
if (*fmt != '%') {
*outp++ = *fmt++;
continue;
}
if (isdigit(*fmt)) {
width = fmt++;
} else width = 0;
switch (*fmt) {
case 's':
string_arg = va_arg(args,char *);
while (*string_arg) {
if (outp == outp_last) {
if (enlarge_out_buffer() == 0) {
return;
}
}
*outp++ = *string_arg++;
}
fmt++;
break;
case 'd':
int_arg = va_arg(args,int);
if (width) int_fmt = width;
else int_fmt = fmt;
sprintf(int_literal,int_fmt,int_arg);
int_litp = int_literal;
for (int_litp;*int_litp;) {
if (enlarge_out_buffer() == 0) return;
*outp++ = *int_litp++;
}
fmt++;
break;
default:
if (enlarge_out_buffer() == 0) return;
*outp++ = *fmt++;
break;
}
}
}
int
exp_copy_out(char *s)
{
outp = out_buffer;
int count = 0;
while (*s) {
if ((*s == '\r') && (*(s+1) =='\n')) s++;
if (enlarge_out_buffer() == 0) {
break;
}
*outp = *s;
count++;
}
return count;
}
#endif