1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
661
|
23 /* |
|
24 |
|
25 The function gethostname was adapted from a similar function from GNU |
|
26 Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free |
|
27 Software Foundation, Inc. |
|
28 |
|
29 */ |
|
30 |
240
|
31 #ifdef HAVE_CONFIG_H |
1192
|
32 #include <config.h> |
1
|
33 #endif |
|
34 |
1346
|
35 #include <cfloat> |
1343
|
36 #include <cstddef> |
1346
|
37 #include <cstdio> |
1343
|
38 #include <cstdlib> |
|
39 #include <cstring> |
|
40 |
1728
|
41 #include <string> |
|
42 |
1742
|
43 #include <iostream.h> |
|
44 |
1350
|
45 #ifdef HAVE_UNISTD_H |
2442
|
46 #ifdef HAVE_SYS_TYPES_H |
529
|
47 #include <sys/types.h> |
2442
|
48 #endif |
529
|
49 #include <unistd.h> |
|
50 #endif |
1
|
51 |
1430
|
52 #if defined (HAVE_TERMIOS_H) |
|
53 #include <termios.h> |
|
54 #elif defined (HAVE_TERMIO_H) |
|
55 #include <termio.h> |
|
56 #elif defined (HAVE_SGTTY_H) |
|
57 #include <sgtty.h> |
|
58 #else |
|
59 LOSE! LOSE! |
|
60 #endif |
|
61 |
1463
|
62 #if defined (HAVE_FLOATINGPOINT_H) |
|
63 #include <floatingpoint.h> |
|
64 #endif |
|
65 |
2508
|
66 #if defined (HAVE_IEEEFP_H) |
|
67 #include <ieeefp.h> |
|
68 #endif |
|
69 |
1463
|
70 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
71 #include <sys/utsname.h> |
|
72 #endif |
|
73 |
2470
|
74 #if defined (USE_READLINE) |
1463
|
75 #include <readline/readline.h> |
1750
|
76 #include <readline/tilde.h> |
2470
|
77 #endif |
1463
|
78 |
|
79 extern char *term_clrpag; |
|
80 extern "C" void _rl_output_character_function (); |
|
81 |
2893
|
82 #include "lo-mappers.h" |
2317
|
83 #include "mach-info.h" |
1769
|
84 #include "oct-math.h" |
|
85 |
1352
|
86 #include "defun.h" |
|
87 #include "error.h" |
542
|
88 #include "help.h" |
529
|
89 #include "input.h" |
1755
|
90 #include "oct-obj.h" |
|
91 #include "pathlen.h" |
2370
|
92 #include "ov.h" |
1352
|
93 #include "sysdep.h" |
1755
|
94 #include "toplev.h" |
529
|
95 #include "utils.h" |
|
96 |
|
97 #ifndef STDIN_FILENO |
|
98 #define STDIN_FILENO 1 |
|
99 #endif |
444
|
100 |
2508
|
101 #if defined (__386BSD__) || defined (__FreeBSD__) |
|
102 static void |
|
103 BSD_init (void) |
|
104 { |
|
105 #if defined (HAVE_FLOATINGPOINT_H) |
|
106 // Disable trapping on common exceptions. |
|
107 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
108 #endif |
|
109 } |
|
110 #endif |
|
111 |
2610
|
112 #if defined (NeXT) |
1
|
113 extern "C" |
|
114 { |
|
115 typedef void (*_cplus_fcn_int) (int); |
|
116 extern void (*malloc_error (_cplus_fcn_int)) (int); |
|
117 } |
|
118 |
|
119 static void |
|
120 malloc_handler (int code) |
|
121 { |
|
122 if (code == 5) |
217
|
123 warning ("hopefully recoverable malloc error: freeing wild pointer"); |
1
|
124 else |
|
125 { |
|
126 panic ("probably irrecoverable malloc error: code %d", code); |
|
127 } |
|
128 } |
|
129 |
|
130 static void |
|
131 NeXT_init (void) |
|
132 { |
|
133 malloc_error (malloc_handler); |
|
134 } |
|
135 #endif |
|
136 |
2546
|
137 #if defined (__EMX__) |
|
138 OS2_init (void) |
|
139 { |
|
140 _control87 ((EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW |
|
141 | EM_UNDERFLOW | EM_INEXACT), MCW_EM); |
|
142 } |
|
143 #endif |
|
144 |
2508
|
145 #if defined (SCO) |
|
146 static void |
|
147 SCO_init (void) |
|
148 { |
|
149 #if defined (HAVE_IEEEFP_H) |
|
150 // Disable trapping on common exceptions. |
|
151 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
152 #endif |
|
153 } |
|
154 #endif |
|
155 |
505
|
156 #if defined (EXCEPTION_IN_MATH) |
|
157 extern "C" |
|
158 int |
|
159 matherr (struct exception *x) |
|
160 { |
1358
|
161 // Possibly print our own message someday. Should probably be |
|
162 // user-switchable. |
505
|
163 |
|
164 switch (x->type) |
|
165 { |
|
166 case DOMAIN: |
|
167 case SING: |
|
168 case OVERFLOW: |
|
169 case UNDERFLOW: |
|
170 case TLOSS: |
|
171 case PLOSS: |
|
172 default: |
|
173 break; |
|
174 } |
|
175 |
1358
|
176 // But don't print the system message. |
505
|
177 |
|
178 return 1; |
|
179 } |
1466
|
180 #endif |
505
|
181 |
1
|
182 void |
|
183 sysdep_init (void) |
|
184 { |
959
|
185 #if defined (__386BSD__) || defined (__FreeBSD__) |
2508
|
186 BSD_init (); |
2610
|
187 #elif defined (NeXT) |
1
|
188 NeXT_init (); |
2610
|
189 #elif defined (__EMX__) |
2546
|
190 OS2_init (); |
2508
|
191 #elif defined (SCO) |
|
192 SCO_init (); |
1
|
193 #endif |
444
|
194 |
|
195 octave_ieee_init (); |
1
|
196 } |
|
197 |
767
|
198 // Set terminal in raw mode. From less-177. |
|
199 // |
|
200 // Change terminal to "raw mode", or restore to "normal" mode. |
|
201 // "Raw mode" means |
|
202 // 1. An outstanding read will complete on receipt of a single keystroke. |
|
203 // 2. Input is not echoed. |
|
204 // 3. On output, \n is mapped to \r\n. |
|
205 // 4. \t is NOT expanded into spaces. |
|
206 // 5. Signal-causing characters such as ctrl-C (interrupt), |
|
207 // etc. are NOT disabled. |
|
208 // It doesn't matter whether an input \n is mapped to \r, or vice versa. |
|
209 |
529
|
210 void |
|
211 raw_mode (int on) |
|
212 { |
|
213 static int curr_on = 0; |
|
214 |
|
215 int tty_fd = STDIN_FILENO; |
|
216 if (! isatty (tty_fd)) |
|
217 { |
|
218 if (interactive) |
|
219 error ("stdin is not a tty!"); |
|
220 return; |
|
221 } |
|
222 |
|
223 if (on == curr_on) |
|
224 return; |
|
225 |
|
226 #if defined (HAVE_TERMIOS_H) |
|
227 { |
|
228 struct termios s; |
|
229 static struct termios save_term; |
|
230 |
|
231 if (on) |
|
232 { |
1358
|
233 // Get terminal modes. |
529
|
234 |
|
235 tcgetattr (tty_fd, &s); |
|
236 |
1358
|
237 // Save modes and set certain variables dependent on modes. |
529
|
238 |
|
239 save_term = s; |
|
240 // ospeed = s.c_cflag & CBAUD; |
|
241 // erase_char = s.c_cc[VERASE]; |
|
242 // kill_char = s.c_cc[VKILL]; |
|
243 |
1358
|
244 // Set the modes to the way we want them. |
529
|
245 |
|
246 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
247 s.c_oflag |= (OPOST|ONLCR); |
|
248 #if defined (OCRNL) |
|
249 s.c_oflag &= ~(OCRNL); |
|
250 #endif |
|
251 #if defined (ONOCR) |
|
252 s.c_oflag &= ~(ONOCR); |
|
253 #endif |
|
254 #if defined (ONLRET) |
|
255 s.c_oflag &= ~(ONLRET); |
|
256 #endif |
|
257 s.c_cc[VMIN] = 1; |
|
258 s.c_cc[VTIME] = 0; |
|
259 } |
|
260 else |
|
261 { |
1358
|
262 // Restore saved modes. |
|
263 |
529
|
264 s = save_term; |
|
265 } |
|
266 tcsetattr (tty_fd, TCSAFLUSH, &s); |
|
267 } |
|
268 #elif defined (HAVE_TERMIO_H) |
|
269 { |
|
270 struct termio s; |
|
271 static struct termio save_term; |
|
272 |
|
273 if (on) |
|
274 { |
1358
|
275 // Get terminal modes. |
529
|
276 |
|
277 ioctl (tty_fd, TCGETA, &s); |
|
278 |
1358
|
279 // Save modes and set certain variables dependent on modes. |
529
|
280 |
|
281 save_term = s; |
|
282 // ospeed = s.c_cflag & CBAUD; |
|
283 // erase_char = s.c_cc[VERASE]; |
|
284 // kill_char = s.c_cc[VKILL]; |
|
285 |
1358
|
286 // Set the modes to the way we want them. |
529
|
287 |
|
288 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
289 s.c_oflag |= (OPOST|ONLCR); |
|
290 #if defined (OCRNL) |
|
291 s.c_oflag &= ~(OCRNL); |
|
292 #endif |
|
293 #if defined (ONOCR) |
|
294 s.c_oflag &= ~(ONOCR); |
|
295 #endif |
|
296 #if defined (ONLRET) |
|
297 s.c_oflag &= ~(ONLRET); |
|
298 #endif |
|
299 s.c_cc[VMIN] = 1; |
|
300 s.c_cc[VTIME] = 0; |
|
301 } |
|
302 else |
|
303 { |
1358
|
304 // Restore saved modes. |
|
305 |
529
|
306 s = save_term; |
|
307 } |
|
308 ioctl (tty_fd, TCSETAW, &s); |
|
309 } |
|
310 #elif defined (HAVE_SGTTY_H) |
|
311 { |
|
312 struct sgttyb s; |
|
313 static struct sgttyb save_term; |
|
314 |
|
315 if (on) |
|
316 { |
1358
|
317 // Get terminal modes. |
529
|
318 |
|
319 ioctl (tty_fd, TIOCGETP, &s); |
|
320 |
1358
|
321 // Save modes and set certain variables dependent on modes. |
529
|
322 |
|
323 save_term = s; |
|
324 // ospeed = s.sg_ospeed; |
|
325 // erase_char = s.sg_erase; |
|
326 // kill_char = s.sg_kill; |
|
327 |
1358
|
328 // Set the modes to the way we want them. |
529
|
329 |
|
330 s.sg_flags |= CBREAK; |
|
331 s.sg_flags &= ~(ECHO); |
|
332 } |
|
333 else |
|
334 { |
1358
|
335 // Restore saved modes. |
|
336 |
529
|
337 s = save_term; |
|
338 } |
|
339 ioctl (tty_fd, TIOCSETN, &s); |
|
340 } |
|
341 #else |
|
342 LOSE! LOSE! |
|
343 #endif |
|
344 |
|
345 curr_on = on; |
|
346 } |
|
347 |
767
|
348 // Read one character from the terminal. |
|
349 |
529
|
350 int |
|
351 kbhit (void) |
|
352 { |
|
353 int c; |
|
354 raw_mode (1); |
|
355 c = cin.get (); |
|
356 raw_mode (0); |
|
357 return c; |
|
358 } |
|
359 |
1755
|
360 string |
|
361 octave_getcwd (void) |
1111
|
362 { |
1755
|
363 string retval; |
|
364 char buf[MAXPATHLEN]; |
|
365 |
1113
|
366 #if defined (__EMX__) |
1755
|
367 char *tmp = _getcwd2 (buf, MAXPATHLEN); |
1111
|
368 #else |
1755
|
369 char *tmp = getcwd (buf, MAXPATHLEN); |
1111
|
370 #endif |
1755
|
371 |
|
372 if (tmp) |
|
373 retval = tmp; |
|
374 |
|
375 return retval; |
1111
|
376 } |
|
377 |
|
378 int |
1755
|
379 octave_chdir (const string& path) |
1111
|
380 { |
1113
|
381 #if defined (__EMX__) |
1111
|
382 int retval = -1; |
|
383 |
2549
|
384 char *tmp_path = strsave (path.c_str ()); |
|
385 |
1755
|
386 if (path.length () == 2 && path[1] == ':') |
1111
|
387 { |
2549
|
388 char *upper_case_dir_name = strupr (tmp_path); |
1111
|
389 _chdrive (upper_case_dir_name[0]); |
|
390 if (_getdrive () == upper_case_dir_name[0]) |
|
391 retval = _chdir2 ("/"); |
|
392 } |
|
393 else |
2549
|
394 retval = _chdir2 (tmp_path); |
|
395 |
|
396 delete [] tmp_path; |
1111
|
397 |
|
398 return retval; |
|
399 #else |
1755
|
400 return chdir (path.c_str ()); |
1111
|
401 #endif |
|
402 } |
|
403 |
1957
|
404 DEFUN (clc, , , |
529
|
405 "clc (): clear screen") |
|
406 { |
2086
|
407 octave_value_list retval; |
529
|
408 |
|
409 rl_beg_of_line (); |
|
410 rl_kill_line (1); |
|
411 |
|
412 #if ! defined (_GO32_) |
|
413 if (term_clrpag) |
|
414 tputs (term_clrpag, 1, _rl_output_character_function); |
|
415 else |
|
416 crlf (); |
|
417 #else |
|
418 crlf (); |
|
419 #endif |
|
420 |
|
421 fflush (rl_outstream); |
|
422 |
|
423 return retval; |
|
424 } |
|
425 |
549
|
426 DEFALIAS (home, clc); |
|
427 |
1957
|
428 DEFUN (getenv, args, , |
529
|
429 "getenv (STRING): get environment variable values") |
|
430 { |
2086
|
431 octave_value_list retval; |
529
|
432 |
|
433 int nargin = args.length (); |
|
434 |
712
|
435 if (nargin == 1) |
529
|
436 { |
1728
|
437 string tstr = args(0).string_value (); |
|
438 const char *name = tstr.c_str (); |
636
|
439 |
|
440 if (! error_state) |
|
441 { |
|
442 char *value = getenv (name); |
|
443 if (value) |
|
444 retval = value; |
|
445 else |
|
446 retval = ""; |
|
447 } |
529
|
448 } |
|
449 else |
|
450 print_usage ("getenv"); |
|
451 |
|
452 return retval; |
|
453 } |
|
454 |
1957
|
455 DEFUN (putenv, args, , |
1706
|
456 "putenv (VAR, VALUE): define environment variable VAR=VALUE") |
|
457 { |
2086
|
458 octave_value_list retval; |
1706
|
459 |
|
460 int nargin = args.length (); |
|
461 |
|
462 if (nargin == 2) |
|
463 { |
1755
|
464 string var = args(0).string_value (); |
1706
|
465 |
|
466 if (! error_state) |
|
467 { |
1755
|
468 string val = args(1).string_value (); |
1706
|
469 |
|
470 if (! error_state) |
1755
|
471 oct_putenv (var.c_str (), val.c_str ()); |
1706
|
472 else |
|
473 error ("putenv: second argument should be a string"); |
|
474 } |
|
475 else |
|
476 error ("putenv: first argument should be a string"); |
|
477 } |
|
478 else |
|
479 print_usage ("putenv"); |
|
480 |
|
481 return retval; |
|
482 } |
|
483 |
1957
|
484 DEFUN (kbhit, , , |
529
|
485 "kbhit: get a single character from the terminal") |
|
486 { |
2086
|
487 octave_value_list retval; |
529
|
488 |
1358
|
489 // XXX FIXME XXX -- add timeout and default value args? |
529
|
490 |
|
491 if (interactive) |
|
492 { |
|
493 int c = kbhit (); |
|
494 char *s = new char [2]; |
|
495 s[0] = c; |
|
496 s[1] = '\0'; |
|
497 retval = s; |
|
498 } |
|
499 |
|
500 return retval; |
|
501 } |
|
502 |
1957
|
503 DEFUN (pause, args, , |
529
|
504 "pause (seconds): suspend program execution") |
|
505 { |
2086
|
506 octave_value_list retval; |
529
|
507 |
|
508 int nargin = args.length (); |
|
509 |
712
|
510 if (! (nargin == 0 || nargin == 1)) |
529
|
511 { |
|
512 print_usage ("pause"); |
|
513 return retval; |
|
514 } |
|
515 |
1579
|
516 if (nargin == 1) |
529
|
517 { |
1579
|
518 double dval = args(0).double_value (); |
636
|
519 |
1579
|
520 if (! error_state) |
|
521 { |
|
522 if (xisnan (dval)) |
|
523 warning ("pause: NaN is an invalid delay"); |
2630
|
524 else if (xisinf (dval)) |
|
525 kbhit (); |
1579
|
526 else |
|
527 { |
|
528 int delay = NINT (dval); |
|
529 if (delay > 0) |
|
530 sleep (delay); |
|
531 } |
529
|
532 } |
|
533 } |
1579
|
534 else |
2630
|
535 kbhit (); |
|
536 |
|
537 return retval; |
|
538 } |
|
539 |
|
540 DEFUN (sleep, args, , |
|
541 "sleep (seconds): suspend program execution") |
|
542 { |
|
543 octave_value_list retval; |
|
544 |
|
545 if (args.length () == 1) |
1579
|
546 { |
2630
|
547 double dval = args(0).double_value (); |
|
548 |
|
549 if (! error_state) |
|
550 { |
|
551 if (xisnan (dval)) |
|
552 warning ("sleep: NaN is an invalid delay"); |
|
553 else |
|
554 { |
|
555 int delay = NINT (dval); |
|
556 if (delay > 0) |
|
557 sleep (delay); |
|
558 } |
|
559 } |
1579
|
560 } |
2630
|
561 else |
|
562 print_usage ("sleep"); |
|
563 |
|
564 return retval; |
|
565 } |
|
566 |
|
567 DEFUN (usleep, args, , |
|
568 "usleep (microseconds): suspend program execution") |
|
569 { |
|
570 octave_value_list retval; |
|
571 |
|
572 if (args.length () == 1) |
|
573 { |
|
574 double dval = args(0).double_value (); |
|
575 |
|
576 if (! error_state) |
|
577 { |
|
578 if (xisnan (dval)) |
|
579 warning ("usleep: NaN is an invalid delay"); |
|
580 else |
|
581 { |
2631
|
582 #if defined (HAVE_USLEEP) |
2630
|
583 int delay = NINT (dval); |
2631
|
584 |
2630
|
585 if (delay > 0) |
|
586 usleep (delay); |
|
587 #else |
2631
|
588 int delay = NINT (dval / 1e6); |
2630
|
589 |
2631
|
590 if (delay > 0) |
|
591 sleep (delay); |
2630
|
592 #endif |
|
593 } |
|
594 } |
|
595 } |
|
596 else |
|
597 print_usage ("usleep"); |
529
|
598 |
|
599 return retval; |
|
600 } |
|
601 |
862
|
602 // XXX FIXME XXX -- maybe this should only return 1 if IEEE floating |
|
603 // point functions really work. |
|
604 |
1957
|
605 DEFUN (isieee, , , |
862
|
606 "isieee (): return 1 if host uses IEEE floating point") |
|
607 { |
2317
|
608 oct_mach_info::float_format flt_fmt = |
|
609 oct_mach_info::native_float_format (); |
|
610 |
2800
|
611 return static_cast<double> (flt_fmt == oct_mach_info::ieee_little_endian |
|
612 || flt_fmt == oct_mach_info::ieee_big_endian); |
862
|
613 } |
|
614 |
529
|
615 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
616 int |
|
617 gethostname (char *name, int namelen) |
|
618 { |
|
619 int i; |
|
620 struct utsname ut; |
|
621 |
|
622 --namelen; |
|
623 |
|
624 uname (&ut); |
|
625 i = strlen (ut.nodename) + 1; |
|
626 strncpy (name, ut.nodename, i < namelen ? i : namelen); |
|
627 name[namelen] = '\0'; |
|
628 |
|
629 return 0; |
|
630 } |
|
631 #endif |
|
632 |
1750
|
633 // The check for error state allows us to do this: |
|
634 // |
|
635 // string foo = oct_tilde_expand (args(0).string_value ()); |
|
636 // |
|
637 // without having to use a temporary and check error_state before |
|
638 // calling oct_tilde_expand. |
|
639 |
|
640 string |
|
641 oct_tilde_expand (const string& name) |
|
642 { |
|
643 string retval; |
|
644 |
|
645 if (! error_state) |
|
646 { |
|
647 char *tmp = tilde_expand (name.c_str ()); |
|
648 retval = tmp; |
|
649 delete [] tmp; |
|
650 } |
|
651 |
|
652 return retval; |
|
653 } |
|
654 |
2493
|
655 // A vector version of the above. |
|
656 |
|
657 string_vector |
|
658 oct_tilde_expand (const string_vector& names) |
|
659 { |
|
660 string_vector retval; |
|
661 |
|
662 if (! error_state) |
|
663 { |
|
664 int n = names.length (); |
|
665 |
|
666 retval.resize (n); |
|
667 |
|
668 for (int i = 0; i < n; i++) |
|
669 retval[i] = oct_tilde_expand (names[i]); |
|
670 } |
|
671 |
|
672 return retval; |
|
673 } |
|
674 |
1957
|
675 DEFUN (tilde_expand, args, , |
1750
|
676 "tilde_expand (STRING): perform tilde expansion on STRING") |
|
677 { |
2086
|
678 octave_value_list retval; |
1750
|
679 |
|
680 int nargin = args.length (); |
|
681 |
|
682 if (nargin == 1) |
2493
|
683 retval = oct_tilde_expand (args(0).all_strings ()); |
1750
|
684 else |
|
685 print_usage ("tilde_expand"); |
|
686 |
|
687 return retval; |
|
688 } |
|
689 |
2552
|
690 #if defined (__EMX__) && defined (OS2) |
|
691 |
|
692 DEFUN_TEXT (extproc, , , |
|
693 "extproc : ignored by Octave") |
|
694 { |
|
695 return octave_value_list (); |
|
696 } |
|
697 |
|
698 DEFALIAS (EXTPROC, extproc); |
|
699 |
|
700 #endif |
|
701 |
529
|
702 /* |
1
|
703 ;;; Local Variables: *** |
|
704 ;;; mode: C++ *** |
|
705 ;;; End: *** |
|
706 */ |