home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / Python / Source / Embed / AmigaPythonEmbed.c next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  3.5 KB  |  178 lines

  1.  
  2. /* AmigaPython embedded interpreter init and cleanup code. */
  3. /* Made 19 October 1999 by Irmen de Jong */
  4. /* Based on AmigaPython specific changes in Modules/main.c */
  5.  
  6. /* THE main exported function from this module is:   */
  7.  
  8. /*    void InitAmigaPython(int argc, char **argv)    */
  9.  
  10. /* which should be called even before Py_Initialize. */
  11.  
  12.  
  13.  
  14. #include <proto/dos.h>
  15. #include <proto/exec.h>
  16. #include "Python.h"
  17.  
  18.  
  19. /* For Py_GetArgcArgv(); set by Init */
  20. static char **orig_argv;
  21. static int  orig_argc;
  22.  
  23.  
  24. #ifdef INET225
  25. #include <proto/socket.h>
  26.  
  27. struct Library *SockBase = NULL;
  28.  
  29. int checksocketlib(void)
  30. {
  31.     if(!SockBase)
  32.     {
  33.         if(SockBase=OpenLibrary("inet:libs/socket.library",4))
  34.         {
  35.             setup_sockets(FD_SETSIZE, &errno);
  36.         }
  37.         else
  38.         {
  39.             PyErr_SetString(PyExc_SystemError, "Couldn't open inet:libs/socket.library V4+ (I-Net225 started?)");
  40.             return 0;
  41.         }
  42.     }
  43.     return 1;
  44. }
  45.  
  46. int checkusergrouplib(void)
  47. {
  48.     return checksocketlib();
  49. }
  50.  
  51. #endif /* INET225 */
  52.  
  53. #ifdef AMITCP
  54. #include <proto/socket.h>
  55. #include <amitcp/socketbasetags.h>
  56.  
  57. /* proto for special AmiTCP utility funcion; see _chkufb.c */
  58. extern long _install_AmiTCP_callback(void);
  59.  
  60.  
  61. /* global h_errno */
  62. int h_errno = 0;
  63.  
  64. struct Library *UserGroupBase = NULL;
  65. struct Library *SocketBase = NULL;
  66.  
  67. int checkusergrouplib(void)
  68. {
  69.     if(!UserGroupBase)
  70.     {
  71.         if(!(UserGroupBase=OpenLibrary("usergroup.library",4)))
  72.         {
  73.             PyErr_SetString(PyExc_SystemError, "Couldn't open usergroup.library");
  74.             return 0;
  75.         }
  76.     }
  77.     return 1;
  78. }
  79.  
  80. int checksocketlib(void)
  81. {
  82.     if(!SocketBase)
  83.     {
  84.         if(SocketBase=OpenLibrary("bsdsocket.library",4))
  85.         {
  86.             /*
  87.              * Succesfull. Now tell bsdsocket.library:
  88.              * - the address of our errno
  89.              * - the address of our h_errno
  90.              * - our program name
  91.              */
  92.             SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), &errno,
  93.                            SBTM_SETVAL(SBTC_HERRNOLONGPTR), &h_errno,
  94.                            SBTM_SETVAL(SBTC_LOGTAGPTR), "Python",
  95.                            TAG_END);
  96.         }
  97.         else
  98.         {
  99.             PyErr_SetString(PyExc_SystemError, "Couldn't open bsdsocket.library (start AmiTCP)");
  100.             return 0;
  101.         }
  102.     }
  103.     return 1;
  104. }
  105.  
  106. #endif
  107.  
  108. BOOL from_WB = FALSE;    /* not static! getpath.c needs it! */
  109.  
  110. static void AmigaCleanup(void)
  111. {
  112. #ifdef AMITCP
  113.     if(UserGroupBase)
  114.     {
  115.         CloseLibrary(UserGroupBase);
  116.         UserGroupBase=NULL;
  117.     }
  118.     if(SocketBase)
  119.     {
  120.         CloseLibrary(SocketBase);
  121.         SocketBase=NULL;
  122.     }
  123. #endif
  124. #ifdef INET225
  125.     if(SockBase)
  126.     {
  127.         cleanup_sockets();
  128.         CloseLibrary(SockBase);
  129.         SockBase = NULL;
  130.     }
  131. #endif
  132.     if(from_WB) Delay(112); // small exit delay before closing WB window
  133. }
  134.  
  135.  
  136. /* Init routine for embedded applications */
  137.  
  138. void InitAmigaPython(int argc, char **argv)
  139. {
  140.     if(argc == 0)
  141.     {
  142.         /* Invoked from WorkBench... use WorkBench arguments  */
  143.         /* Make sure <dos.h> was included earlier in order to */
  144.         /* declare _WBArgc and _WBArgv.                       */
  145.         argc = _WBArgc;
  146.         argv = _WBArgv;
  147.         from_WB = TRUE;
  148.     }
  149.  
  150.     atexit(AmigaCleanup);   /* cleanup func */
  151. //  setbuf(stderr,NULL);    /* set error output UNBUFFERED */
  152.  
  153.     orig_argc = argc;    /* For Py_GetArgcArgv() */
  154.     orig_argv = argv;
  155.  
  156.     Py_SetProgramName(argv[0]);
  157.  
  158. #ifdef AMITCP
  159.     /** Sadly, this function cannot be called as a SAS/C standard **/
  160.     /** constructor function. It needs to have the interpreter **/
  161.     /** initialised because it uses the Python Error functions... **/
  162.     (void)_install_AmiTCP_callback();
  163. #endif
  164.  
  165. }
  166.  
  167.  
  168. /* Make the *original* argc/argv available to other modules.
  169.    This is rare, but it is needed by the secureware extension. */
  170.  
  171. void Py_GetArgcArgv(int *argc,  char ***argv)
  172. {
  173.     *argc = orig_argc;
  174.     *argv = orig_argv;
  175. }
  176.  
  177.  
  178.