1 // Created on: 1995-02-23
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <Draw_Interpretor.hxx>
18 #include <Draw_Appli.hxx>
19 #include <Standard_SStream.hxx>
20 #include <Standard_RangeError.hxx>
21 #include <Standard_ErrorHandler.hxx>
22 #include <Standard_Macro.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TCollection_ExtendedString.hxx>
26 #include <OSD_Process.hxx>
27 #include <OSD_Path.hxx>
29 #include <OSD_File.hxx>
37 // for capturing of cout and cerr (dup(), dup2())
42 #if ! defined(STDOUT_FILENO)
43 #define STDOUT_FILENO fileno(stdout)
45 #if ! defined(STDERR_FILENO)
46 #define STDERR_FILENO fileno(stderr)
49 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 1)))
55 void dumpArgs (Standard_OStream& os, int argc, const char *argv[])
57 for (int i=0; i < argc; i++)
62 void flush_standard_streams ()
70 int capture_start (OSD_File& theTmpFile, int std_fd)
72 theTmpFile.BuildTemporary();
73 if (theTmpFile.Failed())
75 cerr << "Error: cannot create temporary file for capturing console output" << endl;
79 // remember current file descriptors of standard stream, and replace it by temporary
80 return theTmpFile.Capture(std_fd);
83 void capture_end (OSD_File* tmp_file, int std_fd, int save_fd, Standard_OStream &log, Standard_Boolean doEcho)
88 // restore normal descriptors of console stream
89 dup2(save_fd, std_fd);
92 // extract all output and copy it to log and optionally to cout
93 const int BUFSIZE = 2048;
94 TCollection_AsciiString buf;
96 while (tmp_file->ReadLine (buf, BUFSIZE) > 0)
103 // close temporary file
106 // remove temporary file if this is not done by the system
107 if (tmp_file->Exists())
114 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
115 static Standard_Integer CommandCmd
116 (ClientData theClientData, Tcl_Interp *interp,
117 Standard_Integer argc, const char* argv[])
119 static Standard_Integer CommandCmd
120 (ClientData theClientData, Tcl_Interp *interp,
121 Standard_Integer argc, char* argv[])
124 static Standard_Integer code;
126 Draw_Interpretor::CallBackData* aCallback = (Draw_Interpretor::CallBackData* )theClientData;
127 Draw_Interpretor& di = *(aCallback->myDI);
129 // log command execution, except commands manipulating log itself and echo
130 Standard_Boolean isLogManipulation = (strcmp (argv[0], "dlog") == 0 ||
131 strcmp (argv[0], "decho") == 0);
132 Standard_Boolean doLog = (di.GetDoLog() && ! isLogManipulation);
133 Standard_Boolean doEcho = (di.GetDoEcho() && ! isLogManipulation);
135 dumpArgs (di.Log(), argc, argv);
137 dumpArgs (cout, argc, argv);
139 // flush cerr and cout
140 flush_standard_streams();
142 // capture cout and cerr to log
143 OSD_File aFile_out, aFile_err;
144 int fd_err_save = -1;
145 int fd_out_save = -1;
148 fd_out_save = capture_start (aFile_out, STDOUT_FILENO);
149 fd_err_save = capture_start (aFile_err, STDERR_FILENO);
156 // get exception if control-break has been pressed
159 // OCC680: Transfer UTF-8 directly to OCC commands without locale usage
161 Standard_Integer fres = aCallback->Invoke ( di, argc, argv /*anArgs.GetArgv()*/ );
165 catch (Standard_Failure) {
167 Handle(Standard_Failure) E = Standard_Failure::Caught();
169 // fail if Draw_ExitOnCatch is set
171 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
172 const char* cc = Tcl_GetVar(interp,
173 "Draw_ExitOnCatch",TCL_GLOBAL_ONLY);
175 char* const cc = Tcl_GetVar(interp,
176 "Draw_ExitOnCatch",TCL_GLOBAL_ONLY);
179 cout << "An exception was caught " << E << endl;
181 if (cc && Draw::Atoi(cc)) {
185 Tcl_Eval(interp,"exit");
189 // get the error message
191 ss << "** Exception ** " << E << ends;
192 Tcl_SetResult(interp,(char*)(ss.str().c_str()),TCL_VOLATILE);
197 flush_standard_streams();
199 // end capturing cout and cerr
202 capture_end (&aFile_err, STDERR_FILENO, fd_err_save, di.Log(), doEcho);
203 capture_end (&aFile_out, STDOUT_FILENO, fd_out_save, di.Log(), doEcho);
206 // log command result
207 const char* aResultStr = NULL;
210 aResultStr = Tcl_GetStringResult (interp);
211 if (aResultStr != 0 && aResultStr[0] != '\0' )
212 di.Log() << Tcl_GetStringResult (interp) << endl;
216 if (aResultStr == NULL)
217 aResultStr = Tcl_GetStringResult (interp);
218 if (aResultStr != 0 && aResultStr[0] != '\0' )
219 cout << Tcl_GetStringResult (interp) << endl;
225 static void CommandDelete (ClientData theClientData)
227 Draw_Interpretor::CallBackData* aCallback = (Draw_Interpretor::CallBackData* )theClientData;
231 //=======================================================================
232 //function : Draw_Interpretor
234 //=======================================================================
236 Draw_Interpretor::Draw_Interpretor() :
237 isAllocated(Standard_False), myDoLog(Standard_False), myDoEcho(Standard_False)
239 // The tcl interpreter is not created immediately as it is kept
240 // by a global variable and created and deleted before the main().
244 //=======================================================================
246 //purpose : It is necessary to call this function
247 //=======================================================================
249 void Draw_Interpretor::Init()
252 Tcl_DeleteInterp(myInterp);
253 isAllocated=Standard_True;
254 myInterp=Tcl_CreateInterp();
257 //=======================================================================
258 //function : Draw_Interpretor
260 //=======================================================================
262 Draw_Interpretor::Draw_Interpretor(const Draw_PInterp& p) :
263 isAllocated(Standard_False),
265 myDoLog(Standard_False),
266 myDoEcho(Standard_False)
270 //=======================================================================
273 //=======================================================================
274 void Draw_Interpretor::add (const Standard_CString theCommandName,
275 const Standard_CString theHelp,
276 const Standard_CString theFileName,
277 Draw_Interpretor::CallBackData* theCallback,
278 const Standard_CString theGroup)
280 if (myInterp == NULL)
285 Standard_PCharacter aName = (Standard_PCharacter )theCommandName;
286 Standard_PCharacter aHelp = (Standard_PCharacter )theHelp;
287 Standard_PCharacter aGroup = (Standard_PCharacter )theGroup;
288 Tcl_CreateCommand (myInterp, aName, CommandCmd, (ClientData )theCallback, CommandDelete);
291 Tcl_SetVar2 (myInterp, "Draw_Helps", aName, aHelp, TCL_GLOBAL_ONLY);
292 Tcl_SetVar2 (myInterp, "Draw_Groups", aGroup, aName,
293 TCL_GLOBAL_ONLY | TCL_APPEND_VALUE | TCL_LIST_ELEMENT);
295 // add path to source file (keep not more than two last subdirectories)
296 if (theFileName == NULL
297 || *theFileName == '\0')
302 OSD_Path aPath (theFileName);
303 Standard_Integer nbTrek = aPath.TrekLength();
304 for (Standard_Integer i = 2; i < nbTrek; ++i)
306 aPath.RemoveATrek (1);
310 TCollection_AsciiString aSrcPath;
311 aPath.SystemName (aSrcPath);
312 Tcl_SetVar2 (myInterp, "Draw_Files", aName, aSrcPath.ToCString(), TCL_GLOBAL_ONLY);
315 //=======================================================================
318 //=======================================================================
320 Standard_Boolean Draw_Interpretor::Remove(Standard_CString const n)
322 Standard_PCharacter pN;
324 pN=(Standard_PCharacter)n;
326 Standard_Integer result = Tcl_DeleteCommand(myInterp,pN);
330 //=======================================================================
333 //=======================================================================
335 Standard_CString Draw_Interpretor::Result() const
337 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
338 return Tcl_GetStringResult(myInterp);
340 return myInterp->result;
344 //=======================================================================
347 //=======================================================================
349 void Draw_Interpretor::Reset()
351 Tcl_ResetResult(myInterp);
354 //=======================================================================
357 //=======================================================================
359 Draw_Interpretor& Draw_Interpretor::Append(const Standard_CString s)
362 // Convert string to UTF-8 format for Tcl
363 Tcl_DString TclString;
364 Tcl_ExternalToUtfDString ( NULL, s, -1, &TclString );
365 Tcl_AppendResult ( myInterp, Tcl_DStringValue ( &TclString ), (Standard_CString)0 );
366 Tcl_DStringFree ( &TclString );
368 Tcl_AppendResult(myInterp,s,(Standard_CString)0);
373 //=======================================================================
376 //=======================================================================
378 Draw_Interpretor& Draw_Interpretor::Append(const TCollection_AsciiString& s)
380 return Append (s.ToCString());
383 //=======================================================================
386 //=======================================================================
388 Draw_Interpretor& Draw_Interpretor::Append(const TCollection_ExtendedString& theString)
391 // Convert string to UTF-8 format for Tcl
392 char *str = new char[theString.LengthOfCString()+1];
393 theString.ToUTF8CString (str);
394 Tcl_AppendResult ( myInterp, str, (Standard_CString)0 );
397 // put as ascii string, replacing non-ascii characters by '?'
398 TCollection_AsciiString str (theString, '?');
399 Tcl_AppendResult(myInterp,str.ToCString(),(Standard_CString)0);
404 //=======================================================================
407 //=======================================================================
409 Draw_Interpretor& Draw_Interpretor::Append(const Standard_Integer i)
413 Tcl_AppendResult(myInterp,c,(Standard_CString)0);
417 //=======================================================================
420 //=======================================================================
422 Draw_Interpretor& Draw_Interpretor::Append(const Standard_Real r)
425 Sprintf(s,"%.17g",r);
426 Tcl_AppendResult(myInterp,s,(Standard_CString)0);
430 //=======================================================================
433 //=======================================================================
435 Draw_Interpretor& Draw_Interpretor::Append(const Standard_SStream& s)
437 return Append (s.str().c_str());
440 //=======================================================================
441 //function : AppendElement
443 //=======================================================================
445 void Draw_Interpretor::AppendElement(const Standard_CString s)
448 // Convert string to UTF-8 format for Tcl
449 Tcl_DString TclString;
450 Tcl_ExternalToUtfDString ( NULL, s, -1, &TclString );
451 Tcl_AppendElement ( myInterp, Tcl_DStringValue ( &TclString ) );
452 Tcl_DStringFree ( &TclString );
455 //AppendElement is declared as (Tcl_Interp *interp, char *string)
457 Tcl_AppendElement(myInterp,(char*) s);
459 Tcl_AppendElement(myInterp, s);
464 //=======================================================================
467 //=======================================================================
469 Standard_Integer Draw_Interpretor::Eval(const Standard_CString line)
471 Standard_PCharacter pLine;
473 pLine=(Standard_PCharacter)line;
475 return Tcl_Eval(myInterp,pLine);
479 //=======================================================================
482 //=======================================================================
484 Standard_Integer Draw_Interpretor::RecordAndEval(const Standard_CString line,
485 const Standard_Integer flags)
487 Standard_PCharacter pLine;
489 pLine=(Standard_PCharacter)line;
490 return Tcl_RecordAndEval(myInterp,pLine,flags);
493 //=======================================================================
494 //function : EvalFile
496 //=======================================================================
498 Standard_Integer Draw_Interpretor::EvalFile(const Standard_CString fname)
500 Standard_PCharacter pfname;
502 pfname=(Standard_PCharacter)fname;
503 return Tcl_EvalFile(myInterp,pfname);
506 //=======================================================================
507 //function : PrintHelp
509 //=======================================================================
511 Standard_Integer Draw_Interpretor::PrintHelp (const Standard_CString theCommandName)
513 TCollection_AsciiString aCmd = TCollection_AsciiString ("help ") + theCommandName;
514 Standard_PCharacter aLinePtr = (Standard_PCharacter )aCmd.ToCString();
515 return Tcl_Eval (myInterp, aLinePtr);
518 //=======================================================================
521 //=======================================================================
523 Standard_Boolean Draw_Interpretor::Complete(const Standard_CString line)
525 Standard_PCharacter pLine;
527 pLine=(Standard_PCharacter)line;
528 return Tcl_CommandComplete(pLine);
531 //=======================================================================
534 //=======================================================================
536 Draw_Interpretor::~Draw_Interpretor()
539 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)))
544 catch (Standard_Failure) {
546 cout <<"Tcl_Exit have an exeption" << endl;
556 //=======================================================================
559 //=======================================================================
561 Draw_PInterp Draw_Interpretor::Interp() const
563 Standard_DomainError_Raise_if (myInterp==NULL , "No call for Draw_Interpretor::Init()");
567 void Draw_Interpretor::Set(const Draw_PInterp& PIntrp)
570 Tcl_DeleteInterp(myInterp);
571 isAllocated = Standard_False;
575 //=======================================================================
578 //=======================================================================
580 void Draw_Interpretor::SetDoLog (Standard_Boolean doLog)
585 void Draw_Interpretor::SetDoEcho (Standard_Boolean doEcho)
590 Standard_Boolean Draw_Interpretor::GetDoLog () const
595 Standard_Boolean Draw_Interpretor::GetDoEcho () const
600 Standard_SStream& Draw_Interpretor::Log ()