OpenSTA/util/ReportTcl.cc

324 lines
7.5 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
2020-03-07 03:50:37 +01:00
// Copyright (c) 2020, Parallax Software, Inc.
2020-04-13 04:19:22 +02:00
//
2018-09-28 17:54:21 +02:00
// This program 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 of the License, or
// (at your option) any later version.
2020-04-13 04:19:22 +02:00
//
2018-09-28 17:54:21 +02:00
// This program 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.
2020-04-13 04:19:22 +02:00
//
2018-09-28 17:54:21 +02:00
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-04-05 23:53:44 +02:00
#include "ReportTcl.hh"
2020-04-05 20:35:51 +02:00
2018-09-28 17:54:21 +02:00
#include <stdio.h>
2020-04-13 04:19:22 +02:00
#include <stdlib.h>
2018-09-28 17:54:21 +02:00
namespace sta {
2020-04-13 04:19:22 +02:00
using ::ClientData;
2018-09-28 17:54:21 +02:00
using ::Tcl_Channel;
2020-04-13 04:19:22 +02:00
using ::Tcl_ChannelOutputProc;
2018-09-28 17:54:21 +02:00
using ::Tcl_ChannelType;
using ::Tcl_DriverOutputProc;
2020-04-13 04:19:22 +02:00
using ::Tcl_GetChannelInstanceData;
using ::Tcl_GetChannelType;
2018-09-28 17:54:21 +02:00
extern "C" {
static int
2020-04-13 04:19:22 +02:00
encapOutputProc(ClientData instanceData,
CONST84 char *buf,
int toWrite,
int *errorCodePtr);
2018-09-28 17:54:21 +02:00
static int
2020-04-13 04:19:22 +02:00
encapErrorOutputProc(ClientData instanceData,
CONST84 char *buf,
int toWrite,
int *errorCodePtr);
2018-09-28 17:54:21 +02:00
static int
encapCloseProc(ClientData instanceData, Tcl_Interp *interp);
static int
2020-04-13 04:19:22 +02:00
encapSetOptionProc(ClientData instanceData,
Tcl_Interp *interp,
CONST84 char *optionName,
CONST84 char *value);
2018-09-28 17:54:21 +02:00
static int
2020-04-13 04:19:22 +02:00
encapGetOptionProc(ClientData instanceData,
Tcl_Interp *interp,
CONST84 char *optionName,
Tcl_DString *dsPtr);
2018-09-28 17:54:21 +02:00
static int
2020-04-13 04:19:22 +02:00
encapInputProc(ClientData instanceData,
char *buf,
int bufSize,
int *errorCodePtr);
2018-09-28 17:54:21 +02:00
static int
2020-04-13 04:19:22 +02:00
encapSeekProc(ClientData instanceData,
long offset,
int seekMode,
int *errorCodePtr);
2018-09-28 17:54:21 +02:00
static void
encapWatchProc(ClientData instanceData, int mask);
static int
2020-04-13 04:19:22 +02:00
encapGetHandleProc(ClientData instanceData,
int direction,
ClientData *handlePtr);
2018-09-28 17:54:21 +02:00
static int
encapBlockModeProc(ClientData instanceData, int mode);
} // extern "C"
Tcl_ChannelType tcl_encap_type_stdout = {
2020-04-13 04:19:22 +02:00
const_cast<char *>("file"),
TCL_CHANNEL_VERSION_4,
encapCloseProc,
encapInputProc,
encapOutputProc,
encapSeekProc,
encapSetOptionProc,
encapGetOptionProc,
encapWatchProc,
encapGetHandleProc,
nullptr, // close2Proc
encapBlockModeProc,
nullptr, // flushProc
nullptr, // handlerProc
nullptr, // wideSeekProc
nullptr, // threadActionProc
nullptr // truncateProc
2018-09-28 17:54:21 +02:00
};
Tcl_ChannelType tcl_encap_type_stderr = {
2020-04-13 04:19:22 +02:00
const_cast<char *>("file"),
TCL_CHANNEL_VERSION_4,
encapCloseProc,
encapInputProc,
encapErrorOutputProc,
encapSeekProc,
encapSetOptionProc,
encapGetOptionProc,
encapWatchProc,
encapGetHandleProc,
nullptr, // close2Proc
encapBlockModeProc,
nullptr, // flushProc
nullptr, // handlerProc
nullptr, // wideSeekProc
nullptr, // threadActionProc
nullptr // truncateProc
2018-09-28 17:54:21 +02:00
};
////////////////////////////////////////////////////////////////
ReportTcl::ReportTcl() :
Report(), interp_(nullptr),
tcl_stdout_(nullptr),
tcl_stderr_(nullptr),
tcl_encap_stdout_(nullptr),
tcl_encap_stderr_(nullptr)
2018-09-28 17:54:21 +02:00
{
}
ReportTcl::~ReportTcl()
{
2019-03-13 01:25:53 +01:00
tcl_encap_stdout_ = nullptr;
tcl_encap_stderr_ = nullptr;
2018-09-28 17:54:21 +02:00
Tcl_UnstackChannel(interp_, tcl_stdout_);
Tcl_UnstackChannel(interp_, tcl_stderr_);
}
void
ReportTcl::setTclInterp(Tcl_Interp *interp)
{
interp_ = interp;
tcl_stdout_ = Tcl_GetStdChannel(TCL_STDOUT);
tcl_stderr_ = Tcl_GetStdChannel(TCL_STDERR);
2020-04-13 04:19:22 +02:00
tcl_encap_stdout_ = Tcl_StackChannel(interp,
&tcl_encap_type_stdout,
this,
TCL_WRITABLE,
tcl_stdout_);
tcl_encap_stderr_ = Tcl_StackChannel(interp,
&tcl_encap_type_stderr,
this,
TCL_WRITABLE,
tcl_stderr_);
2018-09-28 17:54:21 +02:00
}
size_t
2020-12-20 01:27:29 +01:00
ReportTcl::printConsole(const char *buffer,
size_t length)
2018-09-28 17:54:21 +02:00
{
return printTcl(tcl_stdout_, buffer, length);
}
size_t
2020-12-20 01:27:29 +01:00
ReportTcl::printTcl(Tcl_Channel channel,
const char *buffer,
size_t length)
2018-09-28 17:54:21 +02:00
{
const Tcl_ChannelType *ch_type = Tcl_GetChannelType(channel);
Tcl_DriverOutputProc *output_proc = Tcl_ChannelOutputProc(ch_type);
int error_code;
ClientData clientData = Tcl_GetChannelInstanceData(channel);
2020-04-13 04:19:22 +02:00
return output_proc(clientData,
const_cast<char *>(buffer),
length,
&error_code);
2018-09-28 17:54:21 +02:00
}
void
ReportTcl::flush()
{
if (tcl_encap_stdout_)
Tcl_Flush(tcl_encap_stdout_);
if (tcl_encap_stderr_)
Tcl_Flush(tcl_encap_stderr_);
}
2018-09-28 17:54:21 +02:00
// Tcl_Main can eval multiple commands before the flushing the command
// output, so the log/redirect commands must force a flush.
void
ReportTcl::logBegin(const char *filename)
{
flush();
2018-09-28 17:54:21 +02:00
Report::logBegin(filename);
}
void
ReportTcl::logEnd()
{
flush();
2018-09-28 17:54:21 +02:00
Report::logEnd();
}
void
ReportTcl::redirectFileBegin(const char *filename)
{
flush();
2018-09-28 17:54:21 +02:00
Report::redirectFileBegin(filename);
}
void
ReportTcl::redirectFileAppendBegin(const char *filename)
{
flush();
2018-09-28 17:54:21 +02:00
Report::redirectFileAppendBegin(filename);
}
void
ReportTcl::redirectFileEnd()
{
flush();
2018-09-28 17:54:21 +02:00
Report::redirectFileEnd();
}
void
ReportTcl::redirectStringBegin()
{
flush();
2018-09-28 17:54:21 +02:00
Report::redirectStringBegin();
}
const char *
ReportTcl::redirectStringEnd()
{
flush();
2018-09-28 17:54:21 +02:00
return Report::redirectStringEnd();
}
////////////////////////////////////////////////////////////////
static int
2020-12-20 01:27:29 +01:00
encapOutputProc(ClientData instanceData,
CONST84 char *buf,
int toWrite, int *)
2018-09-28 17:54:21 +02:00
{
2020-04-13 04:19:22 +02:00
ReportTcl *report = reinterpret_cast<ReportTcl *>(instanceData);
return report->printString(buf, toWrite);
2018-09-28 17:54:21 +02:00
}
static int
2020-04-13 04:19:22 +02:00
encapErrorOutputProc(ClientData instanceData,
CONST84 char *buf,
int toWrite,
int *)
2018-09-28 17:54:21 +02:00
{
2020-04-13 04:19:22 +02:00
ReportTcl *report = reinterpret_cast<ReportTcl *>(instanceData);
return report->printString(buf, toWrite);
2018-09-28 17:54:21 +02:00
}
static int
2020-12-20 01:27:29 +01:00
encapInputProc(ClientData,
char *,
int,
int *)
2018-09-28 17:54:21 +02:00
{
return -1;
}
static int
2020-12-20 01:27:29 +01:00
encapCloseProc(ClientData instanceData,
Tcl_Interp *)
2018-09-28 17:54:21 +02:00
{
2020-04-13 04:19:22 +02:00
ReportTcl *report = reinterpret_cast<ReportTcl *>(instanceData);
2018-09-28 17:54:21 +02:00
report->logEnd();
report->redirectFileEnd();
report->redirectStringEnd();
return 0;
}
static int
2020-12-20 01:27:29 +01:00
encapSetOptionProc(ClientData,
Tcl_Interp *,
CONST84 char *,
CONST84 char *)
2018-09-28 17:54:21 +02:00
{
return 0;
}
static int
2020-12-20 01:27:29 +01:00
encapGetOptionProc(ClientData,
Tcl_Interp *,
CONST84 char *,
Tcl_DString *)
2018-09-28 17:54:21 +02:00
{
return 0;
}
static int
2020-12-20 01:27:29 +01:00
encapSeekProc(ClientData,
long,
int,
int *)
2018-09-28 17:54:21 +02:00
{
return -1;
}
static void
encapWatchProc(ClientData, int)
{
}
static int
2020-12-20 01:27:29 +01:00
encapGetHandleProc(ClientData,
int,
ClientData *)
2018-09-28 17:54:21 +02:00
{
return TCL_ERROR;
}
static int
2020-12-20 01:27:29 +01:00
encapBlockModeProc(ClientData,
int)
2018-09-28 17:54:21 +02:00
{
return 0;
}
2020-04-13 04:19:22 +02:00
} // namespace sta