~ubuntu-branches/ubuntu/karmic/scilab/karmic

« back to all changes in this revision

Viewing changes to routines/wsci/command.c

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2002-03-21 16:57:43 UTC
  • Revision ID: james.westby@ubuntu.com-20020321165743-e9mv12c1tb1plztg
Tags: upstream-2.6
ImportĀ upstreamĀ versionĀ 2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * command.c : 
 
3
 * (1997) : Jean-Philippe Chancelier 
 
4
 * 
 
5
 */
 
6
 
 
7
#include <stdio.h>
 
8
#include <math.h>
 
9
#include <ctype.h>
 
10
#ifndef STRICT
 
11
#define STRICT
 
12
#endif
 
13
#include <windows.h>
 
14
#include <setjmp.h>
 
15
#include <string.h>
 
16
#include <stdlib.h>
 
17
#include <stdarg.h>
 
18
 
 
19
#if !(defined __CYGWIN32__) && !(defined __ABSC__)
 
20
#include <direct.h>             /* for _chdrive() */
 
21
#endif
 
22
 
 
23
#include <time.h>               /* kludge to provide sleep() */
 
24
 
 
25
#include "wgnuplib.h"
 
26
#include "wcommon.h"
 
27
#include "plot.h"
 
28
 
 
29
extern GW graphwin;             /* graphic window */
 
30
extern TW textwin;
 
31
extern jmp_buf env;             /* from plot.c */
 
32
 
 
33
 
 
34
static char *rlgets (char *s, int n, char *prompt);
 
35
static char *rlgets_nw (char *s, int n, char *prompt);
 
36
 
 
37
/* input data, parsing variables */
 
38
 
 
39
char input_line[MAX_LINE_LEN + 1] = "";
 
40
int inline_num = 0;             /* input line number */
 
41
 
 
42
/**********************************************
 
43
 * Support for input, shell,printer for win32 
 
44
 **********************************************/
 
45
 
 
46
/**********************************************
 
47
 * Used in read_line 
 
48
 * version with readline and a textwindow 
 
49
 **********************************************/
 
50
 
 
51
static char *
 
52
rlgets (char *s, int n, char *prompt)
 
53
{
 
54
  static char *line = (char *) NULL;
 
55
 
 
56
  /* If we already have a line, first free it */
 
57
  if (line != (char *) NULL)
 
58
    free (line);
 
59
 
 
60
  line = readline_win (prompt);
 
61
 
 
62
  /* If it's not an EOF */
 
63
  if (line)
 
64
    {
 
65
      if (*line)
 
66
        add_history_win (line);
 
67
      strncpy (s, line, n);
 
68
      return s;
 
69
    }
 
70
  return line;
 
71
}
 
72
 
 
73
/**********************************************
 
74
 * Used in read_line 
 
75
 * version with readline and no textwindow scilab -nw *
 
76
 **********************************************/
 
77
 
 
78
static char *
 
79
rlgets_nw (char *s, int n, char *prompt)
 
80
{
 
81
  static char *line = (char *) NULL;
 
82
 
 
83
  /* If we already have a line, first free it */
 
84
  if (line != (char *) NULL)
 
85
    free (line);
 
86
  line = readline_nw (prompt);
 
87
  /* If it's not an EOF */
 
88
  if (line)
 
89
    {
 
90
      /* -1 is added for eos ( end of input when using pipes ) */
 
91
      if (*line && *line != -1)
 
92
        add_history_nw (line);
 
93
      strncpy (s, line, n);
 
94
      return s;
 
95
    }
 
96
  return line;
 
97
}
 
98
 
 
99
typedef char *(*RLFUNC) (char *, int, char *);
 
100
RLFUNC rlgets_def = rlgets_nw;
 
101
 
 
102
void 
 
103
switch_rlgets (int i)
 
104
{
 
105
  if (i == 1)
 
106
    rlgets_def = rlgets;
 
107
  else
 
108
    rlgets_def = rlgets_nw;
 
109
}
 
110
 
 
111
/**********************************************
 
112
 * reads a scilab line with rlgets or rlgets_nw 
 
113
 * according to current value of rlgets_def 
 
114
 **********************************************/
 
115
 
 
116
int 
 
117
read_line (char *prompt)
 
118
{
 
119
 
 
120
  int start = 0, ilen = 0;
 
121
  int last;
 
122
  char *p;
 
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)
 
128
    *p = 0;
 
129
  if ((p = strchr (&(input_line[start + 2]), '\n')) != NULL)
 
130
    *p = 0;
 
131
  input_line[start + 1] = strlen (&(input_line[start + 2]));
 
132
 
 
133
  if (input_line[start + 2] == 26 || input_line[start + 2] == -1)
 
134
    {
 
135
      /* XXX 26 XXXX */
 
136
      /* end-of-file or end reached in pipe */
 
137
      input_line[start] = '\0';
 
138
      inline_num++;
 
139
      if (start <= 0)           /* don't quit yet - process what we have */
 
140
        {
 
141
          sciprint ("\n");
 
142
          return (1);           /* exit scilab */
 
143
        }
 
144
    }
 
145
  else
 
146
    {
 
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 */
 
151
 
 
152
      inline_num++;
 
153
      last = strlen (input_line) - 1;
 
154
      if (last < 0)
 
155
        last = 0;               /* stop UAE in Windows */
 
156
      if (last + 1 >= MAX_LINE_LEN)
 
157
        int_error ("Input line too long", NO_CARET);
 
158
    }
 
159
  return (0);
 
160
}
 
161
 
 
162
/**********************************************
 
163
 * a shell function 
 
164
 * XXXX should use /bin/sh if we can find it 
 
165
 **********************************************/
 
166
 
 
167
void 
 
168
do_shell ()
 
169
{
 
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);
 
175
}
 
176
 
 
177
/**********************************************
 
178
 * A system function  (Windows style )
 
179
 * see also the system function.
 
180
 **********************************************/
 
181
 
 
182
void 
 
183
do_system ()
 
184
{
 
185
  if (winsystem (input_line + 1, 0))
 
186
    os_error ("system() failed", NO_CARET);
 
187
}
 
188
 
 
189
 
 
190
void 
 
191
do_system1 ()
 
192
{
 
193
  sciprint ("%d \r\n", system (input_line + 1));
 
194
}
 
195
 
 
196
 
 
197
 
 
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 
 
200
   commands */
 
201
 
 
202
/** #define WINEXECDEBUG **/
 
203
 
 
204
int 
 
205
winsystem (char *s, int flag)
 
206
{
 
207
  LPSTR comspec, execstr, p;
 
208
  int sw_sci_flag;
 
209
  /* get COMSPEC environment variable */
 
210
  char envbuf[81];
 
211
  if (flag == 1)
 
212
    sw_sci_flag = SW_SHOWNORMAL;
 
213
  else
 
214
    sw_sci_flag = SW_HIDE;
 
215
 
 
216
  GetEnvironmentVariable ("COMSPEC", envbuf, 80);
 
217
  if (*envbuf == '\0')
 
218
    comspec = "\\command.com";
 
219
  else
 
220
    comspec = envbuf;
 
221
  /* if the command is blank we must use command.com */
 
222
  p = s;
 
223
  while ((*p == ' ') || (*p == '\n') || (*p == '\r'))
 
224
    p++;
 
225
  if (*p == '\0')
 
226
    {
 
227
#ifdef WINEXECDEBUG
 
228
      sciprint ("TestMessage0 : WinExec de %s\r\n", comspec);
 
229
#endif
 
230
      WinExec (comspec, SW_SHOWNORMAL);
 
231
    }
 
232
  else
 
233
    {
 
234
      /* attempt to run the windows/dos program via windows */
 
235
      int res;
 
236
      execstr = (char *) malloc (strlen (s) + strlen (comspec) + 6);
 
237
      if (execstr == NULL)
 
238
        {
 
239
          sciprint ("Running out of memory\r\n");
 
240
          return 1;
 
241
        }
 
242
      if (s[0] == '/' && s[1] == '/' && s[3] == '/')
 
243
        {
 
244
          sprintf (execstr, "%c:%s", s[2], s + 3);
 
245
#ifdef WINEXECDEBUG
 
246
          sciprint ("TestMessage1 : WinExec de %s\r\n", execstr);
 
247
#endif
 
248
          res = WinExec (execstr, sw_sci_flag);
 
249
        }
 
250
      else
 
251
        {
 
252
          sprintf (execstr, "%s", s);
 
253
#ifdef WINEXECDEBUG
 
254
          sciprint ("TestMessage2 : WinExec de %s\r\n", execstr);
 
255
#endif
 
256
          res = WinExec (execstr, sw_sci_flag);
 
257
        }
 
258
      if (res <= 31)
 
259
        {
 
260
          /* attempt to run it as a dos program from command line */
 
261
          if (s[0] == '/' && s[1] == '/' && s[3] == '/')
 
262
            {
 
263
              sprintf (execstr, "%s /c %c:%s", comspec, s[2], s + 3);
 
264
            }
 
265
          else
 
266
            {
 
267
              sprintf (execstr, "%s /c %s", comspec, s);
 
268
            }
 
269
#ifdef WINEXECDEBUG
 
270
          sciprint ("TestMessage3 : WinExec de %s\r\n", execstr);
 
271
#endif
 
272
          res = WinExec (execstr, sw_sci_flag);
 
273
          if (res <= 31)
 
274
            sciprint ("WinExec of %s failed \r\n", execstr);
 
275
        }
 
276
      free (execstr);
 
277
    }
 
278
  /* regardless of the reality return OK - the consequences of */
 
279
  /* failure include shutting down Windows */
 
280
  return (0);                   /* success */
 
281
}
 
282
 
 
283
/** pas franchement utile **/
 
284
 
 
285
int 
 
286
MyWinExec (char *str, int sw_sci_flag)
 
287
{
 
288
  STARTUPINFO start;
 
289
  SECURITY_ATTRIBUTES sec_attrs;
 
290
  PROCESS_INFORMATION child;
 
291
  int wait_for_child = TRUE;
 
292
  DWORD ret_code = 0;
 
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))
 
303
    {
 
304
      if (wait_for_child)
 
305
        {
 
306
          WaitForSingleObject (child.hProcess, INFINITE);
 
307
          GetExitCodeProcess (child.hProcess, &ret_code);
 
308
        }
 
309
      CloseHandle (child.hThread);
 
310
      CloseHandle (child.hProcess);
 
311
    }
 
312
  else
 
313
    goto error;
 
314
  if ((int) ret_code == 0)
 
315
    return 32;
 
316
  else
 
317
    return 0;
 
318
error:
 
319
  sciprint (" Could not exec %s\r\n", str);
 
320
  return 32;
 
321
}