File size: 7,879 Bytes
23d354c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2026, Parallax Software, Inc.
// 
// 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.
// 
// 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.
// 
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// 
// The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software.
// 
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 
// This notice may not be removed or altered from any source distribution.

#include "ReportTcl.hh"

#include <cstdio>
#include <cstdlib>

namespace sta {

using ::ClientData;
using ::Tcl_Channel;
using ::Tcl_ChannelOutputProc;
using ::Tcl_ChannelType;
using ::Tcl_DriverOutputProc;
using ::Tcl_GetChannelInstanceData;
using ::Tcl_GetChannelType;

extern "C" {

#if TCL_MAJOR_VERSION >= 9
#define CONST84 const
#endif

static int
encapOutputProc(ClientData instanceData,
                CONST84 char *buf,
                int toWrite,
                int *errorCodePtr);
static int
encapSetOptionProc(ClientData instanceData,
                   Tcl_Interp *interp,
                   CONST84 char *optionName,
                   CONST84 char *value);
static int
encapGetOptionProc(ClientData instanceData,
                   Tcl_Interp *interp,
                   CONST84 char *optionName,
                   Tcl_DString *dsPtr);
static int
encapInputProc(ClientData instanceData,
               char *buf,
               int bufSize,
               int *errorCodePtr);
static void
encapWatchProc(ClientData instanceData, int mask);
static int
encapGetHandleProc(ClientData instanceData,
                   int direction,
                   ClientData *handlePtr);
static int
encapBlockModeProc(ClientData instanceData, int mode);

static int
encapClose2Proc(ClientData instanceData,
                Tcl_Interp *interp,
                int flags);

#if TCL_MAJOR_VERSION < 9
static int
encapCloseProc(ClientData instanceData, Tcl_Interp *interp);
static int
encapSeekProc(ClientData instanceData,
              // NOLINTNEXTLINE(google-runtime-int)  // Tcl_DriverSeekProc offset
              long offset,
              int seekMode,
              int *errorCodePtr);
#endif

}  // extern "C"

Tcl_ChannelType tcl_encap_type_stdout = {
  .typeName = "file",
  .version = TCL_CHANNEL_VERSION_5,
#if TCL_MAJOR_VERSION < 9
  .closeProc = encapCloseProc,
#else
  .closeProc = nullptr,  // closeProc unused
#endif
  .inputProc = encapInputProc,
  .outputProc = encapOutputProc,
#if TCL_MAJOR_VERSION < 9
  .seekProc = encapSeekProc,
#else
  .seekProc = nullptr,  // seekProc unused
#endif
  .setOptionProc = encapSetOptionProc,
  .getOptionProc = encapGetOptionProc,
  .watchProc = encapWatchProc,
  .getHandleProc = encapGetHandleProc,
  .close2Proc = encapClose2Proc,
  .blockModeProc = encapBlockModeProc,
  .flushProc = nullptr,
  .handlerProc = nullptr,
  .wideSeekProc = nullptr,
  .threadActionProc = nullptr,
  .truncateProc = nullptr,
};

////////////////////////////////////////////////////////////////

ReportTcl::ReportTcl() = default;

ReportTcl::~ReportTcl()
{
  tcl_encap_stdout_ = nullptr;
  tcl_encap_stderr_ = nullptr;
  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);
  tcl_encap_stdout_ = Tcl_StackChannel(interp,
                                       &tcl_encap_type_stdout,
                                       this,
                                       TCL_WRITABLE,
                                       tcl_stdout_);
  tcl_encap_stderr_ = Tcl_StackChannel(interp,
                                       &tcl_encap_type_stdout,
                                       this,
                                       TCL_WRITABLE,
                                       tcl_stderr_);
}

size_t
ReportTcl::printConsole(const char *buffer,
                        size_t length)
{
  return printTcl(tcl_stdout_, buffer, length);
}

size_t
ReportTcl::printTcl(Tcl_Channel channel,
                    const char *buffer,
                    size_t length)
{
  const Tcl_ChannelType *ch_type = Tcl_GetChannelType(channel);
  Tcl_DriverOutputProc *output_proc = Tcl_ChannelOutputProc(ch_type);
  int error_code;
  ClientData clientData = Tcl_GetChannelInstanceData(channel);
  return output_proc(clientData,
                     const_cast<char *>(buffer),
                     length,
                     &error_code);
}

void
ReportTcl::flush()
{
  if (tcl_encap_stdout_)
    Tcl_Flush(tcl_encap_stdout_);
  if (tcl_encap_stderr_)
    Tcl_Flush(tcl_encap_stderr_);
}

// Tcl_Main can eval multiple commands before the flushing the command
// output, so the log/redirect commands must force a flush.
void
ReportTcl::logBegin(std::string_view filename)
{
  flush();
  Report::logBegin(filename);
}

void
ReportTcl::logEnd()
{
  flush();
  Report::logEnd();
}

void
ReportTcl::redirectFileBegin(std::string_view filename)
{
  flush();
  Report::redirectFileBegin(filename);
}

void
ReportTcl::redirectFileAppendBegin(std::string_view filename)
{
  flush();
  Report::redirectFileAppendBegin(filename);
}

void
ReportTcl::redirectFileEnd()
{
  flush();
  Report::redirectFileEnd();
}

void
ReportTcl::redirectStringBegin()
{
  flush();
  Report::redirectStringBegin();
}

const char *
ReportTcl::redirectStringEnd()
{
  flush();
  return Report::redirectStringEnd();
}

////////////////////////////////////////////////////////////////

static int
encapOutputProc(ClientData instanceData,
                CONST84 char *buf,
                int toWrite,
                int *)
{
  ReportTcl *report = reinterpret_cast<ReportTcl *>(instanceData);
  return report->printString(buf, toWrite);
}

static int
encapInputProc(ClientData,
               char *,
               int,
               int *)
{
  return -1;
}

static int
encapSetOptionProc(ClientData,
                   Tcl_Interp *,
                   CONST84 char *,
                   CONST84 char *)
{
  return 0;
}

static int
encapGetOptionProc(ClientData,
                   Tcl_Interp *,
                   CONST84 char *,
                   Tcl_DString *)
{
  return 0;
}

static void
encapWatchProc(ClientData, int)
{
}

static int
encapGetHandleProc(ClientData,
                   int,
                   ClientData *)
{
  return TCL_ERROR;
}

static int
encapBlockModeProc(ClientData,
                   int)
{
  return 0;
}

// Close channel implementing CloseProc() or Close2Proc()
static int
closeChannel(ReportTcl *report)
{
  report->logEnd();
  report->redirectFileEnd();
  report->redirectStringEnd();
  return 0;
}

static int
encapClose2Proc(ClientData instanceData,
                Tcl_Interp *,
                int)
{
  return closeChannel(reinterpret_cast<ReportTcl *>(instanceData));
}

#if TCL_MAJOR_VERSION < 9

static int
encapCloseProc(ClientData instanceData,
               Tcl_Interp *)
{
  return closeChannel(reinterpret_cast<ReportTcl *>(instanceData));
}

static int
encapSeekProc(ClientData,
              // NOLINTNEXTLINE(google-runtime-int)  // Tcl_DriverSeekProc offset
              long,
              int,
              int *)
{
  return -1;
}

#endif

}  // namespace sta