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