3
* (1997) : Jean-Philippe Chancelier
19
#if !(defined __CYGWIN32__) && !(defined __ABSC__)
20
#include <direct.h> /* for _chdrive() */
23
#include <time.h> /* kludge to provide sleep() */
29
extern GW graphwin; /* graphic window */
31
extern jmp_buf env; /* from plot.c */
34
static char *rlgets (char *s, int n, char *prompt);
35
static char *rlgets_nw (char *s, int n, char *prompt);
37
/* input data, parsing variables */
39
char input_line[MAX_LINE_LEN + 1] = "";
40
int inline_num = 0; /* input line number */
42
/**********************************************
43
* Support for input, shell,printer for win32
44
**********************************************/
46
/**********************************************
48
* version with readline and a textwindow
49
**********************************************/
52
rlgets (char *s, int n, char *prompt)
54
static char *line = (char *) NULL;
56
/* If we already have a line, first free it */
57
if (line != (char *) NULL)
60
line = readline_win (prompt);
62
/* If it's not an EOF */
66
add_history_win (line);
73
/**********************************************
75
* version with readline and no textwindow scilab -nw *
76
**********************************************/
79
rlgets_nw (char *s, int n, char *prompt)
81
static char *line = (char *) NULL;
83
/* If we already have a line, first free it */
84
if (line != (char *) NULL)
86
line = readline_nw (prompt);
87
/* If it's not an EOF */
90
/* -1 is added for eos ( end of input when using pipes ) */
91
if (*line && *line != -1)
92
add_history_nw (line);
99
typedef char *(*RLFUNC) (char *, int, char *);
100
RLFUNC rlgets_def = rlgets_nw;
103
switch_rlgets (int i)
108
rlgets_def = rlgets_nw;
111
/**********************************************
112
* reads a scilab line with rlgets or rlgets_nw
113
* according to current value of rlgets_def
114
**********************************************/
117
read_line (char *prompt)
120
int start = 0, ilen = 0;
123
ilen = MAX_LINE_LEN - start - 1;
124
input_line[start] = ilen > 126 ? 126 : ilen;
125
input_line[start + 2] = 0;
126
(void) (*rlgets_def) (&(input_line[start + 2]), ilen, prompt);
127
if ((p = strchr (&(input_line[start + 2]), '\r')) != NULL)
129
if ((p = strchr (&(input_line[start + 2]), '\n')) != NULL)
131
input_line[start + 1] = strlen (&(input_line[start + 2]));
133
if (input_line[start + 2] == 26 || input_line[start + 2] == -1)
136
/* end-of-file or end reached in pipe */
137
input_line[start] = '\0';
139
if (start <= 0) /* don't quit yet - process what we have */
142
return (1); /* exit scilab */
147
/* normal line input */
148
register int i = start;
149
while ((input_line[i] = input_line[i + 2]) != (char) NULL)
150
i++; /* yuck! move everything down two characters */
153
last = strlen (input_line) - 1;
155
last = 0; /* stop UAE in Windows */
156
if (last + 1 >= MAX_LINE_LEN)
157
int_error ("Input line too long", NO_CARET);
162
/**********************************************
164
* XXXX should use /bin/sh if we can find it
165
**********************************************/
170
register char *comspec;
171
if ((comspec = getenv ("COMSPEC")) == (char *) NULL)
172
comspec = "\\command.com";
173
if (WinExec (comspec, SW_SHOWNORMAL) <= 32)
174
os_error ("unable to spawn shell", NO_CARET);
177
/**********************************************
178
* A system function (Windows style )
179
* see also the system function.
180
**********************************************/
185
if (winsystem (input_line + 1, 0))
186
os_error ("system() failed", NO_CARET);
193
sciprint ("%d \r\n", system (input_line + 1));
198
/* there is a system like call on MS Windows but it is a bit difficult to
199
use, so we will invoke the command interpreter and use it to execute the
202
/** #define WINEXECDEBUG **/
205
winsystem (char *s, int flag)
207
LPSTR comspec, execstr, p;
209
/* get COMSPEC environment variable */
212
sw_sci_flag = SW_SHOWNORMAL;
214
sw_sci_flag = SW_HIDE;
216
GetEnvironmentVariable ("COMSPEC", envbuf, 80);
218
comspec = "\\command.com";
221
/* if the command is blank we must use command.com */
223
while ((*p == ' ') || (*p == '\n') || (*p == '\r'))
228
sciprint ("TestMessage0 : WinExec de %s\r\n", comspec);
230
WinExec (comspec, SW_SHOWNORMAL);
234
/* attempt to run the windows/dos program via windows */
236
execstr = (char *) malloc (strlen (s) + strlen (comspec) + 6);
239
sciprint ("Running out of memory\r\n");
242
if (s[0] == '/' && s[1] == '/' && s[3] == '/')
244
sprintf (execstr, "%c:%s", s[2], s + 3);
246
sciprint ("TestMessage1 : WinExec de %s\r\n", execstr);
248
res = WinExec (execstr, sw_sci_flag);
252
sprintf (execstr, "%s", s);
254
sciprint ("TestMessage2 : WinExec de %s\r\n", execstr);
256
res = WinExec (execstr, sw_sci_flag);
260
/* attempt to run it as a dos program from command line */
261
if (s[0] == '/' && s[1] == '/' && s[3] == '/')
263
sprintf (execstr, "%s /c %c:%s", comspec, s[2], s + 3);
267
sprintf (execstr, "%s /c %s", comspec, s);
270
sciprint ("TestMessage3 : WinExec de %s\r\n", execstr);
272
res = WinExec (execstr, sw_sci_flag);
274
sciprint ("WinExec of %s failed \r\n", execstr);
278
/* regardless of the reality return OK - the consequences of */
279
/* failure include shutting down Windows */
280
return (0); /* success */
283
/** pas franchement utile **/
286
MyWinExec (char *str, int sw_sci_flag)
289
SECURITY_ATTRIBUTES sec_attrs;
290
PROCESS_INFORMATION child;
291
int wait_for_child = TRUE;
293
memset (&start, 0, sizeof (start));
294
start.cb = sizeof (start);
295
start.dwFlags = STARTF_USESHOWWINDOW;
296
start.wShowWindow = SW_HIDE;
297
/** start.wShowWindow = SW_SHOWMINIMIZED; **/
298
sec_attrs.nLength = sizeof (sec_attrs);
299
sec_attrs.lpSecurityDescriptor = NULL;
300
sec_attrs.bInheritHandle = FALSE;
301
if (CreateProcess (NULL, str, &sec_attrs, NULL, TRUE, 0,
302
NULL, NULL, &start, &child))
306
WaitForSingleObject (child.hProcess, INFINITE);
307
GetExitCodeProcess (child.hProcess, &ret_code);
309
CloseHandle (child.hThread);
310
CloseHandle (child.hProcess);
314
if ((int) ret_code == 0)
319
sciprint (" Could not exec %s\r\n", str);