home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / SOURCE / RAW.C < prev    next >
C/C++ Source or Header  |  1996-06-16  |  15KB  |  726 lines

  1. /* Copyright 1991 Digital Equipment Corporation.
  2. ** All Rights Reserved.
  3. *****************************************************************/
  4. /*     $Id: raw.c,v 1.3 1995/07/27 19:21:19 duchier Exp $     */
  5. #ifndef OS2_PORT
  6.  
  7. #ifndef lint
  8. static char vcid[] = "$Id: raw.c,v 1.3 1995/07/27 19:21:19 duchier Exp $";
  9. #endif /* lint */
  10.  
  11. /*  RM: Feb 18 1994
  12.     Added "NORAW" compile flag which removes most of this file.
  13.     */
  14.  
  15. #ifndef NORAW
  16.  
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <sgtty.h>
  20. #include <termio.h>
  21.  
  22. #ifndef TANDEM
  23. #define TANDEM 0
  24. #endif
  25.  
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <sys/ioctl.h>
  29.  
  30. #endif
  31.  
  32. #include "extern.h"
  33. #include "print.h"
  34. #ifndef OS2_PORT
  35. #include "built_ins.h"
  36. #else
  37. #include "built_in.h"
  38. #endif
  39.  
  40. #include "types.h"
  41. #include "trees.h"
  42. #include "lefun.h"
  43. #include "login.h"
  44. #include "error.h"
  45. #include "templates.h"
  46. #include "modules.h"
  47.  
  48. #ifdef X11
  49. #include "xpred.h"
  50. #endif
  51.  
  52. #ifndef NORAW
  53.  
  54.  
  55. #define stdin_fileno fileno (stdin)
  56.  
  57. extern long level;
  58. static struct sgttyb param_input;
  59. static long mode_raw = FALSE;
  60. static char bufbuf[BUFSIZ+1] = {0};
  61.  
  62. /*****************************************************************/
  63. /******** BeginRaw
  64.  
  65.   c_begin_raw ()
  66.  
  67.   set the keyboard to be in mode raw (return each key when pressed)
  68.   and don't echo the character on the standard output.
  69.  
  70.   this built-in should be used only by the life-shell of Kathleen.
  71.  
  72.  */
  73.  
  74. long c_begin_raw ()
  75.  
  76. {
  77.     struct sgttyb param;
  78.     struct termio argio;
  79.  
  80.     if (mode_raw)
  81.     {
  82.         Errorline ("in begin_raw: already in mode raw\n");
  83.         return FALSE;
  84.     }
  85.  
  86.     if (ioctl (stdin_fileno, TIOCGETP, ¶m_input) == -1)
  87.     Errorline ("in begin_raw: cannot get the input parameters\n");
  88.  
  89.     bcopy ((char*)¶m_input, (char*)¶m, sizeof (param));
  90.  
  91. #if 0
  92.     /* with RAW, we catch everything (eg: ^C is 3) */
  93.     param.sg_flags |= CBREAK | TANDEM | RAW;
  94. #else
  95.     param.sg_flags |= CBREAK | TANDEM;
  96. #endif
  97.  
  98.     param.sg_flags &= ~ECHO;
  99.  
  100.     if (ioctl (stdin_fileno, TIOCSETN, ¶m) == -1)
  101.     Errorline ("in begin_raw: cannot set the input parameters\n");
  102.  
  103.     if (ioctl (stdin_fileno, TCGETA, &argio) == -1)
  104.     Errorline ("in begin_raw: cannot get the terminal\n");
  105.  
  106.     /* do not strip the characters (the 8th bit is used for the key Compose) */
  107. #if 1
  108.     /* catch every character */
  109.     argio.c_lflag &= ~(ICANON|ISIG);
  110.     argio.c_cc[VMIN] = 1;
  111.     argio.c_cc[VTIME] = 0;
  112.  
  113.     /* with IXON, do not interpret ctrl-S and ctrl-Q */
  114.     argio.c_iflag &= ~(ISTRIP|IXON);
  115.  
  116.     /* map LF to CR-LF */
  117.     argio.c_oflag |= OPOST|ONLCR;
  118. #else
  119.     argio.c_iflag &= ~(ISTRIP);
  120. #endif
  121.  
  122.     if (ioctl (stdin_fileno, TCSETA, &argio) == -1)
  123.     Errorline ("in begin_raw: cannot set the terminal\n");
  124.  
  125.     setvbuf (stdin, bufbuf, _IOFBF, BUFSIZ);
  126.  
  127.     bzero (bufbuf, BUFSIZ+1);
  128.  
  129.     mode_raw = TRUE;
  130.     return TRUE;
  131. }
  132.  
  133.  
  134. /*****************************************************************/
  135. /******** GetRaw
  136.  
  137.   c_get_raw (-Char, -EventFlag)
  138.  
  139.   return the next key pressed in Char or if a X event has occured
  140.  
  141.   this built-in should be used only by the life-shell of Kathleen.
  142.  
  143.  */
  144.  
  145. long c_get_raw ()
  146. {
  147.     include_var_builtin (2);
  148.     ptr_definition types[2];
  149.     long nfds;
  150.     fd_set readfd, writefd, exceptfd;
  151.     struct timeval timeout;
  152.     long char_flag = FALSE, event_flag = FALSE;
  153.     long c = 0;
  154.     ptr_psi_term key_code;
  155.     long level;
  156.  
  157.     types[0] = real;
  158.     types[1] = boolean;
  159.  
  160.     begin_builtin (c_get_raw, 2, 0, types);
  161.  
  162.     if ((int)strlen (bufbuf) == 0)
  163.     {
  164.     level = (unsigned long) aim->c;
  165.  
  166.  
  167.     do
  168.     {
  169.             FD_ZERO(&readfd);
  170.             FD_SET(stdin_fileno, &readfd);
  171.             FD_ZERO(&writefd);
  172.             FD_ZERO(&exceptfd);
  173.         timeout.tv_sec = 0;
  174.         timeout.tv_usec = 100000;
  175.  
  176.         nfds = select (32, &readfd, &writefd, &exceptfd, &timeout);
  177.         if (nfds == -1)
  178.         {
  179.         if (errno != EINTR) 
  180.         {
  181.             Errorline ("it is not possible to read characters or X events\n");
  182.             exit_life(TRUE);
  183.         }
  184.         else
  185.             interrupt ();
  186.         }
  187.         else
  188.         if (nfds == 0)
  189.         {
  190. #ifdef X11
  191.         if (x_exist_event ())
  192.         {
  193.             event_flag = TRUE;
  194.             release_resid (xevent_existing);
  195.         }        
  196. #endif
  197.         }
  198.         else
  199.         {
  200.         if (FD_ISSET(stdin_fileno, &readfd) != 0)
  201.         {
  202.             /* c cna be equal to 0 with the mouse's selection */
  203.             /* first the selection is inside the buffer bufbuf */
  204.             /* later fgetc returns zeros */
  205.             /* I don't understand - jch - Fri Aug 28 1992 */
  206.             if ((c = fgetc (stdin)) != 0)
  207.             {
  208.             unify_real_result (args[0], (REAL) c);
  209.             char_flag = TRUE;
  210.             /* the shift is done below */
  211.             }
  212.         }
  213.         else
  214.             Errorline ("in select: unknown descriptor\n");
  215.         }
  216.     } while (!(char_flag || event_flag));
  217.     }
  218.     else
  219.     {
  220.     unify_real_result (args[0], (REAL) bufbuf[0]);
  221.     char_flag = TRUE;
  222.     }
  223.  
  224.     /* shift */
  225.     if (char_flag)
  226.         bcopy (&bufbuf[1], bufbuf, BUFSIZ-1);
  227.  
  228.     /* return if an X event has occured */
  229.     unify_bool_result (args[1], event_flag);
  230.  
  231.     success = TRUE;
  232.     end_builtin ();
  233. }
  234.  
  235.  
  236. /*****************************************************************/
  237. /******** PutRaw
  238.  
  239.   c_put_raw (+Char)
  240.  
  241.   write the specified char on the standard output
  242.  
  243.   this built-in should be used only by the life-shell of Kathleen.
  244.  
  245.  */
  246.  
  247. long c_put_raw ()
  248. {
  249.     include_var_builtin (1);
  250.     ptr_definition types[1];
  251.  
  252.     types[0] = real;
  253.  
  254.     begin_builtin (c_put_raw, 1, 0, types);
  255.  
  256.     putchar ((char) val[0]);
  257.     fflush (stdout);
  258.     success = TRUE;
  259.     end_builtin ();
  260. }
  261.  
  262.  
  263. /*****************************************************************/
  264. /******** EndRaw
  265.  
  266.   end_raw ()
  267.  
  268.   reset the keyboard in the previous state before xcInitModeRaw.
  269.  
  270.   this built-in should be used only by the life-shell of Kathleen.
  271.  
  272.  */
  273.  
  274. long c_end_raw ()
  275.  
  276. {
  277.     if (!mode_raw)
  278.     {
  279.         Errorline ("in c_end_raw: not in mode raw\n");
  280.         return FALSE;
  281.     }
  282.  
  283.     if (ioctl (stdin_fileno, TIOCSETN, ¶m_input) == -1)
  284.         Errorline ("in end_raw: cannot reset mode raw\n");
  285.  
  286.     setvbuf (stdin, bufbuf, _IONBF, BUFSIZ);
  287.     bzero (bufbuf, BUFSIZ);
  288.  
  289.     mode_raw = FALSE;
  290.     return TRUE;
  291. }
  292.  
  293.  
  294. /*****************************************************************/
  295. /******** InRaw
  296.  
  297.   in_raw ()
  298.  
  299.   return TRUE if mode raw 
  300.  
  301.   this built-in should be used only by the life-shell of Kathleen.
  302.  
  303.  */
  304.  
  305. long c_in_raw ()
  306.  
  307. {
  308.     deref_ptr (aim->a);
  309.     unify_bool_result (aim->b, mode_raw);
  310.  
  311.     return TRUE;
  312. }
  313.  
  314.  
  315. /*****************************************************************/
  316. /******** WindowFlag
  317.  
  318.   window_flag ()
  319.  
  320.   return TRUE if a window has been created
  321.  
  322.   this built-in should be used only by the life-shell of Kathleen.
  323.  
  324.  */
  325.  
  326. long c_window_flag ()
  327.  
  328. {
  329.     deref_ptr (aim->a);
  330. #ifdef X11
  331.     unify_bool_result (aim->b, x_window_creation);
  332. #else
  333.     unify_bool_result (aim->b, FALSE);
  334. #endif
  335.  
  336.     return TRUE;
  337. }
  338.  
  339.  
  340. /*****************************************************************/
  341. /******** ResetWindowFlag
  342.  
  343.   reset_window_flag ()
  344.  
  345.   return the flag x_window_creation
  346.  
  347.   this built-in should be used only by the life-shell of Kathleen.
  348.  
  349.  */
  350.  
  351. long c_reset_window_flag ()
  352.  
  353. {
  354.     deref_ptr (aim->a);
  355. #ifdef X11
  356.     x_window_creation = FALSE;
  357. #endif
  358.  
  359.     return TRUE;
  360. }
  361.  
  362.  
  363. #endif
  364.  
  365. /*****************************************/
  366.  
  367. /* set up the built-ins for the mode raw */
  368.  
  369. void raw_setup_builtins ()
  370.      
  371. {
  372. #ifndef NORAW  
  373.     new_built_in(bi_module,"begin_raw",         predicate, c_begin_raw);
  374.     new_built_in(bi_module,"get_raw",           predicate, c_get_raw);
  375.     new_built_in(bi_module,"put_raw",           predicate, c_put_raw);
  376.     new_built_in(bi_module,"end_raw",           predicate, c_end_raw);
  377.     new_built_in(bi_module,"in_raw",            function,  c_in_raw);
  378.     new_built_in(bi_module,"window_flag",       function,  c_window_flag);
  379.     new_built_in(bi_module,"reset_window_flag", predicate, c_reset_window_flag);
  380. #endif
  381. }
  382. #else
  383. /* Copyright 1991 Digital Equipment Corporation.
  384. ** All Rights Reserved.
  385. *****************************************************************/
  386.  
  387. /*  RM: Feb 18 1994
  388.     Added "NORAW" compile flag which removes most of this file.
  389.     */
  390.  
  391. #ifndef NORAW
  392.  
  393. #include <stdio.h>
  394. #include <ctype.h>
  395. #include <stdlib.h> 
  396. #include <conio.h>
  397. #define LBSIZE 200 
  398. static char lbuf[LBSIZE]={0}; 
  399. static int lpos[LBSIZE]; 
  400. static int lindex; 
  401. static int lcount; 
  402. static int lposition; 
  403. static long rseed = 1L; 
  404. #ifndef TANDEM
  405. #define TANDEM 0
  406. #endif
  407.  
  408. #include <sys/types.h>
  409. #endif
  410.  
  411. #include "extern.h"
  412. #include "print.h"
  413. #include "built_in.h"
  414. #include "types.h"
  415. #include "trees.h"
  416. #include "lefun.h"
  417. #include "login.h"
  418. #include "error.h"
  419. #include "template.h"
  420. #include "modules.h"
  421.  
  422. #ifndef NORAW
  423.  
  424.  
  425. #define stdin_fileno fileno (stdin)
  426.  
  427. extern long level;
  428. static long mode_raw = FALSE;
  429. static void xputc(int ch) 
  430.        _putch(ch);
  431. static int xgetc() 
  432.       return (_getch());
  433.  
  434. /*****************************************************************/
  435. /******** BeginRaw
  436.  
  437.   c_begin_raw ()
  438.  
  439.   set the keyboard to be in mode raw (return each key when pressed)
  440.   and don't echo the character on the standard output.
  441.  
  442.   this built-in should be used only by the life-shell of Kathleen.
  443.  
  444.  */
  445.  
  446. long c_begin_raw ()
  447.  
  448. {
  449.     if (mode_raw)
  450.     {
  451.         Errorline ("in begin_raw: already in mode raw\n");
  452.         return FALSE;
  453.     }
  454.     lposition = 0; 
  455.     lindex = 0; 
  456.     lcount = 0; 
  457.     mode_raw = TRUE;
  458.     return TRUE;
  459. }
  460.  
  461.  
  462. /*****************************************************************/
  463. /******** GetRaw
  464.  
  465.   c_get_raw (-Char, -EventFlag)
  466.  
  467.   return the next key pressed in Char or if a X event has occured
  468.  
  469.   this built-in should be used only by the life-shell of Kathleen.
  470.  
  471.  */
  472.  
  473. long c_get_raw ()
  474. {
  475.     int ch=0;
  476.     ptr_definition types[2];
  477.     long nfds;
  478.     long char_flag = FALSE, event_flag = FALSE;
  479.     long c = 0;
  480.     ptr_psi_term key_code;
  481.     long level;
  482.  
  483.     include_var_builtin (2);
  484.     types[0] = real;
  485.     types[1] = boolean;
  486.  
  487.     begin_builtin (c_get_raw, 2, 0, types);
  488.  
  489. /* from xlisp begin */ 
  490.     /* check for a buffered character */ 
  491.     event_flag = FALSE;
  492.     if (lcount--) 
  493.     {
  494.        unify_real_result (
  495.        args[0], (REAL) lbuf[lindex++]);
  496.        char_flag = TRUE;
  497.        success=TRUE;
  498.     } 
  499.     else
  500.     {
  501.     /* get an input line */ 
  502.     level = (unsigned long) aim->c;
  503.     for (lcount = 0; ; ) 
  504.     switch (ch = xgetc()) { 
  505.     case '\r':
  506.         case '\n':
  507.         case '\020': 
  508.         lbuf[lcount++] = '\n'; 
  509.         xputc('\r'); xputc('\n'); lposition = 0;\
  510.                 fflush(stdout); 
  511.           lindex = 0; lcount--; 
  512.                 unify_real_result (
  513.                 args[0], (REAL) lbuf[lindex++]);
  514.                 char_flag = TRUE;
  515.                 success=TRUE;
  516.         break;
  517.     case '\010': 
  518.     case '\177': 
  519.         if (lcount) { 
  520.             lcount--; 
  521.             while (lposition > lpos[lcount]) { 
  522.             xputc('\010'); xputc(' '); xputc('\010'); 
  523.                         fflush(stdout); 
  524.             lposition--; 
  525.             } 
  526.         } 
  527.         break; 
  528.     case '\032': 
  529.                unify_real_result (
  530.                args[0], (REAL) ch);
  531.                char_flag = FALSE;
  532.                success = FALSE;
  533.                break;
  534.     default: 
  535.         if (ch == '\t' || (ch >= 0x20 && ch < 0x7F)) { 
  536.             lbuf[lcount] = ch; 
  537.             lpos[lcount] = lposition; 
  538.             if (ch == '\t') 
  539.             do { 
  540.                 xputc(' '); 
  541.                             fflush(stdout); 
  542.             } while (++lposition & 7); 
  543.             else { 
  544.             xputc(ch); lposition++; 
  545.                         fflush(stdout); 
  546.             } 
  547.             lcount++; 
  548.         } 
  549.         else { 
  550.             switch (ch) { 
  551.             case '\003':    interrupt();    /* control-c */ 
  552.             case '\032':    /* control-z */ 
  553.                            event_flag = TRUE;
  554.                            success = FALSE;
  555.                            break;
  556.             default:        /*return (ch);*/ 
  557.                            {
  558.                            fflush(stdout); 
  559.                        unify_real_result (
  560.                            args[0], (REAL) ch);
  561.                        char_flag = TRUE;
  562.                            success = TRUE;
  563.                            break;
  564.                            }
  565.             } 
  566.         } 
  567.     } 
  568.  
  569. /* from xlisp eend */
  570.  
  571.     unify_bool_result (args[1], event_flag);
  572.     end_builtin ();
  573. }
  574.  
  575.  
  576. /*****************************************************************/
  577. /******** PutRaw
  578.  
  579.   c_put_raw (+Char)
  580.  
  581.   write the specified char on the standard output
  582.  
  583.   this built-in should be used only by the life-shell of Kathleen.
  584.  
  585.  */
  586.  
  587. long c_put_raw ()
  588. {
  589.     int ch;
  590.     include_var_builtin (1);
  591.  
  592.  
  593.     ptr_definition types[1];
  594.  
  595.     types[0] = real;
  596.    
  597.     begin_builtin (c_put_raw, 1, 0, types);
  598.     ch = val[0];
  599.     /* output the character */ 
  600.     if (ch == '\n') { 
  601.     xputc('\r'); xputc('\n'); 
  602. /*    lposition = 0; */
  603.     } 
  604.     else { 
  605.     xputc(ch); 
  606. /*    lposition++;  */
  607.    } 
  608.  
  609.     fflush (stdout);
  610.     success = TRUE;
  611.     end_builtin ();
  612. }
  613.  
  614.  
  615. /*****************************************************************/
  616. /******** EndRaw
  617.  
  618.   end_raw ()
  619.  
  620.   reset the keyboard in the previous state before xcInitModeRaw.
  621.  
  622.   this built-in should be used only by the life-shell of Kathleen.
  623.  
  624.  */
  625.  
  626. long c_end_raw ()
  627.  
  628. {
  629.     if (!mode_raw)
  630.     {
  631.         Errorline ("in c_end_raw: not in mode raw\n");
  632.         return FALSE;
  633.     }
  634.     mode_raw = FALSE;
  635.     return TRUE;
  636. }
  637.  
  638.  
  639. /*****************************************************************/
  640. /******** InRaw
  641.  
  642.   in_raw ()
  643.  
  644.   return TRUE if mode raw 
  645.  
  646.   this built-in should be used only by the life-shell of Kathleen.
  647.  
  648.  */
  649.  
  650. long c_in_raw ()
  651.  
  652. {
  653.     deref_ptr (aim->a);
  654.     unify_bool_result (aim->b, mode_raw);
  655.  
  656.     return TRUE;
  657. }
  658.  
  659.  
  660. /*****************************************************************/
  661. /******** WindowFlag
  662.  
  663.   window_flag ()
  664.  
  665.   return TRUE if a window has been created
  666.  
  667.   this built-in should be used only by the life-shell of Kathleen.
  668.  
  669.  */
  670.  
  671. long c_window_flag ()
  672.  
  673. {
  674.     deref_ptr (aim->a);
  675.     unify_bool_result (aim->b, FALSE);
  676.  
  677.     return TRUE;
  678. }
  679.  
  680.  
  681. /*****************************************************************/
  682. /******** ResetWindowFlag
  683.  
  684.   reset_window_flag ()
  685.  
  686.   return the flag x_window_creation
  687.  
  688.   this built-in should be used only by the life-shell of Kathleen.
  689.  
  690.  */
  691.  
  692. long c_reset_window_flag ()
  693.  
  694. {
  695.     deref_ptr (aim->a);
  696.  
  697.     return TRUE;
  698. }
  699.  
  700.  
  701. #endif
  702.  
  703. /*****************************************/
  704.  
  705. /* set up the built-ins for the mode raw */
  706.  
  707. void raw_setup_builtins ()
  708.      
  709. {
  710. #ifndef NORAW  
  711.     new_built_in(bi_module,"begin_raw",         predicate, &c_begin_raw);
  712.     new_built_in(bi_module,"get_raw",           predicate, &c_get_raw);
  713.     new_built_in(bi_module,"put_raw",           predicate, &c_put_raw);
  714.     new_built_in(bi_module,"end_raw",           predicate, &c_end_raw);
  715.     new_built_in(bi_module,"in_raw",            function,  &c_in_raw);
  716.     new_built_in(bi_module,"window_flag",       function,  &c_window_flag);
  717.     new_built_in(bi_module,"reset_window_flag", predicate, &c_reset_window_flag);
  718. #endif
  719. }
  720. #endif
  721.