home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / pros2io.c < prev    next >
C/C++ Source or Header  |  2000-12-05  |  7KB  |  204 lines

  1. /* -*-C-*-
  2.  
  3. $Id: pros2io.c,v 1.9 2000/12/05 21:23:47 cph Exp $
  4.  
  5. Copyright (c) 1994-2000 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "scheme.h"
  23. #include "prims.h"
  24. #include "os2.h"
  25. #include "os2proc.h"
  26.  
  27. extern qid_t OS2_channel_thread_descriptor (Tchannel);
  28.  
  29. DEFINE_PRIMITIVE ("OS2-SELECT-REGISTRY-LUB", Prim_OS2_select_registry_lub, 0, 0, 0)
  30. {
  31.   PRIMITIVE_HEADER (0);
  32.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (QID_MAX + 1));
  33. }
  34.  
  35. DEFINE_PRIMITIVE ("CHANNEL-DESCRIPTOR", Prim_channel_descriptor, 1, 1, 0)
  36. {
  37.   PRIMITIVE_HEADER (1);
  38.   {
  39.     Tchannel channel = (arg_channel (1));
  40.     if (! ((CHANNEL_ABSTRACT_P (channel)) && (CHANNEL_INPUTP (channel))))
  41.       error_bad_range_arg (1);
  42.     PRIMITIVE_RETURN
  43.       (LONG_TO_UNSIGNED_FIXNUM (OS2_channel_thread_descriptor (channel)));
  44.   }
  45. }
  46.  
  47. static qid_t
  48. arg_qid (int arg_number)
  49. {
  50.   unsigned int qid = (arg_index_integer (arg_number, (QID_MAX + 1)));
  51.   if (!OS2_qid_openp (qid))
  52.     error_bad_range_arg (arg_number);
  53.   return (qid);
  54. }
  55.  
  56. DEFINE_PRIMITIVE ("OS2-SELECT-DESCRIPTOR", Prim_OS2_select_descriptor, 2, 2, 0)
  57. {
  58.   PRIMITIVE_HEADER (2);
  59.   switch (OS2_message_availablep ((arg_qid (1)), (BOOLEAN_ARG (2))))
  60.     {
  61.     case mat_available:
  62.       PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (0));
  63.     case mat_not_available:
  64.       PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (1));
  65.     case mat_interrupt:
  66.       if (OS_process_any_status_change ())
  67.     PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (3));
  68.       else
  69.     PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (2));
  70.     default:
  71.       error_external_return ();
  72.       PRIMITIVE_RETURN (UNSPECIFIC);
  73.     }
  74. }
  75.  
  76. DEFINE_PRIMITIVE ("OS2-SELECT-REGISTRY-TEST", Prim_OS2_select_registry_test, 3, 3, 0)
  77. {
  78.   PRIMITIVE_HEADER (3);
  79.   CHECK_ARG (1, STRING_P);
  80.   if ((STRING_LENGTH (ARG_REF (1))) != (QID_MAX + 1))
  81.     error_bad_range_arg (1);
  82.   CHECK_ARG (2, STRING_P);
  83.   if ((STRING_LENGTH (ARG_REF (1))) != (QID_MAX + 1))
  84.     error_bad_range_arg (2);
  85.   {
  86.     char * registry = (STRING_LOC ((ARG_REF (1)), 0));
  87.     char * results = (STRING_LOC ((ARG_REF (2)), 0));
  88.     int blockp = (BOOLEAN_ARG (3));
  89.     int inputp = 0;
  90.     int interruptp = 0;
  91.     qid_t qid;
  92.     int n;
  93.  
  94.     /* This first phase checks the qid subqueues and OS2_scheme_tqueue
  95.        for any previously-queued input.  */
  96.   check_for_input:
  97.     for (qid = 0; (qid <= QID_MAX); qid += 1)
  98.       {
  99.     (results [qid]) = 0;
  100.     if ((registry [qid]) != 0)
  101.       switch (OS2_message_availablep (qid, 0))
  102.         {
  103.         case mat_available:
  104.           inputp = 1;
  105.           (results [qid]) = 1;
  106.           break;
  107.         case mat_interrupt:
  108.           interruptp = 1;
  109.           break;
  110.         }
  111.       }
  112.     /* This second phase waits for input if necessary.  It does not
  113.        check the subqueues for previously-stored data, so it's
  114.        important that we already did this.  Otherwise we could end up
  115.        waiting for input when there was valid input ready.  */
  116.     if (blockp)
  117.       while (! (inputp || interruptp))
  118.     {
  119.       for (qid = 0; (qid <= QID_MAX); qid += 1)
  120.         (OS2_scheme_tqueue_avail_map [qid]) = 0;
  121.       n = (OS2_tqueue_select (OS2_scheme_tqueue, blockp));
  122.       if (n == (-1))
  123.         /* If we're unblocked and there's no message in the
  124.            tqueue, go back and check for input again.  */
  125.         goto check_for_input;
  126.       if (n < 0)
  127.         interruptp = 1;
  128.       else
  129.         for (qid = 0; (qid <= QID_MAX); qid += 1)
  130.           if (((registry [qid]) != 0)
  131.           && (OS2_scheme_tqueue_avail_map [qid]))
  132.         {
  133.           inputp = 1;
  134.           (results [qid]) = 1;
  135.         }
  136.     }
  137.     if (inputp)
  138.       PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (0));
  139.     else if (!interruptp)
  140.       PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (1));
  141.     else if (!OS_process_any_status_change ())
  142.       PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (2));
  143.     else
  144.       PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (3));
  145.   }
  146. }
  147.  
  148. #define PROCESS_CHANNEL_ARG(arg, type, channel)                \
  149. {                                    \
  150.   if ((ARG_REF (arg)) == SHARP_F)                    \
  151.     (type) = process_channel_type_none;                    \
  152.   else if ((ARG_REF (arg)) == (LONG_TO_FIXNUM (-1)))            \
  153.     (type) = process_channel_type_inherit;                \
  154.   else                                    \
  155.     {                                    \
  156.       (type) = process_channel_type_explicit;                \
  157.       (channel) = (arg_channel (arg));                    \
  158.     }                                    \
  159. }
  160.  
  161. DEFINE_PRIMITIVE ("OS2-MAKE-SUBPROCESS", Prim_OS2_make_subprocess, 7, 7,
  162.   "(FILENAME CMD-LINE ENV WORK-DIR STDIN STDOUT STDERR)\n\
  163. Create a subprocess.\n\
  164. FILENAME is the program to run.\n\
  165. CMD-LINE a string containing the program's invocation.\n\
  166. ENV is a string to pass as the program's environment;\n\
  167.   #F means inherit Scheme's environment.\n\
  168. WORK-DIR is a string to pass as the program's working directory;\n\
  169.   #F means inherit Scheme's working directory.\n\
  170. STDIN is the input channel for the subprocess.\n\
  171. STDOUT is the output channel for the subprocess.\n\
  172. STDERR is the error channel for the subprocess.\n\
  173.   Each channel arg can take these values:\n\
  174.   #F means none;\n\
  175.   -1 means use the corresponding channel from Scheme;\n\
  176.   otherwise the argument must be a channel.")
  177. {
  178.   PRIMITIVE_HEADER (7);
  179.   {
  180.     const char * filename = (STRING_ARG (1));
  181.     const char * command_line = (STRING_ARG (2));
  182.     const char * env = (((ARG_REF (3)) == SHARP_F) ? 0 : (STRING_ARG (3)));
  183.     const char * working_directory
  184.       = (((ARG_REF (4)) == SHARP_F) ? 0 : (STRING_ARG (4)));
  185.     enum process_channel_type channel_in_type;
  186.     Tchannel channel_in;
  187.     enum process_channel_type channel_out_type;
  188.     Tchannel channel_out;
  189.     enum process_channel_type channel_err_type;
  190.     Tchannel channel_err;
  191.  
  192.     PROCESS_CHANNEL_ARG (5, channel_in_type, channel_in);
  193.     PROCESS_CHANNEL_ARG (6, channel_out_type, channel_out);
  194.     PROCESS_CHANNEL_ARG (7, channel_err_type, channel_err);
  195.     PRIMITIVE_RETURN
  196.       (long_to_integer
  197.        (OS2_make_subprocess
  198.     (filename, command_line, env, working_directory,
  199.      channel_in_type, channel_in,
  200.      channel_out_type, channel_out,
  201.      channel_err_type, channel_err)));
  202.   }
  203. }
  204.