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