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 << " -args arg1 arg2..: Send directly some arg1 arg2 .. data to the session." << std::endl;
78 std::cerr << " arg1 arg2 .. values can then be read through sciargs()." << std::endl;
79 std::cerr << " -e Instruction : Execute the scilab instruction given in Instruction argument." << std::endl;
80 std::cerr << " -e and -f arguments are mutually exclusive." << std::endl;
81 std::cerr << " -f File : Execute the scilab script given in File argument." << std::endl;
82 std::cerr << " -e and -f arguments are mutually exclusive." << std::endl;
83 std::cerr << " -quit : Force scilab exit after execution of script from -e or -f argument." << std::endl;
84 std::cerr << " Flag ignored if it is not used with -e or -f argument." << std::endl;
85 std::cerr << " -l lang : Change the language of scilab (default : en_US)." << std::endl;
86 std::cerr << " -nw : Enable console mode." << std::endl;
87 std::cerr << " -nwni : Enable terminal mode." << std::endl;
88 std::cerr << " -ns : Do not execute etc/scilab.start." << std::endl;
89 std::cerr << " -nouserstartup : Do not execute user startup files (SCIHOME/scilab.ini|.scilab)." << std::endl;
90 std::cerr << " -noatomsautoload : Do not autoload eligible ATOMS modules." << std::endl;
91 std::cerr << " -nb : Do not display the banner (no banner)." << std::endl;
92 std::cerr << " -scihome <dir> : Force SCIHOME to <dir>." << std::endl;
93 std::cerr << " -texmacs : Option to use Scilab from documents through http://texmacs.org." << std::endl;
94 std::cerr << " -version : Display the Scilab version (no session opened)." << std::endl;
95 std::cerr << " -display Display : Set the display to the X server Display (XWindow systems only)." << std::endl;
96 std::cerr << " unix:0.0 is the default." << std::endl;
97 std::cerr << " --help : Display this help." << std::endl;
98 std::cerr << " " << std::endl;
99 std::cerr << "Developer Trace arguments:" << std::endl;
100 std::cerr << " --parse-trace : Display bison state machine evolution." << std::endl;
101 std::cerr << " --AST-trace : Display ASCII-art AST to be human readable." << std::endl;
102 std::cerr << " --pretty-print : Display pretty-printed code, standard Scilab syntax." << std::endl;
103 std::cerr << " " << std::endl;
104 std::cerr << "Developer Timer arguments:" << std::endl;
105 std::cerr << " --AST-timed : Time each AST node." << std::endl;
106 std::cerr << " --timed : Time global execution." << std::endl;
107 std::cerr << " " << std::endl;
108 std::cerr << "Developer Debug arguments:" << std::endl;
109 std::cerr << " --no-exec : Only do Lexing/parsing do not execute instructions." << std::endl;
110 std::cerr << " --context-dump : Display context status." << std::endl;
111 std::cerr << " --exec-verbose : Display command before running it." << std::endl;
112 std::cerr << " --timeout delay : Kill the Scilab process after a delay." << std::endl;
119 static int get_option(const int argc, char *argv[], ScilabEngineInfo* _pSEI)
124 std::cerr << "-*- Getting Options -*-" << std::endl;
127 for (i = 1; i < argc; ++i)
129 if (!strcmp("--parse-trace", argv[i]))
131 _pSEI->iParseTrace = 1;
133 else if (!strcmp("--pretty-print", argv[i]))
135 _pSEI->iPrintAst = 1;
137 else if (!strcmp("--help", argv[i]))
142 else if (!strcmp("--AST-trace", argv[i]))
146 else if (!strcmp("--no-exec", argv[i]))
150 else if (!strcmp("--context-dump", argv[i]))
152 _pSEI->iDumpStack = 1;
154 else if (!strcmp("--timed", argv[i]))
157 ConfigVariable::setTimed(true);
159 else if (!strcmp("--serialize", argv[i]))
161 _pSEI->iSerialize = 1;
162 ConfigVariable::setSerialize(true);
164 else if (!strcmp("--AST-timed", argv[i]))
166 std::cout << "Timed execution" << std::endl;
167 _pSEI->iAstTimed = 1;
169 else if (!strcmp("--parse-file", argv[i]))
174 _pSEI->pstParseFile = argv[i];
177 else if (!strcmp("-quit", argv[i]))
179 _pSEI->iForceQuit = 1;
181 else if (!strcmp("-version", argv[i]))
186 _pSEI->iShowVersion = 1;
189 else if (!strcmp("-f", argv[i]))
194 _pSEI->pstFile = argv[i];
197 else if (!strcmp("-e", argv[i]))
202 _pSEI->pstExec = argv[i];
205 else if (!strcmp("-O", argv[i]))
210 _pSEI->pstExec = argv[i];
211 _pSEI->iCodeAction = 0;
214 else if (!strcmp("-X", argv[i]))
219 _pSEI->pstExec = argv[i];
220 _pSEI->iCodeAction = 1;
223 else if (!strcmp("-P", argv[i]))
228 _pSEI->pstExec = argv[i];
229 _pSEI->iCodeAction = 2;
232 else if (!strcmp("-l", argv[i]))
237 _pSEI->pstLang = argv[i];
240 else if (!strcmp("-nw", argv[i]))
242 _pSEI->iConsoleMode = 1;
244 #if WITH_CONSOLE_JAVA
245 MessageBoxA(NULL, "Argument \"-nw\" is no longer supported.\nSee help page: https://help.scilab.org/scilab \n", "Error", MB_OK | MB_ICONERROR);
247 printf("Argument \"-nw\" is no longer supported.\nSee help page: https://help.scilab.org/scilab \n");
252 else if (!strcmp("-nwni", argv[i]))
254 _pSEI->iConsoleMode = 1;
257 #if WITH_CONSOLE_JAVA
258 MessageBoxA(NULL, "Argument \"-nwni\" is no longer supported.\nSee help page: https://help.scilab.org/scilab \n", "Error", MB_OK | MB_ICONERROR);
260 printf("Argument \"-nwni\" is no longer supported.\nSee help page: https://help.scilab.org/scilab \n");
265 else if (!strcmp("-ns", argv[i]))
269 else if (!strcmp("-nb", argv[i]))
271 _pSEI->iNoBanner = 0;
273 else if (!strcmp("--exec-verbose", argv[i]))
275 _pSEI->iExecVerbose = 1;
277 else if (!strcmp("-nocolor", argv[i]))
283 else if (!strcmp("--timeout", argv[i]))
288 char* timeout = argv[i];
290 char* str_end = NULL;
291 int iTimeoutDelay = strtol(timeout, &str_end, 0);
306 case '\0': // no modifiers
310 std::cerr << "Invalid timeout delay unit: s (for seconds, default), m (for minutes), h (for hours), d (for days) are supported" << std::endl;
315 _pSEI->iTimeoutDelay = iTimeoutDelay * modifier;
319 std::cerr << "Unspecified timeout delay" << std::endl;
324 else if (!strcmp("-keepconsole", argv[i]))
326 _pSEI->iKeepConsole = 1;
328 else if (!strcmp("--webmode", argv[i]))
332 else if (!strcmp("-scihome", argv[i]))
337 _pSEI->pstSciHome = argv[i];
342 ConfigVariable::setCommandLineArgs(argc, argv);
353 extern char *getCmdLine(void);
354 extern void ConsolePrintf(const char *);
357 static void TermError(const char *text)
359 fprintf(stderr, "%s", text);
362 static void TermOutput(const char *text)
364 fprintf(stdout, "%s", text);
370 #if defined(_WIN32) && !defined(WITHOUT_GUI) && defined(WITH_CONSOLE_JAVA)
371 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
373 LPWSTR *szArglist = NULL;
375 szArglist = CommandLineToArgvW(GetCommandLineW(), &argc);
376 char** argv = new char*[argc];
377 for (int i = 0; i < argc; ++i)
379 argv[i] = wide_string_to_UTF8(szArglist[i]);
382 setWindowShowMode(iCmdShow);
385 int main(int argc, char *argv[])
394 ScilabEngineInfo* pSEI = InitScilabEngineInfo();
396 /* Building Scilab-cli-bin. We won't ever had the gui nor the jvm */
397 pSEI->iConsoleMode = 1;
399 setScilabMode(SCILAB_NWNI);
401 setScilabMode(SCILAB_STD);
404 //#if defined(_WIN32) && !defined(WITHOUT_GUI)
406 // LPSTR my_argv[256];
408 // LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
411 // for (int i = 0; i < nArgs; i++)
413 // my_argv[i] = wide_string_to_UTF8(szArglist[i]);
415 // LocalFree(szArglist);
417 // get_option(nArgs, my_argv, pSEI);
420 get_option(argc, argv, pSEI);
423 // if WITHOUT_GUI is defined
424 // force Terminal IO -> Terminal IO + StartScilabEngine
426 // WITHOUT_GUI (All Platform) => Terminal IO + StartScilabEngine
427 // GUI (MacOSX) => [no option] -> Console IO + InitMacOSXEnv
428 // | [-nwni] -> Terminal IO + StartScilabEngine
429 // | [-nw] -> Terminal IO + InitMacOSXEnv
432 #ifndef WITH_CONSOLE_JAVA
433 pSEI->iConsoleMode = SCILAB_NW;
436 if (pSEI->iConsoleMode)
438 setScilabMode(SCILAB_NW);
439 setScilabInputMethod(&getCmdLine);
440 setScilabErrorStreamMethod(&TermError);
441 setScilabOutputStreamMethod(&TermOutput);
442 #if defined(__APPLE__)
443 if (pSEI->iNoJvm == 0)
445 return initMacOSXEnv(pSEI);
447 #endif // !defined(__APPLE__)
451 setScilabMode(SCILAB_STD);
452 setScilabInputMethod(&ConsoleRead);
453 setScilabOutputMethod(&ConsolePrintf);
456 if (pSEI->iCodeAction != -1)
458 //manage calls from explorer ( double click on sce file , ... )
459 char* Cmd = (char*)MALLOC(((PATH_MAX * 2) + 1) * sizeof(char));
461 int ret = CommandByFileExtension(pSEI->pstExec, pSEI->iCodeAction, Cmd);
463 if (ret && Cmd[0] != '\0')
465 pSEI->pstExec = Cmd; //Cmd must be freed in StartScilabEngine after process.
469 pSEI->iCodeAction = -1;
474 #if defined(__APPLE__)
475 return initMacOSXEnv(pSEI);
476 #endif // !defined(__APPLE__)
479 setScilabMode(SCILAB_NWNI);
480 setScilabInputMethod(&getCmdLine);
481 setScilabErrorStreamMethod(&TermError);
482 setScilabOutputStreamMethod(&TermOutput);
483 #endif // defined(WITHOUT_GUI)
486 /* if file descriptor returned is -2 stdin is not associated with an input stream */
487 /* example : echo plot3d | scilex -e */
489 if (!isatty(_fileno(stdin)) && (_fileno(stdin) != -2) && getScilabMode() != SCILAB_STD)
491 if (!isatty(fileno(stdin)) && getScilabMode() != SCILAB_STD)
495 setScilabInputMethod(&getPipeLine);
498 if (pSEI->iShowVersion == 1)
500 disp_scilab_version();
504 int val = setjmp(ScilabJmpEnv);
507 iRet = StartScilabEngine(pSEI);
510 iRet = RunScilabEngine(pSEI);
513 StopScilabEngine(pSEI);
519 // We probably had a segfault so print error
520 std::wcerr << getLastErrorMessage() << std::endl;
524 #if defined(_WIN32) && !defined(WITHOUT_GUI) && defined(WITH_CONSOLE_JAVA)
525 LocalFree(szArglist);