1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 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 |
2317
|
82 #include "mach-info.h" |
1769
|
83 #include "oct-math.h" |
|
84 |
1352
|
85 #include "defun.h" |
|
86 #include "error.h" |
542
|
87 #include "help.h" |
529
|
88 #include "input.h" |
1742
|
89 #include "mappers.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 |
|
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 |
2508
|
137 #if defined (SCO) |
|
138 static void |
|
139 SCO_init (void) |
|
140 { |
|
141 #if defined (HAVE_IEEEFP_H) |
|
142 // Disable trapping on common exceptions. |
|
143 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
144 #endif |
|
145 } |
|
146 #endif |
|
147 |
505
|
148 #if defined (EXCEPTION_IN_MATH) |
|
149 extern "C" |
|
150 int |
|
151 matherr (struct exception *x) |
|
152 { |
1358
|
153 // Possibly print our own message someday. Should probably be |
|
154 // user-switchable. |
505
|
155 |
|
156 switch (x->type) |
|
157 { |
|
158 case DOMAIN: |
|
159 case SING: |
|
160 case OVERFLOW: |
|
161 case UNDERFLOW: |
|
162 case TLOSS: |
|
163 case PLOSS: |
|
164 default: |
|
165 break; |
|
166 } |
|
167 |
1358
|
168 // But don't print the system message. |
505
|
169 |
|
170 return 1; |
|
171 } |
1466
|
172 #endif |
505
|
173 |
1
|
174 void |
|
175 sysdep_init (void) |
|
176 { |
959
|
177 #if defined (__386BSD__) || defined (__FreeBSD__) |
2508
|
178 BSD_init (); |
|
179 #elif defined NeXT |
1
|
180 NeXT_init (); |
2508
|
181 #elif defined (SCO) |
|
182 SCO_init (); |
1
|
183 #endif |
444
|
184 |
|
185 octave_ieee_init (); |
1
|
186 } |
|
187 |
767
|
188 // Set terminal in raw mode. From less-177. |
|
189 // |
|
190 // Change terminal to "raw mode", or restore to "normal" mode. |
|
191 // "Raw mode" means |
|
192 // 1. An outstanding read will complete on receipt of a single keystroke. |
|
193 // 2. Input is not echoed. |
|
194 // 3. On output, \n is mapped to \r\n. |
|
195 // 4. \t is NOT expanded into spaces. |
|
196 // 5. Signal-causing characters such as ctrl-C (interrupt), |
|
197 // etc. are NOT disabled. |
|
198 // It doesn't matter whether an input \n is mapped to \r, or vice versa. |
|
199 |
529
|
200 void |
|
201 raw_mode (int on) |
|
202 { |
|
203 static int curr_on = 0; |
|
204 |
|
205 int tty_fd = STDIN_FILENO; |
|
206 if (! isatty (tty_fd)) |
|
207 { |
|
208 if (interactive) |
|
209 error ("stdin is not a tty!"); |
|
210 return; |
|
211 } |
|
212 |
|
213 if (on == curr_on) |
|
214 return; |
|
215 |
|
216 #if defined (HAVE_TERMIOS_H) |
|
217 { |
|
218 struct termios s; |
|
219 static struct termios save_term; |
|
220 |
|
221 if (on) |
|
222 { |
1358
|
223 // Get terminal modes. |
529
|
224 |
|
225 tcgetattr (tty_fd, &s); |
|
226 |
1358
|
227 // Save modes and set certain variables dependent on modes. |
529
|
228 |
|
229 save_term = s; |
|
230 // ospeed = s.c_cflag & CBAUD; |
|
231 // erase_char = s.c_cc[VERASE]; |
|
232 // kill_char = s.c_cc[VKILL]; |
|
233 |
1358
|
234 // Set the modes to the way we want them. |
529
|
235 |
|
236 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
237 s.c_oflag |= (OPOST|ONLCR); |
|
238 #if defined (OCRNL) |
|
239 s.c_oflag &= ~(OCRNL); |
|
240 #endif |
|
241 #if defined (ONOCR) |
|
242 s.c_oflag &= ~(ONOCR); |
|
243 #endif |
|
244 #if defined (ONLRET) |
|
245 s.c_oflag &= ~(ONLRET); |
|
246 #endif |
|
247 s.c_cc[VMIN] = 1; |
|
248 s.c_cc[VTIME] = 0; |
|
249 } |
|
250 else |
|
251 { |
1358
|
252 // Restore saved modes. |
|
253 |
529
|
254 s = save_term; |
|
255 } |
|
256 tcsetattr (tty_fd, TCSAFLUSH, &s); |
|
257 } |
|
258 #elif defined (HAVE_TERMIO_H) |
|
259 { |
|
260 struct termio s; |
|
261 static struct termio save_term; |
|
262 |
|
263 if (on) |
|
264 { |
1358
|
265 // Get terminal modes. |
529
|
266 |
|
267 ioctl (tty_fd, TCGETA, &s); |
|
268 |
1358
|
269 // Save modes and set certain variables dependent on modes. |
529
|
270 |
|
271 save_term = s; |
|
272 // ospeed = s.c_cflag & CBAUD; |
|
273 // erase_char = s.c_cc[VERASE]; |
|
274 // kill_char = s.c_cc[VKILL]; |
|
275 |
1358
|
276 // Set the modes to the way we want them. |
529
|
277 |
|
278 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
279 s.c_oflag |= (OPOST|ONLCR); |
|
280 #if defined (OCRNL) |
|
281 s.c_oflag &= ~(OCRNL); |
|
282 #endif |
|
283 #if defined (ONOCR) |
|
284 s.c_oflag &= ~(ONOCR); |
|
285 #endif |
|
286 #if defined (ONLRET) |
|
287 s.c_oflag &= ~(ONLRET); |
|
288 #endif |
|
289 s.c_cc[VMIN] = 1; |
|
290 s.c_cc[VTIME] = 0; |
|
291 } |
|
292 else |
|
293 { |
1358
|
294 // Restore saved modes. |
|
295 |
529
|
296 s = save_term; |
|
297 } |
|
298 ioctl (tty_fd, TCSETAW, &s); |
|
299 } |
|
300 #elif defined (HAVE_SGTTY_H) |
|
301 { |
|
302 struct sgttyb s; |
|
303 static struct sgttyb save_term; |
|
304 |
|
305 if (on) |
|
306 { |
1358
|
307 // Get terminal modes. |
529
|
308 |
|
309 ioctl (tty_fd, TIOCGETP, &s); |
|
310 |
1358
|
311 // Save modes and set certain variables dependent on modes. |
529
|
312 |
|
313 save_term = s; |
|
314 // ospeed = s.sg_ospeed; |
|
315 // erase_char = s.sg_erase; |
|
316 // kill_char = s.sg_kill; |
|
317 |
1358
|
318 // Set the modes to the way we want them. |
529
|
319 |
|
320 s.sg_flags |= CBREAK; |
|
321 s.sg_flags &= ~(ECHO); |
|
322 } |
|
323 else |
|
324 { |
1358
|
325 // Restore saved modes. |
|
326 |
529
|
327 s = save_term; |
|
328 } |
|
329 ioctl (tty_fd, TIOCSETN, &s); |
|
330 } |
|
331 #else |
|
332 LOSE! LOSE! |
|
333 #endif |
|
334 |
|
335 curr_on = on; |
|
336 } |
|
337 |
767
|
338 // Read one character from the terminal. |
|
339 |
529
|
340 int |
|
341 kbhit (void) |
|
342 { |
|
343 int c; |
|
344 raw_mode (1); |
|
345 c = cin.get (); |
|
346 raw_mode (0); |
|
347 return c; |
|
348 } |
|
349 |
1755
|
350 string |
|
351 octave_getcwd (void) |
1111
|
352 { |
1755
|
353 string retval; |
|
354 char buf[MAXPATHLEN]; |
|
355 |
1113
|
356 #if defined (__EMX__) |
1755
|
357 char *tmp = _getcwd2 (buf, MAXPATHLEN); |
1111
|
358 #else |
1755
|
359 char *tmp = getcwd (buf, MAXPATHLEN); |
1111
|
360 #endif |
1755
|
361 |
|
362 if (tmp) |
|
363 retval = tmp; |
|
364 |
|
365 return retval; |
1111
|
366 } |
|
367 |
|
368 int |
1755
|
369 octave_chdir (const string& path) |
1111
|
370 { |
1113
|
371 #if defined (__EMX__) |
1111
|
372 int retval = -1; |
|
373 |
1755
|
374 if (path.length () == 2 && path[1] == ':') |
1111
|
375 { |
1755
|
376 char *upper_case_dir_name = strupr (path.c_str ()); |
1111
|
377 _chdrive (upper_case_dir_name[0]); |
|
378 if (_getdrive () == upper_case_dir_name[0]) |
|
379 retval = _chdir2 ("/"); |
|
380 } |
|
381 else |
1755
|
382 retval = _chdir2 (path.c_str ()); |
1111
|
383 |
|
384 return retval; |
|
385 #else |
1755
|
386 return chdir (path.c_str ()); |
1111
|
387 #endif |
|
388 } |
|
389 |
1957
|
390 DEFUN (clc, , , |
529
|
391 "clc (): clear screen") |
|
392 { |
2086
|
393 octave_value_list retval; |
529
|
394 |
|
395 rl_beg_of_line (); |
|
396 rl_kill_line (1); |
|
397 |
|
398 #if ! defined (_GO32_) |
|
399 if (term_clrpag) |
|
400 tputs (term_clrpag, 1, _rl_output_character_function); |
|
401 else |
|
402 crlf (); |
|
403 #else |
|
404 crlf (); |
|
405 #endif |
|
406 |
|
407 fflush (rl_outstream); |
|
408 |
|
409 return retval; |
|
410 } |
|
411 |
549
|
412 DEFALIAS (home, clc); |
|
413 |
1957
|
414 DEFUN (getenv, args, , |
529
|
415 "getenv (STRING): get environment variable values") |
|
416 { |
2086
|
417 octave_value_list retval; |
529
|
418 |
|
419 int nargin = args.length (); |
|
420 |
712
|
421 if (nargin == 1) |
529
|
422 { |
1728
|
423 string tstr = args(0).string_value (); |
|
424 const char *name = tstr.c_str (); |
636
|
425 |
|
426 if (! error_state) |
|
427 { |
|
428 char *value = getenv (name); |
|
429 if (value) |
|
430 retval = value; |
|
431 else |
|
432 retval = ""; |
|
433 } |
529
|
434 } |
|
435 else |
|
436 print_usage ("getenv"); |
|
437 |
|
438 return retval; |
|
439 } |
|
440 |
1957
|
441 DEFUN (putenv, args, , |
1706
|
442 "putenv (VAR, VALUE): define environment variable VAR=VALUE") |
|
443 { |
2086
|
444 octave_value_list retval; |
1706
|
445 |
|
446 int nargin = args.length (); |
|
447 |
|
448 if (nargin == 2) |
|
449 { |
1755
|
450 string var = args(0).string_value (); |
1706
|
451 |
|
452 if (! error_state) |
|
453 { |
1755
|
454 string val = args(1).string_value (); |
1706
|
455 |
|
456 if (! error_state) |
1755
|
457 oct_putenv (var.c_str (), val.c_str ()); |
1706
|
458 else |
|
459 error ("putenv: second argument should be a string"); |
|
460 } |
|
461 else |
|
462 error ("putenv: first argument should be a string"); |
|
463 } |
|
464 else |
|
465 print_usage ("putenv"); |
|
466 |
|
467 return retval; |
|
468 } |
|
469 |
1957
|
470 DEFUN (kbhit, , , |
529
|
471 "kbhit: get a single character from the terminal") |
|
472 { |
2086
|
473 octave_value_list retval; |
529
|
474 |
1358
|
475 // XXX FIXME XXX -- add timeout and default value args? |
529
|
476 |
|
477 if (interactive) |
|
478 { |
|
479 int c = kbhit (); |
|
480 char *s = new char [2]; |
|
481 s[0] = c; |
|
482 s[1] = '\0'; |
|
483 retval = s; |
|
484 } |
|
485 |
|
486 return retval; |
|
487 } |
|
488 |
1957
|
489 DEFUN (pause, args, , |
529
|
490 "pause (seconds): suspend program execution") |
|
491 { |
2086
|
492 octave_value_list retval; |
529
|
493 |
|
494 int nargin = args.length (); |
|
495 |
712
|
496 if (! (nargin == 0 || nargin == 1)) |
529
|
497 { |
|
498 print_usage ("pause"); |
|
499 return retval; |
|
500 } |
|
501 |
1579
|
502 if (nargin == 1) |
529
|
503 { |
1579
|
504 double dval = args(0).double_value (); |
636
|
505 |
1579
|
506 if (! error_state) |
|
507 { |
|
508 if (xisnan (dval)) |
|
509 warning ("pause: NaN is an invalid delay"); |
|
510 else |
|
511 { |
|
512 int delay = NINT (dval); |
|
513 if (delay > 0) |
|
514 sleep (delay); |
|
515 } |
529
|
516 } |
|
517 } |
1579
|
518 else |
|
519 { |
|
520 if (kbhit () == EOF) |
|
521 clean_up_and_exit (0); |
|
522 } |
529
|
523 |
|
524 return retval; |
|
525 } |
|
526 |
862
|
527 // XXX FIXME XXX -- maybe this should only return 1 if IEEE floating |
|
528 // point functions really work. |
|
529 |
1957
|
530 DEFUN (isieee, , , |
862
|
531 "isieee (): return 1 if host uses IEEE floating point") |
|
532 { |
2317
|
533 oct_mach_info::float_format flt_fmt = |
|
534 oct_mach_info::native_float_format (); |
|
535 |
|
536 return (double) (flt_fmt == oct_mach_info::ieee_little_endian |
|
537 || flt_fmt == oct_mach_info::ieee_big_endian); |
862
|
538 } |
|
539 |
529
|
540 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
541 int |
|
542 gethostname (char *name, int namelen) |
|
543 { |
|
544 int i; |
|
545 struct utsname ut; |
|
546 |
|
547 --namelen; |
|
548 |
|
549 uname (&ut); |
|
550 i = strlen (ut.nodename) + 1; |
|
551 strncpy (name, ut.nodename, i < namelen ? i : namelen); |
|
552 name[namelen] = '\0'; |
|
553 |
|
554 return 0; |
|
555 } |
|
556 #endif |
|
557 |
1750
|
558 // The check for error state allows us to do this: |
|
559 // |
|
560 // string foo = oct_tilde_expand (args(0).string_value ()); |
|
561 // |
|
562 // without having to use a temporary and check error_state before |
|
563 // calling oct_tilde_expand. |
|
564 |
|
565 string |
|
566 oct_tilde_expand (const string& name) |
|
567 { |
|
568 string retval; |
|
569 |
|
570 if (! error_state) |
|
571 { |
|
572 char *tmp = tilde_expand (name.c_str ()); |
|
573 retval = tmp; |
|
574 delete [] tmp; |
|
575 } |
|
576 |
|
577 return retval; |
|
578 } |
|
579 |
2493
|
580 // A vector version of the above. |
|
581 |
|
582 string_vector |
|
583 oct_tilde_expand (const string_vector& names) |
|
584 { |
|
585 string_vector retval; |
|
586 |
|
587 if (! error_state) |
|
588 { |
|
589 int n = names.length (); |
|
590 |
|
591 retval.resize (n); |
|
592 |
|
593 for (int i = 0; i < n; i++) |
|
594 retval[i] = oct_tilde_expand (names[i]); |
|
595 } |
|
596 |
|
597 return retval; |
|
598 } |
|
599 |
1957
|
600 DEFUN (tilde_expand, args, , |
1750
|
601 "tilde_expand (STRING): perform tilde expansion on STRING") |
|
602 { |
2086
|
603 octave_value_list retval; |
1750
|
604 |
|
605 int nargin = args.length (); |
|
606 |
|
607 if (nargin == 1) |
2493
|
608 retval = oct_tilde_expand (args(0).all_strings ()); |
1750
|
609 else |
|
610 print_usage ("tilde_expand"); |
|
611 |
|
612 return retval; |
|
613 } |
|
614 |
529
|
615 /* |
1
|
616 ;;; Local Variables: *** |
|
617 ;;; mode: C++ *** |
|
618 ;;; End: *** |
|
619 */ |