2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006-2008 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2013 - Scilab Enterprises - Antoine ELIAS
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
19 #pragma comment(lib,"../../../../../bin/libintl.lib")
30 #include "initMacOSXEnv.h"
32 #include "InitScilab.h"
33 #include "charEncoding.h"
34 #include "configvariable_interface.h"
35 #include "scilabRead.h"
36 #include "ConsoleRead.h"
38 #include "sci_malloc.h"
39 #include "lasterror.h"
40 #include "getpipeline.h"
42 extern char *getCmdLine(void);
45 #include "FilesAssociations.h"
48 #include "WindowShow.h"
50 #include "cliDisplayManagement.h"
51 extern jmp_buf ScilabJmpEnv;
56 #include "configvariable.hxx"
57 #include "exit_status.hxx"
58 #include "scilabWrite.hxx"
60 #define INTERACTIVE -1
72 ** Display usage : options available
74 static void usage(void)
76 std::cerr << "Usage: Scilab <options>" << std::endl;
77 std::cerr << " -e Instruction : execute the scilab instruction given in Instruction argument." << std::endl;
78 std::cerr << " -e and -f arguments are mutually exclusive." << std::endl;
79 std::cerr << " -f File : execute the scilab script given in File argument." << std::endl;
80 std::cerr << " -e and -f arguments are mutually exclusive." << std::endl;
81 std::cerr << " -quit : force scilab exit after execution of script from -e or -f argument." << std::endl;
82 std::cerr << " this flag is ignored if it is not used with -e or -f argument." << std::endl;
83 std::cerr << " -l lang : Change the language of scilab ( default : en_US )." << std::endl;
84 std::cerr << " -nw : Enable console mode." << std::endl;
85 std::cerr << " -nwni : Enable terminal mode." << std::endl;
86 std::cerr << " -ns : Don't execute etc/scilab.start." << std::endl;
87 std::cerr << " --help : Display this help." << std::endl;
88 std::cerr << " -scihome <dir> : Force SCIHOME to <dir>." << std::endl;
89 std::cerr << "Developer Trace arguments:" << std::endl;
90 std::cerr << " --parse-trace : Display bison state machine evolution." << std::endl;
91 std::cerr << " --AST-trace : Display ASCII-art AST to be human readable." << std::endl;
92 std::cerr << " --pretty-print : Display pretty-printed code, standard Scilab syntax." << std::endl;
93 std::cerr << " " << std::endl;
94 std::cerr << "Developer Timer arguments:" << std::endl;
95 std::cerr << " --AST-timed : Time each AST node." << std::endl;
96 std::cerr << " --timed : Time global execution." << std::endl;
97 std::cerr << " " << std::endl;
98 std::cerr << "Developer Debug arguments:" << std::endl;
99 std::cerr << " --no-exec : Only do Lexing/parsing do not execute instructions." << std::endl;
100 std::cerr << " --context-dump : Display context status." << std::endl;
101 std::cerr << " --exec-verbose : Display command before running it." << std::endl;
102 std::cerr << " --timeout delay : Kill the Scilab process after a delay." << std::endl;
109 static int get_option(const int argc, char *argv[], ScilabEngineInfo* _pSEI)
114 std::cerr << "-*- Getting Options -*-" << std::endl;
117 for (i = 1; i < argc; ++i)
119 if (!strcmp("--parse-trace", argv[i]))
121 _pSEI->iParseTrace = 1;
123 else if (!strcmp("--pretty-print", argv[i]))
125 _pSEI->iPrintAst = 1;
127 else if (!strcmp("--help", argv[i]))
132 else if (!strcmp("--AST-trace", argv[i]))
136 else if (!strcmp("--no-exec", argv[i]))
140 else if (!strcmp("--context-dump", argv[i]))
142 _pSEI->iDumpStack = 1;
144 else if (!strcmp("--timed", argv[i]))
147 ConfigVariable::setTimed(true);
149 else if (!strcmp("--serialize", argv[i]))
151 _pSEI->iSerialize = 1;
152 ConfigVariable::setSerialize(true);
154 else if (!strcmp("--AST-timed", argv[i]))
156 std::cout << "Timed execution" << std::endl;
157 _pSEI->iAstTimed = 1;
159 else if (!strcmp("--parse-file", argv[i]))
164 _pSEI->pstParseFile = argv[i];
167 else if (!strcmp("-quit", argv[i]))
169 _pSEI->iForceQuit = 1;
171 else if (!strcmp("-version", argv[i]))
176 _pSEI->iShowVersion = 1;
179 else if (!strcmp("-f", argv[i]))
184 _pSEI->pstFile = argv[i];
187 else if (!strcmp("-e", argv[i]))
192 _pSEI->pstExec = argv[i];
195 else if (!strcmp("-O", argv[i]))
200 _pSEI->pstExec = argv[i];
201 _pSEI->iCodeAction = 0;
204 else if (!strcmp("-X", argv[i]))
209 _pSEI->pstExec = argv[i];
210 _pSEI->iCodeAction = 1;
213 else if (!strcmp("-P", argv[i]))
218 _pSEI->pstExec = argv[i];
219 _pSEI->iCodeAction = 2;
222 else if (!strcmp("-l", argv[i]))
227 _pSEI->pstLang = argv[i];
230 else if (!strcmp("-nw", argv[i]))
232 _pSEI->iConsoleMode = 1;
234 else if (!strcmp("-nwni", argv[i]))
236 _pSEI->iConsoleMode = 1;
239 else if (!strcmp("-ns", argv[i]))
243 else if (!strcmp("-nb", argv[i]))
245 _pSEI->iNoBanner = 0;
247 else if (!strcmp("--exec-verbose", argv[i]))
249 _pSEI->iExecVerbose = 1;
251 else if (!strcmp("-nocolor", argv[i]))
257 else if (!strcmp("--timeout", argv[i]))
262 char* timeout = argv[i];
264 char* str_end = NULL;
265 int iTimeoutDelay = strtol(timeout, &str_end, 0);
280 case '\0': // no modifiers
284 std::cerr << "Invalid timeout delay unit: s (for seconds, default), m (for minutes), h (for hours), d (for days) are supported" << std::endl;
289 _pSEI->iTimeoutDelay = iTimeoutDelay * modifier;
293 std::cerr << "Unspecified timeout delay" << std::endl;
298 else if (!strcmp("-keepconsole", argv[i]))
300 _pSEI->iKeepConsole = 1;
302 else if (!strcmp("--webmode", argv[i]))
306 else if (!strcmp("-scihome", argv[i]))
311 _pSEI->pstSciHome = argv[i];
316 ConfigVariable::setCommandLineArgs(argc, argv);
327 extern char *getCmdLine(void);
328 extern void ConsolePrintf(const char *);
331 static void TermPrintf(const char *text)
340 #if defined(_WIN32) && !defined(WITHOUT_GUI) && defined(WITH_CONSOLE_JAVA)
341 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
343 LPWSTR *szArglist = NULL;
345 szArglist = CommandLineToArgvW(GetCommandLineW(), &argc);
346 char** argv = new char*[argc];
347 for (int i = 0; i < argc; ++i)
349 argv[i] = wide_string_to_UTF8(szArglist[i]);
352 setWindowShowMode(iCmdShow);
355 int main(int argc, char *argv[])
364 ScilabEngineInfo* pSEI = InitScilabEngineInfo();
366 /* Building Scilab-cli-bin. We won't ever had the gui nor the jvm */
367 pSEI->iConsoleMode = 1;
369 setScilabMode(SCILAB_NWNI);
371 setScilabMode(SCILAB_STD);
374 //#if defined(_WIN32) && !defined(WITHOUT_GUI)
376 // LPSTR my_argv[256];
378 // LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
381 // for (int i = 0; i < nArgs; i++)
383 // my_argv[i] = wide_string_to_UTF8(szArglist[i]);
385 // LocalFree(szArglist);
387 // get_option(nArgs, my_argv, pSEI);
390 get_option(argc, argv, pSEI);
393 // if WITHOUT_GUI is defined
394 // force Terminal IO -> Terminal IO + StartScilabEngine
396 // WITHOUT_GUI (All Platform) => Terminal IO + StartScilabEngine
397 // GUI (MacOSX) => [no option] -> Console IO + InitMacOSXEnv
398 // | [-nwni] -> Terminal IO + StartScilabEngine
399 // | [-nw] -> Terminal IO + InitMacOSXEnv
402 #ifndef WITH_CONSOLE_JAVA
403 pSEI->iConsoleMode = SCILAB_NW;
406 if (pSEI->iConsoleMode)
408 setScilabMode(SCILAB_NW);
409 setScilabInputMethod(&getCmdLine);
410 setScilabOutputMethod(&TermPrintf);
411 #if defined(__APPLE__)
412 if (pSEI->iNoJvm == 0)
414 return initMacOSXEnv(pSEI);
416 #endif // !defined(__APPLE__)
420 setScilabMode(SCILAB_STD);
421 setScilabInputMethod(&ConsoleRead);
422 setScilabOutputMethod(&ConsolePrintf);
425 if (pSEI->iCodeAction != -1)
427 //manage calls from explorer ( double click on sce file , ... )
428 char* Cmd = (char*)MALLOC(((PATH_MAX * 2) + 1) * sizeof(char));
430 int ret = CommandByFileExtension(pSEI->pstExec, pSEI->iCodeAction, Cmd);
432 if (ret && Cmd[0] != '\0')
434 pSEI->pstExec = Cmd; //Cmd must be freed in StartScilabEngine after process.
438 pSEI->iCodeAction = -1;
443 #if defined(__APPLE__)
444 return initMacOSXEnv(pSEI);
445 #endif // !defined(__APPLE__)
448 setScilabMode(SCILAB_NWNI);
449 setScilabInputMethod(&getCmdLine);
450 setScilabOutputMethod(&TermPrintf);
451 #endif // defined(WITHOUT_GUI)
454 /* if file descriptor returned is -2 stdin is not associated with an input stream */
455 /* example : echo plot3d | scilex -nw -e */
457 if (!isatty(_fileno(stdin)) && (_fileno(stdin) != -2) && getScilabMode() != SCILAB_STD)
459 if (!isatty(fileno(stdin)))
463 setScilabInputMethod(&getPipeLine);
466 if (pSEI->iShowVersion == 1)
468 disp_scilab_version();
472 int val = setjmp(ScilabJmpEnv);
475 iRet = StartScilabEngine(pSEI);
478 iRet = RunScilabEngine(pSEI);
481 StopScilabEngine(pSEI);
487 // We probably had a segfault so print error
488 std::wcerr << getLastErrorMessage() << std::endl;
492 #if defined(_WIN32) && !defined(WITHOUT_GUI) && defined(WITH_CONSOLE_JAVA)
493 LocalFree(szArglist);