comparison src/sysdep.cc @ 529:7ea224e713cd

[project @ 1994-07-20 18:54:27 by jwe]
author jwe
date Wed, 20 Jul 1994 19:19:08 +0000
parents f264c1454c2b
children 682393bf54f7
comparison
equal deleted inserted replaced
528:e1e6e33e26f8 529:7ea224e713cd
23 23
24 #ifdef HAVE_CONFIG_H 24 #ifdef HAVE_CONFIG_H
25 #include "config.h" 25 #include "config.h"
26 #endif 26 #endif
27 27
28 #include <sys/types.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
28 #include <math.h> 32 #include <math.h>
33 #include <stddef.h>
29 #include <stdlib.h> 34 #include <stdlib.h>
30 35 #include <stdio.h>
36
37 #include "tree-const.h"
38 #include "octave.h"
39 #include "input.h"
40 #include "utils.h"
41 #include "oct-obj.h"
31 #include "error.h" 42 #include "error.h"
32 #include "sysdep.h" 43 #include "sysdep.h"
44 #include "defun.h"
45
46 extern "C"
47 {
48 #include <readline/readline.h>
49
50 extern char *term_clrpag;
51 extern void _rl_output_character_function ();
52
53 #if defined (HAVE_TERMIOS_H)
54 #include <termios.h>
55 #elif defined (HAVE_TERMIO_H)
56 #include <termio.h>
57 #elif defined (HAVE_SGTTY_H)
58 #include <sgtty.h>
59 #else
60 LOSE! LOSE!
61 #endif
62
63 extern int ioctl ();
64 }
65
66 #ifndef STDIN_FILENO
67 #define STDIN_FILENO 1
68 #endif
33 69
34 // Octave's idea of infinity. 70 // Octave's idea of infinity.
35 double octave_Inf; 71 double octave_Inf;
36 72
37 // Octave's idea of not a number. 73 // Octave's idea of not a number.
77 #if defined (HAVE_INFINITY) 113 #if defined (HAVE_INFINITY)
78 octave_Inf = (double) infinity (); 114 octave_Inf = (double) infinity ();
79 #else 115 #else
80 #ifdef linux 116 #ifdef linux
81 octave_Inf = HUGE_VAL; 117 octave_Inf = HUGE_VAL;
118 #else
119 #ifdef __alpha__
120 extern unsigned int DINFINITY[2];
121 octave_Inf = (*((double *) (DINFINITY)));
82 #else 122 #else
83 double tmp = 1e+10; 123 double tmp = 1e+10;
84 octave_Inf = tmp; 124 octave_Inf = tmp;
85 for (;;) 125 for (;;)
86 { 126 {
89 break; 129 break;
90 tmp = octave_Inf; 130 tmp = octave_Inf;
91 } 131 }
92 #endif 132 #endif
93 #endif 133 #endif
134 #endif
135
136
94 137
95 #if defined (HAVE_QUIET_NAN) 138 #if defined (HAVE_QUIET_NAN)
96 octave_NaN = (double) quiet_nan (); 139 octave_NaN = (double) quiet_nan ();
97 #else 140 #else
98 #ifdef linux 141 #ifdef linux
99 octave_NaN = NAN; 142 octave_NaN = NAN;
100 #else 143 #else
144 #ifdef __alpha__
145 extern unsigned int DQNAN[2];
146 octave_NaN = (*((double *) (DQNAN)));
147 #else
101 octave_NaN = octave_Inf / octave_Inf; 148 octave_NaN = octave_Inf / octave_Inf;
149 #endif
102 #endif 150 #endif
103 #endif 151 #endif
104 152
105 #else 153 #else
106 154
156 204
157 octave_ieee_init (); 205 octave_ieee_init ();
158 } 206 }
159 207
160 /* 208 /*
209 * Set terminal in raw mode. From less-177.
210 *
211 * Change terminal to "raw mode", or restore to "normal" mode.
212 * "Raw mode" means
213 * 1. An outstanding read will complete on receipt of a single keystroke.
214 * 2. Input is not echoed.
215 * 3. On output, \n is mapped to \r\n.
216 * 4. \t is NOT expanded into spaces.
217 * 5. Signal-causing characters such as ctrl-C (interrupt),
218 * etc. are NOT disabled.
219 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
220 */
221 void
222 raw_mode (int on)
223 {
224 static int curr_on = 0;
225
226 int tty_fd = STDIN_FILENO;
227 if (! isatty (tty_fd))
228 {
229 if (interactive)
230 error ("stdin is not a tty!");
231 return;
232 }
233
234 if (on == curr_on)
235 return;
236
237 #if defined (HAVE_TERMIOS_H)
238 {
239 struct termios s;
240 static struct termios save_term;
241
242 if (on)
243 {
244 // Get terminal modes.
245
246 tcgetattr (tty_fd, &s);
247
248 // Save modes and set certain variables dependent on modes.
249
250 save_term = s;
251 // ospeed = s.c_cflag & CBAUD;
252 // erase_char = s.c_cc[VERASE];
253 // kill_char = s.c_cc[VKILL];
254
255 // Set the modes to the way we want them.
256
257 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
258 s.c_oflag |= (OPOST|ONLCR);
259 #if defined (OCRNL)
260 s.c_oflag &= ~(OCRNL);
261 #endif
262 #if defined (ONOCR)
263 s.c_oflag &= ~(ONOCR);
264 #endif
265 #if defined (ONLRET)
266 s.c_oflag &= ~(ONLRET);
267 #endif
268 s.c_cc[VMIN] = 1;
269 s.c_cc[VTIME] = 0;
270 }
271 else
272 {
273 // Restore saved modes.
274 s = save_term;
275 }
276 tcsetattr (tty_fd, TCSAFLUSH, &s);
277 }
278 #elif defined (HAVE_TERMIO_H)
279 {
280 struct termio s;
281 static struct termio save_term;
282
283 if (on)
284 {
285 // Get terminal modes.
286
287 ioctl (tty_fd, TCGETA, &s);
288
289 // Save modes and set certain variables dependent on modes.
290
291 save_term = s;
292 // ospeed = s.c_cflag & CBAUD;
293 // erase_char = s.c_cc[VERASE];
294 // kill_char = s.c_cc[VKILL];
295
296 // Set the modes to the way we want them.
297
298 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
299 s.c_oflag |= (OPOST|ONLCR);
300 #if defined (OCRNL)
301 s.c_oflag &= ~(OCRNL);
302 #endif
303 #if defined (ONOCR)
304 s.c_oflag &= ~(ONOCR);
305 #endif
306 #if defined (ONLRET)
307 s.c_oflag &= ~(ONLRET);
308 #endif
309 s.c_cc[VMIN] = 1;
310 s.c_cc[VTIME] = 0;
311 }
312 else
313 {
314 // Restore saved modes.
315 s = save_term;
316 }
317 ioctl (tty_fd, TCSETAW, &s);
318 }
319 #elif defined (HAVE_SGTTY_H)
320 {
321 struct sgttyb s;
322 static struct sgttyb save_term;
323
324 if (on)
325 {
326 // Get terminal modes.
327
328 ioctl (tty_fd, TIOCGETP, &s);
329
330 // Save modes and set certain variables dependent on modes.
331
332 save_term = s;
333 // ospeed = s.sg_ospeed;
334 // erase_char = s.sg_erase;
335 // kill_char = s.sg_kill;
336
337 // Set the modes to the way we want them.
338
339 s.sg_flags |= CBREAK;
340 s.sg_flags &= ~(ECHO);
341 }
342 else
343 {
344 // Restore saved modes.
345 s = save_term;
346 }
347 ioctl (tty_fd, TIOCSETN, &s);
348 }
349 #else
350 LOSE! LOSE!
351 #endif
352
353 curr_on = on;
354 }
355
356 /*
357 * Read one character from the terminal.
358 */
359 int
360 kbhit (void)
361 {
362 int c;
363 raw_mode (1);
364 c = cin.get ();
365 raw_mode (0);
366 return c;
367 }
368
369 DEFUN ("clc", Fclc, Sclc, 1, 0,
370 "clc (): clear screen")
371 {
372 Octave_object retval;
373
374 rl_beg_of_line ();
375 rl_kill_line (1);
376
377 #if ! defined (_GO32_)
378 if (term_clrpag)
379 tputs (term_clrpag, 1, _rl_output_character_function);
380 else
381 crlf ();
382 #else
383 crlf ();
384 #endif
385
386 fflush (rl_outstream);
387
388 return retval;
389 }
390
391 DEFUN ("getenv", Fgetenv, Sgetenv, 2, 1,
392 "getenv (STRING): get environment variable values")
393 {
394 Octave_object retval;
395
396 int nargin = args.length ();
397
398 if (nargin == 2 && args(1).is_string_type ())
399 {
400 char *value = getenv (args(1).string_value ());
401 if (value)
402 retval = value;
403 else
404 retval = "";
405 }
406 else
407 print_usage ("getenv");
408
409 return retval;
410 }
411
412 DEFALIAS (home, clc)
413
414 DEFUN ("kbhit", Fkbhit, Skbhit, 1, 1,
415 "kbhit: get a single character from the terminal")
416 {
417 Octave_object retval;
418
419 // XXX FIXME XXX -- add timeout and default value args?
420
421 if (interactive)
422 {
423 int c = kbhit ();
424 char *s = new char [2];
425 s[0] = c;
426 s[1] = '\0';
427 retval = s;
428 }
429
430 return retval;
431 }
432
433 DEFUN ("pause", Fpause, Spause, 1, 1,
434 "pause (seconds): suspend program execution")
435 {
436 Octave_object retval;
437
438 int nargin = args.length ();
439
440 if (! (nargin == 1 || nargin == 2))
441 {
442 print_usage ("pause");
443 return retval;
444 }
445
446 if (interactive)
447 {
448 switch (nargin)
449 {
450 case 2:
451 {
452 int delay = NINT (args(1).double_value ());
453 if (delay > 0)
454 {
455 sleep (delay);
456 break;
457 }
458 }
459 default:
460 if (kbhit () == EOF)
461 clean_up_and_exit (0);
462 break;
463 }
464 }
465
466 return retval;
467 }
468
469 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H)
470 extern "C"
471 {
472 #include <sys/utsname.h>
473 int
474 gethostname (char *name, int namelen)
475 {
476 int i;
477 struct utsname ut;
478
479 --namelen;
480
481 uname (&ut);
482 i = strlen (ut.nodename) + 1;
483 strncpy (name, ut.nodename, i < namelen ? i : namelen);
484 name[namelen] = '\0';
485
486 return 0;
487 }
488 }
489 #endif
490
491 /*
161 ;;; Local Variables: *** 492 ;;; Local Variables: ***
162 ;;; mode: C++ *** 493 ;;; mode: C++ ***
163 ;;; page-delimiter: "^/\\*" *** 494 ;;; page-delimiter: "^/\\*" ***
164 ;;; End: *** 495 ;;; End: ***
165 */ 496 */